views:

41

answers:

1

How does one react to menuitems that are clicked via mouse or invoked via keyboard, e.g: CMD+Q ?

[NSApp beginSheet:my_sheet  ...arguments... ];

/*  
The sheet is now shown and the mainmenu isn't usable.
How does one make it usable?
*/

[NSApp endSheet:my_sheet returnCode:0];
+1  A: 

I assume you're using NSApp's -beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: method. If so, then you already should be able to do menu commands. That method runs the sheet modal for the given window only (as opposed to modal for the entire app). However, if you also call NSApp's -runModalForWindow:, then it will be modal for the entire app. So, assuming you're not calling that, then other windows and menu commands should work fine when the sheet is showing.

However, the 'Quit' menu item is one exception. It won't let you quit because there is a modal session active for a window, which it assumes needs to be dealt with before the app can quit. If this is what you're really trying to do, then one possible solution is to subclass NSApplication and override its -terminate: method to first close your sheet (if it's open). First, you need to make a subclass of NSApplication, and have your app use it by setting it both in 'MainMenu.xib' and as the Principal Class in your 'Info.plist'). Then add something like this to your subclass:

- (void)terminate:(id)sender
{
    // First close the sheet (if it's active), using whatever method
    // you have set up to do this...
    [((MyAppDelegate*)[self delegate]) closeSheet:self];

    // Now call the normal implementation
    [super terminate:sender];
}
Kelan