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_
20 #include "wx/object.h"
21 #include "wx/longlong.h"
23 #include "wx/stopwatch.h" // for backwards compatibility
27 // more readable flags for Start():
29 // generate notifications periodically until the timer is stopped (default)
30 #define wxTIMER_CONTINUOUS false
32 // only send the notification once and then stop the timer
33 #define wxTIMER_ONE_SHOT true
35 class WXDLLIMPEXP_FWD_BASE wxTimerImpl
;
36 class WXDLLIMPEXP_FWD_BASE wxTimerEvent
;
39 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE
, wxEVT_TIMER
, wxTimerEvent
)
41 // the interface of wxTimer class
42 class WXDLLIMPEXP_BASE wxTimer
: public wxEvtHandler
45 // ctors and initializers
46 // ----------------------
48 // default: if you don't call SetOwner(), your only chance to get timer
49 // notifications is to override Notify() in the derived class
56 // ctor which allows to avoid having to override Notify() in the derived
57 // class: the owner will get timer notifications which can be handled with
59 wxTimer(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
62 SetOwner(owner
, timerid
);
66 void SetOwner(wxEvtHandler
*owner
, int timerid
= wxID_ANY
);
71 // working with the timer
72 // ----------------------
74 // NB: Start() and Stop() are not supposed to be overridden, they are only
75 // virtual for historical reasons, only Notify() can be overridden
77 // start the timer: if milliseconds == -1, use the same value as for the
80 // it is now valid to call Start() multiple times: this just restarts the
81 // timer if it is already running
82 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
84 // stop the timer, does nothing if the timer is not running
87 // override this in your wxTimer-derived class if you want to process timer
88 // messages in it, use non default ctor or SetOwner() otherwise
89 virtual void Notify();
95 // get the object notified about the timer events
96 wxEvtHandler
*GetOwner() const;
98 // return true if the timer is running
99 bool IsRunning() const;
101 // return the timer ID
104 // get the (last) timer interval in milliseconds
105 int GetInterval() const;
107 // return true if the timer is one shot
108 bool IsOneShot() const;
111 // common part of all ctors
116 DECLARE_NO_COPY_CLASS(wxTimer
)
119 // ----------------------------------------------------------------------------
120 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
121 // ----------------------------------------------------------------------------
123 class WXDLLIMPEXP_BASE wxTimerRunner
126 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
127 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false)
130 m_timer
.Start(milli
, oneShot
);
133 void Start(int milli
, bool oneShot
= false)
135 m_timer
.Start(milli
, oneShot
);
140 if ( m_timer
.IsRunning() )
149 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 class WXDLLIMPEXP_BASE wxTimerEvent
: public wxEvent
160 : wxEvent(wxID_ANY
, wxEVT_TIMER
) { m_timer
=NULL
; }
162 wxTimerEvent(wxTimer
& timer
)
163 : wxEvent(timer
.GetId(), wxEVT_TIMER
),
166 SetEventObject(timer
.GetOwner());
170 int GetInterval() const { return m_timer
->GetInterval(); }
171 wxTimer
& GetTimer() const { return *m_timer
; }
173 // implement the base class pure virtual
174 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
179 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
182 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
184 #define wxTimerEventHandler(func) \
185 wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
187 #define EVT_TIMER(timerid, func) \
188 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
190 #endif // wxUSE_TIMER
192 #endif // _WX_TIMER_H_BASE_