From b5f18da11fb526a444d2550715d8b867b711e67a Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 30 Jun 2016 15:21:00 -0700 Subject: QCocoaMenuLoader: Remove NIB file Since 10.6, the first menu is always identified as the application menu. See remark about Nibless apps and the application menu in, https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html Therefore, we can get rid of the NIB file together with the loading logic we had in place (and which, incidentaly, was using deprecated API). Change-Id: I99bf0e9d8ea749a9be9295fa12602335abc6548e Reviewed-by: Jake Petroules --- src/plugins/platforms/cocoa/qcocoaintegration.mm | 1 - src/plugins/platforms/cocoa/qcocoamenuloader.h | 7 +- src/plugins/platforms/cocoa/qcocoamenuloader.mm | 187 ++++++++++++--------- src/plugins/platforms/cocoa/qcocoaresources.qrc | 5 - .../platforms/cocoa/qt_menu.nib/classes.nib | 59 ------- src/plugins/platforms/cocoa/qt_menu.nib/info.nib | 18 -- .../platforms/cocoa/qt_menu.nib/keyedobjects.nib | Bin 5560 -> 0 bytes 7 files changed, 104 insertions(+), 173 deletions(-) delete mode 100644 src/plugins/platforms/cocoa/qt_menu.nib/classes.nib delete mode 100644 src/plugins/platforms/cocoa/qt_menu.nib/info.nib delete mode 100644 src/plugins/platforms/cocoa/qt_menu.nib/keyedobjects.nib (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index f6a3b3943f..b0b91c6633 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -332,7 +332,6 @@ QCocoaIntegration::QCocoaIntegration(const QStringList ¶mList) // Load the application menu. This menu contains Preferences, Hide, Quit. QCocoaMenuLoader *qtMenuLoader = [[QCocoaMenuLoader alloc] init]; - qt_mac_loadMenuNib(qtMenuLoader); [cocoaApplication setMenu:[qtMenuLoader menu]]; [newDelegate setMenuLoader:qtMenuLoader]; } diff --git a/src/plugins/platforms/cocoa/qcocoamenuloader.h b/src/plugins/platforms/cocoa/qcocoamenuloader.h index 6f58b2f24c..d1f47b18f0 100644 --- a/src/plugins/platforms/cocoa/qcocoamenuloader.h +++ b/src/plugins/platforms/cocoa/qcocoamenuloader.h @@ -68,6 +68,7 @@ NSMenuItem *hideAllOthersItem; NSMenuItem *showAllItem; } +- (instancetype)init; - (void)ensureAppMenuInMenu:(NSMenu *)menu; - (void)removeActionsFromAppMenu; - (NSMenu *)applicationMenu; @@ -92,10 +93,4 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QCocoaMenuLoader); -QT_BEGIN_NAMESPACE - -void qt_mac_loadMenuNib(QCocoaMenuLoader *qtMenuLoader); - -QT_END_NAMESPACE - #endif // QCOCOAMENULOADER_P_H diff --git a/src/plugins/platforms/cocoa/qcocoamenuloader.mm b/src/plugins/platforms/cocoa/qcocoamenuloader.mm index e440a9080c..9b16999d48 100644 --- a/src/plugins/platforms/cocoa/qcocoamenuloader.mm +++ b/src/plugins/platforms/cocoa/qcocoamenuloader.mm @@ -55,86 +55,117 @@ QT_FORWARD_DECLARE_CLASS(QCFString) QT_FORWARD_DECLARE_CLASS(QString) +@implementation QCocoaMenuLoader -QT_BEGIN_NAMESPACE - -/* - Loads and instantiates the main app menu from the menu nib file(s). - - The main app menu contains the Quit, Hide About, Preferences entries, and - The reason for having the nib file is that those can not be created - programmatically. To ease deployment the nib files are stored in Qt resources - and written to QDir::temp() before loading. (Earlier Qt versions used - to require having the nib file in the Qt GUI framework.) -*/ -void qt_mac_loadMenuNib(QCocoaMenuLoader *qtMenuLoader) +- (instancetype)init { - // Create qt_menu.nib dir in temp. - QDir temp = QDir::temp(); - temp.mkdir("qt_menu.nib"); - QString nibDir = temp.canonicalPath() + QLatin1String("/") + QLatin1String("qt_menu.nib/"); - if (!QDir(nibDir).exists()) { - qWarning("qt_mac_loadMenuNib: could not create nib directory in temp"); - return; - } - - // Copy nib files from resources to temp. - QDir nibResource(":/qt-project.org/mac/qt_menu.nib/"); - if (!nibResource.exists()) { - qWarning("qt_mac_loadMenuNib: could not load nib from resources"); - return; - } - foreach (const QFileInfo &file, nibResource.entryInfoList()) { - QFileInfo destinationFile(nibDir + QLatin1String("/") + file.fileName()); - if (destinationFile.exists() && destinationFile.size() != file.size()) - QFile::remove(destinationFile.absoluteFilePath()); - - QFile::copy(file.absoluteFilePath(), destinationFile.absoluteFilePath()); + if ((self = [super init])) { + NSString *appName = qt_mac_applicationName().toNSString(); + + // Menubar as menu. Title as set in the NIB file + theMenu = [[NSMenu alloc] initWithTitle:@"Main Menu"]; + + // Application menu. Since 10.6, the first menu + // is always identified as the application menu. + NSMenuItem *appItem = [[[NSMenuItem alloc] init] autorelease]; + appItem.title = appName; + [theMenu addItem:appItem]; + appMenu = [[NSMenu alloc] initWithTitle:appName]; + appItem.submenu = appMenu; + + // About Application + aboutItem = [[NSMenuItem alloc] initWithTitle:[@"About " stringByAppendingString:appName] + action:@selector(orderFrontStandardAboutPanel:) + keyEquivalent:@""]; + aboutItem.target = self; + // Disable until a QAction is associated + aboutItem.enabled = NO; + aboutItem.hidden = YES; + [appMenu addItem:aboutItem]; + + // About Qt (shameless self-promotion) + aboutQtItem = [[NSMenuItem alloc] init]; + aboutQtItem.title = @"About Qt"; + // Disable until a QAction is associated + aboutQtItem.enabled = NO; + aboutQtItem.hidden = YES; + [appMenu addItem:aboutQtItem]; + + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Preferences + preferencesItem = [[NSMenuItem alloc] initWithTitle:@"Preferences…" + action:@selector(qtDispatcherToQPAMenuItem:) + keyEquivalent:@","]; + preferencesItem.target = self; + // Disable until a QAction is associated + preferencesItem.enabled = NO; + preferencesItem.hidden = YES; + [appMenu addItem:preferencesItem]; + + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Services item and menu + servicesItem = [[NSMenuItem alloc] init]; + servicesItem.title = @"Services"; + NSApplication *app = [NSApplication sharedApplication]; + app.servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease]; + servicesItem.submenu = app.servicesMenu; + [appMenu addItem:servicesItem]; + + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Hide Application + hideItem = [[NSMenuItem alloc] initWithTitle:[@"Hide " stringByAppendingString:appName] + action:@selector(hide:) + keyEquivalent:@"h"]; + hideItem.target = self; + [appMenu addItem:hideItem]; + + // Hide Others + hideAllOthersItem = [[NSMenuItem alloc] initWithTitle:@"Hide Others" + action:@selector(hideOtherApplications:) + keyEquivalent:@"h"]; + hideAllOthersItem.target = self; + hideAllOthersItem.keyEquivalentModifierMask = NSCommandKeyMask | NSAlternateKeyMask; + [appMenu addItem:hideAllOthersItem]; + + // Show All + showAllItem = [[NSMenuItem alloc] initWithTitle:@"Show All" + action:@selector(unhideAllApplications:) + keyEquivalent:@""]; + showAllItem.target = self; + [appMenu addItem:showAllItem]; + + [appMenu addItem:[NSMenuItem separatorItem]]; + + // Quit Application + quitItem = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:appName] + action:@selector(terminate:) + keyEquivalent:@"q"]; + quitItem.target = self; + [appMenu addItem:quitItem]; } - // Load and instantiate nib file from temp - NSURL *nibUrl = [NSURL fileURLWithPath : QCFString::toNSString(nibDir)]; - NSNib *nib = [[NSNib alloc] initWithContentsOfURL : nibUrl]; - [nib autorelease]; - if(!nib) { - qWarning("qt_mac_loadMenuNib: could not load nib from temp"); - return; - } - bool ok = [nib instantiateNibWithOwner : qtMenuLoader topLevelObjects : nil]; - if (!ok) { - qWarning("qt_mac_loadMenuNib: could not instantiate nib"); - } + return self; } -QT_END_NAMESPACE +- (void)dealloc +{ + [theMenu release]; + [appMenu release]; + [aboutItem release]; + [aboutQtItem release]; + [preferencesItem release]; + [servicesItem release]; + [hideItem release]; + [hideAllOthersItem release]; + [showAllItem release]; + [quitItem release]; -@implementation QCocoaMenuLoader + [lastAppSpecificItem release]; -- (void)awakeFromNib -{ - servicesItem = [[appMenu itemWithTitle:@"Services"] retain]; - hideAllOthersItem = [[appMenu itemWithTitle:@"Hide Others"] retain]; - showAllItem = [[appMenu itemWithTitle:@"Show All"] retain]; - - // Get the names in the nib to match the app name set by Qt. - const NSString *appName = qt_mac_applicationName().toNSString(); - [quitItem setTitle:[[quitItem title] stringByReplacingOccurrencesOfString:@"NewApplication" - withString:const_cast(appName)]]; - [hideItem setTitle:[[hideItem title] stringByReplacingOccurrencesOfString:@"NewApplication" - withString:const_cast(appName)]]; - [aboutItem setTitle:[[aboutItem title] stringByReplacingOccurrencesOfString:@"NewApplication" - withString:const_cast(appName)]]; - // Disable the items that don't do anything. If someone associates a QAction with them - // They should get synced back in. - [preferencesItem setEnabled:NO]; - [preferencesItem setHidden:YES]; - - // should set this in the NIB - [preferencesItem setTarget: self]; - [preferencesItem setAction: @selector(qtDispatcherToQPAMenuItem:)]; - - [aboutItem setEnabled:NO]; - [aboutItem setHidden:YES]; + [super dealloc]; } - (void)ensureAppMenuInMenu:(NSMenu *)menu @@ -179,18 +210,6 @@ QT_END_NAMESPACE [item setTag:0]; } -- (void)dealloc -{ - [servicesItem release]; - [hideAllOthersItem release]; - [showAllItem release]; - - [lastAppSpecificItem release]; - [theMenu release]; - [appMenu release]; - [super dealloc]; -} - - (NSMenu *)menu { return [[theMenu retain] autorelease]; diff --git a/src/plugins/platforms/cocoa/qcocoaresources.qrc b/src/plugins/platforms/cocoa/qcocoaresources.qrc index 9e0640db7d..4255bfba9d 100644 --- a/src/plugins/platforms/cocoa/qcocoaresources.qrc +++ b/src/plugins/platforms/cocoa/qcocoaresources.qrc @@ -9,9 +9,4 @@ images/leopard-unified-toolbar-on.png - -qt_menu.nib/classes.nib -qt_menu.nib/info.nib -qt_menu.nib/keyedobjects.nib - diff --git a/src/plugins/platforms/cocoa/qt_menu.nib/classes.nib b/src/plugins/platforms/cocoa/qt_menu.nib/classes.nib deleted file mode 100644 index 78941153c2..0000000000 --- a/src/plugins/platforms/cocoa/qt_menu.nib/classes.nib +++ /dev/null @@ -1,59 +0,0 @@ - - - - - IBClasses - - - ACTIONS - - hide - id - hideOtherApplications - id - orderFrontStandardAboutPanel - id - qtDispatcherToQPAMenuItem - id - terminate - id - unhideAllApplications - id - - CLASS - QCocoaMenuLoader - LANGUAGE - ObjC - OUTLETS - - aboutItem - NSMenuItem - aboutQtItem - NSMenuItem - appMenu - NSMenu - hideItem - NSMenuItem - preferencesItem - NSMenuItem - quitItem - NSMenuItem - theMenu - NSMenu - - SUPERCLASS - NSResponder - - - CLASS - FirstResponder - LANGUAGE - ObjC - SUPERCLASS - NSObject - - - IBVersion - 1 - - diff --git a/src/plugins/platforms/cocoa/qt_menu.nib/info.nib b/src/plugins/platforms/cocoa/qt_menu.nib/info.nib deleted file mode 100644 index 02e5cca562..0000000000 --- a/src/plugins/platforms/cocoa/qt_menu.nib/info.nib +++ /dev/null @@ -1,18 +0,0 @@ - - - - - IBFramework Version - 672 - IBOldestOS - 5 - IBOpenObjects - - 57 - - IBSystem Version - 9L31a - targetFramework - IBCocoaFramework - - diff --git a/src/plugins/platforms/cocoa/qt_menu.nib/keyedobjects.nib b/src/plugins/platforms/cocoa/qt_menu.nib/keyedobjects.nib deleted file mode 100644 index 67207ca628..0000000000 Binary files a/src/plugins/platforms/cocoa/qt_menu.nib/keyedobjects.nib and /dev/null differ -- cgit v1.2.3