Events handling in a2dObject
Although a2dObject is directly derived from wxEvtHandler, it has some extra function on to ease the use of dynamic events. Within wxArt2D the events are used for signaling changes in the framework, which can be connected to, so one gets notified of a change. So it is not to handle such events, but more to know something. Normally one use wxEvthandler::Connect() for that. But that connects directly to a function. It does not go through the static event table of the class from which a member function is connected. For a library it is often best to offer a default implementation, and the user of the library can extend it to add his own stuff. For that static event tables or very nice. The same behavior with dynamic events, would require virtual function in a base class, which would be the default implementation on receiving a signal/notify event. There is a way around that. Instead of connecting directly to a member function, wxArt2D, connects to a2dObject::ProcessConnectedEvent() instead. this function calls wxEvtHandler::ProcessEvent(). So in the end ClassA sends a signal event to classB via the dynamic event system of wxWidgets. There it calls (via ProcessConnectedEvent() ) ProcessEvent() for classB. And therefore it goes to check the static event table of classB to see if the signal event with that wxEventType was intercepted/handled. ClassB will not receive all events of classA, just the events to which it connected. Also classB does not need to handle the events to which it is connected, it may handle them. If not handled in classB, it will be skipped, so if a classC connects to the same event, it can be handled there also.
1 // a2dView connect to its document FOR EVENT wxEVT_CHANGEDTITLE_DOCUMENT.
2 m_viewDocument->ConnectEvent( wxEVT_CHANGEDTITLE_DOCUMENT, this );
3
4 //a2dView its event table handles wxEVT_CHANGEDTITLE_DOCUMENT EVENT
5 BEGIN_EVENT_TABLE( a2dView, a2dObject )
6 EVT_CHANGEDTITLE_DOCUMENT( a2dView::OnChangeTitle )
7 END_EVENT_TABLE()
Here the code in a2dObject to achieve this:
1 bool a2dObject::ProcessConnectedEvent( wxEvent& event )
2 {
3 if ( ProcessEvent( event ) )
4 {
5 event.Skip( false );
6 return true;
7 }
8 event.Skip( true );
9 return false;
10 }
11
12 void a2dObject::ConnectEvent( wxEventType type, wxEvtHandler* evtObject )
13 {
14 Connect( type, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
15 }
16
17 bool a2dObject::DisconnectEvent( wxEventType type, wxEvtHandler* evtObject )
18 {
19 return Disconnect( type, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
20 }
21
22 bool a2dObject::DisconnectEventAll( wxEvtHandler* evtObject )
23 {
24 bool succeed = false;
25 bool once = false;
26 do
27 {
28 succeed = Disconnect( wxEVT_NULL, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
29 once = succeed || once;
30 }
31 while ( succeed );
32 return once;
33 }
