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