~ubuntu-branches/ubuntu/utopic/rlvm/utopic-proposed

« back to all changes in this revision

Viewing changes to src/Platforms/osx/SDLMain.mm

  • Committer: Bazaar Package Importer
  • Author(s): Ying-Chun Liu (PaulLiu), Ying-Chun Liu (PaulLiu), Elliot Glaysher
  • Date: 2011-05-19 00:28:44 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110519002844-qszwmj7oiixww0eg
Tags: 0.12-1
[ Ying-Chun Liu (PaulLiu) <paulliu@debian.org> ]
* New upstream release

[ Elliot Glaysher <glaysher@umich.edu> ]
* New GTK+ interface with desktop integration and UI refinements
* Partial Japanese localizations
* Fix graphics corruption in in-game dialogs when a dialog is brought
  up, and then fullscreen mode activated
* Smooth the output of text in rlBabel using games
* Don't play voice samples while fast forwarding

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
#import "SDL.h"
9
9
#import "SDLMain.h"
10
 
#import "FindFont.h"
11
10
#import <sys/param.h> /* for MAXPATHLEN */
12
11
#import <unistd.h>
13
12
 
14
13
#import <AppKit/NSPanel.h>
15
14
 
 
15
#include "Utilities/gettext.h"
 
16
#include "Platforms/osx/CocoaRLVMInstance.h"
 
17
 
 
18
#include <boost/filesystem/operations.hpp>
 
19
namespace fs = boost::filesystem;
 
20
 
 
21
NSString* utf8str(const char* str) {
 
22
  return [NSString stringWithUTF8String:str];
 
23
}
 
24
 
 
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()];
 
28
}
 
29
 
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. */
20
34
- (void)setAppleMenu:(NSMenu *)menu;
21
35
@end
22
36
 
23
 
/* Use this flag to determine whether we use SDLMain.nib or not */
24
 
#define         SDL_USE_NIB_FILE        0
25
 
 
26
37
static int    gArgc;
27
38
static char  **gArgv;
28
39
static BOOL   gFinderLaunch;
44
55
    return appName;
45
56
}
46
57
 
47
 
#if SDL_USE_NIB_FILE
48
 
/* A helper category for NSString */
49
 
@interface NSString (ReplaceSubString)
50
 
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
51
 
@end
52
 
#endif
53
58
 
54
59
@interface SDLApplication : NSApplication
55
60
@end
56
61
 
 
62
static NSEvent* lastEvent = nil;
 
63
 
 
64
NSEvent* GetLastRightClickEvent() {
 
65
  return lastEvent;
 
66
}
 
67
 
57
68
@implementation SDLApplication
58
69
/* Invoked from the Quit menu item */
59
70
- (void)terminate:(id)sender
64
75
    SDL_PushEvent(&event);
65
76
}
66
77
 
 
78
-(void)sendEvent:(NSEvent*)event
 
79
{
 
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) {
 
85
    [lastEvent release];
 
86
    lastEvent = [event retain];
 
87
  }
 
88
 
 
89
  [super sendEvent:event];
 
90
}
67
91
 
68
92
-(void)showREADME:(id)sender
69
93
{
108
132
 
109
133
}
110
134
 
111
 
#if SDL_USE_NIB_FILE
112
 
 
113
 
/* Fix menu to contain the real app name instead of "SDL App" */
114
 
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
115
 
{
116
 
    NSRange aRange;
117
 
    NSEnumerator *enumerator;
118
 
    NSMenuItem *menuItem;
119
 
 
120
 
    aRange = [[aMenu title] rangeOfString:@"SDL App"];
121
 
    if (aRange.length != 0)
122
 
        [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
123
 
 
124
 
    enumerator = [[aMenu itemArray] objectEnumerator];
125
 
    while ((menuItem = [enumerator nextObject]))
126
 
    {
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];
132
 
    }
133
 
    [ aMenu sizeToFit ];
134
 
}
135
 
 
136
 
