I would like my component to listen to mouse events, but discard them in some conditions, and let the event propagate to the other components.
Is that something implemented?
I would like my component to listen to mouse events, but discard them in some conditions, and let the event propagate to the other components.
Is that something implemented?
Not really… it’s one of those things that kind of sounds like it’d be easy, but in practice allowing events to be discarded create all kinds of tricky edge-cases.
However I’ve rarely found a situation where you can’t implement the behaviour you need using things like hitTest(), layered components, or adding/removing mouse listeners dynamically.
If I make my component listening to its own mouse events, but also setInterceptsMouseClicks(false, false) on this same component, will mouse events propagate, and will the listener be called?
That wouldn’t help, see docs for Component::setInterceptMouseClicks()
Changes the default return value for the hitTest() method.
Setting this to false is an easy way to make a component pass its mouse-clicks through to the components behind it.
This means, when setting to false, no mouse events will arrive.
But by adding a Component as MouseListener, you can receive a copy of the original mouse event. Just make sure to transform the coordinates using the MouseEvent::originalComponent.
If a mouse event occurs, it is handed through the components front to back, until it hits a component which returns hitTest = true.
I see. I did not understand the hitTest() that way. This is perfect. Thanks.
I found a case where it gets tricky to do what I want: if I want the Component to react only for a mouse event which has a specific Modifier (such as show popup menu). If the modifier is not used, the click could go to the components behind it.
I think it would be nice to have a hitTest with more information.
I think the trick is in your parent Component
childComponent.addMouseListener(this);
then in the mouse handler methods you can check if event.originalComponent == &childComponent
and any other modifiers you want and then call some method on childComponent
appropriately.
That is an option. Another option is, also using the additional MouseEventListener, but check in both mouseDown for the presence or absence of the modifier.
It looks like a suitable addition, to add the modifiers to hitTest, but I think it doesn’t always apply, since hitTest is also called on mouseEnter etc.
But you can check the current modifier state from everywhere, you don’t need it in the arguments. Use the static ModifierKeys::getCurrentModifiers() method (in your hitTest e.g.).
exactly what I need.