Search code examples
eventswxwidgets

wxWidgets generic event handlers


Using wxSmith I have a dialog with many buttons which require the same event handlers, but of course acting on their own instance of data.

Back in the day using C++ builder I would have placed an identifer string in the tag field of the widget. Then I would have a function to iterate all the widgets and use the string as the constructor to create a custom object and placed a pointer to that in the tag field.

That way a generic event handler could simply cast the tag field to get a custom specic instance that knows what to do with the event.

Other GUI API's, such as visual basic, were more simple but allowed an array of controls or an index and events were called with a reference to the index, so this sort of thing was still doable with arrays.

So far the only way I can see off associating additional info with a wxWidget is to inheret it into my own customised control, but I fear that will break compatibility with wxSmith.

Anybody know an elegant way to do this?


Solution

  • A simple way to handle this is, as you said, using a generic event handler. Just connect (Bind) all buttons events to the same function (method) of your dialog :

    Bind(wxEVT_BUTTON, TheDialog::OnButtonClicked, this);
    

    In the corresponding event handler, you'll be able to retreive the related button using event.GetEventObject(), or its Id if you know it using event.GetId().

    Regards Xav'