]>
Commit | Line | Data |
---|---|---|
a660d684 KB |
1 | \section{\class{wxTimer}}\label{wxtimer} |
2 | ||
2531c7e3 VZ |
3 | The wxTimer class allows you to execute code at specified intervals. Its |
4 | precision is platform-dependent, but in general will not be better than 1ms nor | |
5 | worse than 1s. | |
6 | ||
7 | There are two different ways to use this class: | |
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 | |
13 | \helpref{wxEvtHandler}{wxevthandler} derived object by using the non default | |
14 | constructor or \helpref{SetOwner}{wxtimersetowner}. Then use {\tt EVT\_TIMER} | |
15 | macro to connect it to the event handler which will receive | |
16 | \helpref{wxTimerEvent}{wxtimerevent} notifications. | |
17 | \end{enumerate} | |
18 | ||
19 | In any case, you must start the timer with \helpref{Start}{wxtimerstart} | |
20 | after constructing it before it actually starts sending notifications. It can | |
21 | be stopped later with \helpref{Stop}{wxtimerstop}. | |
a660d684 | 22 | |
2b5f62a0 VZ |
23 | {\bf NB:} note that timer can only be used from the main thread currently. |
24 | ||
a660d684 KB |
25 | \wxheading{Derived from} |
26 | ||
27 | \helpref{wxObject}{wxobject} | |
28 | ||
954b8ae6 JS |
29 | \wxheading{Include files} |
30 | ||
31 | <wx/timer.h> | |
32 | ||
a660d684 KB |
33 | \wxheading{See also} |
34 | ||
5f445b31 | 35 | \helpref{::wxStartTimer}{wxstarttimer}, \helpref{::wxGetElapsedTime}{wxgetelapsedtime}, \helpref{wxStopWatch}{wxstopwatch} |
a660d684 KB |
36 | |
37 | \latexignore{\rtfignore{\wxheading{Members}}} | |
38 | ||
f6bcfd97 | 39 | \membersection{wxTimer::wxTimer}\label{wxtimerwxtimer} |
a660d684 KB |
40 | |
41 | \func{}{wxTimer}{\void} | |
42 | ||
2531c7e3 VZ |
43 | Default constructor. If you use it to construct the object and don't call |
44 | \helpref{SetOwner}{wxtimersetowner} later, you must override | |
45 | \helpref{Notify}{wxtimernotify} method to process the notifications. | |
46 | ||
2531c7e3 VZ |
47 | \func{}{wxTimer}{\param{wxEvtHandler *}{owner}, \param{int }{id = -1}} |
48 | ||
49 | Creates a timer and associates it with {\it owner}. Please see | |
50 | \helpref{SetOwner}{wxtimersetowner} for the description of parameters. | |
a660d684 KB |
51 | |
52 | \membersection{wxTimer::\destruct{wxTimer}} | |
53 | ||
54 | \func{}{\destruct{wxTimer}}{\void} | |
55 | ||
2531c7e3 VZ |
56 | Destructor. Stops the timer if it is running. |
57 | ||
58 | \membersection{wxTimer::GetInterval}{wxtimergetinterval} | |
59 | ||
60 | \constfunc{int}{GetInterval}{\void} | |
61 | ||
62 | Returns the current interval for the timer (in milliseconds). | |
63 | ||
457e6c54 | 64 | \membersection{wxTimer::IsOneShot}\label{wxtimerisoneshot} |
2531c7e3 VZ |
65 | |
66 | \constfunc{bool}{IsOneShot}{\void} | |
67 | ||
b0a2d8a8 | 68 | Returns {\tt TRUE} if the timer is one shot, i.e.\ if it will stop after firing the |
2531c7e3 | 69 | first notification automatically. |
a660d684 | 70 | |
457e6c54 | 71 | \membersection{wxTimer::IsRunning}\label{wxtimerisrunning} |
a660d684 | 72 | |
2531c7e3 | 73 | \constfunc{bool}{IsRunning}{\void} |
a660d684 | 74 | |
b0a2d8a8 | 75 | Returns {\tt TRUE} if the timer is running, {\tt FALSE} if it is stopped. |
a660d684 | 76 | |
457e6c54 | 77 | \membersection{wxTimer::Notify}\label{wxtimernotify} |
a660d684 KB |
78 | |
79 | \func{void}{Notify}{\void} | |
80 | ||
2531c7e3 VZ |
81 | This member should be overridden by the user if the default constructor was |
82 | used and \helpref{SetOwner}{wxtimersetowner} wasn't called. | |
a660d684 | 83 | |
2531c7e3 VZ |
84 | Perform whatever action which is to be taken periodically here. |
85 | ||
457e6c54 | 86 | \membersection{wxTimer::SetOwner}\label{wxtimersetowner} |
2531c7e3 VZ |
87 | |
88 | \func{void}{SetOwner}{\param{wxEvtHandler *}{owner}, \param{int }{id = -1}} | |
89 | ||
b0a2d8a8 | 90 | Associates the timer with the given {\it owner}\/ object. When the timer is |
2531c7e3 | 91 | running, the owner will receive \helpref{timer events}{wxtimerevent} with |
b0a2d8a8 | 92 | id equal to {\it id}\/ specified here. |
2531c7e3 | 93 | |
457e6c54 | 94 | \membersection{wxTimer::Start}\label{wxtimerstart} |
a660d684 | 95 | |
b0a2d8a8 | 96 | \func{bool}{Start}{\param{int}{milliseconds = -1}, \param{bool }{oneShot = {\tt FALSE}}} |
a660d684 | 97 | |
b0a2d8a8 VZ |
98 | (Re)starts the timer. If {\it milliseconds}\/ parameter is -1 (value by default), |
99 | the previous value is used. Returns {\tt FALSE} if the timer could not be started, | |
100 | {\tt TRUE} otherwise (in MS Windows timers are a limited resource). | |
101 | ||
102 | If {\it oneShot}\/ is {\tt FALSE} (the default), the \helpref{Notify}{wxtimernotify} | |
103 | function will be called repeatedly until the timer is stopped. If {\tt TRUE}, | |
104 | it will be called only once and the timer will stop automatically. To make your | |
105 | code more readable you may also use the following symbolic constants | |
106 | \twocolwidtha{5cm}% | |
107 | \begin{twocollist}\itemsep=0pt | |
108 | \twocolitem{wxTIMER\_CONTINUOUS}{Start a normal, continuously running, timer} | |
109 | \twocolitem{wxTIMER\_ONE\_SHOT}{Start a one shot timer} | |
110 | \end{twocollist} | |
a660d684 | 111 | |
a660d684 | 112 | |
99646f7e VZ |
113 | If the timer was already running, it will be stopped by this method before |
114 | restarting it. | |
115 | ||
457e6c54 | 116 | \membersection{wxTimer::Stop}\label{wxtimerstop} |
a660d684 KB |
117 | |
118 | \func{void}{Stop}{\void} | |
119 | ||
120 | Stops the timer. | |
121 | ||
2531c7e3 VZ |
122 | \section{\class{wxTimerEvent}}\label{wxtimerevent} |
123 | ||
124 | wxTimerEvent object is passed to the event handler of timer events. | |
125 | ||
126 | For example: | |
127 | ||
128 | \begin{verbatim} | |
129 | class MyFrame : public wxFrame | |
130 | { | |
131 | public: | |
132 | ... | |
133 | void OnTimer(wxTimerEvent& event); | |
134 | ||
135 | private: | |
136 | wxTimer m_timer; | |
137 | }; | |
138 | ||
139 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
140 | EVT_TIMER(TIMER_ID, MyFrame::OnTimer) | |
141 | END_EVENT_TABLE() | |
142 | ||
143 | MyFrame::MyFrame() | |
144 | : m_timer(this, TIMER_ID) | |
145 | { | |
146 | m_timer.Start(1000); // 1 second interval | |
147 | } | |
148 | ||
149 | void MyFrame::OnTimer(wxTimerEvent& event) | |
150 | { | |
151 | // do whatever you want to do every second here | |
152 | } | |
153 | ||
154 | \end{verbatim} | |
155 | ||
156 | \wxheading{Include files} | |
157 | ||
158 | <wx/timer.h> | |
159 | ||
160 | \wxheading{See also} | |
161 | ||
162 | \helpref{wxTimer}{wxtimer} | |
163 | ||
164 | \latexignore{\rtfignore{\wxheading{Members}}} | |
165 | ||
457e6c54 | 166 | \membersection{wxTimerEvent::GetInterval}\label{wxtimereventgetinterval} |
2531c7e3 VZ |
167 | |
168 | \constfunc{int}{GetInterval}{\void} | |
169 | ||
170 | Returns the interval of the timer which generated this event. | |
171 |