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