IsValidPBEvent?[Resolved]

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: IsValidPBEvent?

Post by mestnyi »

But isn't it strange, that no one of the other few thousand PB-users ever were in need of something like this?
it is not true.
I needed and did not write again
From the help:
Quote:
After a WindowEvent() or WaitWindowEvent() function, use this function to determine on which window the event has occurred.
In Linux somebody ran for example, so there very well.
at least, why are not the same behavior?
if you Ghawar it is not possible it is not true in this instance.
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: IsValidPBEvent?

Post by HeX0R »

mestnyi wrote:
But isn't it strange, that no one of the other few thousand PB-users ever were in need of something like this?
it is not true.
I needed and did not write again
O.K., sorry then 2 of a few thousand.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: IsValidPBEvent?

Post by collectordave »

The whole thing seemd to revolve around how to write the main event loop.

It appears that writing it like this below:-

Code: Select all

Repeat
  
  Event = WaitWindowEvent()
  
  Select event
    Case  #PB_Event_Menu
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
          
    Case  #PB_Event_Gadget 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_SysTray
      
       Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_Timer
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_CloseWindow 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_Repaint    
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_SizeWindow 
      
      Select EventWindow()
                    
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_MoveWindow  
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_MinimizeWindow 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_MaximizeWindow 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_RestoreWindow 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_ActivateWindow  
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_DeactivateWindow
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_WindowDrop  
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_GadgetDrop 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_RightClick 
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_LeftClick 
      
       Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
    Case  #PB_Event_LeftDoubleClick
      
      Select EventWindow()
          
        Case MainFrm
          Event+Handler(Event)
          
        Case PreferencesFrm
          Preferencesd::Event_Handler(Event)
          
        Case SearchFrm
          SearchFrm::Event_Handler(Event)
          
        Case GridDispFrm
          GridDispFrm::Event_Handler(Event)
          
        Case PrintPrevFrm
          PrintPrevFrm::Event_Handler(Event)
          
      EndSelect ;EventWindow()
      
  EndSelect ;Event
  
  Until Event = #PB_Event_CloseWindow
is ok but like this:-

Code: Select all

  Event = WaitWindowEvent()
  
  If IsValidPBEvent(Event)
      
    Select EventWindow()
          
      Case MainFrm
        Event+Handler(Event)
          
      Case PreferencesFrm
        Preferencesd::Event_Handler(Event)
          
      Case SearchFrm
        SearchFrm::Event_Handler(Event)
          
      Case GridDispFrm
        GridDispFrm::Event_Handler(Event)
          
      Case PrintPrevFrm
        PrintPrevFrm::Event_Handler(Event)
          
    EndSelect ;EventWindow()
    
  EndIf
  
Is not?
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:...but like this:-

Code: Select all

  Event = WaitWindowEvent()
  
  ; If IsValidPBEvent(Event)
      
    Select EventWindow()
          
      Case MainFrm
        Event_Handler(Event)
          
      Case PreferencesFrm
        Preferencesd::Event_Handler(Event)
          
      Case SearchFrm
        SearchFrm::Event_Handler(Event)
          
      Case GridDispFrm
        GridDispFrm::Event_Handler(Event)
          
      Case PrintPrevFrm
        PrintPrevFrm::Event_Handler(Event)
          
    EndSelect ;EventWindow()
    
  ; EndIf
  
Is not?
Looks fine, but without your IsValidPBEvent() function.
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 »

Except that as shown here http://www.purebasic.fr/english/viewtop ... 13&t=63778 eventwindow is not guaranteed to return a correct window reference except after AValidPBEvent! So commenting out the IsValidPBEvent() call simply allows errors to creep in as shown above.
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:...eventwindow is not guaranteed to return a correct window reference except after AValidPBEvent! So commenting out the IsValidPBEvent() call simply allows errors to creep in as shown above.
All valid events for any focused window would be processed properly if handled correctly.

Even the example that you linked to raises no error.
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 »

A quote from the linked topic from the PB team leader.
You should always check for a valid PB event,
Of course specific PB events are checked and processed in the specific event handler of the specific window on which the specific event happend.
Even the example that you linked to raises no error.
Yes the example I linked to raises no error that is correct. However it does show that Eventwindow() does, in some circumstances, return an erronous result.

