How do you add the ability to right click on a row in an NSOutlineView so you can say delete an object or some other activity. (ie Like when you right click on a folder in the Apple Mail app)
I think I am half way there, I have a subclass of NSOutlineView that allows me to catch the right click and display a context menu based on the selected row rather than the row the mouse is clicking on.
@implementation NSContextOutlineView
- (NSMenu *)defaultMenu {
if([self selectedRow] < 0) return nil;
NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"Model browser context menu"] autorelease];
[theMenu insertItemWithTitle:@"Add package" action:@selector(addSite:) keyEquivalent:@"" atIndex:0];
NSString* deleteItem = [NSString stringWithFormat: @"Remove '%i'", [self selectedRow]];
[theMenu insertItemWithTitle: deleteItem action:@selector(removeSite:) keyEquivalent:@"" atIndex:1];
return theMenu;
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
return [self defaultMenu];
}
@end
Sorry if the answer is obvious I just cant find any help on this online or in the documentation.
Thanks to Void for the answer, it lead me to using this:
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
id item = [self itemAtRow: [self rowAtPoint:pt]];
return [self defaultMenuFor: item];
}
Much later than the OP question, but for others like me wondering, here is my solution. It also needs subclassing NSOutlineView, which is not encouraged by Apple doc, anyway…
Rather than override
menuForEvent:
I overriderightMouseDown:
This has the advantage of keeping delegate calls to update the menu thereafter and also keeps row outlining on right click.
Here is a Swift 2.0 example which uses a subclass and extends the default
NSOutlineDelegate
so you can define your menus in the delegate.In your menuForEvent method you can find out which row the click occurred on. You can pass that as a parameter to your defaultMenu method -- maybe call it defaultMenuForRow:
Now you can build the menu for the row you found in the event...
Also, as already mentioned in the comments, you really shouldn't use the NS-prefix on your own classes. There is a potential for a clash in the future plus it will confuse everybody that is looking at your code - including yourself :)
Hope this helps...
If you prefer, you can attach the menu to the individual cell view or row view and build it with interface builder: