]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/timer.cpp
Tries to fix wxListBox focus problems.
[wxWidgets.git] / src / mgl / timer.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
1acd70f9 2// Name: mgl/timer.cpp
32b8ec41 3// Purpose: wxTimer implementation
1acd70f9 4// Author: Vaclav Slavik
32b8ec41 5// Id: $Id$
1acd70f9 6// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
32b8ec41
VZ
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "timer.h"
13#endif
14
15#include "wx/timer.h"
16
1acd70f9
VS
17#if wxUSE_TIMER
18
88f2a771 19#include "wx/log.h"
1acd70f9
VS
20#include "wx/mgl/private.h"
21
22extern "C" ulong _EVT_getTicks();
23
24// ----------------------------------------------------------------------------
25// helper structures and wxTimerScheduler
26// ----------------------------------------------------------------------------
27
28class wxTimerDesc
29{
30public:
31 wxTimerDesc(wxTimer *t) : timer(t), running(FALSE), next(NULL), prev(NULL) {}
32
33 wxTimer *timer;
34 bool running;
35 wxTimerDesc *next, *prev;
36 unsigned long shotTime;
37};
38
39class wxTimerScheduler
40{
41public:
42 wxTimerScheduler() : m_timers(NULL) {}
43
44 void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
45 void RemoveTimer(wxTimerDesc *desc);
46 void NotifyTimers();
47
48private:
49 wxTimerDesc *m_timers;
50};
51
52void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
53{
54 if ( when == 0 )
55 when = _EVT_getTicks() + desc->timer->GetInterval();
56 desc->shotTime = when;
57 desc->running = TRUE;
58
88f2a771
VS
59 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
60 desc->timer, when);
61
1acd70f9
VS
62 if ( m_timers )
63 {
64 wxTimerDesc *d = m_timers;
65 while ( d->next && d->next->shotTime < when ) d = d->next;
66 desc->next = d->next;
67 desc->prev = d;
68 if ( d->next )
69 d->next->prev = desc;
70 d->next = desc;
71 }
72 else
73 {
74 m_timers = desc;
75 desc->prev = desc->next = NULL;
76 }
77}
78
79void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
80{
81 desc->running = FALSE;
82 if ( desc == m_timers )
83 m_timers = desc->next;
84 if ( desc->prev )
85 desc->prev->next = desc->next;
86 if ( desc->next )
87 desc->next->prev = desc->prev;
88 desc->prev = desc->next = NULL;
89}
90
91void wxTimerScheduler::NotifyTimers()
92{
93 if ( m_timers )
94 {
95 unsigned long now = _EVT_getTicks();
96 wxTimerDesc *desc;
88f2a771
VS
97
98 wxLogTrace("mgl_timer", "notifying timers, time is %i", now);
1acd70f9
VS
99
100 while ( m_timers && m_timers->shotTime <= now )
101 {
102 desc = m_timers;
88f2a771 103 bool oneShot = desc->timer->IsOneShot();
1acd70f9 104 RemoveTimer(desc);
88f2a771
VS
105
106 desc->timer->Notify();
107 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
108 desc->timer, desc->shotTime);
109
110 if ( !oneShot )
1acd70f9
VS
111 {
112 QueueTimer(desc, now + desc->timer->GetInterval());
113 }
114 }
115 }
116}
117
118
32b8ec41
VZ
119
120// ----------------------------------------------------------------------------
121// wxTimer
122// ----------------------------------------------------------------------------
123
124IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
125
1acd70f9
VS
126wxTimerScheduler *wxTimer::ms_scheduler = NULL;
127size_t wxTimer::ms_timersCnt = 0;
128
129void wxTimer::Init()
130{
131 if ( ms_timersCnt++ == 0 )
132 ms_scheduler = new wxTimerScheduler;
133 m_desc = new wxTimerDesc(this);
88f2a771 134 wxLogTrace("mgl_timer", "--added timer (count=%i)", ms_timersCnt);
1acd70f9
VS
135}
136
137wxTimer::~wxTimer()
138{
139 if ( IsRunning() )
140 Stop();
141
142 if ( --ms_timersCnt == 0 )
143 {
144 delete ms_scheduler;
145 ms_scheduler = NULL;
146 }
147 delete m_desc;
88f2a771 148 wxLogTrace("mgl_timer", "--removed timer (count=%i)", ms_timersCnt);
1acd70f9
VS
149}
150
151bool wxTimer::IsRunning() const
152{
153 return m_desc->running;
154}
155
156bool wxTimer::Start(int millisecs, bool oneShot)
157{
88f2a771
VS
158 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
159 this, millisecs, oneShot);
160
1acd70f9
VS
161 if ( !wxTimerBase::Start(millisecs, oneShot) )
162 return FALSE;
163
164 ms_scheduler->QueueTimer(m_desc);
165 return TRUE;
166}
167
168void wxTimer::Stop()
169{
170 if ( !m_desc->running ) return;
171
172 ms_scheduler->RemoveTimer(m_desc);
173}
174
175/*static*/ void wxTimer::NotifyTimers()
176{
177 ms_scheduler->NotifyTimers();
178}
179
180#endif //wxUSE_TIMER