Showing popup menu in a thread?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Showing popup menu in a thread?

Post by Dude »

I really need to show a popup menu that is triggered from inside a thread, because my main window is invisible and I need the popup menu to appear on demand, at any time.

The following doesn't work. What to do? Thanks.

Code: Select all

Enumeration
  #Menu_Item_0
  #Menu_Item_1
  #Menu_Item_2
  #Menu_Item_3
EndEnumeration

Global Menu=CreatePopupImageMenu(#PB_Any)

If Menu
  MenuItem(#Menu_Item_0, "Open")
  MenuItem(#Menu_Item_1, "Save")
  MenuItem(#Menu_Item_2, "Save as")
  MenuItem(#Menu_Item_3, "Quit")
EndIf

Procedure ShowMenu(null)
  Repeat
    Sleep_(1)
    If GetAsyncKeyState_(#VK_CONTROL) & $8000
      Debug "Showing menu now"
      DisplayPopupMenu(Menu,WindowID(0),20,20) ; Doesn't show!
      Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_CONTROL)=0
    EndIf
  ForEver
EndProcedure

CreateThread(@ShowMenu(),0)

OpenWindow(0, 200, 200, 200, 120, "Example", #PB_Window_Invisible)
Debug "Press Ctrl to see popup menu"
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Showing popup menu in a thread?

Post by firace »

You can solve this by posting a custom event to the main loop.
Does this do what you want?

Code: Select all

Enumeration
  #Menu_Item_0
  #Menu_Item_1
  #Menu_Item_2
  #Menu_Item_3
EndEnumeration

Global Menu=CreatePopupImageMenu(#PB_Any)

If Menu
  MenuItem(#Menu_Item_0, "Open")
  MenuItem(#Menu_Item_1, "Save")
  MenuItem(#Menu_Item_2, "Save as")
  MenuItem(#Menu_Item_3, "Quit")
EndIf

OpenWindow(0, 200, 200, 200, 120, "Example", #PB_Window_Invisible)

Procedure ShowMenu(null)
  Repeat
    Sleep_(1)
    If GetAsyncKeyState_(#VK_CONTROL) & $8000
      Debug "Showing menu now"
      PostEvent(123)
      Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_CONTROL)=0
    EndIf
  ForEver
EndProcedure

CreateThread(@ShowMenu(),0)

Debug "Press Ctrl to see popup menu"

Repeat 
  e = WaitWindowEvent()
  If e = 123:       DisplayPopupMenu(Menu,WindowID(0),20,20) : EndIf  
Until e=#PB_Event_CloseWindow
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Showing popup menu in a thread?

Post by Dude »

Thanks firace, I'll work off that. :)
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Showing popup menu in a thread?

Post by mk-soft »

Please use valid event numbers for PostEvent.

Code: Select all

;- Constants
Enumeration #PB_Event_FirstCustomValue
  #My_DoEvent
EndEnumeration
Tipp for updates menus and gadgets from threads
http://www.purebasic.fr/english/viewtop ... 12&t=66180
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
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Showing popup menu in a thread?

Post by Dude »

Is there any reason why DisplayPopupMenu() can't be called in a thread? The manual doesn't say it's prohibited, like it does for OpenWindow().
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Showing popup menu in a thread?

Post by Marc56us »

The main function of a thread is to work in the background, so in a non-interactive gui way.
A popupmenu is a function to interact with the user so in forground.
A popupmenu is a gadget designed to be triggered normally by the user and not auto triggered by the program.
The documentation only refers to the conventional use of objects.
Otherwise it would make several megabytes

:wink:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Showing popup menu in a thread?

Post by Dude »

Marc56us wrote:A popupmenu is a gadget designed to be triggered normally by the user and not auto triggered by the program.
Yep, that's what my app does. It has no visible window 99% of the time, but does sometimes (to change app Settings). My app's purpose is to show a popup menu whenever a key or mouse button is pressed, at any time, no matter what the user is doing. Hence, I can't use hotkeys to do it, because mouse buttons can't be assigned as a hotkey. So I have to wait for the keypress or mouse button in a thread, which then shows the popup menu.

The other issue is that while the popup menu is shown, my app can't process any other window events, like #PB_Event_CloseWindow. :( I don't know the best way to code around all these problems.
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Showing popup menu in a thread?

Post by Bisonte »

Dude wrote:The other issue is that while the popup menu is shown, my app can't process any other window events, like #PB_Event_CloseWindow.
The other question is: Have you ever closed manually a window with an opened menu? No, it's not possible.
Only if the menu is closed, other interaction is possible.

Usually everything needed for user interaction is plugged into the MainThread and all other threads send messages to this MainThread via PostEvent.

Other procedures should not be used.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Showing popup menu in a thread?

Post by Dude »

Bisonte wrote:Have you ever closed manually a window with an opened menu? No, it's not possible.
What? Of course you can. Open a menu in Notepad (or whatever) and then click the Close (X) button while that menu is open: the window will close. Same if you send a #WM_CLOSE message to Notepad when it has a menu open, whether that menu is from the menu bar or a right-click popup menu in the text area. It closes when it receives the #WM_CLOSE message -- an open menu doesn't stop it at all.
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Showing popup menu in a thread?

Post by Bisonte »

I see what you mean, but the menu closes before! the window accept the click. The same is on standard PB Windows with an open menu
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Showing popup menu in a thread?

Post by Marc56us »

When a PopUpMenu or Menu is opened in the main thread, the other events are managed (At least CloseWindows) either in PB or with other languages. But if you look closely, the system first closes the menu before leaving. (As Bison said before me)

:!: A PopUpMenu needs to know the active gadget to know which PopUpMenu to activate. I haven't tested it, but it's not sure if you can make a SetActiveGadget() in a thread. :?:

:wink:
Post Reply