]> git.saurik.com Git - wxWidgets.git/blob - include/wx/timer.h
c7459e915296ed7f6bc2a89d2c831655f2ce2d8d
[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 #ifdef __GNUG__
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 // the interface of wxTimer class
32 class WXDLLEXPORT wxTimerBase : public wxObject
33 {
34 public:
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)
46 { Init(); SetOwner(owner, id); }
47
48 // same as ctor above
49 void SetOwner(wxEvtHandler *owner, int id = -1)
50 { m_owner = owner; m_idTimer = id; }
51
52 #ifdef __DARWIN__
53 virtual ~wxTimerBase() { }
54 #endif
55
56 // working with the timer
57 // ----------------------
58
59 // start the timer: if milliseconds == -1, use the same value as for the
60 // last Start()
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);
65
66 // stop the timer
67 virtual void Stop() = 0;
68
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();
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:
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
98 int m_milli; // the timer interval
99 bool m_oneShot; // TRUE if one shot
100 };
101
102 // ----------------------------------------------------------------------------
103 // wxTimer itself
104 // ----------------------------------------------------------------------------
105
106 #if defined(__WXMSW__)
107 #include "wx/msw/timer.h"
108 #elif defined(__WXMOTIF__)
109 #include "wx/motif/timer.h"
110 #elif defined(__WXGTK__)
111 #include "wx/gtk/timer.h"
112 #elif defined(__WXMGL__)
113 #include "wx/mgl/timer.h"
114 #elif defined(__WXMAC__)
115 #include "wx/mac/timer.h"
116 #elif defined(__WXPM__)
117 #include "wx/os2/timer.h"
118 #elif defined(__WXSTUBS__)
119 #include "wx/stubs/timer.h"
120 #endif
121
122 // ----------------------------------------------------------------------------
123 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
124 // ----------------------------------------------------------------------------
125
126 class WXDLLEXPORT wxTimerRunner
127 {
128 public:
129 wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
130 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE)
131 : m_timer(timer)
132 {
133 m_timer.Start(milli, oneShot);
134 }
135
136 void Start(int milli, bool oneShot = FALSE)
137 {
138 m_timer.Start(milli, oneShot);
139 }
140
141 ~wxTimerRunner()
142 {
143 if ( m_timer.IsRunning() )
144 {
145 m_timer.Stop();
146 }
147 }
148
149 private:
150 wxTimer& m_timer;
151 };
152
153 // ----------------------------------------------------------------------------
154 // wxTimerEvent
155 // ----------------------------------------------------------------------------
156
157 class WXDLLEXPORT wxTimerEvent : public wxEvent
158 {
159 public:
160 wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
161 {
162 m_eventType = wxEVT_TIMER;
163
164 m_interval = interval;
165 }
166
167 // accessors
168 int GetInterval() const { return m_interval; }
169
170 private:
171 int m_interval;
172
173 DECLARE_DYNAMIC_CLASS(wxTimerEvent)
174 };
175
176 typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
177
178 #define EVT_TIMER(id, func) \
179 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
180
181 #endif // wxUSE_GUI && wxUSE_TIMER
182
183 // ----------------------------------------------------------------------------
184 // wxStopWatch: measure time intervals with up to 1ms resolution
185 // ----------------------------------------------------------------------------
186
187 #if wxUSE_STOPWATCH
188
189 class WXDLLEXPORT wxStopWatch
190 {
191 public:
192 // ctor starts the stop watch
193 wxStopWatch() { Start(); }
194 void Start(long t = 0);
195 void Pause() { m_pause = GetElapsedTime(); }
196 void Resume() { Start(m_pause); }
197
198 // get elapsed time since the last Start() or Pause() in milliseconds
199 long Time() const;
200
201 protected:
202 // returns the elapsed time since t0
203 long GetElapsedTime() const;
204
205 private:
206 wxLongLong m_t0; // the time of the last Start()
207 long m_pause; // the time of the last Pause() or 0
208 };
209
210 #endif // wxUSE_STOPWATCH
211
212 #if wxUSE_LONGLONG
213
214 // Starts a global timer
215 // -- DEPRECATED: use wxStopWatch instead
216 void WXDLLEXPORT wxStartTimer();
217
218 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
219 // -- DEPRECATED: use wxStopWatch instead
220 long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE);
221
222 #endif // wxUSE_LONGLONG
223
224 // ----------------------------------------------------------------------------
225 // global time functions
226 // ----------------------------------------------------------------------------
227
228 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
229 extern long WXDLLEXPORT wxGetLocalTime();
230
231 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
232 extern long WXDLLEXPORT wxGetUTCTime();
233
234 #if wxUSE_LONGLONG
235 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
236 extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis();
237 #endif // wxUSE_LONGLONG
238
239 #define wxGetCurrentTime() wxGetLocalTime()
240
241 #endif
242 // _WX_TIMER_H_BASE_