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
)
56 m_idTimer
= timerid
== wxID_ANY ? wxWindow
::NewControlId() : timerid
;
59 wxEvtHandler
*GetOwner() const { return m_owner
; }
61 virtual ~wxTimerBase();
63 // working with the timer
64 // ----------------------
66 // start the timer: if milliseconds == -1, use the same value as for the
69 // it is now valid to call Start() multiple times: this just restarts the
70 // timer if it is already running
71 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
74 virtual void Stop() = 0;
76 // override this in your wxTimer-derived class if you want to process timer
77 // messages in it, use non default ctor or SetOwner() otherwise
78 virtual void Notify();
83 // return true if the timer is running
84 virtual bool IsRunning() const = 0;
86 // return the timer ID
87 int GetId() const { return m_idTimer
; }
89 // get the (last) timer interval in milliseconds
90 int GetInterval() const { return m_milli
; }
92 // return true if the timer is one shot
93 bool IsOneShot() const { return m_oneShot
; }
96 // common part of all ctors
98 { m_owner
= NULL
; m_idTimer
= wxID_ANY
; m_milli
= 0; m_oneShot
= false; }
100 wxEvtHandler
*m_owner
;
102 int m_milli
; // the timer interval
103 bool m_oneShot
; // true if one shot
105 DECLARE_NO_COPY_CLASS(wxTimerBase
)
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 #if defined(__WXMSW__)
113 #include "wx/msw/timer.h"
114 #elif defined(__WXMOTIF__)
115 #include "wx/motif/timer.h"
116 #elif defined(__WXGTK20__)
117 #include "wx/gtk/timer.h"
118 #elif defined(__WXGTK__)
119 #include "wx/gtk1/timer.h"
120 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__)
121 #include "wx/generic/timer.h"
122 #elif defined (__WXCOCOA__)
123 #include "wx/cocoa/timer.h"
124 #elif defined(__WXMAC__)
125 #include "wx/mac/timer.h"
126 #elif defined(__WXPM__)
127 #include "wx/os2/timer.h"
130 // ----------------------------------------------------------------------------
131 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
132 // ----------------------------------------------------------------------------
134 class WXDLLEXPORT wxTimerRunner
137 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
138 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false)
141 m_timer
.Start(milli
, oneShot
);
144 void Start(int milli
, bool oneShot
= false)
146 m_timer
.Start(milli
, oneShot
);
151 if ( m_timer
.IsRunning() )
160 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 class WXDLLEXPORT wxTimerEvent
: public wxEvent
170 wxTimerEvent(int timerid
= 0, int interval
= 0) : wxEvent(timerid
)
172 m_eventType
= wxEVT_TIMER
;
174 m_interval
= interval
;
178 int GetInterval() const { return m_interval
; }
180 // implement the base class pure virtual
181 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
186 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
189 typedef void (wxEvtHandler
::*wxTimerEventFunction
)(wxTimerEvent
&);
191 #define wxTimerEventHandler(func) \
192 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
194 #define EVT_TIMER(timerid, func) \
195 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
197 #endif // wxUSE_GUI && wxUSE_TIMER