]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/mbarman.mm
Halfway reasonable implementation of wxFont for wxCocoa.
[wxWidgets.git] / src / cocoa / mbarman.mm
CommitLineData
268bec5a
DE
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
065e208e 9// Licence: wxWidgets licence
268bec5a
DE
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"
e7e1ad7d 23#include "wx/cocoa/objc/objc_uniquifying.h"
268bec5a
DE
24
25#import <Foundation/NSString.h>
f1d04a42 26#import <Foundation/NSNotification.h>
268bec5a
DE
27#import <AppKit/NSMenu.h>
28#import <AppKit/NSApplication.h>
f1d04a42
DE
29#import <AppKit/NSWindow.h>
30
0206be63 31#define wxUSE_FSCRIPT 0
82ba4885
DE
32#if wxUSE_FSCRIPT
33 #import <FScript/FScriptMenuItem.h>
34#endif
35
9c41aa97
DE
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
f1d04a42
DE
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
e7e1ad7d 60WX_DECLARE_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
f1d04a42
DE
61
62@implementation wxMenuBarManagerObserver : NSObject
63- (id)init
64{
2b030203 65 wxFAIL_MSG(wxT("[wxMenuBarManagerObserver -init] should never be called!"));
f1d04a42
DE
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
e7e1ad7d 110WX_IMPLEMENT_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
268bec5a
DE
111
112// ============================================================================
113// wxMenuBarManager
114// ============================================================================
115wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
116
82ba4885
DE
117static 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
268bec5a
DE
128wxMenuBarManager::wxMenuBarManager()
129{
e7e1ad7d 130 m_observer = [[WX_GET_OBJC_CLASS(wxMenuBarManagerObserver) alloc]
f1d04a42
DE
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
268bec5a
DE
149 m_menuApp = nil;
150 m_menuServices = nil;
151 m_menuWindows = nil;
152 m_menuMain = nil;
268bec5a
DE
153 m_mainMenuBarInstalled = true;
154 m_mainMenuBar = NULL;
af849107 155 m_currentNSWindow = nil;
268bec5a
DE
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]];
82ba4885 168/**/AddFScriptItem(m_menuApp);
268bec5a
DE
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]];
372208c2 187/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
268bec5a
DE
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
221wxMenuBarManager::~wxMenuBarManager()
222{
f1d04a42 223 [m_observer release];
268bec5a
DE
224}
225
226void wxMenuBarManager::CreateInstance()
227{
228 sm_mbarmanInstance = new wxMenuBarManager;
229}
230
231void wxMenuBarManager::DestroyInstance()
232{
233 delete sm_mbarmanInstance;
234 sm_mbarmanInstance = NULL;
235}
236
268bec5a
DE
237void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
238{
239 m_mainMenuBarInstalled = false;
268bec5a
DE
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 }
f1d04a42
DE
246 else
247 InstallMainMenu();
268bec5a
DE
248}
249
250void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
251{
252 m_mainMenuBar = menubar;
253 if(m_mainMenuBarInstalled)
254 InstallMainMenu();
255}
256
257void wxMenuBarManager::InstallMainMenu()
258{
259 if(m_mainMenuBar)
260 SetMenuBar(m_mainMenuBar);
261 else
262 {
268bec5a
DE
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
f1d04a42
DE
270void wxMenuBarManager::WindowDidBecomeKey(NSNotification *notification)
271{
af849107
DE
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);
f1d04a42
DE
276 if(win)
277 InstallMenuBarForWindow(win);
278 else
279 SetMenuBar(NULL);
280}
281
282#if 0
283void wxMenuBarManager::WindowDidResignKey(NSNotification *notification)
268bec5a 284{
268bec5a
DE
285}
286
f1d04a42 287void wxMenuBarManager::WindowDidBecomeMain(NSNotification *notification)
268bec5a 288{
268bec5a
DE
289}
290
f1d04a42 291void wxMenuBarManager::WindowDidResignMain(NSNotification *notification)
268bec5a 292{
268bec5a
DE
293}
294
f1d04a42 295void wxMenuBarManager::WindowWillClose(NSNotification *notification)
268bec5a 296{
268bec5a 297}
f1d04a42 298#endif // 0
268bec5a 299
f1d04a42 300void wxMenuBarManager::InstallMenuBarForWindow(wxCocoaNSWindow *win)
268bec5a 301{
3e84f98f 302 wxASSERT(win);
8ded703d 303 wxMenuBar *menubar = win->GetAppMenuBar(win);
48580976 304 wxLogTrace(wxTRACE_COCOA,wxT("Found menubar=%p for window=%p."),menubar,win);
268bec5a
DE
305 SetMenuBar(menubar);
306}
307
243f5c2d 308void wxMenuBarManager::UpdateMenuBar()
268bec5a 309{
af849107
DE
310 if(m_currentNSWindow)
311 {
312 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
313 if(win)
314 InstallMenuBarForWindow(win);
315 }
268bec5a
DE
316}
317
318#endif // wxUSE_MENUS