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 05:38:45 +0000
I regret to report that the menuNeedsUpdate delegate method for the
submenu in question is called before
_prepareForOpeningOfOpenRecentMenu, so it can't be used for this
particular purpose.
Best wishes,
Hamish
On Mar 17, 2005, at 1:33, Charilaos Skiadas wrote:
I am not sure if this would work for what you are trying to do, but
maybe you could set yourself up as the menu's delegate, and then make
the necessary changes when the menu needs to be updated through
menuNeedsUpdate:
I was not messing around with the Open Recent submenu, so I am not
sure what you would do with that, but, I did want to populate a menu
dynamically, and assign a shortcut to its first object. The NSMenu
delegate methods are all that I needed.
The question is who populates the menu in the first place, presumably
its delegate, maybe that actually is NSDocumentController? In that
case, you could try subclassing it and overriding menuNeedsUpdate,
where you would first call super's implementation of menuNeedsUpdate
to populate the menu, and then you would mess with it as you like.
Not sure if this would help, just an idea. Let me know if it does, I'd
probably like to implement it myself at some point.
HTH,
Haris
On Mar 16, 2005, at 6:15 PM, Hamish Allan wrote:
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:
email@hidden
This email sent to email@hidden
_______________________________________________
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