]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/timer.h
6753e2dd12d4126f7d445de7482c7a493af5da03
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxTimer
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
8 // generate notifications periodically until the timer is stopped (default)
9 #define wxTIMER_CONTINUOUS false
11 // only send the notification once and then stop the timer
12 #define wxTIMER_ONE_SHOT true
14 wxEventType wxEVT_TIMER
;
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.
24 There are three different ways to use this class:
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.
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().
41 @note A timer can only be used from the main thread.
48 class wxTimer
: public wxEvtHandler
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.
59 Creates a timer and associates it with @a owner.
60 Please see SetOwner() for the description of parameters.
62 wxTimer(wxEvtHandler
* owner
, int id
= -1);
65 Destructor. Stops the timer if it is running.
70 Returns the ID of the events generated by this timer.
75 Returns the current interval for the timer (in milliseconds).
77 int GetInterval() const;
80 Returns the current @e owner of the timer.
82 If non-@NULL this is the event handler which will receive the
83 timer events (see wxTimerEvent) when the timer is running.
85 wxEvtHandler
* GetOwner() const;
88 Returns @true if the timer is one shot, i.e.\ if it will stop after firing
89 the first notification automatically.
91 bool IsOneShot() const;
94 Returns @true if the timer is running, @false if it is stopped.
96 bool IsRunning() const;
99 This member should be overridden by the user if the default constructor was
100 used and SetOwner() wasn't called.
102 Perform whatever action which is to be taken periodically here.
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.
108 virtual void Notify();
111 Associates the timer with the given @a owner object.
113 When the timer is running, the owner will receive timer events (see wxTimerEvent)
114 with @a id equal to @a id specified here.
116 void SetOwner(wxEvtHandler
* owner
, int id
= -1);
119 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
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).
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.
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
130 Alternatively, use StartOnce().
132 If the timer was already running, it will be stopped by this method before
135 virtual bool Start(int milliseconds
= -1, bool oneShot
= wxTIMER_CONTINUOUS
);
138 Starts the timer for a once-only notification.
140 This is a simple wrapper for Start() with @c wxTIMER_ONE_SHOT parameter.
144 bool StartOnce(int milliseconds
= -1);
156 Starts the timer in its ctor, stops in the dtor.
161 wxTimerRunner(wxTimer
& timer
);
162 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false);
163 void Start(int milli
, bool oneShot
= false);
170 wxTimerEvent object is passed to the event handler of timer events
171 (see wxTimer::SetOwner).
176 class MyFrame : public wxFrame
180 void OnTimer(wxTimerEvent& event);
184 wxDECLARE_EVENT_TABLE();
187 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
188 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
192 : m_timer(this, TIMER_ID)
194 m_timer.Start(1000); // 1 second interval
197 void MyFrame::OnTimer(wxTimerEvent& event)
199 // do whatever you want to do every second here
208 class wxTimerEvent
: public wxEvent
212 wxTimerEvent(wxTimer
& timer
);
215 Returns the interval of the timer which generated this event.
217 int GetInterval() const;
220 Returns the timer object which generated this event.
222 wxTimer
& GetTimer() const;