2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: timer.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxTimer
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
@class wxTimer
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-10-04 07:55:28 -04:00
|
|
|
The wxTimer class allows you to execute code at specified intervals.
|
|
|
|
Its precision is platform-dependent, but in general will not be better than
|
|
|
|
@c 1ms nor worse than @c 1s.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
There are three different ways to use this class:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-10-04 07:55:28 -04:00
|
|
|
- You may derive a new class from wxTimer and override the
|
|
|
|
wxTimer::Notify member to perform the required action.
|
|
|
|
- You may redirect the notifications to any wxEvtHandler derived object by
|
|
|
|
using the non-default constructor or wxTimer::SetOwner.
|
|
|
|
Then use the @c EVT_TIMER macro to connect it to the event handler which
|
|
|
|
will receive wxTimerEvent notifications.
|
|
|
|
- You may use a derived class and the @c EVT_TIMER macro to connect it to
|
|
|
|
an event handler defined in the derived class. If the default constructor
|
|
|
|
is used, the timer object will be its own owner object, since it is
|
|
|
|
derived from wxEvtHandler.
|
|
|
|
|
|
|
|
In any case, you must start the timer with wxTimer::Start() after constructing
|
|
|
|
it before it actually starts sending notifications.
|
|
|
|
It can be stopped later with wxTimer::Stop().
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-01 09:59:28 -04:00
|
|
|
@note A timer can only be used from the main thread.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{misc}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxStopWatch
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
class wxTimer : public wxEvtHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-10-04 07:55:28 -04:00
|
|
|
Default constructor.
|
|
|
|
If you use it to construct the object and don't call SetOwner() later,
|
|
|
|
you must override Notify() method to process the notifications.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxTimer();
|
2008-10-04 07:55:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Creates a timer and associates it with @a owner.
|
|
|
|
Please see SetOwner() for the description of parameters.
|
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
wxTimer(wxEvtHandler* owner, int id = -1);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor. Stops the timer if it is running.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual ~wxTimer();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the ID of the events generated by this timer.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int GetId() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current interval for the timer (in milliseconds).
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int GetInterval() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current @e owner of the timer.
|
2008-10-04 07:55:28 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
If non-@NULL this is the event handler which will receive the
|
2008-10-04 07:55:28 -04:00
|
|
|
timer events (see wxTimerEvent) when the timer is running.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-29 11:34:31 -04:00
|
|
|
wxEvtHandler* GetOwner() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:55:28 -04:00
|
|
|
Returns @true if the timer is one shot, i.e. if it will stop after firing
|
|
|
|
the first notification automatically.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOneShot() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the timer is running, @false if it is stopped.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsRunning() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
This member should be overridden by the user if the default constructor was
|
|
|
|
used and SetOwner() wasn't called.
|
2008-10-04 07:55:28 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Perform whatever action which is to be taken periodically here.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual void Notify();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:55:28 -04:00
|
|
|
Associates the timer with the given @a owner object.
|
|
|
|
|
|
|
|
When the timer is running, the owner will receive timer events (see wxTimerEvent)
|
|
|
|
with @a id equal to @a id specified here.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 08:33:59 -04:00
|
|
|
void SetOwner(wxEvtHandler* owner, int id = -1);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
(Re)starts the timer. If @a milliseconds parameter is -1 (value by default),
|
2008-03-08 08:52:38 -05:00
|
|
|
the previous value is used. Returns @false if the timer could not be started,
|
|
|
|
@true otherwise (in MS Windows timers are a limited resource).
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-10-04 07:55:28 -04:00
|
|
|
If @a oneShot is @false (the default), the Notify() function will be called
|
|
|
|
repeatedly until the timer is stopped.
|
|
|
|
If @true, it will be called only once and the timer will stop automatically.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-10-04 07:55:28 -04:00
|
|
|
To make your code more readable you may also use the following symbolic constants:
|
|
|
|
- wxTIMER_CONTINUOUS: Start a normal, continuously running, timer
|
|
|
|
- wxTIMER_ONE_SHOT: Start a one shot timer
|
2008-03-08 08:52:38 -05:00
|
|
|
If the timer was already running, it will be stopped by this method before
|
|
|
|
restarting it.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual bool Start(int milliseconds = -1, bool oneShot = false);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Stops the timer.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual void Stop();
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxTimerEvent
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2009-02-18 12:58:51 -05:00
|
|
|
wxTimerEvent object is passed to the event handler of timer events
|
|
|
|
(see wxTimer::SetOwner).
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
For example:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
...
|
|
|
|
void OnTimer(wxTimerEvent& event);
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
private:
|
|
|
|
wxTimer m_timer;
|
|
|
|
};
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
|
|
|
|
END_EVENT_TABLE()
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
MyFrame::MyFrame()
|
|
|
|
: m_timer(this, TIMER_ID)
|
|
|
|
{
|
|
|
|
m_timer.Start(1000); // 1 second interval
|
|
|
|
}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
void MyFrame::OnTimer(wxTimerEvent& event)
|
|
|
|
{
|
|
|
|
// do whatever you want to do every second here
|
|
|
|
}
|
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{events}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxTimer
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
class wxTimerEvent : public wxEvent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Returns the interval of the timer which generated this event.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
int GetInterval() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the timer object which generated this event.
|
|
|
|
*/
|
2008-10-29 11:34:31 -04:00
|
|
|
wxTimer& GetTimer() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|