Differences between revisions 4 and 6 (spanning 2 versions)
Revision 4 as of 2011-06-01 20:53:57
Size: 3062
Comment:
Revision 6 as of 2011-06-01 21:44:42
Size: 3330
Comment:
Deletions are marked like this. Additions are marked like this.
Line 27: Line 27:
bool a2dObject::ProcessConnectedEvent( wxEvent& event ) void a2dObject::ProcessConnectedEvent( wxEvent& event )
Line 31: Line 31:
        event.Skip( false );
        return true;
    }
        //even if the event was handled, and was not skipped,
        //we want it to skip it here, so the next dynamic connected event
        //in the calling wxEvtHandler;:ProcessEventIfMatches, will be called too.
        //The signal is typically distributed to all connected classes.
        event.Skip( true );
        return; }
Line 35: Line 38:
    return false;

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 void a2dObject::ProcessConnectedEvent( wxEvent& event )
   2 {
   3     if ( ProcessEvent( event ) )
   4     {
   5         //even if the event was handled, and was not skipped,
   6         //we want it to skip it here, so the next dynamic connected event 
   7         //in the calling wxEvtHandler;:ProcessEventIfMatches, will be called too.
   8         //The signal is typically distributed to all connected classes.
   9         event.Skip( true );
  10         return;    }
  11     event.Skip( true );
  12 }
  13 
  14 void a2dObject::ConnectEvent( wxEventType type, wxEvtHandler* evtObject )
  15 {
  16     Connect( type, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
  17 }
  18 
  19 bool a2dObject::DisconnectEvent( wxEventType type, wxEvtHandler* evtObject )
  20 {
  21     return Disconnect( type, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
  22 }
  23 
  24 bool a2dObject::DisconnectEventAll( wxEvtHandler* evtObject )
  25 {
  26     bool succeed = false;
  27     bool once = false;
  28     do
  29     {
  30         succeed = Disconnect( wxEVT_NULL, wxObjectEventFunction( &a2dObject::ProcessConnectedEvent ), 0, evtObject );
  31         once = succeed || once;
  32     }
  33     while ( succeed );
  34     return once;
  35 }

wxArt2D: EventDynamicToStatic (last edited 2011-06-01 22:12:46 by KlaasHolwerda)