File: appmenu_osx.mm

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (344 lines) | stat: -rw-r--r-- 12,068 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

// 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 "common/ustr.h"

#include "backends/platform/sdl/macosx/macosx-compat.h"
#include <Cocoa/Cocoa.h>
#include <AppKit/NSWorkspace.h>

#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
#define NSEventModifierFlagCommand NSCommandKeyMask
#define NSEventModifierFlagOption  NSAlternateKeyMask
#endif

#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_10
#define NSEventModifierFlags NSUInteger
#endif

#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Cocoa64BitGuide/64BitChangesCocoa/64BitChangesCocoa.html
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef unsigned long NSUInteger;
#else
typedef unsigned int NSUInteger;
#endif

// Those are not defined in the 10.4 SDK, but they are defined when targeting
// Mac OS X 10.4 or above in the 10.5 SDK, and they do work with 10.4.
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
enum {
	NSUTF32StringEncoding = 0x8c000100,
	NSUTF32BigEndianStringEncoding = 0x98000100,
	NSUTF32LittleEndianStringEncoding = 0x9c000100
};
#endif
#endif

// Apple added setAppleMenu in 10.5 and removed it in 10.6.
// But as the method still exists we declare it ourselves here.
// Yes, this works :)
@interface NSApplication(MissingFunction)
- (void)setAppleMenu:(NSMenu *)menu;
@end
// However maybe we should conditionally use it depending on the system on which we run ScummVM (and not
// the one on which we compile) to only do it on OS X 10.5.
// Here is the relevant bit from the release notes for 10.6:
// In Leopard and earlier, apps that tried to construct a menu bar without a nib would get an undesirable
// stubby application menu that could not be removed. To work around this problem on Leopard, you can call
// the undocumented setAppleMenu: method and pass it the application menu, like so:
//   [NSApp setAppleMenu:[[[NSApp mainMenu] itemAtIndex:0] submenu]];
// In SnowLeopard, this workaround is unnecessary and should not be used. Under SnowLeopard, the first menu
// is always identified as the application menu.

static void openFromBundle(NSString *file) {
	NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"rtf"];
	if (!path) {
		path = [[NSBundle mainBundle] pathForResource:file ofType:@"html"];
		if (!path) {
			path = [[NSBundle mainBundle] pathForResource:file ofType:@""];
			if (!path)
				path = [[NSBundle mainBundle] pathForResource:file ofType:@"md"];
		}
	}

	// RTF and HTML files are widely recognized and we can rely on the default
	// file association working for those. For the other ones this might not be
	// the case so we explicitly indicate they should be open with TextEdit.
	if (path) {
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_15
		if ([path hasSuffix:@".html"] || [path hasSuffix:@".rtf"])
			[[NSWorkspace sharedWorkspace] openFile:path];
		else
			[[NSWorkspace sharedWorkspace] openFile:path withApplication:@"TextEdit"];
#else
		NSURL *pathUrl = [NSURL fileURLWithPath:path isDirectory:NO];
		if ([path hasSuffix:@".html"] || [path hasSuffix:@".rtf"]) {
			[[NSWorkspace sharedWorkspace] openURL:pathUrl];
		} else {
			[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObjects:pathUrl, nil]
				withApplicationAtURL:[[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.apple.TextEdit"]
				configuration:[NSWorkspaceOpenConfiguration configuration]
				completionHandler:nil
			];
		}
#endif
	}
}

@interface ScummVMMenuHandler : NSObject {
}
- (void) openReadme;
- (void) openLicenseGPL;
- (void) openLicenseApache;
- (void) openLicenseBSD;
- (void) openLicenseBSL;
- (void) openLicenseFreefont;
- (void) openLicenseGLAD;
- (void) openLicenseISC;
- (void) openLicenseLGPL;
- (void) openLicenseLUA;
- (void) openLicenseMIT;
- (void) openLicenseMKV;
- (void) openLicenseMPL;
- (void) openLicenseOFL;
- (void) openLicenseTinyGL;
- (void) openLicenseCatharon;
- (void) openNews;
- (void) openUserManual;
- (void) openCredits;
@end

@implementation ScummVMMenuHandler : NSObject
- (void)openReadme {
	openFromBundle(@"README");
}

