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
|
|
|
|
// Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch)
|
|
|
|
// 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$
|
2000-02-04 20:57:38 -05:00
|
|
|
// Copyright: (c) wxWindows team
|
2003-03-17 05:34:04 -05: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
|
|
|
|
2002-08-31 07:29:13 -04:00
|
|
|
#if defined(__GNUG__) && !defined(__APPLE__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#pragma interface "timerbase.h"
|
|
|
|
#endif
|
|
|
|
|
1999-12-16 20:28:44 -05:00
|
|
|
#include "wx/setup.h"
|
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"
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#if wxUSE_GUI && wxUSE_TIMER
|
2000-02-25 11:21:53 -05:00
|
|
|
|
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)
|
|
|
|
#define wxTIMER_CONTINUOUS FALSE
|
|
|
|
|
|
|
|
// only send the notification once and then stop the timer
|
|
|
|
#define wxTIMER_ONE_SHOT TRUE
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// the interface of wxTimer class
|
|
|
|
class WXDLLEXPORT wxTimerBase : public wxObject
|
|
|
|
{
|
|
|
|
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
|
|
|
|
wxTimerBase() { Init(); SetOwner(NULL); }
|
|
|
|
|
|
|
|
// 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
|
|
|
|
wxTimerBase(wxEvtHandler *owner, int id = -1)
|
2000-02-09 12:51:16 -05:00
|
|
|
{ Init(); SetOwner(owner, id); }
|
2000-02-04 20:57:38 -05:00
|
|
|
|
|
|
|
// same as ctor above
|
|
|
|
void SetOwner(wxEvtHandler *owner, int id = -1)
|
|
|
|
{ m_owner = owner; m_idTimer = id; }
|
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
|
|
|
|
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
|
|
|
|
// ------------
|
|
|
|
|
|
|
|
// return TRUE if the timer is running
|
|
|
|
virtual bool IsRunning() const = 0;
|
|
|
|
|
|
|
|
// get the (last) timer interval in the milliseconds
|
|
|
|
int GetInterval() const { return m_milli; }
|
|
|
|
|
|
|
|
// return TRUE if the timer is one shot
|
|
|
|
bool IsOneShot() const { return m_oneShot; }
|
|
|
|
|
|
|
|
#if WXWIN_COMPATIBILITY_2
|
|
|
|
// deprecated functions
|
|
|
|
int Interval() const { return GetInterval(); };
|
|
|
|
bool OneShot() const { return IsOneShot(); }
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2
|
|
|
|
|
|
|
|
protected:
|
2000-02-04 20:57:38 -05:00
|
|
|
// common part of all ctors
|
|
|
|
void Init() { m_oneShot = FALSE; m_milli = 0; }
|
|
|
|
|
|
|
|
wxEvtHandler *m_owner;
|
|
|
|
int m_idTimer;
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
int m_milli; // the timer interval
|
|
|
|
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"
|
1998-07-10 10:15:17 -04:00
|
|
|
#elif defined(__WXGTK__)
|
1999-11-12 14:19:38 -05:00
|
|
|
#include "wx/gtk/timer.h"
|
2003-03-22 01:07:53 -05:00
|
|
|
#elif defined(__WXX11__) || defined(__WXMGL__) || defined(__WXCOCOA__)
|
2002-02-17 09:57:33 -05:00
|
|
|
#include "wx/generic/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) { }
|
|
|
|
wxTimerRunner(wxTimer& timer, int milli, bool oneShot = FALSE)
|
|
|
|
: m_timer(timer)
|
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start(int milli, bool oneShot = FALSE)
|
|
|
|
{
|
|
|
|
m_timer.Start(milli, oneShot);
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxTimerRunner()
|
|
|
|
{
|
|
|
|
if ( m_timer.IsRunning() )
|
|
|
|
{
|
|
|
|
m_timer.Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxTimer& m_timer;
|
|
|
|
};
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2000-02-04 20:57:38 -05:00
|
|
|
// wxTimerEvent
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class WXDLLEXPORT wxTimerEvent : public wxEvent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTimerEvent(int id = 0, int interval = 0) : wxEvent(id)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
DECLARE_DYNAMIC_CLASS(wxTimerEvent)
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (wxEvtHandler::*wxTimerEventFunction)(wxTimerEvent&);
|
|
|
|
|
2001-01-26 12:35:36 -05:00
|
|
|
#define EVT_TIMER(id, func) \
|
2001-01-31 12:16:40 -05:00
|
|
|
DECLARE_EVENT_TABLE_ENTRY( wxEVT_TIMER, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTimerEventFunction) & func, NULL),
|
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
|
|
|
|
2000-02-04 20:57:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxStopWatch: measure time intervals with up to 1ms resolution
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#if wxUSE_STOPWATCH
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
class WXDLLEXPORT wxStopWatch
|
1999-09-29 15:40:04 -04:00
|
|
|
{
|
2000-02-04 20:57:38 -05:00
|
|
|
public:
|
1999-11-12 14:19:38 -05:00
|
|
|
// ctor starts the stop watch
|
2002-04-09 06:35:35 -04:00
|
|
|
wxStopWatch() { m_pauseCount = 0; Start(); }
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2002-04-09 06:35:35 -04:00
|
|
|
// start the stop watch at the moment t0
|
|
|
|
void Start(long t0 = 0);
|
|
|
|
|
|
|
|
// pause the stop watch
|
2003-02-12 19:22:36 -05:00
|
|
|
void Pause()
|
|
|
|
{
|
|
|
|
if ( !m_pauseCount++ )
|
|
|
|
m_pause = GetElapsedTime();
|
|
|
|
}
|
2002-04-09 06:35:35 -04:00
|
|
|
|
|
|
|
// resume it
|
2003-02-12 19:22:36 -05:00
|
|
|
void Resume()
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( m_pauseCount > 0,
|
|
|
|
_T("Resuming stop watch which is not paused") );
|
|
|
|
|
|
|
|
if ( !--m_pauseCount )
|
|
|
|
Start(m_pause);
|
|
|
|
}
|
2002-04-09 06:35:35 -04:00
|
|
|
|
|
|
|
// get elapsed time since the last Start() in milliseconds
|
1999-11-12 14:19:38 -05:00
|
|
|
long Time() const;
|
1999-09-29 15:40:04 -04:00
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
protected:
|
|
|
|
// returns the elapsed time since t0
|
1999-12-16 20:28:44 -05:00
|
|
|
long GetElapsedTime() const;
|
2000-02-04 20:57:38 -05:00
|
|
|
|
1999-09-29 15:40:04 -04:00
|
|
|
private:
|
2002-04-09 06:35:35 -04:00
|
|
|
// the time of the last Start()
|
|
|
|
wxLongLong m_t0;
|
|
|
|
|
|
|
|
// the time of the last Pause() (only valid if m_pauseCount > 0)
|
|
|
|
long m_pause;
|
|
|
|
|
|
|
|
// if > 0, the stop watch is paused, otherwise it is running
|
|
|
|
int m_pauseCount;
|
1999-09-29 15:40:04 -04:00
|
|
|
};
|
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#endif // wxUSE_STOPWATCH
|
|
|
|
|
|
|
|
#if wxUSE_LONGLONG
|
1999-11-12 14:19:38 -05:00
|
|
|
|
2000-02-04 20:57:38 -05:00
|
|
|
// Starts a global timer
|
1999-12-16 20:28:44 -05:00
|
|
|
// -- DEPRECATED: use wxStopWatch instead
|
1999-09-29 15:40:04 -04:00
|
|
|
void WXDLLEXPORT wxStartTimer();
|
|
|
|
|
1999-12-16 20:28:44 -05:00
|
|
|
// Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
|
|
|
|
// -- DEPRECATED: use wxStopWatch instead
|
1999-09-29 15:40:04 -04:00
|
|
|
long WXDLLEXPORT wxGetElapsedTime(bool resetTimer = TRUE);
|
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#endif // wxUSE_LONGLONG
|
|
|
|
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-16 20:28:44 -05:00
|
|
|
// global time functions
|
1999-11-12 14:19:38 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-12-16 20:28:44 -05:00
|
|
|
// Get number of seconds since local time 00:00:00 Jan 1st 1970.
|
2000-07-15 15:51:35 -04:00
|
|
|
extern long WXDLLEXPORT wxGetLocalTime();
|
1999-12-16 20:28:44 -05:00
|
|
|
|
|
|
|
// Get number of seconds since GMT 00:00:00, Jan 1st 1970.
|
2000-07-15 15:51:35 -04:00
|
|
|
extern long WXDLLEXPORT wxGetUTCTime();
|
|
|
|
|
2001-06-26 16:59:19 -04:00
|
|
|
#if wxUSE_LONGLONG
|
2000-07-15 15:51:35 -04:00
|
|
|
// Get number of milliseconds since local time 00:00:00 Jan 1st 1970
|
|
|
|
extern wxLongLong WXDLLEXPORT wxGetLocalTimeMillis();
|
2001-06-26 16:59:19 -04:00
|
|
|
#endif // wxUSE_LONGLONG
|
1999-12-16 20:28:44 -05:00
|
|
|
|
1999-12-17 11:35:48 -05:00
|
|
|
#define wxGetCurrentTime() wxGetLocalTime()
|
1999-11-12 14:19:38 -05:00
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
#endif
|
1998-08-14 20:23:28 -04:00
|
|
|
// _WX_TIMER_H_BASE_
|