Page 1 of 1

Titlebar and toolbar height

Posted: Wed Oct 23, 2019 1:30 am
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.

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 6:26 am
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 ?

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 11:04 am
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")

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 11:56 am
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

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 2:13 pm
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?

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 2:49 pm
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

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 2:57 pm
by wombats
I had no idea about those flags. Thank you!

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 7:40 pm
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

Re: Titlebar and toolbar height

Posted: Wed Oct 23, 2019 9:08 pm
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

Re: Titlebar and toolbar height

Posted: Thu Oct 24, 2019 12:54 am
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!

Re: Titlebar and toolbar height

Posted: Thu Oct 24, 2019 8:20 am
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

Re: Titlebar and toolbar height

Posted: Thu Oct 24, 2019 4:08 pm
by wombats
wilbert wrote:Here's an example of automatically detecting if the coordinates are flipped or not.
...
Thanks!