Skip to content

Instantly share code, notes, and snippets.

@masonmark
Created July 28, 2010 05:44
Show Gist options
  • Save masonmark/493504 to your computer and use it in GitHub Desktop.
Save masonmark/493504 to your computer and use it in GitHub Desktop.
// ContextualMenuTableView.j Created by mason on 2010/07/26. This source code is in the public domain.
// NOTES: This subclass adds simple right-click contextual menu support to CPTableView.
// It does not work with Cappuccino 0.8.1, but it works with the latest master branch as of
// 2010-07-27. To use it, just have your table's delegate implement -tableView:contextualMenuForRows:.
// The table view handles updating the selection before invoking the delegate method. This doesn't
// support the cool modern Mac OS X 10.6 style of contextual menus that don't destructively change
// the user selection (discussed by me in this thread: http://groups.google.com/group/objectivej/browse_thread/thread/129dca07459d9325 ),
// because that is not easily done with CPTableView as it currently exists. So if you right click
// inside the selected rows, you get a context menu for the selected rows. If you right click
// outside of the selected rows, the current selection is discarded, the right-clicked row is
// selected, and you get a contextual menu for just that row. However, someday I hope to support
// that more sophisticated style of contextual menu, which is why the delegate method has the
// affectedRows parameter. (Currently affectedRows is always the same as the table's -selectedRowIndexes.)
// I don't think it is yet known exactly how Cappuccino's CPTableView will eventually implement
// support for contextual menus.
@import <AppKit/CPTableView.j>
@implementation ContextualMenuTableView : CPTableView
{
}
- (void)rightMouseDown:(CPEvent)anEvent
{
var mouseDownPoint = [self convertPoint:[anEvent locationInWindow] fromView:nil];
var clickedRow = [self rowAtPoint:mouseDownPoint];
var selectedRows = [self selectedRowIndexes];
var affectedRows = [CPIndexSet indexSetWithIndex:clickedRow];
if ([selectedRows containsIndex:clickedRow])
{
affectedRows = selectedRows;
}
var contextualMenu = [self tableView:self contextualMenuForRows:affectedRows];
if (contextualMenu)
{
[self selectRowIndexes:affectedRows byExtendingSelection:NO];
[CPMenu popUpContextMenu:contextualMenu withEvent:anEvent forView:self];
}
else
{
[super rightMouseDown:anEvent];
}
}
- (CPMenu)tableView:(CPTableView)aTableView contextualMenuForRows:(CPIndexSet)affectedRows
{
var del = [self delegate];
if ([del respondsToSelector:@selector(tableView:contextualMenuForRows:)])
{
return [del tableView:self contextualMenuForRows:affectedRows];
}
var testMenu = [[CPMenu alloc] initWithTitle:@"test context menu"];
[testMenu addItemWithTitle:@"contextual menus" action:nil keyEquivalent:@""];
[testMenu addItemWithTitle:@"are not yet" action:nil keyEquivalent:@""];
[testMenu addItemWithTitle:@"implemented! wh00!" action:nil keyEquivalent:@""];
[testMenu addItemWithTitle:@" \\(^_^)/" action:nil keyEquivalent:@""];
return testMenu;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment