Titlebar and toolbar height
Titlebar and toolbar height
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
Can you give an example of what you mean with an on-screen position ?wombats wrote:I need to calculate the on-screen positions of gadgets
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Titlebar and toolbar height
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:
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
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: .wombats wrote:unless there's some other way to do it.
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)
Raspberry Pi OS (Arm64)
Re: Titlebar and toolbar height
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?
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
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.
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)
Raspberry Pi OS (Arm64)
Re: Titlebar and toolbar height
I had no idea about those flags. Thank you!
Re: Titlebar and toolbar height
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?
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
Hi wombats, to flip the editor y I simply used this:
not sure if is what you want
Code: Select all
EditorLoc\y = -(EditorLoc\y - GadgetHeight(1))
Re: Titlebar and toolbar height
Yup, that's exactly what I needed. Thanks!Justin wrote:Hi wombats, to flip the editor y I simply used this:not sure if is what you wantCode: Select all
EditorLoc\y = -(EditorLoc\y - GadgetHeight(1))
Re: Titlebar and toolbar height
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)
Raspberry Pi OS (Arm64)
Re: Titlebar and toolbar height
Thanks!wilbert wrote:Here's an example of automatically detecting if the coordinates are flipped or not.
...