Mixed Fonts on a Single Button

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Mixed Fonts on a Single Button

Post by Randy Walker »

Code updated For 5.40+

Please contribute if you know alternate way to mix fonts on a single button. Hoping for a simpler way to do this, but this works for me. I haven't tested this for other types yet but this SetParent() approach should work with other gadget types, including ListIconGadget().

Code: Select all

; Sample code to show a single button sharing mixed fonts at the same time.
courier8.l = LoadFont(#PB_Default,"Courier",8)
arial8b.l = LoadFont(#PB_Default,"Arial",8,#PB_Font_Bold)
arial8.l=LoadFont(#PB_Default,"Arial",8)

HWND0 = OpenWindow(0,174,8,170,100,"Multi-Font Button",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If HWND0
  
  
  ; Setup font for main label.
  SetGadgetFont(#PB_Default,FontID(courier8))
  TextGadget(0,10,10,150,25,"Click the button.",#PB_Text_Center) 
  
  ; Setup main font for button.
  SetGadgetFont(#PB_Default,FontID(arial8b))
  buttonMode = ButtonGadget(1,25,40,120,28,"Bright              ",#PB_Text_Center)
  
  ; Setup alternate font for button.
  SetGadgetFont(#PB_Default,FontID(arial8))
  buttonAlt = TextGadget(2,60,6,50,15," >     Dim",#PB_Text_Center) 
  
  ; Here's the trick...
  SetParent_(buttonAlt,buttonMode) ; attach alternate label gadget to main button
  
  
  ; Swap and show "current mode in bold" and "alternate mode as normal".
  Repeat
    EventID = WaitWindowEvent()
    Select EventID
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            If Mode.b ;
              hBrush0 = CreateSolidBrush_(RGB(200,200,200))
              SetClassLong_(HWND0, #GCL_HBRBACKGROUND, hBrush0)
              InvalidateRect_(HWND0, #Null, #True)
              SetGadgetText(1, "Bright              ")
              SetGadgetText(2, " >     Dim")
              Mode.b = 0
            Else
              Debug "off"
              hBrush0 = CreateSolidBrush_(RGB(120,120,120))
              SetClassLong_(HWND0, #GCL_HBRBACKGROUND, hBrush0)
              InvalidateRect_(HWND0, #Null, #True)
              SetGadgetText(1, "Dim               ")
              SetGadgetText(2, ">   Bright")
              Mode.b = -1
            EndIf
        EndSelect
    EndSelect
  Until EventID = #PB_Event_CloseWindow
EndIf

DeleteObject_(hBrush0)  ; Cleanup

End
Last edited by Randy Walker on Tue Nov 17, 2015 7:14 am, edited 2 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Post by Randy Walker »

Code updated For 5.40+

Here it is agian but this time button fonts only toggle back and forth.

Code: Select all

; Sample to show font toggling on a single button.
courier8.l = LoadFont(#PB_Default,"Courier",8)
arial12.l = LoadFont(#PB_Default,"Arial",12)
roman12.l = LoadFont(#PB_Default,"Roman",12,#PB_Font_Bold)
HWND0 = OpenWindow(0,174,8,170,100,"Multi-Font Button",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
If HWND0
    
  ; Setup font for main label.
  SetGadgetFont(#PB_Default,FontID(courier8))
  TextGadget(0,10,10,150,25,"Click the button.",#PB_Text_Center)  
    
  ; Setup main font for button.
  SetGadgetFont(#PB_Default,FontID(arial12))
  buttonMode = ButtonGadget(1,25,40,120,25,"Main") 
    
  ; Setup alternate font for button.
  SetGadgetFont(#PB_Default,FontID(roman12))
  buttonAlt = TextGadget(2,5,4,110,15,"Alternate",#PB_Text_Center)  
    
  ; Here's the trick...
  SetParent_(buttonAlt,buttonMode) ; attach alternate label to button
  HideGadget(2,1)                 ; hide alternate label
  Mode.b = -1                    ; set mode flag to "normal"
  
  ; Swap and show "current mode in bold" and "alternate mode as normal".
  Repeat 
    EventID = WaitWindowEvent() 
    Select EventID 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 1
            If Mode.b ;
              HideGadget(2,0)
              Mode.b = 0
            Else
              Debug "off"
              HideGadget(2,1)
              Mode.b = -1
            EndIf
        EndSelect 
    EndSelect 
  Until EventID = #PB_Event_CloseWindow 
EndIf 

End
Last edited by Randy Walker on Wed Nov 18, 2015 9:37 pm, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Mixed Fonts on a Single Button

Post by Randy Walker »

New sample For 5.40+ (still using the "SetParent" method)

Just another sample:

Code: Select all

fontAri10B.l = LoadFont(#PB_Default,"Arial",10,#PB_Font_Bold)
Enumeration
  #MixedFontButton
  #SecondFont
EndEnumeration
  
If OpenWindow(0, 0, 0, 200, 60, "Mixed Font Toggle Button", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  SetGadgetFont(#PB_Default,FontID(fontAri10B.l))
  MFbutton.w = ButtonGadget(#MixedFontButton,    35, 10, 130, 25, "  Primary  ", #PB_Button_Right)
  SetGadgetFont(#PB_Default,#PB_Default)
  MF2nd.w = Frame3DGadget(#SecondFont,    -1, 6, 2, 9, "Alternate <")
  SetParent_(MF2nd,MFbutton)
  SetActiveGadget(#MixedFontButton)
  AltMode.b = 0
  Repeat
    _mess = WaitWindowEvent()
    Select _mess 
      Case #PB_Event_Gadget
        Select EventGadget()         ;- EventGadget()  --  Which button activated
          Case #MixedFontButton      ;/ Processing Mode -- Alternate/Primary
            SetActiveGadget(PrevFocus.l)
            If AltMode.b ; switch to "Primary"
              Debug "switch to Primary"
              SetGadgetText(#MixedFontButton, "  Primary  ")
              SetGadgetText(#SecondFont, "Alternate <")
              AltMode.b = 0
            Else          ; switch to "Alternate"
              Debug "switch to Alternate"
              SetGadgetText(#MixedFontButton, "Alternate  ")
              SetGadgetText(#SecondFont, "  Primary <")
              AltMode.b = -1
            EndIf
        EndSelect
    EndSelect
    ; Branch processing according to mode
    If AltMode  ; Do alternate mode stuff
      Debug "Alternate pocessing activities"
    Else        ; Do primary mode stuff
      Debug "Primary pocessing activities"
    EndIf
  Until _mess = #PB_Event_CloseWindow
EndIf
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Mixed Fonts on a Single Button

Post by kvitaliy »

Randy Walker wrote:New sample For 5.40+ (still using the "SetParent" method)
For 5.40 and Frame3DGadget ? :D
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Mixed Fonts on a Single Button

Post by Kwai chang caine »

Yes and in the second code "CreateGadgetList(WindowID(0))' :wink:

Else.... Thanks for for sharing, it's works well 8)
Since 2004 i have not see it :oops:
ImageThe happiness is a road...
Not a destination
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Mixed Fonts on a Single Button

Post by Randy Walker »

Thanks Kwai -- I removed the CreateGadgetList()
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply