1 /////////////////////////////////////////////////////////////////////////////
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)
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
17 #pragma interface "timerbase.h"
21 #include "wx/object.h"
22 #include "wx/longlong.h"
25 #if wxUSE_GUI && wxUSE_TIMER
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // the interface of wxTimer class
32 class WXDLLEXPORT wxTimerBase
: public wxObject
35 // ctors and initializers
36 // ----------------------
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
); }
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
45 wxTimerBase(wxEvtHandler
*owner
, int id
= -1)
46 { Init(); SetOwner(owner
, id
); }
49 void SetOwner(wxEvtHandler
*owner
, int id
= -1)
50 { m_owner
= owner
; m_idTimer
= id
; }
52 virtual ~wxTimerBase();
54 // working with the timer
55 // ----------------------
57 // start the timer: if milliseconds == -1, use the same value as for the
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
);
65 virtual void Stop() = 0;
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();
74 // return TRUE if the timer is running
75 virtual bool IsRunning() const = 0;
77 // get the (last) timer interval in the milliseconds
78 int GetInterval() const { return m_milli
; }
80 // return TRUE if the timer is one shot
81 bool IsOneShot() const { return m_oneShot
; }
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
90 // common part of all ctors
91 void Init() { m_oneShot
= FALSE
; m_milli
= 0; }
93 wxEvtHandler
*m_owner
;
96 int m_milli
; // the timer interval
97 bool m_oneShot
; // TRUE if one shot
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 #if defined(__WXMSW__)
105 #include "wx/msw/timer.h"
106 #elif defined(__WXMOTIF__)
107 #include "wx/motif/timer.h"
108 #elif defined(__WXGTK__)
109 #include "wx/gtk/timer.h"
110 #elif defined(__WXX11__) || defined(__WXMGL__)
111 #include "wx/generic/timer.h"
112 #elif defined(__WXMAC__)
113 #include "wx/mac/timer.h"
114 #elif defined(__WXPM__)
115 #include "wx/os2/timer.h"
116 #elif defined(__WXSTUBS__)
117 #include "wx/stubs/timer.h"
120 // ----------------------------------------------------------------------------
121 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
122 // ----------------------------------------------------------------------------
124 class WXDLLEXPORT wxTimerRunner
127 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
128 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= FALSE
)
131 m_timer
.Start(milli
, oneShot
);
134 void Start(int milli
, bool oneShot
= FALSE
)
136 m_timer
.Start(milli
, oneShot
);
141 if ( m_timer
.IsRunning() )
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 class WXDLLEXPORT wxTimerEvent
: public wxEvent
158 wxTimerEvent(int id
= 0, int interval
= 0) : wxEvent(id
)
160 m_eventType
= wxEVT_TIMER
;
162 m_interval
= interval
;
166 int GetInterval() const { return m_interval
; }
168 // implement the base class pure virtual
169 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
174 DECLARE_DYNAMIC_CLASS(wxTimerEvent
)
177 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
179 #define EVT_TIMER(id, func) \
180 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
182 #endif // wxUSE_GUI && wxUSE_TIMER
184 // ----------------------------------------------------------------------------
185 // wxStopWatch: measure time intervals with up to 1ms resolution
186 // ----------------------------------------------------------------------------
190 class WXDLLEXPORT wxStopWatch
193 // ctor starts the stop watch
194 wxStopWatch() { m_pauseCount
= 0; Start(); }
196 // start the stop watch at the moment t0
197 void Start(long t0
= 0);
199 // pause the stop watch
200 void Pause() { if ( !m_pauseCount
++) m_pause
= GetElapsedTime(); }
203 void Resume() { if ( !--m_pauseCount
) Start(m_pause
); }
205 // get elapsed time since the last Start() in milliseconds
209 // returns the elapsed time since t0
210 long GetElapsedTime() const;
213 // the time of the last Start()
216 // the time of the last Pause() (only valid if m_pauseCount > 0)
219 // if > 0, the stop watch is paused, otherwise it is running
223 #endif // wxUSE_STOPWATCH
227 // Starts a global timer
228 // -- DEPRECATED: use wxStopWatch instead
229 void WXDLLEXPORT
wxStartTimer();
231 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
232 // -- DEPRECATED: use wxStopWatch instead
233 long WXDLLEXPORT
wxGetElapsedTime(bool resetTimer
= TRUE
);
235 #endif // wxUSE_LONGLONG
237 // ----------------------------------------------------------------------------
238 // global time functions
239 // ----------------------------------------------------------------------------
241 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
242 extern long WXDLLEXPORT
wxGetLocalTime();
244 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
245 extern long WXDLLEXPORT
wxGetUTCTime();
248 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
249 extern wxLongLong WXDLLEXPORT
wxGetLocalTimeMillis();
250 #endif // wxUSE_LONGLONG
252 #define wxGetCurrentTime() wxGetLocalTime()