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