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
; }
53 virtual ~wxTimerBase() {} // Added min for Mac X
56 // working with the timer
57 // ----------------------
59 // start the timer: if milliseconds == -1, use the same value as for the
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
);
67 virtual void Stop() = 0;
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();
76 // return TRUE if the timer is running
77 virtual bool IsRunning() const = 0;
79 // get the (last) timer interval in the milliseconds
80 int GetInterval() const { return m_milli
; }
82 // return TRUE if the timer is one shot
83 bool IsOneShot() const { return m_oneShot
; }
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
92 // common part of all ctors
93 void Init() { m_oneShot
= FALSE
; m_milli
= 0; }
95 wxEvtHandler
*m_owner
;
98 int m_milli
; // the timer interval
99 bool m_oneShot
; // TRUE if one shot
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
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(__WXQT__)
113 #include "wx/qt/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"
122 // ----------------------------------------------------------------------------
123 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxTimerRunner
129 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
130 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= FALSE
)
133 m_timer
.Start(milli
, oneShot
);
136 void Start(int milli
, bool oneShot
= FALSE
)
138 m_timer
.Start(milli
, oneShot
);
143 if ( m_timer
.IsRunning() )
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 class WXDLLEXPORT wxTimerEvent
: public wxEvent
160 wxTimerEvent(int id
= 0, int interval
= 0) : wxEvent(id
)
162 m_eventType
= wxEVT_TIMER
;
164 m_interval
= interval
;
168 int GetInterval() const { return m_interval
; }
173 DECLARE_DYNAMIC_CLASS(wxTimerEvent
)
176 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
178 #define EVT_TIMER(id, func) \
179 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
183 // ----------------------------------------------------------------------------
184 // wxStopWatch: measure time intervals with up to 1ms resolution
185 // ----------------------------------------------------------------------------
187 class WXDLLEXPORT wxStopWatch
190 // ctor starts the stop watch
191 wxStopWatch() { Start(); }
192 void Start(long t
= 0);
193 void Pause() { m_pause
= GetElapsedTime(); }
194 void Resume() { Start(m_pause
); }
196 // get elapsed time since the last Start() or Pause() in milliseconds
200 // returns the elapsed time since t0
201 long GetElapsedTime() const;
204 wxLongLong m_t0
; // the time of the last Start()
205 long m_pause
; // the time of the last Pause() or 0
209 // Starts a global timer
210 // -- DEPRECATED: use wxStopWatch instead
211 void WXDLLEXPORT
wxStartTimer();
213 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
214 // -- DEPRECATED: use wxStopWatch instead
215 long WXDLLEXPORT
wxGetElapsedTime(bool resetTimer
= TRUE
);
217 // ----------------------------------------------------------------------------
218 // global time functions
219 // ----------------------------------------------------------------------------
221 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
222 extern long WXDLLEXPORT
wxGetLocalTime();
224 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
225 extern long WXDLLEXPORT
wxGetUTCTime();
227 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
228 extern wxLongLong WXDLLEXPORT
wxGetLocalTimeMillis();
230 #define wxGetCurrentTime() wxGetLocalTime()