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