Update ooo320-m1
[ooovba.git] / sd / source / ui / slidesorter / controller / SlsAnimator.cxx
blobbb6f542f9e6d7ab578f583311dea0dd1c1e90e0c
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: SlsAnimator.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "precompiled_sd.hxx"
33 #include "controller/SlsAnimator.hxx"
34 #include "view/SlideSorterView.hxx"
35 #include "View.hxx"
37 namespace sd { namespace slidesorter { namespace controller {
39 namespace {
40 static const sal_Int32 gnResolution = 25;
42 /** Handle one animation function by using a timer for frequent calls to
43 the animations operator().
45 class Animator::Animation
47 public:
48 Animation (
49 const Animator::AnimationFunction& rAnimation,
50 const double nDelta);
51 ~Animation (void);
52 bool Run (void);
53 bool IsExpired (void);
54 Animator::AnimationFunction maAnimation;
55 double mnValue;
56 double mnDelta;
62 class Animator::DrawLock
64 public:
65 DrawLock (View& rView);
66 ~DrawLock (void);
68 private:
69 View& mrView;
75 Animator::Animator (SlideSorter& rSlideSorter)
76 : mrSlideSorter(rSlideSorter),
77 maTimer(),
78 maAnimations(),
79 mpDrawLock()
81 maTimer.SetTimeout(gnResolution);
82 maTimer.SetTimeoutHdl(LINK(this,Animator,TimeoutHandler));
88 Animator::~Animator (void)
90 maTimer.Stop();
91 mpDrawLock.reset();
97 void Animator::AddAnimation (
98 const AnimationFunction& rAnimation,
99 const sal_Int32 nDuration)
101 const double nDelta = double(gnResolution) / double(nDuration);
102 maAnimations.push_back(boost::shared_ptr<Animation>(new Animation(rAnimation, nDelta)));
104 // Prevent redraws except for the ones in TimeoutHandler.
105 // While the Animator is active it will schedule repaints regularly.
106 // Repaints in between would only lead to visual artifacts.
107 mpDrawLock.reset(new DrawLock(mrSlideSorter.GetView()));
108 maTimer.Start();
114 bool Animator::ServeAnimations (void)
116 bool bExpired (false);
118 AnimationList aCopy (maAnimations);
119 AnimationList::const_iterator iAnimation;
120 for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
122 bExpired |= (*iAnimation)->Run();
125 return bExpired;
131 void Animator::CleanUpAnimationList (void)
133 AnimationList aActiveAnimations;
135 AnimationList::const_iterator iAnimation;
136 for (iAnimation=maAnimations.begin(); iAnimation!=maAnimations.end(); ++iAnimation)
138 if ( ! (*iAnimation)->IsExpired())
139 aActiveAnimations.push_back(*iAnimation);
142 maAnimations.swap(aActiveAnimations);
148 IMPL_LINK(Animator, TimeoutHandler, Timer*, EMPTYARG)
150 if (ServeAnimations())
151 CleanUpAnimationList();
153 // Unlock the draw lock. This should lead to a repaint.
154 mpDrawLock.reset();
156 if (maAnimations.size() > 0)
158 mpDrawLock.reset(new DrawLock(mrSlideSorter.GetView()));
159 maTimer.Start();
162 return 0;
168 //===== Animator::Animation ===================================================
170 Animator::Animation::Animation (
171 const Animator::AnimationFunction& rAnimation,
172 const double nDelta)
173 : maAnimation(rAnimation),
174 mnValue(0),
175 mnDelta(nDelta)
178 maAnimation(mnValue);
179 mnValue = mnDelta;
186 Animator::Animation::~Animation (void)
193 bool Animator::Animation::Run (void)
195 if (mnValue < 1.0)
197 maAnimation(mnValue);
198 mnValue += mnDelta;
199 return false;
201 else
203 maAnimation(1.0);
204 return true;
211 bool Animator::Animation::IsExpired (void)
213 return mnValue >= 1.0;
219 //===== Animator::DrawLock ====================================================
221 Animator::DrawLock::DrawLock (View& rView)
222 : mrView(rView)
224 mrView.LockRedraw(TRUE);
230 Animator::DrawLock::~DrawLock (void)
232 mrView.LockRedraw(FALSE);
236 } } } // end of namespace ::sd::slidesorter::controller