1999-11-12 14:19:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/timer.h
|
2000-02-04 20:57:38 -05:00
|
|
|
// Purpose: wxTimer, wxStopWatch and global time-related functions
|
2005-05-04 14:57:50 -04:00
|
|
|
// Author: Julian Smart
|
2000-02-04 20:57:38 -05:00
|
|
|
// Modified by: Vadim Zeitlin (wxTimerBase)
|
|
|
|
// Guillermo Rodriguez (global clean up)
|
1999-11-12 14:19:38 -05:00
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
2005-05-04 14:57:50 -04:00
|
|
|
// Copyright: (c) Julian Smart
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
1999-11-12 14:19:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-14 20:23:28 -04:00
|
|
|
#ifndef _WX_TIMER_H_BASE_
|
|
|
|
#define _WX_TIMER_H_BASE_
|
1998-05-20 10:01:55 -04:00
|
|
|
|
2005-03-16 11:18:31 -05:00
|
|
|
#include "wx/defs.h"
|
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
#if wxUSE_TIMER
|
2005-03-16 11:18:31 -05:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/object.h"
|
1999-12-16 20:28:44 -05:00
|
|
|
#include "wx/longlong.h"
|
2000-02-04 20:57:38 -05:00
|
|
|
#include "wx/event.h"
|
2003-06-25 19:49:45 -04:00
|
|
|
#include "wx/stopwatch.h" // for backwards compatibility
|
2007-04-19 21:29:16 -04:00
|
|
|
#include "wx/utils.h"
|
2003-06-25 19:49:45 -04:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2002-06-04 10:59:52 -04:00
|
|
|
// more readable flags for Start():
|
|
|
|
|
|
|
|
// generate notifications periodically until the timer is stopped (default)
|
2004-09-23 14:20:56 -04:00
|
|
|
#define wxTIMER_CONTINUOUS false
|
2002-06-04 10:59:52 -04:00
|
|
|
|
|
|
|
// only send the notification once and then stop the timer
|
2004-09-23 14:20:56 -04:00
|
|
|
#define wxTIMER_ONE_SHOT true
|
2002-06-04 10:59:52 -04:00
|
|
|
|
2007-07-09 06:09:52 -04:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxTimerImpl;
|
2009-01-12 09:26:13 -05:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
|
|
|
|
|
|
|
|
// timer event type
|
2009-02-07 13:59:25 -05:00
|
|
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
|
2007-04-19 21:29:16 -04:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// the interface of wxTimer class
|
2007-04-19 21:29:16 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxTimer : public wxEvtHandler
|
1999-11-12 14:19:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2000-02-04 20:57:38 -05: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-19 21:29:16 -04:00
|
|
|
wxTimer()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
SetOwner(this);
|
|
|
|
}
|
2000-02-04 20:57:38 -05: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-19 21:29:16 -04:00
|
|
|
wxTimer(wxEvtHandler *owner, int timerid = wxID_ANY)
|
2007-01-18 09:15:54 -05:00
|
|
|
{
|
2007-04-19 21:29:16 -04:00
|
|
|
Init();
|
|
|
|
SetOwner(owner, timerid);
|
2007-01-18 09:15:54 -05:00
|
|
|
}
|
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
// same as ctor above
|
|
|
|
void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY);
|
|
|
|
|
|
|
|
virtual ~wxTimer();
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2001-02-15 16:51:14 -05:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// working with the timer
|
|
|
|
// ----------------------
|
|
|
|
|
2007-04-19 21:29:16 -04: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 14:19:38 -05:00
|
|
|
// start the timer: if milliseconds == -1, use the same value as for the
|
|
|
|
// last Start()
|
2000-09-13 05:04:48 -04:00
|
|
|
//
|
|
|
|
// it is now valid to call Start() multiple times: this just restarts the
|
|
|
|
// timer if it is already running
|
2004-09-23 14:20:56 -04:00
|
|
|
virtual bool Start(int milliseconds = -1, bool oneShot = false);
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2013-07-02 20:24:10 -04:00
|
|
|
// start the timer for one iteration only, this is just a simple wrapper
|
|
|
|
// for Start()
|
|
|
|
bool StartOnce(int milliseconds = -1) { return Start(milliseconds, true); }
|
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
// stop the timer, does nothing if the timer is not running
|
|
|
|
virtual void Stop();
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2000-02-04 20:57:38 -05: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 14:19:38 -05:00
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
|
|
|
|
// accessors
|
|
|
|
// ---------
|
|
|
|
|
|
|
|
// get the object notified about the timer events
|
|
|
|
wxEvtHandler *GetOwner() const;
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2004-09-23 14:20:56 -04:00
|
|
|
// return true if the timer is running
|
2007-04-19 21:29:16 -04:00
|
|
|
bool IsRunning() const;
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2006-03-14 17:46:49 -05:00
|
|
|
// return the timer ID
|
2007-04-19 21:29:16 -04:00
|
|
|
int GetId() const;
|
2006-03-14 17:46:49 -05:00
|
|
|
|
|
|
|
// get the (last) timer interval in milliseconds
|
2007-04-19 21:29:16 -04:00
|
|
|
int GetInterval() const;
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2004-09-23 14:20:56 -04:00
|
|
|
// return true if the timer is one shot
|
2007-04-19 21:29:16 -04:00
|
|
|
bool IsOneShot() const;
|
1999-11-12 14:19:38 -05:00
|
|
|
|
|
|
|
protected:
|
2000-02-04 20:57:38 -05:00
|
|
|
// common part of all ctors
|
2007-04-19 21:29:16 -04:00
|
|
|
void Init();
|
2000-02-04 20:57:38 -05:00
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
wxTimerImpl *m_impl;
|
2003-01-02 18:38:11 -05:00
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxTimer);
|
1999-11-12 14:19:38 -05:00
|
|
|
};
|
|
|
|
|
2000-02-25 11:21:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimerRunner: starts the timer in its ctor, stops in the dtor
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2007-12-11 20:35:53 -05:00
|
|
|
class WXDLLIMPEXP_BASE wxTimerRunner
|
2000-02-25 11:21:53 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTimerRunner(wxTimer& timer) : m_timer(timer) { }
|
2004-09-23 14:20:56 -04:00
|
|
|
wxTimerRunner(wxTimer& timer, int milli, bool oneShot = false)
|
2000-02-25 11:21:53 -05:00
|
|
|
: m_timer(timer)
|
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
2004-09-23 14:20:56 -04:00
|
|
|
void Start(int milli, bool oneShot = false)
|
2000-02-25 11:21:53 -05:00
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxTimerRunner()
|
|
|
|
{
|
|
|
|
if ( m_timer.IsRunning() )
|
|
|
|
{
|
|
|
|
m_timer.Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxTimer& m_timer;
|
2003-07-21 20:24:07 -04:00
|
|
|
|
2009-02-08 06:45:59 -05:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxTimerRunner);
|
2000-02-25 11:21:53 -05:00
|
|
|
};
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2000-02-04 20:57:38 -05:00
|
|
|
// wxTimerEvent
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2007-04-19 21:39:17 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent
|
2000-02-04 20:57:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-12-27 08:40:49 -05:00
|
|
|
wxTimerEvent()
|
|
|
|
: wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
|
|
|
|
|
2007-05-05 10:59:25 -04:00
|
|
|
wxTimerEvent(wxTimer& timer)
|
|
|
|
: wxEvent(timer.GetId(), wxEVT_TIMER),
|
2008-12-27 08:40:49 -05:00
|
|
|
m_timer(&timer)
|
2000-02-04 20:57:38 -05:00
|
|
|
{
|
2007-05-05 10:59:25 -04:00
|
|
|
SetEventObject(timer.GetOwner());
|
2000-02-04 20:57:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// accessors
|
2008-12-27 08:40:49 -05:00
|
|
|
int GetInterval() const { return m_timer->GetInterval(); }
|
|
|
|
wxTimer& GetTimer() const { return *m_timer; }
|
2000-02-04 20:57:38 -05:00
|
|
|
|
2001-11-21 16:44:52 -05:00
|
|
|
// implement the base class pure virtual
|
|
|
|
virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
|
2009-02-04 12:42:28 -05:00
|
|
|
virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
|
2001-11-21 16:44:52 -05:00
|
|
|
|
2000-02-04 20:57:38 -05:00
|
|
|
private:
|
2008-12-27 08:40:49 -05:00
|
|
|
wxTimer* m_timer;
|
2000-02-04 20:57:38 -05:00
|
|
|
|
2008-12-27 08:40:49 -05:00
|
|
|
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent)
|
2000-02-04 20:57:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
|
|
|
|
|
2005-02-14 18:53:48 -05:00
|
|
|
#define wxTimerEventHandler(func) \
|
2009-01-12 09:26:13 -05:00
|
|
|
wxEVENT_HANDLER_CAST(wxTimerEventFunction, func)
|
2005-02-14 18:53:48 -05:00
|
|
|
|
2003-10-26 22:42:30 -05:00
|
|
|
#define EVT_TIMER(timerid, func) \
|
2005-02-14 18:53:48 -05:00
|
|
|
wx__DECLARE_EVT1(wxEVT_TIMER, timerid, wxTimerEventHandler(func))
|
2000-02-04 20:57:38 -05:00
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
#endif // wxUSE_TIMER
|
2000-02-25 11:21:53 -05:00
|
|
|
|
2007-04-19 21:29:16 -04:00
|
|
|
#endif // _WX_TIMER_H_BASE_
|