]> git.saurik.com Git - wxWidgets.git/commitdiff
wxTimer for wxMGL
authorVáclav Slavík <vslavik@fastmail.fm>
Sun, 11 Nov 2001 18:59:30 +0000 (18:59 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Sun, 11 Nov 2001 18:59:30 +0000 (18:59 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12377 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/mgl/timer.h
src/mgl/app.cpp
src/mgl/evtloop.cpp
src/mgl/timer.cpp

index b9b28416517a38d7a9c66a324990e4005efc9027..a97030f2be8b742706527a5be8f90e78c6943bd6 100644 (file)
@@ -1,6 +1,6 @@
 /////////////////////////////////////////////////////////////////////////////
 // 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__
index d4903a2af29190f2ca6e431984a6c5f6d2caac0a..77a30f02201f2d299e1db977e8c0ff7709fb2039 100644 (file)
@@ -27,6 +27,7 @@
     #include "wx/dialog.h"
     #include "wx/log.h"
     #include "wx/intl.h"
+    #include "wx/resource.h"
 #endif
 
 #include "wx/app.h"
index d19bd8639cfb6c8d60a9cc5025188b2e6bcb12b4..e4b4f2637366bcc863f49132e6050e33acda43df 100644 (file)
@@ -29,8 +29,9 @@
 #endif //WX_PRECOMP
 
 #include "wx/evtloop.h"
-
+#include "wx/timer.h"
 #include "wx/mgl/private.h"
+#include "pmapi.h"
 
 // ----------------------------------------------------------------------------
 // wxEventLoopImpl
@@ -77,7 +78,22 @@ void wxEventLoopImpl::Dispatch()
 
     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);
 }
 
index dbef60e5ddd016deeb036f260c0d976368420e5e..f56c8459c8dbd11677f75c8417e2bd8b70342d67 100644 (file)
@@ -1,9 +1,9 @@
 /////////////////////////////////////////////////////////////////////////////
-// 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