Re: TableView and context menus
Re: TableView and context menus
- Subject: Re: TableView and context menus
- From: Ondra Cada <email@hidden>
- Date: Tue, 25 Sep 2001 16:45:23 +0200
Sven,
>
>>>>> Sven A. Schmidt (SAS) wrote at Tue, 25 Sep 2001 15:36:47 +0200:
SAS> {
SAS> NSEnumerator *iter = [ [ crTable tableColumns ]
SAS> objectEnumerator ];
SAS> NSTableColumn *col;
SAS> NSMenu *menu = [ [ NSMenu alloc ] init ];
SAS> [ menu addItemWithTitle: NSLocalizedString( @"Run now", @"Run
SAS> now context menu item" )
SAS> action: @selector(runSelectedCommand) keyEquivalent: @"" ];
SAS> while ( col = [ iter nextObject ] )
SAS> [ [ col dataCell ] setMenu: menu ];
SAS> [ menu release ];
SAS> }
SAS> What am I doing wrong?
Presuming that each table cell has its own data cell, which "stays" where
the table cell is. That's not true (perhaps you've used NSMatrix, which works
that way, before). The NSColumn, though, has just _one_ data cell, which
stays "nowhere", but is used to draw any item (with changing coordinates).
For your trivial case of one menu for whole table it should be possible to
set it directly to the TableView (even in InterfaceBuilder without any
programming).
Should you need specific menus for different columns, or even for different
cells, you'd have to do something like this:
@implementation NSTableView (OCSPopUpMenuForSpecificCell)
-(NSMenu*)menuForEvent:(NSEvent*)evt {
NSPoint pt=[self convertPoint:[evt locationInWindow] fromView:nil];
int column=[self columnAtPoint:pt],row=[self rowAtPoint:pt];
if (column>=0 && row>=0 && [[self delegate]
respondsToSelector:@selector(menuForTableColumn:row:)])
return [[self delegate] menuForTableColumn:[[self tableColumns]
objectAtIndex:column] row:row];
return nil;
}
@end
Here the actual menu is provided by a delegate (that's how I use it in some
of my applications, from whose implementation I've copied the excerpt), but
of course you can hardcode the menu there as well.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc