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 <Foundation/NSNotification.h>
26 #import <AppKit/NSMenu.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSWindow.h>
30 // ============================================================================
31 // wxMenuBarManagerObserver
32 // ============================================================================
33 @interface wxMenuBarManagerObserver : NSObject
35 wxMenuBarManager *m_mbarman;
39 - (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman;
40 - (void)windowDidBecomeKey: (NSNotification *)notification;
42 - (void)windowDidResignKey: (NSNotification *)notification;
43 - (void)windowDidBecomeMain: (NSNotification *)notification;
44 - (void)windowDidResignMain: (NSNotification *)notification;
45 - (void)windowWillClose: (NSNotification *)notification;
47 @end // interface wxMenuBarManagerObserver : NSObject
49 @implementation wxMenuBarManagerObserver : NSObject
52 wxFAIL_MSG("[wxMenuBarManagerObserver -init] should never be called!");
57 - (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman
64 - (void)windowDidBecomeKey: (NSNotification *)notification
67 m_mbarman->WindowDidBecomeKey(notification);
71 - (void)windowDidResignKey: (NSNotification *)notification
74 m_mbarman->WindowDidResignKey(notification);
77 - (void)windowDidBecomeMain: (NSNotification *)notification
80 m_mbarman->WindowDidBecomeMain(notification);
83 - (void)windowDidResignMain: (NSNotification *)notification
86 m_mbarman->WindowDidResignMain(notification);
89 - (void)windowWillClose: (NSNotification *)notification
92 m_mbarman->WindowWillClose(notification);
96 @end // implementation wxMenuBarManagerObserver : NSObject
98 // ============================================================================
100 // ============================================================================
101 wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
103 wxMenuBarManager::wxMenuBarManager()
105 m_observer = [[wxMenuBarManagerObserver alloc]
106 initWithWxMenuBarManager:this];
107 [[NSNotificationCenter defaultCenter] addObserver:m_observer
108 selector:@selector(windowDidBecomeKey:)
109 name:NSWindowDidBecomeKeyNotification object:nil];
111 [[NSNotificationCenter defaultCenter] addObserver:m_observer
112 selector:@selector(windowDidResignKey:)
113 name:NSWindowDidResignKeyNotification object:nil];
114 [[NSNotificationCenter defaultCenter] addObserver:m_observer
115 selector:@selector(windowDidBecomeMain:)
116 name:NSWindowDidBecomeMainNotification object:nil];
117 [[NSNotificationCenter defaultCenter] addObserver:m_observer
118 selector:@selector(windowDidResignMain:)
119 name:NSWindowDidResignMainNotification object:nil];
120 [[NSNotificationCenter defaultCenter] addObserver:m_observer
121 selector:@selector(windowWillClose:)
122 name:NSWindowWillCloseNotification object:nil];
125 m_menuServices = nil;
128 m_mainMenuBarInstalled = true;
129 m_mainMenuBar = NULL;
130 m_windowCurrent = NULL;
132 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
133 // Create the services menu.
134 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
135 [theNSApplication setServicesMenu:m_menuServices];
137 NSMenuItem *menuitem;
138 // Create the application (Apple) menu.
139 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
141 /**/[m_menuApp addItemWithTitle:@"Preferences..." action:nil keyEquivalent:@""];
142 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
143 /**/menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" action:nil keyEquivalent:@""];
144 [menuitem setSubmenu:m_menuServices];
145 [m_menuApp addItem: menuitem];
147 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
148 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@""];
149 [menuitem setTarget: theNSApplication];
150 [m_menuApp addItem: menuitem];
152 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
153 [menuitem setTarget: theNSApplication];
154 [m_menuApp addItem: menuitem];
156 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
157 [menuitem setTarget: theNSApplication];
158 [m_menuApp addItem: menuitem];
160 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
161 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
162 [menuitem setTarget: theNSApplication];
163 [m_menuApp addItem: menuitem];
166 [theNSApplication setAppleMenu:m_menuApp];
168 // Create the Windows menu
169 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
171 /**/[m_menuWindows addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
172 /**/[m_menuWindows addItem: [NSMenuItem separatorItem]];
173 /**/[m_menuWindows addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
175 [theNSApplication setWindowsMenu:m_menuWindows];
177 // Create the main menubar
178 m_menuMain = [[NSMenu alloc] initWithTitle: @"wxApp Menu"];
179 /**/NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
180 /* Note: title gets clobbered by app name anyway */
181 action:nil keyEquivalent:@""];
182 [dummyItem setSubmenu:m_menuApp];
183 [m_menuMain addItem:dummyItem];
185 /**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
186 action:nil keyEquivalent:@""];
187 [dummyItem setSubmenu:m_menuWindows];
188 [m_menuMain addItem:dummyItem];
191 [theNSApplication setMainMenu: m_menuMain];
195 wxMenuBarManager::~wxMenuBarManager()
197 [m_observer release];
200 void wxMenuBarManager::CreateInstance()
202 sm_mbarmanInstance = new wxMenuBarManager;
205 void wxMenuBarManager::DestroyInstance()
207 delete sm_mbarmanInstance;
208 sm_mbarmanInstance = NULL;
211 void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
213 m_mainMenuBarInstalled = false;
216 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
217 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
218 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
224 void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
226 m_mainMenuBar = menubar;
227 if(m_mainMenuBarInstalled)
231 void wxMenuBarManager::InstallMainMenu()
234 SetMenuBar(m_mainMenuBar);
237 m_mainMenuBarInstalled = true;
238 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
239 [[m_menuMain itemAtIndex:0] setSubmenu:m_menuApp];
240 [wxTheApp->GetNSApplication() setMainMenu:m_menuMain];
244 void wxMenuBarManager::WindowDidBecomeKey(NSNotification *notification)
246 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
248 InstallMenuBarForWindow(win);
254 void wxMenuBarManager::WindowDidResignKey(NSNotification *notification)
258 void wxMenuBarManager::WindowDidBecomeMain(NSNotification *notification)
262 void wxMenuBarManager::WindowDidResignMain(NSNotification *notification)
266 void wxMenuBarManager::WindowWillClose(NSNotification *notification)
271 void wxMenuBarManager::InstallMenuBarForWindow(wxCocoaNSWindow *win)
274 m_windowCurrent = win;
275 wxMenuBar *menubar = win->GetAppMenuBar(win);
276 wxLogDebug("Found menubar=%p for window=%p.",menubar,win);
280 void wxMenuBarManager::UpdateMenuBar()
283 InstallMenuBarForWindow(m_windowCurrent);
286 #endif // wxUSE_MENUS