author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qmacdefines_mac.h" |
|
43 |
#ifdef QT_MAC_USE_COCOA |
|
44 |
#include <qaction.h> |
|
45 |
#include <qcoreapplication.h> |
|
46 |
#include <private/qcocoamenuloader_mac_p.h> |
|
47 |
#include <private/qapplication_p.h> |
|
48 |
#include <private/qt_mac_p.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
49 |
#include <private/qmenubar_p.h> |
0 | 50 |
#include <qmenubar.h> |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
51 |
#include <private/qt_cocoa_helpers_mac_p.h> |
0 | 52 |
|
53 |
QT_FORWARD_DECLARE_CLASS(QCFString) |
|
54 |
QT_FORWARD_DECLARE_CLASS(QString) |
|
55 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
56 |
#ifndef QT_NO_TRANSLATION |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
57 |
QT_BEGIN_NAMESPACE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
58 |
extern QString qt_mac_applicationmenu_string(int type); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
59 |
QT_END_NAMESPACE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
60 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
61 |
|
0 | 62 |
QT_USE_NAMESPACE |
63 |
||
64 |
@implementation QT_MANGLE_NAMESPACE(QCocoaMenuLoader) |
|
65 |
||
66 |
- (void)awakeFromNib |
|
67 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
68 |
servicesItem = [[appMenu itemWithTitle:@"Services"] retain]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
69 |
hideAllOthersItem = [[appMenu itemWithTitle:@"Hide Others"] retain]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
70 |
showAllItem = [[appMenu itemWithTitle:@"Show All"] retain]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
71 |
|
0 | 72 |
// Get the names in the nib to match the app name set by Qt. |
73 |
NSString *appName = reinterpret_cast<const NSString*>(QCFString::toCFStringRef(qAppName())); |
|
74 |
[quitItem setTitle:[[quitItem title] stringByReplacingOccurrencesOfString:@"NewApplication" |
|
75 |
withString:appName]]; |
|
76 |
[hideItem setTitle:[[hideItem title] stringByReplacingOccurrencesOfString:@"NewApplication" |
|
77 |
withString:appName]]; |
|
78 |
[aboutItem setTitle:[[aboutItem title] stringByReplacingOccurrencesOfString:@"NewApplication" |
|
79 |
withString:appName]]; |
|
80 |
[appName release]; |
|
81 |
// Disable the items that don't do anything. If someone associates a QAction with them |
|
82 |
// They should get synced back in. |
|
83 |
[preferencesItem setEnabled:NO]; |
|
84 |
[preferencesItem setHidden:YES]; |
|
85 |
[aboutItem setEnabled:NO]; |
|
86 |
[aboutItem setHidden:YES]; |
|
87 |
} |
|
88 |
||
89 |
- (void)ensureAppMenuInMenu:(NSMenu *)menu |
|
90 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
// The application menu is the menu in the menu bar that contains the |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
// 'Quit' item. When changing menu bar (e.g when swithing between |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
93 |
// windows with different menu bars), we never recreate this menu, but |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
94 |
// instead pull it out the current menu bar and place into the new one: |
0 | 95 |
NSMenu *mainMenu = [NSApp mainMenu]; |
96 |
if ([NSApp mainMenu] == menu) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
return; // nothing to do (menu is the current menu bar)! |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
|
0 | 99 |
#ifndef QT_NAMESPACE |
100 |
Q_ASSERT(mainMenu); |
|
101 |
#endif |
|
102 |
// Grab the app menu out of the current menu. |
|
103 |
int numItems = [mainMenu numberOfItems]; |
|
104 |
NSMenuItem *oldAppMenuItem = 0; |
|
105 |
for (int i = 0; i < numItems; ++i) { |
|
106 |
NSMenuItem *item = [mainMenu itemAtIndex:i]; |
|
107 |
if ([item submenu] == appMenu) { |
|
108 |
oldAppMenuItem = item; |
|
109 |
[oldAppMenuItem retain]; |
|
110 |
[mainMenu removeItemAtIndex:i]; |
|
111 |
break; |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
if (oldAppMenuItem) { |
|
116 |
[oldAppMenuItem setSubmenu:nil]; |
|
117 |
[oldAppMenuItem release]; |
|
118 |
NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple" |
|
119 |
action:nil keyEquivalent:@""]; |
|
120 |
[appMenuItem setSubmenu:appMenu]; |
|
121 |
[menu insertItem:appMenuItem atIndex:0]; |
|
122 |
} |
|
123 |
} |
|
124 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
125 |
- (void)removeActionsFromAppMenu |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
126 |
{ |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
127 |
for (NSMenuItem *item in [appMenu itemArray]) |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
128 |
[item setTag:nil]; |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
129 |
} |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
130 |
|
0 | 131 |
- (void)dealloc |
132 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
133 |
[servicesItem release]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
134 |
[hideAllOthersItem release]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
135 |
[showAllItem release]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
136 |
|
0 | 137 |
[lastAppSpecificItem release]; |
138 |
[theMenu release]; |
|
139 |
[appMenu release]; |
|
140 |
[super dealloc]; |
|
141 |
} |
|
142 |
||
143 |
- (NSMenu *)menu |
|
144 |
{ |
|
145 |
return [[theMenu retain] autorelease]; |
|
146 |
} |
|
147 |
||
148 |
- (NSMenu *)applicationMenu |
|
149 |
{ |
|
150 |
return [[appMenu retain] autorelease]; |
|
151 |
} |
|
152 |
||
153 |
- (NSMenuItem *)quitMenuItem |
|
154 |
{ |
|
155 |
return [[quitItem retain] autorelease]; |
|
156 |
} |
|
157 |
||
158 |
- (NSMenuItem *)preferencesMenuItem |
|
159 |
{ |
|
160 |
return [[preferencesItem retain] autorelease]; |
|
161 |
} |
|
162 |
||
163 |
- (NSMenuItem *)aboutMenuItem |
|
164 |
{ |
|
165 |
return [[aboutItem retain] autorelease]; |
|
166 |
} |
|
167 |
||
168 |
- (NSMenuItem *)aboutQtMenuItem |
|
169 |
{ |
|
170 |
return [[aboutQtItem retain] autorelease]; |
|
171 |
} |
|
172 |
||
173 |
- (NSMenuItem *)hideMenuItem; |
|
174 |
{ |
|
175 |
return [[hideItem retain] autorelease]; |
|
176 |
} |
|
177 |
||
178 |
- (NSMenuItem *)appSpecificMenuItem; |
|
179 |
{ |
|
180 |
// Create an App-Specific menu item, insert it into the menu and return |
|
181 |
// it as an autorelease item. |
|
182 |
NSMenuItem *item = [[NSMenuItem alloc] init]; |
|
183 |
||
184 |
NSInteger location; |
|
185 |
if (lastAppSpecificItem == nil) { |
|
186 |
location = [appMenu indexOfItem:aboutQtItem]; |
|
187 |
} else { |
|
188 |
location = [appMenu indexOfItem:lastAppSpecificItem]; |
|
189 |
[lastAppSpecificItem release]; |
|
190 |
} |
|
191 |
lastAppSpecificItem = item; // Keep track of this for later (i.e., don't release it) |
|
192 |
[appMenu insertItem:item atIndex:location + 1]; |
|
193 |
||
194 |
return [[item retain] autorelease]; |
|
195 |
} |
|
196 |
||
197 |
- (BOOL) acceptsFirstResponder |
|
198 |
{ |
|
199 |
return YES; |
|
200 |
} |
|
201 |
||
202 |
- (void)terminate:(id)sender |
|
203 |
{ |
|
204 |
[NSApp terminate:sender]; |
|
205 |
} |
|
206 |
||
207 |
- (void)orderFrontStandardAboutPanel:(id)sender |
|
208 |
{ |
|
209 |
[NSApp orderFrontStandardAboutPanel:sender]; |
|
210 |
} |
|
211 |
||
212 |
- (void)hideOtherApplications:(id)sender |
|
213 |
{ |
|
214 |
[NSApp hideOtherApplications:sender]; |
|
215 |
} |
|
216 |
||
217 |
- (void)unhideAllApplications:(id)sender |
|
218 |
{ |
|
219 |
[NSApp unhideAllApplications:sender]; |
|
220 |
} |
|
221 |
||
222 |
- (void)hide:(id)sender |
|
223 |
{ |
|
224 |
[NSApp hide:sender]; |
|
225 |
} |
|
226 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
227 |
- (void)qtUpdateMenubar |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
228 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
229 |
QMenuBarPrivate::macUpdateMenuBarImmediatly(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
230 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
231 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
232 |
- (void)qtTranslateApplicationMenu |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
233 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
234 |
#ifndef QT_NO_TRANSLATION |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
235 |
[servicesItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(0))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
236 |
[hideItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(1).arg(qAppName()))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
237 |
[hideAllOthersItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(2))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
238 |
[showAllItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(3))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
239 |
[preferencesItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(4))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
240 |
[quitItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(5).arg(qAppName()))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
241 |
[aboutItem setTitle: qt_mac_QStringToNSString(qt_mac_applicationmenu_string(6).arg(qAppName()))]; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
242 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
243 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
244 |
|
0 | 245 |
- (IBAction)qtDispatcherToQAction:(id)sender |
246 |
{ |
|
247 |
QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData); |
|
248 |
NSMenuItem *item = static_cast<NSMenuItem *>(sender); |
|
249 |
if (QAction *action = reinterpret_cast<QAction *>([item tag])) { |
|
250 |
action->trigger(); |
|
251 |
} else if (item == quitItem) { |
|
252 |
// We got here because someone was once the quitItem, but it has been |
|
253 |
// abandoned (e.g., the menubar was deleted). In the meantime, just do |
|
254 |
// normal QApplication::quit(). |
|
255 |
qApp->quit(); |
|
256 |
} |
|
257 |
} |
|
258 |
@end |
|
259 |
#endif // QT_MAC_USE_COCOA |