Opening a PopupMenu correctly on MacOS

Mac OSX specific forum
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Opening a PopupMenu correctly on MacOS

Post by Andre »

The following code snippet (shorter form) is from the help file about the DisplayPupupMenu() command:

Code: Select all

  If OpenWindow(0, 200, 200, 200, 120, "Popup-Menu Example")
    If CreatePopupMenu(0)      ; hier beginnt das Erstellen des Popup-Menüs...
      MenuItem(3, "Save as")
      MenuItem(4, "Quit")
    EndIf
    Repeat
      Select WaitWindowEvent()     ; überprüfe Window-Ereignisse
        Case #WM_RButtonDown       ; rechte Maustaste wurde gedrückt =>
          DisplayPopupMenu(0, WindowID(0))  ; stelle jetzt das Popup-Menü dar
        Case #PB_Event_Menu        ; ein Eintrag des Popup-Menüs wurde angeklickt
          Select EventMenu()       ; ermittle den angeklickten Menü-Eintrag...
            Case 3 : Debug "Menu: Save as"
            Case 4 : Quit = 1
          EndSelect
        Case #PB_Event_CloseWindow
          Quit = 1
      EndSelect
    Until Quit = 1
  EndIf
Even their in the help is the Windows constant #WM_RButtonDown used to detect a right-mouse click in the program correctly and then display the popup-menu.

1) How must this done correctly on MacOS?
2) Shouldn't their be a PB constant for this on all platforms?

Thanks!
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Opening a PopupMenu correctly on MacOS

Post by Vera »

Dear Andre,

you're right. This is a wrong example in the sense to sign it as 'Supported OS: All'. It doesn't work on Linux either.

As for the mouse click events - there are 4 PB constants, they only needed to be assigned for the WindowEvent as well. (Maybe this should be a feature request ?)

#PB_EventType_LeftClick
#PB_EventType_LeftDoubleClick
#PB_EventType_RightClick
#PB_EventType_RightDoubleClick


To show how the popup-menu works does not need to dependent on the 'WindowEvent'. Here are two alternatives:

Code: Select all

If OpenWindow(0, 200, 200, 200, 120, "Popup-Menu Example")
   CreateImage(0, 200, 120)
   ImageGadget(0, 0, 0, 200, 120, ImageID(0))

    If CreatePopupMenu(0)      ; hier beginnt das Erstellen des Popup-Menüs...
      MenuItem(3, "Save as")
      MenuItem(4, "Quit")
    EndIf
    Repeat
      Select WaitWindowEvent()     ; überprüfe Window-Ereignisse
        Case #PB_Event_Gadget       ; rechte Maustaste wurde gedrückt =>
          If EventType() = #PB_EventType_RightClick
            DisplayPopupMenu(0, WindowID(0))  ; stelle jetzt das Popup-Menü dar
          EndIf 
        
        Case #PB_Event_Menu        ; ein Eintrag des Popup-Menüs wurde angeklickt
          Select EventMenu()       ; ermittle den angeklickten Menü-Eintrag...
            Case 3 : Debug "Menu: Save as"
            Case 4 : Quit = 1
          EndSelect
        Case #PB_Event_CloseWindow
          Quit = 1
      EndSelect
    Until Quit = 1
  EndIf

Code: Select all

If OpenWindow(0, 100, 100, 300, 100, "Popup-Menu Example 2")
  ButtonGadget(0, 130, 25, 100, 30, "PopupMenu")

  If  CreateMenu(1, WindowID(0)) 
      MenuTitle("Menu-1")
        MenuItem(1, "Tab 1"  +Chr(9) + "A")
        MenuItem(3, "Tab 3"  +Chr(9)+"Ctrl+B")
        MenuItem(5, "Tab 5"  +Chr(9)+"Cmd+C")  ; used on MAC
  EndIf
      
 Repeat
  Select WaitWindowEvent()
     Case #PB_Event_Gadget
         Select EventGadget()
            Case 0
               CreatePopupImageMenu(0)
               MenuItem(0, "Item 1" + Chr(9) + "A")
               MenuItem(0, "Item 2" + Chr(9) + "Ctrl+B")
               MenuItem(0, "Item 3" + Chr(9) + "Cmd+C")  ; used on MAC
               DisplayPopupMenu(0, WindowID(0))
         EndSelect   
      Case #PB_Event_CloseWindow
       Break
   EndSelect
 Until WaitWindowEvent()=#PB_Event_CloseWindow
 
EndIf
(also works on MAC, verified by WilliamL ~ thanks)


regards ~ Vera


