]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "timer.h"
19 #include "wx/mgl/private.h"
21 extern "C" ulong
_EVT_getTicks();
23 // ----------------------------------------------------------------------------
24 // helper structures and wxTimerScheduler
25 // ----------------------------------------------------------------------------
30 wxTimerDesc(wxTimer
*t
) : timer(t
), running(FALSE
), next(NULL
), prev(NULL
) {}
34 wxTimerDesc
*next
, *prev
;
35 unsigned long shotTime
;
38 class wxTimerScheduler
41 wxTimerScheduler() : m_timers(NULL
) {}
43 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
44 void RemoveTimer(wxTimerDesc
*desc
);
48 wxTimerDesc
*m_timers
;
51 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
54 when
= _EVT_getTicks() + desc
->timer
->GetInterval();
55 desc
->shotTime
= when
;
60 wxTimerDesc
*d
= m_timers
;
61 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
71 desc
->prev
= desc
->next
= NULL
;
75 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
77 desc
->running
= FALSE
;
78 if ( desc
== m_timers
)
79 m_timers
= desc
->next
;
81 desc
->prev
->next
= desc
->next
;
83 desc
->next
->prev
= desc
->prev
;
84 desc
->prev
= desc
->next
= NULL
;
87 void wxTimerScheduler::NotifyTimers()
91 unsigned long now
= _EVT_getTicks();
94 while ( m_timers
&& m_timers
->shotTime
<= now
)
97 desc
->timer
->Notify();
99 if ( !desc
->timer
->IsOneShot() )
101 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
115 wxTimerScheduler
*wxTimer::ms_scheduler
= NULL
;
116 size_t wxTimer::ms_timersCnt
= 0;
120 if ( ms_timersCnt
++ == 0 )
121 ms_scheduler
= new wxTimerScheduler
;
122 m_desc
= new wxTimerDesc(this);
130 if ( --ms_timersCnt
== 0 )
138 bool wxTimer::IsRunning() const
140 return m_desc
->running
;
143 bool wxTimer::Start(int millisecs
, bool oneShot
)
145 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
148 ms_scheduler
->QueueTimer(m_desc
);
154 if ( !m_desc
->running
) return;
156 ms_scheduler
->RemoveTimer(m_desc
);
159 /*static*/ void wxTimer::NotifyTimers()
161 ms_scheduler
->NotifyTimers();