1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/mbarman.cpp
3 // Purpose: wxMenuBarManager implementation
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/toplevel.h"
21 #include "wx/cocoa/mbarman.h"
22 #include "wx/cocoa/autorelease.h"
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSMenu.h>
26 #import <AppKit/NSApplication.h>
28 // ============================================================================
30 // ============================================================================
31 wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
33 wxMenuBarManager::wxMenuBarManager()
40 m_mainMenuBarInstalled = true;
45 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
46 // Create the services menu.
47 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
48 [theNSApplication setServicesMenu:m_menuServices];
51 // Create the application (Apple) menu.
52 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
54 /**/[m_menuApp addItemWithTitle:@"Preferences..." action:nil keyEquivalent:@""];
55 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
56 /**/menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" action:nil keyEquivalent:@""];
57 [menuitem setSubmenu:m_menuServices];
58 [m_menuApp addItem: menuitem];
60 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
61 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@""];
62 [menuitem setTarget: theNSApplication];
63 [m_menuApp addItem: menuitem];
65 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
66 [menuitem setTarget: theNSApplication];
67 [m_menuApp addItem: menuitem];
69 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
70 [menuitem setTarget: theNSApplication];
71 [m_menuApp addItem: menuitem];
73 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
74 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"Q"];
75 [menuitem setTarget: theNSApplication];
76 [m_menuApp addItem: menuitem];
79 [theNSApplication setAppleMenu:m_menuApp];
81 // Create the Windows menu
82 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
84 /**/[m_menuWindows addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
85 /**/[m_menuWindows addItem: [NSMenuItem separatorItem]];
86 /**/[m_menuWindows addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
88 [theNSApplication setWindowsMenu:m_menuWindows];
90 // Create the main menubar
91 m_menuMain = [[NSMenu alloc] initWithTitle: @"wxApp Menu"];
92 /**/NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
93 /* Note: title gets clobbered by app name anyway */
94 action:nil keyEquivalent:@""];
95 [dummyItem setSubmenu:m_menuApp];
96 [m_menuMain addItem:dummyItem];
98 /**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
99 action:nil keyEquivalent:@""];
100 [dummyItem setSubmenu:m_menuWindows];
101 [m_menuMain addItem:dummyItem];
104 [theNSApplication setMainMenu: m_menuMain];
108 wxMenuBarManager::~wxMenuBarManager()
112 void wxMenuBarManager::CreateInstance()
114 sm_mbarmanInstance = new wxMenuBarManager;
117 void wxMenuBarManager::DestroyInstance()
119 delete sm_mbarmanInstance;
120 sm_mbarmanInstance = NULL;
123 void wxMenuBarManager::CocoaInternalIdle()
129 void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
131 m_mainMenuBarInstalled = false;
132 m_needMenuBar = !menubar;
135 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
136 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
137 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
141 void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
143 m_mainMenuBar = menubar;
144 if(m_mainMenuBarInstalled)
148 void wxMenuBarManager::InstallMainMenu()
151 SetMenuBar(m_mainMenuBar);
154 m_needMenuBar = false;
155 m_mainMenuBarInstalled = true;
156 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
157 [[m_menuMain itemAtIndex:0] setSubmenu:m_menuApp];
158 [wxTheApp->GetNSApplication() setMainMenu:m_menuMain];
162 void wxMenuBarManager::WindowDidBecomeKey(wxTopLevelWindowNative *win)
164 wxASSERT(!m_windowKey);
166 InstallMenuBarForWindow(win);
169 void wxMenuBarManager::WindowDidResignKey(wxTopLevelWindowNative *win, bool uninstallMenuBar)
171 wxASSERT(m_windowKey==win);
177 void wxMenuBarManager::WindowDidBecomeMain(wxTopLevelWindowNative *win)
179 wxASSERT(!m_windowMain);
183 void wxMenuBarManager::WindowDidResignMain(wxTopLevelWindowNative *win)
185 wxASSERT(m_windowMain==win);
189 void wxMenuBarManager::InstallMenuBarForWindow(wxTopLevelWindowNative *win)
191 wxMenuBar *menubar = NULL;
192 for(wxTopLevelWindowNative *destwin = win;
194 destwin = wxDynamicCast(destwin->GetParent(), wxTopLevelWindow))
196 menubar = destwin->GetAppMenuBar();
201 void wxMenuBarManager::UpdateWindowMenuBar(wxTopLevelWindowNative *win)
203 InstallMenuBarForWindow(m_windowKey);
206 #endif // wxUSE_MENUS