• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting the context of a context menu
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting the context of a context menu


  • Subject: Re: Getting the context of a context menu
  • From: Nicko van Someren <email@hidden>
  • Date: Sat, 14 Apr 2007 12:50:38 +0100

On 12 Apr 2007, at 12:50, Nicko van Someren wrote:
...
So, is there some way that I've not found to determine which view was responsible for raising a context menu, or am I going to have to subclass every standard UI item I use just so that I can bolt in a -menuForEvent: method that says { menuContext = self; return [self menu]; }

Thanks to everyone for all the suggestions on this. In summary, there seems to be no clean, simple, legitimate way to do this. I have however found ways that violate only one from the set of clean, simple and legitimate! Here's a quick summary for the archive.


The 'not clean' method is to switch the -menuForEvent: method on the base implementation of NSView for one which records the event which raised any given context menu.

The 'not simple' method is to do what I was trying to avoid, which is to sub-class every type of UI object to which you might ever want to attach a context menu, so that it records the most recent source of the context menu. Once all the necessary classes have been sub- classed you need to also set the 'custom class' for each UI item in the NIB.

The 'not legitimate' method, which I worked out this morning, is to extract the necessary information from the NSMenu class against its will. Looking through a class-dump for the AppKit framework one finds the NSMenu class method +_contextMenuEvent; which leads to the following solution called from within the menu item target action:
NSEvent *event = [NSMenu _contextMenuEvent];
NSView *contents = [[event window] contentView];
NSPoint location = [[contents superview] convertPoint: [event locationInWindow] fromView:nil];
NSView *source = [contents hitTest: location];
NSLog(@"Context menu came from %@", source);


It's not clear to me why the +_contextMenuEvent: method needs to be private, and I've not checked to see if it's a recent addition or something that has been there since NextStep, but it does the trick for the moment...

	Cheers,
		Nicko

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


  • Follow-Ups:
    • Re: Getting the context of a context menu
      • From: "Clark Cox" <email@hidden>
References: 
 >Getting the context of a context menu (From: Nicko van Someren <email@hidden>)

  • Prev by Date: Re: Programatic binding to a popup button selection
  • Next by Date: Re: Force update
  • Previous by thread: Re: Getting the context of a context menu
  • Next by thread: Re: Getting the context of a context menu
  • Index(es):
    • Date
    • Thread