Re: Dynamic Menu Items
Re: Dynamic Menu Items
- Subject: Re: Dynamic Menu Items
- From: Dustin Voss <email@hidden>
- Date: Mon, 16 Sep 2002 19:04:22 -0700
On Monday, September 16, 2002, at 04:06 PM, Rob Petrovec wrote:
>
Hey,
>
I have a menu that I am creating on the fly. How do I
>
programmatically
>
make one of my menu items dynamic?
>
>
Meaning, when the menu is displayed and the user hits the Option
>
key
>
(for example), menu items set to be dynamic will automatically change
>
to
>
their alternate states. Like the File Menu in the X Finder does when
>
you
>
hold down the option key.
>
>
I have done a ton of searches through the docs and sample code and
>
can't
>
seem to find any info other the docs saying it can be done but no info
>
on
>
how to actually do it.
>
>
Thanks for any help you can give...
>
>
--Rob
>
_______________________________________________
>
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.
The best source of info on this is down towards the bottom of Menus.h.
There's a brief discussion of dynamic menu items.
First, in your NIB file, list all alternates in order:
Paste bV
Paste Asb& bb%V
Second, after the app starts up, use this undocumented API to get the
Carbon menu reference to the menu:
MenuRef _NSGetCarbonMenu(NSMenu *);
Third, for each of the dynamic items (bPasteb and bPaste Asb&b in
this
example), call this API:
err = ChangeMenuItemAttributes (menuref, itemnumber,
kMenuItemAttrDynamic, 0);
Note that Carbon menu item numbers start at 1 for the first item, where
Cocoa's menu item numbers start at 0. Also, this will only work after
NSApp's applicationDidFinishLaunching gets called.
I have written a category on NSMenuItem that does this, based on
OmniAppKit code and something from cocoa-dev. Here is the
implementation:
#import "NSMenuItemExtension.h"
#import <Carbon/Carbon.h>
@implementation NSMenuItem (NSMenuItemExtension)
// Must be called after NSApp delegate's applicationDidFinishLaunching.
- (void)setDynamic:(BOOL)aFlag
{
OSStatus err;
extern MenuRef _NSGetCarbonMenu(NSMenu *); // If this ever becomes
obsolete, can use Carbon's AcquireRootMenu and work my way to the item.
NSMenu *cocoaMenu = [self menu];
int item = [cocoaMenu indexOfItemWithTag:[self tag]] + 1;
MenuRef carbonMenu = _NSGetCarbonMenu (cocoaMenu);
err = ChangeMenuItemAttributes (carbonMenu, item,
(aFlag ? kMenuItemAttrDynamic : 0),
(aFlag ? 0 : kMenuItemAttrDynamic) );
NSAssert2 (!err, @"Couldn't set dynamic attribute of menu item %@ (err
%d).", [self title], err);
}
- (BOOL)isDynamic
{
OSStatus err;
extern MenuRef _NSGetCarbonMenu(NSMenu *); // If this ever becomes
obsolete, can use Carbon's AcquireRootMenu and work my way to the item.
NSMenu *cocoaMenu = [self menu];
int item = [cocoaMenu indexOfItemWithTag:[self tag]] + 1;
MenuRef carbonMenu = _NSGetCarbonMenu (cocoaMenu);
MenuItemAttributes attr;
err = GetMenuItemAttributes (carbonMenu, item, &attr);
NSAssert2 (!err, @"Couldn't get attributes of menu item %@ (err %d).",
[self title], err);
return (attr & kMenuItemAttrDynamic);
}
@end
_______________________________________________
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.