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