Using Declares to Set Font Size

This is actually quite fascinating.

If I run the declares on a text field, I get back the same size as Xojo. So a text field with a Xojo width of 80 and a Xojo height of 22 comes back in the function with an NSWidth of 80 and NSHeight of 22 (I’m using the terms NSWidth and NSHeight as what the Cocoa declare is returning in the NSSize structure).

For a PopUpMenu with Xojo height of 20 and Xojo width of 80 (default), the declare returns an NSHeight of 26 and an NSWidth of 85.

For a Button with Xojo height of 20 and Xojo width of 80, the declare returns an NSHeight of 32 and an NSWidth of 92!

It seems quite strange how Cocoa reports back different pixel sizes than Xojo…

And it doesn’t matter if I use getBounds or getFrame.

I can only assume that Cocoa adds some space around the more “graphical” controls…

Bounds has always been a weird one, quite useful in some circumstances, i.e. you can do scaling of a control this way. AFAIK Cocoa has always returned different values to Xojo.

declare function convertRectToBacking lib "Cocoa" selector "convertRectToBacking:" ( NSViewInstance as integer, rect as NSRect ) as NSRect

Call this on a rectControl (using .handle on the control) and pass in a NSRect, the returned rect will be size that you need.

There’s also the function, but for NSSize [NSView convertSizeToBacking:]

Sam,

Thank you. However, not sure how useful this is…

So if I do the following:

declare function convertRectToBacking lib "Cocoa" selector "convertRectToBacking:" ( NSViewInstance as integer, rect as NSRect ) as NSRect

Dim ctrl as integer = MyControl.Handle
Dim MyRect as NSRect
MyRect = convertRectToBacking(ctrl,MyRect)

I get 0 size.

If I do this:

Declare Function getFrame Lib "Cocoa" selector "frame" (id As Integer) As NSRect
declare function convertRectToBacking lib "Cocoa" selector "convertRectToBacking:" ( NSViewInstance as integer, rect as NSRect ) as NSRect

Dim ctrl as integer = MyControl.Handle
Dim MyRect as NSRect = getFrame(ctrl)
Dim MyRect2 as NSRect
MyRect2 = convertRectToBacking(ctrl,MyRect)

There is no difference in size between MyRect and MyRect2. I still get 22 pixels for my “small” size pop-up.

The only thing that seems to be a difference after running this function is that ROrigin in NSRect has different values. Returns like a -22 for the Y coordinate.

All sizes have stayed the same. I’ve tried convertSizeFromBacking as well and no difference.

It means that you’re using a 1:1 screen, 1 point to 1 pixel.

This is a known by Xojo and considered correct as effectively you’re changing the underlying control values without letting Xojo know. So it still thinks it’s the old values.

Almost every control that you obtain the .frame NSRect from will have different numbers to what Xojo uses, I think this is so that x-plat is more consistent.

Frame and Bounds are NSView dimensions, so I’m guessing the reason why you’re not getting the actual popup menu height is because we asking for the wrong thing, we’re asking for the area that the popup menu can reside in.