diff options
Diffstat (limited to 'src/macos.m')
-rw-r--r-- | src/macos.m | 77 |
1 files changed, 62 insertions, 15 deletions
diff --git a/src/macos.m b/src/macos.m index c8793eac..4e214923 100644 --- a/src/macos.m +++ b/src/macos.m | |||
@@ -367,40 +367,86 @@ void enableMenu_MacOS(const char *menuLabel, iBool enable) { | |||
367 | [label release]; | 367 | [label release]; |
368 | } | 368 | } |
369 | 369 | ||
370 | static void setShortcut_NSMenuItem_(NSMenuItem *item, int key, int kmods) { | 370 | void enableMenuItem_MacOS(const char *menuItemCommand, iBool enable) { |
371 | iString str; | 371 | NSApplication *app = [NSApplication sharedApplication]; |
372 | init_String(&str); | 372 | NSMenu *appMenu = [app mainMenu]; |
373 | MyDelegate *myDel = (MyDelegate *) app.delegate; | ||
374 | for (NSMenuItem *mainMenuItem in appMenu.itemArray) { | ||
375 | NSMenu *menu = mainMenuItem.submenu; | ||
376 | if (menu) { | ||
377 | for (NSMenuItem *menuItem in menu.itemArray) { | ||
378 | NSString *command = [myDel commandForItem:menuItem]; | ||
379 | if (command) { | ||
380 | if (!iCmpStr([command cStringUsingEncoding:NSUTF8StringEncoding], | ||
381 | menuItemCommand)) { | ||
382 | [menuItem setEnabled:enable]; | ||
383 | return; | ||
384 | } | ||
385 | } | ||
386 | } | ||
387 | } | ||
388 | } | ||
389 | } | ||
390 | |||
391 | static iString *composeKeyEquivalent_(int key, int kmods, NSEventModifierFlags *modMask) { | ||
392 | iString *str = new_String(); | ||
373 | if (key == SDLK_LEFT) { | 393 | if (key == SDLK_LEFT) { |
374 | appendChar_String(&str, 0x2190); | 394 | appendChar_String(str, 0x2190); |
375 | } | 395 | } |
376 | else if (key == SDLK_RIGHT) { | 396 | else if (key == SDLK_RIGHT) { |
377 | appendChar_String(&str, 0x2192); | 397 | appendChar_String(str, 0x2192); |
378 | } | 398 | } |
379 | else if (key == SDLK_UP) { | 399 | else if (key == SDLK_UP) { |
380 | appendChar_String(&str, 0x2191); | 400 | appendChar_String(str, 0x2191); |
381 | } | 401 | } |
382 | else if (key == SDLK_DOWN) { | 402 | else if (key == SDLK_DOWN) { |
383 | appendChar_String(&str, 0x2193); | 403 | appendChar_String(str, 0x2193); |
384 | } | 404 | } |
385 | else if (key) { | 405 | else if (key) { |
386 | appendChar_String(&str, key); | 406 | appendChar_String(str, key); |
387 | } | 407 | } |
388 | NSEventModifierFlags modMask = 0; | 408 | *modMask = 0; |
389 | if (kmods & KMOD_GUI) { | 409 | if (kmods & KMOD_GUI) { |
390 | modMask |= NSEventModifierFlagCommand; | 410 | *modMask |= NSEventModifierFlagCommand; |
391 | } | 411 | } |
392 | if (kmods & KMOD_ALT) { | 412 | if (kmods & KMOD_ALT) { |
393 | modMask |= NSEventModifierFlagOption; | 413 | *modMask |= NSEventModifierFlagOption; |
394 | } | 414 | } |
395 | if (kmods & KMOD_CTRL) { | 415 | if (kmods & KMOD_CTRL) { |
396 | modMask |= NSEventModifierFlagControl; | 416 | *modMask |= NSEventModifierFlagControl; |
397 | } | 417 | } |
398 | if (kmods & KMOD_SHIFT) { | 418 | if (kmods & KMOD_SHIFT) { |
399 | modMask |= NSEventModifierFlagShift; | 419 | *modMask |= NSEventModifierFlagShift; |
400 | } | 420 | } |
421 | return str; | ||
422 | } | ||
423 | |||
424 | void enableMenuItemsByKey_MacOS(int key, int kmods, iBool enable) { | ||
425 | NSApplication *app = [NSApplication sharedApplication]; | ||
426 | NSMenu *appMenu = [app mainMenu]; | ||
427 | NSEventModifierFlags modMask; | ||
428 | iString *keyEquiv = composeKeyEquivalent_(key, kmods, &modMask); | ||
429 | for (NSMenuItem *mainMenuItem in appMenu.itemArray) { | ||
430 | NSMenu *menu = mainMenuItem.submenu; | ||
431 | if (menu) { | ||
432 | for (NSMenuItem *menuItem in menu.itemArray) { | ||
433 | if (menuItem.keyEquivalentModifierMask == modMask && | ||
434 | !iCmpStr([menuItem.keyEquivalent cStringUsingEncoding:NSUTF8StringEncoding], | ||
435 | cstr_String(keyEquiv))) { | ||
436 | [menuItem setEnabled:enable]; | ||
437 | } | ||
438 | } | ||
439 | } | ||
440 | } | ||
441 | delete_String(keyEquiv); | ||
442 | } | ||
443 | |||
444 | static void setShortcut_NSMenuItem_(NSMenuItem *item, int key, int kmods) { | ||
445 | NSEventModifierFlags modMask; | ||
446 | iString *str = composeKeyEquivalent_(key, kmods, &modMask); | ||
401 | [item setKeyEquivalentModifierMask:modMask]; | 447 | [item setKeyEquivalentModifierMask:modMask]; |
402 | [item setKeyEquivalent:[NSString stringWithUTF8String:cstr_String(&str)]]; | 448 | [item setKeyEquivalent:[NSString stringWithUTF8String:cstr_String(str)]]; |
403 | deinit_String(&str); | 449 | delete_String(str); |
404 | } | 450 | } |
405 | 451 | ||
406 | void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem *items, size_t count) { | 452 | void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem *items, size_t count) { |
@@ -412,6 +458,7 @@ void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem * | |||
412 | keyEquivalent:@"" | 458 | keyEquivalent:@"" |
413 | atIndex:atIndex]; | 459 | atIndex:atIndex]; |
414 | NSMenu *menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:menuLabel]]; | 460 | NSMenu *menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:menuLabel]]; |
461 | [menu setAutoenablesItems:NO]; | ||
415 | for (size_t i = 0; i < count; ++i) { | 462 | for (size_t i = 0; i < count; ++i) { |
416 | const char *label = items[i].label; | 463 | const char *label = items[i].label; |
417 | if (label[0] == '\r') { | 464 | if (label[0] == '\r') { |