The purpose of this procedure is only to check that a valid PB event has been recieved, which then allows the EventWindow() procedure to always return correct results, of course this then allows the specific event to be processed in the event handler of the window on which the event occurred.

I should state here that I have no interest at all in processing OS specific events that are not supported by PB as I wish my code to take advantage of the cross platform compatibily of PB code, developing on a Mac as well as a windows PC.
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
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: IsValidPBEvent?

Post by HeX0R »

There are only rare situations, where you really need to check the EventWindow(), in fact you need to check it only when Window events, like #PB_Event_CloseWindow e.g.
occur.
All Gadgets, Menus, Toolbars, [...] should have unique numbers and you don't care about the window they are placed on.

An example:

Code: Select all

Enumeration Windows
	#Window_1
	#Window_2
EndEnumeration

Enumeration Gadgets
	;Gadgets on first Window
	#Text_On_Window_1
	#String_On_Window_1
	#Button_On_Window_1
	;Gadgets on second Window
	#Text_On_Window_2
	#String_On_Window_2
	#Button_On_Window_2
	;[...]
EndEnumeration

Procedure main()
	
	;Create windows
	OpenWindow(#Window_1, 15, 15, 350, 200, "Window 1")
	TextGadget(#Text_On_Window_1, 5, 5, 100, 22, "Bla Peng")
	StringGadget(#String_On_Window_1, 110, 5, 200, 22, "")
	ButtonGadget(#Button_On_Window_1, 5, 35, 140, 24, "Set Text to Window 1")
	
	OpenWindow(#Window_2, 15, 365, 350, 200, "Window 2")
	TextGadget(#Text_On_Window_2, 5, 5, 100, 22, "Bla Peng")
	StringGadget(#String_On_Window_2, 110, 5, 200, 22, "")
	ButtonGadget(#Button_On_Window_2, 5, 35, 140, 24, "Set Text to Window 1")
	
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				;o.k. this event is really from a Window, so we need to look for EventWindow()
				Select EventWindow()
					Case #Window_1
						CloseWindow(#Window_1)
						If IsWindow(#Window_2) = 0
							;ups, both are closed, we should jump out
							Break
						EndIf
					Case #Window_2
						CloseWindow(#Window_2)
						If IsWindow(#Window_1) = 0
							;ups, both are closed, we should jump out
							Break
						EndIf
				EndSelect
			Case #PB_Event_Gadget
				;this is from a Gadget, we KNOW where our Gadgets are placed on, so why should we check for the EventWindow()?
				Select EventGadget()
					Case #Button_On_Window_1
						SetGadgetText(#String_On_Window_1, "Yeahhh")
					Case #Button_On_Window_2
						;Yes, we can also set Text in the other Window, why not?
						SetGadgetText(#String_On_Window_1, "Yeahhh")
					Case #String_On_Window_1
						If EventType() = #PB_EventType_Change
							;o.k. user is typing text here, we just copy it to the other Stringgadget
							SetGadgetText(#String_On_Window_2, GetGadgetText(#String_On_Window_1))
						EndIf
				EndSelect
		EndSelect
	ForEver
	
EndProcedure

main()
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: IsValidPBEvent?

Post by kenmo »

dave... You don't need to check that an Event is within the set of ALL POSSIBLE PB events, since your event handler already checks for a SMALLER SUBSET of PB events.

Look at this example code, and think about the two checks...

Code: Select all

; for simplicity,
; imagine there were only 3 'valid' PB events...
;
; #PB_Event_CloseWindow
; #PB_Event_MoveWindow
; #PB_Event_SizeWindow

OpenWindow(0, 0, 0, 320, 240, "Move Me", #PB_Window_SizeGadget|#PB_Window_SystemMenu)

Repeat
  Event = WaitWindowEvent()
  
  ; check for valid events
  Select (Event)
    Case #PB_Event_CloseWindow, #PB_Event_MoveWindow, #PB_Event_SizeWindow
      
      ; event handler (could be in a procedure)
      Select (Event)
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_MoveWindow
          Debug "MoveWindow"
        Default
          ; don't handle other events, including SizeWindow
      EndSelect
      
    Default
      ; ignore invalid events
  EndSelect
ForEver
Or even clearer:

Code: Select all

If (x = 2) Or (x = 3)

  If (x = 2)
    Debug "two"
  ElseIf (x = 3)
    Debug "three"
  EndIf
  
EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: IsValidPBEvent?

Post by mk-soft »

I like this...

Code: Select all

;-TOP

; Visual Generate EventMain (Do Not Edit - Allways Overwrite)

; *********************************************************************************************************

; Init Event Pointer

Prototype MyProtoInvoke()
Prototype MyProtoInvoke1(EventType)

Structure udtCallEvent
  Invoke.MyProtoInvoke[0]
EndStructure

Structure udtCallEvent1
  Invoke.MyProtoInvoke1[0]
EndStructure

Global *CallEventMenu.udtCallEvent    = ?CallEventMenu
Global *CallEventGadget.udtCallEvent1 = ?CallEventGadget
Global *CallEventSize.udtCallEvent    = ?CallEventSize
Global *CallEventExit.udtCallEvent    = ?CallEventExit

#CallEventMenuCount   = 0
#CallEventGadgetCount = 33
#CallEventWindowCount = 1

; *********************************************************************************************************

Procedure MyEventMenu(MenuID)
  If MenuID >= 0 And MenuID < #CallEventMenuCount
    *CallEventMenu\Invoke[MenuID]()
    
  ; Begin MacMenus
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS  
  Else
    Select MenuID
      Case #PB_Menu_About : EventMenu_MacAbout()
      Case #PB_Menu_Preferences: EventMenu_MacPreferences()
      Case #PB_Menu_Quit : EventMenu_MacQuit()
    EndSelect
  CompilerEndIf
  ; End MacMenus
  
  EndIf
EndProcedure

; *********************************************************************************************************

Procedure MyEventGadget(GadgetID, EventType)
  If GadgetID >= 0 And GadgetID < #CallEventGadgetCount
    *CallEventGadget\Invoke[GadgetID](EventType)
  EndIf
EndProcedure

; *********************************************************************************************************

Procedure MyEventSize(WindowID)
  If WindowID >= 0 And WindowID < #CallEventWindowCount
    *CallEventSize\Invoke[WindowID]()
  EndIf
EndProcedure

; *********************************************************************************************************

Procedure MyEventExit(WindowID)
  If WindowID >= 0 And WindowID < #CallEventWindowCount
    *CallEventExit\Invoke[WindowID]()
  EndIf
EndProcedure

; *********************************************************************************************************

Procedure MyEventDispatch(EventType)
  
EndProcedure

; *********************************************************************************************************

DataSection
  CallEventMenu:
  CallEventGadget:
    Data.i @EventGadget_Main_Container_NumBlock()
    Data.i @EventGadget_Main_Button_NB_CE()
    Data.i @EventGadget_Main_Button_NB_C()
    Data.i @EventGadget_Main_Button_NB_Mul()
    Data.i @EventGadget_Main_Button_NB_Sub()
    Data.i @EventGadget_Main_Button_NB_Add()
    Data.i @EventGadget_Main_Button_NB_Enter()
    Data.i @EventGadget_Main_Button_NB_7()
    Data.i @EventGadget_Main_Button_NB_0()
    Data.i @EventGadget_Main_Button_NB_3()
    Data.i @EventGadget_Main_Button_NB_2()
    Data.i @EventGadget_Main_Button_NB_1()
    Data.i @EventGadget_Main_Button_NB_6()
    Data.i @EventGadget_Main_Button_NB_5()
    Data.i @EventGadget_Main_Button_NB_4()
    Data.i @EventGadget_Main_Button_NB_9()
    Data.i @EventGadget_Main_Button_NB_8()
    Data.i @EventGadget_Main_Button_NB_00()
    Data.i @EventGadget_Main_Container_NB_Output()
    Data.i @EventGadget_Main_String_NB_Output()
    Data.i @EventGadget_Main_ListView_Kasse()
    Data.i @EventGadget_Main_Container_Scanner()
    Data.i @EventGadget_Main_String_Card_ID()
    Data.i @EventGadget_Main_String_Card_Value()
    Data.i @EventGadget_Main_Container_Buttons()
    Data.i @EventGadget_Main_Button_Card_Insert()
    Data.i @EventGadget_Main_Button_Card_Delete()
    Data.i @EventGadget_Main_Button_Card_Add_Value()
    Data.i @EventGadget_Main_Button_Card_Sub_Value()
    Data.i @EventGadget_Main_ListIcon_ListCard()
    Data.i @EventGadget_Main_Button_Card_List()
    Data.i @EventGadget_Main_Button_Card_Detail()
    Data.i @EventGadget_Main_ListView_Log()
  CallEventSize:
    Data.i @EventSize_Main()
  CallEventExit:
    Data.i @EventExit_Main()
EndDataSection

; *********************************************************************************************************
Show signature...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
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:...it does show that Eventwindow() does, in some circumstances, return an erronous result.
Erroneous, no. As per the rules of the language construct, polling EventWindow() at the inappropriate time would definitely yield inaccurate results. But even so, branching to any window's event handler when there are no relevant events to process is not consequential, as the code would simply flow through and return.

With all due respect, we do understand your intended approach. Technically, there's nothing wrong in applying an extra check before routing events to their target windows. However, it's simply redundant to do so, and as the BindGadgetEvent() example in the other thread has shown, redundancy can sometimes cause problems.

Events should simply be allowed to flow through, with only the required ones intercepted and processed.

That said, in all honesty, I can't see any problems that your IsValidPBEvent() function would pose, although I can't see any benefits either.

But we all know how you love redundancy. :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 »

Erroneous, no. As per the rules of the language construct, polling EventWindow() at the inappropriate time would definitely yield inaccurate results. But even so, branching to any window's event handler when there are no relevant events to process is not consequential, as the code would simply flow through and return.
Erroneous yes. EventWindow only returns a valid window number after a valid PB event.. As per the rules in the help file.
After a WindowEvent() or WaitWindowEvent() function, use this function to determine on which window the event has occurred.
The valid PB event is not mentioned just "event". I am assuming nothing here just that after an event it returns the window on which the event occurred as stated.
With all due respect, we do understand your intended approach. Technically, there's nothing wrong in applying an extra check before routing events to their target windows. However, it's simply redundant to do so, and as the BindGadgetEvent() example in the other thread has shown, redundancy can sometimes cause problems.

Events should simply be allowed to flow through, with only the required ones intercepted and processed.
In my original programme I allowed events to simply flow through as you say believing all to be Ok. I reached a point where the user enters values on a secondary window which affect the display on the main window and started to notice some strange effects. This started me on the investigation into my programme to see where I had gone wrong. Which is where I had reached when posting the first example multiple window.

I have rewritten the example to show this a little clearer, code below:-

Code: Select all

frmMain = OpenWindow(#PB_Any, 0, 0, 600, 220, "", #PB_Window_SystemMenu)
CreateMenu(100, WindowID(frmMain))
  MenuTitle("File")
  MenuItem(101, "Open")
String_0 = StringGadget(#PB_Any, 80, 30, 310, 30, "")
  
Repeat
 
  event = WaitWindowEvent()

  If EventWindow() = Wnd2;Select window.

      Window2::Event_Handler(event)
      SetGadgetText(String_0,"")
      counter = counter + 1
      SetGadgetText(String_0,Str(counter))
      
  EndIf ;EventWindow()
     
 ;Not the second window so must be the main window
 Select event
     
   Case #PB_Event_Menu
     
     Select EventMenu()
             
       ;Only one menu item
      Case 101
        ;Show the 2nd window
        Debug "Open 2nd"
        Wnd2 = Window2::OpenWnd2()
       
     EndSelect ;Eventmenu
       
   Case   #PB_Event_CloseWindow
         
     End
         
 EndSelect ;Event

ForEver 
and second window

Code: Select all

DeclareModule Window2
 
  Global ThisWindow.l
  Global Ok.i = 0
 
  Declare.l OpenWnd2()
  Declare Event_Handler(event)
 
EndDeclareModule

Module Window2

Global btnOk, btnCancel

Procedure.l OpenWnd2()
 
  ThisWindow = OpenWindow(#PB_Any, 300, 300, 280, 180, "Test Window", #PB_Window_SystemMenu)
  btnOk = ButtonGadget(#PB_Any, 130, 140, 60, 30, "Ok")
  btnCancel = ButtonGadget(#PB_Any, 210, 140, 60, 30, "Cancel")
 
  ProcedureReturn ThisWindow
 
EndProcedure

Procedure Event_Handler(event)
 
  Select event
     
    Case #PB_Event_CloseWindow
     
      CloseWindow(ThisWindow)

    Case #PB_Event_Gadget
     
      Select EventGadget()
         
        Case   btnOk
         
          Ok = 1
          CloseWindow(ThisWindow)
         
        Case btnCancel
         
          Ok = 0
          CloseWindow(ThisWindow)

      EndSelect
     
  EndSelect
 
EndProcedure

EndModule
Simply open the second window and the stringgadget will start to count and will keep counting until you cause another valid PB event. Just move your mouse away from all windows to see this the only events happening then are the events on window 1 where the stringgadget text is being changed. However the counter is only increased when a window 2 event is recieved!

The result of the first post explained that only after a valid PB event could eventwindow() be counted on. Which was the birth of this procedure.

When I am coding I like to try to elliminate as many potential bugs as possible so, thanks to another forum user who wrote this originally as they also had problems, I check for all valid PB events straight away. Adding the check to the programme above elliminates the problem. However please do not assume that the check will be in the finished programme as after completing the programme I will revisit this procedure and at the very least remove redundant event checks. One which springs to mind here is the timer event which i am not using at the present time. It may also result in the check being elliminated alltogether, but I will have to wait and see.

This is not a critisism of PB just the help file. If event window was stated that it was valid only after a valid PB event I would have gone away and written this procedure and not published at all. Assuming that everyone else had faced the same problem and overcome it in their own way.

Now to move on a little.

I do understand that all windows,gadgets and menus etc have to have unique identifiers. I started with Pure Basic trying to invent lots and lots of unique identifiers using enumerations etc and the list did look quite impressive in my code. I did get some wrong and watched my gadgets disappear. I then started to invent lots and lots of variable names and to use #PB_Any to assign the values to these variables. Thankfully before going to far I discovered modules. So now where I have a button on a window that the user clicks to say "Ok" I can simply call them all btn_Ok in the knowledge that it will not clash with others in other windows etc that have the same variable name. This also makes coding multiple windows with roughly the same content and operation a breeze. However the importance of eventwindow() becomes very clear.
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:This is not a critisism of PB just the help file.
Not at all; it should be a criticism of your fundamental grasp. :lol:
collectordave wrote:...the only events happening then are the events on window 1 where the stringgadget text is being changed. However the counter is only increased when a window 2 event is recieved!
That's because you're updating the string gadget only when the second window has the focus. And updating the string gadget would not shift the window focus.

So, no error.

Code: Select all

SetGadgetText(String_0,"")             ;<--- redundant!
SetGadgetText(String_0,Str(counter))   ;<--- this will overwrite
Here's another free lesson: You don't need to zero-out the string gadget before updating it; contents are always overwritten. :wink:
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 »

Of course we should use the whole quote hope you don't mind me reproducing it here.
This is not a critisism of PB just the help file. If event window was stated that it was valid only after a valid PB event I would have gone away and written this procedure and not published at all. Assuming that everyone else had faced the same problem and overcome it in their own way.
Of course this then means that
it should be a criticism of your fundamental grasp
your quote as you seem to have not read the whole paragraph.
That's because you're updating the string gadget only when the second window has the focus. And updating the string gadget would not shift the window focus.
I think you have missed the point again. The string gadget being updated is on window 1 so the events being created are all happening on window 1 yet EventWindow() is reporting them as being on window 2 nothing to do with focus. of course reading just a little further you would have read
the stringgadget will start to count and will keep counting until you cause another valid PB event
after which if you created an event on window 1 the counter would stop. So error. EventWindow() only returns a valid window after a ValidPBEvent. Not as stated in the help file after any event.
Here's another free lesson: You don't need to zero-out the string gadget before updating it; contents are always overwritten.
I know it does but thank you for reminding everyone.
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 think you have missed the point again. The string gadget being updated is on window 1 so the events being created are all happening on window 1 yet EventWindow() is reporting them as being on window 2...
Sadly, it's still you that have missed everything. :lol:

Updating the string gadget with the SetGadgetText() function does not trigger any events. The event that is causing the loop to run is the paint event from the OS, to redraw the string gadget. And since that event is handled internally by PureBasic, no valid events are being routed through the message loop. Accordingly, EventWindow() correctly continues to return the value of the last active window.

So, again, no error.
collectordave wrote:I know it does but thank you for reminding everyone.
Your boo-boo, so just you. And you're welcome. :wink:
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
Post Reply