]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/eventhandling.h
add support for persistent controls
[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
7f853dd0 187need not use DECLARE_EVENT_TABLE() nor BEGIN_EVENT_TABLE() and the\r
d76259e2
FM
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
7f853dd0
FM
334 using wxEvtHandler::SetNextHandler():\r
335 @image html overview_eventhandling_chain.png\r
336 (referring to the image, if @c A->ProcessEvent is called and it doesn't handle\r
337 the event, @c B->ProcessEvent will be called and so on...).\r
338 In the case of wxWindow you can build a stack (implemented using wxEvtHandler\r
339 double-linked list) using wxWindow::PushEventHandler():\r
340 @image html overview_eventhandling_winstack.png\r
341 (referring to the image, if @c W->ProcessEvent is called, it immediately calls\r
342 @c A->ProcessEvent; if nor @c A nor @c B handle the event, then the wxWindow\r
343 itself is used - i.e. the dynamically connected event handlers and static\r
344 event table entries of wxWindow are looked as the last possibility, after\r
345 all pushed event handlers were tested).\r
346 Note however that usually there are no wxEvtHandler chains nor wxWindows stacks\r
347 so this step will usually do anything.\r
8319fb52 348 </li>\r
a007d249 349\r
8319fb52 350 <li value="6">\r
358e9f2f
VZ
351 If the object is a wxWindow and the event is set to propagate (by default\r
352 only wxCommandEvent-derived events are set to propagate), then the\r
8319fb52 353 processing restarts from the step (1) (and excluding the step (7)) for the\r
358e9f2f
VZ
354 parent window. If this object is not a window but the next handler exists,\r
355 the event is passed to its parent if it is a window. This ensures that in a\r
356 common case of (possibly several) non-window event handlers pushed on top\r
357 of a window, the event eventually reaches the window parent.\r
8319fb52
VZ
358 </li>\r
359\r
360 <li value="7">\r
d76259e2 361 Finally, i.e., if the event is still not processed, the wxApp object itself\r
7f853dd0 362 (which derives from wxEvtHandler) gets a last chance to process it.\r
8319fb52 363 </li>\r
a007d249 364</ol>\r
8319fb52 365\r
4eda9c09 366<em>Please pay close attention to step 6!</em> People often overlook or get\r
8319fb52 367confused by this powerful feature of the wxWidgets event processing system. The\r
d76259e2 368details of event propagation up the window hierarchy are described in the\r
8319fb52
VZ
369next section.\r
370\r
371Also please notice that there are additional steps in the event handling for\r
d76259e2 372the windows-making part of wxWidgets document-view framework, i.e.,\r
8319fb52 373wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame\r
d76259e2 374and wxDocMDIChildFrame. The parent frame classes modify step (2) above to\r
8319fb52
VZ
375send the events received by them to wxDocManager object first. This object, in\r
376turn, sends the event to the current view and the view itself lets its\r
d76259e2 377associated document process the event first. The child frame classes send\r
8319fb52
VZ
378the event directly to the associated view which still forwards it to its\r
379document object. Notice that to avoid remembering the exact order in which the\r
380events are processed in the document-view frame, the simplest, and recommended,\r
d76259e2 381solution is to only handle the events at the view classes level, and not in the\r
8319fb52
VZ
382document or document manager classes\r
383\r
384\r
385@section overview_eventhandling_propagation How Events Propagate Upwards\r
386\r
387As mentioned in the previous section, the events of the classes deriving from\r
388wxCommandEvent are propagated by default to the parent window if they are not\r
389processed in this window itself. But although by default only the command\r
390events are propagated like this, other events can be propagated as well because\r
d76259e2 391the event handling code uses wxEvent::ShouldPropagate() to check whether an\r
8319fb52
VZ
392event should be propagated. It is also possible to propagate the event only a\r
393limited number of times and not until it is processed (or a top level parent\r
394window is reached).\r
a007d249
VZ
395\r
396Finally, there is another additional complication (which, in fact, simplifies\r
397life of wxWidgets programmers significantly): when propagating the command\r
d76259e2
FM
398events up to the parent window, the event propagation stops when it\r
399reaches the parent dialog, if any. This means that you don't risk getting\r
a007d249
VZ
400unexpected events from the dialog controls (which might be left unprocessed by\r
401the dialog itself because it doesn't care about them) when a modal dialog is\r
402popped up. The events do propagate beyond the frames, however. The rationale\r
403for this choice is that there are only a few frames in a typical application\r
404and their parent-child relation are well understood by the programmer while it\r
d76259e2 405may be difficult, if not impossible, to track down all the dialogs that\r
a007d249
VZ
406may be popped up in a complex program (remember that some are created\r
407automatically by wxWidgets). If you need to specify a different behaviour for\r
408some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)\r
409explicitly to prevent the events from being propagated beyond the given window\r
d76259e2 410or unset this flag for the dialogs that have it on by default.\r
a007d249
VZ
411\r
412Typically events that deal with a window as a window (size, motion,\r
413paint, mouse, keyboard, etc.) are sent only to the window. Events\r
d76259e2 414that have a higher level of meaning or are generated by the window\r
a007d249
VZ
415itself, (button click, menu select, tree expand, etc.) are command\r
416events and are sent up to the parent to see if it is interested in the event.\r
417\r
8319fb52
VZ
418As mentioned above, only command events are recursively applied to the parents\r
419event handler in the library itself. As this quite often causes confusion for\r
d76259e2 420users, here is a list of system events that will @em not get sent to the\r
8319fb52 421parent's event handler:\r
a007d249
VZ
422\r
423@li wxEvent: The event base class\r
424@li wxActivateEvent: A window or application activation event\r
425@li wxCloseEvent: A close window or end session event\r
426@li wxEraseEvent: An erase background event\r
427@li wxFocusEvent: A window focus event\r
428@li wxKeyEvent: A keypress event\r
429@li wxIdleEvent: An idle event\r
430@li wxInitDialogEvent: A dialog initialisation event\r
431@li wxJoystickEvent: A joystick event\r
432@li wxMenuEvent: A menu event\r
433@li wxMouseEvent: A mouse event\r
434@li wxMoveEvent: A move event\r
435@li wxPaintEvent: A paint event\r
436@li wxQueryLayoutInfoEvent: Used to query layout information\r
437@li wxSetCursorEvent: Used for special cursor processing based on current mouse position\r
438@li wxSizeEvent: A size event\r
439@li wxScrollWinEvent: A scroll event sent by a scrolled window (not a scroll bar)\r
440@li wxSysColourChangedEvent: A system colour change event\r
441\r
442In some cases, it might be desired by the programmer to get a certain number\r
443of system events in a parent window, for example all key events sent to, but not\r
444used by, the native controls in a dialog. In this case, a special event handler\r
445will have to be written that will override ProcessEvent() in order to pass\r
446all events (or any selection of them) to the parent window.\r
447\r
448\r
8319fb52
VZ
449@section overview_eventhandling_virtual Event Handlers vs Virtual Methods\r
450\r
451It may be noted that wxWidgets' event processing system implements something\r
452close to virtual methods in normal C++ in spirit: both of these mechanisms\r
d76259e2 453allow you to alter the behaviour of the base class by defining the event handling\r
8319fb52
VZ
454functions in the derived classes.\r
455\r
456There is however an important difference between the two mechanisms when you\r
457want to invoke the default behaviour, as implemented by the base class, from a\r
458derived class handler. With the virtual functions, you need to call the base\r
459class function directly and you can do it either in the beginning of the\r
460derived class handler function (to post-process the event) or at its end (to\r
461pre-process the event). With the event handlers, you only have the option of\r
d76259e2 462pre-processing the events and in order to still let the default behaviour\r
8319fb52
VZ
463happen you must call wxEvent::Skip() and @em not call the base class event\r
464handler directly. In fact, the event handler probably doesn't even exist in the\r
465base class as the default behaviour is often implemented in platform-specific\r
466code by the underlying toolkit or OS itself. But even if it does exist at\r
d76259e2 467wxWidgets level, it should never be called directly as the event handlers are\r
8319fb52
VZ
468not part of wxWidgets API and should never be called directly.\r
469\r
470Finally, please notice that the event handlers themselves shouldn't be virtual.\r
471They should always be non-virtual and usually private (as there is no need to\r
472make them public) methods of a wxEvtHandler-derived class.\r
473\r
474\r
a007d249
VZ
475@section overview_eventhandling_prog User Generated Events vs Programmatically Generated Events\r
476\r
477While generically wxEvents can be generated both by user\r
d76259e2
FM
478actions (e.g., resize of a wxWindow) and by calls to functions\r
479(e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r
a007d249
VZ
480events only for the user-generated events. The only @b exceptions to this rule are:\r
481\r
482@li wxNotebook::AddPage: No event-free alternatives\r
483@li wxNotebook::AdvanceSelection: No event-free alternatives\r
484@li wxNotebook::DeletePage: No event-free alternatives\r
485@li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as\r
486 wxNotebook::SetSelection is deprecated\r
487@li wxTreeCtrl::Delete: No event-free alternatives\r
488@li wxTreeCtrl::DeleteAllItems: No event-free alternatives\r
489@li wxTreeCtrl::EditLabel: No event-free alternatives\r
490@li All wxTextCtrl methods\r
491\r
492wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other\r
493functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free\r
494equivalents.\r
495\r
496\r
497\r
498@section overview_eventhandling_pluggable Pluggable Event Handlers\r
499\r
500In fact, you don't have to derive a new class from a window class\r
501if you don't want to. You can derive a new class from wxEvtHandler instead,\r
502defining the appropriate event table, and then call wxWindow::SetEventHandler\r
503(or, preferably, wxWindow::PushEventHandler) to make this\r
504event handler the object that responds to events. This way, you can avoid\r
505a lot of class derivation, and use instances of the same event handler class (but different\r
506objects as the same event handler object shouldn't be used more than once) to\r
507handle events from instances of different widget classes.\r
508\r
509If you ever have to call a window's event handler\r
510manually, use the GetEventHandler function to retrieve the window's event handler and use that\r
511to call the member function. By default, GetEventHandler returns a pointer to the window itself\r
512unless an application has redirected event handling using SetEventHandler or PushEventHandler.\r
513\r
514One use of PushEventHandler is to temporarily or permanently change the\r
515behaviour of the GUI. For example, you might want to invoke a dialog editor\r
516in your application that changes aspects of dialog boxes. You can\r
517grab all the input for an existing dialog box, and edit it 'in situ',\r
518before restoring its behaviour to normal. So even if the application\r
519has derived new classes to customize behaviour, your utility can indulge\r
520in a spot of body-snatching. It could be a useful technique for on-line\r
521tutorials, too, where you take a user through a serious of steps and\r
522don't want them to diverge from the lesson. Here, you can examine the events\r
523coming from buttons and windows, and if acceptable, pass them through to\r
524the original event handler. Use PushEventHandler/PopEventHandler\r
525to form a chain of event handlers, where each handler processes a different\r
526range of events independently from the other handlers.\r
527\r
528\r
529\r
530@section overview_eventhandling_winid Window Identifiers\r
531\r
532Window identifiers are integers, and are used to\r
533uniquely determine window identity in the event system (though you can use it\r
534for other purposes). In fact, identifiers do not need to be unique\r
d76259e2 535across your entire application as long they are unique within the\r
a007d249 536particular context you're interested in, such as a frame and its children. You\r
2a638719
FM
537may use the @c wxID_OK identifier, for example, on any number of dialogs\r
538as long as you don't have several within the same dialog.\r
a007d249
VZ
539\r
540If you pass @c wxID_ANY to a window constructor, an identifier will be\r
541generated for you automatically by wxWidgets. This is useful when you don't\r
542care about the exact identifier either because you're not going to process the\r
2a638719 543events from the control being created or because you process the events\r
a007d249
VZ
544from all controls in one place (in which case you should specify @c wxID_ANY\r
545in the event table or wxEvtHandler::Connect call\r
2a638719 546as well). The automatically generated identifiers are always negative and so\r
a007d249
VZ
547will never conflict with the user-specified identifiers which must be always\r
548positive.\r
549\r
550See @ref page_stdevtid for the list of standard identifiers available.\r
551You can use wxID_HIGHEST to determine the number above which it is safe to\r
552define your own identifiers. Or, you can use identifiers below wxID_LOWEST.\r
2a638719 553Finally, you can allocate identifiers dynamically using wxNewId() function too.\r
a007d249 554If you use wxNewId() consistently in your application, you can be sure that\r
2a638719 555your identifiers don't conflict accidentally.\r
a007d249
VZ
556\r
557\r
558@section overview_eventhandling_custom Custom Event Summary\r
559\r
560@subsection overview_eventhandling_custom_general General approach\r
561\r
2a638719
FM
562Since version 2.2.x of wxWidgets, each event type is identified by an ID\r
563given to the event type @e at runtime that makes it possible to add\r
a007d249 564new event types to the library or application without risking ID clashes\r
c53ab026
FM
565(two different event types mistakingly getting the same event ID).\r
566This event type ID is stored in a struct of type <b>const wxEventType</b>.\r
a007d249
VZ
567\r
568In order to define a new event type, there are principally two choices.\r
2a638719 569One is to define an entirely new event class (typically deriving from\r
c53ab026 570wxEvent or wxCommandEvent).\r
a007d249 571\r
2a638719
FM
572The other is to use the existing event classes and give them a new event\r
573type. You'll have to define and declare a new event type either way\r
574using the following macros:\r
a007d249
VZ
575\r
576@code\r
577// in the header of the source file\r
c53ab026 578extern const wxEventType wxEVT_YOUR_EVENT_NAME;\r
a007d249
VZ
579\r
580// in the implementation\r
c53ab026 581DEFINE_EVENT_TYPE(wxEVT_YOUR_EVENT_NAME)\r
a007d249
VZ
582@endcode\r
583\r
a007d249
VZ
584See also the @ref page_samples_event for an example of code\r
585defining and working with the custom event types.\r
586\r
587\r
588@subsection overview_eventhandling_custom_existing Using Existing Event Classes\r
589\r
2a638719 590If you just want to use a wxCommandEvent with a new event type, use\r
c53ab026
FM
591one of the generic event table macros listed below, without having to define a\r
592new event class yourself. This also has the advantage that you won't have to define a\r
593new wxEvent::Clone() method for posting events between threads etc.\r
594\r
595Example:\r
a007d249
VZ
596\r
597@code\r
c53ab026 598extern const wxEventType wxEVT_MY_EVENT;\r
a007d249
VZ
599DEFINE_EVENT_TYPE(wxEVT_MY_EVENT)\r
600\r
601// user code intercepting the event\r
602\r
603BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
604EVT_MENU (wxID_EXIT, MyFrame::OnExit)\r
605// ....\r
c53ab026 606EVT_COMMAND (ID_MY_WINDOW, wxEVT_MY_EVENT, MyFrame::OnMyEvent)\r
a007d249
VZ
607END_EVENT_TABLE()\r
608\r
c53ab026 609void MyFrame::OnMyEvent( wxCommandEvent& event )\r
a007d249
VZ
610{\r
611 // do something\r
612 wxString text = event.GetText();\r
613}\r
614\r
615\r
616// user code sending the event\r
617\r
618void MyWindow::SendEvent()\r
619{\r
620 wxCommandEvent event( wxEVT_MY_EVENT, GetId() );\r
621 event.SetEventObject( this );\r
c53ab026 622\r
a007d249
VZ
623 // Give it some contents\r
624 event.SetText( wxT("Hallo") );\r
c53ab026 625\r
a007d249
VZ
626 // Send it\r
627 GetEventHandler()->ProcessEvent( event );\r
628}\r
629@endcode\r
630\r
631\r
632@subsection overview_eventhandling_custom_generic Generic Event Table Macros\r
633\r
634@beginTable\r
635@row2col{EVT_CUSTOM(event\, id\, func),\r
636 Allows you to add a custom event table\r
637 entry by specifying the event identifier (such as wxEVT_SIZE),\r
638 the window identifier, and a member function to call.}\r
639@row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func),\r
640 The same as EVT_CUSTOM, but responds to a range of window identifiers.}\r
641@row2col{EVT_COMMAND(id\, event\, func),\r
642 The same as EVT_CUSTOM, but expects a member function with a\r
643 wxCommandEvent argument.}\r
644@row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func),\r
645 The same as EVT_CUSTOM_RANGE, but\r
646 expects a member function with a wxCommandEvent argument.}\r
647@row2col{EVT_NOTIFY(event\, id\, func),\r
648 The same as EVT_CUSTOM, but\r
649 expects a member function with a wxNotifyEvent argument.}\r
650@row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func),\r
651 The same as EVT_CUSTOM_RANGE, but\r
652 expects a member function with a wxNotifyEvent argument.}\r
653@endTable\r
654\r
655\r
656@subsection overview_eventhandling_custom_ownclass Defining Your Own Event Class\r
657\r
d76259e2
FM
658Under certain circumstances, you must define your own event\r
659class e.g., for sending more complex data from one place to another. Apart\r
a007d249
VZ
660from defining your event class, you will also need to define your own\r
661event table macro (which is quite long). Watch out to put in enough\r
662casts to the inherited event function. Here is an example:\r
663\r
664@code\r
665// code defining event\r
666\r
667class wxPlotEvent: public wxNotifyEvent\r
668{\r
669public:\r
670 wxPlotEvent( wxEventType commandType = wxEVT_NULL, int id = 0 );\r
671\r
672 // accessors\r
673 wxPlotCurve *GetCurve()\r
674 { return m_curve; }\r
675\r
676 // required for sending with wxPostEvent()\r
677 virtual wxEvent *Clone() const;\r
678\r
679private:\r
680 wxPlotCurve *m_curve;\r
681};\r
682\r
c53ab026 683extern const wxEventType wxEVT_PLOT_ACTION;\r
a007d249
VZ
684typedef void (wxEvtHandler::*wxPlotEventFunction)(wxPlotEvent&);\r
685\r
c53ab026
FM
686#define wxPlotEventHandler(func) \\r
687 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxPlotEventFunction, &func)\r
a007d249 688#define EVT_PLOT(id, fn) \\r
c53ab026 689 wx__DECLARE_EVT1(wxEVT_PLOT_ACTION, id, wxPlotEventHandler(fn))\r
a007d249
VZ
690\r
691\r
692// code implementing the event type and the event class\r
693\r
694DEFINE_EVENT_TYPE( wxEVT_PLOT_ACTION )\r
695\r
c53ab026
FM
696wxPlotEvent::wxPlotEvent( ... )\r
697{\r
698 ...\r
699}\r
a007d249
VZ
700\r
701\r
702// user code intercepting the event\r
703\r
704BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
705EVT_PLOT (ID_MY_WINDOW, MyFrame::OnPlot)\r
706END_EVENT_TABLE()\r
707\r
708void MyFrame::OnPlot( wxPlotEvent &event )\r
709{\r
710 wxPlotCurve *curve = event.GetCurve();\r
711}\r
712\r
713\r
714// user code sending the event\r
715\r
716void MyWindow::SendEvent()\r
717{\r
718 wxPlotEvent event( wxEVT_PLOT_ACTION, GetId() );\r
719 event.SetEventObject( this );\r
720 event.SetCurve( m_curve );\r
721 GetEventHandler()->ProcessEvent( event );\r
722}\r
723@endcode\r
724\r
725\r
726@section overview_eventhandling_macros Event Handling Summary\r
727\r
728For the full list of event classes, please see the\r
729@ref group_class_events "event classes group page".\r
730\r
731\r
2a638719
FM
732@todo For all controls, state clearly when calling a member function results in\r
733 an event being generated and when it doesn't (possibly updating also the\r
734 'Events generated by the user versus programmatically-generated events'\r
735 paragraph of the 'Event Handling Overview' with the list of the functions\r
736 that break the rule).\r
a007d249
VZ
737\r
738*/\r
739\r