]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/tevent.tex
* Stream doc update.
[wxWidgets.git] / docs / latex / wx / tevent.tex
1 \section{Event handling overview}\label{eventhandlingoverview}
2
3 Classes: \helpref{wxEvtHandler}{wxevthandler}, \helpref{wxWindow}{wxwindow}, \helpref{wxEvent}{wxevent}
4
5 \subsection{Introduction}
6
7 Before version 2.0 of wxWindows, events were handled by the application
8 either by supplying callback functions, or by overriding virtual member
9 functions such as {\bf OnSize}.
10
11 From wxWindows 2.0, {\it event tables} are used instead, with a few exceptions.
12
13 An event table is placed in an implementation file to tell wxWindows how to map
14 events to member functions. These member functions are not virtual functions, but
15 they all similar in form: they take a single wxEvent-derived argument, and have a void return
16 type.
17
18 Here's an example of an event table.
19
20 \begin{verbatim}
21 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
22 EVT_MENU (wxID_EXIT, MyFrame::OnExit)
23 EVT_MENU (DO_TEST, MyFrame::DoTest)
24 EVT_SIZE ( MyFrame::OnSize)
25 EVT_BUTTON (BUTTON1, MyFrame::OnButton1)
26 END_EVENT_TABLE()
27 \end{verbatim}
28
29 The first two entries map menu commands to two different member functions. The EVT\_SIZE macro
30 doesn't need a window identifier, since normally you are only interested in the
31 current window's size events. (In fact you could intercept a particular window's size event
32 by using EVT\_CUSTOM(wxEVT\_SIZE, id, func).)
33
34 The EVT\_BUTTON macro demonstrates that the originating event does not have to come from
35 the window class implementing the event table - if the event source is a button within a panel within a frame, this will still
36 work, because event tables are searched up through the hierarchy of windows. In this
37 case, the button's event table will be searched, then the parent panel's, then the frame's.
38
39 As mentioned before, the member functions that handle events do not have to be virtual.
40 These member functions take an event argument, and the class of event differs according
41 to the type of event and the class of the originating window. For size
42 events, \helpref{wxSizeEvent}{wxsizeevent} is used. For menu commands and most control
43 commands (such as button presses), \helpref{wxCommandEvent}{wxcommandevent} is used.
44 When controls get more complicated, then specific event classes are used, such
45 as \helpref{wxTreeEvent}{wxtreeevent} for events from \helpref{wxTreeCtrl}{wxtreectrl} windows.
46
47 As well as the event table in the implementation file, there must be a DECLARE\_EVENT\_TABLE
48 macro in the class definition. For example:
49
50 {\small%
51 \begin{verbatim}
52 class MyFrame: public wxFrame {
53
54 DECLARE_DYNAMIC_CLASS(MyFrame)
55
56 public:
57 ...
58 void OnExit(wxCommandEvent& event);
59 void OnSize(wxSizeEvent& event);
60 protected:
61 int m_count;
62 ...
63 DECLARE_EVENT_TABLE()
64 };
65 \end{verbatim}
66 }%
67
68 \subsection{How events are processed}\label{eventprocessing}
69
70 When an event is received from the windowing system, wxWindows calls \helpref{wxEvtHandler::ProcessEvent}{wxevthandlerprocessevent} on
71 the first event handler object belonging to the window generating the event.
72
73 The normal order of event table searching by ProcessEvent is as follows:
74
75 \begin{enumerate}\itemsep=0pt
76 \item If the object is disabled (via a call to \helpref{wxEvtHandler::SetEvtHandlerEnabled}{wxevthandlersetevthandlerenabled})
77 the function skips to step (6).
78 \item If the object is a wxWindow, {\bf ProcessEvent} is recursively called on the window's\rtfsp
79 \helpref{wxValidator}{wxvalidator}. If this returns TRUE, the function exits.
80 \item {\bf SearchEventTable} is called for this event handler. If this fails, the base
81 class table is tried, and so on until no more tables exist or an appropriate function was found,
82 in which case the function exits.
83 \item The search is applied down the entire chain of event handlers (usually the chain has a length
84 of one). If this succeeds, the function exits.
85 \item If the object is a wxWindow and the event is a wxCommandEvent, {\bf ProcessEvent} is
86 recursively applied to the parent window's event handler. If this returns TRUE, the function exits.
87 \item Finally, {\bf ProcessEvent} is called on the wxApp object.
88 \end{enumerate}
89
90 Note that your application may wish to override ProcessEvent to redirect processing of
91 events. This is done in the document/view framework, for example, to allow event handlers
92 to be defined in the document or view.
93
94 \subsection{Pluggable event handlers}
95
96 In fact, you don't have to derive a new class from a window class
97 if you don't want to. You can derive a new class from wxEvtHandler instead,
98 defining the appropriate event table, and then call
99 \rtfsp\helpref{wxWindow::SetEventHandler}{wxwindowseteventhandler} (or, preferably,
100 \rtfsp\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler}) to make this
101 event handler the object that responds to events. This way, you can avoid
102 a lot of class derivation, and use the same event handler object to
103 handle events from instances of different classes. If you ever have to call a window's event handler
104 manually, use the GetEventHandler function to retrieve the window's event handler and use that
105 to call the member function. By default, GetEventHandler returns a pointer to the window itself
106 unless an application has redirected event handling using SetEventHandler or PushEventHandler.
107
108 One use of PushEventHandler is to temporarily or permanently change the
109 behaviour of the GUI. For example, you might want to invoke a dialog editor
110 in your application that changes aspects of dialog boxes. You can
111 grab all the input for an existing dialog box, and edit it `in situ',
112 before restoring its behaviour to normal. So even if the application
113 has derived new classes to customize behaviour, your utility can indulge
114 in a spot of body-snatching. It could be a useful technique for on-line
115 tutorials, too, where you take a user through a serious of steps and
116 don't want them to diverge from the lesson. Here, you can examine the events
117 coming from buttons and windows, and if acceptable, pass them through to
118 the original event handler. Use PushEventHandler/PopEventHandler
119 to form a chain of event handlers, where each handler processes a different
120 range of events independently from the other handlers.
121
122 \subsection{Event macros summary}\label{eventmacros}
123
124 \wxheading{Specifying an event table}
125
126 \twocolwidtha{8cm}%
127 \begin{twocollist}\itemsep=0pt
128 \twocolitem{\windowstyle{EVT\_CUSTOM(eventId, id, func)}}{Allows you to add a custom event table
129 entry by specifying the event identifier (such as wxEVT\_SIZE), the window identifier,
130 and a member function to call.}
131 \twocolitem{\windowstyle{EVT\_CUSTOM\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM,
132 but responds to a range of window identifiers.}
133 \end{twocollist}
134
135 \wxheading{Generic event table macros}
136
137 \twocolwidtha{8cm}%
138 \begin{twocollist}\itemsep=0pt
139 \twocolitem{\windowstyle{EVT\_CUSTOM(eventId, id, func)}}{Allows you to add a custom event table
140 entry by specifying the event identifier (such as wxEVT\_SIZE), the window identifier,
141 and a member function to call.}
142 \twocolitem{\windowstyle{EVT\_CUSTOM\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM,
143 but responds to a range of window identifiers.}
144 \twocolitem{\windowstyle{EVT\_COMMAND(eventId, id, func)}}{The same as EVT\_CUSTOM, but
145 expects a member function with a wxCommandEvent argument.}
146 \twocolitem{\windowstyle{EVT\_COMMAND\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM\_RANGE, but
147 expects a member function with a wxCommandEvent argument.}
148 \end{twocollist}
149
150 \wxheading{Macros listed by event class}
151
152 The documentation for specific event macros is organised by event class. Please refer
153 to these sections for details.
154
155 \twocolwidtha{8cm}%
156 \begin{twocollist}\itemsep=0pt
157 \twocolitem{\helpref{wxActivateEvent}{wxactivateevent}}{The EVT\_ACTIVATE and EVT\_ACTIVATE\_APP macros intercept
158 activation and deactivation events.}
159 \twocolitem{\helpref{wxCommandEvent}{wxcommandevent}}{A range of commonly-used control events.}
160 \twocolitem{\helpref{wxCloseEvent}{wxcloseevent}}{The EVT\_CLOSE macro handles window closure
161 called via \helpref{wxWindow::Close}{wxwindowclose}.}
162 \twocolitem{\helpref{wxDropFilesEvent}{wxdropfilesevent}}{The EVT\_DROP\_FILES macros handles
163 file drop events.}
164 \twocolitem{\helpref{wxEraseEvent}{wxeraseevent}}{The EVT\_ERASE\_BACKGROUND macro is used to handle window erase requests.}
165 \twocolitem{\helpref{wxFocusEvent}{wxfocusevent}}{The EVT\_SET\_FOCUS and EVT\_KILL\_FOCUS macros are used to handle keybaord focus events.}
166 \twocolitem{\helpref{wxKeyEvent}{wxkeyevent}}{EVT\_CHAR and EVT\_CHAR\_HOOK macros handle keyboard
167 input for any window.}
168 \twocolitem{\helpref{wxIdleEvent}{wxidleevent}}{The EVT\_IDLE macro handle application idle events
169 (to process background tasks, for example).}
170 \twocolitem{\helpref{wxInitDialogEvent}{wxinitdialogevent}}{The EVT\_INIT\_DIALOG macro is used
171 to handle dialog initialisation.}
172 \twocolitem{\helpref{wxListEvent}{wxlistevent}}{These macros handle \helpref{wxListCtrl}{wxlistctrl} events.}
173 \twocolitem{\helpref{wxMenuEvent}{wxmenuevent}}{These macros handle special menu events (not menu commands).}
174 \twocolitem{\helpref{wxMouseEvent}{wxmouseevent}}{Mouse event macros can handle either individual
175 mouse events or all mouse events.}
176 \twocolitem{\helpref{wxMoveEvent}{wxmoveevent}}{The EVT\_MOVE macro is used to handle a window move.}
177 \twocolitem{\helpref{wxUpdateUIEvent}{wxupdateuievent}}{The EVT\_UPDATE\_UI macro is used to handle user interface
178 update pseudo-events, which are generated to give the application the chance to update the visual state of menus,
179 toolbars and controls.}
180 \twocolitem{\helpref{wxPaintEvent}{wxpaintevent}}{The EVT\_PAINT macro is used to handle window paint requests.}
181 \twocolitem{\helpref{wxScrollEvent}{wxscrollevent}}{These macros are used to handle scroll events from
182 windows, \helpref{wxScrollBar}{wxscrollbar}, and \helpref{wxSpinButton}{wxspinbutton}.}
183 \twocolitem{\helpref{wxSizeEvent}{wxsizeevent}}{The EVT\_SIZE macro is used to handle a window resize.}
184 \twocolitem{\helpref{wxSysColourChangedEvent}{wxsyscolourchangedevent}}{The EVT\_SYS\_COLOUR\_CHANGED macro is used to handle
185 events informing the application that the user has changed the system colours (Windows only).}
186 \twocolitem{\helpref{wxTreeEvent}{wxtreeevent}}{These macros handle \helpref{wxTreeCtrl}{wxtreectrl} events.}
187 \end{twocollist}
188