]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/timer.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxTimer
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // generate notifications periodically until the timer is stopped (default)
10 #define wxTIMER_CONTINUOUS false
12 // only send the notification once and then stop the timer
13 #define wxTIMER_ONE_SHOT true
15 wxEventType wxEVT_TIMER
;
21 The wxTimer class allows you to execute code at specified intervals.
22 Its precision is platform-dependent, but in general will not be better than
23 @c 1ms nor worse than @c 1s.
25 There are three different ways to use this class:
27 - You may derive a new class from wxTimer and override the
28 wxTimer::Notify member to perform the required action.
29 - You may redirect the notifications to any wxEvtHandler derived object by
30 using the non-default constructor or wxTimer::SetOwner.
31 Then use the @c EVT_TIMER macro to connect it to the event handler which
32 will receive wxTimerEvent notifications.
33 - You may use a derived class and the @c EVT_TIMER macro to connect it to
34 an event handler defined in the derived class. If the default constructor
35 is used, the timer object will be its own owner object, since it is
36 derived from wxEvtHandler.
38 In any case, you must start the timer with wxTimer::Start() after constructing
39 it before it actually starts sending notifications.
40 It can be stopped later with wxTimer::Stop().
42 @note A timer can only be used from the main thread.
49 class wxTimer
: public wxEvtHandler
54 If you use it to construct the object and don't call SetOwner() later,
55 you must override Notify() method to process the notifications.
60 Creates a timer and associates it with @a owner.
61 Please see SetOwner() for the description of parameters.
63 wxTimer(wxEvtHandler
* owner
, int id
= -1);
66 Destructor. Stops the timer if it is running.
71 Returns the ID of the events generated by this timer.
76 Returns the current interval for the timer (in milliseconds).
78 int GetInterval() const;
81 Returns the current @e owner of the timer.
83 If non-@NULL this is the event handler which will receive the
84 timer events (see wxTimerEvent) when the timer is running.
86 wxEvtHandler
* GetOwner() const;
89 Returns @true if the timer is one shot, i.e. if it will stop after firing
90 the first notification automatically.
92 bool IsOneShot() const;
95 Returns @true if the timer is running, @false if it is stopped.
97 bool IsRunning() const;
100 This member should be overridden by the user if the default constructor was
101 used and SetOwner() wasn't called.
103 Perform whatever action which is to be taken periodically here.
105 Notice that throwing exceptions from this method is currently not
106 supported, use event-based timer handling approach if an exception can
107 be thrown while handling timer notifications.
109 virtual void Notify();
112 Associates the timer with the given @a owner object.
114 When the timer is running, the owner will receive timer events (see wxTimerEvent)
115 with @a id equal to @a id specified here.
117 void SetOwner(wxEvtHandler
* owner
, int id
= -1);
120 (Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
121 the previous value is used. Returns @false if the timer could not be started,
122 @true otherwise (in MS Windows timers are a limited resource).
124 If @a oneShot is @false (the default), the Notify() function will be called
125 repeatedly until the timer is stopped.
126 If @true, it will be called only once and the timer will stop automatically.
128 To make your code more readable you may also use the following symbolic constants:
129 - wxTIMER_CONTINUOUS: Start a normal, continuously running, timer
130 - wxTIMER_ONE_SHOT: Start a one shot timer
131 If the timer was already running, it will be stopped by this method before
134 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
146 Starts the timer in its ctor, stops in the dtor.
151 wxTimerRunner(wxTimer
& timer
);
152 wxTimerRunner(wxTimer
& timer
, int milli
, bool oneShot
= false);
153 void Start(int milli
, bool oneShot
= false);
160 wxTimerEvent object is passed to the event handler of timer events
161 (see wxTimer::SetOwner).
166 class MyFrame : public wxFrame
170 void OnTimer(wxTimerEvent& event);
174 wxDECLARE_EVENT_TABLE();
177 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
178 EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
182 : m_timer(this, TIMER_ID)
184 m_timer.Start(1000); // 1 second interval
187 void MyFrame::OnTimer(wxTimerEvent& event)
189 // do whatever you want to do every second here
198 class wxTimerEvent
: public wxEvent
202 Returns the interval of the timer which generated this event.
204 int GetInterval() const;
207 Returns the timer object which generated this event.
209 wxTimer
& GetTimer() const;