/////////////////////////////////////////////////////////////////////////////
// Name: timer.h
-// Purpose:
+// Purpose: wxTimer class
// Author: Vaclav Slavik
// Id: $Id$
// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
// wxTimer
//-----------------------------------------------------------------------------
-//FIXME_MGL
+class wxTimerDesc;
+class wxTimerScheduler;
+
class WXDLLEXPORT wxTimer : public wxTimerBase
{
public:
wxTimer() { Init(); }
wxTimer(wxEvtHandler *owner, int id = -1) : wxTimerBase(owner, id)
{ Init(); }
- ~wxTimer() {}
+ ~wxTimer();
- virtual bool Start( int millisecs = -1, bool oneShot = FALSE ) { return TRUE; }
- virtual void Stop() {}
+ virtual bool Start(int millisecs = -1, bool oneShot = FALSE);
+ virtual void Stop();
- virtual bool IsRunning() const { return m_tag != -1; }
+ virtual bool IsRunning() const;
-protected:
- void Init() {}
+ // implementation
+ static void NotifyTimers();
- int m_tag;
+protected:
+ void Init();
private:
+ wxTimerDesc *m_desc;
+
+ static wxTimerScheduler *ms_scheduler;
+ static size_t ms_timersCnt;
+
DECLARE_ABSTRACT_CLASS(wxTimer)
};
-#endif // __GTKTIMERH__
+#endif // __WX_TIMER_H__
#endif //WX_PRECOMP
#include "wx/evtloop.h"
-
+#include "wx/timer.h"
#include "wx/mgl/private.h"
+#include "pmapi.h"
// ----------------------------------------------------------------------------
// wxEventLoopImpl
MGL_wmUpdateDC(g_winMng);
- EVT_halt(&evt, EVT_EVERYEVT);
+ // VS: The code bellow is equal to MGL's EVT_halt implementation, with
+ // two things added: sleeping (busy waiting is stupid, lets make CPU's
+ // life a bit easier) and timers updating
+
+ // EVT_halt(&evt, EVT_EVERYEVT);
+ do
+ {
+ EVT_pollJoystick();
+ EVT_getNext(&evt, EVT_EVERYEVT);
+#if wxUSE_TIMER
+ wxTimer::NotifyTimers();
+#endif
+ PM_sleep(10);
+ } while (!(evt.what & EVT_EVERYEVT));
+ // end of EVT_halt
+
MGL_wmProcessEvent(g_winMng, &evt);
}
/////////////////////////////////////////////////////////////////////////////
-// Name: gtk/timer.cpp
+// Name: mgl/timer.cpp
// Purpose: wxTimer implementation
-// Author: Robert Roebling
+// Author: Vaclav Slavik
// Id: $Id$
-// Copyright: (c) 1998 Robert Roebling
+// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/timer.h"
+#if wxUSE_TIMER
+
+#include "wx/mgl/private.h"
+
+extern "C" ulong _EVT_getTicks();
+
+// ----------------------------------------------------------------------------
+// helper structures and wxTimerScheduler
+// ----------------------------------------------------------------------------
+
+class wxTimerDesc
+{
+public:
+ wxTimerDesc(wxTimer *t) : timer(t), running(FALSE), next(NULL), prev(NULL) {}
+
+ wxTimer *timer;
+ bool running;
+ wxTimerDesc *next, *prev;
+ unsigned long shotTime;
+};
+
+class wxTimerScheduler
+{
+public:
+ wxTimerScheduler() : m_timers(NULL) {}
+
+ void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
+ void RemoveTimer(wxTimerDesc *desc);
+ void NotifyTimers();
+
+private:
+ wxTimerDesc *m_timers;
+};
+
+void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
+{
+ if ( when == 0 )
+ when = _EVT_getTicks() + desc->timer->GetInterval();
+ desc->shotTime = when;
+ desc->running = TRUE;
+
+ if ( m_timers )
+ {
+ wxTimerDesc *d = m_timers;
+ while ( d->next && d->next->shotTime < when ) d = d->next;
+ desc->next = d->next;
+ desc->prev = d;
+ if ( d->next )
+ d->next->prev = desc;
+ d->next = desc;
+ }
+ else
+ {
+ m_timers = desc;
+ desc->prev = desc->next = NULL;
+ }
+}
+
+void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
+{
+ desc->running = FALSE;
+ if ( desc == m_timers )
+ m_timers = desc->next;
+ if ( desc->prev )
+ desc->prev->next = desc->next;
+ if ( desc->next )
+ desc->next->prev = desc->prev;
+ desc->prev = desc->next = NULL;
+}
+
+void wxTimerScheduler::NotifyTimers()
+{
+ if ( m_timers )
+ {
+ unsigned long now = _EVT_getTicks();
+ wxTimerDesc *desc;
+
+ while ( m_timers && m_timers->shotTime <= now )
+ {
+ desc = m_timers;
+ desc->timer->Notify();
+ RemoveTimer(desc);
+ if ( !desc->timer->IsOneShot() )
+ {
+ QueueTimer(desc, now + desc->timer->GetInterval());
+ }
+ }
+ }
+}
+
+
// ----------------------------------------------------------------------------
// wxTimer
IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
-// FIXME_MGL
+wxTimerScheduler *wxTimer::ms_scheduler = NULL;
+size_t wxTimer::ms_timersCnt = 0;
+
+void wxTimer::Init()
+{
+ if ( ms_timersCnt++ == 0 )
+ ms_scheduler = new wxTimerScheduler;
+ m_desc = new wxTimerDesc(this);
+}
+
+wxTimer::~wxTimer()
+{
+ if ( IsRunning() )
+ Stop();
+
+ if ( --ms_timersCnt == 0 )
+ {
+ delete ms_scheduler;
+ ms_scheduler = NULL;
+ }
+ delete m_desc;
+}
+
+bool wxTimer::IsRunning() const
+{
+ return m_desc->running;
+}
+
+bool wxTimer::Start(int millisecs, bool oneShot)
+{
+ if ( !wxTimerBase::Start(millisecs, oneShot) )
+ return FALSE;
+
+ ms_scheduler->QueueTimer(m_desc);
+ return TRUE;
+}
+
+void wxTimer::Stop()
+{
+ if ( !m_desc->running ) return;
+
+ ms_scheduler->RemoveTimer(m_desc);
+}
+
+/*static*/ void wxTimer::NotifyTimers()
+{
+ ms_scheduler->NotifyTimers();
+}
+
+#endif //wxUSE_TIMER