Contents Up Previous Next

How events are processed

When an event is received from the windowing system, wxWindows calls wxhelprefwxEvtHandler::ProcessEventwxevthandlerprocessevent on the first event handler object belonging to the window generating the event.

It may be noted that wxWindows' event processing system implements something very close to virtual methods in normal C++, i.e. it is possible to alter the behaviour of a class by overriding its event handling functions. In many cases this works even for changing the behaviour of native controls. For example it is possible to filter out a number of key events sent by the system to a native text control by overriding wxTextCtrl and defining a handler for key events using EVT_KEY_DOWN. This would indeed prevent any key events from being sent to the native control - which might not be what is desired. In this case the event handler function has to call Skip() so as to indicate that the search for the event handler should continue.

To summarize, instead of explicitly calling the base class version as you would have done with C++ virtual functions (i.e. wxTextCtrl::OnChar()), you should instead call wxhelprefwxEvent::Skipwxeventskip.

In practice, this would look like the following if the derived text control only accepts 'a' to 'z' and 'A' to 'Z':

void MyTextCtrl::OnChar(wxKeyEvent& event)
{
    if ( isalpha( event.KeyCode() ) )
    {
       // key code is within legal range. we call event.Skip() so the
       // event can be processed either in the base wxWindows class
       // or the native control.

       event.Skip();
    }
    else
    {
       // illegal key hit. we don't call event.Skip() so the
       // event is not processed anywhere else.

       wxBell();
    }
}
The normal order of event table searching by ProcessEvent is as follows:

  1. If the object is disabled (via a call to wxhelprefwxEvtHandler::SetEvtHandlerEnabledwxevthandlersetevthandlerenabled) the function skips to step (6).
  2. If the object is a wxWindow, ProcessEvent is recursively called on the window's wxhelprefwxValidatorwxvalidator. If this returns TRUE, the function exits.
  3. SearchEventTable is called for this event handler. If this fails, the base class table is tried, and so on until no more tables exist or an appropriate function was found, in which case the function exits.
  4. The search is applied down the entire chain of event handlers (usually the chain has a length of one). If this succeeds, the function exits.
  5. If the object is a wxWindow and the event is a wxCommandEvent, ProcessEvent is recursively applied to the parent window's event handler. If this returns TRUE, the function exits.
  6. Finally, ProcessEvent is called on the wxApp object.

Pay close attention to Step 5. People often overlook or get confused by this powerful feature of the wxWindows event processing system. To put it a different way, events derived either directly or indirectly from wxCommandEvent will travel up the containment hierarchy from child to parent until an event handler is found that doesn't call event.Skip(). Events not derived from wxCommandEvent are sent only to the window they occurred in and then stop.

Typically events that deal with a window as a window (size, motion, paint, mouse, keyboard, etc.) are sent only to the window. Events that have a higher level of meaning and/or are generated by the window itself, (button click, menu select, tree expand, etc.) are command events and are sent up to the parent to see if it is interested in the event.

Note that your application may wish to override ProcessEvent to redirect processing of events. This is done in the document/view framework, for example, to allow event handlers to be defined in the document or view. To test for command events (which will probably be the only events you wish to redirect), you may use wxEvent::IsCommandEvent for efficiency, instead of using the slower run-time type system.

As mentioned above, only command events are recursively applied to the parents event handler. As this quite often causes confusion for users, here is a list of system events which will not get sent to the parent's event handler:

wxhelprefwxEventwxevent The event base class
wxhelprefwxActivateEventwxactivateevent A window or application activation event
wxhelprefwxCloseEventwxcloseevent A close window or end session event
wxhelprefwxEraseEventwxeraseevent An erase background event
wxhelprefwxFocusEventwxfocusevent A window focus event
wxhelprefwxKeyEventwxkeyevent A keypress event
wxhelprefwxIdleEventwxidleevent An idle event
wxhelprefwxInitDialogEventwxinitdialogevent A dialog initialisation event
wxhelprefwxJoystickEventwxjoystickevent A joystick event
wxhelprefwxMenuEventwxmenuevent A menu event
wxhelprefwxMouseEventwxmouseevent A mouse event
wxhelprefwxMoveEventwxmoveevent A move event
wxhelprefwxPaintEventwxpaintevent A paint event
wxhelprefwxQueryLayoutInfoEventwxquerylayoutinfoevent Used to query layout information
wxhelprefwxSizeEventwxsizeevent A size event
wxhelprefwxScrollWinEventwxscrollwinevent A scroll event sent by a scrolled window (not a scroll bar)
wxhelprefwxSysColourChangedEventwxsyscolourchangedevent A system colour change event
wxhelprefwxUpdateUIEventwxupdateuievent A user interface update event

In some cases, it might be desired by the programmer to get a certain number of system events in a parent window, for example all key events sent to, but not used by, the native controls in a dialog. In this case, a special event handler will have to be written that will override ProcessEvent() in order to pass all events (or any selection of them) to the parent window.