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