]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/mbarman.mm
Give NULL explicit type to make interface recognizeable
[wxWidgets.git] / src / cocoa / mbarman.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/mbarman.cpp
3 // Purpose: wxMenuBarManager implementation
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/09/04
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #if wxUSE_MENUS
14 #ifndef WX_PRECOMP
15 #include "wx/log.h"
16 #include "wx/app.h"
17 #include "wx/menu.h"
18 #include "wx/toplevel.h"
19 #endif // WX_PRECOMP
20
21 #include "wx/cocoa/mbarman.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/objc/objc_uniquifying.h"
24
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSNotification.h>
27 #import <AppKit/NSMenu.h>
28 #import <AppKit/NSApplication.h>
29 #import <AppKit/NSWindow.h>
30
31 // Declare setAppleMenu: in an NSApplication category since Tiger and later
32 // releases support it but don't declare it as it's considered deprecated.
33 @interface NSApplication(wxDeprecatedMethodsWeWantToUse)
34 - (void)setAppleMenu:(NSMenu *)menu;
35 @end
36
37 // ============================================================================
38 // wxMenuBarManagerObserver
39 // ============================================================================
40 @interface wxMenuBarManagerObserver : NSObject
41 {
42 wxMenuBarManager *m_mbarman;
43 }
44
45 - (id)init;
46 - (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman;
47 - (void)windowDidBecomeKey: (NSNotification *)notification;
48 #if 0
49 - (void)windowDidResignKey: (NSNotification *)notification;
50 - (void)windowDidBecomeMain: (NSNotification *)notification;
51 - (void)windowDidResignMain: (NSNotification *)notification;
52 - (void)windowWillClose: (NSNotification *)notification;
53 #endif // 0
54 @end // interface wxMenuBarManagerObserver : NSObject
55 WX_DECLARE_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
56
57 @implementation wxMenuBarManagerObserver : NSObject
58 - (id)init
59 {
60 wxFAIL_MSG(wxT("[wxMenuBarManagerObserver -init] should never be called!"));
61 m_mbarman = NULL;
62 return self;
63 }
64
65 - (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman
66 {
67 wxASSERT(mbarman);
68 m_mbarman = mbarman;
69 return [super init];
70 }
71
72 - (void)windowDidBecomeKey: (NSNotification *)notification
73 {
74 wxASSERT(m_mbarman);
75 m_mbarman->WindowDidBecomeKey(notification);
76 }
77
78 #if 0
79 - (void)windowDidResignKey: (NSNotification *)notification
80 {
81 wxASSERT(m_mbarman);
82 m_mbarman->WindowDidResignKey(notification);
83 }
84
85 - (void)windowDidBecomeMain: (NSNotification *)notification
86 {
87 wxASSERT(m_mbarman);
88 m_mbarman->WindowDidBecomeMain(notification);
89 }
90
91 - (void)windowDidResignMain: (NSNotification *)notification
92 {
93 wxASSERT(m_mbarman);
94 m_mbarman->WindowDidResignMain(notification);
95 }
96
97 - (void)windowWillClose: (NSNotification *)notification
98 {
99 wxASSERT(m_mbarman);
100 m_mbarman->WindowWillClose(notification);
101 }
102 #endif // 0
103
104 @end // implementation wxMenuBarManagerObserver : NSObject
105 WX_IMPLEMENT_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
106
107 // ============================================================================
108 // wxMenuBarManager
109 // ============================================================================
110 wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
111
112 wxMenuBarManager::wxMenuBarManager()
113 {
114 m_observer = [[WX_GET_OBJC_CLASS(wxMenuBarManagerObserver) alloc]
115 initWithWxMenuBarManager:this];
116 [[NSNotificationCenter defaultCenter] addObserver:m_observer
117 selector:@selector(windowDidBecomeKey:)
118 name:NSWindowDidBecomeKeyNotification object:nil];
119 #if 0
120 [[NSNotificationCenter defaultCenter] addObserver:m_observer
121 selector:@selector(windowDidResignKey:)
122 name:NSWindowDidResignKeyNotification object:nil];
123 [[NSNotificationCenter defaultCenter] addObserver:m_observer
124 selector:@selector(windowDidBecomeMain:)
125 name:NSWindowDidBecomeMainNotification object:nil];
126 [[NSNotificationCenter defaultCenter] addObserver:m_observer
127 selector:@selector(windowDidResignMain:)
128 name:NSWindowDidResignMainNotification object:nil];
129 [[NSNotificationCenter defaultCenter] addObserver:m_observer
130 selector:@selector(windowWillClose:)
131 name:NSWindowWillCloseNotification object:nil];
132 #endif // 0
133 m_menuApp = nil;
134 m_menuServices = nil;
135 m_menuWindows = nil;
136 m_menuMain = nil;
137 m_mainMenuBarInstalled = true;
138 m_mainMenuBar = NULL;
139 m_currentNSWindow = nil;
140
141 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
142 // Create the services menu.
143 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
144 [theNSApplication setServicesMenu:m_menuServices];
145
146 NSMenuItem *menuitem;
147 // Create the application (Apple) menu.
148 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
149
150 /**/[m_menuApp addItemWithTitle:@"Preferences..." action:nil keyEquivalent:@""];
151 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
152 /**/menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" action:nil keyEquivalent:@""];
153 [menuitem setSubmenu:m_menuServices];
154 [m_menuApp addItem: menuitem];
155 [menuitem release];
156 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
157 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@""];
158 [menuitem setTarget: theNSApplication];
159 [m_menuApp addItem: menuitem];
160 [menuitem release];
161 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
162 [menuitem setTarget: theNSApplication];
163 [m_menuApp addItem: menuitem];
164 [menuitem release];
165 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
166 [menuitem setTarget: theNSApplication];
167 [m_menuApp addItem: menuitem];
168 [menuitem release];
169 /**/[m_menuApp addItem: [NSMenuItem separatorItem]];
170 /**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
171 [menuitem setTarget: theNSApplication];
172 [m_menuApp addItem: menuitem];
173 [menuitem release];
174
175 [theNSApplication setAppleMenu:m_menuApp];
176
177 // Create the Windows menu
178 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
179
180 /**/[m_menuWindows addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
181 /**/[m_menuWindows addItem: [NSMenuItem separatorItem]];
182 /**/[m_menuWindows addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
183
184 [theNSApplication setWindowsMenu:m_menuWindows];
185
186 // Create the main menubar
187 m_menuMain = [[NSMenu alloc] initWithTitle: @"wxApp Menu"];
188 /**/NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
189 /* Note: title gets clobbered by app name anyway */
190 action:nil keyEquivalent:@""];
191 [dummyItem setSubmenu:m_menuApp];
192 [m_menuMain addItem:dummyItem];
193 [dummyItem release];
194 /**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
195 action:nil keyEquivalent:@""];
196 [dummyItem setSubmenu:m_menuWindows];
197 [m_menuMain addItem:dummyItem];
198 [dummyItem release];
199
200 [theNSApplication setMainMenu: m_menuMain];
201
202 }
203
204 wxMenuBarManager::~wxMenuBarManager()
205 {
206 [m_observer release];
207 }
208
209 void wxMenuBarManager::CreateInstance()
210 {
211 sm_mbarmanInstance = new wxMenuBarManager;
212 }
213
214 void wxMenuBarManager::DestroyInstance()
215 {
216 delete sm_mbarmanInstance;
217 sm_mbarmanInstance = NULL;
218 }
219
220 void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
221 {
222 m_mainMenuBarInstalled = false;
223 if(menubar)
224 {
225 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
226 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
227 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
228 }
229 else
230 InstallMainMenu();
231 }
232
233 void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
234 {
235 m_mainMenuBar = menubar;
236 if(m_mainMenuBarInstalled)
237 InstallMainMenu();
238 }
239
240 void wxMenuBarManager::InstallMainMenu()
241 {
242 if(m_mainMenuBar)
243 SetMenuBar(m_mainMenuBar);
244 else
245 {
246 m_mainMenuBarInstalled = true;
247 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
248 [[m_menuMain itemAtIndex:0] setSubmenu:m_menuApp];
249 [wxTheApp->GetNSApplication() setMainMenu:m_menuMain];
250 }
251 }
252
253 void wxMenuBarManager::WindowDidBecomeKey(NSNotification *notification)
254 {
255 /* NOTE: m_currentNSWindow might be destroyed but we only ever use it
256 to look it up in the hash table. Do not send messages to it. */
257 m_currentNSWindow = [notification object];
258 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
259 if(win)
260 InstallMenuBarForWindow(win);
261 else
262 SetMenuBar(NULL);
263 }
264
265 #if 0
266 void wxMenuBarManager::WindowDidResignKey(NSNotification *notification)
267 {
268 }
269
270 void wxMenuBarManager::WindowDidBecomeMain(NSNotification *notification)
271 {
272 }
273
274 void wxMenuBarManager::WindowDidResignMain(NSNotification *notification)
275 {
276 }
277
278 void wxMenuBarManager::WindowWillClose(NSNotification *notification)
279 {
280 }
281 #endif // 0
282
283 void wxMenuBarManager::InstallMenuBarForWindow(wxCocoaNSWindow *win)
284 {
285 wxASSERT(win);
286 wxMenuBar *menubar = win->GetAppMenuBar(win);
287 wxLogTrace(wxTRACE_COCOA,wxT("Found menubar=%p for window=%p."),menubar,win);
288 SetMenuBar(menubar);
289 }
290
291 void wxMenuBarManager::UpdateMenuBar()
292 {
293 if(m_currentNSWindow)
294 {
295 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
296 if(win)
297 InstallMenuBarForWindow(win);
298 }
299 }
300
301 #endif // wxUSE_MENUS