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
24 #include "wx/window.h" // only for NewControlId()
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 // the interface of wxTimer class
36 class WXDLLEXPORT wxTimerBase
: public wxEvtHandler
39 // ctors and initializers
40 // ----------------------
42 // default: if you don't call SetOwner(), your only chance to get timer
43 // notifications is to override Notify() in the derived class
45 { Init(); SetOwner(this); }
47 // ctor which allows to avoid having to override Notify() in the derived
48 // class: the owner will get timer notifications which can be handled with
50 wxTimerBase(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
51 { Init(); SetOwner(owner
, timerid
); }
54 void SetOwner(wxEvtHandler
*owner
, int timerid
= wxID_ANY
)
57 m_idTimer
= timerid
== wxID_ANY
? wxWindow::NewControlId() : timerid
;
60 wxEvtHandler
*GetOwner() const { return m_owner
; }
62 virtual ~wxTimerBase();
64 // working with the timer
65 // ----------------------
67 // start the timer: if milliseconds == -1, use the same value as for the
70 // it is now valid to call Start() multiple times: this just restarts the
71 // timer if it is already running
72 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
75 virtual void Stop() = 0;
77 // override this in your wxTimer-derived class if you want to process timer
78 // messages in it, use non default ctor or SetOwner() otherwise
79 virtual void Notify();
84 // return true if the timer is running
85 virtual bool IsRunning() const = 0;
87 // return the timer ID
88 int GetId() const { return m_idTimer
; }
90 // get the (last) timer interval in milliseconds
91 int GetInterval() const { return m_milli
; }
93 // return true if the timer is one shot
94 bool IsOneShot() const { return m_oneShot
; }
97 // common part of all ctors
99 { m_owner
= NULL
; m_idTimer
= wxID_ANY
; m_milli
= 0; m_oneShot
= false; }
101 wxEvtHandler
*m_owner
;
103 int m_milli
; // the timer interval
104 bool m_oneShot
; // true if one shot
106 DECLARE_NO_COPY_CLASS(wxTimerBase
)
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 #if defined(__WXMSW__)
114 #include "wx/msw/timer.h"
115 #elif defined(__WXMOTIF__)
116 #include "wx/motif/timer.h"
117 #elif defined(__WXGTK20__)
118 #include "wx/gtk/timer.h"
119 #elif defined(__WXGTK__)
120 #include "wx/gtk1/timer.h"
121 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__)
122 #include "wx/generic/timer.h"
123 #elif defined (__WXCOCOA__)
124 #include "wx/cocoa/timer.h"
125 #elif defined(__WXMAC__)
126 #include "wx/mac/timer.h"
127 #elif defined(__WXPM__)
128 #include "wx/os2/timer.h"
131 // ----------------------------------------------------------------------------
132 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
133 // ----------------------------------------------------------------------------
135 class WXDLLEXPORT wxTimerRunner
138 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
139 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false)
142 m_timer
.Start(milli
, oneShot
);
145 void Start(int milli
, bool oneShot
= false)
147 m_timer
.Start(milli
, oneShot
);
152 if ( m_timer
.IsRunning() )
161 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 class WXDLLEXPORT wxTimerEvent
: public wxEvent
171 wxTimerEvent(int timerid
= 0, int interval
= 0) : wxEvent(timerid
)
173 m_eventType
= wxEVT_TIMER
;
175 m_interval
= interval
;
179 int GetInterval() const { return m_interval
; }
181 // implement the base class pure virtual
182 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
187 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
190 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
192 #define wxTimerEventHandler(func) \
193 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
195 #define EVT_TIMER(timerid, func) \
196 wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
198 #endif // wxUSE_GUI && wxUSE_TIMER