]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/eventhandling.h
fix doxygen warning about XHTML syntax
[wxWidgets.git] / docs / doxygen / overviews / eventhandling.h
CommitLineData
a007d249
VZ
1/////////////////////////////////////////////////////////////////////////////\r
2// Name: eventhandling.h\r
3// Purpose: topic overview\r
4// Author: wxWidgets team\r
5// RCS-ID: $Id$\r
6// Licence: wxWindows license\r
7/////////////////////////////////////////////////////////////////////////////\r
8\r
9/**\r
10\r
11@page overview_eventhandling Event Handling\r
12\r
13Classes: wxEvtHandler, wxWindow, wxEvent\r
14\r
15@li @ref overview_eventhandling_introduction\r
16@li @ref overview_eventhandling_eventtables\r
17@li @ref overview_eventhandling_connect\r
18@li @ref overview_eventhandling_processing\r
8319fb52
VZ
19@li @ref overview_eventhandling_propagation\r
20@li @ref overview_eventhandling_virtual\r
a007d249
VZ
21@li @ref overview_eventhandling_prog\r
22@li @ref overview_eventhandling_pluggable\r
23@li @ref overview_eventhandling_winid\r
24@li @ref overview_eventhandling_custom\r
25@li @ref overview_eventhandling_macros\r
26\r
27\r
28<hr>\r
29\r
30\r
31@section overview_eventhandling_introduction Introduction\r
32\r
33There are two principal ways to handle events in wxWidgets. One of them uses\r
34<em>event table</em> macros and allows you to define the connection between events\r
d76259e2 35and their handlers only statically, i.e., during program compilation. The other\r
a007d249 36one uses wxEvtHandler::Connect() call and can be used to connect, and\r
d76259e2
FM
37disconnect, the handlers dynamically, i.e., during run-time depending on some\r
38conditions. It also allows the direct connection of the events of one object to a\r
39handler method in another object. The static event tables can only handle\r
a007d249
VZ
40events in the object where they are defined so using Connect() is more flexible\r
41than using the event tables. On the other hand, event tables are more succinct\r
42and centralize all event handlers connection in one place. You can either\r
d76259e2 43choose a single approach that you find preferable or freely combine both\r
a007d249
VZ
44methods in your program in different classes or even in one and the same class,\r
45although this is probably sufficiently confusing to be a bad idea.\r
46\r
d76259e2
FM
47But before you make this choice, let us discuss these two ways in more\r
48detail. In the next section we provide a short introduction to handling the\r
49events using the event tables. Please see @ref overview_eventhandling_connect\r
a007d249
VZ
50for the discussion of Connect().\r
51\r
52@section overview_eventhandling_eventtables Event Handling with Event Tables\r
53\r
54To use an <em>event table</em> you must first decide in which class you wish to\r
55handle the events. The only requirement imposed by wxWidgets is that this class\r
56must derive from wxEvtHandler and so, considering that wxWindow derives from\r
57it, any classes representing windows can handle events. Simple events such as\r
58menu commands are usually processed at the level of a top-level window\r
59containing the menu, so let's suppose that you need to handle some events in @c\r
60MyFrame class deriving from wxFrame.\r
61\r
d76259e2
FM
62First define one or more <em>event handlers</em>. They\r
63are just simple (non-virtual) methods of the class that take as a parameter a\r
64reference to an object of a wxEvent-derived class and have no return value (any\r
a007d249
VZ
65return information is passed via the argument, which is why it is non-const).\r
66You also need to insert a macro\r
67\r
68@code\r
69DECLARE_EVENT_TABLE()\r
70@endcode\r
71\r
d76259e2
FM
72somewhere in the class declaration. It doesn't matter where it appears but\r
73it's customary to put it at the end because the macro changes the access\r
74type internally so it's safest if nothing follows it. The\r
a007d249
VZ
75full class declaration might look like this:\r
76\r
77@code\r
78class MyFrame : public wxFrame\r
79{\r
80public:\r
81 MyFrame(...) : wxFrame(...) { }\r
82\r
83 ...\r
84\r
85protected:\r
86 int m_whatever;\r
87\r
88private:\r
d76259e2
FM
89 // Notice that as the event handlers normally are not called from outside\r
90 // the class, they normally are private. In particular they don't need\r
91 // to be public.\r
a007d249
VZ
92 void OnExit(wxCommandEvent& event);\r
93 void OnButton1(wxCommandEvent& event);\r
94 void OnSize(wxSizeEvent& event);\r
95\r
96 // it's common to call the event handlers OnSomething() but there is no\r
d76259e2 97 // obligation to do that; this one is an event handler too:\r
a007d249
VZ
98 void DoTest(wxCommandEvent& event);\r
99\r
100 DECLARE_EVENT_TABLE()\r
101};\r
102@endcode\r
103\r
d76259e2
FM
104Next the event table must be defined and, as with any definition, it must be\r
105placed in an implementation file. The event table tells wxWidgets how to map\r
a007d249
VZ
106events to member functions and in our example it could look like this:\r
107\r
108@code\r
109BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
110 EVT_MENU(wxID_EXIT, MyFrame::OnExit)\r
111 EVT_MENU(DO_TEST, MyFrame::DoTest)\r
112 EVT_SIZE(MyFrame::OnSize)\r
113 EVT_BUTTON(BUTTON1, MyFrame::OnButton1)\r
114END_EVENT_TABLE()\r
115@endcode\r
116\r
117Notice that you must mention a method you want to use for the event handling in\r
d76259e2 118the event table definition; just defining it in MyFrame class is @e not enough.\r
a007d249
VZ
119\r
120Let us now look at the details of this definition: the first line means that we\r
121are defining the event table for MyFrame class and that its base class is\r
d76259e2 122wxFrame, so events not processed by MyFrame will, by default, be handled by\r
a007d249
VZ
123wxFrame. The next four lines define connections of individual events to their\r
124handlers: the first two of them map menu commands from the items with the\r
125identifiers specified as the first macro parameter to two different member\r
126functions. In the next one, @c EVT_SIZE means that any changes in the size of\r
127the frame will result in calling OnSize() method. Note that this macro doesn't\r
128need a window identifier, since normally you are only interested in the current\r
129window's size events.\r
130\r
6496345c 131The @c EVT_BUTTON macro demonstrates that the originating event does not have to\r
a007d249
VZ
132come from the window class implementing the event table -- if the event source\r
133is a button within a panel within a frame, this will still work, because event\r
d76259e2
FM
134tables are searched up through the hierarchy of windows for the command events.\r
135(But only command events, so you can't catch mouse move events in a child\r
a007d249 136control in the parent window in the same way because wxMouseEvent doesn't\r
d76259e2 137derive from wxCommandEvent. See below for how you can do it.) In this case, the\r
a007d249
VZ
138button's event table will be searched, then the parent panel's, then the\r
139frame's.\r
140\r
141Finally, you need to implement the event handlers. As mentioned before, all\r
142event handlers take a wxEvent-derived argument whose exact class differs\r
143according to the type of event and the class of the originating window. For\r
144size events, wxSizeEvent is used. For menu commands and most control commands\r
d76259e2 145(such as button presses), wxCommandEvent is used. When controls get more\r
a007d249
VZ
146complicated, more specific wxCommandEvent-derived event classes providing\r
147additional control-specific information can be used, such as wxTreeEvent for\r
148events from wxTreeCtrl windows.\r
149\r
150In the simplest possible case an event handler may not use the @c event\r
d76259e2 151parameter at all. For example,\r
a007d249
VZ
152\r
153@code\r
d76259e2 154void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))\r
a007d249
VZ
155{\r
156 // when the user selects "Exit" from the menu we should close\r
157 Close(true);\r
158}\r
159@endcode\r
160\r
161In other cases you may need some information carried by the @c event argument,\r
162as in:\r
163\r
164@code\r
165void MyFrame::OnSize(wxSizeEvent& event)\r
166{\r
167 wxSize size = event.GetSize();\r
168\r
169 ... update the frame using the new size ...\r
170}\r
171@endcode\r
172\r
173You will find the details about the event table macros and the corresponding\r
174wxEvent-derived classes in the discussion of each control generating these\r
175events.\r
176\r
177\r
178@section overview_eventhandling_connect Dynamic Event Handling\r
179\r
d76259e2
FM
180As with the event tables, decide in which class you intend to\r
181handle the events first and, as before, this class must derive from\r
182wxEvtHandler (usually indirectly via wxWindow). See the declaration of MyFrame\r
a007d249 183in the previous section. However the similarities end here and both the syntax\r
d76259e2 184and the possibilities of handling events in this way are rather different.\r
2a638719 185\r
a007d249 186Let us start by looking at the syntax: the first obvious difference is that you\r
d76259e2
FM
187need not use @c DECLARE_EVENT_TABLE() nor @c BEGIN_EVENT_TABLE and the\r
188associated macros. Instead, in any place in your code, but usually in\r
189the code of the class defining the handler itself (and definitely not in the\r
190global scope as with the event tables), call its Connect() method like this:\r
a007d249
VZ
191\r
192@code\r
193MyFrame::MyFrame(...)\r
194{\r
195 Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,\r
196 wxCommandEventHandler(MyFrame::OnExit));\r
197}\r
198@endcode\r
199\r
200This class should be self-explanatory except for wxCommandEventHandler part:\r
d76259e2
FM
201this is a macro that ensures that the method is of the correct type by using\r
202static_cast in the same way as the event table macros.\r
a007d249
VZ
203\r
204Now let us describe the semantic differences:\r
205<ul>\r
206 <li>\r
d76259e2
FM
207 Event handlers can be connected at any moment. For example, it's possible\r
208 to do some initialization first and only connect the handlers if and when\r
209 it succeeds. This can avoid the need to test that the object was properly\r
210 initialized in the event handlers themselves. With Connect() they\r
211 simply won't be called if it wasn't correctly initialized.\r
a007d249
VZ
212 </li>\r
213\r
214 <li>\r
215 As a slight extension of the above, the handlers can also be\r
d76259e2
FM
216 Disconnect()-ed at any time and maybe later reconnected. Of course,\r
217 it's also possible to emulate this behaviour with the classic\r
218 static (i.e., connected via event tables) handlers by using an internal\r
a007d249
VZ
219 flag indicating whether the handler is currently enabled and returning\r
220 from it if it isn't, but using dynamically connected handlers requires\r
221 less code and is also usually more clear.\r
222 </li>\r
223\r
224 <li>\r
225 Also notice that you must derive a class inherited from, say,\r
226 wxTextCtrl even if you don't want to modify the control behaviour at\r
227 all but just want to handle some of its events. This is especially\r
228 inconvenient when the control is loaded from the XRC. Connecting the\r
229 event handler dynamically bypasses the need for this unwanted\r
230 sub-classing.\r
231 </li>\r
232\r
233 <li>\r
234 Last but very, very far from least is the possibility to connect an\r
235 event of some object to a method of another object. This is impossible\r
d76259e2 236 to do with event tables because it is not possible to specify the\r
a007d249
VZ
237 object to dispatch the event to so it necessarily needs to be sent to\r
238 the same object which generated the event. Not so with Connect() which\r
d76259e2 239 has an optional @c eventSink parameter that can be used to specify the\r
a007d249 240 object which will handle the event. Of course, in this case the method\r
d76259e2 241 being connected must belong to the class that is the type of the\r
a007d249
VZ
242 @c eventSink object! To give a quick example, people often want to catch\r
243 mouse movement events happening when the mouse is in one of the frame\r
244 children in the frame itself. Doing it in a naive way doesn't work:\r
245 <ul>\r
246 <li>\r
247 A @c EVT_LEAVE_WINDOW(MyFrame::OnMouseLeave) line in the frame\r
248 event table has no effect as mouse move (including entering and\r
d76259e2 249 leaving) events are not propagated up to the parent window\r
a007d249
VZ
250 (at least not by default).\r
251 </li>\r
252\r
253 <li>\r
254 Putting the same line in a child event table will crash during\r
255 run-time because the MyFrame method will be called on a wrong\r
256 object -- it's easy to convince oneself that the only object\r
d76259e2 257 that can be used here is the pointer to the child, as\r
a007d249
VZ
258 wxWidgets has nothing else. But calling a frame method with the\r
259 child window pointer instead of the pointer to the frame is, of\r
260 course, disastrous.\r
261 </li>\r
262 </ul>\r
263\r
264 However writing\r
265 @code\r
266 MyFrame::MyFrame(...)\r
267 {\r
268 m_child->Connect(wxID_ANY, wxEVT_LEAVE_WINDOW,\r
269 wxMouseEventHandler(MyFrame::OnMouseLeave),\r
270 NULL, // unused extra data parameter\r
271 this); // this indicates the object to connect to\r
272 }\r
4eda9c09 273 @endcode\r
d76259e2
FM
274 will work exactly as expected. Note that you can get the object that\r
275 generated the event -- and that is not the same as the frame -- via\r
4eda9c09
VZ
276 wxEvent::GetEventObject() method of @c event argument passed to the\r
277 event handler.\r
278 </li>\r
a007d249
VZ
279</ul>\r
280\r
281To summarize, using Connect() requires slightly more typing but is much more\r
282flexible than using static event tables so don't hesitate to use it when you\r
283need this extra power. On the other hand, event tables are still perfectly fine\r
284in simple situations where this extra flexibility is not needed.\r
285\r
286\r
287@section overview_eventhandling_processing How Events are Processed\r
288\r
8319fb52 289The previous sections explain how to define event handlers but don't address\r
d76259e2
FM
290the question of how exactly wxWidgets finds the handler to call for the\r
291given event. This section describes the algorithm used in detail.\r
8319fb52 292\r
a007d249 293When an event is received from the windowing system, wxWidgets calls\r
8319fb52
VZ
294wxEvtHandler::ProcessEvent() on the first event handler object belonging to the\r
295window generating the event. The normal order of event table searching by\r
296ProcessEvent() is as follows, with the event processing stopping as soon as a\r
297handler is found (unless the handler calls wxEvent::Skip() in which case it\r
4eda9c09 298doesn't count as having handled the event and the search continues):\r
8319fb52
VZ
299<ol>\r
300 <li value="0">\r
301 Before anything else happens, wxApp::FilterEvent() is called. If it returns\r
302 anything but -1 (default), the event handling stops immediately.\r
303 </li>\r
a007d249 304\r
8319fb52
VZ
305 <li value="1">\r
306 If this event handler is disabled via a call to\r
307 wxEvtHandler::SetEvtHandlerEnabled() the next three steps are skipped and\r
308 the event handler resumes at step (5).\r
d76259e2 309 </li>\r
a007d249 310\r
8319fb52
VZ
311 <li value="2">\r
312 If the object is a wxWindow and has an associated validator, wxValidator\r
313 gets a chance to process the event.\r
314 </li>\r
a007d249 315\r
8319fb52 316 <li value="3">\r
d76259e2 317 The list of dynamically connected event handlers, i.e., those for which\r
8319fb52
VZ
318 Connect() was called, is consulted. Notice that this is done before\r
319 checking the static event table entries, so if both a dynamic and a static\r
320 event handler match the same event, the static one is never going to be\r
321 used.\r
322 </li>\r
a007d249 323\r
8319fb52
VZ
324 <li value="4">\r
325 The event table containing all the handlers defined using the event table\r
326 macros in this class and its base classes is examined. Notice that this\r
327 means that any event handler defined in a base class will be executed at\r
328 this step.\r
329 </li>\r
a007d249 330\r
8319fb52
VZ
331 <li value="5">\r
332 The event is passed to the next event handler, if any, in the event handler\r
d76259e2 333 chain, i.e., the steps (1) to (4) are done for it. This chain can be formed\r
358e9f2f
VZ
334 using wxEvtHandler::SetNextHandler() or wxWindow::PushEventHandler() but\r
335 usually there is no next event handler and chaining event handlers using\r
336 these functions is much less useful now that Connect() exists so this step\r
337 will almost never do anything.\r
8319fb52 338 </li>\r
a007d249 339\r
8319fb52 340 <li value="6">\r
358e9f2f
VZ
341 If the object is a wxWindow and the event is set to propagate (by default\r
342 only wxCommandEvent-derived events are set to propagate), then the\r
8319fb52 343 processing restarts from the step (1) (and excluding the step (7)) for the\r
358e9f2f
VZ
344 parent window. If this object is not a window but the next handler exists,\r
345 the event is passed to its parent if it is a window. This ensures that in a\r
346 common case of (possibly several) non-window event handlers pushed on top\r
347 of a window, the event eventually reaches the window parent.\r
8319fb52
VZ
348 </li>\r
349\r
350 <li value="7">\r
d76259e2 351 Finally, i.e., if the event is still not processed, the wxApp object itself\r
8319fb52
VZ
352 gets a last chance to process it.\r
353 </li>\r
a007d249 354</ol>\r
8319fb52 355\r
4eda9c09 356<em>Please pay close attention to step 6!</em> People often overlook or get\r
8319fb52 357confused by this powerful feature of the wxWidgets event processing system. The\r
d76259e2 358details of event propagation up the window hierarchy are described in the\r
8319fb52
VZ
359next section.\r
360\r
361Also please notice that there are additional steps in the event handling for\r
d76259e2 362the windows-making part of wxWidgets document-view framework, i.e.,\r
8319fb52 363wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame\r
d76259e2 364and wxDocMDIChildFrame. The parent frame classes modify step (2) above to\r
8319fb52
VZ
365send the events received by them to wxDocManager object first. This object, in\r
366turn, sends the event to the current view and the view itself lets its\r
d76259e2 367associated document process the event first. The child frame classes send\r
8319fb52
VZ
368the event directly to the associated view which still forwards it to its\r
369document object. Notice that to avoid remembering the exact order in which the\r
370events are processed in the document-view frame, the simplest, and recommended,\r
d76259e2 371solution is to only handle the events at the view classes level, and not in the\r
8319fb52
VZ
372document or document manager classes\r
373\r
374\r
375@section overview_eventhandling_propagation How Events Propagate Upwards\r
376\r
377As mentioned in the previous section, the events of the classes deriving from\r
378wxCommandEvent are propagated by default to the parent window if they are not\r
379processed in this window itself. But although by default only the command\r
380events are propagated like this, other events can be propagated as well because\r
d76259e2 381the event handling code uses wxEvent::ShouldPropagate() to check whether an\r
8319fb52
VZ
382event should be propagated. It is also possible to propagate the event only a\r
383limited number of times and not until it is processed (or a top level parent\r
384window is reached).\r
a007d249
VZ
385\r
386Finally, there is another additional complication (which, in fact, simplifies\r
387life of wxWidgets programmers significantly): when propagating the command\r
d76259e2
FM
388events up to the parent window, the event propagation stops when it\r
389reaches the parent dialog, if any. This means that you don't risk getting\r
a007d249
VZ
390unexpected events from the dialog controls (which might be left unprocessed by\r
391the dialog itself because it doesn't care about them) when a modal dialog is\r
392popped up. The events do propagate beyond the frames, however. The rationale\r
393for this choice is that there are only a few frames in a typical application\r
394and their parent-child relation are well understood by the programmer while it\r
d76259e2 395may be difficult, if not impossible, to track down all the dialogs that\r
a007d249
VZ
396may be popped up in a complex program (remember that some are created\r
397automatically by wxWidgets). If you need to specify a different behaviour for\r
398some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)\r
399explicitly to prevent the events from being propagated beyond the given window\r
d76259e2 400or unset this flag for the dialogs that have it on by default.\r
a007d249
VZ
401\r
402Typically events that deal with a window as a window (size, motion,\r
403paint, mouse, keyboard, etc.) are sent only to the window. Events\r
d76259e2 404that have a higher level of meaning or are generated by the window\r
a007d249
VZ
405itself, (button click, menu select, tree expand, etc.) are command\r
406events and are sent up to the parent to see if it is interested in the event.\r
407\r
8319fb52
VZ
408As mentioned above, only command events are recursively applied to the parents\r
409event handler in the library itself. As this quite often causes confusion for\r
d76259e2 410users, here is a list of system events that will @em not get sent to the\r
8319fb52 411parent's event handler:\r
a007d249
VZ
412\r
413@li wxEvent: The event base class\r
414@li wxActivateEvent: A window or application activation event\r
415@li wxCloseEvent: A close window or end session event\r
416@li wxEraseEvent: An erase background event\r
417@li wxFocusEvent: A window focus event\r
418@li wxKeyEvent: A keypress event\r
419@li wxIdleEvent: An idle event\r
420@li wxInitDialogEvent: A dialog initialisation event\r
421@li wxJoystickEvent: A joystick event\r
422@li wxMenuEvent: A menu event\r
423@li wxMouseEvent: A mouse event\r
424@li wxMoveEvent: A move event\r
425@li wxPaintEvent: A paint event\r
426@li wxQueryLayoutInfoEvent: Used to query layout information\r
427@li wxSetCursorEvent: Used for special cursor processing based on current mouse position\r
428@li wxSizeEvent: A size event\r
429@li wxScrollWinEvent: A scroll event sent by a scrolled window (not a scroll bar)\r
430@li wxSysColourChangedEvent: A system colour change event\r
431\r
432In some cases, it might be desired by the programmer to get a certain number\r
433of system events in a parent window, for example all key events sent to, but not\r
434used by, the native controls in a dialog. In this case, a special event handler\r
435will have to be written that will override ProcessEvent() in order to pass\r
436all events (or any selection of them) to the parent window.\r
437\r
438\r
8319fb52
VZ
439@section overview_eventhandling_virtual Event Handlers vs Virtual Methods\r
440\r
441It may be noted that wxWidgets' event processing system implements something\r
442close to virtual methods in normal C++ in spirit: both of these mechanisms\r
d76259e2 443allow you to alter the behaviour of the base class by defining the event handling\r
8319fb52
VZ
444functions in the derived classes.\r
445\r
446There is however an important difference between the two mechanisms when you\r
447want to invoke the default behaviour, as implemented by the base class, from a\r
448derived class handler. With the virtual functions, you need to call the base\r
449class function directly and you can do it either in the beginning of the\r
450derived class handler function (to post-process the event) or at its end (to\r
451pre-process the event). With the event handlers, you only have the option of\r
d76259e2 452pre-processing the events and in order to still let the default behaviour\r
8319fb52
VZ
453happen you must call wxEvent::Skip() and @em not call the base class event\r
454handler directly. In fact, the event handler probably doesn't even exist in the\r
455base class as the default behaviour is often implemented in platform-specific\r
456code by the underlying toolkit or OS itself. But even if it does exist at\r
d76259e2 457wxWidgets level, it should never be called directly as the event handlers are\r
8319fb52
VZ
458not part of wxWidgets API and should never be called directly.\r
459\r
460Finally, please notice that the event handlers themselves shouldn't be virtual.\r
461They should always be non-virtual and usually private (as there is no need to\r
462make them public) methods of a wxEvtHandler-derived class.\r
463\r
464\r
a007d249
VZ
465@section overview_eventhandling_prog User Generated Events vs Programmatically Generated Events\r
466\r
467While generically wxEvents can be generated both by user\r
d76259e2
FM
468actions (e.g., resize of a wxWindow) and by calls to functions\r
469(e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r
a007d249
VZ
470events only for the user-generated events. The only @b exceptions to this rule are:\r
471\r
472@li wxNotebook::AddPage: No event-free alternatives\r
473@li wxNotebook::AdvanceSelection: No event-free alternatives\r
474@li wxNotebook::DeletePage: No event-free alternatives\r
475@li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as\r
476 wxNotebook::SetSelection is deprecated\r
477@li wxTreeCtrl::Delete: No event-free alternatives\r
478@li wxTreeCtrl::DeleteAllItems: No event-free alternatives\r
479@li wxTreeCtrl::EditLabel: No event-free alternatives\r
480@li All wxTextCtrl methods\r
481\r
482wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other\r
483functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free\r
484equivalents.\r
485\r
486\r
487\r
488@section overview_eventhandling_pluggable Pluggable Event Handlers\r
489\r
490In fact, you don't have to derive a new class from a window class\r
491if you don't want to. You can derive a new class from wxEvtHandler instead,\r
492defining the appropriate event table, and then call wxWindow::SetEventHandler\r
493(or, preferably, wxWindow::PushEventHandler) to make this\r
494event handler the object that responds to events. This way, you can avoid\r
495a lot of class derivation, and use instances of the same event handler class (but different\r
496objects as the same event handler object shouldn't be used more than once) to\r
497handle events from instances of different widget classes.\r
498\r
499If you ever have to call a window's event handler\r
500manually, use the GetEventHandler function to retrieve the window's event handler and use that\r
501to call the member function. By default, GetEventHandler returns a pointer to the window itself\r
502unless an application has redirected event handling using SetEventHandler or PushEventHandler.\r
503\r
504One use of PushEventHandler is to temporarily or permanently change the\r
505behaviour of the GUI. For example, you might want to invoke a dialog editor\r
506in your application that changes aspects of dialog boxes. You can\r
507grab all the input for an existing dialog box, and edit it 'in situ',\r
508before restoring its behaviour to normal. So even if the application\r
509has derived new classes to customize behaviour, your utility can indulge\r
510in a spot of body-snatching. It could be a useful technique for on-line\r
511tutorials, too, where you take a user through a serious of steps and\r
512don't want them to diverge from the lesson. Here, you can examine the events\r
513coming from buttons and windows, and if acceptable, pass them through to\r
514the original event handler. Use PushEventHandler/PopEventHandler\r
515to form a chain of event handlers, where each handler processes a different\r
516range of events independently from the other handlers.\r
517\r
518\r
519\r
520@section overview_eventhandling_winid Window Identifiers\r
521\r
522Window identifiers are integers, and are used to\r
523uniquely determine window identity in the event system (though you can use it\r
524for other purposes). In fact, identifiers do not need to be unique\r
d76259e2 525across your entire application as long they are unique within the\r
a007d249 526particular context you're interested in, such as a frame and its children. You\r
2a638719
FM
527may use the @c wxID_OK identifier, for example, on any number of dialogs\r
528as long as you don't have several within the same dialog.\r
a007d249
VZ
529\r
530If you pass @c wxID_ANY to a window constructor, an identifier will be\r
531generated for you automatically by wxWidgets. This is useful when you don't\r
532care about the exact identifier either because you're not going to process the\r
2a638719 533events from the control being created or because you process the events\r
a007d249
VZ
534from all controls in one place (in which case you should specify @c wxID_ANY\r
535in the event table or wxEvtHandler::Connect call\r
2a638719 536as well). The automatically generated identifiers are always negative and so\r
a007d249
VZ
537will never conflict with the user-specified identifiers which must be always\r
538positive.\r
539\r
540See @ref page_stdevtid for the list of standard identifiers available.\r
541You can use wxID_HIGHEST to determine the number above which it is safe to\r
542define your own identifiers. Or, you can use identifiers below wxID_LOWEST.\r
2a638719 543Finally, you can allocate identifiers dynamically using wxNewId() function too.\r
a007d249 544If you use wxNewId() consistently in your application, you can be sure that\r
2a638719 545your identifiers don't conflict accidentally.\r
a007d249
VZ
546\r
547\r
548@section overview_eventhandling_custom Custom Event Summary\r
549\r
550@subsection overview_eventhandling_custom_general General approach\r
551\r
2a638719
FM
552Since version 2.2.x of wxWidgets, each event type is identified by an ID\r
553given to the event type @e at runtime that makes it possible to add\r
a007d249 554new event types to the library or application without risking ID clashes\r
c53ab026
FM
555(two different event types mistakingly getting the same event ID).\r
556This event type ID is stored in a struct of type <b>const wxEventType</b>.\r
a007d249
VZ
557\r
558In order to define a new event type, there are principally two choices.\r
2a638719 559One is to define an entirely new event class (typically deriving from\r
c53ab026 560wxEvent or wxCommandEvent).\r
a007d249 561\r
2a638719
FM
562The other is to use the existing event classes and give them a new event\r
563type. You'll have to define and declare a new event type either way\r
564using the following macros:\r
a007d249
VZ
565\r
566@code\r
567// in the header of the source file\r
c53ab026 568extern const wxEventType wxEVT_YOUR_EVENT_NAME;\r
a007d249
VZ
569\r
570// in the implementation\r
c53ab026 571DEFINE_EVENT_TYPE(wxEVT_YOUR_EVENT_NAME)\r
a007d249
VZ
572@endcode\r
573\r
a007d249
VZ
574See also the @ref page_samples_event for an example of code\r
575defining and working with the custom event types.\r
576\r
577\r
578@subsection overview_eventhandling_custom_existing Using Existing Event Classes\r
579\r
2a638719 580If you just want to use a wxCommandEvent with a new event type, use\r
c53ab026
FM
581one of the generic event table macros listed below, without having to define a\r
582new event class yourself. This also has the advantage that you won't have to define a\r
583new wxEvent::Clone() method for posting events between threads etc.\r
584\r
585Example:\r
a007d249
VZ
586\r
587@code\r
c53ab026 588extern const wxEventType wxEVT_MY_EVENT;\r
a007d249
VZ
589DEFINE_EVENT_TYPE(wxEVT_MY_EVENT)\r
590\r
591// user code intercepting the event\r
592\r
593BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
594EVT_MENU (wxID_EXIT, MyFrame::OnExit)\r
595// ....\r
c53ab026 596EVT_COMMAND (ID_MY_WINDOW, wxEVT_MY_EVENT, MyFrame::OnMyEvent)\r
a007d249
VZ
597END_EVENT_TABLE()\r
598\r
c53ab026 599void MyFrame::OnMyEvent( wxCommandEvent& event )\r
a007d249
VZ
600{\r
601 // do something\r
602 wxString text = event.GetText();\r
603}\r
604\r
605\r
606// user code sending the event\r
607\r
608void MyWindow::SendEvent()\r
609{\r
610 wxCommandEvent event( wxEVT_MY_EVENT, GetId() );\r
611 event.SetEventObject( this );\r
c53ab026 612\r
a007d249
VZ
613 // Give it some contents\r
614 event.SetText( wxT("Hallo") );\r
c53ab026 615\r
a007d249
VZ
616 // Send it\r
617 GetEventHandler()->ProcessEvent( event );\r
618}\r
619@endcode\r
620\r
621\r
622@subsection overview_eventhandling_custom_generic Generic Event Table Macros\r
623\r
624@beginTable\r
625@row2col{EVT_CUSTOM(event\, id\, func),\r
626 Allows you to add a custom event table\r
627 entry by specifying the event identifier (such as wxEVT_SIZE),\r
628 the window identifier, and a member function to call.}\r
629@row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func),\r
630 The same as EVT_CUSTOM, but responds to a range of window identifiers.}\r
631@row2col{EVT_COMMAND(id\, event\, func),\r
632 The same as EVT_CUSTOM, but expects a member function with a\r
633 wxCommandEvent argument.}\r
634@row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func),\r
635 The same as EVT_CUSTOM_RANGE, but\r
636 expects a member function with a wxCommandEvent argument.}\r
637@row2col{EVT_NOTIFY(event\, id\, func),\r
638 The same as EVT_CUSTOM, but\r
639 expects a member function with a wxNotifyEvent argument.}\r
640@row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func),\r
641 The same as EVT_CUSTOM_RANGE, but\r
642 expects a member function with a wxNotifyEvent argument.}\r
643@endTable\r
644\r
645\r
646@subsection overview_eventhandling_custom_ownclass Defining Your Own Event Class\r
647\r
d76259e2
FM
648Under certain circumstances, you must define your own event\r
649class e.g., for sending more complex data from one place to another. Apart\r
a007d249
VZ
650from defining your event class, you will also need to define your own\r
651event table macro (which is quite long). Watch out to put in enough\r
652casts to the inherited event function. Here is an example:\r
653\r
654@code\r
655// code defining event\r
656\r
657class wxPlotEvent: public wxNotifyEvent\r
658{\r
659public:\r
660 wxPlotEvent( wxEventType commandType = wxEVT_NULL, int id = 0 );\r
661\r
662 // accessors\r
663 wxPlotCurve *GetCurve()\r
664 { return m_curve; }\r
665\r
666 // required for sending with wxPostEvent()\r
667 virtual wxEvent *Clone() const;\r
668\r
669private:\r
670 wxPlotCurve *m_curve;\r
671};\r
672\r
c53ab026 673extern const wxEventType wxEVT_PLOT_ACTION;\r
a007d249
VZ
674typedef void (wxEvtHandler::*wxPlotEventFunction)(wxPlotEvent&);\r
675\r
c53ab026
FM
676#define wxPlotEventHandler(func) \\r
677 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxPlotEventFunction, &func)\r
a007d249 678#define EVT_PLOT(id, fn) \\r
c53ab026 679 wx__DECLARE_EVT1(wxEVT_PLOT_ACTION, id, wxPlotEventHandler(fn))\r
a007d249
VZ
680\r
681\r
682// code implementing the event type and the event class\r
683\r
684DEFINE_EVENT_TYPE( wxEVT_PLOT_ACTION )\r
685\r
c53ab026
FM
686wxPlotEvent::wxPlotEvent( ... )\r
687{\r
688 ...\r
689}\r
a007d249
VZ
690\r
691\r
692// user code intercepting the event\r
693\r
694BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
695EVT_PLOT (ID_MY_WINDOW, MyFrame::OnPlot)\r
696END_EVENT_TABLE()\r
697\r
698void MyFrame::OnPlot( wxPlotEvent &event )\r
699{\r
700 wxPlotCurve *curve = event.GetCurve();\r
701}\r
702\r
703\r
704// user code sending the event\r
705\r
706void MyWindow::SendEvent()\r
707{\r
708 wxPlotEvent event( wxEVT_PLOT_ACTION, GetId() );\r
709 event.SetEventObject( this );\r
710 event.SetCurve( m_curve );\r
711 GetEventHandler()->ProcessEvent( event );\r
712}\r
713@endcode\r
714\r
715\r
716@section overview_eventhandling_macros Event Handling Summary\r
717\r
718For the full list of event classes, please see the\r
719@ref group_class_events "event classes group page".\r
720\r
721\r
2a638719
FM
722@todo For all controls, state clearly when calling a member function results in\r
723 an event being generated and when it doesn't (possibly updating also the\r
724 'Events generated by the user versus programmatically-generated events'\r
725 paragraph of the 'Event Handling Overview' with the list of the functions\r
726 that break the rule).\r
a007d249
VZ
727\r
728*/\r
729\r