11
10
#import <sys/param.h> /* for MAXPATHLEN */
14
13
#import <AppKit/NSPanel.h>
15
#include "Utilities/gettext.h"
16
#include "Platforms/osx/CocoaRLVMInstance.h"
18
#include <boost/filesystem/operations.hpp>
19
namespace fs = boost::filesystem;
21
NSString* utf8str(const char* str) {
22
return [NSString stringWithUTF8String:str];
25
NSString* b_format(const char* format_str, NSString* data) {
26
std::string s = str( format(format_str) % [data UTF8String]);
27
return [NSString stringWithUTF8String:s.c_str()];
16
30
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
17
31
but the method still is there and works. To avoid warnings, we declare
18
32
it ourselves here. */
48
/* A helper category for NSString */
49
@interface NSString (ReplaceSubString)
50
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
54
59
@interface SDLApplication : NSApplication
62
static NSEvent* lastEvent = nil;
64
NSEvent* GetLastRightClickEvent() {
57
68
@implementation SDLApplication
58
69
/* Invoked from the Quit menu item */
59
70
- (void)terminate:(id)sender
64
75
SDL_PushEvent(&event);
78
-(void)sendEvent:(NSEvent*)event
80
// In the specific case of right click mouse events, we grab the event before
81
// we pass it on so that when we get through the cross platform menu dispatch
82
// code, we can pass the event that caused the menu to popup back to Cocoa.
83
if ([event type] == NSRightMouseDown ||
84
[event type] == NSRightMouseUp) {
86
lastEvent = [event retain];
89
[super sendEvent:event];
68
92
-(void)showREADME:(id)sender
113
/* Fix menu to contain the real app name instead of "SDL App" */
114
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
117
NSEnumerator *enumerator;
118
NSMenuItem *menuItem;
120
aRange = [[aMenu title] rangeOfString:@"SDL App"];
121
if (aRange.length != 0)
122
[aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
124
enumerator = [[aMenu itemArray] objectEnumerator];
125
while ((menuItem = [enumerator nextObject]))
127
aRange = [[menuItem title] rangeOfString:@"SDL App"];
128
if (aRange.length != 0)
129
[menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
130
if ([menuItem hasSubmenu])
131
[self fixMenu:[menuItem submenu] withAppName:appName];
138
135
static void setApplicationMenu(void)
140
137
/* warning: this code is very odd */
147
144
appleMenu = [[NSMenu alloc] initWithTitle:@""];
149
146
/* Add menu items */
150
title = [@"About " stringByAppendingString:appName];
147
title = b_format(_("About %1%"), appName);
151
148
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
153
150
[appleMenu addItem:[NSMenuItem separatorItem]];
155
title = [@"Hide " stringByAppendingString:appName];
152
title = b_format(_("Hide %1%"), appName);
156
153
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
158
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
155
menuItem = (NSMenuItem *)[appleMenu
156
addItemWithTitle:utf8str(_("Hide Others"))
157
action:@selector(hideOtherApplications:)
159
159
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
161
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
161
[appleMenu addItemWithTitle:utf8str(_("Show All"))
162
action:@selector(unhideAllApplications:)
163
165
[appleMenu addItem:[NSMenuItem separatorItem]];
165
title = [@"Quit " stringByAppendingString:appName];
166
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
167
title = b_format(_("Quit %1%"), appName);
168
[appleMenu addItemWithTitle:title
169
action:@selector(terminate:)
169
172
/* Put menu into the menubar */
170
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
173
menuItem = [[NSMenuItem alloc] initWithTitle:@""
171
176
[menuItem setSubmenu:appleMenu];
172
177
[[NSApp mainMenu] addItem:menuItem];
186
191
NSMenuItem *windowMenuItem;
187
192
NSMenuItem *menuItem;
189
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
194
windowMenu = [[NSMenu alloc] initWithTitle:utf8str(_("Window"))];
191
196
/* "Minimize" item */
192
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
197
menuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Minimize"))
198
action:@selector(performMiniaturize:)
193
200
[windowMenu addItem:menuItem];
194
201
[menuItem release];
196
203
/* Put menu into the menubar */
197
windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
204
windowMenuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Window"))
198
207
[windowMenuItem setSubmenu:windowMenu];
199
208
[[NSApp mainMenu] addItem:windowMenuItem];
213
222
NSMenuItem *helpMenuItem;
214
223
NSMenuItem *menuItem;
216
helpMenu = [[NSMenu alloc] initWithTitle:@"Help"];
225
helpMenu = [[NSMenu alloc] initWithTitle:utf8str(_("Help"))];
218
227
/* "rlvm Readme" */
219
menuItem = [[NSMenuItem alloc] initWithTitle:@"Readme"
228
menuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Readme"))
220
229
action:@selector(showREADME:)
221
230
keyEquivalent:@""];
222
231
[menuItem setTarget:NSApp];
237
246
[menuItem release];
239
248
/* GNU General Public License v3 */
240
menuItem = [[NSMenuItem alloc] initWithTitle:@"GNU General Public License v3"
241
action:@selector(showGPL:)
249
menuItem = [[NSMenuItem alloc]
250
initWithTitle:utf8str(_("GNU General Public License v3"))
251
action:@selector(showGPL:)
243
253
[menuItem setTarget:NSApp];
244
254
[helpMenu addItem:menuItem];
245
255
[menuItem release];
247
257
/* Put the helpMenu into the menubar */
248
helpMenuItem = [[NSMenuItem alloc] initWithTitle:@"Help" action:nil keyEquivalent:@""];
258
helpMenuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Help"))
249
261
[helpMenuItem setSubmenu:helpMenu];
250
262
[[NSApp mainMenu] addItem:helpMenuItem];
348
357
/* Set the working directory to the .app's parent directory */
349
358
[self setupWorkingDirectory:gFinderLaunch];
352
/* Set the main menu to contain the real app name instead of "SDL App" */
353
[self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
356
/* If we were called from the Finder and we weren't given a file,
357
pop up a native modal dialog to select the file */
360
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
361
[oPanel setAllowsMultipleSelection:NO];
362
[oPanel setCanChooseFiles:NO];
363
[oPanel setCanChooseDirectories:YES];
364
[oPanel setTitle:@"Select game folder..."];
365
[oPanel setPrompt:@"Play"];
367
int status = [oPanel runModal];
368
if(status == NSCancelButton)
371
NSArray* filenames = [oPanel filenames];
372
NSString* filename = [filenames objectAtIndex:0];
376
/* Check for font issues */
377
const char* fontName = NULL;
378
if(findFontName(&fontName, [filename fileSystemRepresentation]))
384
setIncomingFilename(filename);
360
CocoaRLVMInstance instance;
387
362
/* Hand off to main application code */
388
363
gCalledAppMainline = TRUE;
389
status = SDL_main (gArgc, gArgv);
365
// TODO(erg): Share parameter parsing code and make this go away.
366
fs::path gamerootPath = instance.SelectGameDirectory();
367
if (gamerootPath.empty())
370
instance.Run(gamerootPath);
391
372
/* We're done, thank you for playing */
417
static void rlvm_set_locale() {
419
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
421
// A little bit of code stolen from MacVim. We need to also steal the Tiger
423
NSArray *languages = nil;
424
if ([NSLocale respondsToSelector:@selector(preferredLanguages)] == YES) {
426
languages = [NSLocale preferredLanguages];
429
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
430
languages = [defs objectForKey:@"AppleLanguages"];
433
if (languages && [languages count] > 0) {
434
int i, count = [languages count];
435
for (i = 0; i < count; ++i) {
436
if ([[languages objectAtIndex:i]
437
isEqualToString:@"en"]) {
442
NSRange r = { 0, count };
443
NSString *s = [[languages subarrayWithRange:r]
444
componentsJoinedByString:@":"];
445
setenv("LANGUAGE", [s UTF8String], 0);
448
NSString* locale_path = [[[NSBundle mainBundle] resourcePath]
449
stringByAppendingPathComponent:@"locale"];
450
setlocale(LC_ALL, "");
451
bindtextdomain("rlvm", [locale_path fileSystemRepresentation]);