Page 1 of 1

Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 9:20 am
by Kukulkan
Hi,

I'm using the oMsg() functions to send Cocoa messages and I found this code to determine the screen DPI. I'm on PB 5.0 for this, so I cant use CocoaMessage...

Two questions:
(1) Can somebody help me to translate this for PB:
NSScreen *screen = [NSScreen mainScreen];
NSDictionary *description = [screen deviceDescription];
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
CGSize displayPhysicalSize = CGDisplayScreenSize(
[[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);

NSLog(@"DPI is %0.2f",
(displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
This is where I stuck:

Code: Select all

Define screen.i = oMsg(0, 0, "NSScreen mainScreen")
Debug screen.i

Define description.i = oMsg(0, screen.i, "deviceDescription")
Debug description.i

Define objDisplayPixelSize.i = oMsg(0, description.i, "objectForKey:$", @"NSDeviceSize")
Debug objDisplayPixelSize.i

; How to get sizeValue now???
(2) Is this the correct function to determine the DPI if someone is using Retina Display on his Mac?

Best,

Kukulkan

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 10:06 am
by wilbert
I hope you don't mind I'm using the 5.10+ CocoaMessage syntax since I'm using 5.10.
Just replace it with oMsg in your case.

Code: Select all

Define dpi.NSSize
Define screen.i = CocoaMessage(0, 0, "NSScreen mainScreen")
Define description.i = CocoaMessage(0, screen, "deviceDescription")
CocoaMessage(@dpi, CocoaMessage(0, description, "objectForKey:$", @"NSDeviceResolution"), "sizeValue")

Debug dpi\width
Debug dpi\height
I don't know about Retina Display. Maybe someone who has one can report you the values the code above returns.

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 11:00 am
by Kukulkan
Ah, I did not realize the first parameter of oMsg() and this is where I stuck.

Works now! Thank you!

Best,

Volker

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 6:22 pm
by WilliamL
Hi wilbert!

Oddly enough I get 72.0 & 72.0 on my retina MBP

...maybe because PureBasic has 'Open in Low Resolution' checked?

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 8:07 pm
by wilbert
Thanks for checking William.

After some reading, the backingScaleFactor method of NSScreen should return 2.0 for a retina display.

Code: Select all

Define backingScaleFactor.CGFloat = 1.0

If OSVersion() >= #PB_OS_MacOSX_10_7
  CocoaMessage(@backingScaleFactor, CocoaMessage(0, 0, "NSScreen mainScreen"), "backingScaleFactor")
EndIf

Debug backingScaleFactor
Does this display 2.0 for a retina display ?

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 9:27 pm
by gwhuntoon
Wilbert, running your code I'm getting 1.0 on my MacBook Pro w/Retina running OS X 10.8.2. I tried both the Best for Retina display setting and Scaled - More Space and still get 1.0.

Greg

Re: Need help getting screen DPI using Cocoa messages

Posted: Wed Mar 06, 2013 10:33 pm
by WilliamL
I agree with Greg, I get 1.0 also. Once again, I'm guessing that is because PB is in low-res. The retina res is twice normal and that (would seem) to be a factor or 2 (which you are looking for).

I note that, even as an executable, the scaling factor is still 1 (and in 'low res').

Re: Need help getting screen DPI using Cocoa messages

Posted: Thu Mar 07, 2013 6:33 am
by wilbert
WilliamL wrote:I agree with Greg, I get 1.0 also. Once again, I'm guessing that is because PB is in low-res.
You probably need to add

Code: Select all

<key>NSHighResolutionCapable</key>
<string>true</string>
to the info.plist file.

Re: Need help getting screen DPI using Cocoa messages

Posted: Thu Mar 07, 2013 8:17 am
by Kukulkan
This is interesting. I did some include to wrap most gadget and window creation functions. This include is checking the DPI and calculates a Scale-Factor (Windows: DPI / 96). PureBasic scales the font sizes, but not windows and gadgets. So scaling all that dimension values in the includes leads to great scaling on Windows if the DPI is 120 (125% - 1.25) or even 144 (150% - 1.5).

I want to do the same on MacOS. But this are the open questions:
- Is PB also scaling the fonts but not the gadgets and windows like in Windows?
- How to determine the scale factor? Can I really use 72 DPI as default and NSDeviceResolution to get the factor?
- Is it needed to set NSHighResolutionCapable=true in info.plist. Does this activate?
- How can I test/simulate this behaviour on my MacBook with MacOS X 10.7.4?

Best,

Kukulkan

Re: Need help getting screen DPI using Cocoa messages

Posted: Thu Mar 07, 2013 8:46 am
by wilbert
You need Graphics Tools for Xcode.
When installed, you need to launch Quartz Debug .
Enable HiDPI display modes (Window ---> UI Resolution)

If your Macbook has a screen resolution of 1280 x 800, you should be able to select a HiDPI resolution of 640 x 400. Since this is probably not enough to run OS X itself I doubt if it will work for you. On my 21,5" display (1920 x 1080 px) I can select a HiDPI resolution of 960 x 540 which is enough to work with for testing purposes.

Re: Need help getting screen DPI using Cocoa messages

Posted: Thu Mar 07, 2013 5:59 pm
by WilliamL
If the MBP-retina is 2880x1800 (220 ppi), I'm guessing Kukulkan wants to work at that higher resolution. It would be interesting to see how that works.