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"
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 // working with the timer
53 // ----------------------
55 // start the timer: if milliseconds == -1, use the same value as for the
57 virtual bool Start(int milliseconds
= -1, bool oneShot
= FALSE
)
59 if ( milliseconds
!= -1 )
61 m_milli
= milliseconds
;
71 virtual void Stop() = 0;
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();
80 // return TRUE if the timer is running
81 virtual bool IsRunning() const = 0;
83 // get the (last) timer interval in the milliseconds
84 int GetInterval() const { return m_milli
; }
86 // return TRUE if the timer is one shot
87 bool IsOneShot() const { return m_oneShot
; }
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
96 // common part of all ctors
97 void Init() { m_oneShot
= FALSE
; m_milli
= 0; }
99 wxEvtHandler
*m_owner
;
102 int m_milli
; // the timer interval
103 bool m_oneShot
; // TRUE if one shot
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 #if defined(__WXMSW__)
111 #include "wx/msw/timer.h"
112 #elif defined(__WXMOTIF__)
113 #include "wx/motif/timer.h"
114 #elif defined(__WXGTK__)
115 #include "wx/gtk/timer.h"
116 #elif defined(__WXQT__)
117 #include "wx/qt/timer.h"
118 #elif defined(__WXMAC__)
119 #include "wx/mac/timer.h"
120 #elif defined(__WXPM__)
121 #include "wx/os2/timer.h"
122 #elif defined(__WXSTUBS__)
123 #include "wx/stubs/timer.h"
126 // ----------------------------------------------------------------------------
127 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
128 // ----------------------------------------------------------------------------
130 class WXDLLEXPORT wxTimerRunner
133 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
134 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= FALSE
)
137 m_timer
.Start(milli
, oneShot
);
140 void Start(int milli
, bool oneShot
= FALSE
)
142 m_timer
.Start(milli
, oneShot
);
147 if ( m_timer
.IsRunning() )
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 class WXDLLEXPORT wxTimerEvent
: public wxEvent
164 wxTimerEvent(int id
= 0, int interval
= 0) : wxEvent(id
)
166 m_eventType
= wxEVT_TIMER
;
168 m_interval
= interval
;
172 int GetInterval() const { return m_interval
; }
177 DECLARE_DYNAMIC_CLASS(wxTimerEvent
)
180 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
182 #define EVT_TIMER(id, func) { wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL},
186 // ----------------------------------------------------------------------------
187 // wxStopWatch: measure time intervals with up to 1ms resolution
188 // ----------------------------------------------------------------------------
190 class WXDLLEXPORT wxStopWatch
193 // ctor starts the stop watch
194 wxStopWatch() { Start(); }
195 void Start(long t
= 0);
196 void Pause() { m_pause
= GetElapsedTime(); }
197 void Resume() { Start(m_pause
); }
199 // get elapsed time since the last Start() or Pause() in milliseconds
203 // returns the elapsed time since t0
204 long GetElapsedTime() const;
207 wxLongLong m_t0
; // the time of the last Start()
208 long m_pause
; // the time of the last Pause() or 0
212 // Starts a global timer
213 // -- DEPRECATED: use wxStopWatch instead
214 void WXDLLEXPORT
wxStartTimer();
216 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
217 // -- DEPRECATED: use wxStopWatch instead
218 long WXDLLEXPORT
wxGetElapsedTime(bool resetTimer
= TRUE
);
220 // ----------------------------------------------------------------------------
221 // global time functions
222 // ----------------------------------------------------------------------------
224 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
225 extern long WXDLLEXPORT
wxGetLocalTime();
227 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
228 extern long WXDLLEXPORT
wxGetUTCTime();
230 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
231 extern wxLongLong WXDLLEXPORT
wxGetLocalTimeMillis();
233 #define wxGetCurrentTime() wxGetLocalTime()