"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Event Tables and HandlersThe wxWidgets event processing system is a more flexible mechanism than virtual functions, allowing us to avoid declaring all possible event handlers in a base class, which would be totally impractical as well as inefficient.Every class that derives from wxEvtHandler, including frames, buttons, menus, and even documents, can contain an event table to tell wxWidgets how events are routed to handler functions. All window classes (derived from wxWindow), and the application class, are derived from wxEvtHandler.To create a static event table (one that's created at compile time), you need to
The code to add menu items will be similar to the code in the previous chapter, while code to add an OK button might look like this, in the frame constructor: Here's the event table, allowing the frame to handle menu, button, and size events. When the user clicks on the About or Quit menu items, the event is sent to the frame, and MyFrame's event table tells wxWidgets that a menu event with identifier wxID_ABOUT should be sent to MyFrame::OnAbout, and a menu event with identifier wxID_EXIT should be sent to MyFrame::OnQuit. In other words, these functions will be called with a single argument (in this case, wxCommandEvent) when the event loop processes the events.The EVT_SIZE macro does not take an identifier argument because a size event can only be handled by the object that generated it.The EVT_BUTTON entry will cause OnButtonOK to be called when a button in the frame's window hierarchy with identifier wxID_OK is pressed. This example shows that the event can be processed by a window other than the source of the event. Let's assume the button is a child of MyFrame. When the button is pressed, wxWidgets will check the wxButton class for a suitable handler. When one is not found, it checks the parent of the buttonin this case, a MyFrame instance. The event matches an entry in the event table, so MyFrame::OnButtonOK is called. This search of the window component hierarchy, as well as the inheritance hierarchy, means that you can choose where you handle events. For example, if you are designing a dialog class that must respond to commands such as wxID_OK, but you need to leave the creation of the actual controls to other programmers using your code, you can still define default behavior for the controls as long as they have the expected identifiers.The generation of a button click event and its subsequent matching against an appropriate event table entry is illustrated in Figure 3-1. Two class hierarchies are shown, for wxButton and MyFrame. Each class has its own event table that potentially contains the entry that will match the event. When the user clicks on the OK button, a new wxCommandEvent object is created that contains the identifier (wxID_OK) and the type of event (wxEVT_COMMAND_BUTTON_ CLICKED). Now the event tables are searched using wxEvtHandler::ProcessEvent; all of the wxButton's event table events are tried, then wxControl's, then wxWindow's. If no appropriate entry is found that matches against the event type and identifier, wxWidgets tries the parent of the button and starts searching its event table. It has a matching entry:
Figure 3-1. Processing a button click event![]() |