]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/tevent.tex
ODBC updates
[wxWidgets.git] / docs / latex / wx / tevent.tex
CommitLineData
a660d684
KB
1\section{Event handling overview}\label{eventhandlingoverview}
2
3Classes: \helpref{wxEvtHandler}{wxevthandler}, \helpref{wxWindow}{wxwindow}, \helpref{wxEvent}{wxevent}
4
5\subsection{Introduction}
6
7Before version 2.0 of wxWindows, events were handled by the application
8either by supplying callback functions, or by overriding virtual member
9functions such as {\bf OnSize}.
10
11From wxWindows 2.0, {\it event tables} are used instead, with a few exceptions.
12
13An event table is placed in an implementation file to tell wxWindows how to map
14events to member functions. These member functions are not virtual functions, but
15they all similar in form: they take a single wxEvent-derived argument, and have a void return
16type.
17
18Here's an example of an event table.
19
20\begin{verbatim}
21BEGIN_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)
26END_EVENT_TABLE()
27\end{verbatim}
28
29The first two entries map menu commands to two different member functions. The EVT\_SIZE macro
30doesn't need a window identifier, since normally you are only interested in the
31current window's size events. (In fact you could intercept a particular window's size event
32by using EVT\_CUSTOM(wxEVT\_SIZE, id, func).)
33
34The EVT\_BUTTON macro demonstrates that the originating event does not have to come from
35the window class implementing the event table - if the event source is a button within a panel within a frame, this will still
36work, because event tables are searched up through the hierarchy of windows. In this
37case, the button's event table will be searched, then the parent panel's, then the frame's.
38
39As mentioned before, the member functions that handle events do not have to be virtual.
40These member functions take an event argument, and the class of event differs according
41to the type of event and the class of the originating window. For size
42events, \helpref{wxSizeEvent}{wxsizeevent} is used. For menu commands and most control
43commands (such as button presses), \helpref{wxCommandEvent}{wxcommandevent} is used.
44When controls get more complicated, then specific event classes are used, such
45as \helpref{wxTreeEvent}{wxtreeevent} for events from \helpref{wxTreeCtrl}{wxtreectrl} windows.
46
47As well as the event table in the implementation file, there must be a DECLARE\_EVENT\_TABLE
48macro in the class definition. For example:
49
50{\small%
51\begin{verbatim}
52class MyFrame: public wxFrame {
53
54 DECLARE_DYNAMIC_CLASS(MyFrame)
55
56public:
57 ...
58 void OnExit(wxCommandEvent& event);
59 void OnSize(wxSizeEvent& event);
60protected:
61 int m_count;
62 ...
63 DECLARE_EVENT_TABLE()
64};
65\end{verbatim}
66}%
67
68\subsection{How events are processed}\label{eventprocessing}
69
70When an event is received from the windowing system, wxWindows calls \helpref{wxEvtHandler::ProcessEvent}{wxevthandlerprocessevent} on
71the first event handler object belonging to the window generating the event.
72
73The 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})
77the 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
81class table is tried, and so on until no more tables exist or an appropriate function was found,
82in which case the function exits.
83\item The search is applied down the entire chain of event handlers (usually the chain has a length
84of one). If this succeeds, the function exits.
85\item If the object is a wxWindow and the event is a wxCommandEvent, {\bf ProcessEvent} is
86recursively 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
90Note that your application may wish to override ProcessEvent to redirect processing of
91events. This is done in the document/view framework, for example, to allow event handlers
92to be defined in the document or view.
93
94\subsection{Pluggable event handlers}
95
96In fact, you don't have to derive a new class from a window class
97if you don't want to. You can derive a new class from wxEvtHandler instead,
98defining the appropriate event table, and then call
99\rtfsp\helpref{wxWindow::SetEventHandler}{wxwindowseteventhandler} (or, preferably,
100\rtfsp\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler}) to make this
101event handler the object that responds to events. This way, you can avoid
102a lot of class derivation, and use the same event handler object to
103handle events from instances of different classes. If you ever have to call a window's event handler
104manually, use the GetEventHandler function to retrieve the window's event handler and use that
105to call the member function. By default, GetEventHandler returns a pointer to the window itself
106unless an application has redirected event handling using SetEventHandler or PushEventHandler.
107
108One use of PushEventHandler is to temporarily or permanently change the
109behaviour of the GUI. For example, you might want to invoke a dialog editor
110in your application that changes aspects of dialog boxes. You can
111grab all the input for an existing dialog box, and edit it `in situ',
112before restoring its behaviour to normal. So even if the application
113has derived new classes to customize behaviour, your utility can indulge
114in a spot of body-snatching. It could be a useful technique for on-line
115tutorials, too, where you take a user through a serious of steps and
116don't want them to diverge from the lesson. Here, you can examine the events
117coming from buttons and windows, and if acceptable, pass them through to
118the original event handler. Use PushEventHandler/PopEventHandler
119to form a chain of event handlers, where each handler processes a different
120range 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
129entry by specifying the event identifier (such as wxEVT\_SIZE), the window identifier,
130and a member function to call.}
131\twocolitem{\windowstyle{EVT\_CUSTOM\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM,
132but 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
140entry by specifying the event identifier (such as wxEVT\_SIZE), the window identifier,
141and a member function to call.}
142\twocolitem{\windowstyle{EVT\_CUSTOM\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM,
143but responds to a range of window identifiers.}
144\twocolitem{\windowstyle{EVT\_COMMAND(eventId, id, func)}}{The same as EVT\_CUSTOM, but
145expects a member function with a wxCommandEvent argument.}
146\twocolitem{\windowstyle{EVT\_COMMAND\_RANGE(eventId, id1, id2, func)}}{The same as EVT\_CUSTOM\_RANGE, but
147expects a member function with a wxCommandEvent argument.}
148\end{twocollist}
149
150\wxheading{Macros listed by event class}
151
152The documentation for specific event macros is organised by event class. Please refer
153to 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
158activation 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
161called via \helpref{wxWindow::Close}{wxwindowclose}.}
162\twocolitem{\helpref{wxDropFilesEvent}{wxdropfilesevent}}{The EVT\_DROP\_FILES macros handles
163file 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
167input 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
171to 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
175mouse 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
178update pseudo-events, which are generated to give the application the chance to update the visual state of menus,
179toolbars 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
182windows, \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
185events 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