]>
git.saurik.com Git - wxWidgets.git/blob - interface/timer.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxTimer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
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
17 There are three different ways to use this class:
19 You may derive a new class from wxTimer and override the
20 wxTimer::Notify member to perform the required action.
21 Or you may redirect the notifications to any
22 wxEvtHandler derived object by using the non-default
23 constructor or wxTimer::SetOwner. Then use the @c EVT_TIMER
24 macro to connect it to the event handler which will receive
25 wxTimerEvent notifications.
26 Or you may use a derived class and the @c EVT_TIMER
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.
31 In any case, you must start the timer with wxTimer::Start
32 after constructing it before it actually starts sending notifications. It can
33 be stopped later with wxTimer::Stop.
35 @b Note: A timer can only be used from the main thread.
42 class wxTimer
: public wxEvtHandler
47 Creates a timer and associates it with @e owner. Please see
48 SetOwner() for the description of parameters.
51 wxTimer(wxEvtHandler
* owner
, int id
= -1);
55 Destructor. Stops the timer if it is running.
60 Returns the ID of the events generated by this timer.
65 Returns the current interval for the timer (in milliseconds).
67 int GetInterval() const;
70 Returns the current @e owner of the timer.
71 If non-@NULL this is the event handler which will receive the
72 @ref overview_wxtimerevent "timer events" when the timer is running.
74 wxEvtHandler
GetOwner() const;
77 Returns @true if the timer is one shot, i.e. if it will stop after firing the
78 first notification automatically.
80 bool IsOneShot() const;
83 Returns @true if the timer is running, @false if it is stopped.
85 bool IsRunning() const;
88 This member should be overridden by the user if the default constructor was
89 used and SetOwner() wasn't called.
90 Perform whatever action which is to be taken periodically here.
95 Associates the timer with the given @a owner object. When the timer is
96 running, the owner will receive @ref overview_wxtimerevent "timer events" with
97 id equal to @a id specified here.
99 void SetOwner(wxEvtHandler
* owner
, int id
= -1);
102 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
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).
105 If @a oneShot is @false (the default), the Notify()
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:
112 Start a normal, continuously running, timer
116 Start a one shot timer
118 If the timer was already running, it will be stopped by this method before
121 bool Start(int milliseconds
= -1, bool oneShot
= false);
135 wxTimerEvent object is passed to the event handler of timer events.
140 class MyFrame : public wxFrame
144 void OnTimer(wxTimerEvent& event);
150 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
155 : m_timer(this, TIMER_ID)
157 m_timer.Start(1000); // 1 second interval
160 void MyFrame::OnTimer(wxTimerEvent& event)
162 // do whatever you want to do every second here
171 class wxTimerEvent
: public wxEvent
175 Returns the interval of the timer which generated this event.
177 int GetInterval() const;
180 Returns the timer object which generated this event.
182 wxTimer
GetTimer() const;