I have a scenario in which I would like some wxWidgets controls to respond to middle-button mouse clicks, behaving in exactly the same way as they normally would for a left click, except that I should be able to tell it's a middle click and do something different in my application.
Specifically, I am working with buttons and sliders, and want the buttons to be pressable and the slider to be draggable with the middle button. I'm in Windows (and that's the only platform I care about), in case that makes a difference.
After poking through the wx docs a bit I didn't find any way to set which mouse buttons a control listens to. So, my first idea was to write an event handler that captures middle-button events, translates them to the corresponding left-button events, and tells wx to process them:
void OnMouseEvent(wxMouseEvent & wxevent)
{
wxMouseEvent wxeventTranslated = wxevent;
if (wxevent.MiddleDown())
{
wxeventTranslated.SetEventType(wxEVT_LEFT_DOWN);
}
else if (wxevent.MiddleUp())
{
wxeventTranslated.SetEventType(wxEVT_LEFT_UP);
}
else
{
return;
}
m_pWxbutton->ProcessWindowEvent(wxeventTranslated);
}
(here m_pWxbutton is the wxButton where the events are registered). It didn't work: the mouse event handler is called, but I never get a wxEVT_COMMAND_BUTTON_CLICKED event, nor does the button enter the "pressed" visual state in the GUI. I also tried a version where I changed the event type on the passed-in event and then called Skip(), but that had the same result.
Admittedly for buttons it would be easy to just detect the click myself, but I am hoping to get this to work for sliders as well, and I don't relish the idea of reimplementing all the mouse logic for those. :)
How can I translate mouse events and get them to behave like native GUI events? Or, is there a completely different approach I should try?