Page 1 of 1

Adding MenuItems to an Applications Dock-Menu

Posted: Fri Aug 19, 2016 9:58 pm
by fsw
Hi,
Did someone already look into how to add MenuItems to an Applications Dock-Menu?

Found something for Swift that I will try out (possibly this evening) but was wondering if someone already came across this and has a working solution in PureBasic.

Thanks

Re: Adding MenuItems to an Applications Dock-Menu

Posted: Sat Aug 20, 2016 11:30 am
by Shardik
The following code demonstrates how to add own menu entries to the dock menu of your application:

Code: Select all

Define SharedApp.I = CocoaMessage(0, 0, "NSApplication sharedApplication")
Define AppDelegate.I = CocoaMessage(0, SharedApp, "delegate")
Define DelegateClass.I = CocoaMessage(0, AppDelegate, "class")

ProcedureC DockMenuCallback(Object.I, Selector.I, Sender.I)
  Shared MyDockMenu.I

  ; ----- Return own custom menu to be integrated into your app's dock menu
  ProcedureReturn MyDockMenu
EndProcedure

OpenWindow(0, 270, 100, 300, 20, "Please right click my dock icon!")

; ----- Initialize dock menu callback
class_addMethod_(DelegateClass, sel_registerName_("applicationDockMenu:"),
  @DockMenuCallback(), "v@:@")
CocoaMessage(0, SharedApp, "setDelegate:", AppDelegate)

; ----- Create own custom menu
CreateMenu(0, 0)
MenuTitle("My menu")
MenuItem(1, "My menu item 1")
MenuItem(2, "My menu item 2")
MyDockMenu = CocoaMessage(0, MenuID(0), "objectAtIndex:", 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Menu
      SelectedMenuItem = EventMenu()

      Select SelectedMenuItem
        Case 1, 2
          MessageRequester("Info", "'My menu item " + Str(SelectedMenuItem) +
            "' was selected.")
      EndSelect
  EndSelect
ForEver

Re: Adding MenuItems to an Applications Dock-Menu

Posted: Mon Aug 22, 2016 11:51 pm
by fsw
Hello Shardik,
this is working really great.

Awesome :!:

Thank you.