Cocoa PSA: Do not use -[NSView convert[Point|Rect][To|From]Base:]

1054 views
Skip to first unread message

Avi Drissman

unread,
Feb 25, 2010, 12:18:26 AM2/25/10
to Chromium-dev
http://code.google.com/p/chromium/issues/detail?id=36485

Briefly: Suppose you want to do a conversion to or from an NSView's coordinate system to the coordinate system of its window. NSView has a tempting set of methods:

convertPointFromBase:
convertPointToBase:
convertRectFromBase:
convertRectToBase:
convertSizeFromBase:
convertSizeToBase:

DO NOT USE THEM!

If you want to convert to/from the window's base coordinate system, use the original -convert[xx]:[to|from]View: and pass in a nil view.

What those "base" calls do is a conversion to the "base" coordinate system which isn't the "window base" coordinate system. This will not get you the coordinates you want. Don't use them. (The "base" coordinate system has to do with resolution independence; go look it up.)

The confusion is compounded by the "base" methods on NSWindow: convertBaseToScreen: and convertScreenToBase:. Those are conversions betwwen the window base system and screen coordinates, and are what you want to use for that case.

Avi

Avi Drissman

unread,
Mar 15, 2010, 3:07:31 PM3/15/10
to Chromium-dev
As a follow-up, I'm finding with surprising frequency that code is converting coordinates to the window's contentView's coordinate system when the intent is to convert it to the window's coordinate system. (For example, I've seen code that converts a point from a view's coordinate system to the window's content view coordinate system, and then turns around and converts it from the window's coordinate system to global space.)

This might accidentally work today, but fails horribly under resolution independence. If you need to convert to window coordinate space, please actually do so. (Use convertXXX:toView:nil as described earlier.) Using the window's content view as a target for convertXX:toView: is a very bad idea.

Avi
Reply all
Reply to author
Forward
0 new messages