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