Re: adding contextual menu items to an NSTextView subclass menu
Re: adding contextual menu items to an NSTextView subclass menu
- Subject: Re: adding contextual menu items to an NSTextView subclass menu
- From: daniel <email@hidden>
- Date: Mon, 14 Feb 2005 10:55:54 -0800
Hi Michael - if your superclass is caching its default menu (not creating it from scratch on the fly), then your duplicate items problem makes sense because you're appending your item every time the +defaultMenu method is called. You probably want to create a copy of the super menu and avoid modifying it directly. That way other subclasses of your superclass won't have the same default menu logic forced on them. Then whether you cache your copy or not is up to you.
As for the validation issues, the problem is you're asking a menu item to target "self" from a class method. But your class doesn't have a class method called "revealVisualCuesInFinderCM:" - it only has an *instance* method with that signature. What happens if you change the "setTarget" line in your code to setTarget:nil? Does that make your validate method get called? I'm not sure if that's the right way to set target to first responder programatically, but I suspect it might do it.
Daniel
On Feb 14, 2005, at 9:42 AM, Michael Hanna wrote:
<x-tad-smaller>in my NSTextView subclass I have the following code, I'm trying to add and an enable a contextual menu item, but I'm having trouble. If the NSTextView is empty, the new separator and item appears disabled(good thing), but when I have something in its NSTextStorage, the item I'm adding is repeated(bad thing) and not enabled either. ValidateMenuItem is called, but it never seems to match @"Show Visual Cues File". I know I'm way off on the implementation, but not sure where. oOwningDocument is an IBOutlet to the NSDocument that it's a field declaration of.
regards,
Michael
+(NSMenu *)defaultMenu
{
NSMenu *theDefaultMenu = [</x-tad-smaller><x-tad-smaller>super</x-tad-smaller><x-tad-smaller> defaultMenu];
NSMenuItem *anItem = </x-tad-smaller><x-tad-smaller>nil</x-tad-smaller><x-tad-smaller>;
NSLog(</x-tad-smaller><x-tad-smaller>@"revealVisualCuesInFinder"</x-tad-smaller><x-tad-smaller>);
[theDefaultMenu addItem:[NSMenuItem separatorItem]];
anItem = [[[NSMenuItem alloc] initWithTitle:</x-tad-smaller><x-tad-smaller>@"Show Visual Cues File"</x-tad-smaller><x-tad-smaller> action:</x-tad-smaller><x-tad-smaller>@selector</x-tad-smaller><x-tad-smaller>(revealVisualCuesInFinderCM:) keyEquivalent:</x-tad-smaller><x-tad-smaller>@""</x-tad-smaller><x-tad-smaller>] autorelease];
[theDefaultMenu addItem:anItem];
[anItem setTarget:</x-tad-smaller><x-tad-smaller>self</x-tad-smaller><x-tad-smaller>];
</x-tad-smaller><x-tad-smaller>return</x-tad-smaller><x-tad-smaller> theDefaultMenu;
}
-(</x-tad-smaller><x-tad-smaller>id</x-tad-smaller><x-tad-smaller>)sender
{
NSLog(</x-tad-smaller><x-tad-smaller>@"revealVisualCuesInFinderCM"</x-tad-smaller><x-tad-smaller>);
}
- (</x-tad-smaller><x-tad-smaller>BOOL</x-tad-smaller><x-tad-smaller>)validateMenuItem:(NSMenuItem *)inItem
{
NSLog(</x-tad-smaller><x-tad-smaller>@"[inItem title] %@"</x-tad-smaller><x-tad-smaller>, [inItem title]);
</x-tad-smaller><x-tad-smaller>if</x-tad-smaller><x-tad-smaller> ([[inItem title] isEqualToString:</x-tad-smaller><x-tad-smaller>@"Show Visual Cues File"</x-tad-smaller><x-tad-smaller>])
{
NSLog(</x-tad-smaller><x-tad-smaller>@"found Show Visual Cues File]"</x-tad-smaller><x-tad-smaller>);
</x-tad-smaller><x-tad-smaller>if</x-tad-smaller><x-tad-smaller>(![oOwningDocument visualCueIsPresent])
</x-tad-smaller><x-tad-smaller>return</x-tad-smaller><x-tad-smaller> </x-tad-smaller><x-tad-smaller>NO</x-tad-smaller><x-tad-smaller>;
}
</x-tad-smaller><x-tad-smaller>return</x-tad-smaller><x-tad-smaller> </x-tad-smaller><x-tad-smaller>YES</x-tad-smaller><x-tad-smaller>;
}
2005-02-14 12:19:01.509 GuitarPrompter[758] [inItem title] Cut
2005-02-14 12:19:01.509 GuitarPrompter[758] [inItem title] Copy
2005-02-14 12:19:01.509 GuitarPrompter[758] [inItem title] Paste
2005-02-14 12:19:01.509 GuitarPrompter[758] [inItem title] Spelling...
2005-02-14 12:19:01.509 GuitarPrompter[758] [inItem title] Check Spelling
2005-02-14 12:19:01.510 GuitarPrompter[758] [inItem title] Check Spelling as You Type
2005-02-14 12:19:01.510 GuitarPrompter[758] [inItem title] Underline
2005-02-14 12:19:01.510 GuitarPrompter[758] [inItem title] Outline
2005-02-14 12:19:01.510 GuitarPrompter[758] [inItem title] Start Speaking
2005-02-14 12:19:01.511 GuitarPrompter[758] [inItem title] Stop Speaking
2005-02-14 12:19:01.511 GuitarPrompter[758] [inItem title] Right to Left
</x-tad-smaller> _______________________________________________
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
_______________________________________________
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