src/gui/kernel/qcocoamenuloader_mac.mm
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     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>
       
    49 #include <qmenubar.h>
       
    50 
       
    51 QT_FORWARD_DECLARE_CLASS(QCFString)
       
    52 QT_FORWARD_DECLARE_CLASS(QString)
       
    53 
       
    54 QT_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     NSMenu *mainMenu = [NSApp mainMenu];
       
    80     if ([NSApp mainMenu] == menu)
       
    81         return; // nothing to do!
       
    82 #ifndef QT_NAMESPACE
       
    83     Q_ASSERT(mainMenu);
       
    84 #endif
       
    85     // Grab the app menu out of the current menu.
       
    86     int numItems = [mainMenu numberOfItems];
       
    87     NSMenuItem *oldAppMenuItem = 0;
       
    88     for (int i = 0; i < numItems; ++i) {
       
    89         NSMenuItem *item = [mainMenu itemAtIndex:i];
       
    90         if ([item submenu] == appMenu) {
       
    91             oldAppMenuItem = item;
       
    92             [oldAppMenuItem retain];
       
    93             [mainMenu removeItemAtIndex:i];
       
    94             break;
       
    95         }
       
    96     }
       
    97 
       
    98     if (oldAppMenuItem) {
       
    99         [oldAppMenuItem setSubmenu:nil];
       
   100         [oldAppMenuItem release];
       
   101         NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:@"Apple"
       
   102             action:nil keyEquivalent:@""];
       
   103         [appMenuItem setSubmenu:appMenu];
       
   104         [menu insertItem:appMenuItem atIndex:0];
       
   105     }
       
   106 }
       
   107 
       
   108 - (void)dealloc
       
   109 {
       
   110     [lastAppSpecificItem release];
       
   111     [theMenu release];
       
   112     [appMenu release];
       
   113     [super dealloc];
       
   114 }
       
   115 
       
   116 - (NSMenu *)menu
       
   117 {
       
   118     return [[theMenu retain] autorelease];
       
   119 }
       
   120 
       
   121 - (NSMenu *)applicationMenu
       
   122 {
       
   123     return [[appMenu retain] autorelease];
       
   124 }
       
   125 
       
   126 - (NSMenuItem *)quitMenuItem
       
   127 {
       
   128     return [[quitItem retain] autorelease];
       
   129 }
       
   130 
       
   131 - (NSMenuItem *)preferencesMenuItem
       
   132 {
       
   133     return [[preferencesItem retain] autorelease];
       
   134 }
       
   135 
       
   136 - (NSMenuItem *)aboutMenuItem
       
   137 {
       
   138     return [[aboutItem retain] autorelease];
       
   139 }
       
   140 
       
   141 - (NSMenuItem *)aboutQtMenuItem
       
   142 {
       
   143     return [[aboutQtItem retain] autorelease];
       
   144 }
       
   145 
       
   146 - (NSMenuItem *)hideMenuItem;
       
   147 {
       
   148     return [[hideItem retain] autorelease];
       
   149 }
       
   150 
       
   151 - (NSMenuItem *)appSpecificMenuItem;
       
   152 {
       
   153     // Create an App-Specific menu item, insert it into the menu and return
       
   154     // it as an autorelease item.
       
   155     NSMenuItem *item = [[NSMenuItem alloc] init];
       
   156 
       
   157     NSInteger location;
       
   158     if (lastAppSpecificItem == nil) {
       
   159         location = [appMenu indexOfItem:aboutQtItem];
       
   160     } else {
       
   161         location = [appMenu indexOfItem:lastAppSpecificItem];
       
   162         [lastAppSpecificItem release];
       
   163     }
       
   164     lastAppSpecificItem = item;  // Keep track of this for later (i.e., don't release it)
       
   165     [appMenu insertItem:item atIndex:location + 1];
       
   166 
       
   167     return [[item retain] autorelease];
       
   168 }
       
   169 
       
   170 - (BOOL) acceptsFirstResponder
       
   171 {
       
   172     return YES;
       
   173 }
       
   174 
       
   175 - (void)terminate:(id)sender
       
   176 {
       
   177     [NSApp terminate:sender];
       
   178 }
       
   179 
       
   180 - (void)orderFrontStandardAboutPanel:(id)sender
       
   181 {
       
   182     [NSApp orderFrontStandardAboutPanel:sender];
       
   183 }
       
   184 
       
   185 - (void)hideOtherApplications:(id)sender
       
   186 {
       
   187     [NSApp hideOtherApplications:sender];
       
   188 }
       
   189 
       
   190 - (void)unhideAllApplications:(id)sender
       
   191 {
       
   192     [NSApp unhideAllApplications:sender];
       
   193 }
       
   194 
       
   195 - (void)hide:(id)sender
       
   196 {
       
   197     [NSApp hide:sender];
       
   198 }
       
   199 
       
   200 - (IBAction)qtDispatcherToQAction:(id)sender
       
   201 {
       
   202     QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData);
       
   203     NSMenuItem *item = static_cast<NSMenuItem *>(sender);
       
   204     if (QAction *action = reinterpret_cast<QAction *>([item tag])) {
       
   205         action->trigger();
       
   206     } else if (item == quitItem) {
       
   207         // We got here because someone was once the quitItem, but it has been
       
   208         // abandoned (e.g., the menubar was deleted). In the meantime, just do
       
   209         // normal QApplication::quit().
       
   210         qApp->quit();
       
   211     }
       
   212 }
       
   213 @end
       
   214 #endif // QT_MAC_USE_COCOA