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 #include "precompiled_slideshow.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include "screenupdater.hxx"
30*cdf0e10cSrcweir #include "listenercontainer.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <boost/bind.hpp>
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir #include <algorithm>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace {
37*cdf0e10cSrcweir     class UpdateLock : public ::slideshow::internal::ScreenUpdater::UpdateLock
38*cdf0e10cSrcweir     {
39*cdf0e10cSrcweir     public:
40*cdf0e10cSrcweir         UpdateLock (::slideshow::internal::ScreenUpdater& rUpdater, const bool bStartLocked);
41*cdf0e10cSrcweir         virtual ~UpdateLock (void);
42*cdf0e10cSrcweir         virtual void Activate (void);
43*cdf0e10cSrcweir     private:
44*cdf0e10cSrcweir         ::slideshow::internal::ScreenUpdater& mrUpdater;
45*cdf0e10cSrcweir         bool mbIsActivated;
46*cdf0e10cSrcweir     };
47*cdf0e10cSrcweir }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace slideshow
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir namespace internal
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     typedef std::vector<
54*cdf0e10cSrcweir         std::pair<UnoViewSharedPtr,bool> > UpdateRequestVector;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     struct ScreenUpdater::ImplScreenUpdater
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir         /** List of registered ViewUpdaters, to consult for necessary
59*cdf0e10cSrcweir             updates
60*cdf0e10cSrcweir         */
61*cdf0e10cSrcweir         ThreadUnsafeListenerContainer<
62*cdf0e10cSrcweir             ViewUpdateSharedPtr,
63*cdf0e10cSrcweir             std::vector<ViewUpdateSharedPtr> > maUpdaters;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         /// Views that have been notified for update
66*cdf0e10cSrcweir         UpdateRequestVector                    maViewUpdateRequests;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         /// List of View. Used to issue screen updates on.
69*cdf0e10cSrcweir         UnoViewContainer const&                mrViewContainer;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         /// True, if a notifyUpdate() for all views has been issued.
72*cdf0e10cSrcweir         bool                                   mbUpdateAllRequest;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         /// True, if at least one notifyUpdate() call had bViewClobbered set
75*cdf0e10cSrcweir         bool                                   mbViewClobbered;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         /// The screen is updated only when mnLockCount==0
78*cdf0e10cSrcweir         sal_Int32 mnLockCount;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         explicit ImplScreenUpdater( UnoViewContainer const& rViewContainer ) :
81*cdf0e10cSrcweir             maUpdaters(),
82*cdf0e10cSrcweir             maViewUpdateRequests(),
83*cdf0e10cSrcweir             mrViewContainer(rViewContainer),
84*cdf0e10cSrcweir             mbUpdateAllRequest(false),
85*cdf0e10cSrcweir             mbViewClobbered(false),
86*cdf0e10cSrcweir             mnLockCount(0)
87*cdf0e10cSrcweir         {}
88*cdf0e10cSrcweir     };
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     ScreenUpdater::ScreenUpdater( UnoViewContainer const& rViewContainer ) :
91*cdf0e10cSrcweir         mpImpl(new ImplScreenUpdater(rViewContainer) )
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     ScreenUpdater::~ScreenUpdater()
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         // outline because of pimpl
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     void ScreenUpdater::notifyUpdate()
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         mpImpl->mbUpdateAllRequest = true;
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     void ScreenUpdater::notifyUpdate( const UnoViewSharedPtr& rView,
106*cdf0e10cSrcweir                                       bool                    bViewClobbered )
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         mpImpl->maViewUpdateRequests.push_back(
109*cdf0e10cSrcweir             std::make_pair(rView, bViewClobbered) );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         if( bViewClobbered )
112*cdf0e10cSrcweir             mpImpl->mbViewClobbered = true;
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     void ScreenUpdater::commitUpdates()
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         if (mpImpl->mnLockCount > 0)
118*cdf0e10cSrcweir             return;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         // cases:
121*cdf0e10cSrcweir         //
122*cdf0e10cSrcweir         // (a) no update necessary at all
123*cdf0e10cSrcweir         //
124*cdf0e10cSrcweir         // (b) no ViewUpdate-generated update
125*cdf0e10cSrcweir         //     I. update all views requested -> for_each( mrViewContainer )
126*cdf0e10cSrcweir         //    II. update some views requested -> for_each( maViewUpdateRequests )
127*cdf0e10cSrcweir         //
128*cdf0e10cSrcweir         // (c) ViewUpdate-triggered update - update all views
129*cdf0e10cSrcweir         //
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         // any ViewUpdate-triggered updates?
132*cdf0e10cSrcweir         const bool bViewUpdatesNeeded(
133*cdf0e10cSrcweir             mpImpl->maUpdaters.apply(
134*cdf0e10cSrcweir                 boost::mem_fn(&ViewUpdate::needsUpdate)) );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         if( bViewUpdatesNeeded )
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             mpImpl->maUpdaters.applyAll(
139*cdf0e10cSrcweir                 boost::mem_fn((bool (ViewUpdate::*)())&ViewUpdate::update) );
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         if( bViewUpdatesNeeded ||
143*cdf0e10cSrcweir             mpImpl->mbUpdateAllRequest )
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             // unconditionally update all views
146*cdf0e10cSrcweir             std::for_each( mpImpl->mrViewContainer.begin(),
147*cdf0e10cSrcweir                            mpImpl->mrViewContainer.end(),
148*cdf0e10cSrcweir                            mpImpl->mbViewClobbered ?
149*cdf0e10cSrcweir                            boost::mem_fn(&View::paintScreen) :
150*cdf0e10cSrcweir                            boost::mem_fn(&View::updateScreen) );
151*cdf0e10cSrcweir         }
152*cdf0e10cSrcweir         else if( !mpImpl->maViewUpdateRequests.empty() )
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             // update notified views only
155*cdf0e10cSrcweir             UpdateRequestVector::const_iterator aIter(
156*cdf0e10cSrcweir                 mpImpl->maViewUpdateRequests.begin() );
157*cdf0e10cSrcweir             const UpdateRequestVector::const_iterator aEnd(
158*cdf0e10cSrcweir                 mpImpl->maViewUpdateRequests.end() );
159*cdf0e10cSrcweir             while( aIter != aEnd )
160*cdf0e10cSrcweir             {
161*cdf0e10cSrcweir                 // TODO(P1): this is O(n^2) in the number of views, if
162*cdf0e10cSrcweir                 // lots of views notify updates.
163*cdf0e10cSrcweir                 const UnoViewVector::const_iterator aEndOfViews(
164*cdf0e10cSrcweir                     mpImpl->mrViewContainer.end() );
165*cdf0e10cSrcweir                 UnoViewVector::const_iterator aFoundView;
166*cdf0e10cSrcweir                 if( (aFoundView=std::find(mpImpl->mrViewContainer.begin(),
167*cdf0e10cSrcweir                                           aEndOfViews,
168*cdf0e10cSrcweir                                           aIter->first)) != aEndOfViews )
169*cdf0e10cSrcweir                 {
170*cdf0e10cSrcweir                     if( aIter->second )
171*cdf0e10cSrcweir                         (*aFoundView)->paintScreen(); // force-paint
172*cdf0e10cSrcweir                     else
173*cdf0e10cSrcweir                         (*aFoundView)->updateScreen(); // update changes only
174*cdf0e10cSrcweir                 }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir                 ++aIter;
177*cdf0e10cSrcweir             }
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         // done - clear requests
181*cdf0e10cSrcweir         mpImpl->mbViewClobbered = false;
182*cdf0e10cSrcweir         mpImpl->mbUpdateAllRequest = false;
183*cdf0e10cSrcweir         UpdateRequestVector().swap( mpImpl->maViewUpdateRequests );
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     void ScreenUpdater::addViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         mpImpl->maUpdaters.add( rViewUpdate );
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     void ScreenUpdater::removeViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         mpImpl->maUpdaters.remove( rViewUpdate );
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     void ScreenUpdater::requestImmediateUpdate()
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         if (mpImpl->mnLockCount > 0)
199*cdf0e10cSrcweir             return;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         // TODO(F2): This will interfere with other updates, since it
202*cdf0e10cSrcweir         // happens out-of-sync with main animation loop. Might cause
203*cdf0e10cSrcweir         // artifacts.
204*cdf0e10cSrcweir         std::for_each( mpImpl->mrViewContainer.begin(),
205*cdf0e10cSrcweir                        mpImpl->mrViewContainer.end(),
206*cdf0e10cSrcweir                        boost::mem_fn(&View::updateScreen) );
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     void ScreenUpdater::lockUpdates (void)
210*cdf0e10cSrcweir     {
211*cdf0e10cSrcweir         ++mpImpl->mnLockCount;
212*cdf0e10cSrcweir         OSL_ASSERT(mpImpl->mnLockCount>0);
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     void ScreenUpdater::unlockUpdates (void)
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         OSL_ASSERT(mpImpl->mnLockCount>0);
218*cdf0e10cSrcweir         if (mpImpl->mnLockCount > 0)
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             --mpImpl->mnLockCount;
221*cdf0e10cSrcweir             if (mpImpl->mnLockCount)
222*cdf0e10cSrcweir                 commitUpdates();
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     ::boost::shared_ptr<ScreenUpdater::UpdateLock> ScreenUpdater::createLock (const bool bStartLocked)
227*cdf0e10cSrcweir     {
228*cdf0e10cSrcweir         return ::boost::shared_ptr<ScreenUpdater::UpdateLock>(new ::UpdateLock(*this, bStartLocked));
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir } // namespace internal
233*cdf0e10cSrcweir } // namespace slideshow
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir namespace {
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir UpdateLock::UpdateLock (
238*cdf0e10cSrcweir     ::slideshow::internal::ScreenUpdater& rUpdater,
239*cdf0e10cSrcweir     const bool bStartLocked)
240*cdf0e10cSrcweir     : mrUpdater(rUpdater),
241*cdf0e10cSrcweir       mbIsActivated(false)
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir     if (bStartLocked)
244*cdf0e10cSrcweir         Activate();
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir UpdateLock::~UpdateLock (void)
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir     if (mbIsActivated)
253*cdf0e10cSrcweir         mrUpdater.unlockUpdates();
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir void UpdateLock::Activate (void)
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir     if ( ! mbIsActivated)
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         mbIsActivated = true;
264*cdf0e10cSrcweir         mrUpdater.lockUpdates();
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir }
269