]>
Commit | Line | Data |
---|---|---|
0470b1e6 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/timer.h | |
ed791986 | 3 | // Purpose: wxTimer, wxStopWatch and global time-related functions |
99d80019 | 4 | // Author: Julian Smart |
ed791986 VZ |
5 | // Modified by: Vadim Zeitlin (wxTimerBase) |
6 | // Guillermo Rodriguez (global clean up) | |
0470b1e6 VZ |
7 | // Created: 04/01/98 |
8 | // RCS-ID: $Id$ | |
99d80019 | 9 | // Copyright: (c) Julian Smart |
65571936 | 10 | // Licence: wxWindows licence |
0470b1e6 VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_TIMER_H_BASE_ |
14 | #define _WX_TIMER_H_BASE_ | |
c801d85f | 15 | |
2ecf902b WS |
16 | #include "wx/defs.h" |
17 | ||
18 | #if wxUSE_GUI && wxUSE_TIMER | |
19 | ||
0470b1e6 | 20 | #include "wx/object.h" |
d895ad7c | 21 | #include "wx/longlong.h" |
ed791986 | 22 | #include "wx/event.h" |
5fb98c22 | 23 | #include "wx/stopwatch.h" // for backwards compatibility |
186453cc | 24 | #include "wx/window.h" // only for NewControlId() |
5fb98c22 | 25 | |
0470b1e6 | 26 | |
b0a2d8a8 VZ |
27 | // more readable flags for Start(): |
28 | ||
29 | // generate notifications periodically until the timer is stopped (default) | |
cb719f2e | 30 | #define wxTIMER_CONTINUOUS false |
b0a2d8a8 VZ |
31 | |
32 | // only send the notification once and then stop the timer | |
cb719f2e | 33 | #define wxTIMER_ONE_SHOT true |
b0a2d8a8 | 34 | |
0470b1e6 | 35 | // the interface of wxTimer class |
01ebf752 | 36 | class WXDLLEXPORT wxTimerBase : public wxEvtHandler |
0470b1e6 VZ |
37 | { |
38 | public: | |
ed791986 VZ |
39 | // ctors and initializers |
40 | // ---------------------- | |
41 | ||
42 | // default: if you don't call SetOwner(), your only chance to get timer | |
43 | // notifications is to override Notify() in the derived class | |
4c286ef6 DS |
44 | wxTimerBase() |
45 | { Init(); SetOwner(this); } | |
ed791986 VZ |
46 | |
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 | |
49 | // EVT_TIMER | |
cb719f2e | 50 | wxTimerBase(wxEvtHandler *owner, int timerid = wxID_ANY) |
c5c5dad5 | 51 | { Init(); SetOwner(owner, timerid); } |
ed791986 VZ |
52 | |
53 | // same as ctor above | |
cb719f2e | 54 | void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY) |
9249d38d VZ |
55 | { |
56 | m_owner = owner; | |
57 | m_idTimer = timerid == wxID_ANY ? wxWindow::NewControlId() : timerid; | |
58 | } | |
59 | ||
60 | wxEvtHandler *GetOwner() const { return m_owner; } | |
0470b1e6 | 61 | |
799ea011 | 62 | virtual ~wxTimerBase(); |
03e11df5 | 63 | |
0470b1e6 VZ |
64 | // working with the timer |
65 | // ---------------------- | |
66 | ||
67 | // start the timer: if milliseconds == -1, use the same value as for the | |
68 | // last Start() | |
99646f7e VZ |
69 | // |
70 | // it is now valid to call Start() multiple times: this just restarts the | |
71 | // timer if it is already running | |
cb719f2e | 72 | virtual bool Start(int milliseconds = -1, bool oneShot = false); |
0470b1e6 VZ |
73 | |
74 | // stop the timer | |
75 | virtual void Stop() = 0; | |
76 | ||
ed791986 VZ |
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(); | |
0470b1e6 VZ |
80 | |
81 | // getting info | |
82 | // ------------ | |
83 | ||
cb719f2e | 84 | // return true if the timer is running |
0470b1e6 VZ |
85 | virtual bool IsRunning() const = 0; |
86 | ||
4c286ef6 DS |
87 | // return the timer ID |
88 | int GetId() const { return m_idTimer; } | |
89 | ||
90 | // get the (last) timer interval in milliseconds | |
0470b1e6 VZ |
91 | int GetInterval() const { return m_milli; } |
92 | ||
cb719f2e | 93 | // return true if the timer is one shot |
0470b1e6 VZ |
94 | bool IsOneShot() const { return m_oneShot; } |
95 | ||
0470b1e6 | 96 | protected: |
ed791986 | 97 | // common part of all ctors |
4c286ef6 DS |
98 | void Init() |
99 | { m_owner = NULL; m_idTimer = wxID_ANY; m_milli = 0; m_oneShot = false; } | |
ed791986 VZ |
100 | |
101 | wxEvtHandler *m_owner; | |
102 | int m_idTimer; | |
0470b1e6 | 103 | int m_milli; // the timer interval |
cb719f2e | 104 | bool m_oneShot; // true if one shot |
22f3361e VZ |
105 | |
106 | DECLARE_NO_COPY_CLASS(wxTimerBase) | |
0470b1e6 VZ |
107 | }; |
108 | ||
ed791986 VZ |
109 | // ---------------------------------------------------------------------------- |
110 | // wxTimer itself | |
111 | // ---------------------------------------------------------------------------- | |
112 | ||
2049ba38 | 113 | #if defined(__WXMSW__) |
0470b1e6 | 114 | #include "wx/msw/timer.h" |
2049ba38 | 115 | #elif defined(__WXMOTIF__) |
0470b1e6 | 116 | #include "wx/motif/timer.h" |
1be7a35c | 117 | #elif defined(__WXGTK20__) |
0470b1e6 | 118 | #include "wx/gtk/timer.h" |
1be7a35c MR |
119 | #elif defined(__WXGTK__) |
120 | #include "wx/gtk1/timer.h" | |
b3c86150 | 121 | #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXDFB__) |
c331ac2e | 122 | #include "wx/generic/timer.h" |
2fcd7f64 RN |
123 | #elif defined (__WXCOCOA__) |
124 | #include "wx/cocoa/timer.h" | |
34138703 | 125 | #elif defined(__WXMAC__) |
0470b1e6 | 126 | #include "wx/mac/timer.h" |
1777b9bb | 127 | #elif defined(__WXPM__) |
0470b1e6 | 128 | #include "wx/os2/timer.h" |
c801d85f KB |
129 | #endif |
130 | ||
52a07708 VZ |
131 | // ---------------------------------------------------------------------------- |
132 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | class WXDLLEXPORT wxTimerRunner | |
136 | { | |
137 | public: | |
138 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | |
cb719f2e | 139 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false) |
52a07708 VZ |
140 | : m_timer(timer) |
141 | { | |
142 | m_timer.Start(milli, oneShot); | |
143 | } | |
144 | ||
cb719f2e | 145 | void Start(int milli, bool oneShot = false) |
52a07708 VZ |
146 | { |
147 | m_timer.Start(milli, oneShot); | |
148 | } | |
149 | ||
150 | ~wxTimerRunner() | |
151 | { | |
152 | if ( m_timer.IsRunning() ) | |
153 | { | |
154 | m_timer.Stop(); | |
155 | } | |
156 | } | |
157 | ||
158 | private: | |
159 | wxTimer& m_timer; | |
fc7a2a60 VZ |
160 | |
161 | DECLARE_NO_COPY_CLASS(wxTimerRunner) | |
52a07708 VZ |
162 | }; |
163 | ||
0470b1e6 | 164 | // ---------------------------------------------------------------------------- |
ed791986 VZ |
165 | // wxTimerEvent |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | class WXDLLEXPORT wxTimerEvent : public wxEvent | |
169 | { | |
170 | public: | |
c5c5dad5 | 171 | wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid) |
ed791986 VZ |
172 | { |
173 | m_eventType = wxEVT_TIMER; | |
174 | ||
175 | m_interval = interval; | |
176 | } | |
177 | ||
178 | // accessors | |
179 | int GetInterval() const { return m_interval; } | |
180 | ||
acd15a3f VZ |
181 | // implement the base class pure virtual |
182 | virtual wxEvent *Clone() const { return new wxTimerEvent(*this); } | |
183 | ||
ed791986 VZ |
184 | private: |
185 | int m_interval; | |
186 | ||
fc7a2a60 | 187 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent) |
ed791986 VZ |
188 | }; |
189 | ||
190 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); | |
191 | ||
7fa03f04 | 192 | #define wxTimerEventHandler(func) \ |
8bc3ec1f | 193 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func) |
7fa03f04 | 194 | |
c5c5dad5 | 195 | #define EVT_TIMER(timerid, func) \ |
7fa03f04 | 196 | wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func)) |
ed791986 | 197 | |
1e6feb95 | 198 | #endif // wxUSE_GUI && wxUSE_TIMER |
52a07708 | 199 | |
c801d85f | 200 | #endif |
34138703 | 201 | // _WX_TIMER_H_BASE_ |