Determine if Popup Menu was cancelled

Just starting out? Need help? Post your questions and find answers here.
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Determine if Popup Menu was cancelled

Post by Worm »

I'm trying to determine if an item was selected in one of my popup menus or if the menu was hidden by clicking somewhere else. I've tried setting a variable to false before calling DisplayPopupMenu, setting it to true in my EventMenu routine, then checking its value after DisplayPopupMenu, but am having little luck.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

If an item was selected then it would trigger a menu-event, if the menu was hidden then you won't get an event.

Just check in your loop for the events.
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post by Worm »

Thanks Derek.

I do get the event when a menu item is chosen, but how do I check a *non* event for when the menus hidden. That's the part that is slipping through my fingers.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Is this what you're after?

Code: Select all

Global flag

If CreatePopupMenu(0)
  MenuItem(1, "Cut")
  MenuItem(2, "Copy")
  MenuItem(3, "Paste")
  MenuBar()
  OpenSubMenu("Options")
    MenuItem(4, "Window...")
    MenuItem(5, "Gadget...")
  CloseSubMenu()
  MenuBar()
  MenuItem( 6, "Quit")
EndIf

;
; We just have to open a window and see when an event happen on the menu
;

If OpenWindow(0, 100, 100, 300, 260, "PureBasic - PopupMenu Example")

  Repeat

    Select WaitWindowEvent()
        
      Case #WM_RBUTTONDOWN
        DisplayPopupMenu(0, WindowID(0))
        flag=1

      Case #PB_Event_Menu
        flag=0      
        Select EventMenu()  ; To see which menu has been selected

          Case 1 ; Cut
            MessageRequester("PureBasic", "Cut", 0)

          Case 2 ; Copy
            MessageRequester("PureBasic", "Copy", 0)

          Case 3 ; Paste
            MessageRequester("PureBasic", "Paste", 0)

          Case 6 ; Quit
            Quit = 1

        EndSelect
        
      Case #PB_Event_CloseWindow
        Quit = 1
      Default
        If flag=1
          flag=0
          Debug "Popup cancelled!"
        EndIf

    EndSelect
  Until Quit = 1

EndIf

End
I may look like a mule, but I'm not a complete ass.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

I think you don't, since all you are concerned about in your event loop is events, if one isn't triggered then you don't do anything.

The popup menu being hidden isn't a problem as it will come back if needed.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Nice one srod, although I still don't see why you would need to know if 'nothing' has been selected. But whatever. :wink:
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post by Worm »

srod, that's exaclty what I was trying to do. Thank you.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I agree Derek! :)

@worm: you're welcome.
I may look like a mule, but I'm not a complete ass.
Worm
User
User
Posts: 20
Joined: Wed Apr 14, 2004 2:32 am
Location: Grand Rapids, MI
Contact:

Post by Worm »

I know it's not the *normal* thing to try to capture this non event, but for my application its something I need to know. If the popup is displayed to a user, I need feed back as to whether they selected an item, or if the menu itself was ignore/canceled.

I appreciate your help, again thanks.
Post Reply