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

前端 未结 5 1983
终归单人心
终归单人心 2020-12-25 14:16

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 t

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-25 15:10

    If you prefer, you can attach the menu to the individual cell view or row view and build it with interface builder:

    @implementation BSMotleyOutlineView
    
    -(NSMenu *)menuForEvent:(NSEvent *)event
    {
        NSPoint pt = [self convertPoint:[event locationInWindow] fromView:nil];
        NSInteger row = [self rowAtPoint:pt];
        if (row >= 0) {
            NSTableRowView* rowView = [self rowViewAtRow:row makeIfNecessary:NO];
            if (rowView) {
                NSInteger col = [self columnAtPoint:pt];
                if (col >= 0) {
                    NSTableCellView* cellView = [rowView viewAtColumn:col];
                    NSMenu* cellMenu = cellView.menu;
                    if(cellMenu) {
                        return cellMenu;
                    }
                }
                NSMenu* rowMenu = rowView.menu;
                if (rowMenu) {
                    return rowMenu;
                }
            }
        }
        return [super menuForEvent:event];
    }
    @end
    

提交回复
热议问题