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
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 // the interface of wxTimer class
35 class WXDLLEXPORT wxTimerBase
: public wxEvtHandler
38 // ctors and initializers
39 // ----------------------
41 // default: if you don't call SetOwner(), your only chance to get timer
42 // notifications is to override Notify() in the derived class
44 { Init(); SetOwner(this); }
46 // ctor which allows to avoid having to override Notify() in the derived
47 // class: the owner will get timer notifications which can be handled with
49 wxTimerBase(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
50 { Init(); SetOwner(owner
, timerid
); }
53 void SetOwner(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
54 { m_owner
= owner
; m_idTimer
= timerid
== wxID_ANY
? wxNewId() : timerid
; }
55 wxEvtHandler
* GetOwner() const { return m_owner
; }
57 virtual ~wxTimerBase();
59 // working with the timer
60 // ----------------------
62 // start the timer: if milliseconds == -1, use the same value as for the
65 // it is now valid to call Start() multiple times: this just restarts the
66 // timer if it is already running
67 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
70 virtual void Stop() = 0;
72 // override this in your wxTimer-derived class if you want to process timer
73 // messages in it, use non default ctor or SetOwner() otherwise
74 virtual void Notify();
79 // return true if the timer is running
80 virtual bool IsRunning() const = 0;
82 // return the timer ID
83 int GetId() const { return m_idTimer
; }
85 // get the (last) timer interval in milliseconds
86 int GetInterval() const { return m_milli
; }
88 // return true if the timer is one shot
89 bool IsOneShot() const { return m_oneShot
; }
92 // common part of all ctors
94 { m_owner
= NULL
; m_idTimer
= wxID_ANY
; m_milli
= 0; m_oneShot
= false; }
96 wxEvtHandler
*m_owner
;
98 int m_milli
; // the timer interval
99 bool m_oneShot
; // true if one shot
101 DECLARE_NO_COPY_CLASS(wxTimerBase
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 #if defined(__WXMSW__)
109 #include "wx/msw/timer.h"
110 #elif defined(__WXMOTIF__)
111 #include "wx/motif/timer.h"
112 #elif defined(__WXGTK20__)
113 #include "wx/gtk/timer.h"
114 #elif defined(__WXGTK__)
115 #include "wx/gtk1/timer.h"
116 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__)
117 #include "wx/generic/timer.h"
118 #elif defined (__WXCOCOA__)
119 #include "wx/cocoa/timer.h"
120 #elif defined(__WXMAC__)
121 #include "wx/mac/timer.h"
122 #elif defined(__WXPM__)
123 #include "wx/os2/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() )
156 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 class WXDLLEXPORT wxTimerEvent
: public wxEvent
166 wxTimerEvent(int timerid
= 0, int interval
= 0) : wxEvent(timerid
)
168 m_eventType
= wxEVT_TIMER
;
170 m_interval
= interval
;
174 int GetInterval() const { return m_interval
; }
176 // implement the base class pure virtual
177 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
182 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
185 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
187 #define wxTimerEventHandler(func) \
188 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
190 #define EVT_TIMER(timerid, func) \
191 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
193 #endif // wxUSE_GUI && wxUSE_TIMER