#else
137
 
 
138
135
static void setApplicationMenu(void)
139
136
{
140
137
    /* warning: this code is very odd */
147
144
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
148
145
    
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:@""];
152
149
 
153
150
    [appleMenu addItem:[NSMenuItem separatorItem]];
154
151
 
155
 
    title = [@"Hide " stringByAppendingString:appName];
 
152
    title = b_format(_("Hide %1%"), appName);
156
153
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
157
154
 
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:)
 
158
                                  keyEquivalent:@"h"];
159
159
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
160
160
 
161
 
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
 
161
    [appleMenu addItemWithTitle:utf8str(_("Show All"))
 
162
                         action:@selector(unhideAllApplications:)
 
163
                  keyEquivalent:@""];
162
164
 
163
165
    [appleMenu addItem:[NSMenuItem separatorItem]];
164
166
 
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:)
 
170
                  keyEquivalent:@"q"];
167
171
 
168
 
    
169
172
    /* Put menu into the menubar */
170
 
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
 
173
    menuItem = [[NSMenuItem alloc] initWithTitle:@""
 
174
                                          action:nil
 
175
                                   keyEquivalent:@""];
171
176
    [menuItem setSubmenu:appleMenu];
172
177
    [[NSApp mainMenu] addItem:menuItem];
173
178
 
186
191
    NSMenuItem  *windowMenuItem;
187
192
    NSMenuItem  *menuItem;
188
193
 
189
 
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
 
194
    windowMenu = [[NSMenu alloc] initWithTitle:utf8str(_("Window"))];
190
195
    
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:)
 
199
                                   keyEquivalent:@"m"];
193
200
    [windowMenu addItem:menuItem];
194
201
    [menuItem release];
195
202
    
196
203
    /* Put menu into the menubar */
197
 
    windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
 
204
    windowMenuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Window"))
 
205
                                                action:nil
 
206
                                         keyEquivalent:@""];
198
207
    [windowMenuItem setSubmenu:windowMenu];
199
208
    [[NSApp mainMenu] addItem:windowMenuItem];
200
209
    
213
222
    NSMenuItem  *helpMenuItem;
214
223
    NSMenuItem  *menuItem;
215
224
 
216
 
    helpMenu = [[NSMenu alloc] initWithTitle:@"Help"];
 
225
    helpMenu = [[NSMenu alloc] initWithTitle:utf8str(_("Help"))];
217
226
 
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];
229
238
    [menuItem release];
230
239
 
231
240
    /* COPYING */
232
 
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Copying" 
 
241
    menuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Copying"))
233
242
                                   action:@selector(showCopying:)
234
243
                                   keyEquivalent:@""];
235
244
    [menuItem setTarget:NSApp];
237
246
    [menuItem release];
238
247
 
239
248
    /* GNU General Public License v3 */
240
 
    menuItem = [[NSMenuItem alloc] initWithTitle:@"GNU General Public License v3" 
241
 
                                   action:@selector(showGPL:)
242
 
                                   keyEquivalent:@""];
 
249
    menuItem = [[NSMenuItem alloc]
 
250
                 initWithTitle:utf8str(_("GNU General Public License v3"))
 
251
                        action:@selector(showGPL:)
 
252
                 keyEquivalent:@""];
243
253
    [menuItem setTarget:NSApp];
244
254
    [helpMenu addItem:menuItem];
245
255
    [menuItem release];
246
256
 
247
257
    /* Put the helpMenu into the menubar */
248
 
    helpMenuItem = [[NSMenuItem alloc] initWithTitle:@"Help" action:nil keyEquivalent:@""];
 
258
    helpMenuItem = [[NSMenuItem alloc] initWithTitle:utf8str(_("Help"))
 
259
                                              action:nil
 
260
                                       keyEquivalent:@""];
249
261
    [helpMenuItem setSubmenu:helpMenu];
250
262
    [[NSApp mainMenu] addItem:helpMenuItem];
251
263
 
281
293
    [pool release];
282
294
}
283
295
 
