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;
|
2009-01-12 14:26:13 +00:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxTimerEvent;
|
|
|
|
|
|
|
|
// timer event type
|
2009-02-07 18:59:25 +00:00
|
|
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_TIMER, wxTimerEvent);
|
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
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_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
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2007-12-12 01:35:53 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxTimerRunner
|
2000-02-25 16:21:53 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_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:
|
2008-12-27 13:40:49 +00:00
|
|
|
wxTimerEvent()
|
|
|
|
: wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; }
|
|
|
|
|
2007-05-05 14:59:25 +00:00
|
|
|
wxTimerEvent(wxTimer& timer)
|
|
|
|
: wxEvent(timer.GetId(), wxEVT_TIMER),
|
2008-12-27 13:40:49 +00:00
|
|
|
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
|
2008-12-27 13:40:49 +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); }
|
2009-02-04 17:42:28 +00:00
|
|
|
virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_TIMER; }
|
2001-11-21 21:44:52 +00:00
|
|
|
|
2000-02-05 01:57:38 +00:00
|
|
|
private:
|
2008-12-27 13:40:49 +00:00
|
|
|
wxTimer* m_timer;
|
2000-02-05 01:57:38 +00:00
|
|
|
|
2008-12-27 13:40:49 +00:00
|
|
|
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(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) \
|
2009-01-12 14:26:13 +00:00
|
|
|
wxEVENT_HANDLER_CAST(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_
|