It is currently Fri Apr 10, 2020 10:15 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 61 posts ]  Go to page 1, 2, 3, 4, 5  Next
Author Message
 Post subject: IsValidPBEvent?[Resolved]
PostPosted: Sat Nov 14, 2015 8:04 am 
Offline
Addict
Addict

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

_________________
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.


Last edited by collectordave on Fri Jan 13, 2017 10:44 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sat Nov 14, 2015 9:05 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2306
Location: Singapore
collectordave wrote:
My way of overcoming this is to use the following procedure.
Code:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sat Nov 14, 2015 10:01 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4606
Location: Germany
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


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sat Nov 14, 2015 10:28 pm 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sat Nov 14, 2015 11:13 pm 
Offline
Addict
Addict

Joined: Thu Nov 01, 2007 5:37 pm
Posts: 1981
Location: Germany
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 3:41 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2306
Location: Singapore
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 6:19 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
Quote:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 6:31 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
Hi All

Quote:
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?

Quote:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 7:47 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 8:49 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2306
Location: Singapore
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 9:43 am 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
Quote:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 12:05 pm 
Offline
Addict
Addict

Joined: Thu Nov 01, 2007 5:37 pm
Posts: 1981
Location: Germany
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 5:26 pm 
Offline
Addict
Addict

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 1076
Location: Portugal
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 6:52 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3557
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:

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


Top
 Profile  
Reply with quote  
 Post subject: Re: IsValidPBEvent?
PostPosted: Sun Nov 15, 2015 8:01 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4606
Location: Germany
Hi collectordave,

You've got mail.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 61 posts ]  Go to page 1, 2, 3, 4, 5  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye