views:

368

answers:

4

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.

+2  A: 

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.

Marc Charbonneau
So is there a way to figure out coordinates of the cell, or the table row containing the cell?
Walker Argendeli
Getting the drawing frame for a cell really depends on what you're trying to do. Can you edit your question to explain in more detail?
Marc Charbonneau
Ok, I just added more detail
Walker Argendeli
+2  A: 

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];
e.James
controlView isn't really guaranteed to be set correctly. I remember trying to depend on it once in a custom table view cell, and found it was always nil.
Marc Charbonneau
+1  A: 

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.)

Peter Hosey
+2  A: 

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.

Marc Charbonneau