]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/timer.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxTimer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 The wxTimer class allows you to execute code at specified intervals. Its
13 precision is platform-dependent, but in general will not be better than 1ms nor
16 There are three different ways to use this class:
18 You may derive a new class from wxTimer and override the
19 wxTimer::Notify member to perform the required action.
20 Or you may redirect the notifications to any
21 wxEvtHandler derived object by using the non-default
22 constructor or wxTimer::SetOwner. Then use the @c EVT_TIMER
23 macro to connect it to the event handler which will receive
24 wxTimerEvent notifications.
25 Or you may use a derived class and the @c EVT_TIMER
26 macro to connect it to an event handler defined in the derived class.
27 If the default constructor is used, the timer object will be its
28 own owner object, since it is derived from wxEvtHandler.
30 In any case, you must start the timer with wxTimer::Start
31 after constructing it before it actually starts sending notifications. It can
32 be stopped later with wxTimer::Stop.
34 @note A timer can only be used from the main thread.
41 class wxTimer
: public wxEvtHandler
46 Creates a timer and associates it with @e owner. Please see
47 SetOwner() for the description of parameters.
50 wxTimer(wxEvtHandler
* owner
, int id
= -1);
54 Destructor. Stops the timer if it is running.
59 Returns the ID of the events generated by this timer.
64 Returns the current interval for the timer (in milliseconds).
66 int GetInterval() const;
69 Returns the current @e owner of the timer.
70 If non-@NULL this is the event handler which will receive the
71 @ref overview_wxtimerevent "timer events" when the timer is running.
73 wxEvtHandler
GetOwner() const;
76 Returns @true if the timer is one shot, i.e. if it will stop after firing the
77 first notification automatically.
79 bool IsOneShot() const;
82 Returns @true if the timer is running, @false if it is stopped.
84 bool IsRunning() const;
87 This member should be overridden by the user if the default constructor was
88 used and SetOwner() wasn't called.
89 Perform whatever action which is to be taken periodically here.
94 Associates the timer with the given @a owner object. When the timer is
95 running, the owner will receive @ref overview_wxtimerevent "timer events" with
96 id equal to @a id specified here.
98 void SetOwner(wxEvtHandler
* owner
, int id
= -1);
101 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
102 the previous value is used. Returns @false if the timer could not be started,
103 @true otherwise (in MS Windows timers are a limited resource).
104 If @a oneShot is @false (the default), the Notify()
105 function will be called repeatedly until the timer is stopped. If @true,
106 it will be called only once and the timer will stop automatically. To make your
107 code more readable you may also use the following symbolic constants:
111 Start a normal, continuously running, timer
115 Start a one shot timer
117 If the timer was already running, it will be stopped by this method before
120 bool Start(int milliseconds
= -1, bool oneShot
= false);
133 wxTimerEvent object is passed to the event handler of timer events.
138 class MyFrame : public wxFrame
142 void OnTimer(wxTimerEvent& event);
148 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
149 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
153 : m_timer(this, TIMER_ID)
155 m_timer.Start(1000); // 1 second interval
158 void MyFrame::OnTimer(wxTimerEvent& event)
160 // do whatever you want to do every second here
169 class wxTimerEvent
: public wxEvent
173 Returns the interval of the timer which generated this event.
175 int GetInterval() const;
178 Returns the timer object which generated this event.
180 wxTimer
GetTimer() const;