source: trunk/src/gui/kernel/qcocoamenuloader_mac.mm@ 624

Last change on this file since 624 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 6.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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>
49#include <qmenubar.h>
50
51QT_FORWARD_DECLARE_CLASS(QCFString)
52QT_FORWARD_DECLARE_CLASS(QString)
53
54QT_USE_NAMESPACE
55
56@implementation QT_MANGLE_NAMESPACE(QCocoaMenuLoader)
57
58- (void)awakeFromNib
59{
60 // Get the names in the nib to match the app name set by Qt.
61 NSString *appName = reinterpret_cast<const NSString*>(QCFString::toCFStringRef(qAppName()));
62 [quitItem setTitle:[[quitItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
63 withString:appName]];
64 [hideItem setTitle:[[hideItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
65 withString:appName]];
66 [aboutItem setTitle:[[aboutItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
67 withString:appName]];
68 [appName release];
69 // Disable the items that don't do anything. If someone associates a QAction with them
70 // They should get synced back in.
71 [preferencesItem setEnabled:NO];
72 [preferencesItem setHidden:YES];
73 [aboutItem setEnabled:NO];
74 [aboutItem setHidden:YES];
75}
76
77- (void)ensureAppMenuInMenu:(NSMenu *)menu
78{
79 // The application menu is the menu in the menu bar that contains the
80 // 'Quit' item. When changing menu bar (e.g when swithing between
81 // windows with different menu bars), we never recreate this menu, but
82 // instead pull it out the current menu bar and place into the new one:
83 NSMenu *mainMenu = [NSApp mainMenu];
84 if ([NSApp mainMenu] == menu)
85 return; // nothing to do (menu is the current menu bar)!
86
87#ifndef QT_NAMESPACE
88 Q_ASSERT(mainMenu);
89#endif
90 // Grab the app menu out of the current menu.
91 int numItems = [mainMenu numberOfItems];
92 NSMenuItem *oldAppMenuItem = 0;
93 for (int i = 0; i < numItems; ++i) {
94 NSMenuItem *item = [mainMenu itemAtIndex:i];
95 if ([item submenu] == appMenu) {
96 oldAppMenuItem = item;
97 [oldAppMenuItem retain];
98 [mainMenu removeItemAtIndex:i];
99 break;
100 }
101 }
102
103 if (oldAppMenuItem) {
104 [oldAppMenuItem setSubmenu:nil];
105 [oldAppMenuItem release];
106 NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple"
107 action:nil keyEquivalent:@""];
108 [appMenuItem setSubmenu:appMenu];
109 [menu insertItem:appMenuItem atIndex:0];
110 }
111}
112
113- (void)dealloc
114{
115 [lastAppSpecificItem release];
116 [theMenu release];
117 [appMenu release];
118 [super dealloc];
119}
120
121- (NSMenu *)menu
122{
123 return [[theMenu retain] autorelease];
124}
125
126- (NSMenu *)applicationMenu
127{
128 return [[appMenu retain] autorelease];
129}
130
131- (NSMenuItem *)quitMenuItem
132{
133 return [[quitItem retain] autorelease];
134}
135
136- (NSMenuItem *)preferencesMenuItem
137{
138 return [[preferencesItem retain] autorelease];
139}
140
141- (NSMenuItem *)aboutMenuItem
142{
143 return [[aboutItem retain] autorelease];
144}
145
146- (NSMenuItem *)aboutQtMenuItem
147{
148 return [[aboutQtItem retain] autorelease];
149}
150
151- (NSMenuItem *)hideMenuItem;
152{
153 return [[hideItem retain] autorelease];
154}
155
156- (NSMenuItem *)appSpecificMenuItem;
157{
158 // Create an App-Specific menu item, insert it into the menu and return
159 // it as an autorelease item.
160 NSMenuItem *item = [[NSMenuItem alloc] init];
161
162 NSInteger location;
163 if (lastAppSpecificItem == nil) {
164 location = [appMenu indexOfItem:aboutQtItem];
165 } else {
166 location = [appMenu indexOfItem:lastAppSpecificItem];
167 [lastAppSpecificItem release];
168 }
169 lastAppSpecificItem = item; // Keep track of this for later (i.e., don't release it)
170 [appMenu insertItem:item atIndex:location + 1];
171
172 return [[item retain] autorelease];
173}
174
175- (BOOL) acceptsFirstResponder
176{
177 return YES;
178}
179
180- (void)terminate:(id)sender
181{
182 [NSApp terminate:sender];
183}
184
185- (void)orderFrontStandardAboutPanel:(id)sender
186{
187 [NSApp orderFrontStandardAboutPanel:sender];
188}
189
190- (void)hideOtherApplications:(id)sender
191{
192 [NSApp hideOtherApplications:sender];
193}
194
195- (void)unhideAllApplications:(id)sender
196{
197 [NSApp unhideAllApplications:sender];
198}
199
200- (void)hide:(id)sender
201{
202 [NSApp hide:sender];
203}
204
205- (IBAction)qtDispatcherToQAction:(id)sender
206{
207 QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData);
208 NSMenuItem *item = static_cast<NSMenuItem *>(sender);
209 if (QAction *action = reinterpret_cast<QAction *>([item tag])) {
210 action->trigger();
211 } else if (item == quitItem) {
212 // We got here because someone was once the quitItem, but it has been
213 // abandoned (e.g., the menubar was deleted). In the meantime, just do
214 // normal QApplication::quit().
215 qApp->quit();
216 }
217}
218@end
219#endif // QT_MAC_USE_COCOA
Note: See TracBrowser for help on using the repository browser.