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

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

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 6.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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)removeActionsFromAppMenu
114{
115 for (NSMenuItem *item in [appMenu itemArray])
116 [item setTag:nil];
117}
118
119- (void)dealloc
120{
121 [lastAppSpecificItem release];
122 [theMenu release];
123 [appMenu release];
124 [super dealloc];
125}
126
127- (NSMenu *)menu
128{
129 return [[theMenu retain] autorelease];
130}
131
132- (NSMenu *)applicationMenu
133{
134 return [[appMenu retain] autorelease];
135}
136
137- (NSMenuItem *)quitMenuItem
138{
139 return [[quitItem retain] autorelease];
140}
141
142- (NSMenuItem *)preferencesMenuItem
143{
144 return [[preferencesItem retain] autorelease];
145}
146
147- (NSMenuItem *)aboutMenuItem
148{
149 return [[aboutItem retain] autorelease];
150}
151
152- (NSMenuItem *)aboutQtMenuItem
153{
154 return [[aboutQtItem retain] autorelease];
155}
156
157- (NSMenuItem *)hideMenuItem;
158{
159 return [[hideItem retain] autorelease];
160}
161
162- (NSMenuItem *)appSpecificMenuItem;
163{
164 // Create an App-Specific menu item, insert it into the menu and return
165 // it as an autorelease item.
166 NSMenuItem *item = [[NSMenuItem alloc] init];
167
168 NSInteger location;
169 if (lastAppSpecificItem == nil) {
170 location = [appMenu indexOfItem:aboutQtItem];
171 } else {
172 location = [appMenu indexOfItem:lastAppSpecificItem];
173 [lastAppSpecificItem release];
174 }
175 lastAppSpecificItem = item; // Keep track of this for later (i.e., don't release it)
176 [appMenu insertItem:item atIndex:location + 1];
177
178 return [[item retain] autorelease];
179}
180
181- (BOOL) acceptsFirstResponder
182{
183 return YES;
184}
185
186- (void)terminate:(id)sender
187{
188 [NSApp terminate:sender];
189}
190
191- (void)orderFrontStandardAboutPanel:(id)sender
192{
193 [NSApp orderFrontStandardAboutPanel:sender];
194}
195
196- (void)hideOtherApplications:(id)sender
197{
198 [NSApp hideOtherApplications:sender];
199}
200
201- (void)unhideAllApplications:(id)sender
202{
203 [NSApp unhideAllApplications:sender];
204}
205
206- (void)hide:(id)sender
207{
208 [NSApp hide:sender];
209}
210
211- (IBAction)qtDispatcherToQAction:(id)sender
212{
213 QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData);
214 NSMenuItem *item = static_cast<NSMenuItem *>(sender);
215 if (QAction *action = reinterpret_cast<QAction *>([item tag])) {
216 action->trigger();
217 } else if (item == quitItem) {
218 // We got here because someone was once the quitItem, but it has been
219 // abandoned (e.g., the menubar was deleted). In the meantime, just do
220 // normal QApplication::quit().
221 qApp->quit();
222 }
223}
224@end
225#endif // QT_MAC_USE_COCOA
Note: See TracBrowser for help on using the repository browser.