Re: Please point me in the right direction for contextual menus
Re: Please point me in the right direction for contextual menus
- Subject: Re: Please point me in the right direction for contextual menus
- From: Eric Peyton <email@hidden>
- Date: Thu, 30 Aug 2001 22:12:32 -0500
Check out the code for Fire.app. The buddy outline view class handles
the display of context menus. it is not currently complete from my web
page, but once you grok it, send me a note and I will send you the most
updated code.
Basically, you subclass the cell and have each cell have a pointer to
their respective menu (or in the case of Fire, I just included the
cellMenu as an attribute of the item stored in the cell). Then from the
outline view you use code like the following to return the menu ...
@interface BuddyOutlineView : NSOutlineView
{
BOOL supportsContextMenus;
}
- (BOOL)supportsContextMenus;
- (void)setSupportsContextMenus:(BOOL)supports;
@end
@implementation BuddyOutlineView
- (BOOL)supportsContextMenus
{
return supportsContextMenus;
}
- (void)setSupportsContextMenus:(BOOL)supports;
{
supportsContextMenus = supports;
}
- (NSMenu*) menuForEvent: (NSEvent*)event
{
NSPoint where;
int row;
NSTableColumn *column;
NSRect cellFrame;
NSMenu *menu = nil;
where = [self convertPoint: [event locationInWindow] fromView:
[[self window] contentView]];
row = [self rowAtPoint: where];
if (row < 0) {
return nil;
}
[self selectRow: row byExtendingSelection: NO];
column = [[self tableColumns] objectAtIndex: 0];
cellFrame = [self frameOfCellAtColumn: 0 row: row];
if ([[self itemAtRow:row] respondsToSelector:@selector(cellMenu)] &&
[self supportsContextMenus]) {
menu = [[self itemAtRow:row] cellMenu];
}
if (menu) {
return menu;
}
return nil;
}
Some of this code is cribbed from others :-)
eric
(walking on the backs of giants here)
On Thursday, August 30, 2001, at 09:38 PM, email@hidden wrote:
Hi, all,
I'd like to implement contextual menus on the contents of an
NSOutlineView; the contents of the menu (or at least the highlighted
state of the items in it) need to vary depending on which cell (i.e.,
item/column) is underneath the cursor when the menu is brought up.
I've looked at adding a menu to my NSOutlineView, but it's not clear
how to tell where the cursor is when the menu is brought up; will event
notifications for the view tell me what I need to know?
The other possibility I've considered is attaching a menu to the
NSCell's used to display the outline view contents. Near as I can
figure, NSOutlineView only uses one cell per column, so I would still
need to determine which item/row of the outline view is under the
cursor when the menu is invoked.
Thanks in advance for any pointers.
Doug K;
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev