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