If I attach a Component (MouseListener) to another Component, the position of the mouse in the MouseEvent, which should be relative to the eventComponent, may not be. The Component sends the MouseEvent to the MouseListener using HierarchyChecker::eventWithNearestParent(), in which case the eventComponent may change, but the mouse position is not updated accordingly.
However, the MouseEvent::position documentation states that:
This value is relative to the top-left of the component to which the event applies (as indicated by the MouseEvent::eventComponent field).
In the MouseListener, if I use
getLocalPoint(event.eventComponent, event.position), the position is erroneous.
You can use:
auto local = event.getEventRelativeTo(this);
to convert the event to your local component.
otherComponent->getLocalPoint (eventComponent, position) the problem impacts also
We’re using event.getEventRelativeTo() all over our code and never ran into trouble, so perhaps I’d need some more elaborate example that shows how this fails.
The problem lies in
HierarchyChecker::eventWithNearestParent(), it should be something like this:
MouseEvent eventWithNearestParent() const
Otherwise, the position of the event generated is not valid.
Can you explain the use case and component Hierarchy that you generated that caused the error?
I think there’s something non-standard in your specific usage possibly that confuses the system, which perhaps hides other potential bugs.
I’m sorry for not responding sooner. I’ve started to create a simplified example with no success because my problem arises in a rather complicated situation where the MouseListener listens to almost all the windows in my application (that is pretty big) and perhaps that’s where the problem lies. I can easily solve my problem using getMouseXYRelative() in the MouseListener but in general, it seems to me that if the component of a MouseEvent changes, the relative position should be updated accordingly, don’t you think? Anyway, there are a lot of workarounds for the problem, so I guess this is not so important.
I don’t know, I’m still not sure what the problem is. If you can somehow show a project that shows the sequence of events/component layout that creates the problem, then a discussion about solutions could take place.
Of course, if I can come up with a simple example, I’ll post it here, but I haven’t been able to yet…