Addendum: as the second example is more an empty static structure I've made a more extensive one rounded with operable event commands and posted it in Using PopUpMenus.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Opening a PopupMenu correctly on MacOS

Post by Andre »

Thanks a lot for your effort, Vera! (also in the other thread regarding pop-up menus on MacOS)

Your second example of your answer in this thread works as expected.
But it needs the "full-size" ImageGadget to work / get the needed event.

Is there any possibility on MacOS to recognize a right-mouseclick in an empty (!) window, means without adding (dummy) imagegadgets or similar?
My purpose is (that's why I'm asking...): my application has a full-size window, which is only partly filled with gadgets and a larger part is empty at the moment. And I want, that the popup-menu at any time, the right-mouse is clicked and not only when it's clicked over a gadget....

I think something like the Windows-only "#WM_RBUTTONDOWN" as event should be natively supported by PB! :-)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Opening a PopupMenu correctly on MacOS

Post by Shardik »

Andre wrote:Is there any possibility on MacOS to recognize a right-mouseclick in an empty (!) window, means without adding (dummy) imagegadgets or similar?

Code: Select all

#kEventClassMouse = 'mous'
#kEventMouseButtonPrimary = 1
#kEventMouseButtonSecondary = 2
#kEventMouseButtonTertiary = 3
#kEventMouseDown = 1
#kEventParamKeyModifiers = 'kmod'
#kEventParamMouseButton = 'mbtn'

#typeMouseButton = 'mbtn'
#typeUInt32 = 'magn'

ImportC ""
  GetEventClass(Event)
EndImport

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

Procedure EventHandler(*NextEventHandler, Event, UserData)
  Select GetEventClass(Event)
    Case #kEventClassMouse
      Select GetEventKind_(Event)
        Case #kEventMouseDown
          If GetEventParameter_(Event, #kEventParamMouseButton, #typeMouseButton, 0, 4, 0, @ButtonType) = 0
            If GetEventParameter_(Event, #kEventParamKeyModifiers, #typeUInt32, 0, 4, 0, @KeyModifier) = 0
              Select ButtonType
                Case #kEventMouseButtonPrimary
                  If KeyModifier = $00001000
                    Debug "Right mouse button"
                  Else
                    Debug "Left mouse button"
                  EndIf
                Case #kEventMouseButtonSecondary
                  Debug "Right mouse button"
                Case #kEventMouseButtonTertiary
                  Debug "Middle mouse button"
              EndSelect
            EndIf
          EndIf
      EndSelect
  EndSelect

  If *NextEventHandler
    CallNextEventHandler_(*NextEventHandler, Event)
  EndIf
EndProcedure

OpenWindow(0, 100, 100, 400, 200, "Detect left, middle and right mouse button click")

EventHandlerUPP = NewEventHandlerUPP_(@EventHandler())
EventCount = 1
Dim EventTypes.EventTypeSpec(EventCount - 1)
EventTypes(0)\EventClass = #kEventClassMouse
EventTypes(0)\EventKind = #kEventMouseDown
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), EventHandlerUPP, EventCount, @EventTypes(), UserData, @EventHandlerRef)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by Shardik on Thu Nov 25, 2010 8:52 pm, edited 1 time in total.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Opening a PopupMenu correctly on MacOS

Post by Andre »

Thanks Shardik, that works! :D

Even if it would be better, if that event handling would native in PB... :twisted:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Opening a PopupMenu correctly on MacOS

Post by WilliamL »

Odd, if I use my trackball with a right button Shardik's code works but if I use the 'ctrl'+(mousepad) button I get a left-click. In other words, if I use my MBP without an external mouse and try to right click.. I get only left-clicks. This limitation may be a problem for some users.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Opening a PopupMenu correctly on MacOS

Post by Shardik »

WilliamL wrote:Odd, if I use my trackball with a right button Shardik's code works but if I use the 'ctrl'+(mousepad) button I get a left-click.
You are right William, I didn't take care in my code example for using a left click in
combination with the Ctrl key as a right mouse click (presumably for those Apple users
with a one button mouse) although I had already read a posting from David McLeod in
the apple mailing list where he presented C code which demonstrates how to analyse
modifier keys and mouse clicks.

I have therefore updated my above code example. You are now free to use the Ctrl or Alt
or Cmd key together with the left mouse button to produce a right click... :wink:

I have tested my above example with an iMac and Mac OS X 10.6.5, a mighty mouse and
an USB Wheel mouse on which pressing the wheel down is correctly interpreted as a middle
button click.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Opening a PopupMenu correctly on MacOS

Post by WilliamL »

Thanks Shardik,

Works perfectly on my MBP in 10.5.8. :D

(added to API list)
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply