]>
Commit | Line | Data |
---|---|---|
0470b1e6 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/timer.h | |
ed791986 VZ |
3 | // Purpose: wxTimer, wxStopWatch and global time-related functions |
4 | // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch) | |
5 | // Modified by: Vadim Zeitlin (wxTimerBase) | |
6 | // Guillermo Rodriguez (global clean up) | |
0470b1e6 VZ |
7 | // Created: 04/01/98 |
8 | // RCS-ID: $Id$ | |
ed791986 | 9 | // Copyright: (c) wxWindows team |
371a5b4e | 10 | // Licence: wxWindows licence |
0470b1e6 VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_TIMER_H_BASE_ |
14 | #define _WX_TIMER_H_BASE_ | |
c801d85f | 15 | |
af49c4b8 | 16 | #if defined(__GNUG__) && !defined(__APPLE__) |
0470b1e6 VZ |
17 | #pragma interface "timerbase.h" |
18 | #endif | |
19 | ||
d895ad7c | 20 | #include "wx/setup.h" |
0470b1e6 | 21 | #include "wx/object.h" |
d895ad7c | 22 | #include "wx/longlong.h" |
ed791986 | 23 | #include "wx/event.h" |
0470b1e6 | 24 | |
1e6feb95 | 25 | #if wxUSE_GUI && wxUSE_TIMER |
52a07708 | 26 | |
0470b1e6 VZ |
27 | // ---------------------------------------------------------------------------- |
28 | // wxTimer | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
b0a2d8a8 VZ |
31 | // more readable flags for Start(): |
32 | ||
33 | // generate notifications periodically until the timer is stopped (default) | |
34 | #define wxTIMER_CONTINUOUS FALSE | |
35 | ||
36 | // only send the notification once and then stop the timer | |
37 | #define wxTIMER_ONE_SHOT TRUE | |
38 | ||
0470b1e6 VZ |
39 | // the interface of wxTimer class |
40 | class WXDLLEXPORT wxTimerBase : public wxObject | |
41 | { | |
42 | public: | |
ed791986 VZ |
43 | // ctors and initializers |
44 | // ---------------------- | |
45 | ||
46 | // default: if you don't call SetOwner(), your only chance to get timer | |
47 | // notifications is to override Notify() in the derived class | |
48 | wxTimerBase() { Init(); SetOwner(NULL); } | |
49 | ||
50 | // ctor which allows to avoid having to override Notify() in the derived | |
51 | // class: the owner will get timer notifications which can be handled with | |
52 | // EVT_TIMER | |
53 | wxTimerBase(wxEvtHandler *owner, int id = -1) | |
06b466c7 | 54 | { Init(); SetOwner(owner, id); } |
ed791986 VZ |
55 | |
56 | // same as ctor above | |
57 | void SetOwner(wxEvtHandler *owner, int id = -1) | |
58 | { m_owner = owner; m_idTimer = id; } | |
0470b1e6 | 59 | |
799ea011 | 60 | virtual ~wxTimerBase(); |
03e11df5 | 61 | |
0470b1e6 VZ |
62 | // working with the timer |
63 | // ---------------------- | |
64 | ||
65 | // start the timer: if milliseconds == -1, use the same value as for the | |
66 | // last Start() | |
99646f7e VZ |
67 | // |
68 | // it is now valid to call Start() multiple times: this just restarts the | |
69 | // timer if it is already running | |
70 | virtual bool Start(int milliseconds = -1, bool oneShot = FALSE); | |
0470b1e6 VZ |
71 | |
72 | // stop the timer | |
73 | virtual void Stop() = 0; | |
74 | ||
ed791986 VZ |
75 | // override this in your wxTimer-derived class if you want to process timer |
76 | // messages in it, use non default ctor or SetOwner() otherwise | |
77 | virtual void Notify(); | |
0470b1e6 VZ |
78 | |
79 | // getting info | |
80 | // ------------ | |
81 | ||
82 | // return TRUE if the timer is running | |
83 | virtual bool IsRunning() const = 0; | |
84 | ||
85 | // get the (last) timer interval in the milliseconds | |
86 | int GetInterval() const { return m_milli; } | |
87 | ||
88 | // return TRUE if the timer is one shot | |
89 | bool IsOneShot() const { return m_oneShot; } | |
90 | ||
91 | #if WXWIN_COMPATIBILITY_2 | |
92 | // deprecated functions | |
93 | int Interval() const { return GetInterval(); }; | |
94 | bool OneShot() const { return IsOneShot(); } | |
95 | #endif // WXWIN_COMPATIBILITY_2 | |
96 | ||
97 | protected: | |
ed791986 VZ |
98 | // common part of all ctors |
99 | void Init() { m_oneShot = FALSE; m_milli = 0; } | |
100 | ||
101 | wxEvtHandler *m_owner; | |
102 | int m_idTimer; | |
103 | ||
0470b1e6 VZ |
104 | int m_milli; // the timer interval |
105 | bool m_oneShot; // TRUE if one shot | |
22f3361e VZ |
106 | |
107 | DECLARE_NO_COPY_CLASS(wxTimerBase) | |
0470b1e6 VZ |
108 | }; |
109 | ||
ed791986 VZ |
110 | // ---------------------------------------------------------------------------- |
111 | // wxTimer itself | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
2049ba38 | 114 | #if defined(__WXMSW__) |
0470b1e6 | 115 | #include "wx/msw/timer.h" |
2049ba38 | 116 | #elif defined(__WXMOTIF__) |
0470b1e6 | 117 | #include "wx/motif/timer.h" |
2049ba38 | 118 | #elif defined(__WXGTK__) |
0470b1e6 | 119 | #include "wx/gtk/timer.h" |
51a8edc7 | 120 | #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__) |
c331ac2e | 121 | #include "wx/generic/timer.h" |
34138703 | 122 | #elif defined(__WXMAC__) |
0470b1e6 | 123 | #include "wx/mac/timer.h" |
1777b9bb | 124 | #elif defined(__WXPM__) |
0470b1e6 | 125 | #include "wx/os2/timer.h" |
c801d85f KB |
126 | #endif |
127 | ||
52a07708 VZ |
128 | // ---------------------------------------------------------------------------- |
129 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | class WXDLLEXPORT wxTimerRunner | |
133 | { | |
134 | public: | |
135 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | |
136 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE) | |
137 | : m_timer(timer) | |
138 | { | |
139 | m_timer.Start(milli, oneShot); | |
140 | } | |
141 | ||
142 | void Start(int milli, bool oneShot = FALSE) | |
143 | { | |
144 | m_timer.Start(milli, oneShot); | |
145 | } | |
146 | ||
147 | ~wxTimerRunner() | |
148 | { | |
149 | if ( m_timer.IsRunning() ) | |
150 | { | |
151 | m_timer.Stop(); | |
152 | } | |
153 | } | |
154 | ||
155 | private: | |
156 | wxTimer& m_timer; | |
157 | }; | |
158 | ||
0470b1e6 | 159 | // ---------------------------------------------------------------------------- |
ed791986 VZ |
160 | // wxTimerEvent |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
163 | class WXDLLEXPORT wxTimerEvent : public wxEvent | |
164 | { | |
165 | public: | |
166 | wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id) | |
167 | { | |
168 | m_eventType = wxEVT_TIMER; | |
169 | ||
170 | m_interval = interval; | |
171 | } | |
172 | ||
173 | // accessors | |
174 | int GetInterval() const { return m_interval; } | |
175 | ||
acd15a3f VZ |
176 | // implement the base class pure virtual |
177 | virtual wxEvent *Clone() const { return new wxTimerEvent(*this); } | |
178 | ||
ed791986 VZ |
179 | private: |
180 | int m_interval; | |
181 | ||
182 | DECLARE_DYNAMIC_CLASS(wxTimerEvent) | |
183 | }; | |
184 | ||
185 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); | |
186 | ||
82a5f02c | 187 | #define EVT_TIMER(id, func) \ |
2e4df4bf | 188 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL), |
ed791986 | 189 | |
1e6feb95 | 190 | #endif // wxUSE_GUI && wxUSE_TIMER |
52a07708 | 191 | |
ed791986 VZ |
192 | // ---------------------------------------------------------------------------- |
193 | // wxStopWatch: measure time intervals with up to 1ms resolution | |
0470b1e6 VZ |
194 | // ---------------------------------------------------------------------------- |
195 | ||
1e6feb95 VZ |
196 | #if wxUSE_STOPWATCH |
197 | ||
0470b1e6 | 198 | class WXDLLEXPORT wxStopWatch |
f0599ea9 | 199 | { |
ed791986 | 200 | public: |
0470b1e6 | 201 | // ctor starts the stop watch |
677eff07 | 202 | wxStopWatch() { m_pauseCount = 0; Start(); } |
0470b1e6 | 203 | |
677eff07 VZ |
204 | // start the stop watch at the moment t0 |
205 | void Start(long t0 = 0); | |
206 | ||
207 | // pause the stop watch | |
a08e7186 VZ |
208 | void Pause() |
209 | { | |
210 | if ( !m_pauseCount++ ) | |
211 | m_pause = GetElapsedTime(); | |
212 | } | |
677eff07 VZ |
213 | |
214 | // resume it | |
a08e7186 VZ |
215 | void Resume() |
216 | { | |
217 | wxASSERT_MSG( m_pauseCount > 0, | |
218 | _T("Resuming stop watch which is not paused") ); | |
219 | ||
220 | if ( !--m_pauseCount ) | |
221 | Start(m_pause); | |
222 | } | |
677eff07 VZ |
223 | |
224 | // get elapsed time since the last Start() in milliseconds | |
0470b1e6 | 225 | long Time() const; |
f0599ea9 | 226 | |
0470b1e6 VZ |
227 | protected: |
228 | // returns the elapsed time since t0 | |
d895ad7c | 229 | long GetElapsedTime() const; |
ed791986 | 230 | |
f0599ea9 | 231 | private: |
677eff07 VZ |
232 | // the time of the last Start() |
233 | wxLongLong m_t0; | |
234 | ||
235 | // the time of the last Pause() (only valid if m_pauseCount > 0) | |
236 | long m_pause; | |
237 | ||
238 | // if > 0, the stop watch is paused, otherwise it is running | |
239 | int m_pauseCount; | |
f0599ea9 SB |
240 | }; |
241 | ||
1e6feb95 VZ |
242 | #endif // wxUSE_STOPWATCH |
243 | ||
244 | #if wxUSE_LONGLONG | |
0470b1e6 | 245 | |
ed791986 | 246 | // Starts a global timer |
d895ad7c | 247 | // -- DEPRECATED: use wxStopWatch instead |
f0599ea9 SB |
248 | void WXDLLEXPORT wxStartTimer(); |
249 | ||
d895ad7c GRG |
250 | // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime |
251 | // -- DEPRECATED: use wxStopWatch instead | |
f0599ea9 SB |
252 | long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE); |
253 | ||
1e6feb95 VZ |
254 | #endif // wxUSE_LONGLONG |
255 | ||
0470b1e6 | 256 | // ---------------------------------------------------------------------------- |
d895ad7c | 257 | // global time functions |
0470b1e6 VZ |
258 | // ---------------------------------------------------------------------------- |
259 | ||
d895ad7c | 260 | // Get number of seconds since local time 00:00:00 Jan 1st 1970. |
f6bcfd97 | 261 | extern long WXDLLEXPORT wxGetLocalTime(); |
d895ad7c GRG |
262 | |
263 | // Get number of seconds since GMT 00:00:00, Jan 1st 1970. | |
f6bcfd97 BP |
264 | extern long WXDLLEXPORT wxGetUTCTime(); |
265 | ||
1e6feb95 | 266 | #if wxUSE_LONGLONG |
f6bcfd97 BP |
267 | // Get number of milliseconds since local time 00:00:00 Jan 1st 1970 |
268 | extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis(); | |
1e6feb95 | 269 | #endif // wxUSE_LONGLONG |
d895ad7c | 270 | |
df0c3226 | 271 | #define wxGetCurrentTime() wxGetLocalTime() |
0470b1e6 | 272 | |
c801d85f | 273 | #endif |
34138703 | 274 | // _WX_TIMER_H_BASE_ |