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