| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 41 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 42 | **
|
|---|
| 43 | ****************************************************************************/
|
|---|
| 44 |
|
|---|
| 45 | #include "qmacdefines_mac.h"
|
|---|
| 46 | #ifdef QT_MAC_USE_COCOA
|
|---|
| 47 | #include <qaction.h>
|
|---|
| 48 | #include <qcoreapplication.h>
|
|---|
| 49 | #include <private/qcocoamenuloader_mac_p.h>
|
|---|
| 50 | #include <private/qt_mac_p.h>
|
|---|
| 51 | #include <qmenubar.h>
|
|---|
| 52 |
|
|---|
| 53 | QT_FORWARD_DECLARE_CLASS(QCFString)
|
|---|
| 54 | QT_FORWARD_DECLARE_CLASS(QString)
|
|---|
| 55 |
|
|---|
| 56 | QT_USE_NAMESPACE
|
|---|
| 57 |
|
|---|
| 58 | @implementation QT_MANGLE_NAMESPACE(QCocoaMenuLoader)
|
|---|
| 59 |
|
|---|
| 60 | - (void)awakeFromNib
|
|---|
| 61 | {
|
|---|
| 62 | // Get the names in the nib to match the app name set by Qt.
|
|---|
| 63 | NSString *appName = reinterpret_cast<const NSString*>(QCFString::toCFStringRef(qAppName()));
|
|---|
| 64 | [quitItem setTitle:[[quitItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
|
|---|
| 65 | withString:appName]];
|
|---|
| 66 | [hideItem setTitle:[[hideItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
|
|---|
| 67 | withString:appName]];
|
|---|
| 68 | [aboutItem setTitle:[[aboutItem title] stringByReplacingOccurrencesOfString:@"NewApplication"
|
|---|
| 69 | withString:appName]];
|
|---|
| 70 | [appName release];
|
|---|
| 71 | // Disable the items that don't do anything. If someone associates a QAction with them
|
|---|
| 72 | // They should get synced back in.
|
|---|
| 73 | [preferencesItem setEnabled:NO];
|
|---|
| 74 | [preferencesItem setHidden:YES];
|
|---|
| 75 | [aboutItem setEnabled:NO];
|
|---|
| 76 | [aboutItem setHidden:YES];
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | - (void)ensureAppMenuInMenu:(NSMenu *)menu
|
|---|
| 80 | {
|
|---|
| 81 | NSMenu *mainMenu = [NSApp mainMenu];
|
|---|
| 82 | if ([NSApp mainMenu] == menu)
|
|---|
| 83 | return; // nothing to do!
|
|---|
| 84 | #ifndef QT_NAMESPACE
|
|---|
| 85 | Q_ASSERT(mainMenu);
|
|---|
| 86 | #endif
|
|---|
| 87 | // Grab the app menu out of the current menu.
|
|---|
| 88 | int numItems = [mainMenu numberOfItems];
|
|---|
| 89 | NSMenuItem *oldAppMenuItem = 0;
|
|---|
| 90 | for (int i = 0; i < numItems; ++i) {
|
|---|
| 91 | NSMenuItem *item = [mainMenu itemAtIndex:i];
|
|---|
| 92 | if ([item submenu] == appMenu) {
|
|---|
| 93 | oldAppMenuItem = item;
|
|---|
| 94 | [oldAppMenuItem retain];
|
|---|
| 95 | [mainMenu removeItemAtIndex:i];
|
|---|
| 96 | break;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | if (oldAppMenuItem) {
|
|---|
| 101 | [oldAppMenuItem setSubmenu:nil];
|
|---|
| 102 | [oldAppMenuItem release];
|
|---|
| 103 | NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple"
|
|---|
| 104 | action:nil keyEquivalent:@""];
|
|---|
| 105 | [appMenuItem setSubmenu:appMenu];
|
|---|
| 106 | [menu insertItem:appMenuItem atIndex:0];
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | - (void)dealloc
|
|---|
| 111 | {
|
|---|
| 112 | [lastAppSpecificItem release];
|
|---|
| 113 | [theMenu release];
|
|---|
| 114 | [appMenu release];
|
|---|
| 115 | [super dealloc];
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | - (NSMenu *)menu
|
|---|
| 119 | {
|
|---|
| 120 | return [[theMenu retain] autorelease];
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | - (NSMenu *)applicationMenu
|
|---|
| 124 | {
|
|---|
| 125 | return [[appMenu retain] autorelease];
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | - (NSMenuItem *)quitMenuItem
|
|---|
| 129 | {
|
|---|
| 130 | return [[quitItem retain] autorelease];
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | - (NSMenuItem *)preferencesMenuItem
|
|---|
| 134 | {
|
|---|
| 135 | return [[preferencesItem retain] autorelease];
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | - (NSMenuItem *)aboutMenuItem
|
|---|
| 139 | {
|
|---|
| 140 | return [[aboutItem retain] autorelease];
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | - (NSMenuItem *)aboutQtMenuItem
|
|---|
| 144 | {
|
|---|
| 145 | return [[aboutQtItem retain] autorelease];
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | - (NSMenuItem *)hideMenuItem;
|
|---|
| 149 | {
|
|---|
| 150 | return [[hideItem retain] autorelease];
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | - (NSMenuItem *)appSpecificMenuItem;
|
|---|
| 154 | {
|
|---|
| 155 | // Create an App-Specific menu item, insert it into the menu and return
|
|---|
| 156 | // it as an autorelease item.
|
|---|
| 157 | NSMenuItem *item = [[NSMenuItem alloc] init];
|
|---|
| 158 |
|
|---|
| 159 | NSInteger location;
|
|---|
| 160 | if (lastAppSpecificItem == nil) {
|
|---|
| 161 | location = [appMenu indexOfItem:aboutQtItem];
|
|---|
| 162 | } else {
|
|---|
| 163 | location = [appMenu indexOfItem:lastAppSpecificItem];
|
|---|
| 164 | [lastAppSpecificItem release];
|
|---|
| 165 | }
|
|---|
| 166 | lastAppSpecificItem = item; // Keep track of this for later (i.e., don't release it)
|
|---|
| 167 | [appMenu insertItem:item atIndex:location + 1];
|
|---|
| 168 |
|
|---|
| 169 | return [[item retain] autorelease];
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | - (BOOL) acceptsFirstResponder
|
|---|
| 173 | {
|
|---|
| 174 | return YES;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | - (void)terminate:(id)sender
|
|---|
| 178 | {
|
|---|
| 179 | [NSApp terminate:sender];
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | - (void)orderFrontStandardAboutPanel:(id)sender
|
|---|
| 183 | {
|
|---|
| 184 | [NSApp orderFrontStandardAboutPanel:sender];
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | - (void)hideOtherApplications:(id)sender
|
|---|
| 188 | {
|
|---|
| 189 | [NSApp hideOtherApplications:sender];
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | - (void)unhideAllApplications:(id)sender
|
|---|
| 193 | {
|
|---|
| 194 | [NSApp unhideAllApplications:sender];
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | - (void)hide:(id)sender
|
|---|
| 198 | {
|
|---|
| 199 | [NSApp hide:sender];
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | - (IBAction)qtDispatcherToQAction:(id)sender
|
|---|
| 203 | {
|
|---|
| 204 | NSMenuItem *item = static_cast<NSMenuItem *>(sender);
|
|---|
| 205 | if (QAction *action = reinterpret_cast<QAction *>([item tag])) {
|
|---|
| 206 | action->trigger();
|
|---|
| 207 | } else if (item == quitItem) {
|
|---|
| 208 | // We got here because someone was once the quitItem, but it has been
|
|---|
| 209 | // abandoned (e.g., the menubar was deleted). In the meantime, just do
|
|---|
| 210 | // normal QApplication::quit().
|
|---|
| 211 | qApp->quit();
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | @end
|
|---|
| 215 | #endif // QT_MAC_USE_COCOA
|
|---|