]>
Commit | Line | Data |
---|---|---|
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 | |
13 | Related 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 | |
27 | Like with all the other GUI frameworks, the control of flow in wxWidgets\r | |
28 | applications is event-based: the program normally performs most of its actions\r | |
29 | in response to the events generated by the user. These events can be triggered\r | |
30 | by using the input devices (such as keyboard, mouse, joystick) directly or,\r | |
31 | more commonly, by a standard control which synthesizes such input events into\r | |
32 | higher level events: for example, a wxButton can generate a click event when\r | |
33 | the user presses the left mouse button on it and then releases it without\r | |
34 | pressing @c Esc in the meanwhile. There are also events which don't directly\r | |
35 | correspond to the user actions, such as wxTimerEvent or wxSocketEvent.\r | |
36 | \r | |
37 | But in all cases wxWidgets represents these events in a uniform way and allows\r | |
38 | you to handle them in the same way wherever they originate from. And while the\r | |
39 | events are normally generated by wxWidgets itself, you can also do this, which\r | |
40 | is especially useful when using custom events (see @ref overview_events_custom).\r | |
41 | \r | |
42 | To 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 |
63 | There 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 | 65 | and their handlers only statically, i.e., during program compilation. The other\r |
a007d249 | 66 | one uses wxEvtHandler::Connect() call and can be used to connect, and\r |
d76259e2 FM |
67 | disconnect, the handlers dynamically, i.e., during run-time depending on some\r |
68 | conditions. It also allows the direct connection of the events of one object to a\r | |
69 | handler method in another object. The static event tables can only handle\r | |
a007d249 VZ |
70 | events in the object where they are defined so using Connect() is more flexible\r |
71 | than using the event tables. On the other hand, event tables are more succinct\r | |
72 | and centralize all event handlers connection in one place. You can either\r | |
d76259e2 | 73 | choose a single approach that you find preferable or freely combine both\r |
a007d249 VZ |
74 | methods in your program in different classes or even in one and the same class,\r |
75 | although this is probably sufficiently confusing to be a bad idea.\r | |
76 | \r | |
d76259e2 FM |
77 | But before you make this choice, let us discuss these two ways in more\r |
78 | detail. In the next section we provide a short introduction to handling the\r | |
3e083d65 | 79 | events using the event tables. Please see @ref overview_events_connect\r |
a007d249 VZ |
80 | for the discussion of Connect().\r |
81 | \r | |
3e083d65 | 82 | @subsection overview_events_eventtables Event Handling with Event Tables\r |
a007d249 VZ |
83 | \r |
84 | To use an <em>event table</em> you must first decide in which class you wish to\r | |
85 | handle the events. The only requirement imposed by wxWidgets is that this class\r | |
86 | must derive from wxEvtHandler and so, considering that wxWindow derives from\r | |
87 | it, any classes representing windows can handle events. Simple events such as\r | |
88 | menu commands are usually processed at the level of a top-level window\r | |
89 | containing the menu, so let's suppose that you need to handle some events in @c\r | |
90 | MyFrame class deriving from wxFrame.\r | |
91 | \r | |
d76259e2 FM |
92 | First define one or more <em>event handlers</em>. They\r |
93 | are just simple (non-virtual) methods of the class that take as a parameter a\r | |
94 | reference to an object of a wxEvent-derived class and have no return value (any\r | |
a007d249 VZ |
95 | return information is passed via the argument, which is why it is non-const).\r |
96 | You also need to insert a macro\r | |
97 | \r | |
98 | @code\r | |
99 | DECLARE_EVENT_TABLE()\r | |
100 | @endcode\r | |
101 | \r | |
d76259e2 FM |
102 | somewhere in the class declaration. It doesn't matter where it appears but\r |
103 | it's customary to put it at the end because the macro changes the access\r | |
104 | type internally so it's safest if nothing follows it. The\r | |
a007d249 VZ |
105 | full class declaration might look like this:\r |
106 | \r | |
107 | @code\r | |
108 | class MyFrame : public wxFrame\r | |
109 | {\r | |
110 | public:\r | |
111 | MyFrame(...) : wxFrame(...) { }\r | |
112 | \r | |
113 | ...\r | |
114 | \r | |
115 | protected:\r | |
116 | int m_whatever;\r | |
117 | \r | |
118 | private:\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 |
134 | Next the event table must be defined and, as with any definition, it must be\r |
135 | placed in an implementation file. The event table tells wxWidgets how to map\r | |
a007d249 VZ |
136 | events to member functions and in our example it could look like this:\r |
137 | \r | |
138 | @code\r | |
139 | BEGIN_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 | |
144 | END_EVENT_TABLE()\r | |
145 | @endcode\r | |
146 | \r | |
147 | Notice that you must mention a method you want to use for the event handling in\r | |
d76259e2 | 148 | the event table definition; just defining it in MyFrame class is @e not enough.\r |
a007d249 VZ |
149 | \r |
150 | Let us now look at the details of this definition: the first line means that we\r | |
151 | are defining the event table for MyFrame class and that its base class is\r | |
d76259e2 | 152 | wxFrame, so events not processed by MyFrame will, by default, be handled by\r |
a007d249 VZ |
153 | wxFrame. The next four lines define connections of individual events to their\r |
154 | handlers: the first two of them map menu commands from the items with the\r | |
155 | identifiers specified as the first macro parameter to two different member\r | |
156 | functions. In the next one, @c EVT_SIZE means that any changes in the size of\r | |
157 | the frame will result in calling OnSize() method. Note that this macro doesn't\r | |
158 | need a window identifier, since normally you are only interested in the current\r | |
159 | window's size events.\r | |
160 | \r | |
6496345c | 161 | The @c EVT_BUTTON macro demonstrates that the originating event does not have to\r |
a007d249 VZ |
162 | come from the window class implementing the event table -- if the event source\r |
163 | is a button within a panel within a frame, this will still work, because event\r | |
d76259e2 FM |
164 | tables 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 | 166 | control in the parent window in the same way because wxMouseEvent doesn't\r |
d76259e2 | 167 | derive from wxCommandEvent. See below for how you can do it.) In this case, the\r |
a007d249 VZ |
168 | button's event table will be searched, then the parent panel's, then the\r |
169 | frame's.\r | |
170 | \r | |
171 | Finally, you need to implement the event handlers. As mentioned before, all\r | |
172 | event handlers take a wxEvent-derived argument whose exact class differs\r | |
173 | according to the type of event and the class of the originating window. For\r | |
174 | size 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 |
176 | complicated, more specific wxCommandEvent-derived event classes providing\r |
177 | additional control-specific information can be used, such as wxTreeEvent for\r | |
178 | events from wxTreeCtrl windows.\r | |
179 | \r | |
180 | In the simplest possible case an event handler may not use the @c event\r | |
d76259e2 | 181 | parameter at all. For example,\r |
a007d249 VZ |
182 | \r |
183 | @code\r | |
d76259e2 | 184 | void 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 | |
191 | In other cases you may need some information carried by the @c event argument,\r | |
192 | as in:\r | |
193 | \r | |
194 | @code\r | |
195 | void 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 | |
203 | You will find the details about the event table macros and the corresponding\r | |
204 | wxEvent-derived classes in the discussion of each control generating these\r | |
205 | events.\r | |
206 | \r | |
207 | \r | |
3e083d65 | 208 | @subsection overview_events_connect Dynamic Event Handling\r |
a007d249 | 209 | \r |
d76259e2 FM |
210 | As with the event tables, decide in which class you intend to\r |
211 | handle the events first and, as before, this class must derive from\r | |
212 | wxEvtHandler (usually indirectly via wxWindow). See the declaration of MyFrame\r | |
a007d249 | 213 | in the previous section. However the similarities end here and both the syntax\r |
d76259e2 | 214 | and the possibilities of handling events in this way are rather different.\r |
2a638719 | 215 | \r |
a007d249 | 216 | Let us start by looking at the syntax: the first obvious difference is that you\r |
7f853dd0 | 217 | need not use DECLARE_EVENT_TABLE() nor BEGIN_EVENT_TABLE() and the\r |
d76259e2 FM |
218 | associated macros. Instead, in any place in your code, but usually in\r |
219 | the code of the class defining the handler itself (and definitely not in the\r | |
220 | global scope as with the event tables), call its Connect() method like this:\r | |
a007d249 VZ |
221 | \r |
222 | @code\r | |
223 | MyFrame::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 | |
230 | This class should be self-explanatory except for wxCommandEventHandler part:\r | |
d76259e2 FM |
231 | this is a macro that ensures that the method is of the correct type by using\r |
232 | static_cast in the same way as the event table macros.\r | |
a007d249 VZ |
233 | \r |
234 | Now 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 | |
311 | To summarize, using Connect() requires slightly more typing but is much more\r | |
312 | flexible than using static event tables so don't hesitate to use it when you\r | |
313 | need this extra power. On the other hand, event tables are still perfectly fine\r | |
314 | in 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 | 319 | The previous sections explain how to define event handlers but don't address\r |
d76259e2 FM |
320 | the question of how exactly wxWidgets finds the handler to call for the\r |
321 | given event. This section describes the algorithm used in detail.\r | |
8319fb52 | 322 | \r |
a007d249 | 323 | When an event is received from the windowing system, wxWidgets calls\r |
8319fb52 VZ |
324 | wxEvtHandler::ProcessEvent() on the first event handler object belonging to the\r |
325 | window generating the event. The normal order of event table searching by\r | |
326 | ProcessEvent() is as follows, with the event processing stopping as soon as a\r | |
327 | handler is found (unless the handler calls wxEvent::Skip() in which case it\r | |
4eda9c09 | 328 | doesn'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 | 397 | confused by this powerful feature of the wxWidgets event processing system. The\r |
d76259e2 | 398 | details of event propagation up the window hierarchy are described in the\r |
8319fb52 VZ |
399 | next section.\r |
400 | \r | |
401 | Also please notice that there are additional steps in the event handling for\r | |
d76259e2 | 402 | the windows-making part of wxWidgets document-view framework, i.e.,\r |
8319fb52 | 403 | wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame\r |
d76259e2 | 404 | and wxDocMDIChildFrame. The parent frame classes modify step (2) above to\r |
8319fb52 VZ |
405 | send the events received by them to wxDocManager object first. This object, in\r |
406 | turn, sends the event to the current view and the view itself lets its\r | |
d76259e2 | 407 | associated document process the event first. The child frame classes send\r |
8319fb52 VZ |
408 | the event directly to the associated view which still forwards it to its\r |
409 | document object. Notice that to avoid remembering the exact order in which the\r | |
410 | events are processed in the document-view frame, the simplest, and recommended,\r | |
d76259e2 | 411 | solution is to only handle the events at the view classes level, and not in the\r |
8319fb52 VZ |
412 | document 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 |
417 | As mentioned above, the events of the classes deriving from wxCommandEvent are\r |
418 | propagated by default to the parent window if they are not processed in this\r | |
419 | window itself. But although by default only the command events are propagated\r | |
420 | like this, other events can be propagated as well because the event handling\r | |
421 | code uses wxEvent::ShouldPropagate() to check whether an event should be\r | |
422 | propagated. It is also possible to propagate the event only a limited number of\r | |
423 | times and not until it is processed (or a top level parent window is reached).\r | |
a007d249 VZ |
424 | \r |
425 | Finally, there is another additional complication (which, in fact, simplifies\r | |
426 | life of wxWidgets programmers significantly): when propagating the command\r | |
d76259e2 FM |
427 | events up to the parent window, the event propagation stops when it\r |
428 | reaches the parent dialog, if any. This means that you don't risk getting\r | |
a007d249 VZ |
429 | unexpected events from the dialog controls (which might be left unprocessed by\r |
430 | the dialog itself because it doesn't care about them) when a modal dialog is\r | |
431 | popped up. The events do propagate beyond the frames, however. The rationale\r | |
432 | for this choice is that there are only a few frames in a typical application\r | |
433 | and their parent-child relation are well understood by the programmer while it\r | |
d76259e2 | 434 | may be difficult, if not impossible, to track down all the dialogs that\r |
a007d249 VZ |
435 | may be popped up in a complex program (remember that some are created\r |
436 | automatically by wxWidgets). If you need to specify a different behaviour for\r | |
437 | some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)\r | |
438 | explicitly to prevent the events from being propagated beyond the given window\r | |
d76259e2 | 439 | or unset this flag for the dialogs that have it on by default.\r |
a007d249 VZ |
440 | \r |
441 | Typically events that deal with a window as a window (size, motion,\r | |
442 | paint, mouse, keyboard, etc.) are sent only to the window. Events\r | |
d76259e2 | 443 | that have a higher level of meaning or are generated by the window\r |
a007d249 VZ |
444 | itself, (button click, menu select, tree expand, etc.) are command\r |
445 | events and are sent up to the parent to see if it is interested in the event.\r | |
446 | \r | |
8319fb52 VZ |
447 | As mentioned above, only command events are recursively applied to the parents\r |
448 | event handler in the library itself. As this quite often causes confusion for\r | |
d76259e2 | 449 | users, here is a list of system events that will @em not get sent to the\r |
8319fb52 | 450 | parent'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 | |
471 | In some cases, it might be desired by the programmer to get a certain number\r | |
472 | of system events in a parent window, for example all key events sent to, but not\r | |
473 | used by, the native controls in a dialog. In this case, a special event handler\r | |
474 | will have to be written that will override ProcessEvent() in order to pass\r | |
475 | all 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 |
482 | As each event is uniquely defined by its event type, defining a custom event\r |
483 | starts with defining a new event type for it. This is done using\r | |
484 | wxDEFINE_EVENT() macro. As an event type is a variable, it can also be\r | |
485 | declared using wxDECLARE_EVENT() if necessary.\r | |
a007d249 | 486 | \r |
4475b410 VZ |
487 | The next thing to do is to decide whether you need to define a custom event\r |
488 | class for events of this type or if you can reuse an existing class, typically\r | |
489 | either wxEvent (which doesn't provide any extra information) or wxCommandEvent\r | |
490 | (which contains several extra fields and also propagates upwards by default).\r | |
491 | Both strategies are described in details below. See also the @ref\r | |
492 | page_samples_event for a complete example of code defining and working with the\r | |
493 | custom 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 |
498 | If you just want to use a wxCommandEvent with a new event type, use one of the\r |
499 | generic event table macros listed below, without having to define a new event\r | |
500 | class yourself.\r | |
c53ab026 FM |
501 | \r |
502 | Example:\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 |
506 | wxDECLARE_EVENT(MY_EVENT, wxCommandEvent);\r | |
a007d249 | 507 | \r |
4475b410 VZ |
508 | // this is a definition so can't be in a header\r |
509 | wxDEFINE_EVENT(MY_EVENT, wxCommandEvent);\r | |
a007d249 | 510 | \r |
4475b410 | 511 | // example of code handling the event with event tables\r |
a007d249 | 512 | BEGIN_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 |
516 | END_EVENT_TABLE()\r |
517 | \r | |
4475b410 | 518 | void 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 |
525 | MyFrame::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 |
531 | void 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 |
547 | Under certain circumstances, you must define your own event class e.g., for\r |
548 | sending more complex data from one place to another. Apart from defining your\r | |
549 | event class, you also need to define your own event table macro if you want to\r | |
550 | use event tables for handling events of this type.\r | |
a007d249 | 551 | \r |
4475b410 | 552 | Here is an example:\r |
a007d249 | 553 | \r |
4475b410 VZ |
554 | @code\r |
555 | // define a new event class\r | |
556 | class MyPlotEvent: public wxEvent\r | |
a007d249 VZ |
557 | {\r |
558 | public:\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 |
571 | private:\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 | |
579 | wxDEFINE_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 |
583 | typedef 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 | 602 | BEGIN_EVENT_TABLE(MyFrame, wxFrame)\r |
4475b410 | 603 | EVT_PLOT(ID_MY_WINDOW, MyFrame::OnPlot)\r |
a007d249 VZ |
604 | END_EVENT_TABLE()\r |
605 | \r | |
4475b410 | 606 | MyFrame::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 |
611 | void 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 |
618 | void 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 | |
632 | It may be noted that wxWidgets' event processing system implements something\r | |
633 | close to virtual methods in normal C++ in spirit: both of these mechanisms\r | |
634 | allow you to alter the behaviour of the base class by defining the event handling\r | |
635 | functions in the derived classes.\r | |
636 | \r | |
637 | There is however an important difference between the two mechanisms when you\r | |
638 | want to invoke the default behaviour, as implemented by the base class, from a\r | |
639 | derived class handler. With the virtual functions, you need to call the base\r | |
640 | class function directly and you can do it either in the beginning of the\r | |
641 | derived class handler function (to post-process the event) or at its end (to\r | |
642 | pre-process the event). With the event handlers, you only have the option of\r | |
643 | pre-processing the events and in order to still let the default behaviour\r | |
644 | happen you must call wxEvent::Skip() and @em not call the base class event\r | |
645 | handler directly. In fact, the event handler probably doesn't even exist in the\r | |
646 | base class as the default behaviour is often implemented in platform-specific\r | |
647 | code by the underlying toolkit or OS itself. But even if it does exist at\r | |
648 | wxWidgets level, it should never be called directly as the event handlers are\r | |
649 | not part of wxWidgets API and should never be called directly.\r | |
650 | \r | |
651 | Finally, please notice that the event handlers themselves shouldn't be virtual.\r | |
652 | They should always be non-virtual and usually private (as there is no need to\r | |
653 | make 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 | |
658 | While generically wxEvents can be generated both by user\r | |
659 | actions (e.g., resize of a wxWindow) and by calls to functions\r | |
660 | (e.g., wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived\r | |
661 | events 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 | |
673 | wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other\r | |
674 | functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free\r | |
675 | equivalents.\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 | |
683 | In fact, you don't have to derive a new class from a window class\r | |
684 | if you don't want to. You can derive a new class from wxEvtHandler instead,\r | |
685 | defining the appropriate event table, and then call wxWindow::SetEventHandler\r | |
686 | (or, preferably, wxWindow::PushEventHandler) to make this\r | |
687 | event handler the object that responds to events. This way, you can avoid\r | |
688 | a lot of class derivation, and use instances of the same event handler class (but different\r | |
689 | objects as the same event handler object shouldn't be used more than once) to\r | |
690 | handle events from instances of different widget classes.\r | |
691 | \r | |
692 | If you ever have to call a window's event handler\r | |
693 | manually, use the GetEventHandler function to retrieve the window's event handler and use that\r | |
694 | to call the member function. By default, GetEventHandler returns a pointer to the window itself\r | |
695 | unless an application has redirected event handling using SetEventHandler or PushEventHandler.\r | |
696 | \r | |
697 | One use of PushEventHandler is to temporarily or permanently change the\r | |
698 | behaviour of the GUI. For example, you might want to invoke a dialog editor\r | |
699 | in your application that changes aspects of dialog boxes. You can\r | |
700 | grab all the input for an existing dialog box, and edit it 'in situ',\r | |
701 | before restoring its behaviour to normal. So even if the application\r | |
702 | has derived new classes to customize behaviour, your utility can indulge\r | |
703 | in a spot of body-snatching. It could be a useful technique for on-line\r | |
704 | tutorials, too, where you take a user through a serious of steps and\r | |
705 | don't want them to diverge from the lesson. Here, you can examine the events\r | |
706 | coming from buttons and windows, and if acceptable, pass them through to\r | |
707 | the original event handler. Use PushEventHandler/PopEventHandler\r | |
708 | to form a chain of event handlers, where each handler processes a different\r | |
709 | range of events independently from the other handlers.\r | |
710 | \r | |
711 | \r | |
712 | \r | |
713 | @subsection overview_events_winid Window Identifiers\r | |
714 | \r | |
715 | Window identifiers are integers, and are used to\r | |
716 | uniquely determine window identity in the event system (though you can use it\r | |
717 | for other purposes). In fact, identifiers do not need to be unique\r | |
718 | across your entire application as long they are unique within the\r | |
719 | particular context you're interested in, such as a frame and its children. You\r | |
720 | may use the @c wxID_OK identifier, for example, on any number of dialogs\r | |
721 | as long as you don't have several within the same dialog.\r | |
722 | \r | |
723 | If you pass @c wxID_ANY to a window constructor, an identifier will be\r | |
724 | generated for you automatically by wxWidgets. This is useful when you don't\r | |
725 | care about the exact identifier either because you're not going to process the\r | |
726 | events from the control being created or because you process the events\r | |
727 | from all controls in one place (in which case you should specify @c wxID_ANY\r | |
728 | in the event table or wxEvtHandler::Connect call\r | |
729 | as well). The automatically generated identifiers are always negative and so\r | |
730 | will never conflict with the user-specified identifiers which must be always\r | |
731 | positive.\r | |
732 | \r | |
733 | See @ref page_stdevtid for the list of standard identifiers available.\r | |
734 | You can use wxID_HIGHEST to determine the number above which it is safe to\r | |
735 | define your own identifiers. Or, you can use identifiers below wxID_LOWEST.\r | |
736 | Finally, you can allocate identifiers dynamically using wxNewId() function too.\r | |
737 | If you use wxNewId() consistently in your application, you can be sure that\r | |
738 | your 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 |
768 | For 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 |