IsValidPBEvent?[Resolved]

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

IsValidPBEvent?[Resolved]

Post 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
Last edited by collectordave on Fri Jan 13, 2017 10:44 am, edited 1 time in total.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: IsValidPBEvent?

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: IsValidPBEvent?

Post 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
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: IsValidPBEvent?

Post 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).
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: IsValidPBEvent?

Post 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. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post by collectordave »

Doesn't sound like much of a design to begin with.
Maybe because no design parameters have been laid down?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post 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?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: IsValidPBEvent?

Post 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. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: IsValidPBEvent?

Post 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()).
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: IsValidPBEvent?

Post 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

Egypt my love
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: IsValidPBEvent?

Post by infratec »

Hi collectordave,

You've got mail.
Post Reply