]>
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 | |
52a07708 VZ |
25 | #if wxUSE_GUI |
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 VZ |
51 | |
52 | // working with the timer | |
53 | // ---------------------- | |
54 | ||
55 | // start the timer: if milliseconds == -1, use the same value as for the | |
56 | // last Start() | |
57 | virtual bool Start(int milliseconds = -1, bool oneShot = FALSE) | |
58 | { | |
59 | if ( milliseconds != -1 ) | |
60 | { | |
61 | m_milli = milliseconds; | |
62 | } | |
63 | ||
64 | m_oneShot = oneShot; | |
65 | ||
66 | return TRUE; | |
67 | } | |
68 | ||
69 | ||
70 | // stop the timer | |
71 | virtual void Stop() = 0; | |
72 | ||
ed791986 VZ |
73 | // override this in your wxTimer-derived class if you want to process timer |
74 | // messages in it, use non default ctor or SetOwner() otherwise | |
75 | virtual void Notify(); | |
0470b1e6 VZ |
76 | |
77 | // getting info | |
78 | // ------------ | |
79 | ||
80 | // return TRUE if the timer is running | |
81 | virtual bool IsRunning() const = 0; | |
82 | ||
83 | // get the (last) timer interval in the milliseconds | |
84 | int GetInterval() const { return m_milli; } | |
85 | ||
86 | // return TRUE if the timer is one shot | |
87 | bool IsOneShot() const { return m_oneShot; } | |
88 | ||
89 | #if WXWIN_COMPATIBILITY_2 | |
90 | // deprecated functions | |
91 | int Interval() const { return GetInterval(); }; | |
92 | bool OneShot() const { return IsOneShot(); } | |
93 | #endif // WXWIN_COMPATIBILITY_2 | |
94 | ||
95 | protected: | |
ed791986 VZ |
96 | // common part of all ctors |
97 | void Init() { m_oneShot = FALSE; m_milli = 0; } | |
98 | ||
99 | wxEvtHandler *m_owner; | |
100 | int m_idTimer; | |
101 | ||
0470b1e6 VZ |
102 | int m_milli; // the timer interval |
103 | bool m_oneShot; // TRUE if one shot | |
104 | }; | |
105 | ||
ed791986 VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // wxTimer itself | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
2049ba38 | 110 | #if defined(__WXMSW__) |
0470b1e6 | 111 | #include "wx/msw/timer.h" |
2049ba38 | 112 | #elif defined(__WXMOTIF__) |
0470b1e6 | 113 | #include "wx/motif/timer.h" |
2049ba38 | 114 | #elif defined(__WXGTK__) |
0470b1e6 | 115 | #include "wx/gtk/timer.h" |
b4e76e0d | 116 | #elif defined(__WXQT__) |
0470b1e6 | 117 | #include "wx/qt/timer.h" |
34138703 | 118 | #elif defined(__WXMAC__) |
0470b1e6 | 119 | #include "wx/mac/timer.h" |
1777b9bb | 120 | #elif defined(__WXPM__) |
0470b1e6 | 121 | #include "wx/os2/timer.h" |
34138703 | 122 | #elif defined(__WXSTUBS__) |
0470b1e6 | 123 | #include "wx/stubs/timer.h" |
c801d85f KB |
124 | #endif |
125 | ||
52a07708 VZ |
126 | // ---------------------------------------------------------------------------- |
127 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | class WXDLLEXPORT wxTimerRunner | |
131 | { | |
132 | public: | |
133 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | |
134 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE) | |
135 | : m_timer(timer) | |
136 | { | |
137 | m_timer.Start(milli, oneShot); | |
138 | } | |
139 | ||
140 | void Start(int milli, bool oneShot = FALSE) | |
141 | { | |
142 | m_timer.Start(milli, oneShot); | |
143 | } | |
144 | ||
145 | ~wxTimerRunner() | |
146 | { | |
147 | if ( m_timer.IsRunning() ) | |
148 | { | |
149 | m_timer.Stop(); | |
150 | } | |
151 | } | |
152 | ||
153 | private: | |
154 | wxTimer& m_timer; | |
155 | }; | |
156 | ||
0470b1e6 | 157 | // ---------------------------------------------------------------------------- |
ed791986 VZ |
158 | // wxTimerEvent |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | class WXDLLEXPORT wxTimerEvent : public wxEvent | |
162 | { | |
163 | public: | |
164 | wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id) | |
165 | { | |
166 | m_eventType = wxEVT_TIMER; | |
167 | ||
168 | m_interval = interval; | |
169 | } | |
170 | ||
171 | // accessors | |
172 | int GetInterval() const { return m_interval; } | |
173 | ||
174 | private: | |
175 | int m_interval; | |
176 | ||
177 | DECLARE_DYNAMIC_CLASS(wxTimerEvent) | |
178 | }; | |
179 | ||
180 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); | |
181 | ||
182 | #define EVT_TIMER(id, func) { wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL}, | |
183 | ||
52a07708 VZ |
184 | #endif // wxUSE_GUI |
185 | ||
ed791986 VZ |
186 | // ---------------------------------------------------------------------------- |
187 | // wxStopWatch: measure time intervals with up to 1ms resolution | |
0470b1e6 VZ |
188 | // ---------------------------------------------------------------------------- |
189 | ||
190 | class WXDLLEXPORT wxStopWatch | |
f0599ea9 | 191 | { |
ed791986 | 192 | public: |
0470b1e6 | 193 | // ctor starts the stop watch |
d895ad7c GRG |
194 | wxStopWatch() { Start(); } |
195 | void Start(long t = 0); | |
92da8bde VZ |
196 | void Pause() { m_pause = GetElapsedTime(); } |
197 | void Resume() { Start(m_pause); } | |
0470b1e6 | 198 | |
d895ad7c | 199 | // get elapsed time since the last Start() or Pause() in milliseconds |
0470b1e6 | 200 | long Time() const; |
f0599ea9 | 201 | |
0470b1e6 VZ |
202 | protected: |
203 | // returns the elapsed time since t0 | |
d895ad7c | 204 | long GetElapsedTime() const; |
ed791986 | 205 | |
f0599ea9 | 206 | private: |
d895ad7c GRG |
207 | wxLongLong m_t0; // the time of the last Start() |
208 | long m_pause; // the time of the last Pause() or 0 | |
f0599ea9 SB |
209 | }; |
210 | ||
0470b1e6 | 211 | |
ed791986 | 212 | // Starts a global timer |
d895ad7c | 213 | // -- DEPRECATED: use wxStopWatch instead |
f0599ea9 SB |
214 | void WXDLLEXPORT wxStartTimer(); |
215 | ||
d895ad7c GRG |
216 | // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime |
217 | // -- DEPRECATED: use wxStopWatch instead | |
f0599ea9 SB |
218 | long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE); |
219 | ||
0470b1e6 | 220 | // ---------------------------------------------------------------------------- |
d895ad7c | 221 | // global time functions |
0470b1e6 VZ |
222 | // ---------------------------------------------------------------------------- |
223 | ||
d895ad7c GRG |
224 | // Get number of seconds since local time 00:00:00 Jan 1st 1970. |
225 | long WXDLLEXPORT wxGetLocalTime(); | |
226 | ||
227 | // Get number of seconds since GMT 00:00:00, Jan 1st 1970. | |
228 | long WXDLLEXPORT wxGetUTCTime(); | |
229 | ||
df0c3226 | 230 | #define wxGetCurrentTime() wxGetLocalTime() |
0470b1e6 | 231 | |
c801d85f | 232 | #endif |
34138703 | 233 | // _WX_TIMER_H_BASE_ |