]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/timer.h
Don't look for catalogs in AddCatalogLookupPathPrefix() path directly.
[wxWidgets.git] / interface / wx / timer.h
CommitLineData
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
15wxEventType 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*/
49class wxTimer : public wxEvtHandler
50{
51public:
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 /**
0824e369 89 Returns @true if the timer is one shot, i.e.\ if it will stop after firing
78e87bf7 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
7c602225
VZ
131 Alternatively, use StartOnce().
132
23324ae1
FM
133 If the timer was already running, it will be stopped by this method before
134 restarting it.
135 */
7c602225
VZ
136 virtual bool Start(int milliseconds = -1, bool oneShot = wxTIMER_CONTINUOUS);
137
138 /**
139 Starts the timer for a once-only notification.
140
141 This is a simple wrapper for Start() with @c wxTIMER_ONE_SHOT parameter.
142
143 @since 2.9.5
144 */
145 bool StartOnce(int milliseconds = -1);
23324ae1
FM
146
147 /**
148 Stops the timer.
149 */
adaaa686 150 virtual void Stop();
23324ae1
FM
151};
152
153
6e350141
RD
154/**
155 @class wxTimerRunner
156
157 Starts the timer in its ctor, stops in the dtor.
158*/
159class wxTimerRunner
160{
161public:
162 wxTimerRunner(wxTimer& timer);
163 wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false);
164 void Start(int milli, bool oneShot = false);
165 ~wxTimerRunner();
166};
e54c96f1 167
23324ae1
FM
168/**
169 @class wxTimerEvent
7c913512 170
3051a44a
FM
171 wxTimerEvent object is passed to the event handler of timer events
172 (see wxTimer::SetOwner).
7c913512 173
23324ae1 174 For example:
7c913512 175
23324ae1
FM
176 @code
177 class MyFrame : public wxFrame
178 {
179 public:
180 ...
181 void OnTimer(wxTimerEvent& event);
7c913512 182
23324ae1
FM
183 private:
184 wxTimer m_timer;
a0e9a5df 185 wxDECLARE_EVENT_TABLE();
23324ae1 186 };
7c913512 187
a0e9a5df 188 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
23324ae1 189 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
a0e9a5df 190 wxEND_EVENT_TABLE()
7c913512 191
23324ae1
FM
192 MyFrame::MyFrame()
193 : m_timer(this, TIMER_ID)
194 {
195 m_timer.Start(1000); // 1 second interval
196 }
7c913512 197
23324ae1
FM
198 void MyFrame::OnTimer(wxTimerEvent& event)
199 {
200 // do whatever you want to do every second here
201 }
202 @endcode
7c913512 203
23324ae1
FM
204 @library{wxbase}
205 @category{events}
7c913512 206
e54c96f1 207 @see wxTimer
23324ae1
FM
208*/
209class wxTimerEvent : public wxEvent
210{
211public:
884a3e9d
RD
212 wxTimerEvent();
213 wxTimerEvent(wxTimer& timer);
214
23324ae1
FM
215 /**
216 Returns the interval of the timer which generated this event.
217 */
328f5751 218 int GetInterval() const;
23324ae1
FM
219
220 /**
221 Returns the timer object which generated this event.
222 */
5267aefd 223 wxTimer& GetTimer() const;
23324ae1 224};
e54c96f1 225