+This class should be self-explanatory except for wxCommandEventHandler part:
+this is a macro which ensures that the method is of correct type by using
+static_cast in the same way as event table macros do it inside them.
+
+Now let us describe the semantic differences:
+<ul>
+ <li>
+ Event handlers can be connected at any moment, e.g. it's possible to do
+ some initialization first and only connect the handlers if and when it
+ succeeds. This can avoid the need to test that the object was properly
+ initialized in the event handlers themselves: with Connect() they
+ simply won't be called at all if it wasn't.
+ </li>
+
+ <li>
+ As a slight extension of the above, the handlers can also be
+ Disconnect()-ed at any time. And maybe later reconnected again. Of
+ course, it's also possible to emulate this behaviour with the classic
+ static (i.e. connected via event tables) handlers by using an internal
+ flag indicating whether the handler is currently enabled and returning
+ from it if it isn't, but using dynamically connected handlers requires
+ less code and is also usually more clear.
+ </li>
+
+ <li>
+ Also notice that you must derive a class inherited from, say,
+ wxTextCtrl even if you don't want to modify the control behaviour at
+ all but just want to handle some of its events. This is especially
+ inconvenient when the control is loaded from the XRC. Connecting the
+ event handler dynamically bypasses the need for this unwanted
+ sub-classing.
+ </li>
+
+ <li>
+ Last but very, very far from least is the possibility to connect an
+ event of some object to a method of another object. This is impossible
+ to do with event tables because there is no possibility to specify the
+ object to dispatch the event to so it necessarily needs to be sent to
+ the same object which generated the event. Not so with Connect() which
+ has an optional @c eventSink parameter which can be used to specify the
+ object which will handle the event. Of course, in this case the method
+ being connected must belong to the class which is the type of the
+ @c eventSink object! To give a quick example, people often want to catch
+ mouse movement events happening when the mouse is in one of the frame
+ children in the frame itself. Doing it in a naive way doesn't work:
+ <ul>
+ <li>
+ A @c EVT_LEAVE_WINDOW(MyFrame::OnMouseLeave) line in the frame
+ event table has no effect as mouse move (including entering and
+ leaving) events are not propagated upwards to the parent window
+ (at least not by default).
+ </li>
+
+ <li>
+ Putting the same line in a child event table will crash during
+ run-time because the MyFrame method will be called on a wrong
+ object -- it's easy to convince oneself that the only object
+ which can be used here is the pointer to the child, as
+ wxWidgets has nothing else. But calling a frame method with the
+ child window pointer instead of the pointer to the frame is, of
+ course, disastrous.
+ </li>
+ </ul>
+
+ However writing
+ @code
+ MyFrame::MyFrame(...)
+ {
+ m_child->Connect(wxID_ANY, wxEVT_LEAVE_WINDOW,
+ wxMouseEventHandler(MyFrame::OnMouseLeave),
+ NULL, // unused extra data parameter
+ this); // this indicates the object to connect to
+ }
+ @endcode
+ will work exactly as expected. Note that you can get the object which
+ generated the event -- and which is not the same as the frame -- via
+ wxEvent::GetEventObject() method of @c event argument passed to the
+ event handler.
+ <li>
+</ul>
+
+To summarize, using Connect() requires slightly more typing but is much more
+flexible than using static event tables so don't hesitate to use it when you
+need this extra power. On the other hand, event tables are still perfectly fine
+in simple situations where this extra flexibility is not needed.