]>
Commit | Line | Data |
---|---|---|
0470b1e6 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/timer.h | |
ed791986 VZ |
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) | |
0470b1e6 VZ |
7 | // Created: 04/01/98 |
8 | // RCS-ID: $Id$ | |
ed791986 | 9 | // Copyright: (c) wxWindows team |
371a5b4e | 10 | // Licence: wxWindows licence |
0470b1e6 VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
34138703 JS |
13 | #ifndef _WX_TIMER_H_BASE_ |
14 | #define _WX_TIMER_H_BASE_ | |
c801d85f | 15 | |
12028905 | 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
0470b1e6 VZ |
17 | #pragma interface "timerbase.h" |
18 | #endif | |
19 | ||
d895ad7c | 20 | #include "wx/setup.h" |
0470b1e6 | 21 | #include "wx/object.h" |
d895ad7c | 22 | #include "wx/longlong.h" |
ed791986 | 23 | #include "wx/event.h" |
0470b1e6 | 24 | |
5fb98c22 VZ |
25 | #include "wx/stopwatch.h" // for backwards compatibility |
26 | ||
1e6feb95 | 27 | #if wxUSE_GUI && wxUSE_TIMER |
52a07708 | 28 | |
0470b1e6 VZ |
29 | // ---------------------------------------------------------------------------- |
30 | // wxTimer | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
b0a2d8a8 VZ |
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 | ||
0470b1e6 | 41 | // the interface of wxTimer class |
01ebf752 | 42 | class WXDLLEXPORT wxTimerBase : public wxEvtHandler |
0470b1e6 VZ |
43 | { |
44 | public: | |
ed791986 VZ |
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 | |
01ebf752 | 50 | wxTimerBase() { Init(); SetOwner(this); } |
ed791986 VZ |
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 | |
c5c5dad5 DE |
55 | wxTimerBase(wxEvtHandler *owner, int timerid = -1) |
56 | { Init(); SetOwner(owner, timerid); } | |
ed791986 VZ |
57 | |
58 | // same as ctor above | |
c5c5dad5 DE |
59 | void SetOwner(wxEvtHandler *owner, int timerid = -1) |
60 | { m_owner = owner; m_idTimer = timerid; } | |
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 | |
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 | ||
84 | // return TRUE if the timer is running | |
85 | virtual bool IsRunning() const = 0; | |
86 | ||
87 | // get the (last) timer interval in the milliseconds | |
88 | int GetInterval() const { return m_milli; } | |
89 | ||
90 | // return TRUE if the timer is one shot | |
91 | bool IsOneShot() const { return m_oneShot; } | |
92 | ||
0470b1e6 | 93 | protected: |
ed791986 VZ |
94 | // common part of all ctors |
95 | void Init() { m_oneShot = FALSE; m_milli = 0; } | |
96 | ||
97 | wxEvtHandler *m_owner; | |
98 | int m_idTimer; | |
99 | ||
0470b1e6 VZ |
100 | int m_milli; // the timer interval |
101 | bool m_oneShot; // TRUE if one shot | |
22f3361e VZ |
102 | |
103 | DECLARE_NO_COPY_CLASS(wxTimerBase) | |
0470b1e6 VZ |
104 | }; |
105 | ||
ed791986 VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // wxTimer itself | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
2049ba38 | 110 | #if defined(__WXMSW__) |
0470b1e6 | 111 | #include "wx/msw/timer.h" |
2049ba38 | 112 | #elif defined(__WXMOTIF__) |
0470b1e6 | 113 | #include "wx/motif/timer.h" |
2049ba38 | 114 | #elif defined(__WXGTK__) |
0470b1e6 | 115 | #include "wx/gtk/timer.h" |
51a8edc7 | 116 | #elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__) |
c331ac2e | 117 | #include "wx/generic/timer.h" |
34138703 | 118 | #elif defined(__WXMAC__) |
0470b1e6 | 119 | #include "wx/mac/timer.h" |
1777b9bb | 120 | #elif defined(__WXPM__) |
0470b1e6 | 121 | #include "wx/os2/timer.h" |
c801d85f KB |
122 | #endif |
123 | ||
52a07708 VZ |
124 | // ---------------------------------------------------------------------------- |
125 | // wxTimerRunner: starts the timer in its ctor, stops in the dtor | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | class WXDLLEXPORT wxTimerRunner | |
129 | { | |
130 | public: | |
131 | wxTimerRunner(wxTimer& timer) : m_timer(timer) { } | |
132 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE) | |
133 | : m_timer(timer) | |
134 | { | |
135 | m_timer.Start(milli, oneShot); | |
136 | } | |
137 | ||
138 | void Start(int milli, bool oneShot = FALSE) | |
139 | { | |
140 | m_timer.Start(milli, oneShot); | |
141 | } | |
142 | ||
143 | ~wxTimerRunner() | |
144 | { | |
145 | if ( m_timer.IsRunning() ) | |
146 | { | |
147 | m_timer.Stop(); | |
148 | } | |
149 | } | |
150 | ||
151 | private: | |
152 | wxTimer& m_timer; | |
fc7a2a60 VZ |
153 | |
154 | DECLARE_NO_COPY_CLASS(wxTimerRunner) | |
52a07708 VZ |
155 | }; |
156 | ||
0470b1e6 | 157 | // ---------------------------------------------------------------------------- |
ed791986 VZ |
158 | // wxTimerEvent |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | class WXDLLEXPORT wxTimerEvent : public wxEvent | |
162 | { | |
163 | public: | |
c5c5dad5 | 164 | wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid) |
ed791986 VZ |
165 | { |
166 | m_eventType = wxEVT_TIMER; | |
167 | ||
168 | m_interval = interval; | |
169 | } | |
170 | ||
171 | // accessors | |
172 | int GetInterval() const { return m_interval; } | |
173 | ||
acd15a3f VZ |
174 | // implement the base class pure virtual |
175 | virtual wxEvent *Clone() const { return new wxTimerEvent(*this); } | |
176 | ||
ed791986 VZ |
177 | private: |
178 | int m_interval; | |
179 | ||
fc7a2a60 | 180 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent) |
ed791986 VZ |
181 | }; |
182 | ||
183 | typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&); | |
184 | ||
c5c5dad5 DE |
185 | #define EVT_TIMER(timerid, func) \ |
186 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, timerid, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL), | |
ed791986 | 187 | |
1e6feb95 | 188 | #endif // wxUSE_GUI && wxUSE_TIMER |
52a07708 | 189 | |
c801d85f | 190 | #endif |
34138703 | 191 | // _WX_TIMER_H_BASE_ |