]>
Commit | Line | Data |
---|---|---|
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 | // ============================================================================ | |
31 | wxMenuBarManager *wxMenuBarManager::sm_mbarmanInstance = NULL; | |
32 | ||
33 | wxMenuBarManager::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 | ||
108 | wxMenuBarManager::~wxMenuBarManager() | |
109 | { | |
110 | } | |
111 | ||
112 | void wxMenuBarManager::CreateInstance() | |
113 | { | |
114 | sm_mbarmanInstance = new wxMenuBarManager; | |
115 | } | |
116 | ||
117 | void wxMenuBarManager::DestroyInstance() | |
118 | { | |
119 | delete sm_mbarmanInstance; | |
120 | sm_mbarmanInstance = NULL; | |
121 | } | |
122 | ||
123 | void wxMenuBarManager::CocoaInternalIdle() | |
124 | { | |
125 | if(m_needMenuBar) | |
126 | InstallMainMenu(); | |
127 | } | |
128 | ||
129 | void 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 | ||
141 | void wxMenuBarManager::SetMainMenuBar(wxMenuBar* menubar) | |
142 | { | |
143 | m_mainMenuBar = menubar; | |
144 | if(m_mainMenuBarInstalled) | |
145 | InstallMainMenu(); | |
146 | } | |
147 | ||
148 | void 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 | ||
162 | void wxMenuBarManager::WindowDidBecomeKey(wxTopLevelWindowNative *win) | |
163 | { | |
c1f12896 | 164 | wxASSERT(!m_windowKey); |
268bec5a DE |
165 | m_windowKey = win; |
166 | InstallMenuBarForWindow(win); | |
167 | } | |
168 | ||
c1f12896 | 169 | void wxMenuBarManager::WindowDidResignKey(wxTopLevelWindowNative *win, bool uninstallMenuBar) |
268bec5a DE |
170 | { |
171 | wxASSERT(m_windowKey==win); | |
172 | m_windowKey = NULL; | |
c1f12896 DE |
173 | if(uninstallMenuBar) |
174 | SetMenuBar(NULL); | |
268bec5a DE |
175 | } |
176 | ||
177 | void wxMenuBarManager::WindowDidBecomeMain(wxTopLevelWindowNative *win) | |
178 | { | |
c1f12896 | 179 | wxASSERT(!m_windowMain); |
268bec5a DE |
180 | m_windowMain = win; |
181 | } | |
182 | ||
183 | void wxMenuBarManager::WindowDidResignMain(wxTopLevelWindowNative *win) | |
184 | { | |
185 | wxASSERT(m_windowMain==win); | |
186 | m_windowMain = NULL; | |
187 | } | |
188 | ||
189 | void wxMenuBarManager::InstallMenuBarForWindow(wxTopLevelWindowNative *win) | |
190 | { | |
191 | wxMenuBar *menubar = NULL; | |
192 | for(wxTopLevelWindowNative *destwin = win; | |
193 | !menubar && destwin; | |
194 | destwin = wxDynamicCast(destwin->GetParent(), wxTopLevelWindow)) | |
195 | { | |
196 | menubar = destwin->GetAppMenuBar(); | |
197 | } | |
198 | SetMenuBar(menubar); | |
199 | } | |
200 | ||
201 | void wxMenuBarManager::UpdateWindowMenuBar(wxTopLevelWindowNative *win) | |
202 | { | |
203 | InstallMenuBarForWindow(m_windowKey); | |
204 | } | |
205 | ||
206 | #endif // wxUSE_MENUS |