MaxGUI - pop up menu events

BlitzMax Forums/BlitzMax Beginners Area/MaxGUI - pop up menu events

Brendane(Posted 2005) [#1]
Hi,

Is there a way to detect when a pop up menu is closed? (ie. for when no menu item is selected).


Brendane(Posted 2005) [#2]
Is there noone with a solution to this?

What I want it to be able to use a pop up menu over the top of a windowed mode Max2D application.

Currently, I create an invisible window and attach the menu to it's windowmenu. I need to end the message pump loop when the menu is closing (this occurs either when an item is selected or the user clicks somewhere off the menu).

Unfortunately, there's no event in the blitzmax GUI which is triggered when the menu closes... so I'm locked in the loop waiting for a menu selection (which now can't happen).

Any ideas?


Chris C(Posted 2005) [#3]
check to see if its visible each message loop??


Brendane(Posted 2005) [#4]
Ive tried that one Chris, thanks. It's visible property remains set apparently.


Chris C(Posted 2005) [#5]
errm see if it still has the focus????
if not ActiveGadget(popupmenu) then ....


Chris C(Posted 2005) [#6]
nope not that either!


Brendane(Posted 2005) [#7]
Hi Chris,

Great idea, thanks. And........ it works!

But, there's a problem. Check out the following code :-

			If( MouseHit( 2 ))
				Local window:TGadget = CreateWindow("win", 0,0,10,10, Null, WINDOW_HIDDEN | WINDOW_MENU )
				Local menu:TGadget = CreateMenu( "Menu", 0, WindowMenu( window ))
				CreateMenu( "Item 1", 1, menu )
				
				Local done = False
				
				ShowMouse()
				PopupWindowMenu( window, menu )
				
				While Not done
					WaitEvent
					Local id = EventID()
					Local dat = EventData()
					
					Select id
						Case EVENT_MENUACTION
							Select dat
								Case 1
									Notify("Item 1 clicked")
									done = True	
							EndSelect
					EndSelect
							
					If( ActiveGadget() <> menu )
						Notify( "Menu closed")
						done = True
					EndIf
				Wend
				
				FreeGadget( window )
				HideMouse()
				FlushMouse()
				
			EndIf



IF the activegadget is not the menu for any reason - the code which selects the eventID does not appear to be executed. What's going on here? Am I going mad, is there some kind of early-out compiler optimisation getting in the way?


Chris C(Posted 2005) [#8]
you can use the main window to parent the popup btw
(and the event processing too...)

I didnt seem to get activegadget to return anything but null, so I must have been doing somthing wrong...

Do you have a complete example as opposed to a code fragment?


Brendane(Posted 2005) [#9]
How do I get the main window? I have been trawling the docs for that.

I understand what's going on then. The ActiveGadget is never actually set to the menu - therefore the next message (a mousedown event) is causing the loop to terminate.

Here is the original problem with a test graphics window

Strict

Graphics 640,480

While Not KeyDown(KEY_ESCAPE)
	Cls
	DrawText "Right mouse to invoke popup menu", 10,10
	Flip

	If( MouseHit( 2 ))
		Local window:TGadget = CreateWindow("win", 0,0,10,10, Null, WINDOW_HIDDEN | WINDOW_MENU )
		Local menu:TGadget = CreateMenu( "Menu", 0, WindowMenu( window ))
		CreateMenu( "Item 1", 1, menu )
		
		Local done = False

		PopupWindowMenu( window, menu )

		While Not done
			WaitEvent
			Local id = EventID()
			Local dat = EventData()
			
			Select id
				Case EVENT_MENUACTION
					Select dat
						Case 1
							Notify("Item 1 clicked")
							done = True	
					EndSelect
			EndSelect

		Wend

		FreeGadget( window )
		FlushMouse()
	EndIf
Wend



Brendane(Posted 2005) [#10]
Problem solved :

The problem is that the PopUpWindowMenu does not send messages to the window until after the menu closes (under Windows at least). At which point all the buffered messages are sent to the window - one of which might be a menu action event.

I've changed the message loop to poll the message queue until it's empty.. this way it drops out once all the messages have been processed.

Strict

Graphics 640,480

While Not KeyDown(KEY_ESCAPE)
	Cls
	DrawText "Right mouse to invoke popup menu", 10,10
	Flip

	If( MouseHit( 2 ))
		Local window:TGadget = CreateWindow("win", 0,0,10,10, Null, WINDOW_HIDDEN | WINDOW_MENU )
		Local menu:TGadget = CreateMenu( "Menu", 0, WindowMenu( window ))
		CreateMenu( "Item 1", 1, menu )
		CreateMenu( "Item 2", 2, menu )
		CreateMenu( "Item 3", 3, menu )
		CreateMenu( "Item 4", 4, menu )
		
		PopupWindowMenu( window, menu )

		Local nEvents = 0
		While PollEvent()
			Select CurrentEvent.id
				Case EVENT_MENUACTION
					Select EventData()
						Case 1
							DebugLog("Item 1 clicked")
						Case 2
							DebugLog("Item 2 clicked")
						Case 3
							DebugLog("Item 3 clicked")
						Case 4
							DebugLog("Item 4 clicked")
					EndSelect
			EndSelect
		Wend
		
		FreeGadget( window )
		FlushMouse()
	EndIf
Wend



Chris C(Posted 2005) [#11]
nice work! well done.


Brendane(Posted 2005) [#12]
Thanks Chris.

It would be interesting to know if this works the same on other platforms (I only use windows at the moment). Anyone willing to test it?