• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Context menu on NSOutlineView, which row is active/selected?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Context menu on NSOutlineView, which row is active/selected?


  • Subject: Re: Context menu on NSOutlineView, which row is active/selected?
  • From: Jens Alfke <email@hidden>
  • Date: Wed, 27 Jan 2010 09:23:54 -0800


On Jan 26, 2010, at 9:09 PM, David Melgar wrote:

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?

Kyle's already pointed out -clickedRow. The pseudo-code for this is

	if there is a clickedRow and the clickedRow is not selected
		operate on the clickedRow only
	else
		operate on the selected rows

I've found it handy to put this into a separate method that returns the selected items or rows, and then use that method as the basis for what to operate on in action methods that operate on the selection. (If invoked through a regular menu or toolbar item or button, this algorithm is still safe because clickedRow will return -1, so you'll be operating on the selection as normal.)

(BTW this applies equally to regular NSTableViews.)

I recently had to implement this logic once again (for the Chrome bookmark manager); here's what it looks like:

// Returns the selected/right-clicked item(s) for a command to act on.
- (NSArray*)actionItems {
  int row = [outline_ clickedRow];
  if (row >= 0 && ![outline_ isRowSelected:row])
    return [NSArray arrayWithObject:[outline_ itemAtRow:row]];

  return [self selectedItems];
}

In fact thanks to the wonders of open source you can easily look at the entire class:
http://src.chromium.org/viewvc/chrome/trunk/src/chrome/browser/cocoa/bookmark_tree_controller.mm?revision=36892&view=markup


—Jens_______________________________________________

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


References: 
 >Context menu on NSOutlineView, which row is active/selected? (From: David Melgar <email@hidden>)

  • Prev by Date: Re: What is the equivalent of SetSystemUIMode() in Leopard and above?
  • Next by Date: Re: Global in NSApplication
  • Previous by thread: Re: Context menu on NSOutlineView, which row is active/selected?
  • Next by thread: Re: Context menu on NSOutlineView, which row is active/selected?
  • Index(es):
    • Date
    • Thread