use DECLARE_NO_COPY_CLASS() where applicable (patch 633384)
[wxWidgets.git] / include / wx / timer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/timer.h
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)
7 // Created: 04/01/98
8 // RCS-ID: $Id$
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
15
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "timerbase.h"
18 #endif
19
20 #include "wx/setup.h"
21 #include "wx/object.h"
22 #include "wx/longlong.h"
23 #include "wx/event.h"
24
25 #if wxUSE_GUI && wxUSE_TIMER
26
27 // ----------------------------------------------------------------------------
28 // wxTimer
29 // ----------------------------------------------------------------------------
30
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
39 // the interface of wxTimer class
40 class WXDLLEXPORT wxTimerBase : public wxObject
41 {
42 public:
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)
54 { Init(); SetOwner(owner, id); }
55
56 // same as ctor above
57 void SetOwner(wxEvtHandler *owner, int id = -1)
58 { m_owner = owner; m_idTimer = id; }
59
60 virtual ~wxTimerBase();
61
62 // working with the timer
63 // ----------------------
64
65 // start the timer: if milliseconds == -1, use the same value as for the
66 // last Start()
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);
71
72 // stop the timer
73 virtual void Stop() = 0;
74
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();
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:
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
104 int m_milli; // the timer interval
105 bool m_oneShot; // TRUE if one shot
106
107 DECLARE_NO_COPY_CLASS(wxTimerBase)
108 };
109
110 // ----------------------------------------------------------------------------
111 // wxTimer itself
112 // ----------------------------------------------------------------------------
113
114 #if defined(__WXMSW__)
115 #include "wx/msw/timer.h"
116 #elif defined(__WXMOTIF__)
117 #include "wx/motif/timer.h"
118 #elif defined(__WXGTK__)
119 #include "wx/gtk/timer.h"
120 #elif defined(__WXX11__) || defined(__WXMGL__)
121 #include "wx/generic/timer.h"
122 #elif defined(__WXMAC__)
123 #include "wx/mac/timer.h"
124 #elif defined(__WXPM__)
125 #include "wx/os2/timer.h"
126 #elif defined(__WXSTUBS__)
127 #include "wx/stubs/timer.h"
128 #endif
129
130 // ----------------------------------------------------------------------------
131 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
132 // ----------------------------------------------------------------------------
133
134 class WXDLLEXPORT wxTimerRunner
135 {
136 public:
137 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
138 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE)
139 : m_timer(timer)
140 {
141 m_timer.Start(milli, oneShot);
142 }
143
144 void Start(int milli, bool oneShot = FALSE)
145 {
146 m_timer.Start(milli, oneShot);
147 }
148
149 ~wxTimerRunner()
150 {
151 if ( m_timer.IsRunning() )
152 {
153 m_timer.Stop();
154 }
155 }
156
157 private:
158 wxTimer& m_timer;
159 };
160
161 // ----------------------------------------------------------------------------
162 // wxTimerEvent
163 // ----------------------------------------------------------------------------
164
165 class WXDLLEXPORT wxTimerEvent : public wxEvent
166 {
167 public:
168 wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
169 {
170 m_eventType = wxEVT_TIMER;
171
172 m_interval = interval;
173 }
174
175 // accessors
176 int GetInterval() const { return m_interval; }
177
178 // implement the base class pure virtual
179 virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
180
181 private:
182 int m_interval;
183
184 DECLARE_DYNAMIC_CLASS(wxTimerEvent)
185 };
186
187 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
188
189 #define EVT_TIMER(id, func) \
190 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
191
192 #endif // wxUSE_GUI && wxUSE_TIMER
193
194 // ----------------------------------------------------------------------------
195 // wxStopWatch: measure time intervals with up to 1ms resolution
196 // ----------------------------------------------------------------------------
197
198 #if wxUSE_STOPWATCH
199
200 class WXDLLEXPORT wxStopWatch
201 {
202 public:
203 // ctor starts the stop watch
204 wxStopWatch() { m_pauseCount = 0; Start(); }
205
206 // start the stop watch at the moment t0
207 void Start(long t0 = 0);
208
209 // pause the stop watch
210 void Pause() { if ( !m_pauseCount++) m_pause = GetElapsedTime(); }
211
212 // resume it
213 void Resume() { if ( !--m_pauseCount ) Start(m_pause); }
214
215 // get elapsed time since the last Start() in milliseconds
216 long Time() const;
217
218 protected:
219 // returns the elapsed time since t0
220 long GetElapsedTime() const;
221
222 private:
223 // the time of the last Start()
224 wxLongLong m_t0;
225
226 // the time of the last Pause() (only valid if m_pauseCount > 0)
227 long m_pause;
228
229 // if > 0, the stop watch is paused, otherwise it is running
230 int m_pauseCount;
231 };
232
233 #endif // wxUSE_STOPWATCH
234
235 #if wxUSE_LONGLONG
236
237 // Starts a global timer
238 // -- DEPRECATED: use wxStopWatch instead
239 void WXDLLEXPORT wxStartTimer();
240
241 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
242 // -- DEPRECATED: use wxStopWatch instead
243 long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE);
244
245 #endif // wxUSE_LONGLONG
246
247 // ----------------------------------------------------------------------------
248 // global time functions
249 // ----------------------------------------------------------------------------
250
251 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
252 extern long WXDLLEXPORT wxGetLocalTime();
253
254 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
255 extern long WXDLLEXPORT wxGetUTCTime();
256
257 #if wxUSE_LONGLONG
258 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
259 extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis();
260 #endif // wxUSE_LONGLONG
261
262 #define wxGetCurrentTime() wxGetLocalTime()
263
264 #endif
265 // _WX_TIMER_H_BASE_