[Done] Incorrect window number for tray menu events.

Post bugreports for the Mac OSX version here
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

[Done] Incorrect window number for tray menu events.

Post by User_Russian »

Code: Select all

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
ForEver
Events from window number 0, but there is no such window in the application!

Code: Select all

Procedure Event_Menu()
  Select Event()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
EndProcedure

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

BindEvent(#PB_Event_Menu, @Event_Menu(), 10)

Repeat
  Select WaitWindowEvent()
;      Case #PB_Event_Menu
;        Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
;        Select EventMenu()
;          Case 1 ; Exit 
;            RemoveSysTrayIcon(0)
;            End
;        EndSelect
  EndSelect
ForEver
There are no events in this code at all.
But if in BindEvent() replace the window number with 0 then events appear.

Code: Select all

Procedure Event_Menu()
  Select Event()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
EndProcedure

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

BindEvent(#PB_Event_Menu, @Event_Menu(), 0)

Repeat
  Select WaitWindowEvent()
;      Case #PB_Event_Menu
;        Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
;        Select EventMenu()
;          Case 1 ; Exit 
;            RemoveSysTrayIcon(0)
;            End
;        EndSelect
  EndSelect
ForEver
This is incorrect because there is no window number 0 in this code!
User avatar
Demivec
Addict
Addict
Posts: 4277
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Incorrect window number for tray menu events.

Post by Demivec »

User_Russian wrote: Sun Sep 28, 2025 11:08 pm Events from window number 0, but there is no such window in the application!
Welcome to the non-null world of PureBasic.

EventWindow() does not have a return value indicating Null or that its value is not applicable to an event. What do you think happens when you Debug EventMenu() for a gadget event or EventGadget() for a menu event? This is also a good example of why it is wise not to use an object ID of zero so that zero can be considered an invalid or Null value indicator (which your first example demonstrates in a round about way).

What value do you want it to return?

I'm inexperienced with pop-up menus, if a menu is already associated with a window, (for display or otherwise) do you really need the window number to process the event?
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Incorrect window number for tray menu events.

Post by User_Russian »

Demivec wrote: Mon Sep 29, 2025 1:24 amWhat value do you want it to return?
When creating the tray icon, the window with ID 10 is specified.

Code: Select all

AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))
On Windows, this code works correctly and the function EventWindow() returns the number 10. But on MacOS the function returns 0.
Demivec wrote: Mon Sep 29, 2025 1:24 amdo you really need the window number to process the event?
The window number is specified in function BindEvent() and it is 10, not 0, which is why there are no events on MacOS and there are events on Windows.
User avatar
Piero
Addict
Addict
Posts: 1016
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Incorrect window number for tray menu events.

Post by Piero »

I would want to celebrate with Vodka the Russian contributions to this Forum, but I'm more inclined to gallons of German Beer :mrgreen:
User avatar
mk-soft
Always Here
Always Here
Posts: 6277
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Incorrect window number for tray menu events.

Post by mk-soft »

BindMenuEvent ...
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: 18335
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] Incorrect window number for tray menu events.

Post by Fred »

Fixed.
Post Reply