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)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TIMER_H_BASE_
13 #define _WX_TIMER_H_BASE_
19 #include "wx/object.h"
20 #include "wx/longlong.h"
22 #include "wx/stopwatch.h" // for backwards compatibility
26 // more readable flags for Start():
28 // generate notifications periodically until the timer is stopped (default)
29 #define wxTIMER_CONTINUOUS false
31 // only send the notification once and then stop the timer
32 #define wxTIMER_ONE_SHOT true
34 class WXDLLIMPEXP_FWD_BASE wxTimerImpl
;
35 class WXDLLIMPEXP_FWD_BASE wxTimerEvent
;
38 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE
, wxEVT_TIMER
, wxTimerEvent
);
40 // the interface of wxTimer class
41 class WXDLLIMPEXP_BASE wxTimer
: public wxEvtHandler
44 // ctors and initializers
45 // ----------------------
47 // default: if you don't call SetOwner(), your only chance to get timer
48 // notifications is to override Notify() in the derived class
55 // ctor which allows to avoid having to override Notify() in the derived
56 // class: the owner will get timer notifications which can be handled with
58 wxTimer(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
61 SetOwner(owner
, timerid
);
65 void SetOwner(wxEvtHandler
*owner
, int timerid
= wxID_ANY
);
70 // working with the timer
71 // ----------------------
73 // NB: Start() and Stop() are not supposed to be overridden, they are only
74 // virtual for historical reasons, only Notify() can be overridden
76 // start the timer: if milliseconds == -1, use the same value as for the
79 // it is now valid to call Start() multiple times: this just restarts the
80 // timer if it is already running
81 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
83 // start the timer for one iteration only, this is just a simple wrapper
85 bool StartOnce(int milliseconds
= -1) { return Start(milliseconds
, true); }
87 // stop the timer, does nothing if the timer is not running
90 // override this in your wxTimer-derived class if you want to process timer
91 // messages in it, use non default ctor or SetOwner() otherwise
92 virtual void Notify();
98 // get the object notified about the timer events
99 wxEvtHandler
*GetOwner() const;
101 // return true if the timer is running
102 bool IsRunning() const;
104 // return the timer ID
107 // get the (last) timer interval in milliseconds
108 int GetInterval() const;
110 // return true if the timer is one shot
111 bool IsOneShot() const;
114 // common part of all ctors
119 wxDECLARE_NO_COPY_CLASS(wxTimer
);
122 // ----------------------------------------------------------------------------
123 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
124 // ----------------------------------------------------------------------------
126 class WXDLLIMPEXP_BASE 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() )
152 wxDECLARE_NO_COPY_CLASS(wxTimerRunner
);
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 class WXDLLIMPEXP_BASE wxTimerEvent
: public wxEvent
163 : wxEvent(wxID_ANY
, wxEVT_TIMER
) { m_timer
=NULL
; }
165 wxTimerEvent(wxTimer
& timer
)
166 : wxEvent(timer
.GetId(), wxEVT_TIMER
),
169 SetEventObject(timer
.GetOwner());
173 int GetInterval() const { return m_timer
->GetInterval(); }
174 wxTimer
& GetTimer() const { return *m_timer
; }
176 // implement the base class pure virtual
177 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
178 virtual wxEventCategory
GetEventCategory() const { return wxEVT_CATEGORY_TIMER
; }
183 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
186 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
188 #define wxTimerEventHandler(func) \
189 wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
191 #define EVT_TIMER(timerid, func) \
192 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
194 #endif // wxUSE_TIMER
196 #endif // _WX_TIMER_H_BASE_