]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/timer.tex
Fix "depreciated" to "deprecated".
[wxWidgets.git] / docs / latex / wx / timer.tex
CommitLineData
a660d684
KB
1\section{\class{wxTimer}}\label{wxtimer}
2
2531c7e3
VZ
3The wxTimer class allows you to execute code at specified intervals. Its
4precision is platform-dependent, but in general will not be better than 1ms nor
5worse than 1s.
6
2cb94e7a 7There are three different ways to use this class:
2531c7e3
VZ
8
9\begin{enumerate}
10\item You may derive a new class from wxTimer and override the
11\helpref{Notify}{wxtimernotify} member to perform the required action.
12\item Or you may redirect the notifications to any
154b6b0f 13\helpref{wxEvtHandler}{wxevthandler} derived object by using the non-default
01ebf752 14constructor or \helpref{SetOwner}{wxtimersetowner}. Then use the {\tt EVT\_TIMER}
2531c7e3
VZ
15macro to connect it to the event handler which will receive
16\helpref{wxTimerEvent}{wxtimerevent} notifications.
01ebf752
JS
17\item Or you may use a derived class and the {\tt EVT\_TIMER}
18macro to connect it to an event handler defined in the derived class.
19If the default constructor is used, the timer object will be its
20own owner object, since it is derived from wxEvtHandler.
2531c7e3
VZ
21\end{enumerate}
22
23In any case, you must start the timer with \helpref{Start}{wxtimerstart}
24after constructing it before it actually starts sending notifications. It can
25be stopped later with \helpref{Stop}{wxtimerstop}.
a660d684 26
2cb94e7a 27{\bf Note:} A timer can only be used from the main thread.
2b5f62a0 28
a660d684
KB
29\wxheading{Derived from}
30
01ebf752 31\helpref{wxEvtHandler}{wxevthandler}
641d44ae 32\helpref{wxObject}{wxobject}
a660d684 33
954b8ae6
JS
34\wxheading{Include files}
35
36<wx/timer.h>
37
a660d684
KB
38\wxheading{See also}
39
5f445b31 40\helpref{::wxStartTimer}{wxstarttimer}, \helpref{::wxGetElapsedTime}{wxgetelapsedtime}, \helpref{wxStopWatch}{wxstopwatch}
a660d684
KB
41
42\latexignore{\rtfignore{\wxheading{Members}}}
43
f6bcfd97 44\membersection{wxTimer::wxTimer}\label{wxtimerwxtimer}
a660d684
KB
45
46\func{}{wxTimer}{\void}
47
2531c7e3
VZ
48Default constructor. If you use it to construct the object and don't call
49\helpref{SetOwner}{wxtimersetowner} later, you must override
50\helpref{Notify}{wxtimernotify} method to process the notifications.
51
2531c7e3
VZ
52\func{}{wxTimer}{\param{wxEvtHandler *}{owner}, \param{int }{id = -1}}
53
54Creates a timer and associates it with {\it owner}. Please see
55\helpref{SetOwner}{wxtimersetowner} for the description of parameters.
a660d684 56
6d06e061 57\membersection{wxTimer::\destruct{wxTimer}}\label{wxtimerdtor}
a660d684
KB
58
59\func{}{\destruct{wxTimer}}{\void}
60
2531c7e3
VZ
61Destructor. Stops the timer if it is running.
62
44cf9b50 63\membersection{wxTimer::GetInterval}\label{wxtimergetinterval}
2531c7e3
VZ
64
65\constfunc{int}{GetInterval}{\void}
66
67Returns the current interval for the timer (in milliseconds).
68
f70f1f25
VZ
69\membersection{wxTimer::GetOwner}\label{wxtimergetowner}
70
71\constfunc{wxEvtHandler}{GetOwner}{\void}
72
73Returns the current {\it owner} of the timer.
74If non-\NULL this is the event handler which will receive the
75\helpref{timer events}{wxtimerevent} when the timer is running.
76
77\membersection{wxTimer::GetId}\label{wxtimergetid}
78
79\constfunc{int}{GetId}{\void}
80
81Returns the ID of the events generated by this timer.
82
457e6c54 83\membersection{wxTimer::IsOneShot}\label{wxtimerisoneshot}
2531c7e3
VZ
84
85\constfunc{bool}{IsOneShot}{\void}
86
cc81d32f 87Returns {\tt true} if the timer is one shot, i.e.\ if it will stop after firing the
2531c7e3 88first notification automatically.
a660d684 89
457e6c54 90\membersection{wxTimer::IsRunning}\label{wxtimerisrunning}
a660d684 91
2531c7e3 92\constfunc{bool}{IsRunning}{\void}
a660d684 93
cc81d32f 94Returns {\tt true} if the timer is running, {\tt false} if it is stopped.
a660d684 95
457e6c54 96\membersection{wxTimer::Notify}\label{wxtimernotify}
a660d684
KB
97
98\func{void}{Notify}{\void}
99
2531c7e3
VZ
100This member should be overridden by the user if the default constructor was
101used and \helpref{SetOwner}{wxtimersetowner} wasn't called.
a660d684 102
2531c7e3
VZ
103Perform whatever action which is to be taken periodically here.
104
457e6c54 105\membersection{wxTimer::SetOwner}\label{wxtimersetowner}
2531c7e3
VZ
106
107\func{void}{SetOwner}{\param{wxEvtHandler *}{owner}, \param{int }{id = -1}}
108
b0a2d8a8 109Associates the timer with the given {\it owner}\/ object. When the timer is
2531c7e3 110running, the owner will receive \helpref{timer events}{wxtimerevent} with
b0a2d8a8 111id equal to {\it id}\/ specified here.
2531c7e3 112
457e6c54 113\membersection{wxTimer::Start}\label{wxtimerstart}
a660d684 114
e7dfcb8e 115\func{bool}{Start}{\param{int }{milliseconds = -1}, \param{bool }{oneShot = {\tt false}}}
a660d684 116
b0a2d8a8 117(Re)starts the timer. If {\it milliseconds}\/ parameter is -1 (value by default),
cc81d32f
VS
118the previous value is used. Returns {\tt false} if the timer could not be started,
119{\tt true} otherwise (in MS Windows timers are a limited resource).
b0a2d8a8 120
cc81d32f
VS
121If {\it oneShot}\/ is {\tt false} (the default), the \helpref{Notify}{wxtimernotify}
122function will be called repeatedly until the timer is stopped. If {\tt true},
b0a2d8a8 123it will be called only once and the timer will stop automatically. To make your
d2c2afc9
JS
124code more readable you may also use the following symbolic constants:
125
126\twocolwidtha{5cm}
b0a2d8a8
VZ
127\begin{twocollist}\itemsep=0pt
128\twocolitem{wxTIMER\_CONTINUOUS}{Start a normal, continuously running, timer}
129\twocolitem{wxTIMER\_ONE\_SHOT}{Start a one shot timer}
130\end{twocollist}
a660d684 131
99646f7e
VZ
132If the timer was already running, it will be stopped by this method before
133restarting it.
134
457e6c54 135\membersection{wxTimer::Stop}\label{wxtimerstop}
a660d684
KB
136
137\func{void}{Stop}{\void}
138
139Stops the timer.
140
f70f1f25
VZ
141
142
143
144
145%% wxTimerEvent
146
147
2531c7e3
VZ
148\section{\class{wxTimerEvent}}\label{wxtimerevent}
149
150wxTimerEvent object is passed to the event handler of timer events.
151
152For example:
153
154\begin{verbatim}
155class MyFrame : public wxFrame
156{
157public:
158 ...
159 void OnTimer(wxTimerEvent& event);
160
161private:
162 wxTimer m_timer;
163};
164
165BEGIN_EVENT_TABLE(MyFrame, wxFrame)
166 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
167END_EVENT_TABLE()
168
169MyFrame::MyFrame()
170 : m_timer(this, TIMER_ID)
171{
172 m_timer.Start(1000); // 1 second interval
173}
174
175void MyFrame::OnTimer(wxTimerEvent& event)
176{
177 // do whatever you want to do every second here
178}
179
180\end{verbatim}
181
b19d9289
MR
182\wxheading{Derived from}
183
184\helpref{wxEvent}{wxevent}
185
2531c7e3
VZ
186\wxheading{Include files}
187
188<wx/timer.h>
189
190\wxheading{See also}
191
192\helpref{wxTimer}{wxtimer}
193
194\latexignore{\rtfignore{\wxheading{Members}}}
195
457e6c54 196\membersection{wxTimerEvent::GetInterval}\label{wxtimereventgetinterval}
2531c7e3
VZ
197
198\constfunc{int}{GetInterval}{\void}
199
200Returns the interval of the timer which generated this event.
201
2cd78b2a
VZ
202
203\membersection{wxTimerEvent::GetTimer}\label{wxtimereventgettimer}
204
205\constfunc{wxTimer\&}{GetTimer}{\void}
206
207Returns the timer object which generated this event.
208