Programming Context Menu
Programming Context Menu
- Subject: Programming Context Menu
- From: Bing Li <email@hidden>
- Date: Tue, 05 Apr 2011 03:38:20 +0800
Dear all,
I am now learning programming on Cocoa following the book, Cocoa Programming
Developers Handbook.
When programming Context Menus (Page 237), I got a problem. Although the
context menu can be shown, it is weird that all of the items in the context
menu are disabled. However, when running the sample provided by the book,
the items are enabled. I think I follow the book restrictedly. I could NOT
get the solution. The major code is as follows, which is completely from the
book. Could you please give me a hint?
Thanks so much!
LB
import "ContextMenuView.h"
static NSMenu *defaultMenu;
@implementation ContextMenuView
+ (NSMenu*)defaultMenu
{
if (nil == defaultMenu)
{
@synchronized(self)
{
if (nil == defaultMenu)
{
defaultMenu = [NSMenu new];
[defaultMenu addItemWithTitle: @"Copy"
action: @selector(copy:)
keyEquivalent: @"c"];
[defaultMenu addItemWithTitle: @"Paste"
action: @selector(paste:)
keyEquivalent: @"v"];
}
}
}
return defaultMenu;
}
static BOOL addItemsToMenuFromMenu(NSMenu *menu, NSMenu *template)
{
for (NSMenuItem *item in [template itemArray])
{
NSMenuItem *itemCopy = [item copy];
[menu addItem: itemCopy];
[itemCopy release];
}
return [menu numberOfItems] > 0;
}
- (NSMenu*)menuForEvent: (NSEvent*)theEvent
{
NSPoint click = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
NSMenuItem *locationMenuItem =
[[NSMenuItem alloc] initWithTitle: NSStringFromPoint(click)
action: NULL
keyEquivalent: @""];
[locationMenuItem setEnabled: NO];
NSMenu *menu = [NSMenu new];
[menu addItem: locationMenuItem];
[locationMenuItem release];
[menu addItem: [NSMenuItem separatorItem]];
if (addItemsToMenuFromMenu(menu, [self menu]))
{
[menu addItem: [NSMenuItem separatorItem]];
}
addItemsToMenuFromMenu(menu, [[self class] defaultMenu]);
return [menu autorelease];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden