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