Page 1 of 5
IsValidPBEvent?[Resolved]
Posted: Sat Nov 14, 2015 8:04 am
by collectordave
Having started programming with PB I needed a way to reliably route messages to the correct message procedure for processing. Ran into some trouble using EventWindow(). After some lengthy discussion here it has been stated that EventWindow() is only valid after a valid PB event.
A good few examples of how to do this have been posted. Thanks to all. My way of overcoming this is to use the following procedure.
Code: Select all
Procedure IsValidPBEvent(event)
Select event
Case #PB_Event_Menu ,
#PB_Event_Gadget ,
#PB_Event_SysTray ,
#PB_Event_Timer ,
#PB_Event_CloseWindow ,
#PB_Event_Repaint ,
#PB_Event_SizeWindow ,
#PB_Event_MoveWindow ,
#PB_Event_MinimizeWindow ,
#PB_Event_MaximizeWindow ,
#PB_Event_RestoreWindow ,
#PB_Event_ActivateWindow ,
#PB_Event_DeactivateWindow,
#PB_Event_WindowDrop ,
#PB_Event_GadgetDrop ,
#PB_Event_RightClick ,
#PB_Event_LeftClick ,
#PB_Event_LeftDoubleClick
ProcedureReturn #True
Default
ProcedureReturn #False
EndSelect ;event
EndProcedure
If you would like to see how I incorporate this into a multiwindow program check this out:-
http://www.codeinbasic.com/index.php?topic=241.0
Re: IsValidPBEvent?
Posted: Sat Nov 14, 2015 9:05 pm
by TI-994A
collectordave wrote:My way of overcoming this is to use the following procedure.
Code: Select all
Procedure IsValidPBEvent(event)
Select event
Case #PB_Event_Menu ,
#PB_Event_Gadget ,
#PB_Event_SysTray ,
#PB_Event_Timer ,
#PB_Event_CloseWindow ,
#PB_Event_Repaint ,
#PB_Event_SizeWindow ,
#PB_Event_MoveWindow ,
#PB_Event_MinimizeWindow ,
#PB_Event_MaximizeWindow ,
#PB_Event_RestoreWindow ,
#PB_Event_ActivateWindow ,
#PB_Event_DeactivateWindow,
#PB_Event_WindowDrop ,
#PB_Event_GadgetDrop ,
#PB_Event_RightClick ,
#PB_Event_LeftClick ,
#PB_Event_LeftDoubleClick
ProcedureReturn #True
Default
ProcedureReturn #False
EndSelect ;event
EndProcedure
In short, totally unnecessary, redundant, and counter-productive.
Re: IsValidPBEvent?
Posted: Sat Nov 14, 2015 10:01 pm
by infratec
If I could download your codeinbasic example,
I would try to make it more 'PB conform'.
Then you could see that there is no need for such a procedure.
But as guest I can not download the code.
And I'm not willing to register.
Bernd
Re: IsValidPBEvent?
Posted: Sat Nov 14, 2015 10:28 pm
by collectordave
When I am designing a new program I cannot predict how many windows I will have to open or what PB events I will need to process, this means that I cannot follow some of the advice given here and check each individual event in the event loop.before I cann EventWindow().
It also makes it possible for me to design a new window with all its functions and when happy to transform this into a module for inclusion in the main program.
It also frees up the main event\message loop to simple ensure that messages about events on any of the applications windows are routed to the correct Event\message procedure for processing.
As I have been told that EventWindow() is only guaranteed to return a correct window ID after a Valid PB event the simplest way is to check that a valid event has been returned before routing.
Sorry to hear you do not wish to register.
Re: IsValidPBEvent?
Posted: Sat Nov 14, 2015 11:13 pm
by c4s
collectordave wrote:It also frees up the main event\message loop to simple ensure that messages about events on any of the applications windows are routed to the correct Event\message procedure for processing.
I strongly recommend you to rethink your current code structure and event handling because just by looking at this procedure more experienced PB programmers can see that there is somethig wrong.
Better use
BindEvent() instead. It automatically does all the "checking" and creates the cleanest event loop ever (if done correctly).
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 3:41 am
by TI-994A
collectordave wrote:When I am designing a new program I cannot predict how many windows I will have to open or what PB events I will need to process...
Doesn't sound like much of a design to begin with.
But it explains a lot. 
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 6:19 am
by collectordave
Doesn't sound like much of a design to begin with.
Maybe because no design parameters have been laid down?
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 6:31 am
by collectordave
Hi All
I strongly recommend you to rethink your current code structure and event handling because just by looking at this procedure more experienced PB programmers can see that there is somethig wrong.
Could the more experienced PB programmers explain what is wrong? Please?
Better use BindEvent() instead.
Will have a look at bind event.
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 7:47 am
by collectordave
Looking at BindEvent.
I simply want all valid PB events that happen on a window to be sent to that windows Event_Handler procedure.
BindEvent seems to deal with just a single event at a time.
Is there a way to use BindEvent to send all events that happen on a window to a set procedure?
From the help file it also looks like bind event only reacts correctly to valid PB events. Is this true? If so does it filter out all non valid PB events?
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 8:49 am
by TI-994A
collectordave wrote:I simply want all valid PB events that happen on a window to be sent to that windows Event_Handler procedure...
Time to take Schumacher's advice. 
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 9:43 am
by collectordave
BindEvent seems to deal with just a single event at a time.
Is there a way to use BindEvent to send all events that happen on a window to a set procedure?
From the help file it also looks like bind event only reacts correctly to valid PB events. Is this true? If so does it filter out all non valid PB events?
Sorry expected an answer to my post.
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 12:05 pm
by c4s
collectordave wrote:I simply want all valid PB events that happen on a window to be sent to that windows Event_Handler procedure. [...] Is there a way to use BindEvent to send all events that happen on a window to a set procedure?
Why? The whole point is to put all logic needed to react on
one specific event in one event handling procedure.
In my opinion it's the preferred way of dealing with events as it encourages you to use the
MVC pattern,
automatically creates cleaner code (compared to the traditional select block) and is closer to the handling of other modern programming languages.
collectordave wrote:From the help file it also looks like bind event only reacts correctly to valid PB events. Is this true? If so does it filter out all non valid PB events?
Yes. But you can make it react on custom events too (meaning those you've triggered yourself with PostEvent()).
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 5:26 pm
by collectordave
It is only my opinion but when I think of one specific event I think of, lets say a button click event, this button can be on one of several windows each of which can be called btnOk as a variable. It is sorting out which one was clicked and then the action to be taken that I need to get to. Routing events to a windows event procedure is to my mind keeping all the code in one event procedure as it will call the relevent procedure then return to the main loop. So the code to react to one specific event is in one sprcific code block and no other which may be the main window event handler or on another window.
If I first react to a button click event and then try to route the event to the correct procedure for that button I am still left with the traditional select..end select to route that event to the correct procedure. I find it easier to use eventwindow first and route the message directly to the particular windows event handler.
I have also found that it helps with debugging. As i create each window used in my application separately, as a module, and test separately when a bug appears I can easily convert that particular window module to a stand alone window and test. Leaves with two choices for each bug. Either the particular window is not reacting to the event correctly or the message is not being routed there correctly.
The last point is that I do not wish to react to any non valid PB events. This simple procedure roots them out immediatly and discards them so all windows in the application which recieve events from the main loop only get official\valid PB events.
Thanks for the link to MVC I will read that later.
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 6:52 pm
by RASHAD
Hi collectordave
I think you can not use a variable for more than one object in the same thread
But next is how to handle more than one window in the same events block
It will not be easy
Good luck
Code: Select all
Global btnOk_0,btnOk_1
Procedure window_1()
OpenWindow(0,0,0,400,300,"Win #1",#PB_Window_SystemMenu |#PB_Window_ScreenCentered)
btnOk_0 = ButtonGadget(#PB_Any,10,270,80,20,"Win #1")
EndProcedure
Procedure window_2()
OpenWindow(1,0,0,200,150,"Win #2",#PB_Window_SystemMenu |#PB_Window_ScreenCentered,WindowID(0))
btnOk_1 = ButtonGadget(#PB_Any,10,120,80,20,"Win #2")
EndProcedure
window_1()
window_2()
Repeat
Event = WaitWindowEvent()
If Event
Select EventWindow()
Case 0 ;For window 0
If Event = #PB_Event_CloseWindow
Quit_0 = 1
Break
EndIf
Select EventGadget()
Case btnOk_0
Debug "btnOk_0"
EndSelect
Case 1 ;for window 1
If Event = #PB_Event_CloseWindow
Quit_1 = 1
Break
EndIf
Select EventGadget()
Case btnOk_1
Debug "btnOk_1"
EndSelect
EndSelect
EndIf
Until Quit_0 = 1 Or Quit_1 = 1
Re: IsValidPBEvent?
Posted: Sun Nov 15, 2015 8:01 pm
by infratec
Hi collectordave,
You've got mail.