Re: Contextual Menus for an NSOutlineView
Re: Contextual Menus for an NSOutlineView
- Subject: Re: Contextual Menus for an NSOutlineView
- From: Max Horn <email@hidden>
- Date: Thu, 22 Nov 2001 16:31:05 +0100
The way I solved this in the end was to use a subclass of
NSOutlineView, which (besides other things like more Finder like cell
editing, tooltips on a per-item base etc.) allows the delegate to
provide a context menu for each item. IMHO that is a better solution
than to e.g. subclass NSTableColumn.
To do this, your subclass must implement menuForEvent:, and the code
will look something like this:
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
int rowIndex;
rowIndex = [self rowAtPoint:[self convertPoint:[theEvent
locationInWindow] fromView:nil]];
if (rowIndex >= 0)
{
// There seems to be a bug in AppKit; selectRow is supposed to
// abort editing, but it doesn't, thus we do it manually.
[self abortEditing];
item = [self itemAtRow:rowIndex];
if (item)
{
id delegate = [self delegate];
if ([delegate
respondsToSelector:@selector(outlineView:shouldSelectItem:)] &&
[delegate outlineView:self shouldSelectItem:item])
[self selectRow:rowIndex byExtendingSelection:NO];
if ([delegate
respondsToSelector:@selector(outlineView:contextMenuForItem:)])
return [delegate outlineView:self contextMenuForItem:item];
}
}
else // click in lala land
{
[self deselectAll:self];
return [self menu];
}
return nil;
}
Then in you delegate, provide this method:
- (NSMenu *)outlineView:(NSOutlineView *)outlineView
contextMenuForItem:(id)item;
Cheers,
Max
--
-----------------------------------------------
Max Horn
Software Developer
email: <
mailto:email@hidden>
phone: (+49) 6151-494890