Adding MenuItems to an Applications Dock-Menu

Mac OSX specific forum
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Adding MenuItems to an Applications Dock-Menu

Post 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

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Adding MenuItems to an Applications Dock-Menu

Post 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
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Adding MenuItems to an Applications Dock-Menu

Post by fsw »

Hello Shardik,
this is working really great.

Awesome :!:

Thank you.

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply