Titlebar and toolbar height

Mac OSX specific forum
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Titlebar and toolbar height

Post by wombats »

I need to calculate the on-screen positions of gadgets, so I need to know the height of the titlebar and toolbar. How can I get that? ToolBarHeight() returns 0 on macOS, as written in the documentation.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Titlebar and toolbar height

Post by wilbert »

wombats wrote:I need to calculate the on-screen positions of gadgets
Can you give an example of what you mean with an on-screen position ?
Windows (x64)
Raspberry Pi OS (Arm64)
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

Hi,

I want to be able to test where the mouse is relative to the gadget, so I believe I need to know the gadget's position from the top-left of the screen, unless there's some other way to do it. Not having the toolbar height throws off the calculation. I'm able to retrieve the menu height like this:

Code: Select all

macMenu = CocoaMessage(0, sharedApplication, "mainMenu")
height = CocoaMessage(0, macMenu, "menuBarHeight")
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Titlebar and toolbar height

Post by wilbert »

wombats wrote:unless there's some other way to do it.
You can get the mouse location in window coordinates by using mouseLocationOutsideOfEventStream and convert those to the coordinate system of a specific view (gadget) using convertPoint:fromView: .

Code: Select all

OpenWindow(0, 0, 0, 270, 250, "Relative mouse coordinates", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 80, 30, "Button")
EditorGadget(1, 50, 60, 210, 150)
SetGadgetText(1, "Editor")

If CreateStatusBar(0, WindowID(0))
  AddStatusBarField(#PB_Ignore)
EndIf

Repeat
  Event = WaitWindowEvent()
  
  CocoaMessage(@WindowMouseLoc.NSPoint, WindowID(0), "mouseLocationOutsideOfEventStream")
  CocoaMessage(@ButtonLoc.NSPoint, GadgetID(0), "convertPoint:@", @WindowMouseLoc, "fromView:", #nil)
  CocoaMessage(@EditorLoc.NSPoint, GadgetID(1), "convertPoint:@", @WindowMouseLoc, "fromView:", #nil)
  StatusBarText(0,0, "Button("+Str(ButtonLoc\x)+","+Str(ButtonLoc\y)+")  Editor("+Str(EditorLoc\x)+","+Str(EditorLoc\y)+")")
  
Until Event = #PB_Event_CloseWindow
Windows (x64)
Raspberry Pi OS (Arm64)
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

Thanks! That's really helpful.

Do you know why the Y coordinate is inverted with some gadgets, like the CanvasGadget, OpenGLGadget, ImageGadget and ContainerGadget? I can deal with that, but will it be consistent across all macOS versions?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Titlebar and toolbar height

Post by wilbert »

You can detect for each gadget if the coordinates are flipped or not (isFlipped) where the normal PB coordinate system is equal to coordinates being flipped.
The default for macOS is that the bottom left is (0,0).

But after a little more thinking I realized that you don't need cocoa to do this.
You can do with normal PB procedures. That also solves the flipped coordinates issue.

Code: Select all

OpenWindow(0, 0, 0, 270, 250, "Relative mouse coordinates", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 80, 30, "Button")
EditorGadget(1, 50, 60, 210, 150)
SetGadgetText(1, "Editor")

If CreateStatusBar(0, WindowID(0))
  AddStatusBarField(#PB_Ignore)
EndIf

Repeat
  Event = WaitWindowEvent()
  
  wx = WindowMouseX(0)
  wy = WindowMouseY(0)
  g0x = wx - GadgetX(0, #PB_Gadget_WindowCoordinate)
  g0y = wy - GadgetY(0, #PB_Gadget_WindowCoordinate)
  g1x = wx - GadgetX(1, #PB_Gadget_WindowCoordinate)
  g1y = wy - GadgetY(1, #PB_Gadget_WindowCoordinate)
  
  StatusBarText(0,0, "Button("+Str(g0x)+","+Str(g0y)+")  Editor("+Str(g1x)+","+Str(g1y)+")")
  
Until Event = #PB_Event_CloseWindow
Windows (x64)
Raspberry Pi OS (Arm64)
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

I had no idea about those flags. Thank you!
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

I think I am going to have to use the Cocoa way because the PureBasic way doesn't work when the mouse is outside the window (it starts giving inverse numbers and then stops).

However, I'm not sure what to do about the Y coordinate. When the mouse is at the top of the canvas, it reports 150 and 0 for the bottom. How can I make it report the Y coordinate as if the coordinates start from the top-left?

Code: Select all

OpenWindow(0, 0, 0, 270, 250, "Relative mouse coordinates", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 80, 30, "Button")
CanvasGadget(1, 50, 60, 210, 150)
SetGadgetText(1, "Editor")

If CreateStatusBar(0, WindowID(0))
  AddStatusBarField(#PB_Ignore)
EndIf

Repeat
  Event = WaitWindowEvent()
 
  CocoaMessage(@WindowMouseLoc.NSPoint, WindowID(0), "mouseLocationOutsideOfEventStream")
  CocoaMessage(@ButtonLoc.NSPoint, GadgetID(0), "convertPoint:@", @WindowMouseLoc, "fromView:", #nil)
  CocoaMessage(@EditorLoc.NSPoint, GadgetID(1), "convertPoint:@", @WindowMouseLoc, "fromView:", #nil)
  StatusBarText(0,0, "Button("+Str(ButtonLoc\x)+","+Str(ButtonLoc\y)+")  Editor("+Str(EditorLoc\x)+","+Str(EditorLoc\y)+")")
 
Until Event = #PB_Event_CloseWindow
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: Titlebar and toolbar height

Post by Justin »

Hi wombats, to flip the editor y I simply used this:

Code: Select all

EditorLoc\y = -(EditorLoc\y - GadgetHeight(1))
not sure if is what you want
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

Justin wrote:Hi wombats, to flip the editor y I simply used this:

Code: Select all

EditorLoc\y = -(EditorLoc\y - GadgetHeight(1))
not sure if is what you want
Yup, that's exactly what I needed. Thanks!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Titlebar and toolbar height

Post by wilbert »

Here's an example of automatically detecting if the coordinates are flipped or not.

Code: Select all

Procedure GadgetMouseLocation(Gadget, *Location.NSPoint)
  Protected.i NSWindow, NSView, Bounds.NSRect
  If *Location
    NSView = GadgetID(Gadget)
    NSWindow = CocoaMessage(0, NSView, "window")
    CocoaMessage(*Location, NSWindow, "mouseLocationOutsideOfEventStream")
    CocoaMessage(*Location, NSView, "convertPoint:@", *Location, "fromView:", #nil)
    If CocoaMessage(0, NSView, "isFlipped") = 0
      CocoaMessage(@Bounds, NSView, "bounds")
      *Location\y = Bounds\size\height - *Location\y
    EndIf
  EndIf
EndProcedure



OpenWindow(0, 0, 0, 270, 250, "Relative mouse coordinates", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 80, 30, "Button")
CanvasGadget(1, 50, 60, 210, 150)

If CreateStatusBar(0, WindowID(0))
  AddStatusBarField(#PB_Ignore)
EndIf

Repeat
  Event = WaitWindowEvent()
  
  GadgetMouseLocation(0, @ButtonLoc.NSPoint)
  GadgetMouseLocation(1, @CanvasLoc.NSPoint)

  StatusBarText(0,0, "Button("+Str(ButtonLoc\x)+","+Str(ButtonLoc\y)+")  Canvas("+Str(CanvasLoc\x)+","+Str(CanvasLoc\y)+")")
  
Until Event = #PB_Event_CloseWindow
Windows (x64)
Raspberry Pi OS (Arm64)
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Titlebar and toolbar height

Post by wombats »

wilbert wrote:Here's an example of automatically detecting if the coordinates are flipped or not.
...
Thanks!
Post Reply