• 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: How to find the position of an NSCell?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to find the position of an NSCell?


  • Subject: Re: How to find the position of an NSCell?
  • From: Wagner Truppel <email@hidden>
  • Date: Wed, 4 Jul 2007 19:43:22 +0200

Hi Kai,

an easy way is to sequentially set each cell tag (either through IB or programmatically), starting from 0. Then, given a tag, you know the cell's row and column in the matrix, as follows: if your matrix has C columns, then the row and column of a cell with a given tag are:

row = tag / C
col = tag % C

(tag, row, and col all start at 0)

The above assumes that the upper-most and left-most cell has tag 0, the next cell to its right has tag 1, and so on.

Once you know a cell's row and column, you can compute its origin's coordinates, since all cells (presumably) have the same size, W pixels wide by H pixels tall. If the origin of a cell is its rectangle's *upper*-left corner, then its x and y coordinates are:

x = x0 + col (W + Xgap)
y = y0 + row (H + Ygap)

where Xgap and Ygap are, respectively, the horizontal and vertical gaps between cells, in pixels. Here, x0 and y0 are the coordinates of the *upper*-left corner of the matrix, plus the respective gaps you might want to have between the edges of the matrix and the cells themselves.

It's easier to carry out these computations using a flipped NSView so that coordinates are naturally computed from the upper-left corner downward.

Hope this helps.
Wagner

Subject: How to find the position of an NSCell?
From: Kai-Mikael Jää-Aro <email@hidden>
Date: Wed, 4 Jul 2007 13:51:19 +0200

I am constructing a hierarchical tool palette and thought the best way to do this would be through using a (subclass of) NSPanel containing an NSMatrix with NSButtonCell objects.

Now, when the user presses one of the NSButtonCells I want to open a new NSPanel with the sub-choices for that button right below the pressed button but I believe that in order to position the NSPanel I will have to know the position of the button and I can't figure out any better method for finding that position than letting the button have a back reference to its containing view and its position in that view and then ask the view for the screen coordinates when needed.

Or, is there a more direct method?
_______________________________________________

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


  • Follow-Ups:
    • Re: How to find the position of an NSCell?
      • From: Kai-Mikael Jää-Aro <email@hidden>
  • Prev by Date: Re: Simple authorization question
  • Next by Date: Re: Simple authorization question
  • Previous by thread: How to find the position of an NSCell?
  • Next by thread: Re: How to find the position of an NSCell?
  • Index(es):
    • Date
    • Thread