Need help getting screen DPI using Cocoa messages

Mac OSX specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Need help getting screen DPI using Cocoa messages

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Need help getting screen DPI using Cocoa messages

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Need help getting screen DPI using Cocoa messages

Post by Kukulkan »

Ah, I did not realize the first parameter of oMsg() and this is where I stuck.

Works now! Thank you!

Best,

Volker
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Need help getting screen DPI using Cocoa messages

Post 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?
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Need help getting screen DPI using Cocoa messages

Post 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 ?
Windows (x64)
Raspberry Pi OS (Arm64)
gwhuntoon
New User
New User
Posts: 9
Joined: Sun Mar 03, 2013 2:50 pm
Location: Muskegon, MI USA

Re: Need help getting screen DPI using Cocoa messages

Post 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
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Need help getting screen DPI using Cocoa messages

Post 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').
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Need help getting screen DPI using Cocoa messages

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Need help getting screen DPI using Cocoa messages

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Need help getting screen DPI using Cocoa messages

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Need help getting screen DPI using Cocoa messages

Post 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.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply