File: wx299.htm

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (641 lines) | stat: -rw-r--r-- 18,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<HTML>
<head><title>Event handling overview</title></head>

<BODY BGCOLOR=#FFFFFF>
<A NAME="eventhandlingoverview"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx278.htm#overviews"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx298.htm#docviewoverview"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx300.htm#roughguide"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>

<H2>Event handling overview</H2>
<P>
Classes: <A HREF="wx85.htm#wxevthandler">wxEvtHandler</A>, <A HREF="wx260.htm#wxwindow">wxWindow</A>, <A HREF="wx84.htm#wxevent">wxEvent</A><P>
<A HREF="#topic1132">Introduction</A><BR>
<A HREF="#eventprocessing">How events are processed</A><BR>
<A HREF="#topic1133">Pluggable event handlers</A><BR>
<A HREF="#windowids">Window identifiers</A><BR>
<A HREF="#eventmacros">Event macros summary</A><BR>
<P>

<HR>
<A NAME="topic1132"></A>
<H3>Introduction</H3>
<P>
Before version 2.0 of wxWindows, events were handled by the application
either by supplying callback functions, or by overriding virtual member
functions such as <B>OnSize</B>.<P>
From wxWindows 2.0, <I>event tables</I> are used instead, with a few exceptions.<P>
An event table is placed in an implementation file to tell wxWindows how to map
events to member functions. These member functions are not virtual functions, but
they all similar in form: they take a single wxEvent-derived argument, and have a void return
type.<P>
Here's an example of an event table.<P>
<PRE>
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  EVT_MENU    (wxID_EXIT, MyFrame::OnExit)
  EVT_MENU    (DO_TEST,   MyFrame::DoTest)
  EVT_SIZE    (           MyFrame::OnSize)
  EVT_BUTTON  (BUTTON1,   MyFrame::OnButton1)
END_EVENT_TABLE()
</PRE>
The first two entries map menu commands to two different member functions. The EVT_SIZE macro
doesn't need a window identifier, since normally you are only interested in the
current window's size events. (In fact you could intercept a particular window's size event
by using EVT_CUSTOM(wxEVT_SIZE, id, func).)<P>
The EVT_BUTTON macro demonstrates that the originating event does not have to come from
the window class implementing the event table - if the event source is a button within a panel within a frame, this will still
work, because event tables are searched up through the hierarchy of windows. In this
case, the button's event table will be searched, then the parent panel's, then the frame's.<P>
As mentioned before, the member functions that handle events do not have to be virtual.
Indeed, the member functions should not be virtual as the event handler ignores that
the functions are virtual, i.e. overriding a virtual member function in a derived class
will not have any effect.
These member functions take an event argument, and the class of event differs according
to the type of event and the class of the originating window. For size
events, <A HREF="wx206.htm#wxsizeevent">wxSizeEvent</A> is used. For menu commands and most control
commands (such as button presses), <A HREF="wx52.htm#wxcommandevent">wxCommandEvent</A> is used.
When controls get more complicated, then specific event classes are used, such
as <A HREF="wx252.htm#wxtreeevent">wxTreeEvent</A> for events from <A HREF="wx250.htm#wxtreectrl">wxTreeCtrl</A> windows.<P>
As well as the event table in the implementation file, there must be a DECLARE_EVENT_TABLE
macro in the class definition. For example:<P>
<FONT SIZE=2><PRE>
class MyFrame: public wxFrame {

  DECLARE_DYNAMIC_CLASS(MyFrame)

public:
  ...
  void OnExit(wxCommandEvent& event);
  void OnSize(wxSizeEvent& event);
protected:
  int       m_count;
  ...
  DECLARE_EVENT_TABLE()
};
</PRE>
</FONT>
<HR>
<A NAME="eventprocessing"></A>
<H3>How events are processed</H3>
<P>
When an event is received from the windowing system, wxWindows calls <A HREF="wx85.htm#wxevthandlerprocessevent">wxEvtHandler::ProcessEvent</A> on
the first event handler object belonging to the window generating the event.<P>
It may be noted that wxWindows' event processing system implements something
very close to virtual methods in normal C++, i.e. it is possible to alter
the behaviour of a class by overriding its event handling functions. In
many cases this works even for changing the behaviour of native controls.
For example it is possible to filter out a number of key events sent by the
system to a native text control by overriding wxTextCtrl and defining a
handler for key events using EVT_KEY_DOWN. This would indeed prevent
any key events from being sent to the native control - which might not be
what is desired. In this case the event handler function has to call Skip()
so as to indicate that it did NOT handle the event at all.<P>
In practice, this would look like this if the derived text control only
accepts 'a' to 'z' and 'A' to 'Z':<P>
<FONT SIZE=2><PRE>
void MyTextCtrl::OnChar(wxKeyEvent& event)
{
    if ( isalpha( event.KeyCode() ) )
    {
       // key code is within legal range. we call event.Skip() so the
       // event can be processed either in the base wxWindows class
       // or the native control.
       
       event.Skip(); 
    }
    else
    {
       // illegal key hit. we don't call event.Skip() so the
       // event is not processed anywhere else.
       
       wxBell();
    }
}
</PRE>
</FONT>
The normal order of event table searching by ProcessEvent is as follows:<P>
<OL>

<LI> If the object is disabled (via a call to <A HREF="wx85.htm#wxevthandlersetevthandlerenabled">wxEvtHandler::SetEvtHandlerEnabled</A>)
the function skips to step (6).
<LI> If the object is a wxWindow, <B>ProcessEvent</B> is recursively called on the window's
<A HREF="wx255.htm#wxvalidator">wxValidator</A>. If this returns TRUE, the function exits.
<LI> <B>SearchEventTable</B> is called for this event handler. If this fails, the base
class table is tried, and so on until no more tables exist or an appropriate function was found,
in which case the function exits.
<LI> The search is applied down the entire chain of event handlers (usually the chain has a length
of one). If this succeeds, the function exits.
<LI> If the object is a wxWindow and the event is a wxCommandEvent, <B>ProcessEvent</B> is
recursively applied to the parent window's event handler. If this returns TRUE, the function exits.
<LI> Finally, <B>ProcessEvent</B> is called on the wxApp object.
</OL>
<P>
Note that your application may wish to override ProcessEvent to redirect processing of
events. This is done in the document/view framework, for example, to allow event handlers
to be defined in the document or view.<P>
As mentioned above, only command events are recursively applied to the parents event
handler. As this quite often causes confusion for users, here is a list of system
events which will NOT get sent to the parent's event handler:<P>

<TABLE>


<TR><TD VALIGN=TOP>
<A HREF="wx84.htm#wxevent">wxEvent</A>
</TD>

<TD VALIGN=TOP>
The event base class
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx25.htm#wxactivateevent">wxActivateEvent</A>
</TD>

<TD VALIGN=TOP>
A window or application activation event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx45.htm#wxcloseevent">wxCloseEvent</A>
</TD>

<TD VALIGN=TOP>
A close window or end session event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx83.htm#wxeraseevent">wxEraseEvent</A>
</TD>

<TD VALIGN=TOP>
An erase background event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx99.htm#wxfocusevent">wxFocusEvent</A>
</TD>

<TD VALIGN=TOP>
A window focus event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx124.htm#wxkeyevent">wxKeyEvent</A>
</TD>

<TD VALIGN=TOP>
A keypress event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx113.htm#wxidleevent">wxIdleEvent</A>
</TD>

<TD VALIGN=TOP>
An idle event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx119.htm#wxinitdialogevent">wxInitDialogEvent</A>
</TD>

<TD VALIGN=TOP>
A dialog initialisation event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx123.htm#wxjoystickevent">wxJoystickEvent</A>
</TD>

<TD VALIGN=TOP>
A joystick event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx143.htm#wxmenuevent">wxMenuEvent</A>
</TD>

<TD VALIGN=TOP>
A menu event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx150.htm#wxmouseevent">wxMouseEvent</A>
</TD>

<TD VALIGN=TOP>
A mouse event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx151.htm#wxmoveevent">wxMoveEvent</A>
</TD>

<TD VALIGN=TOP>
A move event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx164.htm#wxpaintevent">wxPaintEvent</A>
</TD>

<TD VALIGN=TOP>
A paint event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx189.htm#wxquerylayoutinfoevent">wxQueryLayoutInfoEvent</A>
</TD>

<TD VALIGN=TOP>
Used to query layout information
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx206.htm#wxsizeevent">wxSizeEvent</A>
</TD>

<TD VALIGN=TOP>
A size event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx227.htm#wxsyscolourchangedevent">wxSysColourChangedEvent</A>
</TD>

<TD VALIGN=TOP>
A system colour change event
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx253.htm#wxupdateuievent">wxUpdateUIEvent</A>
</TD>

<TD VALIGN=TOP>
A user interface update event
</TD></TR>


</TABLE>
<P>
In some cases, it might be desired by the programmer to get a certain number
of system events in a parent window, for example all key events sent to, but not
used by, the native controls in a dialog. In this case, a special event handler
will have to be written that will override ProcessEvent() in order to pass
all events (or any selection of them) to the parent window. See next section.<P>

<HR>
<A NAME="topic1133"></A>
<H3>Pluggable event handlers</H3>
<P>
In fact, you don't have to derive a new class from a window class
if you don't want to. You can derive a new class from wxEvtHandler instead,
defining the appropriate event table, and then call
<A HREF="wx260.htm#wxwindowseteventhandler">wxWindow::SetEventHandler</A> (or, preferably,
<A HREF="wx260.htm#wxwindowpusheventhandler">wxWindow::PushEventHandler</A>) to make this
event handler the object that responds to events. This way, you can avoid
a lot of class derivation, and use the same event handler object to
handle events from instances of different classes. If you ever have to call a window's event handler
manually, use the GetEventHandler function to retrieve the window's event handler and use that
to call the member function. By default, GetEventHandler returns a pointer to the window itself
unless an application has redirected event handling using SetEventHandler or PushEventHandler.<P>
One use of PushEventHandler is to temporarily or permanently change the
behaviour of the GUI. For example, you might want to invoke a dialog editor
in your application that changes aspects of dialog boxes. You can
grab all the input for an existing dialog box, and edit it 'in situ',
before restoring its behaviour to normal. So even if the application
has derived new classes to customize behaviour, your utility can indulge
in a spot of body-snatching. It could be a useful technique for on-line
tutorials, too, where you take a user through a serious of steps and
don't want them to diverge from the lesson. Here, you can examine the events
coming from buttons and windows, and if acceptable, pass them through to
the original event handler. Use PushEventHandler/PopEventHandler
to form a chain of event handlers, where each handler processes a different
range of events independently from the other handlers.<P>

<HR>
<A NAME="windowids"></A>
<H3>Window identifiers</H3>
<P>
Window identifiers are integers, and are used to uniquely determine window identity in the
event system (though you can use it for other purposes). In fact, identifiers do not need
to be unique across your entire application just so long as they are unique within a particular context you're interested
in, such as a frame and its children. You may use the wxID_OK identifier, for example, on
any number of dialogs so long as you don't have several within the same dialog.<P>
If you pass -1 to a window constructor, an identifier will be generated for you, but beware:
if things don't respond in the way they should, it could be because of an id conflict. It's safer
to supply window ids at all times. Automatic generation of identifiers starts at 1 so may well conflict
with your own identifiers.<P>
The following standard identifiers are supplied. You can use wxID_HIGHEST to determine the
number above which it is safe to define your own identifiers. Or, you can use identifiers below
wxID_LOWEST.<P>
<PRE>
#define wxID_LOWEST             4999

#define wxID_OPEN               5000
#define wxID_CLOSE              5001
#define wxID_NEW                5002
#define wxID_SAVE               5003
#define wxID_SAVEAS             5004
#define wxID_REVERT             5005
#define wxID_EXIT               5006
#define wxID_UNDO               5007
#define wxID_REDO               5008
#define wxID_HELP               5009
#define wxID_PRINT              5010
#define wxID_PRINT_SETUP        5011
#define wxID_PREVIEW            5012
#define wxID_ABOUT              5013
#define wxID_HELP_CONTENTS      5014
#define wxID_HELP_COMMANDS      5015
#define wxID_HELP_PROCEDURES    5016
#define wxID_HELP_CONTEXT       5017

#define wxID_CUT                5030
#define wxID_COPY               5031
#define wxID_PASTE              5032
#define wxID_CLEAR              5033
#define wxID_FIND               5034
#define wxID_DUPLICATE          5035
#define wxID_SELECTALL          5036

#define wxID_FILE1              5050
#define wxID_FILE2              5051
#define wxID_FILE3              5052
#define wxID_FILE4              5053
#define wxID_FILE5              5054
#define wxID_FILE6              5055
#define wxID_FILE7              5056
#define wxID_FILE8              5057
#define wxID_FILE9              5058

#define wxID_OK                 5100
#define wxID_CANCEL             5101
#define wxID_APPLY              5102
#define wxID_YES                5103
#define wxID_NO                 5104
#define wxID_STATIC             5105

#define wxID_HIGHEST            5999
</PRE>

<HR>
<A NAME="eventmacros"></A>
<H3>Event macros summary</H3>
<P>
<B><FONT COLOR="#FF0000">Generic event table macros</FONT></B><P>

<TABLE>


<TR><TD VALIGN=TOP>
<B>EVT_CUSTOM(event, id, func)</B>
</TD>

