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