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