]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/eventhandling.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: eventhandling.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
10 @page overview_events Events and Event Handling
12 Related classes: wxEvtHandler, wxWindow, wxEvent
14 @li @ref overview_events_introduction
15 @li @ref overview_events_eventhandling
16 @li @ref overview_events_processing
17 @li @ref overview_events_custom
18 @li @ref overview_events_misc
24 @section overview_events_introduction Introduction to Events
26 Like with all the other GUI frameworks, the control of flow in wxWidgets
27 applications is event-based: the program normally performs most of its actions
28 in response to the events generated by the user. These events can be triggered
29 by using the input devices (such as keyboard, mouse, joystick) directly or,
30 more commonly, by a standard control which synthesizes such input events into
31 higher level events: for example, a wxButton can generate a click event when
32 the user presses the left mouse button on it and then releases it without
33 pressing @c Esc in the meanwhile. There are also events which don't directly
34 correspond to the user actions, such as wxTimerEvent or wxSocketEvent.
36 But in all cases wxWidgets represents these events in a uniform way and allows
37 you to handle them in the same way wherever they originate from. And while the
38 events are normally generated by wxWidgets itself, you can also do this, which
39 is especially useful when using custom events (see @ref overview_events_custom).
41 To be more precise, each event is described by:
42 - <em>Event type</em>: this is simply a value of type wxEventType which
43 uniquely identifies the type of the event. For example, clicking on a button,
44 selecting an item from a list box and pressing a key on the keyboard all
45 generate events with different event types.
46 - <em>Event class</em> carried by the event: each event has some information
47 associated with it and this data is represented by an object of a class
48 derived from wxEvent. Events of different types can use the same event class,
49 for example both button click and listbox selection events use wxCommandEvent
50 class (as do all the other simple control events), but the key press event
51 uses wxKeyEvent as the information associated with it is different.
52 - <em>Event source</em>: wxEvent stores the object which generated the event
53 and, for windows, its identifier (see @ref overview_events_winid). As it is
54 common to have more than one object generating events of the same type (e.g. a
55 typical window contains several buttons, all generating the same button click
56 event), checking the event source object or its id allows to distinguish
60 @section overview_events_eventhandling Event Handling
62 There are two principal ways to handle events in wxWidgets. One of them uses
63 <em>event table</em> macros and allows you to define the connection between events
64 and their handlers only statically, i.e., during program compilation. The other
65 one uses wxEvtHandler::Connect() call and can be used to connect, and
66 disconnect, the handlers dynamically, i.e., during run-time depending on some
67 conditions. It also allows the direct connection of the events of one object to a
68 handler method in another object. The static event tables can only handle
69 events in the object where they are defined so using Connect() is more flexible
70 than using the event tables. On the other hand, event tables are more succinct
71 and centralize all event handlers connection in one place. You can either
72 choose a single approach that you find preferable or freely combine both
73 methods in your program in different classes or even in one and the same class,
74 although this is probably sufficiently confusing to be a bad idea.
76 But before you make this choice, let us discuss these two ways in more
77 detail. In the next section we provide a short introduction to handling the
78 events using the event tables. Please see @ref overview_events_connect
79 for the discussion of Connect().
81 @subsection overview_events_eventtables Event Handling with Event Tables
83 To use an <em>event table</em> you must first decide in which class you wish to
84 handle the events. The only requirement imposed by wxWidgets is that this class
85 must derive from wxEvtHandler and so, considering that wxWindow derives from
86 it, any classes representing windows can handle events. Simple events such as
87 menu commands are usually processed at the level of a top-level window
88 containing the menu, so let's suppose that you need to handle some events in @c
89 MyFrame class deriving from wxFrame.
91 First define one or more <em>event handlers</em>. They
92 are just simple (non-virtual) methods of the class that take as a parameter a
93 reference to an object of a wxEvent-derived class and have no return value (any
94 return information is passed via the argument, which is why it is non-const).
95 You also need to insert a macro
101 somewhere in the class declaration. It doesn't matter where it appears but
102 it's customary to put it at the end because the macro changes the access
103 type internally so it's safest if nothing follows it. The
104 full class declaration might look like this:
107 class MyFrame : public wxFrame
110 MyFrame(...) : wxFrame(...) { }
118 // Notice that as the event handlers normally are not called from outside
119 // the class, they normally are private. In particular they don't need
121 void OnExit(wxCommandEvent& event);
122 void OnButton1(wxCommandEvent& event);
123 void OnSize(wxSizeEvent& event);
125 // it's common to call the event handlers OnSomething() but there is no
126 // obligation to do that; this one is an event handler too:
127 void DoTest(wxCommandEvent& event);
129 DECLARE_EVENT_TABLE()
133 Next the event table must be defined and, as with any definition, it must be
134 placed in an implementation file. The event table tells wxWidgets how to map
135 events to member functions and in our example it could look like this:
138 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
139 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
140 EVT_MENU(DO_TEST, MyFrame::DoTest)
141 EVT_SIZE(MyFrame::OnSize)
142 EVT_BUTTON(BUTTON1, MyFrame::OnButton1)
146 Notice that you must mention a method you want to use for the event handling in
147 the event table definition; just defining it in MyFrame class is @e not enough.
149 Let us now look at the details of this definition: the first line means that we
150 are defining the event table for MyFrame class and that its base class is
151 wxFrame, so events not processed by MyFrame will, by default, be handled by
152 wxFrame. The next four lines define connections of individual events to their
153 handlers: the first two of them map menu commands from the items with the
154 identifiers specified as the first macro parameter to two different member
155 functions. In the next one, @c EVT_SIZE means that any changes in the size of
156 the frame will result in calling OnSize() method. Note that this macro doesn't
157 need a window identifier, since normally you are only interested in the current
158 window's size events.
160 The @c EVT_BUTTON macro demonstrates that the originating event does not have to
161 come from the window class implementing the event table -- if the event source
162 is a button within a panel within a frame, this will still work, because event
163 tables are searched up through the hierarchy of windows for the command events.
164 (But only command events, so you can't catch mouse move events in a child
165 control in the parent window in the same way because wxMouseEvent doesn't
166 derive from wxCommandEvent. See below for how you can do it.) In this case, the
167 button's event table will be searched, then the parent panel's, then the
170 Finally, you need to implement the event handlers. As mentioned before, all
171 event handlers take a wxEvent-derived argument whose exact class differs
172 according to the type of event and the class of the originating window. For
173 size events, wxSizeEvent is used. For menu commands and most control commands
174 (such as button presses), wxCommandEvent is used. When controls get more
175 complicated, more specific wxCommandEvent-derived event classes providing
176 additional control-specific information can be used, such as wxTreeEvent for
177 events from wxTreeCtrl windows.
179 In the simplest possible case an event handler may not use the @c event
180 parameter at all. For example,
183 void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
185 // when the user selects "Exit" from the menu we should close
190 In other cases you may need some information carried by the @c event argument,
194 void MyFrame::OnSize(wxSizeEvent& event)
196 wxSize size = event.GetSize();
198 ... update the frame using the new size ...
202 You will find the details about the event table macros and the corresponding
203 wxEvent-derived classes in the discussion of each control generating these
207 @subsection overview_events_connect Dynamic Event Handling
209 As with the event tables, decide in which class you intend to
210 handle the events first and, as before, this class must derive from
211 wxEvtHandler (usually indirectly via wxWindow). See the declaration of MyFrame
212 in the previous section. However the similarities end here and both the syntax
213 and the possibilities of handling events in this way are rather different.
215 Let us start by looking at the syntax: the first obvious difference is that you
216 need not use DECLARE_EVENT_TABLE() nor BEGIN_EVENT_TABLE() and the
217 associated macros. Instead, in any place in your code, but usually in
218 the code of the class defining the handler itself (and definitely not in the
219 global scope as with the event tables), call its Connect() method like this:
222 MyFrame::MyFrame(...)
224 Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
225 wxCommandEventHandler(MyFrame::OnExit));
229 This class should be self-explanatory except for wxCommandEventHandler part:
230 this is a macro that ensures that the method is of the correct type by using
231 static_cast in the same way as the event table macros.
233 Now let us describe the semantic differences:
236 Event handlers can be connected at any moment. For example, it's possible
237 to do some initialization first and only connect the handlers if and when
238 it succeeds. This can avoid the need to test that the object was properly
239 initialized in the event handlers themselves. With Connect() they
240 simply won't be called if it wasn't correctly initialized.
244 As a slight extension of the above, the handlers can also be
245 Disconnect()-ed at any time and maybe later reconnected. Of course,
246 it's also possible to emulate this behaviour with the classic
247 static (i.e., connected via event tables) handlers by using an internal
248 flag indicating whether the handler is currently enabled and returning
249 from it if it isn't, but using dynamically connected handlers requires
250 less code and is also usually more clear.
254 Also notice that you must derive a class inherited from, say,
255 wxTextCtrl even if you don't want to modify the control behaviour at
256 all but just want to handle some of its events. This is especially
257 inconvenient when the control is loaded from the XRC. Connecting the
258 event handler dynamically bypasses the need for this unwanted
263 Last but very, very far from least is the possibility to connect an
264 event of some object to a method of another object. This is impossible
265 to do with event tables because it is not possible to specify the
266 object to dispatch the event to so it necessarily needs to be sent to
267 the same object which generated the event. Not so with Connect() which
268 has an optional @c eventSink parameter that can be used to specify the
269 object which will handle the event. Of course, in this case the method
270 being connected must belong to the class that is the type of the
271 @c eventSink object! To give a quick example, people often want to catch
272 mouse movement events happening when the mouse is in one of the frame
273 children in the frame itself. Doing it in a naive way doesn't work:
276 A @c EVT_LEAVE_WINDOW(MyFrame::OnMouseLeave) line in the frame
277 event table has no effect as mouse move (including entering and
278 leaving) events are not propagated up to the parent window
279 (at least not by default).
283 Putting the same line in a child event table will crash during
284 run-time because the MyFrame method will be called on a wrong
285 object -- it's easy to convince oneself that the only object
286 that can be used here is the pointer to the child, as
287 wxWidgets has nothing else. But calling a frame method with the
288 child window pointer instead of the pointer to the frame is, of
295 MyFrame::MyFrame(...)
297 m_child->Connect(wxID_ANY, wxEVT_LEAVE_WINDOW,
298 wxMouseEventHandler(MyFrame::OnMouseLeave),
299 NULL, // unused extra data parameter
300 this); // this indicates the object to connect to
303 will work exactly as expected. Note that you can get the object that
304 generated the event -- and that is not the same as the frame -- via
305 wxEvent::GetEventObject() method of @c event argument passed to the
310 To summarize, using Connect() requires slightly more typing but is much more
311 flexible than using static event tables so don't hesitate to use it when you
312 need this extra power. On the other hand, event tables are still perfectly fine
313 in simple situations where this extra flexibility is not needed.
316 @section overview_events_processing How Events are Processed
318 The previous sections explain how to define event handlers but don't address
319 the question of how exactly wxWidgets finds the handler to call for the
320 given event. This section describes the algorithm used in detail.
322 When an event is received from the windowing system, wxWidgets calls
323 wxEvtHandler::ProcessEvent() on the first event handler object belonging to the
324 window generating the event. The normal order of event table searching by
325 ProcessEvent() is as follows, with the event processing stopping as soon as a
326 handler is found (unless the handler calls wxEvent::Skip() in which case it
327 doesn't count as having handled the event and the search continues):
330 Before anything else happens, wxApp::FilterEvent() is called. If it returns
331 anything but -1 (default), the event handling stops immediately.
335 If this event handler is disabled via a call to
336 wxEvtHandler::SetEvtHandlerEnabled() the next three steps are skipped and
337 the event handler resumes at step (5).
341 If the object is a wxWindow and has an associated validator, wxValidator
342 gets a chance to process the event.
346 The list of dynamically connected event handlers, i.e., those for which
347 Connect() was called, is consulted. Notice that this is done before
348 checking the static event table entries, so if both a dynamic and a static
349 event handler match the same event, the static one is never going to be
354 The event table containing all the handlers defined using the event table
355 macros in this class and its base classes is examined. Notice that this
356 means that any event handler defined in a base class will be executed at
361 The event is passed to the next event handler, if any, in the event handler
362 chain, i.e., the steps (1) to (4) are done for it. This chain can be formed
363 using wxEvtHandler::SetNextHandler():
364 @image html overview_events_chain.png
365 (referring to the image, if @c A->ProcessEvent is called and it doesn't handle
366 the event, @c B->ProcessEvent will be called and so on...).
367 In the case of wxWindow you can build a stack (implemented using wxEvtHandler
368 double-linked list) using wxWindow::PushEventHandler():
369 @image html overview_events_winstack.png
370 (referring to the image, if @c W->ProcessEvent is called, it immediately calls
371 @c A->ProcessEvent; if nor @c A nor @c B handle the event, then the wxWindow
372 itself is used - i.e. the dynamically connected event handlers and static
373 event table entries of wxWindow are looked as the last possibility, after
374 all pushed event handlers were tested).
375 Note however that usually there are no wxEvtHandler chains nor wxWindows stacks
376 so this step will usually do anything.
380 If the object is a wxWindow and the event is set to propagate (by default
381 only wxCommandEvent-derived events are set to propagate), then the
382 processing restarts from the step (1) (and excluding the step (7)) for the
383 parent window. If this object is not a window but the next handler exists,
384 the event is passed to its parent if it is a window. This ensures that in a
385 common case of (possibly several) non-window event handlers pushed on top
386 of a window, the event eventually reaches the window parent.
390 Finally, i.e., if the event is still not processed, the wxApp object itself
391 (which derives from wxEvtHandler) gets a last chance to process it.
395 <em>Please pay close attention to step 6!</em> People often overlook or get
396 confused by this powerful feature of the wxWidgets event processing system. The
397 details of event propagation up the window hierarchy are described in the
400 Also please notice that there are additional steps in the event handling for
401 the windows-making part of wxWidgets document-view framework, i.e.,
402 wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame
403 and wxDocMDIChildFrame. The parent frame classes modify step (2) above to
404 send the events received by them to wxDocManager object first. This object, in
405 turn, sends the event to the current view and the view itself lets its
406 associated document process the event first. The child frame classes send
407 the event directly to the associated view which still forwards it to its
408 document object. Notice that to avoid remembering the exact order in which the
409 events are processed in the document-view frame, the simplest, and recommended,
410 solution is to only handle the events at the view classes level, and not in the
411 document or document manager classes
414 @subsection overview_events_propagation How Events Propagate Upwards
416 As mentioned above, the events of the classes deriving from wxCommandEvent are
417 propagated by default to the parent window if they are not processed in this
418 window itself. But although by default only the command events are propagated
419 like this, other events can be propagated as well because the event handling
420 code uses wxEvent::ShouldPropagate() to check whether an event should be
421 propagated. It is also possible to propagate the event only a limited number of
422 times and not until it is processed (or a top level parent window is reached).
424 Finally, there is another additional complication (which, in fact, simplifies
425 life of wxWidgets programmers significantly): when propagating the command
426 events up to the parent window, the event propagation stops when it
427 reaches the parent dialog, if any. This means that you don't risk getting
428 unexpected events from the dialog controls (which might be left unprocessed by
429 the dialog itself because it doesn't care about them) when a modal dialog is
430 popped up. The events do propagate beyond the frames, however. The rationale
431 for this choice is that there are only a few frames in a typical application
432 and their parent-child relation are well understood by the programmer while it
433 may be difficult, if not impossible, to track down all the dialogs that
434 may be popped up in a complex program (remember that some are created
435 automatically by wxWidgets). If you need to specify a different behaviour for
436 some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)
437 explicitly to prevent the events from being propagated beyond the given window
438 or unset this flag for the dialogs that have it on by default.
440 Typically events that deal with a window as a window (size, motion,
441 paint, mouse, keyboard, etc.) are sent only to the window. Events
442 that have a higher level of meaning or are generated by the window
443 itself, (button click, menu select, tree expand, etc.) are command
444 events and are sent up to the parent to see if it is interested in the event.
446 As mentioned above, only command events are recursively applied to the parents
447 event handler in the library itself. As this quite often causes confusion for
448 users, here is a list of system events that will @em not get sent to the
449 parent's event handler:
451 @li wxEvent: The event base class
452 @li wxActivateEvent: A window or application activation event
453 @li wxCloseEvent: A close window or end session event
454 @li wxEraseEvent: An erase background event
455 @li wxFocusEvent: A window focus event
456 @li wxKeyEvent: A keypress event
457 @li wxIdleEvent: An idle event
458 @li wxInitDialogEvent: A dialog initialisation event
459 @li wxJoystickEvent: A joystick event
460 @li wxMenuEvent: A menu event
461 @li wxMouseEvent: A mouse event
462 @li wxMoveEvent: A move event
463 @li wxPaintEvent: A paint event
464 @li wxQueryLayoutInfoEvent: Used to query layout information
465 @li wxSetCursorEvent: Used for special cursor processing based on current mouse position
466 @li wxSizeEvent: A size event
467 @li wxScrollWinEvent: A scroll event sent by a scrolled window (not a scroll bar)
468 @li wxSysColourChangedEvent: A system colour change event
470 In some cases, it might be desired by the programmer to get a certain number
471 of system events in a parent window, for example all key events sent to, but not
472 used by, the native controls in a dialog. In this case, a special event handler
473 will have to be written that will override ProcessEvent() in order to pass
474 all events (or any selection of them) to the parent window.
477 @section overview_events_custom Custom Event Summary
479 @subsection overview_events_custom_general General approach
481 As each event is uniquely defined by its event type, defining a custom event
482 starts with defining a new event type for it. This is done using
483 wxDEFINE_EVENT() macro. As an event type is a variable, it can also be
484 declared using wxDECLARE_EVENT() if necessary.
486 The next thing to do is to decide whether you need to define a custom event
487 class for events of this type or if you can reuse an existing class, typically
488 either wxEvent (which doesn't provide any extra information) or wxCommandEvent
489 (which contains several extra fields and also propagates upwards by default).
490 Both strategies are described in details below. See also the @ref
491 page_samples_event for a complete example of code defining and working with the
495 @subsection overview_events_custom_existing Using Existing Event Classes
497 If you just want to use a wxCommandEvent with a new event type, use one of the
498 generic event table macros listed below, without having to define a new event
504 // this is typically in a header: it just declares MY_EVENT event type
505 wxDECLARE_EVENT(MY_EVENT, wxCommandEvent);
507 // this is a definition so can't be in a header
508 wxDEFINE_EVENT(MY_EVENT, wxCommandEvent);
510 // example of code handling the event with event tables
511 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
512 EVT_MENU (wxID_EXIT, MyFrame::OnExit)
514 EVT_COMMAND (ID_MY_WINDOW, MY_EVENT, MyFrame::OnMyEvent)
517 void MyFrame::OnMyEvent(wxCommandEvent& event)
520 wxString text = event.GetText();
523 // example of code handling the event with Connect():
526 Connect(ID_MY_WINDOW, MY_EVENT, &MyFrame::OnMyEvent);
529 // example of code generating the event
530 void MyWindow::SendEvent()
532 wxCommandEvent event(MY_EVENT, GetId());
533 event.SetEventObject(this);
535 // Give it some contents
536 event.SetText("Hello");
539 ProcessWindowEvent(event);
544 @subsection overview_events_custom_ownclass Defining Your Own Event Class
546 Under certain circumstances, you must define your own event class e.g., for
547 sending more complex data from one place to another. Apart from defining your
548 event class, you also need to define your own event table macro if you want to
549 use event tables for handling events of this type.
554 // define a new event class
555 class MyPlotEvent: public wxEvent
558 MyPlotEvent(wxEventType eventType, int winid, const wxPoint& pos)
559 : wxEvent(winid, eventType),
565 wxPoint GetPoint() const { return m_pos; }
567 // implement the base class pure virtual
568 virtual wxEvent *Clone() const { return new MyPlotEvent(*this); }
574 // we define a single MY_PLOT_CLICKED event type associated with the class
575 // above but typically you are going to have more than one event type, e.g. you
576 // could also have MY_PLOT_ZOOMED or MY_PLOT_PANNED &c -- in which case you
577 // would just add more similar lines here
578 wxDEFINE_EVENT(MY_PLOT_CLICKED, MyPlotEvent);
581 // if you want to support old compilers you need to use some ugly macros:
582 typedef void (wxEvtHandler::*MyPlotEventFunction)(MyPlotEvent&);
583 #define MyPlotEventHandler(func) wxEVENT_HANDLER_CAST(MyPlotEventFunction, func)
585 // if your code is only built sing reasonably modern compilers, you could just
587 #define MyPlotEventHandler(func) (&func)
589 // finally define a macro for creating the event table entries for the new
592 // remember that you don't need this at all if you only use Connect() and that
593 // you can replace MyPlotEventHandler(func) with just &func unless you use a
594 // really old compiler
595 #define MY_EVT_PLOT_CLICK(id, func) \
596 wx__DECLARE_EVT1(MY_PLOT_CLICKED, id, MyPlotEventHandler(func))
599 // example of code handling the event (you will use one of these methods, not
601 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
602 EVT_PLOT(ID_MY_WINDOW, MyFrame::OnPlot)
607 Connect(ID_MY_WINDOW, MY_PLOT_CLICKED, &MyFrame::OnPlot);
610 void MyFrame::OnPlot(MyPlotEvent& event)
612 ... do something with event.GetPoint() ...
616 // example of code generating the event:
617 void MyWindow::SendEvent()
619 MyPlotEvent event(MY_PLOT_CLICKED, GetId(), wxPoint(...));
620 event.SetEventObject(this);
621 ProcessWindowEvent(event);
627 @section overview_events_misc Miscellaneous Notes
629 @subsection overview_events_virtual Event Handlers vs Virtual Methods
631 It may be noted that wxWidgets' event processing system implements something
632 close to virtual methods in normal C++ in spirit: both of these mechanisms
633 allow you to alter the behaviour of the base class by defining the event handling
634 functions in the derived classes.
636 There is however an important difference between the two mechanisms when you
637 want to invoke the default behaviour, as implemented by the base class, from a
638 derived class handler. With the virtual functions, you need to call the base
639 class function directly and you can do it either in the beginning of the
640 derived class handler function (to post-process the event) or at its end (to
641 pre-process the event). With the event handlers, you only have the option of
642 pre-processing the events and in order to still let the default behaviour
643 happen you must call wxEvent::Skip() and @em not call the base class event
644 handler directly. In fact, the event handler probably doesn't even exist in the
645 base class as the default behaviour is often implemented in platform-specific
646 code by the underlying toolkit or OS itself. But even if it does exist at
647 wxWidgets level, it should never be called directly as the event handlers are
648 not part of wxWidgets API and should never be called directly.
650 Finally, please notice that the event handlers themselves shouldn't be virtual.
651 They should always be non-virtual and usually private (as there is no need to
652 make them public) methods of a wxEvtHandler-derived class.
655 @subsection overview_events_prog User Generated Events vs Programmatically Generated Events
657 While generically wxEvents can be generated both by user
658 actions (e.g., resize of a wxWindow) and by calls to functions
659 (e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived
660 events only for the user-generated events. The only @b exceptions to this rule are:
662 @li wxNotebook::AddPage: No event-free alternatives
663 @li wxNotebook::AdvanceSelection: No event-free alternatives
664 @li wxNotebook::DeletePage: No event-free alternatives
665 @li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as
666 wxNotebook::SetSelection is deprecated
667 @li wxTreeCtrl::Delete: No event-free alternatives
668 @li wxTreeCtrl::DeleteAllItems: No event-free alternatives
669 @li wxTreeCtrl::EditLabel: No event-free alternatives
670 @li All wxTextCtrl methods
672 wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other
673 functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free
678 @subsection overview_events_pluggable Pluggable Event Handlers
680 <em>TODO: Probably deprecated, Connect() provides a better way to do this</em>
682 In fact, you don't have to derive a new class from a window class
683 if you don't want to. You can derive a new class from wxEvtHandler instead,
684 defining the appropriate event table, and then call wxWindow::SetEventHandler
685 (or, preferably, wxWindow::PushEventHandler) to make this
686 event handler the object that responds to events. This way, you can avoid
687 a lot of class derivation, and use instances of the same event handler class (but different
688 objects as the same event handler object shouldn't be used more than once) to
689 handle events from instances of different widget classes.
691 If you ever have to call a window's event handler
692 manually, use the GetEventHandler function to retrieve the window's event handler and use that
693 to call the member function. By default, GetEventHandler returns a pointer to the window itself
694 unless an application has redirected event handling using SetEventHandler or PushEventHandler.
696 One use of PushEventHandler is to temporarily or permanently change the
697 behaviour of the GUI. For example, you might want to invoke a dialog editor
698 in your application that changes aspects of dialog boxes. You can
699 grab all the input for an existing dialog box, and edit it 'in situ',
700 before restoring its behaviour to normal. So even if the application
701 has derived new classes to customize behaviour, your utility can indulge
702 in a spot of body-snatching. It could be a useful technique for on-line
703 tutorials, too, where you take a user through a serious of steps and
704 don't want them to diverge from the lesson. Here, you can examine the events
705 coming from buttons and windows, and if acceptable, pass them through to
706 the original event handler. Use PushEventHandler/PopEventHandler
707 to form a chain of event handlers, where each handler processes a different
708 range of events independently from the other handlers.
712 @subsection overview_events_winid Window Identifiers
714 Window identifiers are integers, and are used to
715 uniquely determine window identity in the event system (though you can use it
716 for other purposes). In fact, identifiers do not need to be unique
717 across your entire application as long they are unique within the
718 particular context you're interested in, such as a frame and its children. You
719 may use the @c wxID_OK identifier, for example, on any number of dialogs
720 as long as you don't have several within the same dialog.
722 If you pass @c wxID_ANY to a window constructor, an identifier will be
723 generated for you automatically by wxWidgets. This is useful when you don't
724 care about the exact identifier either because you're not going to process the
725 events from the control being created or because you process the events
726 from all controls in one place (in which case you should specify @c wxID_ANY
727 in the event table or wxEvtHandler::Connect call
728 as well). The automatically generated identifiers are always negative and so
729 will never conflict with the user-specified identifiers which must be always
732 See @ref page_stdevtid for the list of standard identifiers available.
733 You can use wxID_HIGHEST to determine the number above which it is safe to
734 define your own identifiers. Or, you can use identifiers below wxID_LOWEST.
735 Finally, you can allocate identifiers dynamically using wxNewId() function too.
736 If you use wxNewId() consistently in your application, you can be sure that
737 your identifiers don't conflict accidentally.
740 @subsection overview_events_custom_generic Generic Event Table Macros
743 @row2col{EVT_CUSTOM(event\, id\, func),
744 Allows you to add a custom event table
745 entry by specifying the event identifier (such as wxEVT_SIZE),
746 the window identifier, and a member function to call.}
747 @row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func),
748 The same as EVT_CUSTOM, but responds to a range of window identifiers.}
749 @row2col{EVT_COMMAND(id\, event\, func),
750 The same as EVT_CUSTOM, but expects a member function with a
751 wxCommandEvent argument.}
752 @row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func),
753 The same as EVT_CUSTOM_RANGE, but
754 expects a member function with a wxCommandEvent argument.}
755 @row2col{EVT_NOTIFY(event\, id\, func),
756 The same as EVT_CUSTOM, but
757 expects a member function with a wxNotifyEvent argument.}
758 @row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func),
759 The same as EVT_CUSTOM_RANGE, but
760 expects a member function with a wxNotifyEvent argument.}
765 @subsection overview_events_macros Event Handling Summary
767 For the full list of event classes, please see the
768 @ref group_class_events "event classes group page".