What's the best way to get local coords for an embedded canvas?

I have a canvas that allows panning and zooming of a graphic. This canvas is inside a ContainerControl along with some buttons and scrollbars for controlling the display of the graphic.

When using the canvas mouse events I know that the X,Y coords are relative to the enclosing ContainerControl. And when that container is enclosed in another container the coords are relative to the outer container, and so on. I have compensated for the various coordinate systems by looping my way up the parent chain and adjusting X,Y by the enclosing container’s Left,Top.

I am just wondering - is there a better way to do this?

Um, that shouldn’t be. The documentation states “The coordinates are local to the control, not to the window.” The coordinates should be relative to the Canvas, not it’s parent ContainerControl. If you can recreate the behavior in a sample, file a ticket.
https://documentation.xojo.com/api/deprecated/rectcontrol.html#rectcontrol-mousemove

Indeed, but in the meantime I still need it to work… which it does. Again, just wondering if there is a better way.

To be quite honest, it sounds like it’s a problem with your code not the framework. If you have a project to share, we as a community might be able to work out what’s not operating correctly.

Why can’t you use the local coordinates? It sounds like you are using absolute coordinates. Show us your code.

I can’t reproduce your Container/Coordinates problem with a sample project.

I’ll put together a sample project later today and post it here.

Hm. I am looking for the same thing; I have a container within a container that might be within a tabPanel - and even another container (I don’t know in the situation I am in since I want to have this working generically if possible).

Now - when the window was deactivated, the mouse moved while the window was deactivated and then the window is activated again (by CMD-Tab or something like that) - I want to highlight something that’s under the mouse instantly. For this I need to calculate X and Y relative to the container from MouseX and Y which are relative to the window.

For the moment I don’t do it (since going up the whole ladder of possible embeds is a nuisance) - but it would be very nice to be able to show the user that something will actually happen when there’s a click. For now, the mouse must be moved by a bit to do the highlighting (which is the same as Safari seems to do so I can always say it’s “consistent behavious”) - but I would like it more if it actually highlighted directly…

For the moment I don’t see a way around how Clifford is doing it, is there?

:slight_smile:

This is still true.

It’s all good when I use “MouseMove” or “MouseDrag” - there, X and Y are relative to the control. But in the Activate Event of my Canvas - I don’t get X and Y coordinates - and have to use them from “Me.MouseX” and “Me.MouseY” - and they are relative to the window (as stated in the docs).

Like this?

Public Function TrueBoundary(Extends theRectControl As RectControl) as BoundaryRect
  
  'gets the boundary of a rect control in relation to the screen and not the parent
  
  dim theBoundary as new BoundaryRect
  theBoundary.height = theRectControl.height
  theBoundary.width = theRectControl.width
  theBoundary.top = theRectControl.top
  theBoundary.left = theRectControl.left
  
  'loop up the containing chain
  dim theWindow as window = theRectControl.window
  while theWindow isa containercontrol
    theBoundary.top = theBoundary.top + theWindow.top
    theBoundary.left = theBoundary.left + theWindow.left
    theWindow = containercontrol(theWindow).window
  wend
  
  'add values for the window
  theBoundary.top = theBoundary.top + theWindow.top
  theBoundary.left = theBoundary.left + theWindow.left
  
  return theBoundary
End Function

Great! Thank you very much for this!

Do you know the feeling when you cannot tell the trees from the woods sometimes? :sweat_smile:

I don’t have a “BoundaryRect” - but it works fine when this is changed to a Xojo.Rect - now I can get the mouse coordinates relative to any canvas in all of my embed-situations by stating:

Var mR As Xojo.Rect = Me.TrueBoundary

Var mXLocal As Integer = Me.MouseX + TrueWindow.Left - mR.Left
Var mYLocal As Integer = Me.MouseY + TrueWindow.Top - mR.Top

Works like a charme - thanks again!