is there a need for a smart eventhandler include for PB?
I've written such a eventhandler for quite some time for my own use and made the code and documentation public in the german forum. It reduce the administrative expenses in the field of event processing.
As I've recently made an update of the german forum thread, I've noticed that I've not introduced the event handler in the english forum.
Unfortunately, all the descriptions are in german language and I'm not able to do an adequate translation to english
(this post is also partly translated by google translate - I'm sure you realized that already

If there is interest in the include code, maybe we found someone who can do the translation.
In this post I once refer to the german thread and give you a short description of the eventhandler commands.
Thread in the german forum: here
Download sourcecode and example code: here
Short description:
1)
After creating your GUI and gadgets you have to assign the needed gadget events to your eventprocedures.
The commands to do this are:
evh_SetWindowEvent(iWindowNr, iEventType, *EventProcedure)
evh_SetMenuEvent (iWindowNr, iMenuitemNr, *EventProcedure)
evh_SetGadgetEvent(iWindowNr, iGadgetNr, iEventType, *EventProcedure)
Example:
; Gadgets
evh_SetGadgetEvent(Window_Main, Window_Main_Button(1), #PB_EventType_LeftClick, @Button1Click())
evh_SetGadgetEvent(Window_Main, Window_Main_Button(2), #PB_EventType_LeftClick, @Button2Click())
; Menus
evh_SetMenuEvent(Window_Main, #MENUPUNKT1, @Menu1())
evh_SetMenuEvent(Window_Main, #MENUPUNKT2, @Menu2())
So each time you click on Window_Main_Button(1) your procedure Button1Click() will be called.
No need to call WaitWindowEvent() in your main programm and no need to check all the events using a big Select-Case-tree in your mainloop.
2)
Your eventprocedures must have one predefined structured parameter:
*stEventArgs.evh_EventArgsstruct
This is a pointer to a structure (predefined by the eventhandler) which contains the following entries:
\iEvent
\iEventType
\iEventWindow
\iEventGadget
\iEventMenu
\iEventTimer
\iEventwParam
\iEventlParam
Example:
Procedure Button1Click(*stEventArgs.evh_EventArgsstruct)
Debug "Button 1 clicked"
EndProcedure
So your Eventprocedure have access to all the relevant data to process the event.
The structure is automatically filled by the event handler and holds the values which are returned by the PB commands WaitWindowEvent(), EventType(), EventWindow() and so on.
3)
The eventloop in your main code is now reduced to this three commands:
While evh_ProcessEvents(25, MainWindow, @OwnEventproc(), 0, 0)
Wend
So you have to call only this one command in your eventloop:
evh_ProcessEvents(iTimeOut, iObserveWindow, [*OwnEventHandler], [*stEvents])
The eventloop will run as long as the window "iObserveWindow" do not receive an CloseWindow event.
You have some more options with this include wich are described in detail in the german forum - sorry for my inadequate english skill.
Here is an short democode. It shows how to use nearly all options of the include in a real condition:
Code: Select all
EnableExplicit
;*************************************************************************
;* Includefiles
;*************************************************************************
IncludeFile "Eventhandler.pb"
;*************************************************************************
;* Variables
;*************************************************************************
Global Window_Main.i, Window_Options.i
Global Dim Window_Main_Buttons.i(10)
Enumeration
#MENUPUNKT1
#MENUPUNKT2
EndEnumeration
;*************************************************************************
;* Eventprocddures
;*************************************************************************
Procedure Button1Click(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Button 1 geklickt")
EndProcedure
Procedure Button2Click(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Button 2 geklickt")
EndProcedure
Procedure Button4Click(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Button 4 geklickt")
EndProcedure
Procedure Menu1(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Menüpunkt 1 ausgewählt")
EndProcedure
Procedure Menu2(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Menüpunkt 2 ausgewählt")
EndProcedure
Procedure Window1Timer(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Timeraufruf erfolgte. Timer Nr. " + Str(*stEventArgs\iEventTimer))
EndProcedure
Procedure Window1Activate(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Das Fenster Nr 1 wurde aktiviert")
EndProcedure
Procedure Window2Activate(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Das Fenster Nr 2 wurde aktiviert")
EndProcedure
Procedure Window2Gadget(*stEventArgs.evh_EventArgsstruct)
AddGadgetItem(0, 0, "Fenster Nr 2, Buttonhandle:" + Str(GadgetID(*stEventArgs\iEventGadget)))
EndProcedure
Procedure CloseMyWindow(*stEventArgs.evh_EventArgsstruct)
; Schließen des zweiten Fensters
If IsWindow(*stEventArgs\iEventWindow)
; Alle registrierten Events von Fenster 2 entfernen
evh_RemoveEvent(Window_Options)
CloseWindow(*stEventArgs\iEventWindow)
AddGadgetItem(0, 0, "Fenster 2 wurde geschlossen")
EndIf
EndProcedure
Procedure OwnEventproc(*stEventArgs.evh_EventArgsstruct)
; Button 3 wird über die eigene Eventauswertung bearbeitet
If *stEventArgs\iEvent = #PB_Event_Gadget And *stEventArgs\iEventGadget = Window_Main_Buttons(3) And *stEventArgs\iEventType = #PB_EventType_LeftClick
AddGadgetItem(0, 0, "Button 3 geklickt")
EndIf
EndProcedure
;*************************************************************************
;* Open Window
;*************************************************************************
Window_Main = OpenWindow(#PB_Any, 10, 10, 300, 500, "Ein 'Kurzer' Eventhandler ;-)", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_MaximizeGadget)
If Window_Main
; Ein Menü erzeugen
If CreateMenu(0, WindowID(Window_Main))
MenuTitle("Menüpunkte...")
MenuItem(#MENUPUNKT1, "Menüpunkt 1")
MenuItem(#MENUPUNKT2, "Menüpunkt 2")
EndIf
; Ein Textfeld für die Meldungen
EditorGadget(0, 10, 50, 280, 420, #PB_Editor_ReadOnly)
; Buttons erzeugen
Window_Main_Buttons(1) = ButtonGadget(#PB_Any, 100, 10, 50, 30, "Button 1")
Window_Main_Buttons(2) = ButtonGadget(#PB_Any, 160, 10, 50, 30, "Button 2")
Window_Main_Buttons(3) = ButtonGadget(#PB_Any, 220, 10, 50, 30, "Button 3")
; Hier werden den Gadgets/Menuitems/dem Window die Eventprozeduren zugeordnet
; Gadgets
evh_SetGadgetEvent(Window_Main, Window_Main_Buttons(1), #PB_EventType_LeftClick, @Button1Click())
evh_SetGadgetEvent(Window_Main, Window_Main_Buttons(2), #PB_EventType_LeftClick, @Button2Click())
; Menüs
evh_SetMenuEvent(Window_Main, #MENUPUNKT1, @Menu1())
evh_SetMenuEvent(Window_Main, #MENUPUNKT2, @Menu2())
; Windowevents
evh_SetWindowEvent(Window_Main, #PB_Event_ActivateWindow, @Window1Activate())
; Achtung! Eine Prozedur für alle WindowTimer. Die Timernummer kann aus der Eventstruktur gelesen werden
evh_SetWindowEvent(Window_Main, #PB_Event_Timer, @Window1Timer())
AddWindowTimer(Window_Main, 0, 3000)
EndIf
Window_Options = OpenWindow(#PB_Any, 320, 10, 150, 50, "Optionen", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_MaximizeGadget)
If Window_Options
; Buttons erzeugen
Window_Main_Buttons(4) = ButtonGadget(#PB_Any, 10, 10, 60, 30, "Button 4")
Window_Main_Buttons(5) = ButtonGadget(#PB_Any, 80, 10, 60, 30, "Button 5")
; Und die Eventprozeduren für Fensterevents zuordnen
evh_SetWindowEvent(Window_Options, #PB_Event_ActivateWindow, @Window2Activate())
evh_SetWindowEvent(Window_Options, #PB_Event_CloseWindow, @CloseMyWindow())
; Hiermit werden die Buttons 4 und 5 zusammen bearbeitet
evh_SetWindowEvent(Window_Options, #PB_Event_Gadget, @Window2Gadget())
EndIf
;*************************************************************************
;* Main code
;*************************************************************************
While evh_ProcessEvents(25, Window_Main, @OwnEventproc())
Wend
If IsWindow(Window_Options)
evh_RemoveEvent(Window_Options)
CloseWindow(Window_Options)
EndIf
If IsWindow(Window_Main)
evh_RemoveEvent(Window_Main)
CloseWindow(Window_Main)
EndIf
End

Each event puts a new line to the Editorgadget.