Page 1 of 1

Change the font in a editor gadged using a font requester

Posted: Mon Jun 19, 2006 8:13 pm
by Joakim Christiansen
Code updated For 5.20+

Just wanted to share ;)

Code: Select all

; --------------------------------------------------------
; About:
; Change the font in a editor gadget using a font requester
;
; Author:   Joakim L. Christiansen
; Homepage: http://www.myhome.no/jlc_software
; --------------------------------------------------------

EnableExplicit

Procedure Editor_FontName(Gadget,FontName.s)
  Protected format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_FACE
  PokeS(@format\szFaceName,FontName)
  SendMessage_(GadgetID(Gadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@format)
EndProcedure
Procedure Editor_FontSize(Gadget,Fontsize)
  Protected format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_SIZE
  format\yHeight = FontSize*20
  SendMessage_(GadgetID(Gadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@format)
EndProcedure
Procedure Editor_FontStyle(Gadget,Flags)
  Protected format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_ITALIC|#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE
  format\dwEffects = Flags
  SendMessage_(GadgetID(Gadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@format)
EndProcedure
Procedure Editor_FontColor(Gadget,Color)
  Protected format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_COLOR
  format\crTextColor = Color
  SendMessage_(GadgetID(Gadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@format)
EndProcedure

#Editor = 0
#Button = 1

OpenWindow(0,0,0,200,200,"Change font example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

EditorGadget(#Editor,0,0,200,180)
ButtonGadget(#Button,0,180,200,20,"Change font")

Global Font_Name.s = "Arial", Font_Size = 12, Font_Style, Font_Color

Procedure SetFont()
  Editor_FontName(#Editor,Font_Name)
  Editor_FontSize(#Editor,Font_Size)
  Editor_FontStyle(#Editor,Font_Style)
  Editor_FontColor(#Editor,Font_Color)
EndProcedure

SetFont()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button
          If FontRequester(Font_Name,Font_Size,#PB_FontRequester_Effects,Font_Color)
            Font_Name = SelectedFontName()
            Font_Size = SelectedFontSize()
            Font_Style = SelectedFontStyle()
            Font_Color = SelectedFontColor()
            
            If Font_Style < 256 ;normal
            ElseIf Font_Style < 512 ;bold
              Font_Style - 256
              Font_Style + 1
            ElseIf Font_Style < 768 ;italic
              Font_Style - 512
              Font_Style + 2
            ElseIf Font_Style >= 768 ;both
              Font_Style - 768
              Font_Style + 3
            EndIf
            
            SetFont()
            SetActiveGadget(#Editor)
          EndIf
      EndSelect
  EndSelect
ForEver
The style number returned by the requester is like this:
0 = normal
256 = bold
512 = italic
768 = bold italic

+ 4 = underline
+ 8 = strikeout
+ 12 = both