Additional parameter for WindowEvent and WaitWindowEvent

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Additional parameter for WindowEvent and WaitWindowEvent

Post by infratec »

Like in NetworkServerEvent() you should be able to add the Window as parameter.
Then you can handle different windows in different event loops.

This makes sense if you need a hidden window as callback receiver (like for playing MIDI in windows)
Then you can handle this without disturbing the rest.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by Caronte3D »

+1
Also useful if like on my case you have a window inside a dll (not a good practice, I know, but works nice on Windows) 8)
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by AZJIO »

EventWindow()?
User avatar
mk-soft
Always Here
Always Here
Posts: 6244
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by mk-soft »

AZJIO wrote: Mon Jun 09, 2025 11:56 amEventWindow()?
After a WindowEvent() or WaitWindowEvent() function, use this function to determine on which window the event has occurred.
Thus, one can separate the further event processing.
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
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by Quin »

+1. This would be super useful.
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by AZJIO »

This will simply insert the following construction into your code

Code: Select all

Bool(WaitWindowEvent() And EventWindow() = #Wim1)
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by Quin »

AZJIO wrote: Mon Jun 09, 2025 1:43 pm This will simply insert the following construction into your code

Code: Select all

Bool(WaitWindowEvent() And EventWindow() = #Wim1)
...Okay?
What is your point exactly?
Being able to use a bool expression to do the same thing does not mean the feature shouldn't be asked for/implemented... :mrgreen:
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by infratec »

AZJIO wrote: Mon Jun 09, 2025 1:43 pm This will simply insert the following construction into your code

Code: Select all

Bool(WaitWindowEvent() And EventWindow() = #Wim1)
That does not work. You get the event already by calling WaitWindowEvent().
So if it does not fit EventWindow(), you lost this event.
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by infratec »

And BindEvent() is also not possible.
It does not work for #MM_MCINOTIFY for example.
So I can not open a hidden window and proccess the MIDI CallBack in the background.
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by AZJIO »

Code: Select all

Procedure WaitWindowEvent2(wnd_id)
	Protected Result
	If EventWindow() = wnd_id
		Result = WaitWindowEvent()
	EndIf
    ProcedureReturn Result
EndProcedure

Event = WaitWindowEvent2(#Wim1)
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by infratec »

AZJIO wrote: Tue Jun 10, 2025 4:12 am

Code: Select all

Procedure WaitWindowEvent2(wnd_id)
	Protected Result
	If EventWindow() = wnd_id
		Result = WaitWindowEvent()
	EndIf
    ProcedureReturn Result
EndProcedure

Event = WaitWindowEvent2(#Wim1)
You can call EventWindow first when WindowEvent or WaitWinowEvent returns.
So you loose one event if it is not for your specified window.
AZJIO
Addict
Addict
Posts: 2187
Joined: Sun May 14, 2017 1:48 am

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by AZJIO »

infratec wrote: Tue Jun 10, 2025 6:49 am You can call EventWindow first when WindowEvent or WaitWinowEvent returns.
If you call WindowEvent() and WaitWinowEvent(), which both capture the event, you can skip it. But EventWindow() does not capture the event, but gets the window in which the event occurred. The help for EventWindow() says that the function should be called after capturing events.

Code: Select all

Procedure WaitWindowEvent2(wnd_id)
	Protected Result
	Result = WaitWindowEvent()
	If EventWindow() <> wnd_id
		Result = 0
	EndIf
    ProcedureReturn Result
EndProcedure


Event = WaitWindowEvent2(#Wim1)
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by Caronte3D »

I think infratec is correct, the WaitWindowEvent() in your example consume an event so if it's not from the correct window, the event is lost, not repeated on the other event loop.
User avatar
mk-soft
Always Here
Always Here
Posts: 6244
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by mk-soft »

There should only ever be one call to (Wait)WindowEvent().
In this loop, you can split the events depending on the window to be processed further
The window number is also entered for each valid event.

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#Version = "v1.01.1"

Enumeration Windows
  #Main
  #Diaglog1
  #HideWindow
EndEnumeration

Enumeration Menus
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuExitApplication
EndEnumeration

Enumeration Gadgets
  
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Global ExitApplication

; ----

Procedure DoEventsMain(Event)
  
  Select Event
    Case #PB_Event_CloseWindow
      ExitApplication = #True
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case #MainMenuExitApplication
          ExitApplication = #True
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
      EndSelect
      
  EndSelect
  
EndProcedure

Procedure DoEventsDialog1(Event)
  
  Select Event
    Case #PB_Event_CloseWindow
      ; TODO
      CloseWindow(#Diaglog1)
      
    Case #PB_Event_Menu
      Select EventMenu()
          
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
      EndSelect
      
  EndSelect
  
EndProcedure

Procedure DoEventsHideWindow(Event)
  
  Select Event
    Case #PB_Event_CloseWindow
      ; TODO
      CloseWindow(#HideWindow)
      
    Case #PB_Event_Menu
      Select EventMenu()
          
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
      EndSelect
      
    Case #PB_Event_Timer
      Select EventTimer()
        Case 1
          Debug "Timer"
          
      EndSelect
      
  EndSelect
  
EndProcedure

; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  
EndProcedure

; ----

Procedure OpenHideWindow()
  If OpenWindow(#HideWindow, 0, 0, 0, 0, "EventTimer", #PB_Window_Invisible, WindowID(#Main))
    AddWindowTimer(#HideWindow, 1, 1000)    
  EndIf
EndProcedure

Procedure Main()
  Protected dx, dy

  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("File")
    MenuItem(#MainMenuExitApplication, "E&xit")
    ; For Mac
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      If Not IsMenu(#MainMenu)
        CreateMenu(#MainMenu, WindowID(#Main))
      EndIf
      MenuItem(#PB_Menu_About, "")
      MenuItem(#PB_Menu_Preferences, "")
    CompilerEndIf
    ; StatusBar  
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    StatusBarText(#MainStatusBar, 0, " " + #Version)
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    
    ; Bind events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    OpenHideWindow()
    
    ; Main loop
    Repeat
      Event = WaitWindowEvent()
      Select EventWindow()
        Case #Main
          DoEventsMain(Event)
        Case #Diaglog1
          DoEventsDialog1(Event)
        Case #HideWindow
          DoEventsHideWindow(Event)
          
      EndSelect
      
    Until ExitApplication
    
  EndIf
  
EndProcedure : Main()
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
Fred
Administrator
Administrator
Posts: 18207
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Additional parameter for WindowEvent and WaitWindowEvent

Post by Fred »

I don't think it will be possible to do that, as there is a single queue for event, so if you want to filter for a specific window, all other events needs to be stored somewhere and it will create issues for sure with callbacks. @mk-soft just shown the right way to do it.
Post Reply