]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/mbarman.mm
fix for HP and IBM compilers, calls to static template functions must be qualified
[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
9c41aa97
DE
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
f1d04a42
DE
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
e7e1ad7d 55WX_DECLARE_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
f1d04a42
DE
56
57@implementation wxMenuBarManagerObserver : NSObject
58- (id)init
59{
2b030203 60 wxFAIL_MSG(wxT("[wxMenuBarManagerObserver -init] should never be called!"));
f1d04a42
DE
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
e7e1ad7d 105WX_IMPLEMENT_GET_OBJC_CLASS(wxMenuBarManagerObserver,NSObject)
268bec5a
DE
106
107// ============================================================================
108// wxMenuBarManager
109// ============================================================================
110wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
111
112wxMenuBarManager::wxMenuBarManager()
113{
e7e1ad7d 114 m_observer = [[WX_GET_OBJC_CLASS(wxMenuBarManagerObserver) alloc]
f1d04a42
DE
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
268bec5a
DE
133 m_menuApp = nil;
134 m_menuServices = nil;
135 m_menuWindows = nil;
136 m_menuMain = nil;
268bec5a
DE
137 m_mainMenuBarInstalled = true;
138 m_mainMenuBar = NULL;
af849107 139 m_currentNSWindow = nil;
268bec5a
DE
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]];
372208c2 170/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
268bec5a
DE
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
204wxMenuBarManager::~wxMenuBarManager()
205{
f1d04a42 206 [m_observer release];
268bec5a
DE
207}
208
209void wxMenuBarManager::CreateInstance()
210{
211 sm_mbarmanInstance = new wxMenuBarManager;
212}
213
214void wxMenuBarManager::DestroyInstance()
215{
216 delete sm_mbarmanInstance;
217 sm_mbarmanInstance = NULL;
218}
219
268bec5a
DE
220void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
221{
222 m_mainMenuBarInstalled = false;
268bec5a
DE
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 }
f1d04a42
DE
229 else
230 InstallMainMenu();
268bec5a
DE
231}
232
233void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
234{
235 m_mainMenuBar = menubar;
236 if(m_mainMenuBarInstalled)
237 InstallMainMenu();
238}
239
240void wxMenuBarManager::InstallMainMenu()
241{
242 if(m_mainMenuBar)
243 SetMenuBar(m_mainMenuBar);
244 else
245 {
268bec5a
DE
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
f1d04a42
DE
253void wxMenuBarManager::WindowDidBecomeKey(NSNotification *notification)
254{
af849107
DE
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);
f1d04a42
DE
259 if(win)
260 InstallMenuBarForWindow(win);
261 else
262 SetMenuBar(NULL);
263}
264
265#if 0
266void wxMenuBarManager::WindowDidResignKey(NSNotification *notification)
268bec5a 267{
268bec5a
DE
268}
269
f1d04a42 270void wxMenuBarManager::WindowDidBecomeMain(NSNotification *notification)
268bec5a 271{
268bec5a
DE
272}
273
f1d04a42 274void wxMenuBarManager::WindowDidResignMain(NSNotification *notification)
268bec5a 275{
268bec5a
DE
276}
277
f1d04a42 278void wxMenuBarManager::WindowWillClose(NSNotification *notification)
268bec5a 279{
268bec5a 280}
f1d04a42 281#endif // 0
268bec5a 282
f1d04a42 283void wxMenuBarManager::InstallMenuBarForWindow(wxCocoaNSWindow *win)
268bec5a 284{
3e84f98f 285 wxASSERT(win);
8ded703d 286 wxMenuBar *menubar = win->GetAppMenuBar(win);
48580976 287 wxLogTrace(wxTRACE_COCOA,wxT("Found menubar=%p for window=%p."),menubar,win);
268bec5a
DE
288 SetMenuBar(menubar);
289}
290
243f5c2d 291void wxMenuBarManager::UpdateMenuBar()
268bec5a 292{
af849107
DE
293 if(m_currentNSWindow)
294 {
295 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
296 if(win)
297 InstallMenuBarForWindow(win);
298 }
268bec5a
DE
299}
300
301#endif // wxUSE_MENUS