| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/timer.h |
| 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) |
| 7 | // Created: 04/01/98 |
| 8 | // RCS-ID: $Id$ |
| 9 | // Copyright: (c) wxWidgets team |
| 10 | // Licence: wxWindows licence |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | #ifndef _WX_TIMER_H_BASE_ |
| 14 | #define _WX_TIMER_H_BASE_ |
| 15 | |
| 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 17 | #pragma interface "timerbase.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "wx/setup.h" |
| 21 | #include "wx/object.h" |
| 22 | #include "wx/longlong.h" |
| 23 | #include "wx/event.h" |
| 24 | |
| 25 | #include "wx/stopwatch.h" // for backwards compatibility |
| 26 | |
| 27 | #if wxUSE_GUI && wxUSE_TIMER |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | // wxTimer |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | // more readable flags for Start(): |
| 34 | |
| 35 | // generate notifications periodically until the timer is stopped (default) |
| 36 | #define wxTIMER_CONTINUOUS false |
| 37 | |
| 38 | // only send the notification once and then stop the timer |
| 39 | #define wxTIMER_ONE_SHOT true |
| 40 | |
| 41 | // the interface of wxTimer class |
| 42 | class WXDLLEXPORT wxTimerBase : public wxEvtHandler |
| 43 | { |
| 44 | public: |
| 45 | // ctors and initializers |
| 46 | // ---------------------- |
| 47 | |
| 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); } |
| 51 | |
| 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 |
| 54 | // EVT_TIMER |
| 55 | wxTimerBase(wxEvtHandler *owner, int timerid = wxID_ANY) |
| 56 | { Init(); SetOwner(owner, timerid); } |
| 57 | |
| 58 | // same as ctor above |
| 59 | void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY) |
| 60 | { m_owner = owner; m_idTimer = timerid; } |
| 61 | wxEvtHandler* GetOwner() const { return m_owner; } |
| 62 | |
| 63 | virtual ~wxTimerBase(); |
| 64 | |
| 65 | // working with the timer |
| 66 | // ---------------------- |
| 67 | |
| 68 | // start the timer: if milliseconds == -1, use the same value as for the |
| 69 | // last Start() |
| 70 | // |
| 71 | // it is now valid to call Start() multiple times: this just restarts the |
| 72 | // timer if it is already running |
| 73 | virtual bool Start(int milliseconds = -1, bool oneShot = false); |
| 74 | |
| 75 | // stop the timer |
| 76 | virtual void Stop() = 0; |
| 77 | |
| 78 | // override this in your wxTimer-derived class if you want to process timer |
| 79 | // messages in it, use non default ctor or SetOwner() otherwise |
| 80 | virtual void Notify(); |
| 81 | |
| 82 | // getting info |
| 83 | // ------------ |
| 84 | |
| 85 | // return true if the timer is running |
| 86 | virtual bool IsRunning() const = 0; |
| 87 | |
| 88 | // get the (last) timer interval in the milliseconds |
| 89 | int GetInterval() const { return m_milli; } |
| 90 | |
| 91 | // return true if the timer is one shot |
| 92 | bool IsOneShot() const { return m_oneShot; } |
| 93 | |
| 94 | // return the timer ID |
| 95 | int GetId() const { return m_idTimer; } |
| 96 | |
| 97 | |
| 98 | protected: |
| 99 | // common part of all ctors |
| 100 | void Init() { m_oneShot = false; m_milli = 0; } |
| 101 | |
| 102 | wxEvtHandler *m_owner; |
| 103 | int m_idTimer; |
| 104 | |
| 105 | int m_milli; // the timer interval |
| 106 | bool m_oneShot; // true if one shot |
| 107 | |
| 108 | DECLARE_NO_COPY_CLASS(wxTimerBase) |
| 109 | }; |
| 110 | |
| 111 | // ---------------------------------------------------------------------------- |
| 112 | // wxTimer itself |
| 113 | // ---------------------------------------------------------------------------- |
| 114 | |
| 115 | #if defined(__WXMSW__) |
| 116 | #include "wx/msw/timer.h" |
| 117 | #elif defined(__WXMOTIF__) |
| 118 | #include "wx/motif/timer.h" |
| 119 | #elif defined(__WXGTK__) |
| 120 | #include "wx/gtk/timer.h" |
| 121 | #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__) |
| 122 | #include "wx/generic/timer.h" |
| 123 | #elif defined(__WXMAC__) |
| 124 | #include "wx/mac/timer.h" |
| 125 | #elif defined(__WXPM__) |
| 126 | #include "wx/os2/timer.h" |
| 127 | #endif |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | |
| 133 | class WXDLLEXPORT wxTimerRunner |
| 134 | { |
| 135 | public: |
| 136 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } |
| 137 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false) |
| 138 | : m_timer(timer) |
| 139 | { |
| 140 | m_timer.Start(milli, oneShot); |
| 141 | } |
| 142 | |
| 143 | void Start(int milli, bool oneShot = false) |
| 144 | { |
| 145 | m_timer.Start(milli, oneShot); |
| 146 | } |
| 147 | |
| 148 | ~wxTimerRunner() |
| 149 | { |
| 150 | if ( m_timer.IsRunning() ) |
| 151 | { |
| 152 | m_timer.Stop(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | wxTimer& m_timer; |
| 158 | |
| 159 | DECLARE_NO_COPY_CLASS(wxTimerRunner) |
| 160 | }; |
| 161 | |
| 162 | // ---------------------------------------------------------------------------- |
| 163 | // wxTimerEvent |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | |
| 166 | class WXDLLEXPORT wxTimerEvent : public wxEvent |
| 167 | { |
| 168 | public: |
| 169 | wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid) |
| 170 | { |
| 171 | m_eventType = wxEVT_TIMER; |
| 172 | |
| 173 | m_interval = interval; |
| 174 | } |
| 175 | |
| 176 | // accessors |
| 177 | int GetInterval() const { return m_interval; } |
| 178 | |
| 179 | // implement the base class pure virtual |
| 180 | virtual wxEvent *Clone() const { return new wxTimerEvent(*this); } |
| 181 | |
| 182 | private: |
| 183 | int m_interval; |
| 184 | |
| 185 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent) |
| 186 | }; |
| 187 | |
| 188 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); |
| 189 | |
| 190 | #define EVT_TIMER(timerid, func) \ |
| 191 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, timerid, wxID_ANY, (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxTimerEventFunction, & func ), NULL), |
| 192 | |
| 193 | #endif // wxUSE_GUI && wxUSE_TIMER |
| 194 | |
| 195 | #endif |
| 196 | // _WX_TIMER_H_BASE_ |