| 1 |
#import <Cocoa/Cocoa.h> |
| 2 |
#import <Availability.h> |
| 3 |
#import <IOKit/hid/IOHIDLib.h> |
| 4 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 |
| 5 |
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h> |
| 6 |
#endif |
| 7 |
|
| 8 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 9 |
#include "allegro5/allegro.h" |
| 10 |
#include "allegro5/allegro_native_dialog.h" |
| 11 |
#include "allegro5/internal/aintern_native_dialog.h" |
| 12 |
#include "allegro5/platform/aintosx.h" |
| 13 |
#include "allegro5/allegro_osx.h" |
| 14 |
|
| 15 |
bool _al_init_native_dialog_addon(void) |
| 16 |
{ |
| 17 |
return true; |
| 18 |
} |
| 19 |
|
| 20 |
void _al_shutdown_native_dialog_addon(void) |
| 21 |
{ |
| 22 |
} |
| 23 |
#pragma mark File Dialog |
| 24 |
|
| 25 |
static NSArray * get_filter_array(const _AL_VECTOR *patterns) |
| 26 |
{ |
| 27 |
NSMutableArray *filter_array = [[NSMutableArray alloc] init]; |
| 28 |
|
| 29 |
bool any_catchalls = false; |
| 30 |
for (size_t i = 0; i < _al_vector_size(patterns); i++) { |
| 31 |
_AL_PATTERNS_AND_DESC *patterns_and_desc = _al_vector_ref(patterns, (int)i); |
| 32 |
for (size_t j = 0; j < _al_vector_size(&patterns_and_desc->patterns_vec); j++) { |
| 33 |
_AL_PATTERN *pattern = _al_vector_ref(&patterns_and_desc->patterns_vec, (int)j); |
| 34 |
if (pattern->is_catchall) { |
| 35 |
any_catchalls = true; |
| 36 |
break; |
| 37 |
} |
| 38 |
char *cstr = al_cstr_dup(al_ref_info(&pattern->info)); |
| 39 |
NSString *filter_text = [NSString stringWithUTF8String: cstr]; |
| 40 |
al_free(cstr); |
| 41 |
if (!pattern->is_mime) { |
| 42 |
/* MacOS expects extensions, so make an attempt to extract them. */ |
| 43 |
NSArray *parts = [filter_text componentsSeparatedByString: @"."]; |
| 44 |
size_t num_parts = parts.count; |
| 45 |
if (num_parts <= 1) |
| 46 |
continue; |
| 47 |
/* Extensions with dots did not work for me, so just grab the last component. */ |
| 48 |
parts = [parts subarrayWithRange:NSMakeRange(num_parts - 1, 1)]; |
| 49 |
filter_text = [parts componentsJoinedByString: @"."]; |
| 50 |
} |
| 51 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 |
| 52 |
UTType *type; |
| 53 |
if (pattern->is_mime) |
| 54 |
type = [UTType typeWithMIMEType:filter_text]; |
| 55 |
else |
| 56 |
type = [UTType typeWithFilenameExtension:filter_text]; |
| 57 |
if (type != nil) |
| 58 |
[filter_array addObject: type]; |
| 59 |
#else |
| 60 |
if (pattern->is_mime) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
[filter_array addObject: filter_text]; |
| 64 |
#endif |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (any_catchalls) { |
| 69 |
filter_array = [[NSMutableArray alloc] init]; |
| 70 |
} |
| 71 |
|
| 72 |
return filter_array; |
| 73 |
} |
| 74 |
|
| 75 |
bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display, |
| 76 |
ALLEGRO_NATIVE_DIALOG *fd) |
| 77 |
{ |
| 78 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 79 |
(void)display; |
| 80 |
int mode = fd->flags; |
| 81 |
NSString *filename; |
| 82 |
NSURL *directory; |
| 83 |
|
| 84 |
/* Set initial directory to pass to the file selector */ |
| 85 |
if (fd->fc_initial_path) { |
| 86 |
ALLEGRO_PATH *initial_directory = al_clone_path(fd->fc_initial_path); |
| 87 |
/* Strip filename from path */ |
| 88 |
al_set_path_filename(initial_directory, NULL); |
| 89 |
|
| 90 |
/* Convert path and filename to NSString objects */ |
| 91 |
directory = [NSURL fileURLWithPath: [NSString stringWithUTF8String: al_path_cstr(initial_directory, '/')] |
| 92 |
isDirectory: YES]; |
| 93 |
filename = [NSString stringWithUTF8String: al_get_path_filename(fd->fc_initial_path)]; |
| 94 |
al_destroy_path(initial_directory); |
| 95 |
} else { |
| 96 |
directory = nil; |
| 97 |
filename = nil; |
| 98 |
} |
| 99 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 100 |
/* We need slightly different code for SAVE and LOAD dialog boxes, which |
| 101 |
* are handled by slightly different classes. |
| 102 |
*/ |
| 103 |
if (mode & ALLEGRO_FILECHOOSER_SAVE) { // Save dialog |
| 104 |
NSSavePanel *panel = [NSSavePanel savePanel]; |
| 105 |
NSArray *filter_array; |
| 106 |
|
| 107 |
/* Set file save dialog box options */ |
| 108 |
[panel setCanCreateDirectories: YES]; |
| 109 |
[panel setCanSelectHiddenExtension: YES]; |
| 110 |
[panel setAllowsOtherFileTypes: YES]; |
| 111 |
[panel setExtensionHidden: NO]; |
| 112 |
if (filename) { |
| 113 |
[panel setNameFieldStringValue:filename]; |
| 114 |
} |
| 115 |
if (_al_vector_size(&fd->fc_patterns) > 0) { |
| 116 |
filter_array = get_filter_array(&fd->fc_patterns); |
| 117 |
if (filter_array && [filter_array count] > 0) { |
| 118 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 |
| 119 |
[panel setAllowedContentTypes:filter_array]; |
| 120 |
#else |
| 121 |
[panel setAllowedFileTypes:filter_array]; |
| 122 |
#endif |
| 123 |
} |
| 124 |
} |
| 125 |
[panel setDirectoryURL: directory]; |
| 126 |
/* Open dialog box */ |
| 127 |
if ([panel runModal] == NSOKButton) { |
| 128 |
/* NOTE: at first glance, it looks as if this code might leak |
| 129 |
* memory, but in fact it doesn't: the string returned by |
| 130 |
* UTF8String is freed automatically when it goes out of scope |
| 131 |
* (according to the UTF8String docs anyway). |
| 132 |
*/ |
| 133 |
const char *s = [[[panel URL] path] UTF8String]; |
| 134 |
fd->fc_path_count = 1; |
| 135 |
fd->fc_paths = al_malloc(fd->fc_path_count * sizeof *fd->fc_paths); |
| 136 |
fd->fc_paths[0] = al_create_path(s); |
| 137 |
} |
| 138 |
} else { // Open dialog |
| 139 |
NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 140 |
NSArray *filter_array; |
| 141 |
|
| 142 |
/* Set file selection box options */ |
| 143 |
if (mode & ALLEGRO_FILECHOOSER_FOLDER) { |
| 144 |
[panel setCanChooseFiles: NO]; |
| 145 |
[panel setCanChooseDirectories: YES]; |
| 146 |
} else { |
| 147 |
[panel setCanChooseFiles: YES]; |
| 148 |
[panel setCanChooseDirectories: NO]; |
| 149 |
} |
| 150 |
|
| 151 |
[panel setResolvesAliases:YES]; |
| 152 |
if (mode & ALLEGRO_FILECHOOSER_MULTIPLE) |
| 153 |
[panel setAllowsMultipleSelection: YES]; |
| 154 |
else |
| 155 |
[panel setAllowsMultipleSelection: NO]; |
| 156 |
[panel setDirectoryURL:directory]; |
| 157 |
if (filename) { |
| 158 |
[panel setNameFieldStringValue:filename]; |
| 159 |
} |
| 160 |
if (_al_vector_size(&fd->fc_patterns) > 0) { |
| 161 |
filter_array = get_filter_array(&fd->fc_patterns); |
| 162 |
if (filter_array && [filter_array count] > 0) { |
| 163 |
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 |
| 164 |
[panel setAllowedContentTypes:filter_array]; |
| 165 |
#else |
| 166 |
[panel setAllowedFileTypes:filter_array]; |
| 167 |
#endif |
| 168 |
} |
| 169 |
} |
| 170 |
/* Open dialog box */ |
| 171 |
if ([panel runModal] == NSOKButton) { |
| 172 |
size_t i; |
| 173 |
fd->fc_path_count = [[panel URLs] count]; |
| 174 |
fd->fc_paths = al_malloc(fd->fc_path_count * sizeof *fd->fc_paths); |
| 175 |
for (i = 0; i < fd->fc_path_count; i++) { |
| 176 |
/* NOTE: at first glance, it looks as if this code might leak |
| 177 |
* memory, but in fact it doesn't: the string returned by |
| 178 |
* UTF8String is freed automatically when it goes out of scope |
| 179 |
* (according to the UTF8String docs anyway). |
| 180 |
*/ |
| 181 |
NSURL* url = [[panel URLs] objectAtIndex: i]; |
| 182 |
const char* s = [[url path] UTF8String]; |
| 183 |
fd->fc_paths[i] = al_create_path(s); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
}); |
| 189 |
_al_osx_clear_mouse_state(); |
| 190 |
|
| 191 |
[pool release]; |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
#pragma mark Alert Box |
| 197 |
int _al_show_native_message_box(ALLEGRO_DISPLAY *display, |
| 198 |
ALLEGRO_NATIVE_DIALOG *fd) |
| 199 |
{ |
| 200 |
(void)display; |
| 201 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 202 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 203 |
NSString* button_text; |
| 204 |
unsigned int i; |
| 205 |
NSAlert* box = [[NSAlert alloc] init]; |
| 206 |
|
| 207 |
if (fd->mb_buttons == NULL) { |
| 208 |
button_text = @"OK"; |
| 209 |
if (fd->flags & ALLEGRO_MESSAGEBOX_YES_NO) button_text = @"Yes|No"; |
| 210 |
if (fd->flags & ALLEGRO_MESSAGEBOX_OK_CANCEL) button_text = @"OK|Cancel"; |
| 211 |
} |
| 212 |
else { |
| 213 |
button_text = [NSString stringWithUTF8String: al_cstr(fd->mb_buttons)]; |
| 214 |
} |
| 215 |
|
| 216 |
NSArray* buttons = [button_text componentsSeparatedByString: @"|"]; |
| 217 |
[box setMessageText:[NSString stringWithUTF8String: al_cstr(fd->title)]]; |
| 218 |
[box setInformativeText:[NSString stringWithUTF8String: al_cstr(fd->mb_text)]]; |
| 219 |
[box setAlertStyle: NSWarningAlertStyle]; |
| 220 |
[[box window] setLevel: NSFloatingWindowLevel]; |
| 221 |
for (i = 0; i < [buttons count]; ++i) |
| 222 |
[box addButtonWithTitle: [buttons objectAtIndex: i]]; |
| 223 |
|
| 224 |
int retval = [box runModal]; |
| 225 |
fd->mb_pressed_button = retval + 1 - NSAlertFirstButtonReturn; |
| 226 |
[box release]; |
| 227 |
}); |
| 228 |
[pool release]; |
| 229 |
_al_osx_clear_mouse_state(); |
| 230 |
return fd->mb_pressed_button; |
| 231 |
} |
| 232 |
|
| 233 |
#pragma mark Text Log View |
| 234 |
|
| 235 |
@interface ALLEGLogView : NSTextView <NSWindowDelegate> |
| 236 |
{ |
| 237 |
@public |
| 238 |
ALLEGRO_NATIVE_DIALOG *textlog; |
| 239 |
} |
| 240 |
- (void)keyDown: (NSEvent*)event; |
| 241 |
- (BOOL)windowShouldClose: (id)sender; |
| 242 |
- (void)emitCloseEventWithKeypress: (BOOL)keypress; |
| 243 |
- (void)appendText: (NSString*)text; |
| 244 |
@end |
| 245 |
|
| 246 |
|
| 247 |
@implementation ALLEGLogView |
| 248 |
|
| 249 |
|
| 250 |
- (void)keyDown: (NSEvent*)event |
| 251 |
{ |
| 252 |
if (([event keyCode] == 0x35) || // Escape |
| 253 |
(([event keyCode] == 0x0D) && ([event modifierFlags] & NSCommandKeyMask))) { // Command+W |
| 254 |
[[self window] close]; |
| 255 |
[self emitCloseEventWithKeypress: YES]; |
| 256 |
} |
| 257 |
else { |
| 258 |
[super keyDown: event]; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
- (BOOL)windowShouldClose: (id)sender |
| 263 |
{ |
| 264 |
(void)sender; |
| 265 |
if (self->textlog->is_active) { |
| 266 |
if (!(self->textlog->flags & ALLEGRO_TEXTLOG_NO_CLOSE)) { |
| 267 |
[self emitCloseEventWithKeypress: NO]; |
| 268 |
} |
| 269 |
} |
| 270 |
return YES; |
| 271 |
} |
| 272 |
|
| 273 |
- (void)emitCloseEventWithKeypress: (BOOL)keypress |
| 274 |
{ |
| 275 |
ALLEGRO_EVENT event; |
| 276 |
event.user.type = ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE; |
| 277 |
event.user.timestamp = al_get_time(); |
| 278 |
event.user.data1 = (intptr_t)self->textlog; |
| 279 |
event.user.data2 = (intptr_t)keypress; |
| 280 |
al_emit_user_event(&self->textlog->tl_events, &event, NULL); |
| 281 |
} |
| 282 |
|
| 283 |
- (void)appendText: (NSString*)text |
| 284 |
{ |
| 285 |
id keys[] = {NSForegroundColorAttributeName, NSFontAttributeName}; |
| 286 |
id objects[] = {[NSColor controlTextColor], [NSFont userFixedPitchFontOfSize: 0]}; |
| 287 |
int count = textlog->flags & ALLEGRO_TEXTLOG_MONOSPACE ? 2 : 1; |
| 288 |
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; |
| 289 |
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:text attributes:attributes] autorelease]; |
| 290 |
NSTextStorage* store = [self textStorage]; |
| 291 |
[store beginEditing]; |
| 292 |
[store appendAttributedString:attributedString]; |
| 293 |
[store endEditing]; |
| 294 |
} |
| 295 |
@end |
| 296 |
|
| 297 |
@interface ALLEGScrollView : NSScrollView <NSWindowDelegate> |
| 298 |
{ |
| 299 |
} |
| 300 |
- (void)appendText: (NSString*)text; |
| 301 |
@end |
| 302 |
|
| 303 |
@implementation ALLEGScrollView |
| 304 |
|
| 305 |
- (void)appendText: (NSString*)text |
| 306 |
{ |
| 307 |
ALLEGLogView *view = (ALLEGLogView *)[self documentView]; |
| 308 |
[view appendText:text]; |
| 309 |
float bottom = view.frame.size.height; |
| 310 |
float container_height = self.frame.size.height; |
| 311 |
if (bottom > container_height) |
| 312 |
[self.contentView scrollToPoint: NSMakePoint(0, bottom)]; |
| 313 |
} |
| 314 |
@end |
| 315 |
|
| 316 |
bool _al_open_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 317 |
{ |
| 318 |
NSRect rect = NSMakeRect(0, 0, 640, 480); |
| 319 |
int adapter = al_get_new_display_adapter(); |
| 320 |
NSScreen *screen; |
| 321 |
unsigned int mask = NSTitledWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask; |
| 322 |
if (!(textlog->flags & ALLEGRO_TEXTLOG_NO_CLOSE)) |
| 323 |
mask |= NSClosableWindowMask; |
| 324 |
|
| 325 |
if ((adapter >= 0) && (adapter < al_get_num_video_adapters())) { |
| 326 |
screen = [[NSScreen screens] objectAtIndex: adapter]; |
| 327 |
} else { |
| 328 |
screen = [NSScreen mainScreen]; |
| 329 |
} |
| 330 |
|
| 331 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 332 |
NSWindow *win = [[NSWindow alloc] initWithContentRect: rect |
| 333 |
styleMask: mask |
| 334 |
backing: NSBackingStoreBuffered |
| 335 |
defer: NO |
| 336 |
screen: screen]; |
| 337 |
[win setReleasedWhenClosed: NO]; |
| 338 |
[win setTitle: @"Allegro Text Log"]; |
| 339 |
[win setMinSize: NSMakeSize(128, 128)]; |
| 340 |
ALLEGScrollView *scrollView = [[ALLEGScrollView alloc] initWithFrame: rect]; |
| 341 |
[scrollView setHasHorizontalScroller: YES]; |
| 342 |
[scrollView setHasVerticalScroller: YES]; |
| 343 |
[scrollView setAutohidesScrollers: YES]; |
| 344 |
[scrollView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 345 |
|
| 346 |
[[scrollView contentView] setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 347 |
[[scrollView contentView] setAutoresizesSubviews: YES]; |
| 348 |
|
| 349 |
NSRect framerect = [[scrollView contentView] frame]; |
| 350 |
ALLEGLogView *view = [[ALLEGLogView alloc] initWithFrame: framerect]; |
| 351 |
view->textlog = textlog; |
| 352 |
[view setHorizontallyResizable: YES]; |
| 353 |
[view setVerticallyResizable: YES]; |
| 354 |
[view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable]; |
| 355 |
[[view textContainer] setContainerSize: NSMakeSize(rect.size.width, 1000000)]; |
| 356 |
[[view textContainer] setWidthTracksTextView: NO]; |
| 357 |
[view setEditable: NO]; |
| 358 |
[scrollView setDocumentView: view]; |
| 359 |
|
| 360 |
[[win contentView] addSubview: scrollView]; |
| 361 |
[scrollView release]; |
| 362 |
|
| 363 |
[win setDelegate: view]; |
| 364 |
[win orderFront: nil]; |
| 365 |
|
| 366 |
/* Save handles for future use. */ |
| 367 |
textlog->window = win; // Non-owning reference |
| 368 |
textlog->tl_textview = scrollView; // Non-owning reference |
| 369 |
}); |
| 370 |
/* Now notify al_show_native_textlog that the text log is ready. */ |
| 371 |
textlog->is_active = true; |
| 372 |
textlog->tl_done = true; |
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
void _al_close_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 377 |
{ |
| 378 |
NSWindow *win = (NSWindow *)textlog->window; |
| 379 |
__block bool is_visible = false; |
| 380 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 381 |
is_visible = [win isVisible]; |
| 382 |
}); |
| 383 |
if (is_visible) { |
| 384 |
[win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES]; |
| 385 |
} |
| 386 |
[win performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO]; |
| 387 |
textlog->window = NULL; |
| 388 |
textlog->tl_textview = NULL; |
| 389 |
/* Notify everyone that we're gone. */ |
| 390 |
textlog->is_active = false; |
| 391 |
textlog->tl_done = true; |
| 392 |
} |
| 393 |
|
| 394 |
void _al_append_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog) |
| 395 |
{ |
| 396 |
if (textlog->is_active) { |
| 397 |
ALLEGScrollView *scrollView = (NSScrollView *)textlog->tl_textview; |
| 398 |
NSString *text = [NSString stringWithUTF8String: al_cstr(textlog->tl_pending_text)]; |
| 399 |
[scrollView performSelectorOnMainThread:@selector(appendText:) |
| 400 |
withObject:text |
| 401 |
waitUntilDone:NO]; |
| 402 |
|
| 403 |
al_ustr_truncate(textlog->tl_pending_text, 0); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#pragma mark Menus |
| 408 |
/* This class manages the association between windows and menus. |
| 409 |
* OS X has one menu per application, but Allegro has one menu |
| 410 |
* per display (i.e. window) as Windows and Linux tend to do. |
| 411 |
* As a result this class is used to keep track of which menu |
| 412 |
* was assigned to which window. It listens for the notification when |
| 413 |
* a window becomes main and swaps in the corresponding menu. |
| 414 |
*/ |
| 415 |
@interface ALLEGTargetManager : NSObject { |
| 416 |
NSMutableArray * _items; |
| 417 |
} |
| 418 |
+(ALLEGTargetManager*) sharedManager; |
| 419 |
-(id) init; |
| 420 |
-(void) dealloc; |
| 421 |
-(void) setMenu: (NSMenu*) menu forWindow:(NSWindow*) window; |
| 422 |
@end |
| 423 |
|
| 424 |
/* This class represents a menu item target. There is one of these |
| 425 |
* for each NSMenu and it handles all the items in that menu. It |
| 426 |
* maintains the correspondence between ALLEGRO_MENU_ITEMs and |
| 427 |
* NSMenuItems, taking into account the 'App menu' (the one with |
| 428 |
* the app's name in bold) which appears by convention on OS X. |
| 429 |
*/ |
| 430 |
@interface ALLEGMenuTarget : NSObject |
| 431 |
{ |
| 432 |
NSLock* lock; |
| 433 |
ALLEGRO_MENU* amenu; |
| 434 |
BOOL _hasAppMenu; |
| 435 |
NSMenu* _menu; |
| 436 |
} |
| 437 |
-(NSMenu*) menu; |
| 438 |
-(id) initWithMenu:(ALLEGRO_MENU*) amenu; // Designated initializer |
| 439 |
-(NSMenu*) menu; |
| 440 |
-(void) show; |
| 441 |
-(void) showPopup; |
| 442 |
-(void) insertItem:(ALLEGRO_MENU_ITEM*) item atIndex:(int) index; |
| 443 |
-(void) updateItem:(ALLEGRO_MENU_ITEM*) item atIndex: (int) index; |
| 444 |
-(void) destroyItem:(ALLEGRO_MENU_ITEM*) item atIndex: (int) index; |
| 445 |
-(void) activated: (id) sender; |
| 446 |
-(BOOL) validateMenuItem:(NSMenuItem *)menuItem; |
| 447 |
-(void) dealloc; |
| 448 |
+(ALLEGMenuTarget*) targetForMenu:(ALLEGRO_MENU*) amenu; |
| 449 |
@end |
| 450 |
|
| 451 |
/* Take a menu caption. If it has an accelerator char (preceeded by & or _) |
| 452 |
* remove the & or _ from the string and return the char. |
| 453 |
*/ |
| 454 |
static NSString* extract_accelerator(NSMutableString* caption) { |
| 455 |
NSRange range = [caption rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"&_"]]; |
| 456 |
if (range.location != NSNotFound && range.location < [caption length]) { |
| 457 |
[caption deleteCharactersInRange:range]; |
| 458 |
return [caption substringWithRange: range]; |
| 459 |
} else { |
| 460 |
// Not found or you ended the string with & or _ |
| 461 |
return @""; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
bool _al_init_menu(ALLEGRO_MENU *amenu) |
| 466 |
{ |
| 467 |
amenu->extra1 = [[ALLEGMenuTarget alloc] initWithMenu: amenu]; |
| 468 |
return true; |
| 469 |
} |
| 470 |
|
| 471 |
bool _al_init_popup_menu(ALLEGRO_MENU *amenu) |
| 472 |
{ |
| 473 |
amenu->extra1 = [[ALLEGMenuTarget alloc] initWithMenu: amenu]; |
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
bool _al_insert_menu_item_at(ALLEGRO_MENU_ITEM *aitem, int i) |
| 478 |
{ |
| 479 |
ALLEGMenuTarget* mt = [ALLEGMenuTarget targetForMenu: aitem->parent]; |
| 480 |
[mt insertItem: aitem atIndex: i]; |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
bool _al_destroy_menu_item_at(ALLEGRO_MENU_ITEM *aitem, int i) |
| 485 |
{ |
| 486 |
ALLEGMenuTarget* mt = [ALLEGMenuTarget targetForMenu: aitem->parent]; |
| 487 |
[mt destroyItem: aitem atIndex: i]; |
| 488 |
return true;} |
| 489 |
|
| 490 |
bool _al_update_menu_item_at(ALLEGRO_MENU_ITEM *item, int i) |
| 491 |
{ |
| 492 |
ALLEGMenuTarget* mt = [ALLEGMenuTarget targetForMenu: item->parent]; |
| 493 |
[mt updateItem: item atIndex: i]; |
| 494 |
return true; |
| 495 |
} |
| 496 |
|
| 497 |
bool _al_show_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *amenu) |
| 498 |
{ |
| 499 |
ALLEGMenuTarget* target = [ALLEGMenuTarget targetForMenu:amenu]; |
| 500 |
NSWindow* window = al_osx_get_window(display); |
| 501 |
[[ALLEGTargetManager sharedManager] setMenu:target.menu |
| 502 |
forWindow:window]; |
| 503 |
[target performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
bool _al_hide_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu) |
| 508 |
{ |
| 509 |
(void)menu; |
| 510 |
(void)display; |
| 511 |
/* Nowhere to hide on OS X */ |
| 512 |
return false; |
| 513 |
} |
| 514 |
|
| 515 |
bool _al_show_popup_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *amenu) |
| 516 |
{ |
| 517 |
(void) display; |
| 518 |
ALLEGMenuTarget* target = [ALLEGMenuTarget targetForMenu: amenu]; |
| 519 |
[target performSelectorOnMainThread:@selector(showPopup) withObject:nil waitUntilDone:NO]; |
| 520 |
|
| 521 |
return true; |
| 522 |
} |
| 523 |
|
| 524 |
int _al_get_menu_display_height(void) |
| 525 |
{ |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
@implementation ALLEGMenuTarget |
| 530 |
/* Initial conversion of ALLEGRO_MENU_ITEM to NSMenuItem. |
| 531 |
* The target (self) is set for the item. |
| 532 |
* The checkbox state is not set here, it's done dynamically in -validateMenuItem. |
| 533 |
*/ |
| 534 |
-(NSMenuItem*) buildMenuItemFor:(ALLEGRO_MENU_ITEM*) aitem |
| 535 |
{ |
| 536 |
NSMenuItem* item; |
| 537 |
if (aitem->caption && al_ustr_length(aitem->caption) > 0) { |
| 538 |
NSMutableString* title = [NSMutableString stringWithUTF8String:al_cstr(aitem->caption)]; |
| 539 |
NSString* key = extract_accelerator(title); |
| 540 |
item = [[NSMenuItem alloc] initWithTitle:title |
| 541 |
action:@selector(activated:) |
| 542 |
keyEquivalent:key]; |
| 543 |
[item setTarget:self]; |
| 544 |
aitem->extra1 = item; |
| 545 |
return item; |
| 546 |
} else { |
| 547 |
item = [NSMenuItem separatorItem]; |
| 548 |
} |
| 549 |
return item; |
| 550 |
} |
| 551 |
// Insert the app menu if it's not there already |
| 552 |
-(void) insertAppMenu |
| 553 |
{ |
| 554 |
if (!self->_hasAppMenu) { |
| 555 |
NSMenuItem* apple = [[NSMenuItem alloc] init]; |
| 556 |
[apple setTitle:@"Apple"]; |
| 557 |
|
| 558 |
NSMenu* appleItems = [[NSMenu alloc] init]; |
| 559 |
[appleItems setTitle:@"Apple"]; |
| 560 |
[apple setSubmenu:appleItems]; |
| 561 |
[appleItems addItemWithTitle:@"Hide" action:@selector(hide:) keyEquivalent:@"h"]; |
| 562 |
[appleItems addItemWithTitle:@"Hide others" action:@selector(hideOtherApplications:) keyEquivalent:@""]; |
| 563 |
[appleItems addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; |
| 564 |
[appleItems addItem:[NSMenuItem separatorItem]]; |
| 565 |
[appleItems addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"]; |
| 566 |
[self.menu insertItem:apple atIndex:0]; |
| 567 |
[apple release]; |
| 568 |
self->_hasAppMenu = YES; |
| 569 |
} |
| 570 |
} |
| 571 |
// Get the NSMenuItem corresponding to this ALLEGRO_MENU_ITEM |
| 572 |
- (NSMenuItem*) itemForAllegroItem:(ALLEGRO_MENU_ITEM*) aitem |
| 573 |
{ |
| 574 |
int index = _al_vector_find(&self->amenu->items, &aitem); |
| 575 |
if (index >= 0) { |
| 576 |
/* If the app menu is showing it will be at index 0 so account for this */ |
| 577 |
if (self->_hasAppMenu) { |
| 578 |
++index; |
| 579 |
} |
| 580 |
return [self.menu itemAtIndex:index]; |
| 581 |
} else { |
| 582 |
return nil; |
| 583 |
} |
| 584 |
} |
| 585 |
// Get the ALLEGRO_MENU_ITEM corresponding to this NSMenuItem |
| 586 |
- (ALLEGRO_MENU_ITEM*) allegroItemforItem: (NSMenuItem*) mi |
| 587 |
{ |
| 588 |
int i; |
| 589 |
ALLEGRO_MENU_ITEM * ami; |
| 590 |
|
| 591 |
for (i = 0; i < (int)_al_vector_size(&self->amenu->items); i++) { |
| 592 |
ami = *(ALLEGRO_MENU_ITEM**) _al_vector_ref(&self->amenu->items, i); |
| 593 |
if (ami->extra1 == mi) { |
| 594 |
return ami; |
| 595 |
} |
| 596 |
} |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
// Create target with ALLEGRO_MENU bound to it. |
| 600 |
- (id)initWithMenu:(ALLEGRO_MENU*) source_menu |
| 601 |
{ |
| 602 |
self = [super init]; |
| 603 |
if (self) { |
| 604 |
self->_hasAppMenu = NO; |
| 605 |
self->lock = [[NSLock alloc] init]; |
| 606 |
self->amenu = source_menu; |
| 607 |
self->_menu = [[NSMenu alloc] init]; |
| 608 |
} |
| 609 |
return self; |
| 610 |
} |
| 611 |
-(id) init |
| 612 |
{ |
| 613 |
/* This isn't a valid initializer */ |
| 614 |
return nil; |
| 615 |
} |
| 616 |
// Manage the enabled and checked state (done dynamically by the framework) |
| 617 |
-(BOOL) validateMenuItem:(NSMenuItem *)menuItem { |
| 618 |
ALLEGRO_MENU_ITEM* aitem = [self allegroItemforItem:menuItem]; |
| 619 |
if (aitem) { |
| 620 |
int checked = (aitem->flags & (ALLEGRO_MENU_ITEM_CHECKBOX|ALLEGRO_MENU_ITEM_CHECKED)) == (ALLEGRO_MENU_ITEM_CHECKBOX|ALLEGRO_MENU_ITEM_CHECKED); |
| 621 |
[menuItem setState: checked ? NSOnState : NSOffState ]; |
| 622 |
return aitem->flags & ALLEGRO_MENU_ITEM_DISABLED ? NO : YES; |
| 623 |
} |
| 624 |
return YES; |
| 625 |
} |
| 626 |
- (void)dealloc |
| 627 |
{ |
| 628 |
[self->lock release]; |
| 629 |
[self->_menu release]; |
| 630 |
[super dealloc]; |
| 631 |
} |
| 632 |
// Get the menu |
| 633 |
-(NSMenu*) menu |
| 634 |
{ |
| 635 |
return self->_menu; |
| 636 |
} |
| 637 |
// Action event when the menu is selected |
| 638 |
-(void) activated:(id)sender { |
| 639 |
NSMenuItem* mitem = (NSMenuItem*) sender; |
| 640 |
ALLEGRO_MENU_ITEM* aitim = [self allegroItemforItem:mitem]; |
| 641 |
if (aitim) { |
| 642 |
if (aitim->flags & ALLEGRO_MENU_ITEM_CHECKBOX) { |
| 643 |
aitim->flags ^= ALLEGRO_MENU_ITEM_CHECKED; |
| 644 |
} |
| 645 |
_al_emit_menu_event(aitim->parent->display, aitim->unique_id); |
| 646 |
} |
| 647 |
} |
| 648 |
// Insert an item, keep the NSMenu in sync |
| 649 |
-(void) insertItem:(ALLEGRO_MENU_ITEM*) aitem atIndex: (int) index |
| 650 |
{ |
| 651 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 652 |
NSMenuItem* item = [self buildMenuItemFor:aitem]; |
| 653 |
[self.menu insertItem:item atIndex:index]; |
| 654 |
if (aitem->popup) { |
| 655 |
ALLEGMenuTarget* sub = [ALLEGMenuTarget targetForMenu:aitem->popup]; |
| 656 |
[[sub menu] setTitle:[item title]]; |
| 657 |
[item setAction:nil]; |
| 658 |
[item setSubmenu:[sub menu]]; |
| 659 |
} |
| 660 |
[pool release]; |
| 661 |
} |
| 662 |
// Update an item (caption only, see -validateMenuItem: ) |
| 663 |
-(void) updateItem:(ALLEGRO_MENU_ITEM *)aitem atIndex:(int)index |
| 664 |
{ |
| 665 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 666 |
(void) index; |
| 667 |
NSMenuItem* item = [self itemForAllegroItem:aitem]; |
| 668 |
NSMutableString* caption = [NSMutableString stringWithUTF8String:al_cstr(aitem->caption)]; |
| 669 |
NSString* key = extract_accelerator(caption); |
| 670 |
[item setTitle:caption]; |
| 671 |
[item setKeyEquivalent:key]; |
| 672 |
if (aitem->popup) { |
| 673 |
[item setEnabled:(aitem->flags & ALLEGRO_MENU_ITEM_DISABLED) ? NO : YES]; |
| 674 |
} |
| 675 |
[pool release]; |
| 676 |
} |
| 677 |
// Remove an item, keep the NSMenu in sync |
| 678 |
-(void) destroyItem:(ALLEGRO_MENU_ITEM *)aitem atIndex:(int)index |
| 679 |
{ |
| 680 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 681 |
(void) index; |
| 682 |
NSMenuItem* item = [self itemForAllegroItem:aitem]; |
| 683 |
[item.menu removeItem:item]; |
| 684 |
[pool release]; |
| 685 |
} |
| 686 |
// Show the menu on the main application menu bar |
| 687 |
-(void) show |
| 688 |
{ |
| 689 |
[self insertAppMenu]; |
| 690 |
[NSApp setMainMenu:self.menu]; |
| 691 |
} |
| 692 |
// Show the menu as a pop-up on the current key window. |
| 693 |
-(void) showPopup |
| 694 |
{ |
| 695 |
NSWindow *window = [NSApp keyWindow]; |
| 696 |
NSPoint mouseLocation = [NSEvent mouseLocation]; |
| 697 |
NSPoint locationInWindow = [window convertScreenToBase: mouseLocation]; |
| 698 |
int eventType = NSLeftMouseDown; |
| 699 |
int winnum = [window windowNumber]; |
| 700 |
NSEvent *fakeMouseEvent = [NSEvent mouseEventWithType:eventType |
| 701 |
location:locationInWindow |
| 702 |
modifierFlags:0 |
| 703 |
timestamp:0 |
| 704 |
windowNumber:winnum |
| 705 |
context:nil |
| 706 |
eventNumber:0 |
| 707 |
clickCount:0 |
| 708 |
pressure:0]; |
| 709 |
|
| 710 |
[NSMenu popUpContextMenu:self.menu withEvent:fakeMouseEvent forView:[window contentView]]; |
| 711 |
} |
| 712 |
// Find the target associated with this ALLEGRO_MENU |
| 713 |
+(ALLEGMenuTarget*) targetForMenu:(ALLEGRO_MENU *)amenu { |
| 714 |
if (!amenu->extra1) { |
| 715 |
amenu->extra1 = [[ALLEGMenuTarget alloc] initWithMenu:amenu]; |
| 716 |
} |
| 717 |
return amenu->extra1; |
| 718 |
} |
| 719 |
@end // ALLEGMenuTarget |
| 720 |
|
| 721 |
static ALLEGTargetManager* _sharedmanager = nil; |
| 722 |
@implementation ALLEGTargetManager |
| 723 |
// Get the singleton manager object |
| 724 |
+(ALLEGTargetManager*) sharedManager |
| 725 |
{ |
| 726 |
if (!_sharedmanager) { |
| 727 |
_sharedmanager = [[ALLEGTargetManager alloc] init]; |
| 728 |
} |
| 729 |
return _sharedmanager; |
| 730 |
} |
| 731 |
// Set up and register for notifications |
| 732 |
-(id) init |
| 733 |
{ |
| 734 |
self = [super init]; |
| 735 |
if (self) { |
| 736 |
self->_items = [[NSMutableArray alloc] init]; |
| 737 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWindowChange:) name:NSWindowDidBecomeMainNotification object:nil]; |
| 738 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWindowClose:) name:NSWindowWillCloseNotification object:nil]; |
| 739 |
|
| 740 |
} |
| 741 |
return self; |
| 742 |
} |
| 743 |
// Destroy and un-register |
| 744 |
-(void) dealloc |
| 745 |
{ |
| 746 |
[self->_items release]; |
| 747 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 748 |
[super dealloc]; |
| 749 |
} |
| 750 |
// Find the index for a window which this object is managing or NSNotFound |
| 751 |
-(NSUInteger) indexForWindow:(NSWindow*) window |
| 752 |
{ |
| 753 |
NSUInteger i; |
| 754 |
for (i=0; i<[self->_items count]; ++i) { |
| 755 |
NSDictionary* entry = [self->_items objectAtIndex:i]; |
| 756 |
if ([window isEqual:[entry valueForKey:@"window"]]) { |
| 757 |
return i; |
| 758 |
} |
| 759 |
} |
| 760 |
return NSNotFound; |
| 761 |
} |
| 762 |
// Event when main window changes - locate the matching menu and show it |
| 763 |
-(void) onWindowChange: (NSNotification*) notification |
| 764 |
{ |
| 765 |
NSWindow* window = [notification object]; |
| 766 |
NSUInteger index = [self indexForWindow: window]; |
| 767 |
if (index != NSNotFound) { |
| 768 |
NSDictionary* entry = [self->_items objectAtIndex:index]; |
| 769 |
NSMenu* menu = [entry valueForKey:@"menu"]; |
| 770 |
[NSApp setMainMenu:menu]; |
| 771 |
} |
| 772 |
/* If not found we do nothing */ |
| 773 |
} |
| 774 |
// Event when window closes, remove it from the managed list |
| 775 |
-(void) onWindowClose: (NSNotification*) notification |
| 776 |
{ |
| 777 |
NSWindow* window = [notification object]; |
| 778 |
NSUInteger index = [self indexForWindow: window]; |
| 779 |
if (index != NSNotFound) { |
| 780 |
[self->_items removeObjectAtIndex:index]; |
| 781 |
} |
| 782 |
/* If not found we do nothing */ |
| 783 |
} |
| 784 |
// Link a menu with a window, replace any existing link |
| 785 |
-(void) setMenu:(NSMenu *)menu forWindow:(NSWindow *)window |
| 786 |
{ |
| 787 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 788 |
NSUInteger index = [self indexForWindow:window]; |
| 789 |
if (menu) { |
| 790 |
NSDictionary* newentry = [NSDictionary dictionaryWithObjectsAndKeys:menu, @"menu", window, @"window", nil]; |
| 791 |
if (index == NSNotFound) { |
| 792 |
[self->_items addObject: newentry]; |
| 793 |
} |
| 794 |
else { |
| 795 |
[self->_items replaceObjectAtIndex:index withObject: newentry]; |
| 796 |
} |
| 797 |
} else { |
| 798 |
/* Menu was null so delete */ |
| 799 |
if (index != NSNotFound) { |
| 800 |
[self->_items removeObjectAtIndex:index]; |
| 801 |
} |
| 802 |
} |
| 803 |
[pool release]; |
| 804 |
} |
| 805 |
@end // ALLEGTargetManager |