Re: Context menu on NSOutlineView, which row is active/selected?
Re: Context menu on NSOutlineView, which row is active/selected?
- Subject: Re: Context menu on NSOutlineView, which row is active/selected?
- From: Corbin Dunn <email@hidden>
- Date: Wed, 27 Jan 2010 09:39:24 -0800
On Jan 26, 2010, at 9:09 PM, David Melgar wrote:
> I have a menu set as the Menu outlet of an NSOutlineView to use as a context menu (right click) on a row.
>
> When the menu is chosen on a row, the chosen action on my target (my OutlineView controller) is invoked as expected.
>
> In my view controller, I had been assuming that the row the right click occurred would show up as a selected row. But it does not always. It appears to be a separate state.
>
> How should I determine the row that the right click occurred on?
>
> This is 10.6.2 BTW.
Take a look at the demo:
http://developer.apple.com/mac/library/samplecode/DragNDropOutlineView/index.html
http://developer.apple.com/mac/library/samplecode/DragNDropOutlineView/listing2.html
corbin
--------
// On Mac OS 10.5 and above, NSTableView and NSOutlineView have better contextual menu support. We now see a highlighted item for what was clicked on, and can access that item to do particular things (such as dynamically change the menu, as we do here!). Each of the contextual menus in the nib file have the delegate set to be the AppController instance. In menuNeedsUpdate, we dynamically update the menus based on the currently clicked upon row/column pair.
- (void)menuNeedsUpdate:(NSMenu *)menu {
NSInteger clickedRow = [outlineView clickedRow];
id item = nil;
SimpleNodeData *nodeData = nil;
BOOL clickedOnMultipleItems = NO;
if (clickedRow != -1) {
// If we clicked on a selected row, then we want to consider all rows in the selection. Otherwise, we only consider the clicked on row.
item = [outlineView itemAtRow:clickedRow];
nodeData = [item representedObject];
clickedOnMultipleItems = [outlineView isRowSelected:clickedRow] && ([outlineView numberOfSelectedRows] > 1);
}
if (menu == outlineViewContextMenu) {
NSMenuItem *menuItem = [menu itemAtIndex:0];
if (nodeData != nil) {
if (clickedOnMultipleItems) {
// We could walk through the selection and note what was clicked on at this point
[menuItem setTitle:[NSString stringWithFormat:@"You clicked on %ld items!", (long)[outlineView numberOfSelectedRows]]];
} else {
[menuItem setTitle:[NSString stringWithFormat:@"You clicked on: '%@'", nodeData.name]];
}
[menuItem setEnabled:YES];
} else {
[menuItem setTitle:@"You didn't click on any rows..."];
[menuItem setEnabled:NO];
}
} else if (menu == expandableColumnMenu) {
NSMenuItem *menuItem = [menu itemAtIndex:0];
if (!clickedOnMultipleItems && (nodeData != nil)) {
// The item will be enabled only if it is a group
[menuItem setEnabled:nodeData.container];
// Check it if it is expandable
[menuItem setState:nodeData.expandable ? 1 : 0];
} else {
[menuItem setEnabled:NO];
}
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden