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
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-25 00:30:49 -04:00
|
|
|
// generate notifications periodically until the timer is stopped (default)
|
|
|
|
#define wxTIMER_CONTINUOUS false
|
|
|
|
|
|
|
|
// only send the notification once and then stop the timer
|
|
|
|
#define wxTIMER_ONE_SHOT true
|
|
|
|
|
|
|
|
wxEventType wxEVT_TIMER;
|
|
|
|
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@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
|
|
|
|
|
|
|
/**
|
2012-11-30 19:14:07 -05:00
|
|
|
Returns @true if the timer is one shot, i.e.\ if it will stop after firing
|
2008-10-04 07:55:28 -04:00
|
|
|
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.
|
2009-11-22 06:24:03 -05:00
|
|
|
|
|
|
|
Notice that throwing exceptions from this method is currently not
|
|
|
|
supported, use event-based timer handling approach if an exception can
|
|
|
|
be thrown while handling timer notifications.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
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
|
2013-07-02 20:24:10 -04:00
|
|
|
Alternatively, use StartOnce().
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
If the timer was already running, it will be stopped by this method before
|
|
|
|
restarting it.
|
|
|
|
*/
|
2013-07-02 20:24:10 -04:00
|
|
|
virtual bool Start(int milliseconds = -1, bool oneShot = wxTIMER_CONTINUOUS);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Starts the timer for a once-only notification.
|
|
|
|
|
|
|
|
This is a simple wrapper for Start() with @c wxTIMER_ONE_SHOT parameter.
|
|
|
|
|
|
|
|
@since 2.9.5
|
|
|
|
*/
|
|
|
|
bool StartOnce(int milliseconds = -1);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-25 00:30:49 -04:00
|
|
|
/**
|
|
|
|
@class wxTimerRunner
|
|
|
|
|
|
|
|
Starts the timer in its ctor, stops in the dtor.
|
2019-01-30 11:28:08 -05:00
|
|
|
*/
|
2011-09-25 00:30:49 -04:00
|
|
|
class wxTimerRunner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTimerRunner(wxTimer& timer);
|
|
|
|
wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false);
|
|
|
|
void Start(int milli, bool oneShot = false);
|
|
|
|
~wxTimerRunner();
|
|
|
|
};
|
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;
|
2010-06-09 10:28:08 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2010-06-09 10:28:08 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2008-03-08 08:52:38 -05:00
|
|
|
EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
|
2010-06-09 10:28:08 -04:00
|
|
|
wxEND_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:
|
2011-11-03 23:38:09 -04:00
|
|
|
wxTimerEvent(wxTimer& timer);
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
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
|
|
|
|