]> git.saurik.com Git - wxWidgets.git/blame - src/generic/timer.cpp
*DO* use wxTR_EDIT_LABELS, it is neccessary
[wxWidgets.git] / src / generic / 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$
c41c20a5 6// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
32b8ec41
VZ
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
32b8ec41
VZ
10#ifdef __GNUG__
11#pragma implementation "timer.h"
12#endif
13
a246f95e
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
a0cb0ba5
VS
21// ----------------------------------------------------------------------------
22// NB: when using generic wxTimer implementation in your port, you *must* call
23// wxTimer::NotifyTimers() often enough. The ideal place for this
24// is in wxEventLoop::Dispatch().
25// ----------------------------------------------------------------------------
26
32b8ec41
VZ
27#include "wx/timer.h"
28
1acd70f9
VS
29#if wxUSE_TIMER
30
88f2a771 31#include "wx/log.h"
0e4b9976 32#include "wx/module.h"
1acd70f9 33
a0cb0ba5
VS
34// ----------------------------------------------------------------------------
35// Time input function
36// ----------------------------------------------------------------------------
37
38#ifdef __WXMGL__
39 // We take advantage of wxMGL's _EVT_getTicks because it is faster
40 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
41 // if we are unlucky and the latter combines information from two sources.
42 #include "wx/mgl/private.h"
43 extern "C" ulong _EVT_getTicks();
44 #define GetMillisecondsTime() _EVT_getTicks()
45#else
46 #define GetMillisecondsTime() wxGetLocalTimeMillis().ToLong()
47#endif
1acd70f9
VS
48
49// ----------------------------------------------------------------------------
50// helper structures and wxTimerScheduler
51// ----------------------------------------------------------------------------
52
53class wxTimerDesc
54{
55public:
0e4b9976
VS
56 wxTimerDesc(wxTimer *t) :
57 timer(t), running(FALSE), next(NULL), prev(NULL),
58 shotTime(0), deleteFlag(NULL) {}
1acd70f9
VS
59
60 wxTimer *timer;
61 bool running;
62 wxTimerDesc *next, *prev;
0e4b9976
VS
63 unsigned long shotTime;
64 volatile bool *deleteFlag; // see comment in ~wxTimer
1acd70f9
VS
65};
66
67class wxTimerScheduler
68{
69public:
70 wxTimerScheduler() : m_timers(NULL) {}
71
72 void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
73 void RemoveTimer(wxTimerDesc *desc);
74 void NotifyTimers();
75
76private:
77 wxTimerDesc *m_timers;
78};
79
80void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
81{
0e4b9976
VS
82 if ( desc->running )
83 return; // already scheduled
84
1acd70f9 85 if ( when == 0 )
a0cb0ba5 86 when = GetMillisecondsTime() + desc->timer->GetInterval();
1acd70f9
VS
87 desc->shotTime = when;
88 desc->running = TRUE;
89
a0cb0ba5 90 wxLogTrace("timer", "queued timer %p at tick %i",
88f2a771
VS
91 desc->timer, when);
92
1acd70f9
VS
93 if ( m_timers )
94 {
95 wxTimerDesc *d = m_timers;
96 while ( d->next && d->next->shotTime < when ) d = d->next;
97 desc->next = d->next;
98 desc->prev = d;
99 if ( d->next )
100 d->next->prev = desc;
101 d->next = desc;
102 }
103 else
104 {
105 m_timers = desc;
106 desc->prev = desc->next = NULL;
107 }
108}
109
110void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
111{
112 desc->running = FALSE;
113 if ( desc == m_timers )
114 m_timers = desc->next;
115 if ( desc->prev )
116 desc->prev->next = desc->next;
117 if ( desc->next )
118 desc->next->prev = desc->prev;
119 desc->prev = desc->next = NULL;
120}
121
122void wxTimerScheduler::NotifyTimers()
123{
124 if ( m_timers )
125 {
0e4b9976
VS
126 bool oneShot;
127 volatile bool timerDeleted;
a0cb0ba5 128 unsigned long now = GetMillisecondsTime();
1acd70f9 129 wxTimerDesc *desc;
88f2a771 130
1acd70f9
VS
131 while ( m_timers && m_timers->shotTime <= now )
132 {
133 desc = m_timers;
0e4b9976 134 oneShot = desc->timer->IsOneShot();
1acd70f9 135 RemoveTimer(desc);
88f2a771 136
0e4b9976
VS
137 timerDeleted = FALSE;
138 desc->deleteFlag = &timerDeleted;
88f2a771 139 desc->timer->Notify();
0e4b9976
VS
140
141 if ( !timerDeleted )
1acd70f9 142 {
a0cb0ba5 143 wxLogTrace("timer", "notified timer %p sheduled for %i",
0e4b9976
VS
144 desc->timer, desc->shotTime);
145
146 desc->deleteFlag = NULL;
147 if ( !oneShot )
148 QueueTimer(desc, now + desc->timer->GetInterval());
1acd70f9
VS
149 }
150 }
151 }
152}
153
154
32b8ec41
VZ
155// ----------------------------------------------------------------------------
156// wxTimer
157// ----------------------------------------------------------------------------
158
159IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
160
0e4b9976 161wxTimerScheduler *gs_scheduler = NULL;
1acd70f9
VS
162
163void wxTimer::Init()
164{
0e4b9976
VS
165 if ( !gs_scheduler )
166 gs_scheduler = new wxTimerScheduler;
1acd70f9
VS
167 m_desc = new wxTimerDesc(this);
168}
169
170wxTimer::~wxTimer()
171{
a0cb0ba5 172 wxLogTrace("timer", "destroying timer %p...", this);
1acd70f9
VS
173 if ( IsRunning() )
174 Stop();
175
0e4b9976
VS
176 // NB: this is a hack: wxTimerScheduler must have some way of knowing
177 // that wxTimer object was deleted under its hands -- this may
178 // happen if somebody is really nasty and deletes the timer
179 // from wxTimer::Notify()
180 if ( m_desc->deleteFlag != NULL )
181 *m_desc->deleteFlag = TRUE;
182
1acd70f9 183 delete m_desc;
a0cb0ba5 184 wxLogTrace("timer", " ...done destroying timer %p...", this);
1acd70f9
VS
185}
186
187bool wxTimer::IsRunning() const
188{
189 return m_desc->running;
190}
191
192bool wxTimer::Start(int millisecs, bool oneShot)
193{
a0cb0ba5 194 wxLogTrace("timer", "started timer %p: %i ms, oneshot=%i",
88f2a771
VS
195 this, millisecs, oneShot);
196
1acd70f9
VS
197 if ( !wxTimerBase::Start(millisecs, oneShot) )
198 return FALSE;
199
0e4b9976 200 gs_scheduler->QueueTimer(m_desc);
1acd70f9
VS
201 return TRUE;
202}
203
204void wxTimer::Stop()
205{
206 if ( !m_desc->running ) return;
207
0e4b9976 208 gs_scheduler->RemoveTimer(m_desc);
1acd70f9
VS
209}
210
211/*static*/ void wxTimer::NotifyTimers()
212{
0e4b9976
VS
213 if ( gs_scheduler )
214 gs_scheduler->NotifyTimers();
1acd70f9
VS
215}
216
0e4b9976
VS
217
218
219// A module to deallocate memory properly:
220class wxTimerModule: public wxModule
221{
222DECLARE_DYNAMIC_CLASS(wxTimerModule)
223public:
224 wxTimerModule() {}
225 bool OnInit() { return TRUE; }
226 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
227};
228
229IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
230
231
1acd70f9 232#endif //wxUSE_TIMER