• 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: NS <-> CG Rect Conversion and screen coordinates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NS <-> CG Rect Conversion and screen coordinates


  • Subject: Re: NS <-> CG Rect Conversion and screen coordinates
  • From: Jesper Storm Bache <email@hidden>
  • Date: Wed, 1 Apr 2009 14:52:33 -0700

Forgive me for being dense. Where is the subtle bug?
The code is using CGMainDisplayID (not [NSScreen mainScreen] which would be the display with the key window)


CGMainDisplayID is documented as:
=======
The main display is the display with its screen location at (0,0) in global coordinates. In a system without display mirroring, the display with the menu bar is typically the main display.
=======


I would expect that CGRectGetHeight(CGDisplayBounds (CGMainDisplayID ())) == zeroScreenHeight()?


Jesper Storm Bache Core Technologies Adobe Systems Inc


On Apr 1, 2009, at 12:25 PM, Peter Ammon wrote:


On Mar 31, 2009, at 9:34 PM, Trygve Inda wrote:

Using these two calls:

NSRect nsRect = [screen frame];
CGRect cgRect = CGDisplayBounds (displayID);

I get for my two screens:

NS    x=0        y=0      w=2560    h=1600  // screen A
CG    x=0        y=0      w=2560    h=1600

NS    x=-1920    y=184    w=1920    h=1200  // screen B
CG    x=-1920    y=216    w=1920    h=1200


It seems CG origin is Top, Left with y growing down, while NS is origin Bottom, Left, y growing up. So I convert CG to NS with:

// Convert CG coordinates from (TL, y down) to (BL, y up)

CGRect    mainScreenRect = CGDisplayBounds (CGMainDisplayID ());

cgRect.origin.y = (cgRect.origin.y + cgRect.size.height -
mainScreenRect.size.height) * -1;

(216 + 1200 - 1600) * -1 = 184

Is there a system function that does this? NSRectFromCGRect does not
do the
coordinate conversion.

No, there is no function to do this conversion.

Note that your conversion has a subtle bug that will fail for multiple
displays.  The CG coordinate system has its origin at the top of the
zero screen, not the main screen (which changes with the user focus).
The zero screen is the screen at index zero in the +screens array.

So if you define a function like this:

CGFloat zeroScreenHeight(void) {
   CGFloat result = 0;
   NSArray *screens = [NSScreen screens];
   if ([screens count] > 0) result = NSHeight([[screens objectAtIndex:
0] frame]);
   return result;
}

then you can do screen-coordinate conversions like this:

NSMakePoint(cgPoint.x, zeroScreenHeight() - cgPoint.y);
NSMakeRect(cgRect.origin.x,  zeroScreenHeight() - cgRect.origin.y -
cgRect.size.height, cgRect.size.width, cgRect.size.height);

-Peter


_______________________________________________

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



_______________________________________________

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: NS <-> CG Rect Conversion and screen coordinates
      • From: Peter Ammon <email@hidden>
References: 
 >Re: NS <-> CG Rect Conversion and screen coordinates (From: Peter Ammon <email@hidden>)

  • Prev by Date: Re: Best Strategy to Control iTunes
  • Next by Date: Re: NS <-> CG Rect Conversion and screen coordinates
  • Previous by thread: Re: NS <-> CG Rect Conversion and screen coordinates
  • Next by thread: Re: NS <-> CG Rect Conversion and screen coordinates
  • Index(es):
    • Date
    • Thread