From b00586df8253a645d4d7a59b68ffa208cb86ee7c Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Sun, 3 Jul 2011 22:34:20 +0200 Subject: MACOSX: Add i18n support for Mac OSX application menu --- backends/platform/sdl/macosx/appMenu_osx.h | 32 +++++++++ backends/platform/sdl/macosx/appMenu_osx.mm | 100 ++++++++++++++++++++++++++++ backends/platform/sdl/macosx/macosx.cpp | 12 ++++ backends/platform/sdl/module.mk | 3 +- 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100755 backends/platform/sdl/macosx/appMenu_osx.h create mode 100755 backends/platform/sdl/macosx/appMenu_osx.mm (limited to 'backends') diff --git a/backends/platform/sdl/macosx/appMenu_osx.h b/backends/platform/sdl/macosx/appMenu_osx.h new file mode 100755 index 0000000000..005414b789 --- /dev/null +++ b/backends/platform/sdl/macosx/appMenu_osx.h @@ -0,0 +1,32 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef APPMENU_OSX_H +#define APPMENU_OSX_H + +#if defined(MACOSX) + +void replaceApplicationMenuItems(); + +#endif // MACOSX + +#endif diff --git a/backends/platform/sdl/macosx/appMenu_osx.mm b/backends/platform/sdl/macosx/appMenu_osx.mm new file mode 100755 index 0000000000..e39815fbd9 --- /dev/null +++ b/backends/platform/sdl/macosx/appMenu_osx.mm @@ -0,0 +1,100 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#if defined(MACOSX) + +#include "backends/platform/sdl/macosx/appMenu_osx.h" +#include "common/translation.h" + +#include + +void replaceApplicationMenuItems() { + + // Code mainly copied and adapted from SDLmain.m + NSMenu *appleMenu; + NSMenu *windowMenu; + NSMenuItem *menuItem; + + // For some reason [[NSApp mainMenu] removeAllItems] doesn't work and crashes, so we need + // to remove the SDL generated menus one by one + [[NSApp mainMenu] removeItemAtIndex:0]; // Remove application menu + [[NSApp mainMenu] removeItemAtIndex:0]; // Remove "Windows" menu + + // Create new application menu + appleMenu = [[NSMenu alloc] initWithTitle:@""]; + + // Get current encoding + NSStringEncoding *stringEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[NSString stringWithCString:(TransMan.getCurrentCharset()).c_str() encoding:NSASCIIStringEncoding])); + + // Add "About ScummVM" menu item + [appleMenu addItemWithTitle:[NSString stringWithCString:_("About ScummVM") encoding:stringEncoding] action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; + + // Add seperator + [appleMenu addItem:[NSMenuItem separatorItem]]; + + // Add "Hide ScummVM" menu item + [appleMenu addItemWithTitle:[NSString stringWithCString:_("Hide ScummVM") encoding:stringEncoding] action:@selector(hide:) keyEquivalent:@"h"]; + + // Add "Hide Others" menu item + menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:[NSString stringWithCString:_("Hide Others") encoding:stringEncoding] action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; + [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; + + // Add "Show All" menu item + [appleMenu addItemWithTitle:[NSString stringWithCString:_("Show All") encoding:stringEncoding] action:@selector(unhideAllApplications:) keyEquivalent:@""]; + + // Add seperator + [appleMenu addItem:[NSMenuItem separatorItem]]; + + // Add "Quit ScummVM" menu item + [appleMenu addItemWithTitle:[NSString stringWithCString:_("Quit ScummVM") encoding:stringEncoding] action:@selector(terminate:) keyEquivalent:@"q"]; + + // Put application menu into the menubar + menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; + [menuItem setSubmenu:appleMenu]; + [[NSApp mainMenu] addItem:menuItem]; + + // Tell the application object that this is now the application menu + [NSApp setAppleMenu:appleMenu]; + + + // Create new "Window" menu + windowMenu = [[NSMenu alloc] initWithTitle:[NSString stringWithCString:_("Window") encoding:stringEncoding]]; + + // Add "Minimize" menu item + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithCString:_("Minimize") encoding:stringEncoding] action:@selector(performMiniaturize:) keyEquivalent:@"m"]; + [windowMenu addItem:menuItem]; + + // Put menu into the menubar + menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithCString:_("Window") encoding:stringEncoding] action:nil keyEquivalent:@""]; + [menuItem setSubmenu:windowMenu]; + [[NSApp mainMenu] addItem:menuItem]; + + // Tell the application object that this is now the window menu. + [NSApp setWindowsMenu:windowMenu]; + + // Finally give up our references to the objects + [appleMenu release]; + [windowMenu release]; + [menuItem release]; +} + +#endif // MACOSX diff --git a/backends/platform/sdl/macosx/macosx.cpp b/backends/platform/sdl/macosx/macosx.cpp index 817f61e864..e263192c82 100644 --- a/backends/platform/sdl/macosx/macosx.cpp +++ b/backends/platform/sdl/macosx/macosx.cpp @@ -29,9 +29,12 @@ #include "backends/platform/sdl/macosx/macosx.h" #include "backends/mixer/doublebuffersdl/doublebuffersdl-mixer.h" +#include "backends/platform/sdl/macosx/appMenu_osx.h" #include "common/archive.h" +#include "common/config-manager.h" #include "common/fs.h" +#include "common/translation.h" #include "ApplicationServices/ApplicationServices.h" // for LSOpenFSRef #include "CoreFoundation/CoreFoundation.h" // for CF* stuff @@ -51,6 +54,15 @@ void OSystem_MacOSX::initBackend() { _mixerManager->init(); } +#ifdef USE_TRANSLATION + // We need to initialize the translataion manager here for the following + // call to replaceApplicationMenuItems() work correctly + TransMan.setLanguage(ConfMan.get("gui_language").c_str()); +#endif // USE_TRANSLATION + + // Replace the SDL generated menu items with our own translated ones on Mac-OSX + replaceApplicationMenuItems(); + // Invoke parent implementation of this method OSystem_POSIX::initBackend(); } diff --git a/backends/platform/sdl/module.mk b/backends/platform/sdl/module.mk index e67bf859d6..34631c372d 100644 --- a/backends/platform/sdl/module.mk +++ b/backends/platform/sdl/module.mk @@ -14,7 +14,8 @@ endif ifdef MACOSX MODULE_OBJS += \ macosx/macosx-main.o \ - macosx/macosx.o + macosx/macosx.o \ + macosx/appMenu_osx.o endif ifdef WIN32 -- cgit v1.2.3 From 5a42f2901ff6300c47a9f905c5803d1139c6565d Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Tue, 12 Jul 2011 00:47:02 +0200 Subject: MACOSX: Fix application menu i18n compilation for Mac OSX 10.4+ --- backends/platform/sdl/macosx/appMenu_osx.mm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'backends') diff --git a/backends/platform/sdl/macosx/appMenu_osx.mm b/backends/platform/sdl/macosx/appMenu_osx.mm index e39815fbd9..930564313d 100755 --- a/backends/platform/sdl/macosx/appMenu_osx.mm +++ b/backends/platform/sdl/macosx/appMenu_osx.mm @@ -22,11 +22,21 @@ #if defined(MACOSX) +// Disable symbol overrides so that we can use system headers. +#define FORBIDDEN_SYMBOL_ALLOW_ALL + #include "backends/platform/sdl/macosx/appMenu_osx.h" #include "common/translation.h" #include +// Apple removed setAppleMenu from the header files in 10.4, +// but as the method still exists we declare it ourselves here. +// Yes, this works :) +@interface NSApplication(MissingFunction) +- (void)setAppleMenu:(NSMenu *)menu; +@end + void replaceApplicationMenuItems() { // Code mainly copied and adapted from SDLmain.m @@ -43,7 +53,7 @@ void replaceApplicationMenuItems() { appleMenu = [[NSMenu alloc] initWithTitle:@""]; // Get current encoding - NSStringEncoding *stringEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[NSString stringWithCString:(TransMan.getCurrentCharset()).c_str() encoding:NSASCIIStringEncoding])); + NSStringEncoding stringEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[NSString stringWithCString:(TransMan.getCurrentCharset()).c_str() encoding:NSASCIIStringEncoding])); // Add "About ScummVM" menu item [appleMenu addItemWithTitle:[NSString stringWithCString:_("About ScummVM") encoding:stringEncoding] action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; -- cgit v1.2.3