]>
Commit | Line | Data |
---|---|---|
15b6757b | 1 | ///////////////////////////////////////////////////////////////////////////// |
3b88355f | 2 | // Name: eventhandling.h |
15b6757b FM |
3 | // Purpose: topic overview |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /*! | |
36c9828f | 10 | |
3b88355f | 11 | @page overview_eventhandling Event handling overview |
36c9828f | 12 | |
98ba1eee | 13 | Classes: wxEvtHandler, wxWindow, wxEvent |
36c9828f | 14 | |
3b88355f FM |
15 | @li @ref overview_eventhandling_introduction |
16 | @li @ref overview_eventhandling_processing | |
17 | @li @ref overview_eventhandling_prog | |
18 | @li @ref overview_eventhandling_pluggable | |
19 | @li @ref overview_eventhandling_winid | |
58fa97d1 FM |
20 | @li @ref overview_eventhandling_custom |
21 | ||
3b88355f | 22 | <!-- @li @ref overview_eventhandling_macros --> |
36c9828f | 23 | |
3b88355f FM |
24 | |
25 | <hr> | |
26 | ||
27 | ||
28 | @section overview_eventhandling_introduction Introduction | |
36c9828f | 29 | |
15b6757b FM |
30 | Before version 2.0 of wxWidgets, events were handled by the application |
31 | either by supplying callback functions, or by overriding virtual member | |
32 | functions such as @b OnSize. | |
3b88355f | 33 | |
15b6757b FM |
34 | From wxWidgets 2.0, @e event tables are used instead, with a few exceptions. |
35 | An event table is placed in an implementation file to tell wxWidgets how to map | |
36 | events to member functions. These member functions are not virtual functions, but | |
3b88355f FM |
37 | they are all similar in form: they take a single wxEvent-derived argument, |
38 | and have a void return type. | |
15b6757b | 39 | Here's an example of an event table. |
36c9828f | 40 | |
15b6757b FM |
41 | @code |
42 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
58fa97d1 FM |
43 | EVT_MENU(wxID_EXIT, MyFrame::OnExit) |
44 | EVT_MENU(DO_TEST, MyFrame::DoTest) | |
45 | EVT_SIZE(MyFrame::OnSize) | |
46 | EVT_BUTTON(BUTTON1, MyFrame::OnButton1) | |
15b6757b FM |
47 | END_EVENT_TABLE() |
48 | @endcode | |
36c9828f | 49 | |
15b6757b FM |
50 | The first two entries map menu commands to two different member functions. The |
51 | EVT_SIZE macro doesn't need a window identifier, since normally you are only | |
52 | interested in the current window's size events. | |
3b88355f | 53 | |
15b6757b FM |
54 | The EVT_BUTTON macro demonstrates that the originating event does not have to |
55 | come from the window class implementing the event table -- if the event source | |
56 | is a button within a panel within a frame, this will still work, because event | |
57 | tables are searched up through the hierarchy of windows for the command events. | |
58 | In this case, the button's event table will be searched, then the parent | |
59 | panel's, then the frame's. | |
3b88355f | 60 | |
15b6757b FM |
61 | As mentioned before, the member functions that handle events do not have to be |
62 | virtual. Indeed, the member functions should not be virtual as the event | |
63 | handler ignores that the functions are virtual, i.e. overriding a virtual | |
64 | member function in a derived class will not have any effect. These member | |
65 | functions take an event argument, and the class of event differs according to | |
36c9828f | 66 | the type of event and the class of the originating window. For size events, |
98ba1eee FM |
67 | wxSizeEvent is used. For menu commands and most control commands |
68 | (such as button presses), wxCommandEvent is used. When controls get more | |
69 | complicated, then specific event classes are used, such as wxTreeEvent for | |
70 | events from wxTreeCtrl windows. | |
3b88355f | 71 | |
15b6757b FM |
72 | As well as the event table in the implementation file, there must also be a |
73 | DECLARE_EVENT_TABLE macro somewhere in the class declaration. For example: | |
36c9828f | 74 | |
15b6757b FM |
75 | @code |
76 | class MyFrame : public wxFrame | |
77 | { | |
78 | public: | |
79 | ... | |
80 | void OnExit(wxCommandEvent& event); | |
81 | void OnSize(wxSizeEvent& event); | |
36c9828f | 82 | |
15b6757b FM |
83 | protected: |
84 | int m_count; | |
85 | ... | |
36c9828f | 86 | |
15b6757b FM |
87 | DECLARE_EVENT_TABLE() |
88 | }; | |
89 | @endcode | |
36c9828f | 90 | |
15b6757b FM |
91 | Note that this macro may occur in any section of the class (public, protected |
92 | or private) but that it is probably better to insert it at the end, as shown, | |
93 | because this macro implicitly changes the access to protected which may be | |
94 | quite unexpected if there is anything following it. | |
3b88355f | 95 | |
15b6757b FM |
96 | Finally, if you don't like using macros for static initialization of the event |
97 | tables you may also use wxEvtHandler::Connect to | |
98 | connect the events to the handlers dynamically, during run-time. See the | |
98ba1eee | 99 | @ref page_utils_samples_event for an example of doing it. |
36c9828f FM |
100 | |
101 | ||
3b88355f FM |
102 | |
103 | @section overview_eventhandling_processing How events are processed | |
36c9828f FM |
104 | |
105 | When an event is received from the windowing system, wxWidgets calls | |
15b6757b FM |
106 | wxEvtHandler::ProcessEvent on the first |
107 | event handler object belonging to the window generating the event. | |
3b88355f | 108 | |
15b6757b FM |
109 | It may be noted that wxWidgets' event processing system implements something |
110 | very close to virtual methods in normal C++, i.e. it is possible to alter | |
111 | the behaviour of a class by overriding its event handling functions. In | |
112 | many cases this works even for changing the behaviour of native controls. | |
3b88355f | 113 | |
15b6757b FM |
114 | For example it is possible to filter out a number of key events sent by the |
115 | system to a native text control by overriding wxTextCtrl and defining a | |
116 | handler for key events using EVT_KEY_DOWN. This would indeed prevent | |
117 | any key events from being sent to the native control - which might not be | |
118 | what is desired. In this case the event handler function has to call Skip() | |
119 | so as to indicate that the search for the event handler should continue. | |
3b88355f | 120 | |
15b6757b FM |
121 | To summarize, instead of explicitly calling the base class version as you |
122 | would have done with C++ virtual functions (i.e. @e wxTextCtrl::OnChar()), | |
98ba1eee | 123 | you should instead call wxEvent::Skip. |
3b88355f | 124 | |
15b6757b FM |
125 | In practice, this would look like this if the derived text control only |
126 | accepts 'a' to 'z' and 'A' to 'Z': | |
36c9828f | 127 | |
15b6757b FM |
128 | @code |
129 | void MyTextCtrl::OnChar(wxKeyEvent& event) | |
130 | { | |
131 | if ( isalpha( event.KeyCode() ) ) | |
132 | { | |
133 | // key code is within legal range. we call event.Skip() so the | |
134 | // event can be processed either in the base wxWidgets class | |
135 | // or the native control. | |
36c9828f | 136 | |
15b6757b FM |
137 | event.Skip(); |
138 | } | |
139 | else | |
140 | { | |
141 | // illegal key hit. we don't call event.Skip() so the | |
142 | // event is not processed anywhere else. | |
36c9828f | 143 | |
15b6757b FM |
144 | wxBell(); |
145 | } | |
146 | } | |
147 | @endcode | |
36c9828f | 148 | |
15b6757b | 149 | The normal order of event table searching by ProcessEvent is as follows: |
36c9828f | 150 | |
3b88355f FM |
151 | @li If the object is disabled (via a call to wxEvtHandler::SetEvtHandlerEnabled) |
152 | the function skips to step (6). | |
153 | @li If the object is a wxWindow, @b ProcessEvent is recursively called on the window's | |
98ba1eee | 154 | wxValidator. If this returns @true, the function exits. |
3b88355f FM |
155 | @li @b SearchEventTable is called for this event handler. If this fails, the base |
156 | class table is tried, and so on until no more tables exist or an appropriate | |
157 | function was found, in which case the function exits. | |
158 | @li The search is applied down the entire chain of event handlers (usually the chain has | |
159 | a length of one). If this succeeds, the function exits. | |
160 | @li If the object is a wxWindow and the event is set to set to propagate (in the library only | |
161 | wxCommandEvent based events are set to propagate), @b ProcessEvent is recursively applied | |
162 | to the parent window's event handler. If this returns @true, the function exits. | |
163 | @li Finally, @b ProcessEvent is called on the wxApp object. | |
164 | ||
165 | <b>Pay close attention to Step 5</b>. People often overlook or get | |
15b6757b | 166 | confused by this powerful feature of the wxWidgets event processing |
36c9828f | 167 | system. To put it a different way, events set to propagate |
58fa97d1 | 168 | (see wxEvent::ShouldPropagate) |
15b6757b | 169 | (most likely derived either directly or indirectly from wxCommandEvent) |
36c9828f | 170 | will travel up the containment hierarchy from child to parent until the |
15b6757b | 171 | maximal propagation level is reached or an event handler is found that |
3b88355f FM |
172 | doesn't call @c event.Skip(). |
173 | ||
15b6757b FM |
174 | Finally, there is another additional complication (which, in fact, simplifies |
175 | life of wxWidgets programmers significantly): when propagating the command | |
176 | events upwards to the parent window, the event propagation stops when it | |
177 | reaches the parent dialog, if any. This means that you don't risk to get | |
178 | unexpected events from the dialog controls (which might be left unprocessed by | |
179 | the dialog itself because it doesn't care about them) when a modal dialog is | |
180 | popped up. The events do propagate beyond the frames, however. The rationale | |
181 | for this choice is that there are only a few frames in a typical application | |
182 | and their parent-child relation are well understood by the programmer while it | |
183 | may be very difficult, if not impossible, to track down all the dialogs which | |
184 | may be popped up in a complex program (remember that some are created | |
185 | automatically by wxWidgets). If you need to specify a different behaviour for | |
98ba1eee | 186 | some reason, you can use wxWindow::SetExtraStyle(wxWS_EX_BLOCK_EVENTS) |
15b6757b FM |
187 | explicitly to prevent the events from being propagated beyond the given window |
188 | or unset this flag for the dialogs which have it on by default. | |
3b88355f | 189 | |
15b6757b FM |
190 | Typically events that deal with a window as a window (size, motion, |
191 | paint, mouse, keyboard, etc.) are sent only to the window. Events | |
192 | that have a higher level of meaning and/or are generated by the window | |
193 | itself, (button click, menu select, tree expand, etc.) are command | |
3b88355f FM |
194 | events and are sent up to the parent to see if it is interested in the event. |
195 | ||
15b6757b FM |
196 | Note that your application may wish to override ProcessEvent to redirect processing of |
197 | events. This is done in the document/view framework, for example, to allow event handlers | |
198 | to be defined in the document or view. To test for command events (which will probably | |
3b88355f | 199 | be the only events you wish to redirect), you may use wxEvent::IsCommandEvent for efficiency, |
15b6757b | 200 | instead of using the slower run-time type system. |
3b88355f | 201 | |
15b6757b FM |
202 | As mentioned above, only command events are recursively applied to the parents event |
203 | handler in the library itself. As this quite often causes confusion for users, | |
204 | here is a list of system events which will NOT get sent to the parent's event handler: | |
36c9828f | 205 | |
98ba1eee FM |
206 | @li wxEvent: The event base class |
207 | @li wxActivateEvent: A window or application activation event | |
208 | @li wxCloseEvent: A close window or end session event | |
209 | @li wxEraseEvent: An erase background event | |
210 | @li wxFocusEvent: A window focus event | |
211 | @li wxKeyEvent: A keypress event | |
212 | @li wxIdleEvent: An idle event | |
213 | @li wxInitDialogEvent: A dialog initialisation event | |
214 | @li wxJoystickEvent: A joystick event | |
215 | @li wxMenuEvent: A menu event | |
216 | @li wxMouseEvent: A mouse event | |
217 | @li wxMoveEvent: A move event | |
218 | @li wxPaintEvent: A paint event | |
219 | @li wxQueryLayoutInfoEvent: Used to query layout information | |
220 | @li wxSetCursorEvent: Used for special cursor processing based on current mouse position | |
221 | @li wxSizeEvent: A size event | |
222 | @li wxScrollWinEvent: A scroll event sent by a scrolled window (not a scroll bar) | |
223 | @li wxSysColourChangedEvent: A system colour change event | |
36c9828f | 224 | |
15b6757b FM |
225 | In some cases, it might be desired by the programmer to get a certain number |
226 | of system events in a parent window, for example all key events sent to, but not | |
227 | used by, the native controls in a dialog. In this case, a special event handler | |
228 | will have to be written that will override ProcessEvent() in order to pass | |
229 | all events (or any selection of them) to the parent window. | |
36c9828f FM |
230 | |
231 | ||
3b88355f | 232 | @section overview_eventhandling_prog Events generated by the user vs programmatically generated events |
36c9828f | 233 | |
98ba1eee FM |
234 | While generically wxEvents can be generated both by user |
235 | actions (e.g. resize of a wxWindow) and by calls to functions | |
236 | (e.g. wxWindow::SetSize), wxWidgets controls normally send wxCommandEvent-derived | |
3b88355f | 237 | events only for the user-generated events. The only @b exceptions to this rule are: |
36c9828f | 238 | |
3b88355f FM |
239 | @li wxNotebook::AddPage: No event-free alternatives |
240 | @li wxNotebook::AdvanceSelection: No event-free alternatives | |
241 | @li wxNotebook::DeletePage: No event-free alternatives | |
242 | @li wxNotebook::SetSelection: Use wxNotebook::ChangeSelection instead, as | |
243 | wxNotebook::SetSelection is deprecated | |
244 | @li wxTreeCtrl::Delete: No event-free alternatives | |
245 | @li wxTreeCtrl::DeleteAllItems: No event-free alternatives | |
246 | @li wxTreeCtrl::EditLabel: No event-free alternatives | |
98ba1eee | 247 | @li All wxTextCtrl methods |
36c9828f | 248 | |
3b88355f | 249 | wxTextCtrl::ChangeValue can be used instead of wxTextCtrl::SetValue but the other |
98ba1eee FM |
250 | functions, such as wxTextCtrl::Replace or wxTextCtrl::WriteText don't have event-free |
251 | equivalents. | |
36c9828f FM |
252 | |
253 | ||
254 | ||
3b88355f | 255 | @section overview_eventhandling_pluggable Pluggable event handlers |
36c9828f | 256 | |
15b6757b FM |
257 | In fact, you don't have to derive a new class from a window class |
258 | if you don't want to. You can derive a new class from wxEvtHandler instead, | |
3b88355f FM |
259 | defining the appropriate event table, and then call wxWindow::SetEventHandler |
260 | (or, preferably, wxWindow::PushEventHandler) to make this | |
15b6757b FM |
261 | event handler the object that responds to events. This way, you can avoid |
262 | a lot of class derivation, and use instances of the same event handler class (but different | |
263 | objects as the same event handler object shouldn't be used more than once) to | |
3b88355f FM |
264 | handle events from instances of different widget classes. |
265 | ||
266 | If you ever have to call a window's event handler | |
15b6757b FM |
267 | manually, use the GetEventHandler function to retrieve the window's event handler and use that |
268 | to call the member function. By default, GetEventHandler returns a pointer to the window itself | |
269 | unless an application has redirected event handling using SetEventHandler or PushEventHandler. | |
3b88355f | 270 | |
15b6757b FM |
271 | One use of PushEventHandler is to temporarily or permanently change the |
272 | behaviour of the GUI. For example, you might want to invoke a dialog editor | |
273 | in your application that changes aspects of dialog boxes. You can | |
274 | grab all the input for an existing dialog box, and edit it 'in situ', | |
275 | before restoring its behaviour to normal. So even if the application | |
276 | has derived new classes to customize behaviour, your utility can indulge | |
277 | in a spot of body-snatching. It could be a useful technique for on-line | |
278 | tutorials, too, where you take a user through a serious of steps and | |
279 | don't want them to diverge from the lesson. Here, you can examine the events | |
280 | coming from buttons and windows, and if acceptable, pass them through to | |
281 | the original event handler. Use PushEventHandler/PopEventHandler | |
282 | to form a chain of event handlers, where each handler processes a different | |
283 | range of events independently from the other handlers. | |
36c9828f | 284 | |
3b88355f FM |
285 | |
286 | ||
287 | @section overview_eventhandling_winid Window identifiers | |
36c9828f | 288 | |
15b6757b FM |
289 | Window identifiers are integers, and are used to |
290 | uniquely determine window identity in the event system (though you can use it | |
291 | for other purposes). In fact, identifiers do not need to be unique | |
292 | across your entire application just so long as they are unique within a | |
293 | particular context you're interested in, such as a frame and its children. You | |
294 | may use the @c wxID_OK identifier, for example, on any number of dialogs so | |
295 | long as you don't have several within the same dialog. | |
3b88355f | 296 | |
15b6757b FM |
297 | If you pass @c wxID_ANY to a window constructor, an identifier will be |
298 | generated for you automatically by wxWidgets. This is useful when you don't | |
299 | care about the exact identifier either because you're not going to process the | |
300 | events from the control being created at all or because you process the events | |
36c9828f | 301 | from all controls in one place (in which case you should specify @c wxID_ANY |
15b6757b FM |
302 | in the event table or wxEvtHandler::Connect call |
303 | as well. The automatically generated identifiers are always negative and so | |
304 | will never conflict with the user-specified identifiers which must be always | |
305 | positive. | |
3b88355f | 306 | |
15b6757b FM |
307 | The following standard identifiers are supplied. You can use wxID_HIGHEST to |
308 | determine the number above which it is safe to define your own identifiers. Or, | |
309 | you can use identifiers below wxID_LOWEST. | |
36c9828f | 310 | |
15b6757b FM |
311 | @code |
312 | #define wxID_ANY -1 | |
36c9828f | 313 | |
15b6757b | 314 | #define wxID_LOWEST 4999 |
36c9828f | 315 | |
15b6757b FM |
316 | #define wxID_OPEN 5000 |
317 | #define wxID_CLOSE 5001 | |
318 | #define wxID_NEW 5002 | |
319 | #define wxID_SAVE 5003 | |
320 | #define wxID_SAVEAS 5004 | |
321 | #define wxID_REVERT 5005 | |
322 | #define wxID_EXIT 5006 | |
323 | #define wxID_UNDO 5007 | |
324 | #define wxID_REDO 5008 | |
325 | #define wxID_HELP 5009 | |
326 | #define wxID_PRINT 5010 | |
327 | #define wxID_PRINT_SETUP 5011 | |
328 | #define wxID_PREVIEW 5012 | |
329 | #define wxID_ABOUT 5013 | |
330 | #define wxID_HELP_CONTENTS 5014 | |
331 | #define wxID_HELP_COMMANDS 5015 | |
332 | #define wxID_HELP_PROCEDURES 5016 | |
333 | #define wxID_HELP_CONTEXT 5017 | |
36c9828f | 334 | |
15b6757b FM |
335 | #define wxID_CUT 5030 |
336 | #define wxID_COPY 5031 | |
337 | #define wxID_PASTE 5032 | |
338 | #define wxID_CLEAR 5033 | |
339 | #define wxID_FIND 5034 | |
340 | #define wxID_DUPLICATE 5035 | |
341 | #define wxID_SELECTALL 5036 | |
342 | #define wxID_DELETE 5037 | |
343 | #define wxID_REPLACE 5038 | |
344 | #define wxID_REPLACE_ALL 5039 | |
345 | #define wxID_PROPERTIES 5040 | |
36c9828f | 346 | |
15b6757b FM |
347 | #define wxID_VIEW_DETAILS 5041 |
348 | #define wxID_VIEW_LARGEICONS 5042 | |
349 | #define wxID_VIEW_SMALLICONS 5043 | |
350 | #define wxID_VIEW_LIST 5044 | |
351 | #define wxID_VIEW_SORTDATE 5045 | |
352 | #define wxID_VIEW_SORTNAME 5046 | |
353 | #define wxID_VIEW_SORTSIZE 5047 | |
354 | #define wxID_VIEW_SORTTYPE 5048 | |
36c9828f | 355 | |
15b6757b FM |
356 | #define wxID_FILE1 5050 |
357 | #define wxID_FILE2 5051 | |
358 | #define wxID_FILE3 5052 | |
359 | #define wxID_FILE4 5053 | |
360 | #define wxID_FILE5 5054 | |
361 | #define wxID_FILE6 5055 | |
362 | #define wxID_FILE7 5056 | |
363 | #define wxID_FILE8 5057 | |
364 | #define wxID_FILE9 5058 | |
36c9828f | 365 | |
15b6757b FM |
366 | #define wxID_OK 5100 |
367 | #define wxID_CANCEL 5101 | |
368 | #define wxID_APPLY 5102 | |
369 | #define wxID_YES 5103 | |
370 | #define wxID_NO 5104 | |
371 | #define wxID_STATIC 5105 | |
36c9828f | 372 | |
15b6757b FM |
373 | #define wxID_HIGHEST 5999 |
374 | @endcode | |
36c9828f FM |
375 | |
376 | ||
3b88355f FM |
377 | |
378 | <!-- | |
379 | ||
380 | NOTE: this list is incomplete and it's a trouble to maintain it! | |
381 | we must find an automatic way to generate it | |
382 | ||
383 | ||
384 | @section overview_eventhandling_macros Event macros summary | |
36c9828f | 385 | |
15b6757b FM |
386 | @b Macros listed by event class |
387 | The documentation for specific event macros is organised by event class. Please refer | |
388 | to these sections for details. | |
36c9828f FM |
389 | |
390 | ||
391 | ||
392 | ||
393 | ||
394 | ||
15b6757b | 395 | #wxActivateEvent |
36c9828f FM |
396 | |
397 | ||
398 | ||
399 | ||
15b6757b FM |
400 | The EVT_ACTIVATE and EVT_ACTIVATE_APP macros intercept |
401 | activation and deactivation events. | |
36c9828f FM |
402 | |
403 | ||
404 | ||
405 | ||
406 | ||
15b6757b | 407 | #wxCommandEvent |
36c9828f FM |
408 | |
409 | ||
410 | ||
411 | ||
15b6757b | 412 | A range of commonly-used control events. |
36c9828f FM |
413 | |
414 | ||
415 | ||
416 | ||
417 | ||
15b6757b | 418 | #wxCloseEvent |
36c9828f FM |
419 | |
420 | ||
421 | ||
422 | ||
15b6757b FM |
423 | The EVT_CLOSE macro handles window closure |
424 | called via wxWindow::Close. | |
36c9828f FM |
425 | |
426 | ||
427 | ||
428 | ||
429 | ||
15b6757b | 430 | #wxDropFilesEvent |
36c9828f FM |
431 | |
432 | ||
433 | ||
434 | ||
15b6757b FM |
435 | The EVT_DROP_FILES macros handles |
436 | file drop events. | |
36c9828f FM |
437 | |
438 | ||
439 | ||
440 | ||
441 | ||
15b6757b | 442 | #wxEraseEvent |
36c9828f FM |
443 | |
444 | ||
445 | ||
446 | ||
15b6757b | 447 | The EVT_ERASE_BACKGROUND macro is used to handle window erase requests. |
36c9828f FM |
448 | |
449 | ||
450 | ||
451 | ||
452 | ||
15b6757b | 453 | #wxFocusEvent |
36c9828f FM |
454 | |
455 | ||
456 | ||
457 | ||
15b6757b | 458 | The EVT_SET_FOCUS and EVT_KILL_FOCUS macros are used to handle keyboard focus events. |
36c9828f FM |
459 | |
460 | ||
461 | ||
462 | ||
463 | ||
15b6757b | 464 | #wxKeyEvent |
36c9828f FM |
465 | |
466 | ||
467 | ||
468 | ||
15b6757b FM |
469 | EVT_CHAR, EVT_KEY_DOWN and |
470 | EVT_KEY_UP macros handle keyboard input for any window. | |
36c9828f FM |
471 | |
472 | ||
473 | ||
474 | ||
475 | ||
15b6757b | 476 | #wxIdleEvent |
36c9828f FM |
477 | |
478 | ||
479 | ||
480 | ||
15b6757b FM |
481 | The EVT_IDLE macro handle application idle events |
482 | (to process background tasks, for example). | |
36c9828f FM |
483 | |
484 | ||
485 | ||
486 | ||
487 | ||
15b6757b | 488 | #wxInitDialogEvent |
36c9828f FM |
489 | |
490 | ||
491 | ||
492 | ||
15b6757b FM |
493 | The EVT_INIT_DIALOG macro is used |
494 | to handle dialog initialisation. | |
36c9828f FM |
495 | |
496 | ||
497 | ||
498 | ||
499 | ||
15b6757b | 500 | #wxListEvent |
36c9828f FM |
501 | |
502 | ||
503 | ||
504 | ||
15b6757b | 505 | These macros handle #wxListCtrl events. |
36c9828f FM |
506 | |
507 | ||
508 | ||
509 | ||
510 | ||
15b6757b | 511 | #wxMenuEvent |
36c9828f FM |
512 | |
513 | ||
514 | ||
515 | ||
15b6757b | 516 | These macros handle special menu events (not menu commands). |
36c9828f FM |
517 | |
518 | ||
519 | ||
520 | ||
521 | ||
15b6757b | 522 | #wxMouseEvent |
36c9828f FM |
523 | |
524 | ||
525 | ||
526 | ||
15b6757b FM |
527 | Mouse event macros can handle either individual |
528 | mouse events or all mouse events. | |
36c9828f FM |
529 | |
530 | ||
531 | ||
532 | ||
533 | ||
15b6757b | 534 | #wxMoveEvent |
36c9828f FM |
535 | |
536 | ||
537 | ||
538 | ||
15b6757b | 539 | The EVT_MOVE macro is used to handle a window move. |
36c9828f FM |
540 | |
541 | ||
542 | ||
543 | ||
544 | ||
15b6757b | 545 | #wxPaintEvent |
36c9828f FM |
546 | |
547 | ||
548 | ||
549 | ||
15b6757b | 550 | The EVT_PAINT macro is used to handle window paint requests. |
36c9828f FM |
551 | |
552 | ||
553 | ||
554 | ||
555 | ||
15b6757b | 556 | #wxScrollEvent |
36c9828f FM |
557 | |
558 | ||
559 | ||
560 | ||
561 | These macros are used to handle scroll events from | |
15b6757b | 562 | #wxScrollBar, #wxSlider,and #wxSpinButton. |
36c9828f FM |
563 | |
564 | ||
565 | ||
566 | ||
567 | ||
15b6757b | 568 | #wxSetCursorEvent |
36c9828f FM |
569 | |
570 | ||
571 | ||
572 | ||
15b6757b | 573 | The EVT_SET_CURSOR macro is used for special cursor processing. |
36c9828f FM |
574 | |
575 | ||
576 | ||
577 | ||
578 | ||
15b6757b | 579 | #wxSizeEvent |
36c9828f FM |
580 | |
581 | ||
582 | ||
583 | ||
15b6757b | 584 | The EVT_SIZE macro is used to handle a window resize. |
36c9828f FM |
585 | |
586 | ||
587 | ||
588 | ||
589 | ||
15b6757b | 590 | #wxSplitterEvent |
36c9828f FM |
591 | |
592 | ||
593 | ||
594 | ||
15b6757b FM |
595 | The EVT_SPLITTER_SASH_POS_CHANGED, EVT_SPLITTER_UNSPLIT |
596 | and EVT_SPLITTER_DCLICK macros are used to handle the various splitter window events. | |
36c9828f FM |
597 | |
598 | ||
599 | ||
600 | ||
601 | ||
15b6757b | 602 | #wxSysColourChangedEvent |
36c9828f FM |
603 | |
604 | ||
605 | ||
606 | ||
15b6757b FM |
607 | The EVT_SYS_COLOUR_CHANGED macro is used to handle |
608 | events informing the application that the user has changed the system colours (Windows only). | |
36c9828f FM |
609 | |
610 | ||
611 | ||
612 | ||
613 | ||
15b6757b | 614 | #wxTreeEvent |
36c9828f FM |
615 | |
616 | ||
617 | ||
618 | ||
15b6757b | 619 | These macros handle #wxTreeCtrl events. |
36c9828f FM |
620 | |
621 | ||
622 | ||
623 | ||
624 | ||
15b6757b | 625 | #wxUpdateUIEvent |
36c9828f FM |
626 | |
627 | ||
628 | ||
629 | ||
15b6757b FM |
630 | The EVT_UPDATE_UI macro is used to handle user interface |
631 | update pseudo-events, which are generated to give the application the chance to update the visual state of menus, | |
632 | toolbars and controls. | |
36c9828f FM |
633 | |
634 | ||
3b88355f | 635 | --> |
36c9828f FM |
636 | |
637 | ||
638 | ||
3b88355f | 639 | @section overview_eventhandling_custom Custom event summary |
36c9828f | 640 | |
3b88355f | 641 | @subsection overview_eventhandling_custom_general General approach |
36c9828f | 642 | |
15b6757b FM |
643 | Since version 2.2.x of wxWidgets, each event type is identified by ID which |
644 | is given to the event type @e at runtime which makes it possible to add | |
645 | new event types to the library or application without risking ID clashes | |
646 | (two different event types mistakingly getting the same event ID). This | |
647 | event type ID is stored in a struct of type @b const wxEventType. | |
3b88355f | 648 | |
15b6757b FM |
649 | In order to define a new event type, there are principally two choices. |
650 | One is to define a entirely new event class (typically deriving from | |
98ba1eee | 651 | wxEvent or wxCommandEvent. |
3b88355f | 652 | |
15b6757b FM |
653 | The other is to use the existing event classes and give them an new event |
654 | type. You'll have to define and declare a new event type using either way, | |
655 | and this is done using the following macros: | |
36c9828f | 656 | |
15b6757b FM |
657 | @code |
658 | // in the header of the source file | |
659 | BEGIN_DECLARE_EVENT_TYPES() | |
660 | DECLARE_EVENT_TYPE(name, value) | |
661 | END_DECLARE_EVENT_TYPES() | |
36c9828f | 662 | |
15b6757b FM |
663 | // in the implementation |
664 | DEFINE_EVENT_TYPE(name) | |
665 | @endcode | |
36c9828f | 666 | |
15b6757b FM |
667 | You can ignore the @e value parameter of the DECLARE_EVENT_TYPE macro |
668 | since it is used only for backwards compatibility with wxWidgets 2.0.x based | |
669 | applications where you have to give the event type ID an explicit value. | |
98ba1eee | 670 | See also the @ref page_utils_samples_event for an example of code |
15b6757b | 671 | defining and working with the custom event types. |
3b88355f FM |
672 | |
673 | ||
674 | @subsection overview_eventhandling_custom_existing Using existing event classes | |
675 | ||
98ba1eee | 676 | If you just want to use a wxCommandEvent with |
15b6757b FM |
677 | a new event type, you can then use one of the generic event table macros |
678 | listed below, without having to define a new macro yourself. This also | |
679 | has the advantage that you won't have to define a new wxEvent::Clone() | |
680 | method for posting events between threads etc. This could look like this | |
681 | in your code: | |
36c9828f | 682 | |
15b6757b FM |
683 | @code |
684 | DECLARE_EVENT_TYPE(wxEVT_MY_EVENT, -1) | |
15b6757b | 685 | DEFINE_EVENT_TYPE(wxEVT_MY_EVENT) |
36c9828f | 686 | |
15b6757b | 687 | // user code intercepting the event |
36c9828f | 688 | |
15b6757b FM |
689 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
690 | EVT_MENU (wxID_EXIT, MyFrame::OnExit) | |
691 | // .... | |
692 | EVT_COMMAND (ID_MY_WINDOW, wxEVT_MY_EVENT, MyFrame::OnMyEvent) | |
693 | END_EVENT_TABLE() | |
36c9828f | 694 | |
15b6757b FM |
695 | void MyFrame::OnMyEvent( wxCommandEvent ) |
696 | { | |
697 | // do something | |
698 | wxString text = event.GetText(); | |
699 | } | |
36c9828f FM |
700 | |
701 | ||
15b6757b | 702 | // user code sending the event |
36c9828f | 703 | |
15b6757b FM |
704 | void MyWindow::SendEvent() |
705 | { | |
706 | wxCommandEvent event( wxEVT_MY_EVENT, GetId() ); | |
707 | event.SetEventObject( this ); | |
708 | // Give it some contents | |
709 | event.SetText( wxT("Hallo") ); | |
710 | // Send it | |
38b5b493 | 711 | GetEventHandler()->ProcessEvent( event ); |
15b6757b FM |
712 | } |
713 | @endcode | |
36c9828f FM |
714 | |
715 | ||
3b88355f FM |
716 | @subsection overview_eventhandling_custom_generic Generic event table macros |
717 | ||
718 | @beginTable | |
719 | @row2col{EVT_CUSTOM(event\, id\, func), | |
720 | Allows you to add a custom event table | |
721 | entry by specifying the event identifier (such as wxEVT_SIZE), | |
722 | the window identifier, and a member function to call.} | |
723 | @row2col{EVT_CUSTOM_RANGE(event\, id1\, id2\, func), | |
724 | The same as EVT_CUSTOM, but responds to a range of window identifiers.} | |
725 | @row2col{EVT_COMMAND(id\, event\, func), | |
726 | The same as EVT_CUSTOM, but expects a member function with a | |
727 | wxCommandEvent argument.} | |
728 | @row2col{EVT_COMMAND_RANGE(id1\, id2\, event\, func), | |
729 | The same as EVT_CUSTOM_RANGE, but | |
730 | expects a member function with a wxCommandEvent argument.} | |
731 | @row2col{EVT_NOTIFY(event\, id\, func), | |
732 | The same as EVT_CUSTOM, but | |
733 | expects a member function with a wxNotifyEvent argument.} | |
734 | @row2col{EVT_NOTIFY_RANGE(event\, id1\, id2\, func), | |
735 | The same as EVT_CUSTOM_RANGE, but | |
736 | expects a member function with a wxNotifyEvent argument.} | |
737 | @endTable | |
738 | ||
739 | ||
740 | @subsection overview_eventhandling_custom_ownclass Defining your own event class | |
36c9828f | 741 | |
15b6757b FM |
742 | Under certain circumstances, it will be required to define your own event |
743 | class e.g. for sending more complex data from one place to another. Apart | |
744 | from defining your event class, you will also need to define your own | |
745 | event table macro (which is quite long). Watch out to put in enough | |
746 | casts to the inherited event function. Here is an example: | |
36c9828f | 747 | |
15b6757b FM |
748 | @code |
749 | // code defining event | |
36c9828f | 750 | |
15b6757b FM |
751 | class wxPlotEvent: public wxNotifyEvent |
752 | { | |
753 | public: | |
98ba1eee | 754 | wxPlotEvent( wxEventType commandType = wxEVT_NULL, int id = 0 ); |
36c9828f | 755 | |
15b6757b FM |
756 | // accessors |
757 | wxPlotCurve *GetCurve() | |
758 | { return m_curve; } | |
36c9828f | 759 | |
15b6757b FM |
760 | // required for sending with wxPostEvent() |
761 | virtual wxEvent *Clone() const; | |
36c9828f | 762 | |
15b6757b FM |
763 | private: |
764 | wxPlotCurve *m_curve; | |
765 | }; | |
36c9828f | 766 | |
15b6757b | 767 | DECLARE_EVENT_TYPE( wxEVT_PLOT_ACTION, -1 ) |
36c9828f | 768 | |
15b6757b | 769 | typedef void (wxEvtHandler::*wxPlotEventFunction)(wxPlotEvent&); |
36c9828f | 770 | |
15b6757b FM |
771 | #define EVT_PLOT(id, fn) \ |
772 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_PLOT_ACTION, id, -1, \ | |
773 | (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) (wxNotifyEventFunction) \ | |
38b5b493 | 774 | wxStaticCastEvent( wxPlotEventFunction, &fn ), (wxObject *) NULL ), |
36c9828f FM |
775 | |
776 | ||
15b6757b | 777 | // code implementing the event type and the event class |
36c9828f | 778 | |
15b6757b | 779 | DEFINE_EVENT_TYPE( wxEVT_PLOT_ACTION ) |
36c9828f | 780 | |
15b6757b | 781 | wxPlotEvent::wxPlotEvent( ... |
36c9828f FM |
782 | |
783 | ||
15b6757b | 784 | // user code intercepting the event |
36c9828f | 785 | |
15b6757b | 786 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
98ba1eee | 787 | EVT_PLOT (ID_MY_WINDOW, MyFrame::OnPlot) |
15b6757b | 788 | END_EVENT_TABLE() |
36c9828f | 789 | |
38b5b493 | 790 | void MyFrame::OnPlot( wxPlotEvent &event ) |
15b6757b FM |
791 | { |
792 | wxPlotCurve *curve = event.GetCurve(); | |
793 | } | |
36c9828f FM |
794 | |
795 | ||
15b6757b | 796 | // user code sending the event |
36c9828f | 797 | |
15b6757b FM |
798 | void MyWindow::SendEvent() |
799 | { | |
800 | wxPlotEvent event( wxEVT_PLOT_ACTION, GetId() ); | |
801 | event.SetEventObject( this ); | |
802 | event.SetCurve( m_curve ); | |
98ba1eee | 803 | GetEventHandler()->ProcessEvent( event ); |
15b6757b FM |
804 | } |
805 | @endcode | |
36c9828f | 806 | |
3b88355f | 807 | */ |
36c9828f | 808 |