Bhavesh Modi
Bhavesh Modi

Reputation: 323

How to create right click Menu in NSOutlineView?

I am new to this technology , I want to display different Context Menu on Right click of Parent Node and Child Node..

Upvotes: 1

Views: 2358

Answers (2)

pkamb
pkamb

Reputation: 34983

Swift Version:

class SubclassOutlineView: NSOutlineView {

    override func menu(for event: NSEvent) -> NSMenu? {
        let point = convert(event.locationInWindow, from: nil)
        let row = self.row(at: point)
        let item = self.item(atRow: row)

        let menu = NSMenu()
        // ...

        return menu
    }

}

The bit I was missing was item(atRow:, which gives you the needed data source item. Found that at this related question:

How do you add context senstive menu to NSOutlineView (ie right click menu)

Upvotes: 1

Parag Bafna
Parag Bafna

Reputation: 22930

Subclass NSOutlineView and implement - (NSMenu *)menuForEvent:(NSEvent *)theEvent.

-(NSMenu*)menuForEvent:(NSEvent*)evt 
{
    NSLog(@"menuForEvent %@ %@",self, [self delegate]);
    NSPoint pt = [self convertPoint:[evt locationInWindow] fromView:nil];
    int row=[self rowAtPoint:pt];
    // create menu ...
    return menu;
} 

On Mac OS 10.5 and above, create NSMenu in nib and set delegate and implement:

-(void)menuNeedsUpdate:(NSMenu *)menu

Upvotes: 2

Related Questions