Change font style of text in StringGadget

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Change font style of text in StringGadget

Post by Shardik »

I already demonstrated how to change the font style of text in the
ListIconGadget and TextGadget:
http://www.purebasic.fr/english/viewtop ... 16&start=1

In the StringGadget this is possible too, but it's a bit different. While
in the other 2 Gadgets you can combine the flag bits (for example
instead 1 for bold and 2 for Italic you can use the value 3), in the
StringGadget at first you have to reset the setting with 0 and then
you have to activate each single feature in succession. Furthermore
there are 2 additional flags available: one for compressed text and
another for wide text.

Code: Select all

EnableExplicit

ImportC ""
  SetControlFontStyle(ControlRef.L, *ControlFontStyleRec)
EndImport

#kControlUseFaceMask = $0002

Structure RGBColor
  Red.U
  Green.U
  Blue.U
EndStructure

Structure ControlFontStyleRec
  Flags.W
  Font.W
  Size.W
  Style.W
  Mode.W
  Just.W
  ForeColor.RGBColor
  BackColor.RGBColor
EndStructure

Procedure ChangeFontStyle(Style.W)
  Protected FontStyle.ControlFontStyleRec
  Protected i.L

  ; ----- Font style bits
  ; Bit 0: Bold
  ; Bit 1: Italic
  ; Bit 2: Underline
  ; Bit 5: Compressed
  ; Bit 6: Wide

  ; ----- Reset all styles

  FontStyle\Flags = #kControlUseFaceMask
  FontStyle\Style = 0
  SetControlFontStyle(GadgetID(0), @FontStyle)

  ; ----- Enable each style with its bit set

  For i = 0 To 6
    If Style & (1 << i)
      FontStyle\Style = Style & (1 << i)
      SetControlFontStyle(GadgetID(0), @FontStyle)
    EndIf
  Next i
EndProcedure

Define GadgetNumber.L
Define i.L
Define Style.W

OpenWindow(0, 200, 100, 250, 420, "Change font style + type")
StringGadget(0, 65, 23, 110, 22, "StringGadget")
Frame3DGadget(1, 20, 60, 210, 221, "Font style:")
OptionGadget(2, 30, 80, 190, 18, "Standard")
OptionGadget(3, 30, 105, 190, 18, "Bold")
OptionGadget(4, 30, 130, 190, 18, "Italic")
OptionGadget(5, 30, 155, 190, 18, "Bold + Italic")
OptionGadget(6, 30, 180, 190, 18, "Underlined")
OptionGadget(7, 30, 205, 190, 18, "Bold + Underlined")
OptionGadget(8, 30, 230, 190, 18, "Italic + Underlined")
OptionGadget(9, 30, 255, 190, 18, "Bold + Italic + Underlined")
SetGadgetState(2, #True)
Frame3DGadget(10, 20, 300, 210, 96, "Font type:")
OptionGadget(11, 30, 320, 200, 20, "Normal")
OptionGadget(12, 30, 345, 200, 20, "Compressed")
OptionGadget(13, 30, 370, 200, 20, "Wide")
SetGadgetState(11, #True)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventType() = #PB_EventType_LeftClick
        GadgetNumber = EventGadget()

        If GadgetNumber >= 1 And GadgetNumber <= 13 
          Select GadgetNumber
            Case 2 To 9
              Style = GadgetNumber - 2
            Case 11 To 13
              For i = 2 To 9
                If GetGadgetState(i)
                  Style = i - 2
                  Break
                EndIf
              Next i
          EndSelect
          
          If GetGadgetState(12)
            Style | 32
          ElseIf GetGadgetState(13)
            Style | 64
          EndIf
          
          ChangeFontStyle(Style)
        EndIf
      EndIf
  EndSelect
ForEver