Any way to get which widget fired an event?

sig9

20-03-2008 16:55:23

Is there a way to find out which widget send off an event. Ie. is it possible to use 1 single event handler function for multiple widgets; I want to have a bunch of buttons which all run the same event handler function, is it possible to somehow find out which button set off the event?

I'm just trying to think of a better way to do this other than having a ton of separate event functions. Any ideas would be helpful.

kungfoomasta

20-03-2008 17:32:51

The current design allows you to specify what handler to use for a given event. You can have all widgets point to the same handler without any problems.

For example, say you have the following:

class X
{
...
void myHandler(const EventArgs& args);
}


You can register it as an event handler to as many widgets as you want:

myButton->addEventHandler(EVENT_GAIN_FOCUS,&X::myHandler,xInstance);
myWindow->addEventHandler(EVENT_MOUSE_BUTTON_DOWN,&X::myHandler,xInstance);

etc..

To find out which widget fired the event:

void myHandler(const EventArgs& args);
{
const WidgetEventArgs& wea = dynamic_cast<const WidgetEventArgs&>(args);
Widget* widgetThatFiredEvent = wea.widget;
....
}

sig9

20-03-2008 18:01:24

Thanks for the quick reply. I knew it was kind of a stupid question, but I wasn't sure whether or not that type of typecast would work in this case.