Update ooo320-m1
[ooovba.git] / svx / source / sdr / animation / scheduler.cxx
blob26416d5ff32fb5345a631bc3a3f2aab5ef3b08d5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: scheduler.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <svx/sdr/animation/scheduler.hxx>
35 #include <vector>
37 //////////////////////////////////////////////////////////////////////////////
38 // event class
40 namespace sdr
42 namespace animation
44 Event::Event(sal_uInt32 nTime)
45 : mnTime(nTime),
46 mpNext(0L)
50 Event::~Event()
54 Event* Event::GetNext() const
56 return mpNext;
59 void Event::SetNext(Event* pNew)
61 if(pNew != mpNext)
63 mpNext = pNew;
67 sal_uInt32 Event::GetTime() const
69 return mnTime;
72 void Event::SetTime(sal_uInt32 nNew)
74 if(mnTime != nNew)
76 mnTime = nNew;
79 } // end of namespace animation
80 } // end of namespace sdr
82 //////////////////////////////////////////////////////////////////////////////
83 // eventlist class
85 namespace sdr
87 namespace animation
89 EventList::EventList()
90 : mpHead(0L)
94 EventList::~EventList()
96 Clear();
99 void EventList::Insert(Event* pNew)
101 if(pNew)
103 Event* pCurrent = mpHead;
104 Event* pPrev = 0L;
106 while(pCurrent && pCurrent->GetTime() < pNew->GetTime())
108 pPrev = pCurrent;
109 pCurrent = pCurrent->GetNext();
112 if(pPrev)
114 pNew->SetNext(pPrev->GetNext());
115 pPrev->SetNext(pNew);
117 else
119 pNew->SetNext(mpHead);
120 mpHead = pNew;
125 void EventList::Remove(Event* pOld)
127 if(pOld && mpHead)
129 Event* pCurrent = mpHead;
130 Event* pPrev = 0L;
132 while(pCurrent && pCurrent != pOld)
134 pPrev = pCurrent;
135 pCurrent = pCurrent->GetNext();
138 if(pPrev)
140 pPrev->SetNext(pOld->GetNext());
142 else
144 mpHead = pOld->GetNext();
147 pOld->SetNext(0L);
151 void EventList::Clear()
153 while(mpHead)
155 Event* pNext = mpHead->GetNext();
156 mpHead->SetNext(0L);
157 mpHead = pNext;
161 Event* EventList::GetFirst()
163 return mpHead;
165 } // end of namespace animation
166 } // end of namespace sdr
168 //////////////////////////////////////////////////////////////////////////////
169 // scheduler class
171 namespace sdr
173 namespace animation
175 Scheduler::Scheduler()
176 : mnTime(0L),
177 mnDeltaTime(0L),
178 mbIsPaused(false)
182 Scheduler::~Scheduler()
184 Stop();
187 void Scheduler::Timeout()
189 // stop timer and add time
190 Stop();
191 mnTime += mnDeltaTime;
193 // execute events
194 triggerEvents();
196 // re-start or stop timer according to event list
197 checkTimeout();
200 void Scheduler::triggerEvents()
202 Event* pNextEvent = maList.GetFirst();
204 if(pNextEvent)
206 // copy events which need to be executed to a vector. Remove them from
207 // the scheduler
208 ::std::vector< Event* > EventPointerVector;
210 while(pNextEvent && pNextEvent->GetTime() <= mnTime)
212 maList.Remove(pNextEvent);
213 EventPointerVector.push_back(pNextEvent);
214 pNextEvent = maList.GetFirst();
217 // execute events from the vector
218 for(::std::vector< Event* >::iterator aCandidate = EventPointerVector.begin();
219 aCandidate != EventPointerVector.end(); aCandidate++)
221 // trigger event. This may re-insert the event to the scheduler again
222 (*aCandidate)->Trigger(mnTime);
227 void Scheduler::checkTimeout()
229 // re-start or stop timer according to event list
230 if(!IsPaused() && maList.GetFirst())
232 mnDeltaTime = maList.GetFirst()->GetTime() - mnTime;
234 if(0L != mnDeltaTime)
236 SetTimeout(mnDeltaTime);
237 Start();
240 else
242 Stop();
246 sal_uInt32 Scheduler::GetTime()
248 return mnTime;
251 // #i38135#
252 void Scheduler::SetTime(sal_uInt32 nTime)
254 // reset time
255 Stop();
256 mnTime = nTime;
258 // get event pointer
259 Event* pEvent = maList.GetFirst();
261 if(pEvent)
263 // retet event time points
264 while(pEvent)
266 pEvent->SetTime(nTime);
267 pEvent = pEvent->GetNext();
270 if(!IsPaused())
272 // without delta time, init events by triggering them. This will invalidate
273 // painted objects and add them to the scheduler again
274 mnDeltaTime = 0L;
275 triggerEvents();
276 checkTimeout();
281 void Scheduler::Reset(sal_uInt32 nTime)
283 mnTime = nTime;
284 mnDeltaTime = 0L;
285 maList.Clear();
288 void Scheduler::InsertEvent(Event* pNew)
290 if(pNew)
292 maList.Insert(pNew);
293 checkTimeout();
297 void Scheduler::RemoveEvent(Event* pOld)
299 if(pOld && maList.GetFirst())
301 maList.Remove(pOld);
302 checkTimeout();
306 void Scheduler::SetPaused(bool bNew)
308 if(bNew != mbIsPaused)
310 mbIsPaused = bNew;
311 checkTimeout();
314 } // end of namespace animation
315 } // end of namespace sdr
317 //////////////////////////////////////////////////////////////////////////////
318 // eof