event handling overviewclasses: wxevthandler, wxwindow, wxevent
introduction
introductionbefore version 2.0 of wxwidgets, events were handled by the application either by supplying callback functions, or by overriding virtual member functions such as onsize. from wxwidgets 2.0, event tables are used instead, with a few exceptions. an event table is placed in an implementation file to tell wxwidgets how to map events to member functions. these member functions are not virtual functions, but they are all similar in form: they take a single wxevent-derived argument, and have a void return type. here's an example of an event table.
begin_event_table(myframe, wxframe) evt_menu (wxid_exit, myframe::onexit) evt_menu (do_test, myframe::dotest) evt_size ( myframe::onsize) evt_button (button1, myframe::onbutton1) end_event_table()the first two entries map menu commands to two different member functions. the evt_size macro doesn't need a window identifier, since normally you are only interested in the current window's size events. the evt_button macro demonstrates that the originating event does not have to come from the window class implementing the event table -- if the event source is a button within a panel within a frame, this will still work, because event tables are searched up through the hierarchy of windows for the command events. in this case, the button's event table will be searched, then the parent panel's, then the frame's. as mentioned before, the member functions that handle events do not have to be virtual. indeed, the member functions should not be virtual as the event handler ignores that the functions are virtual, i.e. overriding a virtual member function in a derived class will not have any effect. these member functions take an event argument, and the class of event differs according to the type of event and the class of the originating window. for size events, wxsizeevent is used. for menu commands and most control commands (such as button presses), wxcommandevent is used. when controls get more complicated, then specific event classes are used, such as wxtreeevent for events from wxtreectrl windows. as well as the event table in the implementation file, there must also be a declare_event_table macro somewhere in the class declaration. for example:
finally, if you don't like using macros for static initialization of the event tables you may also use wxevthandler::connect to connect the events to the handlers dynamically, during run-time. see the event sample for an example of doing it.
how events are processedwhen an event is received from the windowing system, wxwidgets calls wxevthandler::processevent on the first event handler object belonging to the window generating the event. it may be noted that wxwidgets' 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 skip. in practice, this would look like this if the derived text control only accepts 'a' to 'z' and 'a' to 'z':
pay close attention to step 5. people often overlook or get confused by this powerful feature of the wxwidgets event processing system. to put it a different way, events set to propagate (see: wxevent::shouldpropagate) (most likely derived either directly or indirectly from wxcommandevent) will travel up the containment hierarchy from child to parent until the maximal propagation level is reached or an event handler is found that doesn't call event.skip(). finally, there is another additional complication (which, in fact, simplifies life of wxwidgets programmers significantly): when propagating the command events upwards to the parent window, the event propagation stops when it reaches the parent dialog, if any. this means that you don't risk to get unexpected events from the dialog controls (which might be left unprocessed by the dialog itself because it doesn't care about them) when a modal dialog is popped up. the events do propagate beyond the frames, however. the rationale for this choice is that there are only a few frames in a typical application and their parent-child relation are well understood by the programmer while it may be very difficult, if not impossible, to track down all the dialogs which may be popped up in a complex program (remember that some are created automatically by wxwidgets). if you need to specify a different behaviour for some reason, you can use setextrastyle(wxws_ex_block_events) explicitly to prevent the events from being propagated beyond the given window or unset this flag for the dialogs which have it on by default. 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 in the library itself. 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:
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.
events generated by the user vs programmatically generated eventswhile generically wxevents can be generated both by user actions (e.g. resize of a wxwindow) and by calls to functions (e.g. wxwindow::setsize), wxwidgets controls normally send wxcommandevent-derived events only for the user-generated events. the only exceptions to this rule are:
pluggable event handlersin fact, you don't have to derive a new class from a window class if you don't want to. you can derive a new class from wxevthandler instead, defining the appropriate event table, and then call wxwindow::seteventhandler (or, preferably, wxwindow::pusheventhandler) to make this event handler the object that responds to events. this way, you can avoid a lot of class derivation, and use the same event handler object to handle events from instances of different classes. if you ever have to call a window's event handler manually, use the geteventhandler function to retrieve the window's event handler and use that to call the member function. by default, geteventhandler returns a pointer to the window itself unless an application has redirected event handling using seteventhandler or pusheventhandler. one use of pusheventhandler is to temporarily or permanently change the behaviour of the gui. for example, you might want to invoke a dialog editor in your application that changes aspects of dialog boxes. you can grab all the input for an existing dialog box, and edit it 'in situ', before restoring its behaviour to normal. so even if the application has derived new classes to customize behaviour, your utility can indulge in a spot of body-snatching. it could be a useful technique for on-line tutorials, too, where you take a user through a serious of steps and don't want them to diverge from the lesson. here, you can examine the events coming from buttons and windows, and if acceptable, pass them through to the original event handler. use pusheventhandler/popeventhandler to form a chain of event handlers, where each handler processes a different range of events independently from the other handlers.
window identifierswindow identifiers are integers, and are used to uniquely determine window identity in the event system (though you can use it for other purposes). in fact, identifiers do not need to be unique across your entire application just so long as they are unique within a particular context you're interested in, such as a frame and its children. you may use the wxid_ok identifier, for example, on any number of dialogs so long as you don't have several within the same dialog. if you pass wxid_any to a window constructor, an identifier will be generated for you automatically by wxwidgets. this is useful when you don't care about the exact identifier either because you're not going to process the events from the control being created at all or because you process the events from all controls in one place (in which case you should specify wxid_any in the event table or wxevthandler::connect call as well. the automatically generated identifiers are always negative and so will never conflict with the user-specified identifiers which must be always positive. the following standard identifiers are supplied. you can use wxid_highest to determine the number above which it is safe to define your own identifiers. or, you can use identifiers below wxid_lowest.
#define wxid_any -1 #define wxid_lowest 4999 #define wxid_open 5000 #define wxid_close 5001 #define wxid_new 5002 #define wxid_save 5003 #define wxid_saveas 5004 #define wxid_revert 5005 #define wxid_exit 5006 #define wxid_undo 5007 #define wxid_redo 5008 #define wxid_help 5009 #define wxid_print 5010 #define wxid_print_setup 5011 #define wxid_preview 5012 #define wxid_about 5013 #define wxid_help_contents 5014 #define wxid_help_commands 5015 #define wxid_help_procedures 5016 #define wxid_help_context 5017 #define wxid_cut 5030 #define wxid_copy 5031 #define wxid_paste 5032 #define wxid_clear 5033 #define wxid_find 5034 #define wxid_duplicate 5035 #define wxid_selectall 5036 #define wxid_delete 5037 #define wxid_replace 5038 #define wxid_replace_all 5039 #define wxid_properties 5040 #define wxid_view_details 5041 #define wxid_view_largeicons 5042 #define wxid_view_smallicons 5043 #define wxid_view_list 5044 #define wxid_view_sortdate 5045 #define wxid_view_sortname 5046 #define wxid_view_sortsize 5047 #define wxid_view_sorttype 5048 #define wxid_file1 5050 #define wxid_file2 5051 #define wxid_file3 5052 #define wxid_file4 5053 #define wxid_file5 5054 #define wxid_file6 5055 #define wxid_file7 5056 #define wxid_file8 5057 #define wxid_file9 5058 #define wxid_ok 5100 #define wxid_cancel 5101 #define wxid_apply 5102 #define wxid_yes 5103 #define wxid_no 5104 #define wxid_static 5105 #define wxid_highest 5999 event macros summarymacros listed by event class the documentation for specific event macros is organised by event class. please refer to these sections for details.
custom event summarygeneral approach since version 2.2.x of wxwidgets, each event type is identified by id which is given to the event type at runtime which makes it possible to add new event types to the library or application without risking id clashes (two different event types mistakingly getting the same event id). this event type id is stored in a struct of type const wxeventtype. in order to define a new event type, there are principally two choices. one is to define a entirely new event class (typically deriving from wxevent or wxcommandevent. the other is to use the existing event classes and give them an new event type. you'll have to define and declare a new event type using either way, and this is done using the following macros:
// in the header of the source file begin_declare_event_types() declare_event_type(name, value) end_declare_event_types() // in the implementation define_event_type(name)you can ignore the value parameter of the declare_event_type macro since it used only for backwards compatibility with wxwidgets 2.0.x based applications where you have to give the event type id an explicit value. see also the event sample for an example of code defining and working with the custom event types. using existing event classes if you just want to use a wxcommandevent with a new event type, you can then use one of the generic event table macros listed below, without having to define a new macro yourself. this also has the advantage that you won't have to define a new wxevent::clone() method for posting events between threads etc. this could look like this in your code:
defining your own event class under certain circumstances, it will be required to define your own event class e.g. for sending more complex data from one place to another. apart from defining your event class, you will also need to define your own event table macro (which is quite long). watch out to put in enough casts to the inherited event function. here is an example, taken mostly from the wxplot library, which is in the contrib section of the wxwidgets sources.
|