3737
3838static uint32_t s_menu_select_lock = 0 ;
3939static bool s_menu_select_occured = false ;
40+ static bool s_quit_item_has_accelerator = false ;
4041
4142// //////////////////////////////////////////////////////////////////////////////
4243
5354 // is the case, some items will be hidden and a (API-wise) invisible
5455 // menu will be inserted at the front (the application menu).
5556 bool is_menubar : 1 ;
57+
58+ // If the quit item in this menu has an accelerator, this is true.
59+ // (Cocoa seems to 'hide' the quit menu item accelerator for some inexplicable
60+ // reason - it returns 'empty').
61+ bool quit_has_accelerator : 1 ;
5662};
5763
5864// //////////////////////////////////////////////////////////////////////////////
@@ -154,10 +160,21 @@ - (void)menuNeedsUpdate: (NSMenu *)menu
154160
155161- (void )menuItemSelected : (id )sender
156162{
157- if (s_menu_select_lock == 0 )
163+ NSMenuItem *t_item;
164+ t_item = (NSMenuItem *)sender;
165+
166+ // MW-2014-10-22: [[ Bug 13510 ]] As Cocoa hides Cmd-Q for some reason as a key equivalent
167+ // we mark the menu as having an accelerator for quit if one was specified. If no accelerator
168+ // was specified, we handle Cmd-Q as if it weren't an accelerator but was a select.
169+ // (This is for the case where the 'Exit' menu item has no accelerator, but Cocoa requires
170+ // said accelerator for conformance).
171+ bool t_item_is_quit_without_accelerator;
172+ t_item_is_quit_without_accelerator = false ;
173+ if ([[t_item representedObject ] isEqualToString: @" Quit" ])
174+ t_item_is_quit_without_accelerator = ![(com_runrev_livecode_MCMenuDelegate *)[[t_item menu ] delegate ] platformMenuRef ] -> quit_has_accelerator;
175+
176+ if (s_menu_select_lock == 0 || t_item_is_quit_without_accelerator)
158177 {
159- NSMenuItem *t_item;
160- t_item = (NSMenuItem *)sender;
161178 MCPlatformCallbackSendMenuSelect (m_menu, [[t_item menu ] indexOfItem: t_item]);
162179 }
163180 else
@@ -179,6 +196,8 @@ - (BOOL)worksWhenModal
179196
180197- (BOOL )menuHasKeyEquivalent : (NSMenu *)menu forEvent : (NSEvent *)event target : (id *)target action : (SEL *)action
181198{
199+ // MW-2014-10-22: [[ Bug 13510 ]] Make sure we update menus before searching for accelerators.
200+ [[menu delegate ] menuNeedsUpdate: menu];
182201 return NO ;
183202}
184203
@@ -236,12 +255,25 @@ - (void)preferencesMenuItemSelected: (id)sender
236255 [self shadowedMenuItemSelected: @" Preferences" ];
237256}
238257
258+ - (void )quitMenuItemSelected : (id )sender
259+ {
260+ [self shadowedMenuItemSelected: @" Quit" ];
261+ }
262+
239263- (void )menuNeedsUpdate : (NSMenu *)menu
240264{
241- NSMenuItem *t_prefs, *t_about;
265+ NSMenuItem *t_prefs, *t_about, *t_quit ;
242266 t_prefs = [self findShadowedMenuItem: @" Preferences" ];
243267 t_about = [self findShadowedMenuItem: @" About" ];
268+ t_quit = [self findShadowedMenuItem: @" Quit" ];
244269
270+ if (t_quit != nil )
271+ {
272+ [[[t_quit menu ] delegate ] menuNeedsUpdate: [t_prefs menu ]];
273+ t_quit = [self findShadowedMenuItem: @" Quit" ];
274+ [[menu itemAtIndex: 10 ] setEnabled: t_quit != nil ? [t_quit isEnabled ] : NO ];
275+ }
276+
245277 if (t_prefs != nil )
246278 {
247279 [[[t_prefs menu ] delegate ] menuNeedsUpdate: [t_prefs menu ]];
@@ -597,6 +629,9 @@ void MCPlatformSetMenuItemProperty(MCPlatformMenuRef p_menu, uindex_t p_index, M
597629 [t_item setKeyEquivalent: @" " ];
598630 [t_item setKeyEquivalentModifierMask: 0 ];
599631 }
632+
633+ if ([[t_item representedObject ] isEqualToString: @" Quit" ])
634+ p_menu -> quit_has_accelerator = t_accelerator != 0 ;
600635 }
601636 break ;
602637 case kMCPlatformMenuItemPropertyEnabled :
@@ -748,8 +783,9 @@ static void MCPlatformStartUsingMenuAsMenubar(MCPlatformMenuRef p_menu)
748783 keyEquivalent: @" " ];
749784 [t_app_menu addItem: [NSMenuItem separatorItem ]];
750785 [t_app_menu addItemWithTitle: [NSString stringWithFormat: NSLocalizedString(@" Quit %@ " , @" Quit {Application Name}" ), t_app_name]
751- action: @selector (terminate : )
786+ action: @selector (quitMenuItemSelected : )
752787 keyEquivalent: @" q" ];
788+ [[t_app_menu itemAtIndex: 10 ] setTarget: s_app_menu_delegate];
753789 [t_app_menu setDelegate: s_app_menu_delegate];
754790
755791 NSMenuItem *t_app_menu_item;
0 commit comments