]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/evthand.tex
Patches-1851591 ] dataview sample is crashing (under MSW)
[wxWidgets.git] / docs / latex / wx / evthand.tex
CommitLineData
a660d684
KB
1\section{\class{wxEvtHandler}}\label{wxevthandler}
2
3A class that can handle events from the windowing system.
4wxWindow (and therefore all window classes) are derived from
5this class.
6
a685e84a
DE
7When events are received, wxEvtHandler invokes the method listed in the
8event table using itself as the object. When using multiple inheritance
9it is imperative that the wxEvtHandler(-derived) class be the first
10class inherited such that the "this" pointer for the overall object
11will be identical to the "this" pointer for the wxEvtHandler portion.
12
a660d684
KB
13\wxheading{Derived from}
14
15\helpref{wxObject}{wxobject}
16
954b8ae6
JS
17\wxheading{Include files}
18
19<wx/event.h>
20
a7af285d
VZ
21\wxheading{Library}
22
23\helpref{wxBase}{librarieslist}
24
a660d684
KB
25\wxheading{See also}
26
27\overview{Event handling overview}{eventhandlingoverview}
28
29\latexignore{\rtfignore{\wxheading{Members}}}
30
b236c10f 31\membersection{wxEvtHandler::wxEvtHandler}\label{wxevthandlerctor}
a660d684
KB
32
33\func{}{wxEvtHandler}{\void}
34
35Constructor.
36
b236c10f 37\membersection{wxEvtHandler::\destruct{wxEvtHandler}}\label{wxevthandlerdtor}
a660d684
KB
38
39\func{}{\destruct{wxEvtHandler}}{\void}
40
41Destructor. If the handler is part of a chain, the destructor will
42unlink itself and restore the previous and next handlers so that they point to
43each other.
44
8a293590
RR
45\membersection{wxEvtHandler::AddPendingEvent}\label{wxevthandleraddpendingevent}
46
76bfdc9a 47\func{virtual void}{AddPendingEvent}{\param{const wxEvent\& }{event}}
8a293590 48
8e72b8b5 49This function posts an event to be processed later.
8a293590
RR
50
51\wxheading{Parameters}
52
53\docparam{event}{Event to add to process queue.}
54
55\wxheading{Remarks}
56
8e72b8b5
RR
57The difference between sending an event (using the
58\helpref{ProcessEvent}{wxevthandlerprocessevent} method) and posting it is
59that in the first case the event is processed before the function returns,
60while in the second case, the function returns immediately and the event will
61be processed sometime later (usually during the next event loop iteration).
62
63A copy of {\it event} is made by the function, so the original can be deleted
64as soon as function returns (it is common that the original is created on the
65stack). This requires that the \helpref{wxEvent::Clone}{wxeventclone} method
66be implemented by {\it event} so that it can be duplicated and stored until
67it gets processed.
68
69This is also the method to call for inter-thread communication---it will
70post events safely between different threads which means that this method is
71thread-safe by using critical sections where needed. In a multi-threaded
72program, you often need to inform the main GUI thread about the status of
73other working threads and such notification should be done using this method.
74
75This method automatically wakes up idle handling if the underlying window
76system is currently idle and thus would not send any idle events. (Waking
77up idle handling is done calling \helpref{::wxWakeUpIdle}{wxwakeupidle}.)
8a293590 78
f60d0f94
JS
79\membersection{wxEvtHandler::Connect}\label{wxevthandlerconnect}
80
18e33484 81\func{void}{Connect}{\param{int}{ id}, \param{int}{ lastId},
f60d0f94 82 \param{wxEventType }{eventType}, \param{wxObjectEventFunction}{ function},
97e1d37c 83 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
f60d0f94 84
18e33484 85\func{void}{Connect}{\param{int}{ id},
f60d0f94 86 \param{wxEventType }{eventType}, \param{wxObjectEventFunction}{ function},
97e1d37c 87 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
f60d0f94 88
18e33484
VZ
89\func{void}{Connect}{\param{wxEventType }{eventType}, \param{wxObjectEventFunction}{ function},
90 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
91
f60d0f94 92Connects the given function dynamically with the event handler, id and event type. This
f70c0443 93is an alternative to the use of static event tables. See the 'event' or the old 'dynamic' sample for usage.
f60d0f94
JS
94
95\wxheading{Parameters}
96
18e33484
VZ
97\docparam{id}{The identifier (or first of the identifier range) to be
98associated with the event handler function. For the version not taking this
99argument, it defaults to \texttt{wxID\_ANY}.}
f60d0f94
JS
100
101\docparam{lastId}{The second part of the identifier range to be associated with the event handler function.}
102
103\docparam{eventType}{The event type to be associated with this event handler.}
104
18e33484
VZ
105\docparam{function}{The event handler function. Note that this function should
106be explicitly converted to the correct type which can be done using a macro
8b2e0c3a 107called \texttt{wxFooEventHandler} for the handler for any \texttt{wxFooEvent}.}
f60d0f94
JS
108
109\docparam{userData}{Data to be associated with the event table entry.}
110
97e1d37c 111\docparam{eventSink}{Object whose member function should be called. If this is NULL,
18e33484 112\textit{this} will be used.}
97e1d37c 113
f60d0f94
JS
114\wxheading{Example}
115
116\begin{verbatim}
117 frame->Connect( wxID_EXIT,
118 wxEVT_COMMAND_MENU_SELECTED,
18e33484 119 wxCommandEventHandler(MyFrame::OnQuit) );
f60d0f94
JS
120\end{verbatim}
121
2bbd97f4
MB
122\perlnote{In wxPerl this function takes 4 arguments: \texttt{id,
123lastid, type, method}; if \texttt{method} is \texttt{undef}, the
124handler is disconnected.}
125
8a293590
RR
126\membersection{wxEvtHandler::Disconnect}\label{wxevthandlerdisconnect}
127
bdbe28fa
VZ
128\func{bool}{Disconnect}{\param{wxEventType }{eventType = wxEVT\_NULL}, \param{wxObjectEventFunction}{ function = NULL},
129 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
130
e554d626 131\func{bool}{Disconnect}{\param{int}{ id = \texttt{wxID\_ANY}},
605d715d 132 \param{wxEventType }{eventType = wxEVT\_NULL}, \param{wxObjectEventFunction}{ function = NULL},
97e1d37c 133 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
8a293590 134
e554d626 135\func{bool}{Disconnect}{\param{int}{ id}, \param{int}{ lastId = \texttt{wxID\_ANY}},
605d715d 136 \param{wxEventType }{eventType = wxEVT\_NULL}, \param{wxObjectEventFunction}{ function = NULL},
97e1d37c 137 \param{wxObject*}{ userData = NULL}, \param{wxEvtHandler*}{ eventSink = NULL}}
a660d684 138
8a293590 139Disconnects the given function dynamically from the event handler, using the specified
cc81d32f 140parameters as search criteria and returning true if a matching function has been
ccaaf5b0 141found and removed. This method can only disconnect functions which have been added
8a293590 142using the \helpref{wxEvtHandler::Connect}{wxevthandlerconnect} method. There is no way
ccaaf5b0 143to disconnect functions connected using the (static) event tables.
a660d684 144
8a293590 145\wxheading{Parameters}
a660d684 146
8a293590 147\docparam{id}{The identifier (or first of the identifier range) associated with the event handler function.}
a660d684 148
8a293590 149\docparam{lastId}{The second part of the identifier range associated with the event handler function.}
a660d684 150
8a293590 151\docparam{eventType}{The event type associated with this event handler.}
a660d684 152
8a293590 153\docparam{function}{The event handler function.}
a660d684 154
ccaaf5b0 155\docparam{userData}{Data associated with the event table entry.}
a660d684 156
97e1d37c
JS
157\docparam{eventSink}{Object whose member function should be called.}
158
2bbd97f4
MB
159\perlnote{In wxPerl this function takes 3 arguments: \texttt{id,
160lastid, type}.}
161
a660d684
KB
162\membersection{wxEvtHandler::GetClientData}\label{wxevthandlergetclientdata}
163
ccaaf5b0 164\func{void* }{GetClientData}{\void}
a660d684
KB
165
166Gets user-supplied client data.
167
168\wxheading{Remarks}
169
170Normally, any extra data the programmer wishes to associate with the object
ccaaf5b0 171should be made available by deriving a new class with new data members.
a660d684
KB
172
173\wxheading{See also}
174
175\helpref{wxEvtHandler::SetClientData}{wxevthandlersetclientdata}
176
e3ba9f88
RR
177\membersection{wxEvtHandler::GetClientObject}\label{wxevthandlergetclientobject}
178
179\constfunc{wxClientData*}{GetClientObject}{\void}
180
181Get a pointer to the user-supplied client data object.
182
183\wxheading{See also}
184
185\helpref{wxEvtHandler::SetClientObject}{wxevthandlersetclientobject},
186\helpref{wxClientData}{wxclientdata}
187
a660d684
KB
188\membersection{wxEvtHandler::GetEvtHandlerEnabled}\label{wxevthandlergetevthandlerenabled}
189
190\func{bool}{GetEvtHandlerEnabled}{\void}
191
cc81d32f 192Returns true if the event handler is enabled, false otherwise.
a660d684
KB
193
194\wxheading{See also}
195
196\helpref{wxEvtHandler::SetEvtHandlerEnabled}{wxevthandlersetevthandlerenabled}
197
198\membersection{wxEvtHandler::GetNextHandler}\label{wxevthandlergetnexthandler}
199
200\func{wxEvtHandler*}{GetNextHandler}{\void}
201
202Gets the pointer to the next handler in the chain.
203
204\wxheading{See also}
205
b4a2ab72 206\helpref{wxEvtHandler::SetNextHandler}{wxevthandlersetnexthandler},\rtfsp
a660d684
KB
207\helpref{wxEvtHandler::GetPreviousHandler}{wxevthandlergetprevioushandler},\rtfsp
208\helpref{wxEvtHandler::SetPreviousHandler}{wxevthandlersetprevioushandler},\rtfsp
a660d684
KB
209\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp
210\helpref{wxWindow::PopEventHandler}{wxwindowpopeventhandler}
211
212\membersection{wxEvtHandler::GetPreviousHandler}\label{wxevthandlergetprevioushandler}
213
214\func{wxEvtHandler*}{GetPreviousHandler}{\void}
215
216Gets the pointer to the previous handler in the chain.
217
218\wxheading{See also}
219
a660d684 220\helpref{wxEvtHandler::SetPreviousHandler}{wxevthandlersetprevioushandler},\rtfsp
b4a2ab72
SB
221\helpref{wxEvtHandler::GetNextHandler}{wxevthandlergetnexthandler},\rtfsp
222\helpref{wxEvtHandler::SetNextHandler}{wxevthandlersetnexthandler},\rtfsp
a660d684
KB
223\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp
224\helpref{wxWindow::PopEventHandler}{wxwindowpopeventhandler}
225
226\membersection{wxEvtHandler::ProcessEvent}\label{wxevthandlerprocessevent}
227
228\func{virtual bool}{ProcessEvent}{\param{wxEvent\& }{event}}
229
230Processes an event, searching event tables and calling zero or more suitable event handler function(s).
231
232\wxheading{Parameters}
233
234\docparam{event}{Event to process.}
235
236\wxheading{Return value}
237
cc81d32f 238true if a suitable event handler function was found and executed, and the function did not
a660d684
KB
239call \helpref{wxEvent::Skip}{wxeventskip}.
240
241\wxheading{Remarks}
242
fc2171bd 243Normally, your application would not call this function: it is called in the wxWidgets
a660d684
KB
244implementation to dispatch incoming user interface events to the framework (and application).
245
246However, you might need to call it if implementing new functionality (such as a new control) where
247you define new event types, as opposed to allowing the user to override virtual functions.
248
249An instance where you might actually override the {\bf ProcessEvent} function is where you want
fc2171bd 250to direct event processing to event handlers not normally noticed by wxWidgets. For example,
a660d684
KB
251in the document/view architecture, documents and views are potential event handlers.
252When an event reaches a frame, {\bf ProcessEvent} will need to be called on the associated
253document and view in case event handler functions are associated with these objects.
254The property classes library (wxProperty) also overrides {\bf ProcessEvent} for similar reasons.
255
256The normal order of event table searching is as follows:
257
258\begin{enumerate}\itemsep=0pt
259\item If the object is disabled (via a call to \helpref{wxEvtHandler::SetEvtHandlerEnabled}{wxevthandlersetevthandlerenabled})
260the function skips to step (6).
261\item If the object is a wxWindow, {\bf ProcessEvent} is recursively called on the window's\rtfsp
cc81d32f 262\helpref{wxValidator}{wxvalidator}. If this returns true, the function exits.
a660d684
KB
263\item {\bf SearchEventTable} is called for this event handler. If this fails, the base
264class table is tried, and so on until no more tables exist or an appropriate function was found,
265in which case the function exits.
266\item The search is applied down the entire chain of event handlers (usually the chain has a length
267of one). If this succeeds, the function exits.
268\item If the object is a wxWindow and the event is a wxCommandEvent, {\bf ProcessEvent} is
cc81d32f 269recursively applied to the parent window's event handler. If this returns true, the function exits.
a660d684
KB
270\item Finally, {\bf ProcessEvent} is called on the wxApp object.
271\end{enumerate}
272
273\wxheading{See also}
274
275\helpref{wxEvtHandler::SearchEventTable}{wxevthandlersearcheventtable}
276
937013e0
VS
277
278\membersection{wxEvtHandler::SafelyProcessEvent}\label{wxevthandlersafelyprocessevent}
279
280\func{bool}{SafelyProcessEvent}{\param{wxEvent\& }{event}}
281
282Processes an event by calling \helpref{ProcessEvent}{wxevthandlerprocessevent}
283and handles any exceptions that occur in the process. If an exception is
284thrown in event handler, \helpref{wxApp::OnExceptionInMainLoop}{wxapponexceptioninmainloop}
285is called.
286
287\wxheading{Parameters}
288
289\docparam{event}{Event to process.}
290
291\wxheading{Return value}
292
293\true if the event was processed, \false if no handler was found or an
294exception was thrown.
295
296\wxheading{See also}
297
298\helpref{wxWindow::HandleWindowEvent}{wxwindowhandlewindowevent}
299
300
a660d684
KB
301\membersection{wxEvtHandler::SearchEventTable}\label{wxevthandlersearcheventtable}
302
4cee05ac 303\func{virtual bool}{SearchEventTable}{\param{wxEventTable\& }{table}, \param{wxEvent\& }{event}}
a660d684
KB
304
305Searches the event table, executing an event handler function if an appropriate one
306is found.
307
308\wxheading{Parameters}
309
310\docparam{table}{Event table to be searched.}
311
312\docparam{event}{Event to be matched against an event table entry.}
313
314\wxheading{Return value}
315
cc81d32f 316true if a suitable event handler function was found and executed, and the function did not
a660d684
KB
317call \helpref{wxEvent::Skip}{wxeventskip}.
318
319\wxheading{Remarks}
320
321This function looks through the object's event table and tries to find an entry
322that will match the event.
323
324An entry will match if:
325
326\begin{enumerate}\itemsep=0pt
327\item The event type matches, and
328\item the identifier or identifier range matches, or the event table entry's identifier is zero.
329\end{enumerate}
330
331If a suitable function is called but calls \helpref{wxEvent::Skip}{wxeventskip}, this function will
332fail, and searching will continue.
333
334\wxheading{See also}
335
336\helpref{wxEvtHandler::ProcessEvent}{wxevthandlerprocessevent}
337
338\membersection{wxEvtHandler::SetClientData}\label{wxevthandlersetclientdata}
339
ccaaf5b0 340\func{void}{SetClientData}{\param{void* }{data}}
a660d684
KB
341
342Sets user-supplied client data.
343
344\wxheading{Parameters}
345
346\docparam{data}{Data to be associated with the event handler.}
347
348\wxheading{Remarks}
349
ccaaf5b0
RR
350Normally, any extra data the programmer wishes to associate with
351the object should be made available by deriving a new class
e3ba9f88
RR
352with new data members. You must not call this method and
353\helpref{SetClientObject}{wxevthandlersetclientobject} on the
354same class - only one of them.
a660d684
KB
355
356\wxheading{See also}
357
358\helpref{wxEvtHandler::GetClientData}{wxevthandlergetclientdata}
359
e3ba9f88
RR
360\membersection{wxEvtHandler::SetClientObject}\label{wxevthandlersetclientobject}
361
362\func{void}{SetClientObject}{\param{wxClientData* }{data}}
363
364Set the client data object. Any previous object will be deleted.
365
366\wxheading{See also}
367
368\helpref{wxEvtHandler::GetClientObject}{wxevthandlergetclientobject},
369\helpref{wxClientData}{wxclientdata}
370
a660d684
KB
371\membersection{wxEvtHandler::SetEvtHandlerEnabled}\label{wxevthandlersetevthandlerenabled}
372
373\func{void}{SetEvtHandlerEnabled}{\param{bool }{enabled}}
374
375Enables or disables the event handler.
376
377\wxheading{Parameters}
378
cc81d32f 379\docparam{enabled}{true if the event handler is to be enabled, false if it is to be disabled.}
a660d684
KB
380
381\wxheading{Remarks}
382
383You can use this function to avoid having to remove the event handler from the chain, for example
384when implementing a dialog editor and changing from edit to test mode.
385
386\wxheading{See also}
387
388\helpref{wxEvtHandler::GetEvtHandlerEnabled}{wxevthandlergetevthandlerenabled}
389
390\membersection{wxEvtHandler::SetNextHandler}\label{wxevthandlersetnexthandler}
391
392\func{void}{SetNextHandler}{\param{wxEvtHandler* }{handler}}
393
394Sets the pointer to the next handler.
395
396\wxheading{Parameters}
397
398\docparam{handler}{Event handler to be set as the next handler.}
399
400\wxheading{See also}
401
402\helpref{wxEvtHandler::GetNextHandler}{wxevthandlergetnexthandler},\rtfsp
403\helpref{wxEvtHandler::SetPreviousHandler}{wxevthandlersetprevioushandler},\rtfsp
404\helpref{wxEvtHandler::GetPreviousHandler}{wxevthandlergetprevioushandler},\rtfsp
405\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp
406\helpref{wxWindow::PopEventHandler}{wxwindowpopeventhandler}
407
408\membersection{wxEvtHandler::SetPreviousHandler}\label{wxevthandlersetprevioushandler}
409
410\func{void}{SetPreviousHandler}{\param{wxEvtHandler* }{handler}}
411
412Sets the pointer to the previous handler.
413
414\wxheading{Parameters}
415
416\docparam{handler}{Event handler to be set as the previous handler.}
417
418\wxheading{See also}
419
420\helpref{wxEvtHandler::GetPreviousHandler}{wxevthandlergetprevioushandler},\rtfsp
421\helpref{wxEvtHandler::SetNextHandler}{wxevthandlersetnexthandler},\rtfsp
422\helpref{wxEvtHandler::GetNextHandler}{wxevthandlergetnexthandler},\rtfsp
423\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp
424\helpref{wxWindow::PopEventHandler}{wxwindowpopeventhandler}
425
426