MouseListeners and children


#1

I have a component that holds a child component.
This child component could have any internal component layout of its own.

I want my outer component to be able to respond to mouse clicks on the child component by selecting it. I’ve added the outer component as a mouseListener, and it gets clicks upon it fine. If i set it to want clicks on child components, then my callback does indeed get called when any children happen to be clicked on.

However, my problem is: I want to test that the click happened within the main child component. I can’t just say if (e.eventComponent == child), because it might be one of its children.

How can I perform this test? I could test it against the eventComponent’s parent too, but there may be many levels of depth in the child component.

I’ve tried this:

MouseEvent e2 (e.getEventRelativeTo (child));
if (child->contains(e2.x,e2.y))
{
   jassertfalse;
}

but for some reason it doesn’t always work. It’s got an odd behaviour - it will respond if the click is at least child->getX() across the child, but not if it’s less. Weirder still, if i set a breakpoint on the ‘jassertfalse’ line, then it stops at the breakpoint when you would expect it to (if the click is indeed anywhere within the child). That sort of debugging conundrum is ridiculously headscratching [i can’t test to see if the values should be right there, because when i test - they are! and when i don’t test, they’re definitely not!]


#2

can’t you just do if (e.eventComponent != this)


#3

not really, because there’s actually more than one child that might be selected - so i need to test which one it is. I’d give responsibility to the child to notify, but these components could be anything


#4

I’ve found the problem. As usual, a stupid one.


#5

Don’t you just want to look at the originalComponent member of MouseEvent instead of the eventComponent?


#6