- (void)openLicenseGPL {
	openFromBundle(@"COPYING");
}

- (void)openLicenseApache {
	openFromBundle(@"COPYING-Apache");
}

- (void)openLicenseBSD {
	openFromBundle(@"COPYING-BSD");
}

- (void)openLicenseBSL {
	openFromBundle(@"COPYING-BSL");
}

- (void)openLicenseFreefont {
	openFromBundle(@"COPYING-FREEFONT");
}

- (void)openLicenseGLAD {
	openFromBundle(@"COPYING-GLAD");
}

- (void)openLicenseISC {
	openFromBundle(@"COPYING-ISC");
}

- (void)openLicenseLGPL {
	openFromBundle(@"COPYING-LGPL");
}

- (void)openLicenseLUA {
	openFromBundle(@"COPYING-LUA");
}

- (void)openLicenseMIT {
	openFromBundle(@"COPYING-MIT");
}

- (void)openLicenseMKV {
	openFromBundle(@"COPYING-MKV");
}

- (void)openLicenseMPL {
	openFromBundle(@"COPYING-MPL");
}

- (void)openLicenseOFL {
	openFromBundle(@"COPYING-OFL");
}

- (void)openLicenseTinyGL {
	openFromBundle(@"COPYING-TINYGL");
}

- (void)openLicenseCatharon {
	openFromBundle(@"CatharonLicense-txt");
}

- (void)openNews {
	openFromBundle(@"NEWS");
}

- (void)openUserManual {
	// If present locally in the bundle, open that file.
	if ([[NSFileManager defaultManager] respondsToSelector:@selector(contentsOfDirectoryAtPath:error:)]) {
		NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
		NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];
		NSEnumerator *dirEnum = [dirContents objectEnumerator];
		NSString *file;
		while ((file = [dirEnum nextObject])) {
			if ([file hasPrefix:@"ScummVM Manual"] && [file hasSuffix:@".pdf"]) {
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_15
				[[NSWorkspace sharedWorkspace] openFile:[bundlePath stringByAppendingPathComponent:file]];
#else
				NSURL *fileUrl = [NSURL fileURLWithPath:[bundlePath stringByAppendingPathComponent:file] isDirectory:NO];
				[[NSWorkspace sharedWorkspace] openURL:fileUrl];
#endif
				return;
			}
		}
	}

	// Otherwise try to access the only version.
	[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://docs.scummvm.org/"]];
}

- (void)openCredits {
	openFromBundle(@"AUTHORS");
}
@end

NSString *constructNSStringFromU32String(const Common::U32String &rawU32String) {
#ifdef SCUMM_LITTLE_ENDIAN
	NSStringEncoding stringEncoding = NSUTF32LittleEndianStringEncoding;
#else
	NSStringEncoding stringEncoding = NSUTF32BigEndianStringEncoding;
#endif
	return [[NSString alloc] initWithBytes:rawU32String.c_str() length:4*rawU32String.size() encoding: stringEncoding];
}

static NSMenu *addMenu(const Common::U32String &title, NSString *key, SEL setAs) {
	if (setAs && ![NSApp respondsToSelector:setAs]) {
		return nil;
	}

	NSString *str = constructNSStringFromU32String(title);
	NSMenu *menu = [[NSMenu alloc] initWithTitle:str];

	NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:str action:nil keyEquivalent:key];
	[menuItem setSubmenu:menu];
	[[NSApp mainMenu] addItem:menuItem];

	if (setAs) {
		[NSApp performSelector:setAs withObject:menu];
	}

	[str release];
	[menuItem release];

	return menu;
}

static void addMenuItem(const Common::U32String &title, id target, SEL selector, NSString *key, NSMenu *parent, NSEventModifierFlags flags = 0) {
	NSString *nsString = constructNSStringFromU32String(title);
	NSMenuItem *menuItem = [[NSMenuItem alloc]
							initWithTitle:nsString
							action:selector
							keyEquivalent:key];
	if (target) {
		[menuItem setTarget:target];
	}
	if (flags) {
		[menuItem setKeyEquivalentModifierMask:flags];
	}
	[parent addItem:menuItem];
	[nsString release];
}

static ScummVMMenuHandler *delegate = nullptr;

