开发者

intercepting mouse events on FileBrowserComponent in Juce library

开发者 https://www.devze.com 2023-03-04 22:23 出处:网络
I recently begin to use Juce library. I usually post Juce related question on its forum, but I\'m struggling with an issues from a lot of days, and I received still no answers. So stackoveflow does wo

I recently begin to use Juce library. I usually post Juce related question on its forum, but I'm struggling with an issues from a lot of days, and I received still no answers. So stackoveflow does worth a try even if seems that there aren't a lot of Juce's users here.

Here's the question:

I'm making some experiments with Juce's Components. I have the following class:

class MyComponent : public Component{

public :
MyComponent(Component * comp){
    this->child = comp;
    comp->setInterceptsMouseClicks(false, false);

}
void mouseDown (co开发者_JAVA技巧nst MouseEvent &e){

   //do stuff
   Component *target = getTopChild(this->child, e.x, e.y);   //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
   if (target != NULL && doIWantToForwardEventToTarget()){
        MouseEvent newEvent = e.getEventRelativeTo(target);
        target->mouseDown(newEvent);
   }
}
void mouseMove (const MouseEvent &e);
void mouseEnter (const MouseEvent &e);
void mouseExit (const MouseEvent &e);
void mouseDrag (const MouseEvent &e);
void mouseUp (const MouseEvent &e);
void mouseDoubleClick (const MouseEvent &e);
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY);

private:

Component *child;
}

The purposes of this class are:

  1. store a single Component of a hierarchy of Components (child)

  2. intercept all mouse events related to child or one of its descendant

  3. do something
  4. eventually forward the MouseEvent to the Component which it was directed to

I tried this class with sliders components as children, even nested inside other components.. all works fine. Now I was doing some experiments with FileBrowserComponent and it seems not to work properly. For example when I click on the button to move to the upside directory it doesn't (the button receives the mouse event and it's clicked but nothing happen in the tree view). Also selecting items from the list doesn't work.

What could be the problem? (I did some experiments and seems that the method buttonClicked in the FileBrowserComponent isn't called, but I dont't know why) Any suggestion?

I also tried to modify the code this way:

void mouseDown (const MouseEvent &e){

   //do stuff
   Component *target = getTopChild(this->child, e.x, e.y);   //return top most component of child that would have intercept the mouse event if that wasn't intercepted by MyComponent class
   if (target != NULL && doIWantToForwardEventToTarget()){
        target->setInterceptsMouseClicks(true, true);
        MouseEvent newEvent = e.getEventRelativeTo(target);
        target->mouseDown(newEvent);
        target->setInterceptsMouseClicks(false, false);
   }
}

It still doesn't work. Anyway I found out that if I comment the second call to setInterceptMouseClicks( where I disable the mouse click after ) make the things work (even if this isn't the result I'd want to obtain because I need to redisable mouse events on that component).

These facts can led me to 2 considerations:

  1. A component need to intercept mouse clicks even if mouse events are manually passed to its mouseDown method (is this true? I'm not so sure about that)
  2. After the mouse event handling in FileBrowserComponent there are other classes that uses the information of its intercepting mouse click status, otherwise it would work if after the target->mouseDown(newEvent), I'll disable mouse clicks again. Any idea?

Thanks in advance


You should better create derived classes from Component, and in those derived classes implement their required mouse events, and when those events are triggered, you can post a message to the parent class, so it can do other stuff.


It seems that you are clicking on a child component of FileBrowserComponent. and the mouse event of the child is not forwarded back to the parent(FileBrowserComponent). that's why, when you click inside, the parent will not receive the event.

so instead of going Top --> Bottom, you should go bottom --> top. To do this: you should add a mouse listener to the child by this method:

addMouseListener (MouseListener *newListener, 
                  bool wantsEventsForAllNestedChildComponents)

So for the example you're showing :

child->addMouseListener(this, true);

And the method mouseDown() should be like this :

void mouseDown (const MouseEvent &e){

    if(this->child == e.eventComponent){
      // this mouse event is coming from 
      // the child component or child's children
      // as wantsEventsForAllNestedChildComponents = true

      // do something
    }
}

Hope this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消