]>
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 VZ |
9 | // Copyright: (c) wxWindows team |
10 | // Licence: wxWindows license | |
0470b1e6 VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_TIMER_H_BASE_ |
14 | #define _WX_TIMER_H_BASE_ | |
c801d85f | 15 | |
0470b1e6 VZ |
16 | #ifdef __GNUG__ |
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 | ||
31 | // the interface of wxTimer class | |
32 | class WXDLLEXPORT wxTimerBase : public wxObject | |
33 | { | |
34 | public: | |
ed791986 VZ |
35 | // ctors and initializers |
36 | // ---------------------- | |
37 | ||
38 | // default: if you don't call SetOwner(), your only chance to get timer | |
39 | // notifications is to override Notify() in the derived class | |
40 | wxTimerBase() { Init(); SetOwner(NULL); } | |
41 | ||
42 | // ctor which allows to avoid having to override Notify() in the derived | |
43 | // class: the owner will get timer notifications which can be handled with | |
44 | // EVT_TIMER | |
45 | wxTimerBase(wxEvtHandler *owner, int id = -1) | |
06b466c7 | 46 | { Init(); SetOwner(owner, id); } |
ed791986 VZ |
47 | |
48 | // same as ctor above | |
49 | void SetOwner(wxEvtHandler *owner, int id = -1) | |
50 | { m_owner = owner; m_idTimer = id; } | |
0470b1e6 | 51 | |
f11bdd03 GD |
52 | #ifdef __DARWIN__ |
53 | virtual ~wxTimerBase() { } | |
03e11df5 GD |
54 | #endif |
55 | ||
0470b1e6 VZ |
56 | // working with the timer |
57 | // ---------------------- | |
58 | ||
59 | // start the timer: if milliseconds == -1, use the same value as for the | |
60 | // last Start() | |
99646f7e VZ |
61 | // |
62 | // it is now valid to call Start() multiple times: this just restarts the | |
63 | // timer if it is already running | |
64 | virtual bool Start(int milliseconds = -1, bool oneShot = FALSE); | |
0470b1e6 VZ |
65 | |
66 | // stop the timer | |
67 | virtual void Stop() = 0; | |
68 | ||
ed791986 VZ |
69 | // override this in your wxTimer-derived class if you want to process timer |
70 | // messages in it, use non default ctor or SetOwner() otherwise | |
71 | virtual void Notify(); | |
0470b1e6 VZ |
72 | |
73 | // getting info | |
74 | // ------------ | |
75 | ||
76 | // return TRUE if the timer is running | |
77 | virtual bool IsRunning() const = 0; | |
78 | ||
79 | // get the (last) timer interval in the milliseconds | |
80 | int GetInterval() const { return m_milli; } | |
81 | ||
82 | // return TRUE if the timer is one shot | |
83 | bool IsOneShot() const { return m_oneShot; } | |
84 | ||
85 | #if WXWIN_COMPATIBILITY_2 | |
86 | // deprecated functions | |
87 | int Interval() const { return GetInterval(); }; | |
88 | bool OneShot() const { return IsOneShot(); } | |
89 | #endif // WXWIN_COMPATIBILITY_2 | |
90 | ||
91 | protected: | |
ed791986 VZ |
92 | // common part of all ctors |
93 | void Init() { m_oneShot = FALSE; m_milli = 0; } | |
94 | ||
95 | wxEvtHandler *m_owner; | |
96 | int m_idTimer; | |
97 | ||
0470b1e6 VZ |
98 | int m_milli; // the timer interval |
99 | bool m_oneShot; // TRUE if one shot | |
100 | }; | |
101 | ||
ed791986 VZ |
102 | // ---------------------------------------------------------------------------- |
103 | // wxTimer itself | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
2049ba38 | 106 | #if defined(__WXMSW__) |
0470b1e6 | 107 | #include "wx/msw/timer.h" |
2049ba38 | 108 | #elif defined(__WXMOTIF__) |
0470b1e6 | 109 | #include "wx/motif/timer.h" |
2049ba38 | 110 | #elif defined(__WXGTK__) |
0470b1e6 | 111 | #include "wx/gtk/timer.h" |
b2a19d94 VS |
112 | #elif defined(__WXMGL__) |
113 | #include "wx/mgl/timer.h" | |
b4e76e0d | 114 | #elif defined(__WXQT__) |
0470b1e6 | 115 | #include "wx/qt/timer.h" |
34138703 | 116 | #elif defined(__WXMAC__) |
0470b1e6 | 117 | #include "wx/mac/timer.h" |
1777b9bb | 118 | #elif defined(__WXPM__) |
0470b1e6 | 119 | #include "wx/os2/timer.h" |
34138703 | 120 | #elif defined(__WXSTUBS__) |
0470b1e6 | 121 | #include "wx/stubs/timer.h" |
c801d85f KB |
122 | #endif |
123 | ||
52a07708 VZ |
124 | // ---------------------------------------------------------------------------- |
125 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | class WXDLLEXPORT wxTimerRunner | |
129 | { | |
130 | public: | |
131 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | |
132 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE) | |
133 | : m_timer(timer) | |
134 | { | |
135 | m_timer.Start(milli, oneShot); | |
136 | } | |
137 | ||
138 | void Start(int milli, bool oneShot = FALSE) | |
139 | { | |
140 | m_timer.Start(milli, oneShot); | |
141 | } | |
142 | ||
143 | ~wxTimerRunner() | |
144 | { | |
145 | if ( m_timer.IsRunning() ) | |
146 | { | |
147 | m_timer.Stop(); | |
148 | } | |
149 | } | |
150 | ||
151 | private: | |
152 | wxTimer& m_timer; | |
153 | }; | |
154 | ||
0470b1e6 | 155 | // ---------------------------------------------------------------------------- |
ed791986 VZ |
156 | // wxTimerEvent |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | class WXDLLEXPORT wxTimerEvent : public wxEvent | |
160 | { | |
161 | public: | |
162 | wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id) | |
163 | { | |
164 | m_eventType = wxEVT_TIMER; | |
165 | ||
166 | m_interval = interval; | |
167 | } | |
168 | ||
169 | // accessors | |
170 | int GetInterval() const { return m_interval; } | |
171 | ||
172 | private: | |
173 | int m_interval; | |
174 | ||
175 | DECLARE_DYNAMIC_CLASS(wxTimerEvent) | |
176 | }; | |
177 | ||
178 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); | |
179 | ||
82a5f02c | 180 | #define EVT_TIMER(id, func) \ |
2e4df4bf | 181 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL), |
ed791986 | 182 | |
1e6feb95 | 183 | #endif // wxUSE_GUI && wxUSE_TIMER |
52a07708 | 184 | |
ed791986 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // wxStopWatch: measure time intervals with up to 1ms resolution | |
0470b1e6 VZ |
187 | // ---------------------------------------------------------------------------- |
188 | ||
1e6feb95 VZ |
189 | #if wxUSE_STOPWATCH |
190 | ||
0470b1e6 | 191 | class WXDLLEXPORT wxStopWatch |
f0599ea9 | 192 | { |
ed791986 | 193 | public: |
0470b1e6 | 194 | // ctor starts the stop watch |
d895ad7c GRG |
195 | wxStopWatch() { Start(); } |
196 | void Start(long t = 0); | |
92da8bde VZ |
197 | void Pause() { m_pause = GetElapsedTime(); } |
198 | void Resume() { Start(m_pause); } | |
0470b1e6 | 199 | |
d895ad7c | 200 | // get elapsed time since the last Start() or Pause() in milliseconds |
0470b1e6 | 201 | long Time() const; |
f0599ea9 | 202 | |
0470b1e6 VZ |
203 | protected: |
204 | // returns the elapsed time since t0 | |
d895ad7c | 205 | long GetElapsedTime() const; |
ed791986 | 206 | |
f0599ea9 | 207 | private: |
d895ad7c GRG |
208 | wxLongLong m_t0; // the time of the last Start() |
209 | long m_pause; // the time of the last Pause() or 0 | |
f0599ea9 SB |
210 | }; |
211 | ||
1e6feb95 VZ |
212 | #endif // wxUSE_STOPWATCH |
213 | ||
214 | #if wxUSE_LONGLONG | |
0470b1e6 | 215 | |
ed791986 | 216 | // Starts a global timer |
d895ad7c | 217 | // -- DEPRECATED: use wxStopWatch instead |
f0599ea9 SB |
218 | void WXDLLEXPORT wxStartTimer(); |
219 | ||
d895ad7c GRG |
220 | // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime |
221 | // -- DEPRECATED: use wxStopWatch instead | |
f0599ea9 SB |
222 | long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE); |
223 | ||
1e6feb95 VZ |
224 | #endif // wxUSE_LONGLONG |
225 | ||
0470b1e6 | 226 | // ---------------------------------------------------------------------------- |
d895ad7c | 227 | // global time functions |
0470b1e6 VZ |
228 | // ---------------------------------------------------------------------------- |
229 | ||
d895ad7c | 230 | // Get number of seconds since local time 00:00:00 Jan 1st 1970. |
f6bcfd97 | 231 | extern long WXDLLEXPORT wxGetLocalTime(); |
d895ad7c GRG |
232 | |
233 | // Get number of seconds since GMT 00:00:00, Jan 1st 1970. | |
f6bcfd97 BP |
234 | extern long WXDLLEXPORT wxGetUTCTime(); |
235 | ||
1e6feb95 | 236 | #if wxUSE_LONGLONG |
f6bcfd97 BP |
237 | // Get number of milliseconds since local time 00:00:00 Jan 1st 1970 |
238 | extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis(); | |
1e6feb95 | 239 | #endif // wxUSE_LONGLONG |
d895ad7c | 240 | |
df0c3226 | 241 | #define wxGetCurrentTime() wxGetLocalTime() |
0470b1e6 | 242 | |
c801d85f | 243 | #endif |
34138703 | 244 | // _WX_TIMER_H_BASE_ |