# Converting an NSScreen to a Xojo screen from coordinates

Hello,

I have a NSScreenMBS object as input and must convert it to a Xojo screen object. NSScreens being Cocoa based, the {0;0} coordinate is at the lower-left corner of the first display; in Xojo, {0;0} is at the upper-left corner. The same applies to other screens, obviously.

Now, with “simple” setups, it seems relatively easy to find out which Xojo screen equals a NSScreen (only the “y” coordinate must be calculated).
However, with more complex setups, like 3 vertically-aligned screens where screen(0) is at the bottom, I can’t figure out how my code must behave.

For instance, here’s a complex setup (unlikely to be real, but it takes most possible cases; also note the sizes are visually wrong):

(In blue, the Xojo coordinate of each screen; in green, the NSScreen/Cocoa coordinate. In orange, the width/height of each screen and in purple, the computed offsets. The blue and green dots are {0;0} for Xojo and Cocoa, respectively).

In this example, say I want to move a window from NSScreen 1 to NSScreen 6, but I must pass Xojo values. And I must consider screen 5 being in-between.

This is puzzling me; any help, please?
Regards.

Why are you dealing with NSSScreens at all ?
What makes you need / want to convert back & forth ?

EDIT : never mind I saw your post on the Xojo forum

note that Xojo left = ns screen left all the time so we dont need to mess with that ever

given a NSScreen with coordinates at (x,y)
the Xojo left will = the NSScreen left (so we wont bother with it)

1. compute the offset as NSScreen Y - Screen 1 height
for screen 6 this is 1560 - 1080 (or 480)

2. compute the Xojo Top as 0 - offset from step 1 - screen n height
for screen 6 this is 0 - 480 - 500 => -980

for screen 3

1. compute the offset as NSScreen Y - Screen 1 height
- 600 - 1080 => -1680

2. compute the Xojo Top as Xojo origin (0) - offset from step 1 - screen n height
for screen 6 this is 0 - -1680 - 600 => 1080

for screen 5

1. 1080 - 1080 => 0
2. 0 - 0 - 480 => -480

for screen 2

1. 0 - 1080 => -1080
2. 0 - -1080 - 576 => 504

for screen 4

1. 56 - 1080 => -1024
2. 0 - -1024 - 1024 => 0

note you can optimize this by skipping the 0 - offset and just negating the offset

1 Like

Great, according to my tests, it works nicely; thank you very much!

I’ll never understand how people manage to solve that kind of problems. I’m not dumb either, but with my sight disability, mental visualisation is tricky to me.

As for optimising it, that’s unnecessary for my needs (it’s mainly to move windows from one screen to another).

well

1. I realized that for screen 1 the effect is a translation of the origin by the height if the screen
thats why you at one point subtract the height of that screen
it offsets the X,Y origin vertically

2. in order to get the translation of the NS coordinates to the Xojo coordinates you have to get increasing negative values from 0,0 moving up and increasing positive from 0,0 moving down
the exact opposite of the NS Screen

and this is why you subtract from 0 (the Xojo origin Y coordinate) to “flip” things into Xojo coordinates
0,0 in NS space is 0, 1080 for Xojo (if we talk about screen 1)
0, 500 in NS space is 0, 580 for Xojo

and thats where the height - NS Y coordinate comes in to turn the NS Y into the Xojo Y

not sure I have explained my reasoning well enough but thats how I sat down & figured this out

Yes, you explained it very well and, now it makes sense to me (no pun intended), but I wouldn’t have made the correct steps myself without the explanations.

Thank you.

No problem
Interesting question

EDIT : hah ! someone went so far as to remove the post on the xojo forums where I said “its answered on those other forums”
not a link - just a mention

I was also sure this post would be removed when I read it; no surprise in the long run… Still a big shame hardly understandable (if at all).