views:

58

answers:

3

I have a view which contains a few subviews:

mainView
    subViewA
    subViewB
    SubViewC

mainView is an NSView constructed from a nib and is controlled with an NSViewController subclass. The subviews are standard views such as NSTextField and NSImageView and are configured to be non-editable. I want mainView to receive rightMouseDown: even when the event is triggered in one of the subviews.

The default implementation of rightMouseDown: in NSResponder passes the event to the next responder, but NSView changes the default behaviour and does not pass it to the next responder.

I could subclass all of the subviews but this doesn't seem like a very elegant or maintainable solution.

How can I get the subviews to pass rightMouseDown: messages to the next responder without subclassing all of the subviews?

A: 

Override NSApplication's - (void)sendEvent:

- (void)sendEvent:(NSEvent *)event {
  if([event type]== NSRightMouseDown)
    redirect to wherever
  else
    [super sendEvent:event];
}
mustISignUp
A: 

My solution is to add a subview to the mainView which I've called clickCatcher. clickCatcher is transparent and equal in size to mainView and is added so that it's the top most view:

[self addSubview:clickCatcher positioned:NSWindowAbove relativeTo:nil];

The XXXmouseDown: methods of clickCatcher call superview.

Benedict Cohen
+1  A: 

This is usually accomplished by overriding the superviews's -hitTest: method?

kperryua