1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer, wxStopWatch and global time-related functions
4 // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch)
5 // Modified by: Vadim Zeitlin (wxTimerBase)
6 // Guillermo Rodriguez (global clean up)
9 // Copyright: (c) wxWindows team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_TIMER_H_BASE_
14 #define _WX_TIMER_H_BASE_
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "timerbase.h"
21 #include "wx/object.h"
22 #include "wx/longlong.h"
25 #include "wx/stopwatch.h" // for backwards compatibility
27 #if wxUSE_GUI && wxUSE_TIMER
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // more readable flags for Start():
35 // generate notifications periodically until the timer is stopped (default)
36 #define wxTIMER_CONTINUOUS FALSE
38 // only send the notification once and then stop the timer
39 #define wxTIMER_ONE_SHOT TRUE
41 // the interface of wxTimer class
42 class WXDLLEXPORT wxTimerBase
: 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
50 wxTimerBase() { Init(); SetOwner(this); }
52 // ctor which allows to avoid having to override Notify() in the derived
53 // class: the owner will get timer notifications which can be handled with
55 wxTimerBase(wxEvtHandler
*owner
, int timerid
= -1)
56 { Init(); SetOwner(owner
, timerid
); }
59 void SetOwner(wxEvtHandler
*owner
, int timerid
= -1)
60 { m_owner
= owner
; m_idTimer
= timerid
; }
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 // get the (last) timer interval in the milliseconds
88 int GetInterval() const { return m_milli
; }
90 // return TRUE if the timer is one shot
91 bool IsOneShot() const { return m_oneShot
; }
93 // return the timer ID
94 int GetId() const { return m_idTimer
; }
98 // common part of all ctors
99 void Init() { m_oneShot
= FALSE
; m_milli
= 0; }
101 wxEvtHandler
*m_owner
;
104 int m_milli
; // the timer interval
105 bool m_oneShot
; // TRUE if one shot
107 DECLARE_NO_COPY_CLASS(wxTimerBase
)
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 #if defined(__WXMSW__)
115 #include "wx/msw/timer.h"
116 #elif defined(__WXMOTIF__)
117 #include "wx/motif/timer.h"
118 #elif defined(__WXGTK__)
119 #include "wx/gtk/timer.h"
120 #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__)
121 #include "wx/generic/timer.h"
122 #elif defined(__WXMAC__)
123 #include "wx/mac/timer.h"
124 #elif defined(__WXPM__)
125 #include "wx/os2/timer.h"
128 // ----------------------------------------------------------------------------
129 // wxTimerRunner: starts the timer in its ctor, stops in the dtor
130 // ----------------------------------------------------------------------------
132 class WXDLLEXPORT wxTimerRunner
135 wxTimerRunner(wxTimer
& timer
) : m_timer(timer
) { }
136 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= FALSE
)
139 m_timer
.Start(milli
, oneShot
);
142 void Start(int milli
, bool oneShot
= FALSE
)
144 m_timer
.Start(milli
, oneShot
);
149 if ( m_timer
.IsRunning() )
158 DECLARE_NO_COPY_CLASS(wxTimerRunner
)
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 class WXDLLEXPORT wxTimerEvent
: public wxEvent
168 wxTimerEvent(int timerid
= 0, int interval
= 0) : wxEvent(timerid
)
170 m_eventType
= wxEVT_TIMER
;
172 m_interval
= interval
;
176 int GetInterval() const { return m_interval
; }
178 // implement the base class pure virtual
179 virtual wxEvent
*Clone() const { return new wxTimerEvent(*this); }
184 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent
)
187 typedef void (wxEvtHandler::*wxTimerEventFunction
)(wxTimerEvent
&);
189 #define EVT_TIMER(timerid, func) \
190 DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, timerid, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
192 #endif // wxUSE_GUI && wxUSE_TIMER