void releaseMenu() {
	[delegate release];
	delegate = nullptr;
}

void replaceApplicationMenuItems() {
	// We cannot use [[NSApp mainMenu] removeAllItems] as removeAllItems was added in OS X 10.6
	// So remove the SDL generated menus one by one instead.
	while ([[NSApp mainMenu] numberOfItems] > 0) {
		[[NSApp mainMenu] removeItemAtIndex:0];
	}

	NSMenu *appleMenu = addMenu(Common::U32String("ScummVM"), @"", @selector(setAppleMenu:));
	if (appleMenu) {
		addMenuItem(_("About ScummVM"), nil, @selector(orderFrontStandardAboutPanel:), @"", appleMenu);
		[appleMenu addItem:[NSMenuItem separatorItem]];
		addMenuItem(_("Hide ScummVM"), nil, @selector(hide:), @"h", appleMenu);
		addMenuItem(_("Hide Others"), nil, @selector(hideOtherApplications:), @"h", appleMenu, (NSEventModifierFlagOption|NSEventModifierFlagCommand));
		addMenuItem(_("Show All"), nil, @selector(unhideAllApplications:), @"", appleMenu);
		[appleMenu addItem:[NSMenuItem separatorItem]];
		addMenuItem(_("Quit ScummVM"), nil, @selector(terminate:), @"q", appleMenu);
	}

	NSMenu *windowMenu = addMenu(_("Window"), @"", @selector(setWindowsMenu:));
	if (windowMenu) {
		addMenuItem(_("Minimize"), nil, @selector(performMiniaturize:), @"m", windowMenu);
	}

	// Note: this part is expected not to work at run-time on 10.5 and earlier,
	// because setHelpMenu is only available on 10.6+ (see Bug#11260).
	NSMenu *helpMenu = addMenu(_("Help"), @"", @selector(setHelpMenu:));
	if (helpMenu) {
		if (!delegate) {
			delegate = [[ScummVMMenuHandler alloc] init];
		}
		addMenuItem(_("User Manual"), delegate, @selector(openUserManual), @"", helpMenu);
		[helpMenu addItem:[NSMenuItem separatorItem]];
		addMenuItem(_("General Information"), delegate, @selector(openReadme), @"", helpMenu);
		addMenuItem(_("What's New in ScummVM"), delegate, @selector(openNews), @"", helpMenu);
		[helpMenu addItem:[NSMenuItem separatorItem]];
		addMenuItem(_("Credits"), delegate, @selector(openCredits), @"", helpMenu);
		addMenuItem(_("GPL License"), delegate, @selector(openLicenseGPL), @"", helpMenu);
		addMenuItem(_("LGPL License"), delegate, @selector(openLicenseLGPL), @"", helpMenu);
		addMenuItem(_("Freefont License"), delegate, @selector(openLicenseFreefont), @"", helpMenu);
		addMenuItem(_("OFL License"), delegate, @selector(openLicenseOFL), @"", helpMenu);
		addMenuItem(_("BSD License"), delegate, @selector(openLicenseBSD), @"", helpMenu);

		addMenuItem(_("Apache License"), delegate, @selector(openLicenseApache), @"", helpMenu);
		addMenuItem(_("BSL License"), delegate, @selector(openLicenseBSL), @"", helpMenu);
		addMenuItem(_("GLAD License"), delegate, @selector(openLicenseGLAD), @"", helpMenu);
		addMenuItem(_("ISC License"), delegate, @selector(openLicenseISC), @"", helpMenu);
		addMenuItem(_("Lua License"), delegate, @selector(openLicenseLUA), @"", helpMenu);
		addMenuItem(_("MIT License"), delegate, @selector(openLicenseMIT), @"", helpMenu);
		addMenuItem(_("MKV License"), delegate, @selector(openLicenseMKV), @"", helpMenu);
		addMenuItem(_("MPL License"), delegate, @selector(openLicenseMPL), @"", helpMenu);
		addMenuItem(_("TinyGL License"), delegate, @selector(openLicenseTinyGL), @"", helpMenu);
		addMenuItem(_("Catharon License"), delegate, @selector(openLicenseCatharon), @"", helpMenu);
	}

	[appleMenu release];
	[windowMenu release];
	[helpMenu release];
}