]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: timer.h | |
e54c96f1 | 3 | // Purpose: interface of wxTimer |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
6e350141 RD |
9 | // generate notifications periodically until the timer is stopped (default) |
10 | #define wxTIMER_CONTINUOUS false | |
11 | ||
12 | // only send the notification once and then stop the timer | |
13 | #define wxTIMER_ONE_SHOT true | |
14 | ||
15 | wxEventType wxEVT_TIMER; | |
16 | ||
17 | ||
23324ae1 FM |
18 | /** |
19 | @class wxTimer | |
7c913512 | 20 | |
78e87bf7 FM |
21 | The wxTimer class allows you to execute code at specified intervals. |
22 | Its precision is platform-dependent, but in general will not be better than | |
23 | @c 1ms nor worse than @c 1s. | |
7c913512 | 24 | |
23324ae1 | 25 | There are three different ways to use this class: |
7c913512 | 26 | |
78e87bf7 FM |
27 | - You may derive a new class from wxTimer and override the |
28 | wxTimer::Notify member to perform the required action. | |
29 | - You may redirect the notifications to any wxEvtHandler derived object by | |
30 | using the non-default constructor or wxTimer::SetOwner. | |
31 | Then use the @c EVT_TIMER macro to connect it to the event handler which | |
32 | will receive wxTimerEvent notifications. | |
33 | - You may use a derived class and the @c EVT_TIMER macro to connect it to | |
34 | an event handler defined in the derived class. If the default constructor | |
35 | is used, the timer object will be its own owner object, since it is | |
36 | derived from wxEvtHandler. | |
37 | ||
38 | In any case, you must start the timer with wxTimer::Start() after constructing | |
39 | it before it actually starts sending notifications. | |
40 | It can be stopped later with wxTimer::Stop(). | |
7c913512 | 41 | |
cdbcf4c2 | 42 | @note A timer can only be used from the main thread. |
7c913512 | 43 | |
23324ae1 FM |
44 | @library{wxbase} |
45 | @category{misc} | |
7c913512 | 46 | |
e54c96f1 | 47 | @see wxStopWatch |
23324ae1 FM |
48 | */ |
49 | class wxTimer : public wxEvtHandler | |
50 | { | |
51 | public: | |
23324ae1 | 52 | /** |
78e87bf7 FM |
53 | Default constructor. |
54 | If you use it to construct the object and don't call SetOwner() later, | |
55 | you must override Notify() method to process the notifications. | |
23324ae1 FM |
56 | */ |
57 | wxTimer(); | |
78e87bf7 FM |
58 | |
59 | /** | |
60 | Creates a timer and associates it with @a owner. | |
61 | Please see SetOwner() for the description of parameters. | |
62 | */ | |
4cc4bfaf | 63 | wxTimer(wxEvtHandler* owner, int id = -1); |
23324ae1 FM |
64 | |
65 | /** | |
66 | Destructor. Stops the timer if it is running. | |
67 | */ | |
adaaa686 | 68 | virtual ~wxTimer(); |
23324ae1 FM |
69 | |
70 | /** | |
71 | Returns the ID of the events generated by this timer. | |
72 | */ | |
328f5751 | 73 | int GetId() const; |
23324ae1 FM |
74 | |
75 | /** | |
76 | Returns the current interval for the timer (in milliseconds). | |
77 | */ | |
328f5751 | 78 | int GetInterval() const; |
23324ae1 FM |
79 | |
80 | /** | |
81 | Returns the current @e owner of the timer. | |
78e87bf7 | 82 | |
7c913512 | 83 | If non-@NULL this is the event handler which will receive the |
78e87bf7 | 84 | timer events (see wxTimerEvent) when the timer is running. |
23324ae1 | 85 | */ |
5267aefd | 86 | wxEvtHandler* GetOwner() const; |
23324ae1 FM |
87 | |
88 | /** | |
78e87bf7 FM |
89 | Returns @true if the timer is one shot, i.e. if it will stop after firing |
90 | the first notification automatically. | |
23324ae1 | 91 | */ |
328f5751 | 92 | bool IsOneShot() const; |
23324ae1 FM |
93 | |
94 | /** | |
95 | Returns @true if the timer is running, @false if it is stopped. | |
96 | */ | |
328f5751 | 97 | bool IsRunning() const; |
23324ae1 FM |
98 | |
99 | /** | |
100 | This member should be overridden by the user if the default constructor was | |
101 | used and SetOwner() wasn't called. | |
78e87bf7 | 102 | |
23324ae1 | 103 | Perform whatever action which is to be taken periodically here. |
7d491133 VZ |
104 | |
105 | Notice that throwing exceptions from this method is currently not | |
106 | supported, use event-based timer handling approach if an exception can | |
107 | be thrown while handling timer notifications. | |
23324ae1 | 108 | */ |
adaaa686 | 109 | virtual void Notify(); |
23324ae1 FM |
110 | |
111 | /** | |
78e87bf7 FM |
112 | Associates the timer with the given @a owner object. |
113 | ||
114 | When the timer is running, the owner will receive timer events (see wxTimerEvent) | |
115 | with @a id equal to @a id specified here. | |
23324ae1 | 116 | */ |
4cc4bfaf | 117 | void SetOwner(wxEvtHandler* owner, int id = -1); |
23324ae1 FM |
118 | |
119 | /** | |
4cc4bfaf | 120 | (Re)starts the timer. If @a milliseconds parameter is -1 (value by default), |
23324ae1 FM |
121 | the previous value is used. Returns @false if the timer could not be started, |
122 | @true otherwise (in MS Windows timers are a limited resource). | |
3c4f71cc | 123 | |
78e87bf7 FM |
124 | If @a oneShot is @false (the default), the Notify() function will be called |
125 | repeatedly until the timer is stopped. | |
126 | If @true, it will be called only once and the timer will stop automatically. | |
3c4f71cc | 127 | |
78e87bf7 FM |
128 | To make your code more readable you may also use the following symbolic constants: |
129 | - wxTIMER_CONTINUOUS: Start a normal, continuously running, timer | |
130 | - wxTIMER_ONE_SHOT: Start a one shot timer | |
23324ae1 FM |
131 | If the timer was already running, it will be stopped by this method before |
132 | restarting it. | |
133 | */ | |
adaaa686 | 134 | virtual bool Start(int milliseconds = -1, bool oneShot = false); |
23324ae1 FM |
135 | |
136 | /** | |
137 | Stops the timer. | |
138 | */ | |
adaaa686 | 139 | virtual void Stop(); |
23324ae1 FM |
140 | }; |
141 | ||
142 | ||
6e350141 RD |
143 | /** |
144 | @class wxTimerRunner | |
145 | ||
146 | Starts the timer in its ctor, stops in the dtor. | |
147 | */ | |
148 | class wxTimerRunner | |
149 | { | |
150 | public: | |
151 | wxTimerRunner(wxTimer& timer); | |
152 | wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false); | |
153 | void Start(int milli, bool oneShot = false); | |
154 | ~wxTimerRunner(); | |
155 | }; | |
e54c96f1 | 156 | |
23324ae1 FM |
157 | /** |
158 | @class wxTimerEvent | |
7c913512 | 159 | |
3051a44a FM |
160 | wxTimerEvent object is passed to the event handler of timer events |
161 | (see wxTimer::SetOwner). | |
7c913512 | 162 | |
23324ae1 | 163 | For example: |
7c913512 | 164 | |
23324ae1 FM |
165 | @code |
166 | class MyFrame : public wxFrame | |
167 | { | |
168 | public: | |
169 | ... | |
170 | void OnTimer(wxTimerEvent& event); | |
7c913512 | 171 | |
23324ae1 FM |
172 | private: |
173 | wxTimer m_timer; | |
a0e9a5df | 174 | wxDECLARE_EVENT_TABLE(); |
23324ae1 | 175 | }; |
7c913512 | 176 | |
a0e9a5df | 177 | wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) |
23324ae1 | 178 | EVT_TIMER(TIMER_ID, MyFrame::OnTimer) |
a0e9a5df | 179 | wxEND_EVENT_TABLE() |
7c913512 | 180 | |
23324ae1 FM |
181 | MyFrame::MyFrame() |
182 | : m_timer(this, TIMER_ID) | |
183 | { | |
184 | m_timer.Start(1000); // 1 second interval | |
185 | } | |
7c913512 | 186 | |
23324ae1 FM |
187 | void MyFrame::OnTimer(wxTimerEvent& event) |
188 | { | |
189 | // do whatever you want to do every second here | |
190 | } | |
191 | @endcode | |
7c913512 | 192 | |
23324ae1 FM |
193 | @library{wxbase} |
194 | @category{events} | |
7c913512 | 195 | |
e54c96f1 | 196 | @see wxTimer |
23324ae1 FM |
197 | */ |
198 | class wxTimerEvent : public wxEvent | |
199 | { | |
200 | public: | |
201 | /** | |
202 | Returns the interval of the timer which generated this event. | |
203 | */ | |
328f5751 | 204 | int GetInterval() const; |
23324ae1 FM |
205 | |
206 | /** | |
207 | Returns the timer object which generated this event. | |
208 | */ | |
5267aefd | 209 | wxTimer& GetTimer() const; |
23324ae1 | 210 | }; |
e54c96f1 | 211 |