Patrick Jeeves Patrick Jeeves - 4 years ago 143
C++ Question

How to forward events in wxWidgets?

I have a window with a GLCanvas and scroll bars, I want the canvas to capture scroll events and forward them to the scroll bars, which are then used to position the 'camera.' So I've done this:

MainWindow::MainWindow(wxWindow* parent,wxWindowID id)
{
//...
GlCanvas->Connect(ID_GLCANVAS1, wxEVT_MOUSEWHEEL, (wxObjectEventFunction)&MainWindow::onMouseWheel, 0L, this);
}

void MainWindow::onMouseWheel(wxMouseEvent & event)
{
if(event.GetWheelAxis() == wxMOUSE_WHEEL_VERTICAL)
{
std::cerr << "vertical scroll" << std::endl;
verticalScroll->ProcessWindowEvent(event);
}
else
{
std::cerr << "horizontal scroll" << std::endl;
horizontalScroll->ProcessWindowEvent(event);
}
}


However, this isn't doing anything aside from the printouts. What do I do to get the scrollbars to process the wheel events?

VZ. VZ.
Answer Source

You can't forward wx events to native controls, if you think about it, how would it possible for this to work when the native controls don't have any idea about wx? So it doesn't and never will.

In this particular case you can (relatively) easily translate scroll events to the calls to ScrollLines(), ScrollPages() or just SetScrollPos(). This will allow you to control another window scrolling using the same scrollbar.

Do not forget to call Skip() in the original handler to let the window being scrolled from actually scrolling too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download