Gadget String Sizing

Share your advanced PureBasic knowledge/code with the community.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Gadget String Sizing

Post by electrochrisso »

Not sure if anyone has done it this way before.
It should be a cross platform way to do it.
Needs a few tweaks to be made for padding.

Code: Select all

;
; Gadget String Sizing by Electrochrisso 2007
;

ButtonText$="Hot Topics For Today"
FontName$="Tahoma"
FontSize=20
ButtonWidth=200

If OpenWindow(0,100,100,320,200,"Gadget String Sizing") And CreateGadgetList(WindowID(0))

  ; Fit Gadget to Font Size

  LoadFont(0,FontName$,FontSize)
  CreateImage(0,8,8)
  StartDrawing(ImageOutput(0))
    DrawingFont(FontID(0))
    tw=TextWidth(ButtonText$)
    th=TextHeight(ButtonText$)
  StopDrawing()
  ButtonGadget(0,10,10,tw+12,th+6,ButtonText$)
  SetGadgetFont(0,FontID(0))

  ; Fit Font to Gadget Size

ButtonGadget(1,10,GadgetY(0)+GadgetHeight(0)+10,ButtonWidth,30,ButtonText$)
  For FontSize=20 To 8 Step -2
    LoadFont(1,FontName$,FontSize)
    StartDrawing(ImageOutput(0))
      DrawingFont(FontID(1))
      tw=TextWidth(ButtonText$)
      th=TextHeight(ButtonText$)
    StopDrawing()
    If tw+12<ButtonWidth:Break:EndIf
  Next
  ResizeGadget(1,#PB_Ignore,#PB_Ignore,#PB_Ignore,th+6) 
  SetGadgetFont(1,FontID(1))
  FreeImage(0)

  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
[/code]