Re: When is the 'Open Recent' submenu populated?
Re: When is the 'Open Recent' submenu populated?
- Subject: Re: When is the 'Open Recent' submenu populated?
- From: Hamish Allan <email@hidden>
- Date: Thu, 17 Mar 2005 00:15:44 +0000
This didn't work, and I also discovered that the "Open Recent" submenu
is populated on the fly, i.e., even if I change its contents, the next
time I look at it, it's back to normal.
After a bit of further digging with symbolic breakpoints, I overrode
the following private API in NSDocumentController:
- (void)_prepareForOpeningOfOpenRecentMenu:(id)sender
{
[super _prepareForOpeningOfOpenRecentMenu:sender];
int numRecent = [sender numberOfItems] - 2;
if (numRecent > 9)
numRecent = 9;
int i;
for (i = 0; i < numRecent; ++i)
{
NSMenuItem *menuItem = [sender itemAtIndex:i];
[menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[menuItem setKeyEquivalent:[NSString stringWithFormat:@"%d", i + 1]];
}
}
In order to make these keyboard shortcuts available immediately after
launching the application, I also wrote:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSMenu *fileMenu = [[[NSApp mainMenu] itemWithTitle:@"File"] submenu];
NSMenu *recentItems = [[fileMenu itemWithTitle:@"Open Recent"]
submenu];
[self _prepareForOpeningOfOpenRecentMenu:recentItems];
}
I don't like using private APIs, of course, not least of all because I
now get compiler warnings about NSDocumentController maybe not
responding to _prepareForOpeningOfOpenRecentMenu: (can anyone tell me
how to get rid of this warning?). But there doesn't seem to be anything
public all the way up the call stack, although in the debugger it
appears as though _prepareForOpeningOfOpenRecentMenu: is being called
by -[NSDocumentController saveAllDocuments:] (seems unlikely, and
indeed setting a breakpoint there yields no fruit. Is this a bug in
gdb/XCode?)
Thanks,
Hamish
On Mar 16, 2005, at 19:50, John C. Randolph wrote:
On Mar 16, 2005, at 10:11 AM, Hamish Allan wrote:
Hi,
I'm looking to write code to assign key equivalents to items in 'Open
Recent' in a document-based app (e.g., Cmd-1 for most recent, Cmd-2
for secondmost, etc.). However, apparently this submenu isn't yet
populated when applicationDidFinishLaunching: is called. Can anybody
suggest a better delegate method to use?
I would try something like this in your delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self performSelector:@selector(checkRecentsMenu) withObject:nil
afterDelay:0];
}
- (void) checkRecentsMenu
{
// see if the menu has been populated when you get here...
}
This way, the app has not only been launched, it's also been through
the event loop once when you get to the -checkRecentsMenu method.
HTH,
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden