1999-11-12 19:19:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/timer.h
|
2000-02-05 01:57:38 +00:00
|
|
|
// Purpose: wxTimer, wxStopWatch and global time-related functions
|
2005-05-04 18:57:50 +00:00
|
|
|
// Author: Julian Smart
|
2000-02-05 01:57:38 +00:00
|
|
|
// Modified by: Vadim Zeitlin (wxTimerBase)
|
|
|
|
// Guillermo Rodriguez (global clean up)
|
1999-11-12 19:19:38 +00:00
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
2005-05-04 18:57:50 +00:00
|
|
|
// Copyright: (c) Julian Smart
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1999-11-12 19:19:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-15 00:23:28 +00:00
|
|
|
#ifndef _WX_TIMER_H_BASE_
|
|
|
|
#define _WX_TIMER_H_BASE_
|
1998-05-20 14:01:55 +00:00
|
|
|
|
2005-03-16 16:18:31 +00:00
|
|
|
#include "wx/defs.h"
|
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
#if wxUSE_TIMER
|
2005-03-16 16:18:31 +00:00
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
#include "wx/object.h"
|
1999-12-17 01:28:44 +00:00
|
|
|
#include "wx/longlong.h"
|
2000-02-05 01:57:38 +00:00
|
|
|
#include "wx/event.h"
|
2003-06-25 23:49:45 +00:00
|
|
|
#include "wx/stopwatch.h" // for backwards compatibility
|
2007-04-20 01:29:16 +00:00
|
|
|
#include "wx/utils.h"
|
2003-06-25 23:49:45 +00:00
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2002-06-04 14:59:52 +00:00
|
|
|
// more readable flags for Start():
|
|
|
|
|
|
|
|
// generate notifications periodically until the timer is stopped (default)
|
2004-09-23 18:20:56 +00:00
|
|
|
#define wxTIMER_CONTINUOUS false
|
2002-06-04 14:59:52 +00:00
|
|
|
|
|
|
|
// only send the notification once and then stop the timer
|
2004-09-23 18:20:56 +00:00
|
|
|
#define wxTIMER_ONE_SHOT true
|
2002-06-04 14:59:52 +00:00
|
|
|
|
2007-07-09 10:09:52 +00:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
|
2007-04-20 01:29:16 +00:00
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
// the interface of wxTimer class
|
2007-04-20 01:29:16 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
|
1999-11-12 19:19:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2000-02-05 01:57:38 +00:00
|
|
|
// ctors and initializers
|
|
|
|
// ----------------------
|
|
|
|
|
|
|
|
// default: if you don't call SetOwner(), your only chance to get timer
|
|
|
|
// notifications is to override Notify() in the derived class
|
2007-04-20 01:29:16 +00:00
|
|
|
wxTimer()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
SetOwner(this);
|
|
|
|
}
|
2000-02-05 01:57:38 +00:00
|
|
|
|
|
|
|
// ctor which allows to avoid having to override Notify() in the derived
|
|
|
|
// class: the owner will get timer notifications which can be handled with
|
|
|
|
// EVT_TIMER
|
2007-04-20 01:29:16 +00:00
|
|
|
wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
|
2007-01-18 14:15:54 +00:00
|
|
|
{
|
2007-04-20 01:29:16 +00:00
|
|
|
Init();
|
|
|
|
SetOwner(owner, timerid);
|
2007-01-18 14:15:54 +00:00
|
|
|
}
|
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
// same as ctor above
|
|
|
|
void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
|
|
|
|
|
|
|
|
virtual ~wxTimer();
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2001-02-15 21:51:14 +00:00
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
// working with the timer
|
|
|
|
// ----------------------
|
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
// NB: Start() and Stop() are not supposed to be overridden, they are only
|
|
|
|
// virtual for historical reasons, only Notify() can be overridden
|
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
// start the timer: if milliseconds == -1, use the same value as for the
|
|
|
|
// last Start()
|
2000-09-13 09:04:48 +00:00
|
|
|
//
|
|
|
|
// it is now valid to call Start() multiple times: this just restarts the
|
|
|
|
// timer if it is already running
|
2004-09-23 18:20:56 +00:00
|
|
|
virtual bool Start(int milliseconds = -1, bool oneShot = false);
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
// stop the timer, does nothing if the timer is not running
|
|
|
|
virtual void Stop();
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2000-02-05 01:57:38 +00:00
|
|
|
// override this in your wxTimer-derived class if you want to process timer
|
|
|
|
// messages in it, use non default ctor or SetOwner() otherwise
|
|
|
|
virtual void Notify();
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
|
|
|
|
// accessors
|
|
|
|
// ---------
|
|
|
|
|
|
|
|
// get the object notified about the timer events
|
|
|
|
wxEvtHandler *GetOwner() const;
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2004-09-23 18:20:56 +00:00
|
|
|
// return true if the timer is running
|
2007-04-20 01:29:16 +00:00
|
|
|
bool IsRunning() const;
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2006-03-14 22:46:49 +00:00
|
|
|
// return the timer ID
|
2007-04-20 01:29:16 +00:00
|
|
|
int GetId() const;
|
2006-03-14 22:46:49 +00:00
|
|
|
|
|
|
|
// get the (last) timer interval in milliseconds
|
2007-04-20 01:29:16 +00:00
|
|
|
int GetInterval() const;
|
1999-11-12 19:19:38 +00:00
|
|
|
|
2004-09-23 18:20:56 +00:00
|
|
|
// return true if the timer is one shot
|
2007-04-20 01:29:16 +00:00
|
|
|
bool IsOneShot() const;
|
1999-11-12 19:19:38 +00:00
|
|
|
|
|
|
|
protected:
|
2000-02-05 01:57:38 +00:00
|
|
|
// common part of all ctors
|
2007-04-20 01:29:16 +00:00
|
|
|
void Init();
|
2000-02-05 01:57:38 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
wxTimerImpl *m_impl;
|
2003-01-02 23:38:11 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
DECLARE_NO_COPY_CLASS(wxTimer)
|
1999-11-12 19:19:38 +00:00
|
|
|
};
|
|
|
|
|
2000-02-25 16:21:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimerRunner: starts the timer in its ctor, stops in the dtor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class WXDLLEXPORT wxTimerRunner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
|
2004-09-23 18:20:56 +00:00
|
|
|
wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
|
2000-02-25 16:21:53 +00:00
|
|
|
: m_timer(timer)
|
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
2004-09-23 18:20:56 +00:00
|
|
|
void Start(int milli, bool oneShot = false)
|
2000-02-25 16:21:53 +00:00
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxTimerRunner()
|
|
|
|
{
|
|
|
|
if ( m_timer.IsRunning() )
|
|
|
|
{
|
|
|
|
m_timer.Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxTimer& m_timer;
|
2003-07-22 00:24:07 +00:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(wxTimerRunner)
|
2000-02-25 16:21:53 +00:00
|
|
|
};
|
|
|
|
|
1999-11-12 19:19:38 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2000-02-05 01:57:38 +00:00
|
|
|
// wxTimerEvent
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2007-04-20 01:39:17 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
|
2000-02-05 01:57:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-05-05 14:59:25 +00:00
|
|
|
wxTimerEvent(wxTimer& timer)
|
|
|
|
: wxEvent(timer.GetId(), wxEVT_TIMER),
|
|
|
|
m_timer(timer)
|
2000-02-05 01:57:38 +00:00
|
|
|
{
|
2007-05-05 14:59:25 +00:00
|
|
|
SetEventObject(timer.GetOwner());
|
2000-02-05 01:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// accessors
|
2007-05-05 14:59:25 +00:00
|
|
|
int GetInterval() const { return m_timer.GetInterval(); }
|
|
|
|
wxTimer& GetTimer() const { return m_timer; }
|
2000-02-05 01:57:38 +00:00
|
|
|
|
2001-11-21 21:44:52 +00:00
|
|
|
// implement the base class pure virtual
|
|
|
|
virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
|
|
|
|
|
2000-02-05 01:57:38 +00:00
|
|
|
private:
|
2007-05-05 14:59:25 +00:00
|
|
|
wxTimer& m_timer;
|
2000-02-05 01:57:38 +00:00
|
|
|
|
2007-05-05 14:59:25 +00:00
|
|
|
DECLARE_ABSTRACT_CLASS(wxTimerEvent)
|
|
|
|
DECLARE_NO_ASSIGN_CLASS(wxTimerEvent)
|
2000-02-05 01:57:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
|
|
|
|
|
2005-02-14 23:53:48 +00:00
|
|
|
#define wxTimerEventHandler(func) \
|
2005-03-08 20:53:16 +00:00
|
|
|
(wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTimerEventFunction, &func)
|
2005-02-14 23:53:48 +00:00
|
|
|
|
2003-10-27 03:42:30 +00:00
|
|
|
#define EVT_TIMER(timerid, func) \
|
2005-02-14 23:53:48 +00:00
|
|
|
wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
|
2000-02-05 01:57:38 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
#endif // wxUSE_TIMER
|
2000-02-25 16:21:53 +00:00
|
|
|
|
2007-04-20 01:29:16 +00:00
|
|
|
#endif // _WX_TIMER_H_BASE_
|