<TD VALIGN=TOP>
Allows you to add a custom event table
entry by specifying the event identifier (such as wxEVT_SIZE), the window identifier,
and a member function to call.
</TD></TR>


<TR><TD VALIGN=TOP>
<B>EVT_CUSTOM_RANGE(event, id1, id2, func)</B>
</TD>

<TD VALIGN=TOP>
The same as EVT_CUSTOM,
but responds to a range of window identifiers.
</TD></TR>


<TR><TD VALIGN=TOP>
<B>EVT_COMMAND(id, event, func)</B>
</TD>

<TD VALIGN=TOP>
The same as EVT_CUSTOM, but
expects a member function with a wxCommandEvent argument.
</TD></TR>


<TR><TD VALIGN=TOP>
<B>EVT_COMMAND_RANGE(id1, id2, event, func)</B>
</TD>

<TD VALIGN=TOP>
The same as EVT_CUSTOM_RANGE, but
expects a member function with a wxCommandEvent argument.
</TD></TR>


</TABLE>
<P>
<B><FONT COLOR="#FF0000">Macros listed by event class</FONT></B><P>
The documentation for specific event macros is organised by event class. Please refer
to these sections for details.<P>

<TABLE>


<TR><TD VALIGN=TOP>
<A HREF="wx25.htm#wxactivateevent">wxActivateEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_ACTIVATE and EVT_ACTIVATE_APP macros intercept
activation and deactivation events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx52.htm#wxcommandevent">wxCommandEvent</A>
</TD>

<TD VALIGN=TOP>
A range of commonly-used control events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx45.htm#wxcloseevent">wxCloseEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_CLOSE macro handles window closure
called via <A HREF="wx260.htm#wxwindowclose">wxWindow::Close</A>.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx80.htm#wxdropfilesevent">wxDropFilesEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_DROP_FILES macros handles
file drop events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx83.htm#wxeraseevent">wxEraseEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_ERASE_BACKGROUND macro is used to handle window erase requests.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx99.htm#wxfocusevent">wxFocusEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_SET_FOCUS and EVT_KILL_FOCUS macros are used to handle keybaord focus events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx124.htm#wxkeyevent">wxKeyEvent</A>
</TD>

<TD VALIGN=TOP>
EVT_CHAR and EVT_CHAR_HOOK macros handle keyboard
input for any window.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx113.htm#wxidleevent">wxIdleEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_IDLE macro handle application idle events
(to process background tasks, for example).
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx119.htm#wxinitdialogevent">wxInitDialogEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_INIT_DIALOG macro is used
to handle dialog initialisation.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx130.htm#wxlistevent">wxListEvent</A>
</TD>

<TD VALIGN=TOP>
These macros handle <A HREF="wx129.htm#wxlistctrl">wxListCtrl</A> events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx143.htm#wxmenuevent">wxMenuEvent</A>
</TD>

<TD VALIGN=TOP>
These macros handle special menu events (not menu commands).
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx150.htm#wxmouseevent">wxMouseEvent</A>
</TD>

<TD VALIGN=TOP>
Mouse event macros can handle either individual
mouse events or all mouse events.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx151.htm#wxmoveevent">wxMoveEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_MOVE macro is used to handle a window move.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx253.htm#wxupdateuievent">wxUpdateUIEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_UPDATE_UI macro is used to handle user interface
update pseudo-events, which are generated to give the application the chance to update the visual state of menus,
toolbars and controls.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx164.htm#wxpaintevent">wxPaintEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_PAINT macro is used to handle window paint requests.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx202.htm#wxscrollevent">wxScrollEvent</A>
</TD>

<TD VALIGN=TOP>
These macros are used to handle scroll events from
windows, <A HREF="wx201.htm#wxscrollbar">wxScrollBar</A>, and <A HREF="wx216.htm#wxspinbutton">wxSpinButton</A>.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx206.htm#wxsizeevent">wxSizeEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_SIZE macro is used to handle a window resize.
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx227.htm#wxsyscolourchangedevent">wxSysColourChangedEvent</A>
</TD>

<TD VALIGN=TOP>
The EVT_SYS_COLOUR_CHANGED macro is used to handle
events informing the application that the user has changed the system colours (Windows only).
</TD></TR>


<TR><TD VALIGN=TOP>
<A HREF="wx252.htm#wxtreeevent">wxTreeEvent</A>
</TD>

<TD VALIGN=TOP>
These macros handle <A HREF="wx250.htm#wxtreectrl">wxTreeCtrl</A> events.
</TD></TR>


</TABLE>
<P>

</BODY></HTML>