Dynamical button

Share your advanced PureBasic knowledge/code with the community.
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Dynamical button

Post by Character »

Code updated for 5.20+

I found this code when cleaning up my computer.
Maybe somebody can use it.

Code: Select all

; Dynamical button example by Character - 2006.
; Don't pay attention to the mess.
; Most of it is caused by the Visual Designer (v3.95).  ;o)


;- Window Constants
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
Enumeration
  #Button_0
  #Button_1
  #Button_2 
  #Frame3D_0
  #String_0
  #Combo_0
EndEnumeration

;- Fonts
Global FontID0
FontID0 = #PB_Default
Global FontID1
FontID1 = LoadFont(1, "Comic Sans MS", 12, #PB_Font_Bold)
Global FontID2
FontID2 = LoadFont(2, "Verdana", 20, #PB_Font_Bold)
Global FontID3
FontID3 = LoadFont(3, "Verdana", 16)
Global FontID4
FontID4 = LoadFont(4, "Century Gothic", 48, #PB_Font_Bold)

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 255, 291, 600, 165, "Dynamical button stuff...",  #PB_Window_SystemMenu | #PB_Window_TitleBar )
    ButtonGadget(#Button_0, 10, 10, 70, 20, "Change me!")
    ButtonGadget(#Button_1, 390, -30, 20, 30, "")
    FrameGadget(#Frame3D_0, 5, 5, 590, 130, "", #PB_Frame_Double)
    ButtonGadget(#Button_2, 395, 140, 200, 20, "Change him!")
    StringGadget(#String_0, 5, 140, 205, 20, "")
    ComboBoxGadget(#Combo_0, 215, 140, 175, 200)
    AddGadgetItem(#Combo_0, -1, "Default font")
    AddGadgetItem(#Combo_0, -1, "Comic Sans MS 12 Bold")
    AddGadgetItem(#Combo_0, -1, "Verdana 20 Bold")
    AddGadgetItem(#Combo_0, -1, "Verdana 16")
    AddGadgetItem(#Combo_0, -1, "Century Gothic 28 Bold")   
    SetGadgetState(#Combo_0, 0)
  EndIf
EndProcedure


Open_Window_0()


Repeat
 
  Event = WaitWindowEvent()
  GadgetID = EventGadget()

  If Event = #PB_Event_Gadget
   
    If GadgetID = #Button_0
   
      If Caption.s = "": Caption="Change me!":EndIf
      MessageRequester(Caption.s, "Width: " + Str(DynamicalWidth.w) + Chr(13) + "Height: " + Str(DynamicalHeight.w) + Chr(13) + "Font: " + Str(FontLabel))
     
    ElseIf GadgetID = #Button_1

    ElseIf GadgetID = #Button_2
   
      Select FontLabel
        Case 0
          FontID = FontID0
        Case 1
          FontID = FontID1
        Case 2
          FontID = FontID2
        Case 3
          FontID = FontID3
        Case 4
          FontID = FontID4               
      EndSelect
     
      If CreateImage(0, 1, 1)
        StartDrawing(ImageOutput(#Window_0))
          DrawingFont(FontID)
          DynamicalWidth.w = TextWidth(Caption.s)+10
          DynamicalHeight.w = TextHeight(Caption.s)+4
        StopDrawing()
      EndIf 
 
      If DynamicalWidth.w >10 And DynamicalWidth.w < 590
        ButtonGadget(#Button_0, 10, 10, DynamicalWidth.w, DynamicalHeight.w, Caption.s)
        SetGadgetFont(#Button_0, FontID)
      Else
        MessageRequester("OUT OF RANGE", "Size matters!" + Chr(13) + Chr(13) + "A. No button caption in the stringgadget." + Chr(13)+ Chr(13) + "B. You think too big.")
      EndIf
   
    ElseIf GadgetID = #String_0
   
      Caption.s = GetGadgetText(#String_0)
     
    ElseIf GadgetID = #Combo_0
   
      FontLabel = GetGadgetState(#Combo_0)
           
    EndIf
   
  EndIf
 
Until Event = #PB_Event_CloseWindow
End
Cessante causa cessat effectus