]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/timer.h
Do give focus to the wxNotebook page when switching to it under MSW.
[wxWidgets.git] / interface / wx / timer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: timer.h
3 // Purpose: interface of wxTimer
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
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
18 /**
19 @class wxTimer
20
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.
24
25 There are three different ways to use this class:
26
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().
41
42 @note A timer can only be used from the main thread.
43
44 @library{wxbase}
45 @category{misc}
46
47 @see wxStopWatch
48 */
49 class wxTimer : public wxEvtHandler
50 {
51 public:
52 /**
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.
56 */
57 wxTimer();
58
59 /**
60 Creates a timer and associates it with @a owner.
61 Please see SetOwner() for the description of parameters.
62 */
63 wxTimer(wxEvtHandler* owner, int id = -1);
64
65 /**
66 Destructor. Stops the timer if it is running.
67 */
68 virtual ~wxTimer();
69
70 /**
71 Returns the ID of the events generated by this timer.
72 */
73 int GetId() const;
74
75 /**
76 Returns the current interval for the timer (in milliseconds).
77 */
78 int GetInterval() const;
79
80 /**
81 Returns the current @e owner of the timer.
82
83 If non-@NULL this is the event handler which will receive the
84 timer events (see wxTimerEvent) when the timer is running.
85 */
86 wxEvtHandler* GetOwner() const;
87
88 /**
89 Returns @true if the timer is one shot, i.e. if it will stop after firing
90 the first notification automatically.
91 */
92 bool IsOneShot() const;
93
94 /**
95 Returns @true if the timer is running, @false if it is stopped.
96 */
97 bool IsRunning() const;
98
99 /**
100 This member should be overridden by the user if the default constructor was
101 used and SetOwner() wasn't called.
102
103 Perform whatever action which is to be taken periodically here.
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.
108 */
109 virtual void Notify();
110
111 /**
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.
116 */
117 void SetOwner(wxEvtHandler* owner, int id = -1);
118
119 /**
120 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
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).
123
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.
127
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
131 If the timer was already running, it will be stopped by this method before
132 restarting it.
133 */
134 virtual bool Start(int milliseconds = -1, bool oneShot = false);
135
136 /**
137 Stops the timer.
138 */
139 virtual void Stop();
140 };
141
142
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 };
156
157 /**
158 @class wxTimerEvent
159
160 wxTimerEvent object is passed to the event handler of timer events
161 (see wxTimer::SetOwner).
162
163 For example:
164
165 @code
166 class MyFrame : public wxFrame
167 {
168 public:
169 ...
170 void OnTimer(wxTimerEvent& event);
171
172 private:
173 wxTimer m_timer;
174 wxDECLARE_EVENT_TABLE();
175 };
176
177 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
178 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
179 wxEND_EVENT_TABLE()
180
181 MyFrame::MyFrame()
182 : m_timer(this, TIMER_ID)
183 {
184 m_timer.Start(1000); // 1 second interval
185 }
186
187 void MyFrame::OnTimer(wxTimerEvent& event)
188 {
189 // do whatever you want to do every second here
190 }
191 @endcode
192
193 @library{wxbase}
194 @category{events}
195
196 @see wxTimer
197 */
198 class wxTimerEvent : public wxEvent
199 {
200 public:
201 wxTimerEvent();
202 wxTimerEvent(wxTimer& timer);
203
204 /**
205 Returns the interval of the timer which generated this event.
206 */
207 int GetInterval() const;
208
209 /**
210 Returns the timer object which generated this event.
211 */
212 wxTimer& GetTimer() const;
213 };
214