Duplicate events for mouseEnter / mouseExit


#1

I’m probably missing something obvious, so please forgive a Juce absolute beginner:
I have a MainWindow with a Component-derived class that displays an image. The component does addMouseListener(this, false) in its constructor. I have stub mouseExit()/mouseEnter() functions that only DBG() the event.
Now the question: It seems that mouseEnter()/mouseExit() always get called twice when the mouse enters/exits. Why? Sample code below

[code]
class FindTramComponent : public Component {
public:
FindTramComponent();
~FindTramComponent();

void paint(Graphics& g);

// mouseListener
void mouseEnter(const MouseEvent &e);
void mouseExit(const MouseEvent &e);

private:
Image tramImage;
};


FindTramComponent::FindTramComponent() {
tramImage = ImageCache::getFromMemory(BinaryData::tram_jpg, BinaryData::tram_jpgSize);
addMouseListener(this, false);
}

void FindTramComponent::paint(Graphics& g) {
g.drawImageAt(tramImage, 0, 0);
}

void FindTramComponent::mouseEnter(const MouseEvent &e) {
DBG(“Hello Mouse!”);
}

void FindTramComponent::mouseExit(const MouseEvent &e) {
DBG(“Good Bye Mouse!”);
}[/code]


#2

Because you’ve called addMouseListener. Components already get their own mouse event callbacks without needing to register for them, so if you also add it as a listener it’ll get its normal callbacks, plus the ones that are sent to it as a listener.

I suppose I should probably add an assertion to warn if you do this… Can’t think of a good reason why you’d deliberately add a mouselistener to itself.


#3

Because you’ve called addMouseListener. Components already get their own mouse event callbacks without needing to register for them[/quote]

Oh… Thanks!


#4

Thanks for the assertion. I ran into this trap too. For some reason, I had assumed that components didn’t receive their mouse events unless if we explicitly specified this.