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"
|
|
|
|
|
|
|
|
#if wxUSE_GUI && wxUSE_TIMER
|
|
|
|
|
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
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimer
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// the interface of wxTimer class
|
2003-06-16 08:31:45 -04:00
|
|
|
class WXDLLEXPORT wxTimerBase : 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
|
2003-06-16 08:31:45 -04:00
|
|
|
wxTimerBase() { 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
|
2004-09-23 14:20:56 -04:00
|
|
|
wxTimerBase(wxEvtHandler *owner, int timerid = wxID_ANY)
|
2003-10-26 22:42:30 -05:00
|
|
|
{ Init(); SetOwner(owner, timerid); }
|
2000-02-04 20:57:38 -05:00
|
|
|
|
|
|
|
// same as ctor above
|
2004-09-23 14:20:56 -04:00
|
|
|
void SetOwner(wxEvtHandler *owner, int timerid = wxID_ANY)
|
2003-10-26 22:42:30 -05:00
|
|
|
{ m_owner = owner; m_idTimer = timerid; }
|
2004-05-10 16:53:25 -04:00
|
|
|
wxEvtHandler* GetOwner() const { return m_owner; }
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2002-01-07 16:52:28 -05:00
|
|
|
virtual ~wxTimerBase();
|
2001-02-15 16:51:14 -05:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// working with the timer
|
|
|
|
// ----------------------
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// stop the timer
|
|
|
|
virtual void Stop() = 0;
|
|
|
|
|
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
|
|
|
|
|
|
|
// getting info
|
|
|
|
// ------------
|
|
|
|
|
2004-09-23 14:20:56 -04:00
|
|
|
// return true if the timer is running
|
1999-11-12 14:19:38 -05:00
|
|
|
virtual bool IsRunning() const = 0;
|
|
|
|
|
|
|
|
// get the (last) timer interval in the milliseconds
|
|
|
|
int GetInterval() const { return m_milli; }
|
|
|
|
|
2004-09-23 14:20:56 -04:00
|
|
|
// return true if the timer is one shot
|
1999-11-12 14:19:38 -05:00
|
|
|
bool IsOneShot() const { return m_oneShot; }
|
|
|
|
|
2003-12-02 20:37:30 -05:00
|
|
|
// return the timer ID
|
|
|
|
int GetId() const { return m_idTimer; }
|
2004-09-23 14:20:56 -04:00
|
|
|
|
2003-12-02 20:37:30 -05:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
protected:
|
2000-02-04 20:57:38 -05:00
|
|
|
// common part of all ctors
|
2004-09-23 14:20:56 -04:00
|
|
|
void Init() { m_oneShot = false; m_milli = 0; }
|
2000-02-04 20:57:38 -05:00
|
|
|
|
|
|
|
wxEvtHandler *m_owner;
|
|
|
|
int m_idTimer;
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
int m_milli; // the timer interval
|
2004-09-23 14:20:56 -04:00
|
|
|
bool m_oneShot; // true if one shot
|
2003-01-02 18:38:11 -05:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(wxTimerBase)
|
1999-11-12 14:19:38 -05:00
|
|
|
};
|
|
|
|
|
2000-02-04 20:57:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimer itself
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1998-07-10 10:15:17 -04:00
|
|
|
#if defined(__WXMSW__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/msw/timer.h"
|
1998-07-10 10:15:17 -04:00
|
|
|
#elif defined(__WXMOTIF__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/motif/timer.h"
|
2006-01-22 22:27:34 -05:00
|
|
|
#elif defined(__WXGTK20__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/gtk/timer.h"
|
2006-01-22 22:27:34 -05:00
|
|
|
#elif defined(__WXGTK__)
|
|
|
|
#include "wx/gtk1/timer.h"
|
2005-02-04 12:21:28 -05:00
|
|
|
#elif defined(__WXX11__) || defined(__WXMGL__)
|
2002-02-17 09:57:33 -05:00
|
|
|
#include "wx/generic/timer.h"
|
2005-02-04 12:21:28 -05:00
|
|
|
#elif defined (__WXCOCOA__)
|
|
|
|
#include "wx/cocoa/timer.h"
|
1998-08-14 20:23:28 -04:00
|
|
|
#elif defined(__WXMAC__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/mac/timer.h"
|
1999-07-27 23:38:12 -04:00
|
|
|
#elif defined(__WXPM__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/os2/timer.h"
|
1998-05-20 10:01:55 -04:00
|
|
|
#endif
|
|
|
|
|
2000-02-25 11:21:53 -05: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 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
|
|
|
|
|
|
|
DECLARE_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
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class WXDLLEXPORT wxTimerEvent : public wxEvent
|
|
|
|
{
|
|
|
|
public:
|
2003-10-26 22:42:30 -05:00
|
|
|
wxTimerEvent(int timerid = 0, int interval = 0) : wxEvent(timerid)
|
2000-02-04 20:57:38 -05:00
|
|
|
{
|
|
|
|
m_eventType = wxEVT_TIMER;
|
|
|
|
|
|
|
|
m_interval = interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// accessors
|
|
|
|
int GetInterval() const { return m_interval; }
|
|
|
|
|
2001-11-21 16:44:52 -05:00
|
|
|
// implement the base class pure virtual
|
|
|
|
virtual wxEvent *Clone() const { return new wxTimerEvent(*this); }
|
|
|
|
|
2000-02-04 20:57:38 -05:00
|
|
|
private:
|
|
|
|
int m_interval;
|
|
|
|
|
2003-07-21 20:24:07 -04: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) \
|
2005-03-08 15:53:16 -05:00
|
|
|
(wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(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
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#endif // wxUSE_GUI && wxUSE_TIMER
|
2000-02-25 11:21:53 -05:00
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
#endif
|
1998-08-14 20:23:28 -04:00
|
|
|
// _WX_TIMER_H_BASE_
|