284
 
#endif
285
 
 
286
 
 
287
296
BOOL pushArg(const char* newArg)
288
297
{
289
298
  size_t arglen;
348
357
    /* Set the working directory to the .app's parent directory */
349
358
    [self setupWorkingDirectory:gFinderLaunch];
350
359
 
351
 
#if SDL_USE_NIB_FILE
352
 
    /* Set the main menu to contain the real app name instead of "SDL App" */
353
 
    [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
354
 
#endif
355
 
 
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 */
358
 
    if(gArgc == 1)
359
 
    {
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"];
366
 
 
367
 
      int status = [oPanel runModal];
368
 
      if(status == NSCancelButton)
369
 
        exit(-1);
370
 
 
371
 
      NSArray* filenames = [oPanel filenames];
372
 
      NSString* filename = [filenames objectAtIndex:0];
373
 
      if(filename == NULL)
374
 
        exit(-2);
375
 
 
376
 
      /* Check for font issues */
377
 
      const char* fontName = NULL;
378
 
      if(findFontName(&fontName, [filename fileSystemRepresentation]))
379
 
      {
380
 
        pushArg("--font");
381
 
        pushArg(fontName);
382
 
      }
383
 
 
384
 
      setIncomingFilename(filename);
385
 
    }
 
360
    CocoaRLVMInstance instance;
386
361
 
387
362
    /* Hand off to main application code */
388
363
    gCalledAppMainline = TRUE;
389
 
    status = SDL_main (gArgc, gArgv);
 
364
 
 
365
    // TODO(erg): Share parameter parsing code and make this go away.
 
366
    fs::path gamerootPath = instance.SelectGameDirectory();
 
367
    if (gamerootPath.empty())
 
368
      exit(-1);
 
369
 
 
370
    instance.Run(gamerootPath);
390
371
 
391
372
    /* We're done, thank you for playing */
392
 
    exit(status);
 
373
    exit(0);
393
374
}
394
375
@end
395
376
 
433
414
 
434
415
@end
435
416
 
436
 
 
 
417
static void rlvm_set_locale() {
 
418
#ifdef ENABLE_NLS
 
419
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
 
420
 
 
421
    // A little bit of code stolen from MacVim. We need to also steal the Tiger
 
422
    // version.
 
423
    NSArray *languages = nil;
 
424
    if ([NSLocale respondsToSelector:@selector(preferredLanguages)] == YES) {
 
425
      // 10.5
 
426
      languages = [NSLocale preferredLanguages];
 
427
    } else {
 
428
      // 10.4
 
429
      NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 
430
      languages = [defs objectForKey:@"AppleLanguages"];
 
431
    }
 
432
 
 
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"]) {
 
438
          count = i+1;
 
439
          break;
 
440
        }
 
441
      }
 
442
      NSRange r = { 0, count };
 
443
      NSString *s = [[languages subarrayWithRange:r]
 
444
                      componentsJoinedByString:@":"];
 
445
      setenv("LANGUAGE", [s UTF8String], 0);
 
446
    }
 
447
 
 
448
    NSString* locale_path = [[[NSBundle mainBundle] resourcePath]
 
449
                              stringByAppendingPathComponent:@"locale"];
 
450
    setlocale(LC_ALL, "");
 
451
    bindtextdomain("rlvm", [locale_path fileSystemRepresentation]);
 
452
    textdomain("rlvm");
 
453
 
 
454
    [pool release];
 
455
#endif
 
456
}
437
457
 
438
458
#ifdef main
439
459
#  undef main
460
480
        gFinderLaunch = NO;
461
481
    }
462
482
 
463
 
#if SDL_USE_NIB_FILE
464
 
    [SDLApplication poseAsClass:[NSApplication class]];
465
 
    NSApplicationMain (argc, argv);
466
 
#else
 
483
    rlvm_set_locale();
 
484
 
467
485
    CustomApplicationMain (argc, argv);
468
 
#endif
469
486
    return 0;
470
487
}
471
488