ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

appmenu_osx.mm

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Disable symbol overrides so that we can use system headers.
00024 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00025 
00026 #include "backends/platform/sdl/macosx/appmenu_osx.h"
00027 #include "common/translation.h"
00028 
00029 #include "backends/platform/sdl/macosx/macosx-compat.h"
00030 #include <Cocoa/Cocoa.h>
00031 #include <AppKit/NSWorkspace.h>
00032 
00033 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
00034 #define NSEventModifierFlagCommand NSCommandKeyMask
00035 #define NSEventModifierFlagOption  NSAlternateKeyMask
00036 #endif
00037 
00038 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10
00039 #define NSEventModifierFlags NSUInteger
00040 #endif
00041 
00042 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
00043 typedef unsigned long NSUInteger;
00044 #endif
00045 
00046 // Apple added setAppleMenu in 10.5 and removed it in 10.6.
00047 // But as the method still exists we declare it ourselves here.
00048 // Yes, this works :)
00049 @interface NSApplication(MissingFunction)
00050 - (void)setAppleMenu:(NSMenu *)menu;
00051 @end
00052 // However maybe we should conditionally use it depending on the system on which we run ScummVM (and not
00053 // the one on which we compile) to only do it on OS X 10.5.
00054 // Here is the relevant bit from the release notes for 10.6:
00055 // In Leopard and earlier, apps that tried to construct a menu bar without a nib would get an undesirable
00056 // stubby application menu that could not be removed. To work around this problem on Leopard, you can call
00057 // the undocumented setAppleMenu: method and pass it the application menu, like so:
00058 //   [NSApp setAppleMenu:[[[NSApp mainMenu] itemAtIndex:0] submenu]];
00059 // In SnowLeopard, this workaround is unnecessary and should not be used. Under SnowLeopard, the first menu
00060 // is always identified as the application menu.
00061 
00062 static void openFromBundle(NSString *file) {
00063     NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"rtf"];
00064     if (!path) {
00065         path = [[NSBundle mainBundle] pathForResource:file ofType:@""];
00066     }
00067 
00068     if (path) {
00069         [[NSWorkspace sharedWorkspace] openFile:path];
00070     }
00071 }
00072 
00073 @interface ScummVMMenuHandler : NSObject {
00074 }
00075 - (void) openReadme;
00076 - (void) openLicenseGPL;
00077 - (void) openLicenseLGPL;
00078 - (void) openLicenseFreefont;
00079 - (void) openLicenseBSD;
00080 - (void) openNews;
00081 - (void) openUserManual;
00082 - (void) openCredits;
00083 @end
00084 
00085 @implementation ScummVMMenuHandler : NSObject
00086 - (void)openReadme {
00087     openFromBundle(@"README");
00088 }
00089 
00090 - (void)openLicenseGPL {
00091     openFromBundle(@"COPYING");
00092 }
00093 
00094 - (void)openLicenseLGPL {
00095     openFromBundle(@"COPYING-LGPL");
00096 }
00097 
00098 - (void)openLicenseFreefont {
00099     openFromBundle(@"COPYING-FREEFONT");
00100 }
00101 
00102 - (void)openLicenseBSD {
00103     openFromBundle(@"COPYING-BSD");
00104 }
00105 
00106 - (void)openNews {
00107     openFromBundle(@"NEWS");
00108 }
00109 
00110 - (void)openUserManual {
00111     [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.residualvm.org/documentation"]];
00112 }
00113 
00114 - (void)openCredits {
00115     openFromBundle(@"AUTHORS");
00116 }
00117 @end
00118 
00119 NSString *constructNSStringFromCString(const char *rawCString, CFStringEncoding stringEncoding) {
00120     return (NSString *)CFStringCreateWithCString(NULL, rawCString, stringEncoding);
00121 }
00122 
00123 static NSMenu *addMenu(const char *title, CFStringEncoding encoding, NSString *key, SEL setAs) {
00124     NSString *str = constructNSStringFromCString(title, encoding);
00125     NSMenu *menu = [[NSMenu alloc] initWithTitle:str];
00126 
00127     NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:str action:nil keyEquivalent:key];
00128     [menuItem setSubmenu:menu];
00129     [[NSApp mainMenu] addItem:menuItem];
00130 
00131     if (setAs) {
00132         [NSApp performSelector:setAs withObject:menu];
00133     }
00134 
00135     [str release];
00136     [menuItem release];
00137 
00138     return menu;
00139 }
00140 
00141 static void addMenuItem(const char *title, CFStringEncoding encoding, id target, SEL selector, NSString *key, NSMenu *parent, NSEventModifierFlags flags = 0) {
00142     NSString *nsString = constructNSStringFromCString(title, encoding);
00143     NSMenuItem *menuItem = [[NSMenuItem alloc]
00144                             initWithTitle:nsString
00145                             action:selector
00146                             keyEquivalent:key];
00147     if (target) {
00148         [menuItem setTarget:target];
00149     }
00150     if (flags) {
00151         [menuItem setKeyEquivalentModifierMask:flags];
00152     }
00153     [parent addItem:menuItem];
00154     [nsString release];
00155 }
00156 
00157 static ScummVMMenuHandler *delegate = nullptr;
00158 
00159 void releaseMenu() {
00160     [delegate release];
00161     delegate = nullptr;
00162 }
00163 
00164 void replaceApplicationMenuItems() {
00165     if (!delegate) {
00166         delegate = [[ScummVMMenuHandler alloc] init];
00167     }
00168 
00169     // We cannot use [[NSApp mainMenu] removeAllItems] as removeAllItems was added in OS X 10.6
00170     // So remove the SDL generated menus one by one instead.
00171     while ([[NSApp mainMenu] numberOfItems] > 0) {
00172         [[NSApp mainMenu] removeItemAtIndex:0];
00173     }
00174 
00175     NSString *nsString = NULL;
00176 
00177     // Get current encoding
00178 #ifdef USE_TRANSLATION
00179     nsString = constructNSStringFromCString(TransMan.getCurrentCharset().c_str(), NSASCIIStringEncoding);
00180     CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)nsString);
00181     [nsString release];
00182 #else
00183     CFStringEncoding stringEncoding = kCFStringEncodingASCII;
00184 #endif
00185 
00186     NSMenu *appleMenu = addMenu("ResidualVM", kCFStringEncodingASCII, @"", @selector(setAppleMenu:));
00187     addMenuItem(_("About ResidualVM"), stringEncoding, nil, @selector(orderFrontStandardAboutPanel:), @"", appleMenu);
00188     [appleMenu addItem:[NSMenuItem separatorItem]];
00189     addMenuItem(_("Hide ResidualVM"), stringEncoding, nil, @selector(hide:), @"h", appleMenu);
00190     addMenuItem(_("Hide Others"), stringEncoding, nil, @selector(hideOtherApplications:), @"h", appleMenu, (NSEventModifierFlagOption|NSEventModifierFlagCommand));
00191     addMenuItem(_("Show All"), stringEncoding, nil, @selector(unhideAllApplications:), @"", appleMenu);
00192     [appleMenu addItem:[NSMenuItem separatorItem]];
00193     addMenuItem(_("Quit ResidualVM"), stringEncoding, nil, @selector(terminate:), @"q", appleMenu);
00194 
00195     NSMenu *windowMenu = addMenu(_("Window"), stringEncoding, @"", @selector(setWindowsMenu:));
00196     addMenuItem(_("Minimize"), stringEncoding, nil, @selector(performMiniaturize:), @"m", windowMenu);
00197 
00198     NSMenu *helpMenu = addMenu(_("Help"), stringEncoding, @"", @selector(setHelpMenu:));
00199     addMenuItem(_("User Manual"), stringEncoding, delegate, @selector(openUserManual), @"", helpMenu);
00200     [helpMenu addItem:[NSMenuItem separatorItem]];
00201     addMenuItem(_("General Information"), stringEncoding, delegate, @selector(openReadme), @"", helpMenu);
00202     addMenuItem(_("What's New in ResidualVM"), stringEncoding, delegate, @selector(openNews), @"", helpMenu);
00203     [helpMenu addItem:[NSMenuItem separatorItem]];
00204     addMenuItem(_("Credits"), stringEncoding, delegate, @selector(openCredits), @"", helpMenu);
00205     addMenuItem(_("GPL License"), stringEncoding, delegate, @selector(openLicenseGPL), @"", helpMenu);
00206     addMenuItem(_("LGPL License"), stringEncoding, delegate, @selector(openLicenseLGPL), @"", helpMenu);
00207     addMenuItem(_("Freefont License"), stringEncoding, delegate, @selector(openLicenseFreefont), @"", helpMenu);
00208     addMenuItem(_("BSD License"), stringEncoding, delegate, @selector(openLicenseBSD), @"", helpMenu);
00209 
00210 
00211     [appleMenu release];
00212     [windowMenu release];
00213     [helpMenu release];
00214 }


Generated on Sat Mar 16 2019 05:01:19 for ResidualVM by doxygen 1.7.1
curved edge   curved edge