]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/mbarman.mm
Oops, I left an extra { in there.
[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
9// Licence: wxWindows 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
24#import <Foundation/NSString.h>
25#import <AppKit/NSMenu.h>
26#import <AppKit/NSApplication.h>
27
28// ============================================================================
29// wxMenuBarManager
30// ============================================================================
31wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL;
32
33wxMenuBarManager::wxMenuBarManager()
34{
35 m_menuApp = nil;
36 m_menuServices = nil;
37 m_menuWindows = nil;
38 m_menuMain = nil;
39 m_needMenuBar = true;
40 m_mainMenuBarInstalled = true;
41 m_mainMenuBar = NULL;
42 m_windowKey = NULL;
43 m_windowMain = NULL;
44
45 NSApplication *theNSApplication = wxTheApp->GetNSApplication();
46 // Create the services menu.
47 m_menuServices = [[NSMenu alloc] initWithTitle: @"Services"];
48 [theNSApplication setServicesMenu:m_menuServices];
49
50 NSMenuItem *menuitem;
51 // Create the application (Apple) menu.
52 m_menuApp = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
53
54/**/[m_menuApp addItemWithTitle:@"Preferences..." action:nil keyEquivalent:@""];
55/**/[m_menuApp addItem: [NSMenuItem separatorItem]];
56/**/menuitem = [[NSMenuItem alloc] initWithTitle: @"Services" action:nil keyEquivalent:@""];
57 [menuitem setSubmenu:m_menuServices];
58 [m_menuApp addItem: menuitem];
59 [menuitem release];
60/**/[m_menuApp addItem: [NSMenuItem separatorItem]];
61/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@""];
62 [menuitem setTarget: theNSApplication];
63 [m_menuApp addItem: menuitem];
64 [menuitem release];
65/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
66 [menuitem setTarget: theNSApplication];
67 [m_menuApp addItem: menuitem];
68 [menuitem release];
69/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
70 [menuitem setTarget: theNSApplication];
71 [m_menuApp addItem: menuitem];
72 [menuitem release];
73/**/[m_menuApp addItem: [NSMenuItem separatorItem]];
74/**/menuitem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"Q"];
75 [menuitem setTarget: theNSApplication];
76 [m_menuApp addItem: menuitem];
77 [menuitem release];
78
79 [theNSApplication setAppleMenu:m_menuApp];
80
81 // Create the Windows menu
82 m_menuWindows = [[NSMenu alloc] initWithTitle: @"Window"];
83
84/**/[m_menuWindows addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
85/**/[m_menuWindows addItem: [NSMenuItem separatorItem]];
86/**/[m_menuWindows addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
87
88 [theNSApplication setWindowsMenu:m_menuWindows];
89
90 // Create the main menubar
91 m_menuMain = [[NSMenu alloc] initWithTitle: @"wxApp Menu"];
92/**/NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
93 /* Note: title gets clobbered by app name anyway */
94 action:nil keyEquivalent:@""];
95 [dummyItem setSubmenu:m_menuApp];
96 [m_menuMain addItem:dummyItem];
97 [dummyItem release];
98/**/dummyItem = [[NSMenuItem alloc] initWithTitle:@"Window"
99 action:nil keyEquivalent:@""];
100 [dummyItem setSubmenu:m_menuWindows];
101 [m_menuMain addItem:dummyItem];
102 [dummyItem release];
103
104 [theNSApplication setMainMenu: m_menuMain];
105
106}
107
108wxMenuBarManager::~wxMenuBarManager()
109{
110}
111
112void wxMenuBarManager::CreateInstance()
113{
114 sm_mbarmanInstance = new wxMenuBarManager;
115}
116
117void wxMenuBarManager::DestroyInstance()
118{
119 delete sm_mbarmanInstance;
120 sm_mbarmanInstance = NULL;
121}
122
123void wxMenuBarManager::CocoaInternalIdle()
124{
125 if(m_needMenuBar)
126 InstallMainMenu();
127}
128
129void wxMenuBarManager::SetMenuBar(wxMenuBar* menubar)
130{
131 m_mainMenuBarInstalled = false;
132 m_needMenuBar = !menubar;
133 if(menubar)
134 {
135 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
136 [[menubar->GetNSMenu() itemAtIndex:0] setSubmenu:m_menuApp];
137 [wxTheApp->GetNSApplication() setMainMenu:menubar->GetNSMenu()];
138 }
139}
140
141void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar)
142{
143 m_mainMenuBar = menubar;
144 if(m_mainMenuBarInstalled)
145 InstallMainMenu();
146}
147
148void wxMenuBarManager::InstallMainMenu()
149{
150 if(m_mainMenuBar)
151 SetMenuBar(m_mainMenuBar);
152 else
153 {
154 m_needMenuBar = false;
155 m_mainMenuBarInstalled = true;
156 [[[wxTheApp->GetNSApplication() mainMenu] itemAtIndex:0] setSubmenu:nil];
157 [[m_menuMain itemAtIndex:0] setSubmenu:m_menuApp];
158 [wxTheApp->GetNSApplication() setMainMenu:m_menuMain];
159 }
160}
161
162void wxMenuBarManager::WindowDidBecomeKey(wxTopLevelWindowNative *win)
163{
f5382a2f 164// wxASSERT(!m_windowKey);
268bec5a
DE
165 m_windowKey = win;
166 InstallMenuBarForWindow(win);
167}
168
169void wxMenuBarManager::WindowDidResignKey(wxTopLevelWindowNative *win)
170{
171 wxASSERT(m_windowKey==win);
172 m_windowKey = NULL;
173 SetMenuBar(NULL);
174}
175
176void wxMenuBarManager::WindowDidBecomeMain(wxTopLevelWindowNative *win)
177{
f5382a2f 178// wxASSERT(!m_windowMain);
268bec5a
DE
179 m_windowMain = win;
180}
181
182void wxMenuBarManager::WindowDidResignMain(wxTopLevelWindowNative *win)
183{
184 wxASSERT(m_windowMain==win);
185 m_windowMain = NULL;
186}
187
188void wxMenuBarManager::InstallMenuBarForWindow(wxTopLevelWindowNative *win)
189{
190 wxMenuBar *menubar = NULL;
191 for(wxTopLevelWindowNative *destwin = win;
192 !menubar && destwin;
193 destwin = wxDynamicCast(destwin->GetParent(), wxTopLevelWindow))
194 {
195 menubar = destwin->GetAppMenuBar();
196 }
197 SetMenuBar(menubar);
198}
199
200void wxMenuBarManager::UpdateWindowMenuBar(wxTopLevelWindowNative *win)
201{
202 InstallMenuBarForWindow(m_windowKey);
203}
204
205#endif // wxUSE_MENUS