Re: Checking if a menu is visible??!?!?
Re: Checking if a menu is visible??!?!?
- Subject: Re: Checking if a menu is visible??!?!?
- From: Rosyna <email@hidden>
- Date: Tue, 2 Mar 2004 17:13:42 -0700
Ack, at 3/3/04, Peter Maurer said:
_NSGetCarbonMenu will return NULL if the menu is not visible, has
never been visible and is not attached to the menu bar. Cocoa seems
to not make the menu into a real menu item until it is made visible.
True. But the menuReference method I have attached in my first reply
deals with this case -- not too elegant, but it works.
And it will cause an exception if the menu you are testing is a
submenu of a menu not in the menu bar. (That is, if it has a parent
menu). You'd need to make sure that -[self supermenu] is NULL.
And standard warning:
This API is private and may be removed in future OS X versions. I
sooo hope it never does though.
Me too! (Thanks for pointing this out!)
I've spent WAY too much time getting my code to work to have this
thing be removed....
And I recommend (just in case this ever happens) to dynamically load
the symbol "__NSGetCarbonMenu". So you don't strong link to the
symbol and the app doesn't crash on launch if it is removed.
typedef MenuRef (*NSGetCarbonMenuPtr)(NSMenu* menu);
static NSGetCarbonMenuPtr NSGetCarbonMenu=NULL;
if (!NSGetCarbonMenu and NSIsSymbolNameDefined("__NSGetCarbonMenu"))
NSGetCarbonMenu=NSAddressOfSymbol(NSLookupAndBindSymbol("__NSGetCarbonMenu"));
But then you'd have to make sure that NSGetCarbonMenu is not NULL
before each call.
Sigh, I've been doing this for too long.
Peter.
GetMenuTrackingData will return noErr and a non-NULL menu if no
menu is visible but the title of the menu is hilighted. You might
want to make sure theMenuTrackingData.menu and
theMenuTrackingData.menuItemIndex are not NULL (or 0, Carbon
indexes start at 1)
Ack, at 3/2/04, Peter Maurer said:
but I cannot find a way to check if a NSMenu is visible or not..
With a little help from carbon, this should be possible (at least
for OS X versions whose NSMenus are based on carbon menus). First,
get the NSMenu's menu reference via _NSGetCarbonMenu(NSMenu *) --
I have a NSMenu category that contains the following menuReference
method...
extern MenuRef _NSGetCarbonMenu(NSMenu *);
- (MenuRef) menuReference {
MenuRef theMenuReference = _NSGetCarbonMenu(self);
if (theMenuReference==0) {
// this is necessary to make cocoa actually create
the underlying carbon menu
NSMenu *theMainMenu = [NSApp mainMenu];
NSMenuItem *theDummyMenuItem = [theMainMenu
addItemWithTitle: @"sub" action: NULL keyEquivalent: @""];
[theDummyMenuItem setSubmenu: self];
[theDummyMenuItem setSubmenu: nil];
[theMainMenu removeItem: theDummyMenuItem];
return _NSGetCarbonMenu(self);
}
return theMenuReference;
}
Then check for visibility...
NSMenu *theNSMenuInQuestion; // assume this is the menu you want to test
MenuTrackingData theMenuTrackingData;
if (GetMenuTrackingData([theNSMenuInQuestion menuReference],
&theMenuTrackingData)==noErr) {
// the menu is visible
}
Does this help?
--
Sincerely,
Rosyna Keller
Technical Support/Holy Knight/Always needs a hug
Unsanity: Unsane Tools for Insanely Great People
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.