1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer, wxStopWatch and global time-related functions
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (wxTimerBase)
6 // Guillermo Rodriguez (global clean up)
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
18 #if wxUSE_GUI && wxUSE_TIMER
20 #include "wx/object.h"
21 #include "wx/longlong.h"
23 #include "wx/stopwatch.h" // for backwards compatibility
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // more readable flags for Start():
31 // generate notifications periodically until the timer is stopped (default)
32 #define wxTIMER_CONTINUOUS false
34 // only send the notification once and then stop the timer
35 #define wxTIMER_ONE_SHOT true
37 // the interface of wxTimer class
38 class WXDLLEXPORT wxTimerBase
: public wxEvtHandler
41 // ctors and initializers
42 // ----------------------
44 // default: if you don't call SetOwner(), your only chance to get timer
45 // notifications is to override Notify() in the derived class
46 wxTimerBase() { Init(); SetOwner(this); }
48 // ctor which allows to avoid having to override Notify() in the derived
49 // class: the owner will get timer notifications which can be handled with
51 wxTimerBase(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
52 { Init(); SetOwner(owner
, timerid
); }
55 void SetOwner(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
56 { m_owner
= owner
; m_idTimer
= timerid
; }
57 wxEvtHandler
* GetOwner() const { return m_owner
; }
59 virtual ~wxTimerBase();
61 // working with the timer
62 // ----------------------
64 // start the timer: if milliseconds == -1, use the same value as for the
67 // it is now valid to call Start() multiple times: this just restarts the
68 // timer if it is already running
69 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
72 virtual void Stop() = 0;
74 // override this in your wxTimer-derived class if you want to process timer
75 // messages in it, use non default ctor or SetOwner() otherwise
76 virtual void Notify();
81 // return true if the timer is running
82 virtual bool IsRunning() const = 0;
84 // get the (last) timer interval in the milliseconds
85 int GetInterval() const { return m_milli
; }
87 // return true if the timer is one shot
88 bool IsOneShot() const { return m_oneShot
; }
90 // return the timer ID
91 int GetId() const { return m_idTimer
; }
95 // common part of all ctors
96 void Init() { m_oneShot
= false; m_milli
= 0; }
98 wxEvtHandler
*m_owner
;
101 int m_milli
; // the timer interval
102 bool m_oneShot
; // true if one shot
104 DECLARE_NO_COPY_CLASS(wxTimerBase
)
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 #if defined(__WXMSW__)
112 #include "wx/msw/timer.h"
113 #elif defined(__WXMOTIF__)
114 #include "wx/motif/timer.h"
115 #elif defined(__WXGTK__)
116 #include "wx/gtk/timer.h"
117 #elif defined(__WXX11__) || defined(__WXMGL__)
118 #include "wx/generic/timer.h"
119 #elif defined (__WXCOCOA__)
120 #include "wx/cocoa/timer.h"
121 #elif defined(__WXMAC__)
122 #include "wx/mac/timer.h"
123 #elif defined(__WXPM__)
124 #include "wx/os2/timer.h"
127 // ----------------------------------------------------------------------------
128 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
129 // ----------------------------------------------------------------------------
131 class WXDLLEXPORT wxTimerRunner
134 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
135 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false)
138 m_timer
.Start(milli
, oneShot
);
141 void Start(int milli
, bool oneShot
= false)
143 m_timer
.Start(milli
, oneShot
);
148 if ( m_timer
.IsRunning() )
157 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 class WXDLLEXPORT wxTimerEvent
: public wxEvent
167 wxTimerEvent(int timerid
= 0, int interval
= 0) : wxEvent(timerid
)
169 m_eventType
= wxEVT_TIMER
;
171 m_interval
= interval
;
175 int GetInterval() const { return m_interval
; }
177 // implement the base class pure virtual
178 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
183 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
186 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
188 #define wxTimerEventHandler(func) \
189 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
191 #define EVT_TIMER(timerid, func) \
192 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
194 #endif // wxUSE_GUI && wxUSE_TIMER