1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir #include <svx/sdr/animation/scheduler.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <vector>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
35*cdf0e10cSrcweir // event class
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace sdr
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 	namespace animation
40*cdf0e10cSrcweir 	{
41*cdf0e10cSrcweir 		Event::Event(sal_uInt32 nTime)
42*cdf0e10cSrcweir 		:	mnTime(nTime),
43*cdf0e10cSrcweir 			mpNext(0L)
44*cdf0e10cSrcweir 		{
45*cdf0e10cSrcweir 		}
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 		Event::~Event()
48*cdf0e10cSrcweir 		{
49*cdf0e10cSrcweir 		}
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 		Event* Event::GetNext() const
52*cdf0e10cSrcweir 		{
53*cdf0e10cSrcweir 			return mpNext;
54*cdf0e10cSrcweir 		}
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 		void Event::SetNext(Event* pNew)
57*cdf0e10cSrcweir 		{
58*cdf0e10cSrcweir 			if(pNew != mpNext)
59*cdf0e10cSrcweir 			{
60*cdf0e10cSrcweir 				mpNext = pNew;
61*cdf0e10cSrcweir 			}
62*cdf0e10cSrcweir 		}
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 		sal_uInt32 Event::GetTime() const
65*cdf0e10cSrcweir 		{
66*cdf0e10cSrcweir 			return mnTime;
67*cdf0e10cSrcweir 		}
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 		void Event::SetTime(sal_uInt32 nNew)
70*cdf0e10cSrcweir 		{
71*cdf0e10cSrcweir 			if(mnTime != nNew)
72*cdf0e10cSrcweir 			{
73*cdf0e10cSrcweir 				mnTime = nNew;
74*cdf0e10cSrcweir 			}
75*cdf0e10cSrcweir 		}
76*cdf0e10cSrcweir 	} // end of namespace animation
77*cdf0e10cSrcweir } // end of namespace sdr
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
80*cdf0e10cSrcweir // eventlist class
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir namespace sdr
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	namespace animation
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		EventList::EventList()
87*cdf0e10cSrcweir 		:	mpHead(0L)
88*cdf0e10cSrcweir 		{
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		EventList::~EventList()
92*cdf0e10cSrcweir 		{
93*cdf0e10cSrcweir 			Clear();
94*cdf0e10cSrcweir 		}
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 		void EventList::Insert(Event* pNew)
97*cdf0e10cSrcweir 		{
98*cdf0e10cSrcweir 			if(pNew)
99*cdf0e10cSrcweir 			{
100*cdf0e10cSrcweir 				Event* pCurrent = mpHead;
101*cdf0e10cSrcweir 				Event* pPrev = 0L;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 				while(pCurrent && pCurrent->GetTime() < pNew->GetTime())
104*cdf0e10cSrcweir 				{
105*cdf0e10cSrcweir 					pPrev = pCurrent;
106*cdf0e10cSrcweir 					pCurrent = pCurrent->GetNext();
107*cdf0e10cSrcweir 				}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 				if(pPrev)
110*cdf0e10cSrcweir 				{
111*cdf0e10cSrcweir 					pNew->SetNext(pPrev->GetNext());
112*cdf0e10cSrcweir 					pPrev->SetNext(pNew);
113*cdf0e10cSrcweir 				}
114*cdf0e10cSrcweir 				else
115*cdf0e10cSrcweir 				{
116*cdf0e10cSrcweir 					pNew->SetNext(mpHead);
117*cdf0e10cSrcweir 					mpHead = pNew;
118*cdf0e10cSrcweir 				}
119*cdf0e10cSrcweir 			}
120*cdf0e10cSrcweir 		}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 		void EventList::Remove(Event* pOld)
123*cdf0e10cSrcweir 		{
124*cdf0e10cSrcweir 			if(pOld && mpHead)
125*cdf0e10cSrcweir 			{
126*cdf0e10cSrcweir 				Event* pCurrent = mpHead;
127*cdf0e10cSrcweir 				Event* pPrev = 0L;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 				while(pCurrent && pCurrent != pOld)
130*cdf0e10cSrcweir 				{
131*cdf0e10cSrcweir 					pPrev = pCurrent;
132*cdf0e10cSrcweir 					pCurrent = pCurrent->GetNext();
133*cdf0e10cSrcweir 				}
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 				if(pPrev)
136*cdf0e10cSrcweir 				{
137*cdf0e10cSrcweir 					pPrev->SetNext(pOld->GetNext());
138*cdf0e10cSrcweir 				}
139*cdf0e10cSrcweir 				else
140*cdf0e10cSrcweir 				{
141*cdf0e10cSrcweir 					mpHead = pOld->GetNext();
142*cdf0e10cSrcweir 				}
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 				pOld->SetNext(0L);
145*cdf0e10cSrcweir 			}
146*cdf0e10cSrcweir 		}
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 		void EventList::Clear()
149*cdf0e10cSrcweir 		{
150*cdf0e10cSrcweir 			while(mpHead)
151*cdf0e10cSrcweir 			{
152*cdf0e10cSrcweir 				Event* pNext = mpHead->GetNext();
153*cdf0e10cSrcweir 				mpHead->SetNext(0L);
154*cdf0e10cSrcweir 				mpHead = pNext;
155*cdf0e10cSrcweir 			}
156*cdf0e10cSrcweir 		}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		Event* EventList::GetFirst()
159*cdf0e10cSrcweir 		{
160*cdf0e10cSrcweir 			return mpHead;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 	} // end of namespace animation
163*cdf0e10cSrcweir } // end of namespace sdr
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
166*cdf0e10cSrcweir // scheduler class
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir namespace sdr
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir 	namespace animation
171*cdf0e10cSrcweir 	{
172*cdf0e10cSrcweir 		Scheduler::Scheduler()
173*cdf0e10cSrcweir 		:	mnTime(0L),
174*cdf0e10cSrcweir 			mnDeltaTime(0L),
175*cdf0e10cSrcweir 			mbIsPaused(false)
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 		}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 		Scheduler::~Scheduler()
180*cdf0e10cSrcweir 		{
181*cdf0e10cSrcweir 			Stop();
182*cdf0e10cSrcweir 		}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		void Scheduler::Timeout()
185*cdf0e10cSrcweir 		{
186*cdf0e10cSrcweir 			// stop timer and add time
187*cdf0e10cSrcweir 			Stop();
188*cdf0e10cSrcweir 			mnTime += mnDeltaTime;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 			// execute events
191*cdf0e10cSrcweir 			triggerEvents();
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			// re-start or stop timer according to event list
194*cdf0e10cSrcweir 			checkTimeout();
195*cdf0e10cSrcweir 		}
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 		void Scheduler::triggerEvents()
198*cdf0e10cSrcweir 		{
199*cdf0e10cSrcweir 			Event* pNextEvent = maList.GetFirst();
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			if(pNextEvent)
202*cdf0e10cSrcweir 			{
203*cdf0e10cSrcweir 				// copy events which need to be executed to a vector. Remove them from
204*cdf0e10cSrcweir 				// the scheduler
205*cdf0e10cSrcweir 				::std::vector< Event* > EventPointerVector;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 				while(pNextEvent && pNextEvent->GetTime() <= mnTime)
208*cdf0e10cSrcweir 				{
209*cdf0e10cSrcweir 					maList.Remove(pNextEvent);
210*cdf0e10cSrcweir 					EventPointerVector.push_back(pNextEvent);
211*cdf0e10cSrcweir 					pNextEvent = maList.GetFirst();
212*cdf0e10cSrcweir 				}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 				// execute events from the vector
215*cdf0e10cSrcweir 				for(::std::vector< Event* >::iterator aCandidate = EventPointerVector.begin();
216*cdf0e10cSrcweir 					aCandidate != EventPointerVector.end(); aCandidate++)
217*cdf0e10cSrcweir 				{
218*cdf0e10cSrcweir 					// trigger event. This may re-insert the event to the scheduler again
219*cdf0e10cSrcweir 					(*aCandidate)->Trigger(mnTime);
220*cdf0e10cSrcweir 				}
221*cdf0e10cSrcweir 			}
222*cdf0e10cSrcweir 		}
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 		void Scheduler::checkTimeout()
225*cdf0e10cSrcweir 		{
226*cdf0e10cSrcweir 			// re-start or stop timer according to event list
227*cdf0e10cSrcweir 			if(!IsPaused() && maList.GetFirst())
228*cdf0e10cSrcweir 			{
229*cdf0e10cSrcweir 				mnDeltaTime = maList.GetFirst()->GetTime() - mnTime;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 				if(0L != mnDeltaTime)
232*cdf0e10cSrcweir 				{
233*cdf0e10cSrcweir 					SetTimeout(mnDeltaTime);
234*cdf0e10cSrcweir 					Start();
235*cdf0e10cSrcweir 				}
236*cdf0e10cSrcweir 			}
237*cdf0e10cSrcweir 			else
238*cdf0e10cSrcweir 			{
239*cdf0e10cSrcweir 				Stop();
240*cdf0e10cSrcweir 			}
241*cdf0e10cSrcweir 		}
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 		sal_uInt32 Scheduler::GetTime()
244*cdf0e10cSrcweir 		{
245*cdf0e10cSrcweir 			return mnTime;
246*cdf0e10cSrcweir 		}
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 		// #i38135#
249*cdf0e10cSrcweir 		void Scheduler::SetTime(sal_uInt32 nTime)
250*cdf0e10cSrcweir 		{
251*cdf0e10cSrcweir 			// reset time
252*cdf0e10cSrcweir 			Stop();
253*cdf0e10cSrcweir 			mnTime = nTime;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 			// get event pointer
256*cdf0e10cSrcweir 			Event* pEvent = maList.GetFirst();
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 			if(pEvent)
259*cdf0e10cSrcweir 			{
260*cdf0e10cSrcweir 				// retet event time points
261*cdf0e10cSrcweir 				while(pEvent)
262*cdf0e10cSrcweir 				{
263*cdf0e10cSrcweir 					pEvent->SetTime(nTime);
264*cdf0e10cSrcweir 					pEvent = pEvent->GetNext();
265*cdf0e10cSrcweir 				}
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 				if(!IsPaused())
268*cdf0e10cSrcweir 				{
269*cdf0e10cSrcweir 					// without delta time, init events by triggering them. This will invalidate
270*cdf0e10cSrcweir 					// painted objects and add them to the scheduler again
271*cdf0e10cSrcweir 					mnDeltaTime = 0L;
272*cdf0e10cSrcweir 					triggerEvents();
273*cdf0e10cSrcweir 					checkTimeout();
274*cdf0e10cSrcweir 				}
275*cdf0e10cSrcweir 			}
276*cdf0e10cSrcweir 		}
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 		void Scheduler::Reset(sal_uInt32 nTime)
279*cdf0e10cSrcweir 		{
280*cdf0e10cSrcweir 			mnTime = nTime;
281*cdf0e10cSrcweir 			mnDeltaTime = 0L;
282*cdf0e10cSrcweir 			maList.Clear();
283*cdf0e10cSrcweir 		}
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 		void Scheduler::InsertEvent(Event* pNew)
286*cdf0e10cSrcweir 		{
287*cdf0e10cSrcweir 			if(pNew)
288*cdf0e10cSrcweir 			{
289*cdf0e10cSrcweir 				maList.Insert(pNew);
290*cdf0e10cSrcweir 				checkTimeout();
291*cdf0e10cSrcweir 			}
292*cdf0e10cSrcweir 		}
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 		void Scheduler::RemoveEvent(Event* pOld)
295*cdf0e10cSrcweir 		{
296*cdf0e10cSrcweir 			if(pOld && maList.GetFirst())
297*cdf0e10cSrcweir 			{
298*cdf0e10cSrcweir 				maList.Remove(pOld);
299*cdf0e10cSrcweir 				checkTimeout();
300*cdf0e10cSrcweir 			}
301*cdf0e10cSrcweir 		}
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 		void Scheduler::SetPaused(bool bNew)
304*cdf0e10cSrcweir 		{
305*cdf0e10cSrcweir 			if(bNew != mbIsPaused)
306*cdf0e10cSrcweir 			{
307*cdf0e10cSrcweir 				mbIsPaused = bNew;
308*cdf0e10cSrcweir 				checkTimeout();
309*cdf0e10cSrcweir 			}
310*cdf0e10cSrcweir 		}
311*cdf0e10cSrcweir 	} // end of namespace animation
312*cdf0e10cSrcweir } // end of namespace sdr
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
315*cdf0e10cSrcweir // eof
316