Walker
Walker

Reputation: 1225

NSTextFieldCell Coordinates

I'm trying to get the NSPoint cooridnates of an NSTextFieldCell, but NSTextFieldCell doesn't inherit from NSView, and therefore doesn't have the frame method. Any ideas on how to get around this?

I'm trying to use Matt Gemmel's MAAttachedWindow to attach little helper popups to certain elements. I want to attach it to an area on the window, and other than hacking it together by manually messing with the x and y coordinates, I'm not sure how to get the coordinates.

Upvotes: 2

Views: 2220

Answers (4)

Marc Charbonneau
Marc Charbonneau

Reputation: 40515

For using MAAttachedWindow, could you instead use the control's frame (if it's a single-cell control), or in the case of a table view with a specific row one of the NSTableView layout methods? rectOfRow: or rectOfColumn:, for example. You could override NSTextFieldCell and position the window in one of the drawing methods, but I'd save that for my absolute last choice if possible.

Upvotes: 2

Peter Hosey
Peter Hosey

Reputation: 96353

It doesn't have co-ordinates. There are none to get. The way cells work is that the view tells the cell “draw here”.

So, if you're not in one of the cell's drawing methods and you really, really need co-ordinates, you need to ask the view that owns the cell. Usually, this is [cell controlView].

(Perhaps you're thinking of UIKit? UITableViewCells are very different from NSCells.)

Upvotes: 1

James Eichele
James Eichele

Reputation: 119164

You can call [theCell controlView] to get a reference to the control that owns a particular cell. If the NSTextFieldCell object is part of a simple control, such as an NSTextField, the following should be sufficient:

NSRect cellFrame = [[theCell controlView] frame];
NSPoint origin = cellFrame.origin;
//..

If, however, the NSTextFieldCell is part of a more complex control, such as an NSTableView, where a single cell is used in multiple places, you will need more information in order to determine the proper rectangle. NSCell offers the method representedObject, which can help you to determine which object in the NSTableView is represented by the cell at that particular moment. Without knowing more about your specific case, I don't know how much more detail to provide in that regard.

Here is one possible solution, assuming you are able to discern the row and column information from the object stored in representedObject:

NSTableView * tableView = [theCell controlView];
id cellObject = [theCell representedObject];

NSInteger row = //... determine from representedObject
NSInteger col = //... determine from representedObject

NSRect cellFrame = [tableView frameOfCellAtColumn:col row:row];

Upvotes: 3

Marc Charbonneau
Marc Charbonneau

Reputation: 40515

A cell is a reusable object that's owned by an NSControl (an NSView subclass). It doesn't have an origin point because it doesn't actually represent a place on the screen, it's up to the control (which has a specific frame rectangle) to draw it where and when it's needed. Think about a table view for example, it might use a single cell that's re-drawn in several places for each row.

The cell is passed a frame rectangle by the control when it's drawn onto the screen, in most cases that should be all you need. You can also take advantage of NSCell's sizing methods, which can tell you how much room it needs to display its content.

Upvotes: 2

Related Questions