no changes, just some markup fixes
[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. This chain can be formed using wxEvtHandler::SetNextHandler() or
336 wxWindow::PushEventHandler() but usually there is no next event handler and
337 chaining event handlers using these functions is much less useful now that
338 Connect() exists so this step will almost never do anything.
339 </li>
340
341 <li value="6">
342 If the object is a wxWindow and the event is set to set to propagate (by
343 default only wxCommandEvent-derived events are set to propagate), then the
344 processing restarts from the step (1) (and excluding the step (7)) for the
345 parent window.
346 </li>
347
348 <li value="7">
349 Finally, i.e. if the event is still not processed, the wxApp object itself
350 gets a last chance to process it.
351 </li>
352 </ol>
353
354 <em>Please pay close attention to step 6!</em> People often overlook or get
355 confused by this powerful feature of the wxWidgets event processing system. The
356 details of event propagation upwards the window hierarchy are described in the
357 next section.
358
359 Also please notice that there are additional steps in the event handling for
360 the windows making part of wxWidgets document-view framework, i.e.
361 wxDocParentFrame, wxDocChildFrame and their MDI equivalents wxDocMDIParentFrame
362 and wxDocMDIChildFrame. The parent frame classes modify the step (2) above to
363 send the events received by them to wxDocManager object first. This object, in
364 turn, sends the event to the current view and the view itself lets its
365 associated document to process the event first. The child frame classes send
366 the event directly to the associated view which still forwards it to its
367 document object. Notice that to avoid remembering the exact order in which the
368 events are processed in the document-view frame, the simplest, and recommended,
369 solution is to only handle the events at the view classes level, but not in the
370 document or document manager classes
371
372
373 @section overview_eventhandling_propagation How Events Propagate Upwards
374
375 As mentioned in the previous section, the events of the classes deriving from
376 wxCommandEvent are propagated by default to the parent window if they are not
377 processed in this window itself. But although by default only the command
378 events are propagated like this, other events can be propagated as well because
379 the event handling code uses wxEvent::ShouldPropagate() to check for whether an
380 event should be propagated. It is also possible to propagate the event only a
381 limited number of times and not until it is processed (or a top level parent
382 window is reached).
383
384 Finally, there is another additional complication (which, in fact, simplifies
385 life of wxWidgets programmers significantly): when propagating the command
386 events upwards to the parent window, the event propagation stops when it
387 reaches the parent dialog, if any. This means that you don't risk to get
388 unexpected events from the dialog controls (which might be left unprocessed by
389 the dialog itself because it doesn't care about them) when a modal dialog is
390 popped up. The events do propagate beyond the frames, however. The rationale
391 for this choice is that there are only a few frames in a typical application
392 and their parent-child relation are well understood by the programmer while it
393 may be very difficult, if not impossible, to track down all the dialogs which
394 may be popped up in a complex program (remember that some are created
395 automatically by wxWidgets). If you need to specify a different behaviour for
396 some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS)
397 explicitly to prevent the events from being propagated beyond the given window
398 or unset this flag for the dialogs which have it on by default.
399
400 Typically events that deal with a window as a window (size, motion,
401 paint, mouse, keyboard, etc.) are sent only to the window. Events
402 that have a higher level of meaning and/or are generated by the window
403 itself, (button click, menu select, tree expand, etc.) are command
404 events and are sent up to the parent to see if it is interested in the event.
405
406 As mentioned above, only command events are recursively applied to the parents
407 event handler in the library itself. As this quite often causes confusion for
408 users, here is a list of system events which will @em not get sent to the
409 parent's event handler:
410
411 @li wxEvent: The event base class
412 @li wxActivateEvent: A window or application activation event
413 @li wxCloseEvent: A close window or end session event
414 @li wxEraseEvent: An erase background event
415 @li wxFocusEvent: A window focus event
416 @li wxKeyEvent: A keypress event
417 @li wxIdleEvent: An idle event
418 @li wxInitDialogEvent: A dialog initialisation event
419 @li wxJoystickEvent: A joystick event
420 @li wxMenuEvent: A menu event
421 @li wxMouseEvent: A mouse event
422 @li wxMoveEvent: A move event
423 @li wxPaintEvent: A paint event
424 @li wxQueryLayoutInfoEvent: Used to query layout information
425 @li wxSetCursorEvent: Used for special cursor processing based on current mouse position
426 @li wxSizeEvent: A size event
427 @li wxScrollWinEvent: A scroll event sent by a scrolled window (not a scroll bar)
428 @li wxSysColourChangedEvent: A system colour change event
429
430 In some cases, it might be desired by the programmer to get a certain number
431 of system events in a parent window, for example all key events sent to, but not
432 used by, the native controls in a dialog. In this case, a special event handler
433 will have to be written that will override ProcessEvent() in order to pass
434 all events (or any selection of them) to the parent window.
435
436
437 @section overview_eventhandling_virtual Event Handlers vs Virtual Methods
438
439 It may be noted that wxWidgets' event processing system implements something
440 close to virtual methods in normal C++ in spirit: both of these mechanisms
441 allow to alter the behaviour of the base class by defining the event handling
442 functions in the derived classes.
443
444 There is however an important difference between the two mechanisms when you
445 want to invoke the default behaviour, as implemented by the base class, from a
446 derived class handler. With the virtual functions, you need to call the base
447 class function directly and you can do it either in the beginning of the
448 derived class handler function (to post-process the event) or at its end (to
449 pre-process the event). With the event handlers, you only have the option of
450 pre-processing the events and in order to still let the default behaviour to
451 happen you must call wxEvent::Skip() and @em not call the base class event
452 handler directly. In fact, the event handler probably doesn't even exist in the
453 base class as the default behaviour is often implemented in platform-specific
454 code by the underlying toolkit or OS itself. But even if it does exist at
455 wxWidgets level, it should never be called directly as the event handler are
456 not part of wxWidgets API and should never be called directly.
457
458 Finally, please notice that the event handlers themselves shouldn't be virtual.
459 They should always be non-virtual and usually private (as there is no need to
460 make them public) methods of a wxEvtHandler-derived class.
461
462
463 @section overview_eventhandling_prog User Generated Events vs Programmatically Generated Events
464
465 While generically wxEvents can be generated both by user
466 actions (e.g. resize of a wxWindow) and by calls to functions
467 (e.g. wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived
468 events only for the user-generated events. The only @b exceptions to this rule are:
469
470 @li wxNotebook::AddPage: No event-free alternatives
471 @li wxNotebook::AdvanceSelection: No event-free alternatives
472 @li wxNotebook::DeletePage: No event-free alternatives
473 @li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as
474 wxNotebook::SetSelection is deprecated
475 @li wxTreeCtrl::Delete: No event-free alternatives
476 @li wxTreeCtrl::DeleteAllItems: No event-free alternatives
477 @li wxTreeCtrl::EditLabel: No event-free alternatives
478 @li All wxTextCtrl methods
479
480 wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other
481 functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free
482 equivalents.
483
484
485
486 @section overview_eventhandling_pluggable Pluggable Event Handlers
487
488 In fact, you don't have to derive a new class from a window class
489 if you don't want to. You can derive a new class from wxEvtHandler instead,
490 defining the appropriate event table, and then call wxWindow::SetEventHandler
491 (or, preferably, wxWindow::PushEventHandler) to make this
492 event handler the object that responds to events. This way, you can avoid
493 a lot of class derivation, and use instances of the same event handler class (but different
494 objects as the same event handler object shouldn't be used more than once) to
495 handle events from instances of different widget classes.
496
497 If you ever have to call a window's event handler
498 manually, use the GetEventHandler function to retrieve the window's event handler and use that
499 to call the member function. By default, GetEventHandler returns a pointer to the window itself
500 unless an application has redirected event handling using SetEventHandler or PushEventHandler.
501
502 One use of PushEventHandler is to temporarily or permanently change the
503 behaviour of the GUI. For example, you might want to invoke a dialog editor
504 in your application that changes aspects of dialog boxes. You can
505 grab all the input for an existing dialog box, and edit it 'in situ',
506 before restoring its behaviour to normal. So even if the application
507 has derived new classes to customize behaviour, your utility can indulge
508 in a spot of body-snatching. It could be a useful technique for on-line
509 tutorials, too, where you take a user through a serious of steps and
510 don't want them to diverge from the lesson. Here, you can examine the events
511 coming from buttons and windows, and if acceptable, pass them through to
512 the original event handler. Use PushEventHandler/PopEventHandler
513 to form a chain of event handlers, where each handler processes a different
514 range of events independently from the other handlers.
515
516
517
518 @section overview_eventhandling_winid Window Identifiers
519
520 Window identifiers are integers, and are used to
521 uniquely determine window identity in the event system (though you can use it
522 for other purposes). In fact, identifiers do not need to be unique
523 across your entire application just so long as they are unique within a
524 particular context you're interested in, such as a frame and its children. You
525 may use the @c wxID_OK identifier, for example, on any number of dialogs so
526 long as you don't have several within the same dialog.
527
528 If you pass @c wxID_ANY to a window constructor, an identifier will be
529 generated for you automatically by wxWidgets. This is useful when you don't
530 care about the exact identifier either because you're not going to process the
531 events from the control being created at all or because you process the events
532 from all controls in one place (in which case you should specify @c wxID_ANY
533 in the event table or wxEvtHandler::Connect call
534 as well. The automatically generated identifiers are always negative and so
535 will never conflict with the user-specified identifiers which must be always
536 positive.
537
538 See @ref page_stdevtid for the list of standard identifiers available.
539 You can use wxID_HIGHEST to determine the number above which it is safe to
540 define your own identifiers. Or, you can use identifiers below wxID_LOWEST.
541 Finally, you can allocate identifiers dynamically using wxNewId() function to.
542 If you use wxNewId() consistently in your application, you can be sure that
543 the your identifiers don't conflict accidentally.
544
545
546 @section overview_eventhandling_custom Custom Event Summary
547
548 @subsection overview_eventhandling_custom_general General approach
549
550 Since version 2.2.x of wxWidgets, each event type is identified by ID which
551 is given to the event type @e at runtime which makes it possible to add
552 new event types to the library or application without risking ID clashes
553 (two different event types mistakingly getting the same event ID). This
554 event type ID is stored in a struct of type @b const wxEventType.
555
556 In order to define a new event type, there are principally two choices.
557 One is to define a entirely new event class (typically deriving from
558 wxEvent or wxCommandEvent.
559
560 The other is to use the existing event classes and give them an new event
561 type. You'll have to define and declare a new event type using either way,
562 and this is done using the following macros:
563
564 @code
565 // in the header of the source file
566 BEGIN_DECLARE_EVENT_TYPES()
567 DECLARE_EVENT_TYPE(name, value)
568 END_DECLARE_EVENT_TYPES()
569
570 // in the implementation
571 DEFINE_EVENT_TYPE(name)
572 @endcode
573
574 You can ignore the @e value parameter of the DECLARE_EVENT_TYPE macro
575 since it is used only for backwards compatibility with wxWidgets 2.0.x based
576 applications where you have to give the event type ID an explicit value.
577 See also the @ref page_samples_event for an example of code
578 defining and working with the custom event types.
579
580
581 @subsection overview_eventhandling_custom_existing Using Existing Event Classes
582
583 If you just want to use a wxCommandEvent with
584 a new event type, you can then use one of the generic event table macros
585 listed below, without having to define a new macro yourself. This also
586 has the advantage that you won't have to define a new wxEvent::Clone()
587 method for posting events between threads etc. This could look like this
588 in your code:
589
590 @code
591 DECLARE_EVENT_TYPE(wxEVT_MY_EVENT, -1)
592 DEFINE_EVENT_TYPE(wxEVT_MY_EVENT)
593
594 // user code intercepting the event
595
596 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
597 EVT_MENU (wxID_EXIT, MyFrame::OnExit)
598 // ....
599 EVT_COMMAND (ID_MY_WINDOW, wxEVT_MY_EVENT, MyFrame::OnMyEvent)
600 END_EVENT_TABLE()
601
602 void MyFrame::OnMyEvent( wxCommandEvent )
603 {
604 // do something
605 wxString text = event.GetText();
606 }
607
608
609 // user code sending the event
610
611 void MyWindow::SendEvent()
612 {
613 wxCommandEvent event( wxEVT_MY_EVENT, GetId() );
614 event.SetEventObject( this );
615 // Give it some contents
616 event.SetText( wxT("Hallo") );
617 // Send it
618 GetEventHandler()->ProcessEvent( event );
619 }
620 @endcode
621
622
623 @subsection overview_eventhandling_custom_generic Generic Event Table Macros
624
625 @beginTable
626 @row2col{EVT_CUSTOM(event\, id\, func),
627 Allows you to add a custom event table
628 entry by specifying the event identifier (such as wxEVT_SIZE),
629 the window identifier, and a member function to call.}
630 @row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func),
631 The same as EVT_CUSTOM, but responds to a range of window identifiers.}
632 @row2col{EVT_COMMAND(id\, event\, func),
633 The same as EVT_CUSTOM, but expects a member function with a
634 wxCommandEvent argument.}
635 @row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func),
636 The same as EVT_CUSTOM_RANGE, but
637 expects a member function with a wxCommandEvent argument.}
638 @row2col{EVT_NOTIFY(event\, id\, func),
639 The same as EVT_CUSTOM, but
640 expects a member function with a wxNotifyEvent argument.}
641 @row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func),
642 The same as EVT_CUSTOM_RANGE, but
643 expects a member function with a wxNotifyEvent argument.}
644 @endTable
645
646
647 @subsection overview_eventhandling_custom_ownclass Defining Your Own Event Class
648
649 Under certain circumstances, it will be required to define your own event
650 class e.g. for sending more complex data from one place to another. Apart
651 from defining your event class, you will also need to define your own
652 event table macro (which is quite long). Watch out to put in enough
653 casts to the inherited event function. Here is an example:
654
655 @code
656 // code defining event
657
658 class wxPlotEvent: public wxNotifyEvent
659 {
660 public:
661 wxPlotEvent( wxEventType commandType = wxEVT_NULL, int id = 0 );
662
663 // accessors
664 wxPlotCurve *GetCurve()
665 { return m_curve; }
666
667 // required for sending with wxPostEvent()
668 virtual wxEvent *Clone() const;
669
670 private:
671 wxPlotCurve *m_curve;
672 };
673
674 DECLARE_EVENT_TYPE( wxEVT_PLOT_ACTION, -1 )
675
676 typedef void (wxEvtHandler::*wxPlotEventFunction)(wxPlotEvent&);
677
678 #define EVT_PLOT(id, fn) \
679 DECLARE_EVENT_TABLE_ENTRY( wxEVT_PLOT_ACTION, id, -1, \
680 (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) (wxNotifyEventFunction) \
681 wxStaticCastEvent( wxPlotEventFunction, &fn ), (wxObject *) NULL ),
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 // user code intercepting the event
692
693 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
694 EVT_PLOT (ID_MY_WINDOW, MyFrame::OnPlot)
695 END_EVENT_TABLE()
696
697 void MyFrame::OnPlot( wxPlotEvent &event )
698 {
699 wxPlotCurve *curve = event.GetCurve();
700 }
701
702
703 // user code sending the event
704
705 void MyWindow::SendEvent()
706 {
707 wxPlotEvent event( wxEVT_PLOT_ACTION, GetId() );
708 event.SetEventObject( this );
709 event.SetCurve( m_curve );
710 GetEventHandler()->ProcessEvent( event );
711 }
712 @endcode
713
714
715 @section overview_eventhandling_macros Event Handling Summary
716
717 For the full list of event classes, please see the
718 @ref group_class_events "event classes group page".
719
720
721 @todo for all controls state clearly when calling a member function results in an
722 event being generated and when it doesn't (possibly updating also the
723 'Events generated by the user vs programmatically generated events' paragraph
724 of the 'Event handling overview' with the list of the functions which break
725 that rule).
726
727 */
728