Using Mouse events from base class Topic is solved
Using Mouse events from base class
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?
Re: Using Mouse events from base class
Please show the base class and how you use it, both stand-alone and when deriving from it.
Use the source, Luke!
Re: Using Mouse events from base class
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();
}
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()
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 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.
Re: Using Mouse events from base class
Yes, i would expect the same.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.
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 190 times
Use the source, Luke!
Re: Using Mouse events from base class
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?
On what platform and version did it work for you?
Re: Using Mouse events from base class
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.
I was using the latest development version under Windows.
Use the source, Luke!
Re: Using Mouse events from base class
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.
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.
Re: Using Mouse events from base class
I just tested with 2.9.3 under Windows, still works for me.
Use the source, Luke!
Re: Using Mouse events from base class
Reinstalled wxDev-C++ and now it works for me,too.