Re: Flipping over coordinate conversion
Re: Flipping over coordinate conversion
- Subject: Re: Flipping over coordinate conversion
- From: James Bucanek <email@hidden>
- Date: Wed, 26 Apr 2006 11:01:49 -0700
glenn andreas wrote on Wednesday, April 26, 2006:
>You've got a hodge podge of different views and coordinates (and make
>assumptions about what things are flipped and what aren't -
Which was exactly my point. If this is written correctly, I shouldn't be flipping rectangles myself.
>especially since the windows content view isn't guaranteed to be the
>same thing as the window "base").
Ah, you're right. I should have cleaned up my own code before posting it. ;)
I initially tried using toView:nil but got the wrong results and thought maybe I should be using the context view of the window instead. It appear that this assumption is wrong.
>I'd also make it an instance method of the view itself (entirely
>typed in mail, so bound to have at least one problem):
>
>- (NSRect) screenRectEquivalent
>{
> NSRect bounds = [view bounds]; // start with our bounds in our
>coordinate system
I need to get the frame of the view, not its bounds. When a view is in a scroll view its bounds is often larger than its frame (the portion we're going to see in the scroll view). So my code gets the frame of the view and translates it using the coordinate system of its superview.
> bounds = [self convertRect: bounds toView: nil]; // bounds is now
>in the windows base coordinate system
> bounds.origin = [[self window] convertBaseToScreen:
>bounds.origin]; // now convert the origin to screen coordinates
Thank you, this is much simpler than my code.
Alas, I still get exactly the same problem.
And now that I've had time to step away from the problem and think about it again, I realize that it's entirely my mistake. The problem was in using the origin of the view's frame, rather than the entire frame rectangle. The origin will always be {0,0} whether it's the bottom/left corner and the rectangle extends up, or it's the top/left corner and the rectangle extends down. By trying to translate just the origin of the view's rectangle instead of the rectangle itself, I threw away the direction of the rectangle. (I think I was thrown by the fact that there is no rectangle flavor of -[NSWindow convertBaseToScreen:] so I focused on converting the origin point throughout. So I'm guessing that there is no such thing as a "flipped" base coordinate system.)
The corrected code is
+ (NSRect)screenRectEquivelentOfView:(NSView*)view
{
NSWindow* viewWindow = [view window];
NSRect frame = [view frame];
frame = [[view superview] convertRect:frame toView:nil];
frame.origin = [viewWindow convertBaseToScreen:frame.origin];
return (frame);
}
Thanks for all the help.
James
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden