]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/eventhandling.h
deprecated GetCheckBoxSize(wxWindow *) in favour of GetCheckBoxSize() const (the...
[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
3a567740 444itself (button click, menu select, tree expand, etc.) are command\r
a007d249 445events and are sent up to the parent to see if it is interested in the event.\r
3a567740
FM
446More precisely, as said above, all event classes @b not deriving from wxCommandEvent\r
447(see the wxEvent inheritance map) do @b not propagate upward.\r
a007d249
VZ
448\r
449In some cases, it might be desired by the programmer to get a certain number\r
450of system events in a parent window, for example all key events sent to, but not\r
451used by, the native controls in a dialog. In this case, a special event handler\r
452will have to be written that will override ProcessEvent() in order to pass\r
453all events (or any selection of them) to the parent window.\r
454\r
455\r
3e083d65 456@section overview_events_custom Custom Event Summary\r
a007d249 457\r
3e083d65 458@subsection overview_events_custom_general General approach\r
a007d249 459\r
4475b410
VZ
460As each event is uniquely defined by its event type, defining a custom event\r
461starts with defining a new event type for it. This is done using\r
462wxDEFINE_EVENT() macro. As an event type is a variable, it can also be\r
463declared using wxDECLARE_EVENT() if necessary.\r
a007d249 464\r
4475b410
VZ
465The next thing to do is to decide whether you need to define a custom event\r
466class for events of this type or if you can reuse an existing class, typically\r
467either wxEvent (which doesn't provide any extra information) or wxCommandEvent\r
468(which contains several extra fields and also propagates upwards by default).\r
469Both strategies are described in details below. See also the @ref\r
470page_samples_event for a complete example of code defining and working with the\r
471custom event types.\r
a007d249
VZ
472\r
473\r
3e083d65 474@subsection overview_events_custom_existing Using Existing Event Classes\r
a007d249 475\r
4475b410
VZ
476If you just want to use a wxCommandEvent with a new event type, use one of the\r
477generic event table macros listed below, without having to define a new event\r
478class yourself.\r
c53ab026
FM
479\r
480Example:\r
a007d249
VZ
481\r
482@code\r
4475b410
VZ
483// this is typically in a header: it just declares MY_EVENT event type\r
484wxDECLARE_EVENT(MY_EVENT, wxCommandEvent);\r
a007d249 485\r
4475b410
VZ
486// this is a definition so can't be in a header\r
487wxDEFINE_EVENT(MY_EVENT, wxCommandEvent);\r
a007d249 488\r
4475b410 489// example of code handling the event with event tables\r
a007d249 490BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
4475b410
VZ
491 EVT_MENU (wxID_EXIT, MyFrame::OnExit)\r
492 ...\r
493 EVT_COMMAND (ID_MY_WINDOW, MY_EVENT, MyFrame::OnMyEvent)\r
a007d249
VZ
494END_EVENT_TABLE()\r
495\r
4475b410 496void MyFrame::OnMyEvent(wxCommandEvent& event)\r
a007d249
VZ
497{\r
498 // do something\r
499 wxString text = event.GetText();\r
500}\r
501\r
4475b410
VZ
502// example of code handling the event with Connect():\r
503MyFrame::MyFrame()\r
504{\r
505 Connect(ID_MY_WINDOW, MY_EVENT, &MyFrame::OnMyEvent);\r
506}\r
a007d249 507\r
4475b410 508// example of code generating the event\r
a007d249
VZ
509void MyWindow::SendEvent()\r
510{\r
4475b410
VZ
511 wxCommandEvent event(MY_EVENT, GetId());\r
512 event.SetEventObject(this);\r
c53ab026 513\r
a007d249 514 // Give it some contents\r
4475b410 515 event.SetText("Hello");\r
c53ab026 516\r
4475b410
VZ
517 // Do send it\r
518 ProcessWindowEvent(event);\r
a007d249
VZ
519}\r
520@endcode\r
521\r
522\r
3e083d65 523@subsection overview_events_custom_ownclass Defining Your Own Event Class\r
a007d249 524\r
4475b410
VZ
525Under certain circumstances, you must define your own event class e.g., for\r
526sending more complex data from one place to another. Apart from defining your\r
527event class, you also need to define your own event table macro if you want to\r
528use event tables for handling events of this type.\r
a007d249 529\r
4475b410 530Here is an example:\r
a007d249 531\r
4475b410
VZ
532@code\r
533// define a new event class\r
534class MyPlotEvent: public wxEvent\r
a007d249
VZ
535{\r
536public:\r
4475b410
VZ
537 MyPlotEvent(wxEventType eventType, int winid, const wxPoint& pos)\r
538 : wxEvent(winid, eventType),\r
539 m_pos(pos)\r
540 {\r
541 }\r
a007d249
VZ
542\r
543 // accessors\r
4475b410 544 wxPoint GetPoint() const { return m_pos; }\r
a007d249 545\r
4475b410
VZ
546 // implement the base class pure virtual\r
547 virtual wxEvent *Clone() const { return new MyPlotEvent(*this); }\r
a007d249
VZ
548\r
549private:\r
4475b410 550 const wxPoint m_pos;\r
a007d249
VZ
551};\r
552\r
4475b410
VZ
553// we define a single MY_PLOT_CLICKED event type associated with the class\r
554// above but typically you are going to have more than one event type, e.g. you\r
555// could also have MY_PLOT_ZOOMED or MY_PLOT_PANNED &c -- in which case you\r
556// would just add more similar lines here\r
557wxDEFINE_EVENT(MY_PLOT_CLICKED, MyPlotEvent);\r
a007d249
VZ
558\r
559\r
4475b410
VZ
560// if you want to support old compilers you need to use some ugly macros:\r
561typedef void (wxEvtHandler::*MyPlotEventFunction)(MyPlotEvent&);\r
562#define MyPlotEventHandler(func) wxEVENT_HANDLER_CAST(MyPlotEventFunction, func)\r
a007d249 563\r
4475b410
VZ
564// if your code is only built sing reasonably modern compilers, you could just\r
565// do this instead:\r
566#define MyPlotEventHandler(func) (&func)\r
a007d249 567\r
4475b410
VZ
568// finally define a macro for creating the event table entries for the new\r
569// event type\r
570//\r
571// remember that you don't need this at all if you only use Connect() and that\r
572// you can replace MyPlotEventHandler(func) with just &func unless you use a\r
573// really old compiler\r
574#define MY_EVT_PLOT_CLICK(id, func) \\r
575 wx__DECLARE_EVT1(MY_PLOT_CLICKED, id, MyPlotEventHandler(func))\r
a007d249
VZ
576\r
577\r
4475b410
VZ
578// example of code handling the event (you will use one of these methods, not\r
579// both, of course):\r
a007d249 580BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r
4475b410 581 EVT_PLOT(ID_MY_WINDOW, MyFrame::OnPlot)\r
a007d249
VZ
582END_EVENT_TABLE()\r
583\r
4475b410 584MyFrame::MyFrame()\r
a007d249 585{\r
4475b410 586 Connect(ID_MY_WINDOW, MY_PLOT_CLICKED, &MyFrame::OnPlot);\r
a007d249
VZ
587}\r
588\r
4475b410
VZ
589void MyFrame::OnPlot(MyPlotEvent& event)\r
590{\r
591 ... do something with event.GetPoint() ...\r
592}\r
a007d249 593\r
a007d249 594\r
4475b410 595// example of code generating the event:\r
a007d249
VZ
596void MyWindow::SendEvent()\r
597{\r
4475b410
VZ
598 MyPlotEvent event(MY_PLOT_CLICKED, GetId(), wxPoint(...));\r
599 event.SetEventObject(this);\r
600 ProcessWindowEvent(event);\r
a007d249
VZ
601}\r
602@endcode\r
603\r
604\r
4475b410 605\r
3e083d65
VZ
606@section overview_events_misc Miscellaneous Notes\r
607\r
608@subsection overview_events_virtual Event Handlers vs Virtual Methods\r
609\r
610It may be noted that wxWidgets' event processing system implements something\r
611close to virtual methods in normal C++ in spirit: both of these mechanisms\r
612allow you to alter the behaviour of the base class by defining the event handling\r
613functions in the derived classes.\r
614\r
615There is however an important difference between the two mechanisms when you\r
616want to invoke the default behaviour, as implemented by the base class, from a\r
617derived class handler. With the virtual functions, you need to call the base\r
618class function directly and you can do it either in the beginning of the\r
619derived class handler function (to post-process the event) or at its end (to\r
620pre-process the event). With the event handlers, you only have the option of\r
621pre-processing the events and in order to still let the default behaviour\r
622happen you must call wxEvent::Skip() and @em not call the base class event\r
623handler directly. In fact, the event handler probably doesn't even exist in the\r
624base class as the default behaviour is often implemented in platform-specific\r
625code by the underlying toolkit or OS itself. But even if it does exist at\r
626wxWidgets level, it should never be called directly as the event handlers are\r
627not part of wxWidgets API and should never be called directly.\r
628\r
629Finally, please notice that the event handlers themselves shouldn't be virtual.\r
630They should always be non-virtual and usually private (as there is no need to\r
631make them public) methods of a wxEvtHandler-derived class.\r
632\r
633\r
634@subsection overview_events_prog User Generated Events vs Programmatically Generated Events\r
635\r
636While generically wxEvents can be generated both by user\r
637actions (e.g., resize of a wxWindow) and by calls to functions\r
638(e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r
639events only for the user-generated events. The only @b exceptions to this rule are:\r
640\r
641@li wxNotebook::AddPage: No event-free alternatives\r
642@li wxNotebook::AdvanceSelection: No event-free alternatives\r
643@li wxNotebook::DeletePage: No event-free alternatives\r
644@li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as\r
645 wxNotebook::SetSelection is deprecated\r
646@li wxTreeCtrl::Delete: No event-free alternatives\r
647@li wxTreeCtrl::DeleteAllItems: No event-free alternatives\r
648@li wxTreeCtrl::EditLabel: No event-free alternatives\r
649@li All wxTextCtrl methods\r
650\r
651wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other\r
652functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free\r
653equivalents.\r
654\r
655\r
656\r
657@subsection overview_events_pluggable Pluggable Event Handlers\r
658\r
659<em>TODO: Probably deprecated, Connect() provides a better way to do this</em>\r
660\r
661In fact, you don't have to derive a new class from a window class\r
662if you don't want to. You can derive a new class from wxEvtHandler instead,\r
663defining the appropriate event table, and then call wxWindow::SetEventHandler\r
664(or, preferably, wxWindow::PushEventHandler) to make this\r
665event handler the object that responds to events. This way, you can avoid\r
666a lot of class derivation, and use instances of the same event handler class (but different\r
667objects as the same event handler object shouldn't be used more than once) to\r
668handle events from instances of different widget classes.\r
669\r
670If you ever have to call a window's event handler\r
671manually, use the GetEventHandler function to retrieve the window's event handler and use that\r
672to call the member function. By default, GetEventHandler returns a pointer to the window itself\r
673unless an application has redirected event handling using SetEventHandler or PushEventHandler.\r
674\r
675One use of PushEventHandler is to temporarily or permanently change the\r
676behaviour of the GUI. For example, you might want to invoke a dialog editor\r
677in your application that changes aspects of dialog boxes. You can\r
678grab all the input for an existing dialog box, and edit it 'in situ',\r
679before restoring its behaviour to normal. So even if the application\r
680has derived new classes to customize behaviour, your utility can indulge\r
681in a spot of body-snatching. It could be a useful technique for on-line\r
682tutorials, too, where you take a user through a serious of steps and\r
683don't want them to diverge from the lesson. Here, you can examine the events\r
684coming from buttons and windows, and if acceptable, pass them through to\r
685the original event handler. Use PushEventHandler/PopEventHandler\r
686to form a chain of event handlers, where each handler processes a different\r
687range of events independently from the other handlers.\r
688\r
689\r
690\r
691@subsection overview_events_winid Window Identifiers\r
692\r
693Window identifiers are integers, and are used to\r
694uniquely determine window identity in the event system (though you can use it\r
695for other purposes). In fact, identifiers do not need to be unique\r
696across your entire application as long they are unique within the\r
697particular context you're interested in, such as a frame and its children. You\r
698may use the @c wxID_OK identifier, for example, on any number of dialogs\r
699as long as you don't have several within the same dialog.\r
700\r
701If you pass @c wxID_ANY to a window constructor, an identifier will be\r
702generated for you automatically by wxWidgets. This is useful when you don't\r
703care about the exact identifier either because you're not going to process the\r
704events from the control being created or because you process the events\r
705from all controls in one place (in which case you should specify @c wxID_ANY\r
706in the event table or wxEvtHandler::Connect call\r
707as well). The automatically generated identifiers are always negative and so\r
708will never conflict with the user-specified identifiers which must be always\r
709positive.\r
710\r
711See @ref page_stdevtid for the list of standard identifiers available.\r
712You can use wxID_HIGHEST to determine the number above which it is safe to\r
713define your own identifiers. Or, you can use identifiers below wxID_LOWEST.\r
714Finally, you can allocate identifiers dynamically using wxNewId() function too.\r
715If you use wxNewId() consistently in your application, you can be sure that\r
716your identifiers don't conflict accidentally.\r
717\r
718\r
4475b410
VZ
719@subsection overview_events_custom_generic Generic Event Table Macros\r
720\r
721@beginTable\r
722@row2col{EVT_CUSTOM(event\, id\, func),\r
723 Allows you to add a custom event table\r
724 entry by specifying the event identifier (such as wxEVT_SIZE),\r
725 the window identifier, and a member function to call.}\r
726@row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func),\r
727 The same as EVT_CUSTOM, but responds to a range of window identifiers.}\r
728@row2col{EVT_COMMAND(id\, event\, func),\r
729 The same as EVT_CUSTOM, but expects a member function with a\r
730 wxCommandEvent argument.}\r
731@row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func),\r
732 The same as EVT_CUSTOM_RANGE, but\r
733 expects a member function with a wxCommandEvent argument.}\r
734@row2col{EVT_NOTIFY(event\, id\, func),\r
735 The same as EVT_CUSTOM, but\r
736 expects a member function with a wxNotifyEvent argument.}\r
737@row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func),\r
738 The same as EVT_CUSTOM_RANGE, but\r
739 expects a member function with a wxNotifyEvent argument.}\r
740@endTable\r
741\r
742\r
743\r
3a567740 744@subsection overview_events_list List of wxWidgets events\r
a007d249
VZ
745\r
746For the full list of event classes, please see the\r
747@ref group_class_events "event classes group page".\r
748\r
749\r
a007d249
VZ
750*/\r
751\r