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