RE: Contextual menus in Outline Views
RE: Contextual menus in Outline Views
- Subject: RE: Contextual menus in Outline Views
- From: Keith Blount <email@hidden>
- Date: Fri, 7 Jan 2005 05:50:33 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Hello,
There may be a different or more simple way, but the
way I achieved contextual menus in my outline view was
by subclassing NSOutlineView. Actually I had
subclassed NSOutlineView already to add tooltips (by
adapting Andreas Mayer's excellent
AMToolTipTableView), and I just added the following
code:
// in the outline view subclass .h file:
@protocol HEMOutlineViewToolTipDelegate // could
probably be a category
- (NSMenu *)menuForOutlineView:(NSOutlineView*)ov
rowIndexes:(NSIndexSet *)indexes;
@end
// in the outline view subclass implementation code:
- (NSMenu *)menuForEvent:(NSEvent *)event
{
// Make sure the row we are at is selected before
opening the popup menu if it is not already selected
NSPoint pt = [self convertPoint:[event
locationInWindow] fromView:nil];
int row = [self rowAtPoint:pt];
if (![self isRowSelected:row])
[self selectRow:row byExtendingSelection:NO];
NSIndexSet *indexSet = [self selectedRowIndexes];
// Call the delegate method if any rows are selected
if ( ([self selectedRow] > -1) && ([[self delegate]
respondsToSelector:@selector(menuForOutlineView:rowIndexes:)])
)
return [[self delegate] menuForOutlineView:self
rowIndexes:indexSet];
else
return [self menu];
}
All this does is first make sure that the row you are
ctrl-clicking on gets selected if it's not already
selected, and then it asks the outline view's delegate
to return a menu. That way the delegate can examine
the selected rows and decide which menu it needs to
return accordingly.
For instance, if you just wanted a different menu
depending on whether one item or many were selected,
your delegate method might look like this:
- (NSMenu *)menuForOutlineView:(NSOutlineView *)ov
rowIndexes:(NSIndexSet *)indexes
{
if ( [indexes count] == 1 ) // one item selected
return oneItemMenu;
else if ( [indexes count] > 1 ) // more than one item
selected
return multipleItemsMenu;
else
return nil; // else don't show a menu
}
And of course, you can just use itemAtRow: if you want
to examine the actual item selected to make more
specific menu decisions.
Hope that helps,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden