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