]> git.saurik.com Git - wxWidgets.git/blame - interface/timer.h
Finished review/fixes of GDI category of functions and macros.
[wxWidgets.git] / interface / 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$
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
e54c96f1 40 @see wxStopWatch
23324ae1
FM
41*/
42class wxTimer : public wxEvtHandler
43{
44public:
45 //@{
46 /**
7c913512 47 Creates a timer and associates it with @e owner. Please see
23324ae1
FM
48 SetOwner() for the description of parameters.
49 */
50 wxTimer();
4cc4bfaf 51 wxTimer(wxEvtHandler* owner, int id = -1);
23324ae1
FM
52 //@}
53
54 /**
55 Destructor. Stops the timer if it is running.
56 */
57 ~wxTimer();
58
59 /**
60 Returns the ID of the events generated by this timer.
61 */
328f5751 62 int GetId() const;
23324ae1
FM
63
64 /**
65 Returns the current interval for the timer (in milliseconds).
66 */
328f5751 67 int GetInterval() const;
23324ae1
FM
68
69 /**
70 Returns the current @e owner of the timer.
7c913512 71 If non-@NULL this is the event handler which will receive the
23324ae1
FM
72 @ref overview_wxtimerevent "timer events" when the timer is running.
73 */
328f5751 74 wxEvtHandler GetOwner() const;
23324ae1
FM
75
76 /**
77 Returns @true if the timer is one shot, i.e. if it will stop after firing the
78 first notification automatically.
79 */
328f5751 80 bool IsOneShot() const;
23324ae1
FM
81
82 /**
83 Returns @true if the timer is running, @false if it is stopped.
84 */
328f5751 85 bool IsRunning() const;
23324ae1
FM
86
87 /**
88 This member should be overridden by the user if the default constructor was
89 used and SetOwner() wasn't called.
23324ae1
FM
90 Perform whatever action which is to be taken periodically here.
91 */
92 void Notify();
93
94 /**
4cc4bfaf 95 Associates the timer with the given @a owner object. When the timer is
23324ae1 96 running, the owner will receive @ref overview_wxtimerevent "timer events" with
4cc4bfaf 97 id equal to @a id specified here.
23324ae1 98 */
4cc4bfaf 99 void SetOwner(wxEvtHandler* owner, int id = -1);
23324ae1
FM
100
101 /**
4cc4bfaf 102 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
23324ae1
FM
103 the previous value is used. Returns @false if the timer could not be started,
104 @true otherwise (in MS Windows timers are a limited resource).
4cc4bfaf 105 If @a oneShot is @false (the default), the Notify()
23324ae1
FM
106 function will be called repeatedly until the timer is stopped. If @true,
107 it will be called only once and the timer will stop automatically. To make your
108 code more readable you may also use the following symbolic constants:
109
23324ae1
FM
110 wxTIMER_CONTINUOUS
111
23324ae1
FM
112 Start a normal, continuously running, timer
113
114 wxTIMER_ONE_SHOT
115
23324ae1
FM
116 Start a one shot timer
117
118 If the timer was already running, it will be stopped by this method before
119 restarting it.
120 */
4cc4bfaf 121 bool Start(int milliseconds = -1, bool oneShot = false);
23324ae1
FM
122
123 /**
124 Stops the timer.
125 */
126 void Stop();
127};
128
129
e54c96f1 130
23324ae1
FM
131/**
132 @class wxTimerEvent
133 @wxheader{timer.h}
7c913512 134
23324ae1 135 wxTimerEvent object is passed to the event handler of timer events.
7c913512 136
23324ae1 137 For example:
7c913512 138
23324ae1
FM
139 @code
140 class MyFrame : public wxFrame
141 {
142 public:
143 ...
144 void OnTimer(wxTimerEvent& event);
7c913512 145
23324ae1
FM
146 private:
147 wxTimer m_timer;
148 };
7c913512 149
23324ae1
FM
150 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
152 END_EVENT_TABLE()
7c913512 153
23324ae1
FM
154 MyFrame::MyFrame()
155 : m_timer(this, TIMER_ID)
156 {
157 m_timer.Start(1000); // 1 second interval
158 }
7c913512 159
23324ae1
FM
160 void MyFrame::OnTimer(wxTimerEvent& event)
161 {
162 // do whatever you want to do every second here
163 }
164 @endcode
7c913512 165
23324ae1
FM
166 @library{wxbase}
167 @category{events}
7c913512 168
e54c96f1 169 @see wxTimer
23324ae1
FM
170*/
171class wxTimerEvent : public wxEvent
172{
173public:
174 /**
175 Returns the interval of the timer which generated this event.
176 */
328f5751 177 int GetInterval() const;
23324ae1
FM
178
179 /**
180 Returns the timer object which generated this event.
181 */
328f5751 182 wxTimer GetTimer() const;
23324ae1 183};
e54c96f1 184