]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/mbarman.mm
The mmedia contrib requires sys/soundcard.h on unix, so disable it on unix
[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"
23
24#import <Foundation/NSString.h>
f1d04a42 25#import <Foundation/NSNotification.h>
268bec5a
DE
26#import <AppKit/NSMenu.h>
27#import <AppKit/NSApplication.h>
f1d04a42
DE
28#import <AppKit/NSWindow.h>
29
30// ============================================================================
31// wxMenuBarManagerObserver
32// ============================================================================
33@interface wxMenuBarManagerObserver : NSObject
34{
35 wxMenuBarManager *m_mbarman;
36}
37
38- (id)init;
39- (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman;
40- (void)windowDidBecomeKey: (NSNotification *)notification;
41#if 0
42- (void)windowDidResignKey: (NSNotification *)notification;
43- (void)windowDidBecomeMain: (NSNotification *)notification;
44- (void)windowDidResignMain: (NSNotification *)notification;
45- (void)windowWillClose: (NSNotification *)notification;
46#endif // 0
47@end // interface wxMenuBarManagerObserver : NSObject
48
49@implementation wxMenuBarManagerObserver : NSObject
50- (id)init
51{
2b030203 52 wxFAIL_MSG(wxT("[wxMenuBarManagerObserver -init] should never be called!"));
f1d04a42
DE
53 m_mbarman = NULL;
54 return self;
55}
56
57- (id)initWithWxMenuBarManager: (wxMenuBarManager *)mbarman
58{
59 wxASSERT(mbarman);
60 m_mbarman = mbarman;
61 return [super init];
62}
63
64- (void)windowDidBecomeKey: (NSNotification *)notification
65{
66 wxASSERT(m_mbarman);
67 m_mbarman->WindowDidBecomeKey(notification);
68}
69
70#if 0
71- (void)windowDidResignKey: (NSNotification *)notification
72{
73 wxASSERT(m_mbarman);
74 m_mbarman->WindowDidResignKey(notification);
75}
76
77- (void)windowDidBecomeMain: (NSNotification *)notification
78{
79 wxASSERT(m_mbarman);
80 m_mbarman->WindowDidBecomeMain(notification);
81}
82
83- (void)windowDidResignMain: (NSNotification *)notification
84{
85 wxASSERT(m_mbarman);
86 m_mbarman->WindowDidResignMain(notification);
87}
88
89- (void)windowWillClose: (NSNotification *)notification
90{
91 wxASSERT(m_mbarman);
92 m_mbarman->WindowWillClose(notification);
93}
94#endif // 0
95
96@end // implementation wxMenuBarManagerObserver : NSObject
268bec5a
DE
97
98// ============================================================================
99// wxMenuBarManager
100// ============================================================================
101wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
102
103wxMenuBarManager::wxMenuBarManager()
104{
f1d04a42
DE
105 m_observer = [[wxMenuBarManagerObserver alloc]
106 initWithWxMenuBarManager:this];
107 [[NSNotificationCenter defaultCenter] addObserver:m_observer
108 selector:@selector(windowDidBecomeKey:)
109 name:NSWindowDidBecomeKeyNotification object:nil];
110#if 0
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];
123#endif // 0
268bec5a
DE
124 m_menuApp = nil;
125 m_menuServices = nil;
126 m_menuWindows = nil;
127 m_menuMain = nil;
268bec5a
DE
128 m_mainMenuBarInstalled = true;
129 m_mainMenuBar = NULL;
af849107 130 m_currentNSWindow = nil;
268bec5a
DE
131
132 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
133 // Create the services menu.
134 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
135 [theNSApplication setServicesMenu:m_menuServices];
136
137 NSMenuItem *menuitem;
138 // Create the application (Apple) menu.
139 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
140
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];
146 [menuitem release];
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];
151 [menuitem release];
152/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
153 [menuitem setTarget: theNSApplication];
154 [m_menuApp addItem: menuitem];
155 [menuitem release];
156/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
157 [menuitem setTarget: theNSApplication];
158 [m_menuApp addItem: menuitem];
159 [menuitem release];
160/**/[m_menuApp addItem: [NSMenuItem separatorItem]];
372208c2 161/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
268bec5a
DE
162 [menuitem setTarget: theNSApplication];
163 [m_menuApp addItem: menuitem];
164 [menuitem release];
165
166 [theNSApplication setAppleMenu:m_menuApp];
167
168 // Create the Windows menu
169 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
170
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:@""];
174
175 [theNSApplication setWindowsMenu:m_menuWindows];
176
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];
184 [dummyItem release];
185/**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
186 action:nil keyEquivalent:@""];
187 [dummyItem setSubmenu:m_menuWindows];
188 [m_menuMain addItem:dummyItem];
189 [dummyItem release];
190
191 [theNSApplication setMainMenu: m_menuMain];
192
193}
194
195wxMenuBarManager::~wxMenuBarManager()
196{
f1d04a42 197 [m_observer release];
268bec5a
DE
198}
199
200void wxMenuBarManager::CreateInstance()
201{
202 sm_mbarmanInstance = new wxMenuBarManager;
203}
204
205void wxMenuBarManager::DestroyInstance()
206{
207 delete sm_mbarmanInstance;
208 sm_mbarmanInstance = NULL;
209}
210
268bec5a
DE
211void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
212{
213 m_mainMenuBarInstalled = false;
268bec5a
DE
214 if(menubar)
215 {
216 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
217 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
218 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
219 }
f1d04a42
DE
220 else
221 InstallMainMenu();
268bec5a
DE
222}
223
224void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
225{
226 m_mainMenuBar = menubar;
227 if(m_mainMenuBarInstalled)
228 InstallMainMenu();
229}
230
231void wxMenuBarManager::InstallMainMenu()
232{
233 if(m_mainMenuBar)
234 SetMenuBar(m_mainMenuBar);
235 else
236 {
268bec5a
DE
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];
241 }
242}
243
f1d04a42
DE
244void wxMenuBarManager::WindowDidBecomeKey(NSNotification *notification)
245{
af849107
DE
246 /* NOTE: m_currentNSWindow might be destroyed but we only ever use it
247 to look it up in the hash table. Do not send messages to it. */
248 m_currentNSWindow = [notification object];
249 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
f1d04a42
DE
250 if(win)
251 InstallMenuBarForWindow(win);
252 else
253 SetMenuBar(NULL);
254}
255
256#if 0
257void wxMenuBarManager::WindowDidResignKey(NSNotification *notification)
268bec5a 258{
268bec5a
DE
259}
260
f1d04a42 261void wxMenuBarManager::WindowDidBecomeMain(NSNotification *notification)
268bec5a 262{
268bec5a
DE
263}
264
f1d04a42 265void wxMenuBarManager::WindowDidResignMain(NSNotification *notification)
268bec5a 266{
268bec5a
DE
267}
268
f1d04a42 269void wxMenuBarManager::WindowWillClose(NSNotification *notification)
268bec5a 270{
268bec5a 271}
f1d04a42 272#endif // 0
268bec5a 273
f1d04a42 274void wxMenuBarManager::InstallMenuBarForWindow(wxCocoaNSWindow *win)
268bec5a 275{
3e84f98f 276 wxASSERT(win);
8ded703d 277 wxMenuBar *menubar = win->GetAppMenuBar(win);
48580976 278 wxLogTrace(wxTRACE_COCOA,wxT("Found menubar=%p for window=%p."),menubar,win);
268bec5a
DE
279 SetMenuBar(menubar);
280}
281
243f5c2d 282void wxMenuBarManager::UpdateMenuBar()
268bec5a 283{
af849107
DE
284 if(m_currentNSWindow)
285 {
286 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa(m_currentNSWindow);
287 if(win)
288 InstallMenuBarForWindow(win);
289 }
268bec5a
DE
290}
291
292#endif // wxUSE_MENUS