Using Mouse events from base class Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Using Mouse events from base class

Post by Tony0945 » Thu Apr 23, 2015 5:06 pm

I have a generic base class. It handles mouse events. For testing purposes, I put a wxMessageBox in the nandler to Left Mouse Down to indicate if the handler is being reached. If I use the base class, the message box pops up, but if I use a class derived from it, no box pops up when I put the left mouse down. I thought that a derived class inherited the event handling of the base class. Is that only for command events? But when i derive a class from wxDialog, all the dialog mouse functionality works, so I must be doing something wrong, but how do I figure out what?

User avatar
doublemax
Moderator
Moderator
Posts: 13909
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using Mouse events from base class

Post by doublemax » Thu Apr 23, 2015 7:46 pm

Please show the base class and how you use it, both stand-alone and when deriving from it.
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Using Mouse events from base class

Post by Tony0945 » Thu Apr 23, 2015 11:43 pm

Code: Select all

class wxDragControl : public wxControl
{
    public:
        wxDragControl(wxWindow * parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize& size=wxDefaultSize,
                long style=0, const wxValidator &validator=wxDefaultValidator, const wxString &name=wxControlNameStr);
        virtual ~wxDragControl();

        void EnableDrag(bool yesorno) {dragging_is_enabled=yesorno;}  // true to enable (defaule) false to disable
                    

    protected:
        //      Event Handlers
        void LeftDown(wxMouseEvent& event);
        void LeftUp(wxMouseEvent& event);
        void Moving(wxMouseEvent& event);
        void MouseCaptureLost(wxMouseCaptureLostEvent& event);
           
        
    private:
     
        bool        dragging, dragging_is_enabled;
        int         Xpos,Ypos;  // position where dragging started
        wxWindow    *pparent;
        
        DECLARE_EVENT_TABLE();
        
};
In the cpp:

BEGIN_EVENT_TABLE(wxDragControl,wxControl)
    EVT_LEFT_DOWN(wxDragControl::LeftDown)
    EVT_LEFT_UP(wxDragControl::LeftUp)
    EVT_MOTION(wxDragControl::Moving)
    EVT_MOUSE_CAPTURE_LOST(wxDragControl::MouseCaptureLost)
 
END_EVENT_TABLE()

void wxDragControl::LeftDown(wxMouseEvent& event)
{ 
 //   wxMessageBox("LeftDown");
    if (dragging_is_enabled)
    {
        CaptureMouse();
        dragging = true;
        Xpos = event.GetX();
        Ypos = event.GetY();
    }
    
    event.Skip(); 
}
You use it by putting the left mouse down when hovering and dragging the control around it's parent.

If the derived class has an event table like this:

Code: Select all

BEGIN_EVENT_TABLE(CustomControl,wxDragControl)
	EVT_PAINT(CustomControl::paintEvent)
 
END_EVENT_TABLE()
the control won't drag.

I solved it by not having a table but inserting this code in the derived constructor

Code: Select all

    Bind(wxEVT_PAINT, &CustomControl::paintEvent, this);
It works fine, but I don't want to mark this solved until I understand why the table method doesn't work or a guru tells me "It can't work like that, trust me!".

It really is easier to use the BIND than the table anyway. But the documentation and the macro source code led me to believe that if the event wasn't in the derived class table and was in the base class table, it would be called.

User avatar
doublemax
Moderator
Moderator
Posts: 13909
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using Mouse events from base class

Post by doublemax » Fri Apr 24, 2015 8:42 am

But the documentation and the macro source code led me to believe that if the event wasn't in the derived class table and was in the base class table, it would be called.
Yes, i would expect the same.

However, i built a working sample based on your code and it seems to work for me. Check the attached code based on the "minimal" sample.
Attachments
minimal.cpp
(9.07 KiB) Downloaded 90 times
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Using Mouse events from base class

Post by Tony0945 » Fri Apr 24, 2015 3:40 pm

I built your working sample, only changing the full path name of sample.xpm, and it didn't work. There is no reaction at all to the mouse in the custom controls. Since I'm sure it works on your system, it must be a bug. I built and ran this on Windows XP with wxWidgets 2.9.3

On what platform and version did it work for you?

User avatar
doublemax
Moderator
Moderator
Posts: 13909
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using Mouse events from base class

Post by doublemax » Fri Apr 24, 2015 3:45 pm

Please note that i was using wxLogDebug for the debug messages, so you must be running under a debugger or using DBGView to see the output.

I was using the latest development version under Windows.
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Using Mouse events from base class

Post by Tony0945 » Fri Apr 24, 2015 8:15 pm

I compiled your sample under Linux with 2.9.5, it works fine.

So, if it works under windows with 3.0.2(?) and works under Linux with 2.9.5, but doesn't work under windows with 2.9.3, then there is a very strong probability that it is a bug in 2.9.3

I'm going to mark this as solved and see about making a dev-pak for 3.0.x I still use wxDev-Cpp. I tried wxSmith but the learning curve seemed too steep.

Thank you very much for your time.

User avatar
doublemax
Moderator
Moderator
Posts: 13909
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using Mouse events from base class

Post by doublemax » Fri Apr 24, 2015 9:01 pm

I just tested with 2.9.3 under Windows, still works for me.
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Using Mouse events from base class

Post by Tony0945 » Sat Apr 25, 2015 12:53 am

Reinstalled wxDev-C++ and now it works for me,too.

Post Reply