]> git.saurik.com Git - wxWidgets.git/blame - src/generic/timer.cpp
Make wxWindow::HasScrollbar() respect wxScrolled::ShowScrollbars().
[wxWidgets.git] / src / generic / timer.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/generic/timer.cpp
32b8ec41 3// Purpose: wxTimer implementation
1acd70f9 4// Author: Vaclav Slavik
6d89ddef 5// Copyright: (c) Vaclav Slavik
65571936 6// Licence: wxWindows licence
32b8ec41
VZ
7/////////////////////////////////////////////////////////////////////////////
8
a246f95e
VS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
13 #pragma hdrstop
14#endif
15
a0cb0ba5 16// ----------------------------------------------------------------------------
ca65c044
WS
17// NB: when using generic wxTimer implementation in your port, you *must* call
18// wxTimer::NotifyTimers() often enough. The ideal place for this
a0cb0ba5
VS
19// is in wxEventLoop::Dispatch().
20// ----------------------------------------------------------------------------
21
e4db172a
WS
22#if wxUSE_TIMER
23
e4db172a
WS
24#ifndef WX_PRECOMP
25 #include "wx/log.h"
02761f6c 26 #include "wx/module.h"
e4db172a 27#endif
1acd70f9 28
c2ca375c
VZ
29#include "wx/apptrait.h"
30#include "wx/generic/private/timer.h"
31
a0cb0ba5
VS
32// ----------------------------------------------------------------------------
33// Time input function
34// ----------------------------------------------------------------------------
35
0e1f8ea4 36#define GetMillisecondsTime wxGetLocalTimeMillis
9750481f 37
0e1f8ea4 38typedef wxLongLong wxTimerTick_t;
5ed08e5b 39
0e1f8ea4
VZ
40#if wxUSE_LONGLONG_WX
41 #define wxTimerTickFmtSpec wxLongLongFmtSpec "d"
42 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
43#else // using native wxLongLong
44 #define wxTimerTickFmtSpec wxT("s")
45 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
46#endif // wx/native long long
9814d926 47
0e1f8ea4
VZ
48inline bool wxTickGreaterEqual(wxTimerTick_t x, wxTimerTick_t y)
49{
50 return x >= y;
51}
1acd70f9
VS
52
53// ----------------------------------------------------------------------------
54// helper structures and wxTimerScheduler
55// ----------------------------------------------------------------------------
56
57class wxTimerDesc
58{
59public:
c2ca375c 60 wxTimerDesc(wxGenericTimerImpl *t) :
ca65c044 61 timer(t), running(false), next(NULL), prev(NULL),
0e4b9976 62 shotTime(0), deleteFlag(NULL) {}
1acd70f9 63
c2ca375c
VZ
64 wxGenericTimerImpl *timer;
65 bool running;
66 wxTimerDesc *next, *prev;
67 wxTimerTick_t shotTime;
68 volatile bool *deleteFlag; // see comment in ~wxTimer
1acd70f9
VS
69};
70
71class wxTimerScheduler
72{
73public:
74 wxTimerScheduler() : m_timers(NULL) {}
75
9750481f 76 void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0);
1acd70f9
VS
77 void RemoveTimer(wxTimerDesc *desc);
78 void NotifyTimers();
ca65c044 79
1acd70f9
VS
80private:
81 wxTimerDesc *m_timers;
82};
83
9750481f 84void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when)
1acd70f9 85{
0e4b9976
VS
86 if ( desc->running )
87 return; // already scheduled
ca65c044 88
1acd70f9 89 if ( when == 0 )
a0cb0ba5 90 when = GetMillisecondsTime() + desc->timer->GetInterval();
1acd70f9 91 desc->shotTime = when;
ca65c044 92 desc->running = true;
1acd70f9 93
ee9768c9 94 wxLogTrace( wxT("timer"),
ca65c044 95 wxT("queued timer %p at tick %") wxTimerTickFmtSpec,
5ed08e5b 96 desc->timer, wxTimerTickPrintfArg(when));
88f2a771 97
1acd70f9
VS
98 if ( m_timers )
99 {
100 wxTimerDesc *d = m_timers;
101 while ( d->next && d->next->shotTime < when ) d = d->next;
102 desc->next = d->next;
103 desc->prev = d;
104 if ( d->next )
105 d->next->prev = desc;
106 d->next = desc;
107 }
108 else
109 {
110 m_timers = desc;
111 desc->prev = desc->next = NULL;
112 }
113}
114
115void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
116{
ca65c044 117 desc->running = false;
1acd70f9
VS
118 if ( desc == m_timers )
119 m_timers = desc->next;
120 if ( desc->prev )
121 desc->prev->next = desc->next;
122 if ( desc->next )
123 desc->next->prev = desc->prev;
124 desc->prev = desc->next = NULL;
125}
126
127void wxTimerScheduler::NotifyTimers()
128{
129 if ( m_timers )
130 {
0e4b9976
VS
131 bool oneShot;
132 volatile bool timerDeleted;
9750481f 133 wxTimerTick_t now = GetMillisecondsTime();
88f2a771 134
c3732409 135 for ( wxTimerDesc *desc = m_timers; desc; desc = desc->next )
1acd70f9 136 {
9814d926 137 if ( desc->running && wxTickGreaterEqual(now, desc->shotTime) )
1acd70f9 138 {
c3732409
VZ
139 oneShot = desc->timer->IsOneShot();
140 RemoveTimer(desc);
141
142 timerDeleted = false;
143 desc->deleteFlag = &timerDeleted;
144 desc->timer->Notify();
145
146 if ( !timerDeleted )
147 {
148 wxLogTrace( wxT("timer"),
149 wxT("notified timer %p sheduled for %")
150 wxTimerTickFmtSpec,
151 desc->timer,
152 wxTimerTickPrintfArg(desc->shotTime) );
153
154 desc->deleteFlag = NULL;
155 if ( !oneShot )
156 QueueTimer(desc, now + desc->timer->GetInterval());
157 }
925fcf5f 158 else
dd7230ab 159 {
925fcf5f 160 desc = m_timers;
dd7230ab
JS
161 if (!desc)
162 break;
163 }
1acd70f9
VS
164 }
165 }
166 }
167}
168
169
32b8ec41
VZ
170// ----------------------------------------------------------------------------
171// wxTimer
172// ----------------------------------------------------------------------------
173
0e4b9976 174wxTimerScheduler *gs_scheduler = NULL;
1acd70f9 175
c2ca375c 176void wxGenericTimerImpl::Init()
1acd70f9 177{
0e4b9976
VS
178 if ( !gs_scheduler )
179 gs_scheduler = new wxTimerScheduler;
1acd70f9
VS
180 m_desc = new wxTimerDesc(this);
181}
182
c2ca375c 183wxGenericTimerImpl::~wxGenericTimerImpl()
1acd70f9 184{
2b5f62a0 185 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
1acd70f9
VS
186 if ( IsRunning() )
187 Stop();
188
0e4b9976 189 // NB: this is a hack: wxTimerScheduler must have some way of knowing
ca65c044 190 // that wxTimer object was deleted under its hands -- this may
0e4b9976
VS
191 // happen if somebody is really nasty and deletes the timer
192 // from wxTimer::Notify()
193 if ( m_desc->deleteFlag != NULL )
ca65c044 194 *m_desc->deleteFlag = true;
0e4b9976 195
1acd70f9 196 delete m_desc;
2b5f62a0 197 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
1acd70f9
VS
198}
199
c2ca375c 200bool wxGenericTimerImpl::IsRunning() const
1acd70f9
VS
201{
202 return m_desc->running;
203}
204
c2ca375c 205bool wxGenericTimerImpl::Start(int millisecs, bool oneShot)
1acd70f9 206{
ca65c044 207 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
88f2a771
VS
208 this, millisecs, oneShot);
209
c2ca375c
VZ
210 if ( !wxTimerImpl::Start(millisecs, oneShot) )
211 return false;
ca65c044 212
0e4b9976 213 gs_scheduler->QueueTimer(m_desc);
ca65c044 214 return true;
1acd70f9
VS
215}
216
c2ca375c 217void wxGenericTimerImpl::Stop()
1acd70f9
VS
218{
219 if ( !m_desc->running ) return;
ca65c044 220
0e4b9976 221 gs_scheduler->RemoveTimer(m_desc);
1acd70f9
VS
222}
223
c2ca375c 224/*static*/ void wxGenericTimerImpl::NotifyTimers()
1acd70f9 225{
0e4b9976
VS
226 if ( gs_scheduler )
227 gs_scheduler->NotifyTimers();
1acd70f9
VS
228}
229
0e4b9976
VS
230
231
232// A module to deallocate memory properly:
233class wxTimerModule: public wxModule
234{
235DECLARE_DYNAMIC_CLASS(wxTimerModule)
236public:
237 wxTimerModule() {}
ca65c044 238 bool OnInit() { return true; }
5276b0a5 239 void OnExit() { wxDELETE(gs_scheduler); }
0e4b9976
VS
240};
241
242IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
243
c2ca375c
VZ
244// ----------------------------------------------------------------------------
245// wxGUIAppTraits
246// ----------------------------------------------------------------------------
247
248wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
249{
250 return new wxGenericTimerImpl(timer);
251}
252
0e4b9976 253
1acd70f9 254#endif //wxUSE_TIMER