Simple Toggle Button Tutorial

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Simple Toggle Button Tutorial

Post by Rook Zimbabwe »

Toggle buttons can be handled procedurally. I use this simple Procedure / Procedure CALL to handle my Toggle Buttons:

PB 4.2
Will work in PB4.3 if instructions in line 4 are followed

Code: Select all

; simple TOGGLE Tutorial
; by Rook ZImbabwe
; PB 4.2 final 
; should fly in PB 4.3 If line 92 And 104 are deleted or remmed out!
; initially constructed using the Visual Designer with the INCLUDE EVEN LOOP option
Enumeration
    #Window_0
EndEnumeration

Enumeration ; look at the way the controls are enumerated here... 
    #Button_2
    #Button_1
    #Button_0
    #Text_MESSAGE
    #Button_3
EndEnumeration ; later on we will loop through simply by using this order

Structure VisualDesignerGadgets
    Gadget.l
    EventFunction.l
EndStructure

Global NewList EventProcedures.VisualDesignerGadgets()
;-
Procedure SetSong(song)
    For whatbutton = #Button_2 To #Button_0 ; run down the constants... they are in this order in the ENUMERATION list
        pokedbutt = GetGadgetState(whatbutton) ; determine each button state
        If  pokedbutt = 1 ; 1 = TOGGLED
            SetGadgetState(whatbutton,0) ; SET IT TO 0 which is NOT SELECTED
        EndIf
    Next
    Debug "Gadgetselected: "+Str(song) ; just used for independant verification
    SetGadgetState(song,1) ; set gadget we just clicked on to state 1 (SELECTED)
    ; call something else or do something else
EndProcedure
;-
Procedure Text_MESSAGE_Event(Window, Event, Gadget, Type)
    ; Debug "#Text_MESSAGE"
    ; This is not really needed as user will not input text here
EndProcedure

Procedure Button_3_Event(Window, Event, Gadget, Type)
    Debug "#Button_3"
    For HJ = #Button_2 To #Button_0
        SetGadgetState(HJ, 0)
    Next
    SetGadgetText(#Text_MESSAGE, "All Button Gadgets POPPED!!!")
EndProcedure

Procedure Button_2_Event(Window, Event, Gadget, Type)
    Debug "#Button_2"
    SetSong(#Button_2) ; call our procedure with this value!
    SetGadgetText(#Text_MESSAGE, "Button_2 PICKED...")
EndProcedure

Procedure Button_1_Event(Window, Event, Gadget, Type)
    Debug "#Button_1"
    SetSong(#Button_1) ; call our procedure with this value!
    SetGadgetText(#Text_MESSAGE, "Button_1 PICKED...")
EndProcedure

Procedure Button_0_Event(Window, Event, Gadget, Type)
    Debug "#Button_0"
    SetSong(#Button_0) ; call our procedure with this value!
    SetGadgetText(#Text_MESSAGE, "Button_0 PICKED...")
EndProcedure
;-
Procedure RegisterGadgetEvent(Gadget, *Function)
    
    If IsGadget(Gadget)
        AddElement(EventProcedures())
        EventProcedures()\Gadget        = Gadget
        EventProcedures()\EventFunction = *Function
    EndIf
    
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
    
    ForEach EventProcedures()
        If EventProcedures()\Gadget = Gadget
            CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
            LastElement(EventProcedures())
        EndIf
    Next
    
EndProcedure
;-
Procedure Open_Window_0()
    
    If OpenWindow(#Window_0, 5, 5, 317, 158, "BUTTON POPPER 1",  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
        If CreateGadgetList(WindowID(#Window_0))
            ButtonGadget(#Button_0, 15, 15, 140, 45, "BUTTON 0",  #PB_Button_Toggle)
            RegisterGadgetEvent(#Button_0, @Button_0_Event())
            ButtonGadget(#Button_1, 160, 15, 140, 45, "BUTTON 1",  #PB_Button_Toggle)
            RegisterGadgetEvent(#Button_1, @Button_1_Event())
            ButtonGadget(#Button_2, 15, 65, 140, 45, "BUTTON 2",  #PB_Button_Toggle)
            RegisterGadgetEvent(#Button_2, @Button_2_Event())
            ButtonGadget(#Button_3, 160, 65, 140, 45, "POP THEM ALL")
            RegisterGadgetEvent(#Button_3, @Button_3_Event())
            TextGadget(#Text_MESSAGE, 5, 130, 305, 25, "", #PB_Text_Center | #PB_Text_Border)
            RegisterGadgetEvent(#Text_MESSAGE, @Text_MESSAGE_Event())
            
        EndIf
    EndIf
EndProcedure

Open_Window_0()

SetGadgetText(#Text_MESSAGE, "Press a BUTTON to POP!") 
; sets initial text value for our MESSAGE...
; note: This is SET after the window has been opened... 

Repeat ; simple loop that waits for us to select a gadget to operate on
    
    Event  = WaitWindowEvent()
    Gadget = EventGadget()
    Type   = EventType()
    Window = EventWindow()
    
    Select Event
    Case #PB_Event_Gadget
        CallEventFunction(Window, Event, Gadget, Type)
        
    EndSelect
    
Until Event = #PB_Event_CloseWindow
; Will loop until we press the [X] box on our window

End

Code is vastly commented for learning purposes.
Last edited by Rook Zimbabwe on Wed Oct 08, 2008 3:30 pm, edited 2 times in total.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Post by Tomi »

Hello
Thanksful for this code with learning stracture :)
it is usful for a newbie like i :D
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

You are welcome... thats why I did it. I won't pretend to be a PB Super API hooker/coder but I remember the things that gave me problems in the beginning.

As General Info for all:

Search is also your friend, but I like to TITLE my topics by what they are. That is why if you have a problem, don't title the topic simply HELP... detail the name of the command or issue that is giving you a problem so others can find it and see how you managed your way out of it! 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply