Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "screenupdater.hxx"
21 : : #include "listenercontainer.hxx"
22 : :
23 : : #include <boost/shared_ptr.hpp>
24 : : #include <boost/mem_fn.hpp>
25 : : #include <vector>
26 : : #include <algorithm>
27 : :
28 : : namespace {
29 : : class UpdateLock : public ::slideshow::internal::ScreenUpdater::UpdateLock
30 : : {
31 : : public:
32 : : UpdateLock (::slideshow::internal::ScreenUpdater& rUpdater, const bool bStartLocked);
33 : : virtual ~UpdateLock (void);
34 : : virtual void Activate (void);
35 : : private:
36 : : ::slideshow::internal::ScreenUpdater& mrUpdater;
37 : : bool mbIsActivated;
38 : : };
39 : : }
40 : :
41 : : namespace slideshow
42 : : {
43 : : namespace internal
44 : : {
45 : : typedef std::vector<
46 : : std::pair<UnoViewSharedPtr,bool> > UpdateRequestVector;
47 : :
48 : 0 : struct ScreenUpdater::ImplScreenUpdater
49 : : {
50 : : /** List of registered ViewUpdaters, to consult for necessary
51 : : updates
52 : : */
53 : : ThreadUnsafeListenerContainer<
54 : : ViewUpdateSharedPtr,
55 : : std::vector<ViewUpdateSharedPtr> > maUpdaters;
56 : :
57 : : /// Views that have been notified for update
58 : : UpdateRequestVector maViewUpdateRequests;
59 : :
60 : : /// List of View. Used to issue screen updates on.
61 : : UnoViewContainer const& mrViewContainer;
62 : :
63 : : /// True, if a notifyUpdate() for all views has been issued.
64 : : bool mbUpdateAllRequest;
65 : :
66 : : /// True, if at least one notifyUpdate() call had bViewClobbered set
67 : : bool mbViewClobbered;
68 : :
69 : : /// The screen is updated only when mnLockCount==0
70 : : sal_Int32 mnLockCount;
71 : :
72 : 0 : explicit ImplScreenUpdater( UnoViewContainer const& rViewContainer ) :
73 : : maUpdaters(),
74 : : maViewUpdateRequests(),
75 : : mrViewContainer(rViewContainer),
76 : : mbUpdateAllRequest(false),
77 : : mbViewClobbered(false),
78 : 0 : mnLockCount(0)
79 : 0 : {}
80 : : };
81 : :
82 : 0 : ScreenUpdater::ScreenUpdater( UnoViewContainer const& rViewContainer ) :
83 : 0 : mpImpl(new ImplScreenUpdater(rViewContainer) )
84 : : {
85 : 0 : }
86 : :
87 : 0 : ScreenUpdater::~ScreenUpdater()
88 : : {
89 : : // outline because of pimpl
90 : 0 : }
91 : :
92 : 0 : void ScreenUpdater::notifyUpdate()
93 : : {
94 : 0 : mpImpl->mbUpdateAllRequest = true;
95 : 0 : }
96 : :
97 : 0 : void ScreenUpdater::notifyUpdate( const UnoViewSharedPtr& rView,
98 : : bool bViewClobbered )
99 : : {
100 : 0 : mpImpl->maViewUpdateRequests.push_back(
101 : 0 : std::make_pair(rView, bViewClobbered) );
102 : :
103 : 0 : if( bViewClobbered )
104 : 0 : mpImpl->mbViewClobbered = true;
105 : 0 : }
106 : :
107 : 0 : void ScreenUpdater::commitUpdates()
108 : : {
109 : 0 : if (mpImpl->mnLockCount > 0)
110 : 0 : return;
111 : :
112 : : // cases:
113 : : //
114 : : // (a) no update necessary at all
115 : : //
116 : : // (b) no ViewUpdate-generated update
117 : : // I. update all views requested -> for_each( mrViewContainer )
118 : : // II. update some views requested -> for_each( maViewUpdateRequests )
119 : : //
120 : : // (c) ViewUpdate-triggered update - update all views
121 : : //
122 : :
123 : : // any ViewUpdate-triggered updates?
124 : : const bool bViewUpdatesNeeded(
125 : 0 : mpImpl->maUpdaters.apply(
126 : 0 : boost::mem_fn(&ViewUpdate::needsUpdate)) );
127 : :
128 : 0 : if( bViewUpdatesNeeded )
129 : : {
130 : 0 : mpImpl->maUpdaters.applyAll(
131 : 0 : boost::mem_fn((bool (ViewUpdate::*)())&ViewUpdate::update) );
132 : : }
133 : :
134 : 0 : if( bViewUpdatesNeeded ||
135 : 0 : mpImpl->mbUpdateAllRequest )
136 : : {
137 : : // unconditionally update all views
138 : 0 : std::for_each( mpImpl->mrViewContainer.begin(),
139 : 0 : mpImpl->mrViewContainer.end(),
140 : 0 : mpImpl->mbViewClobbered ?
141 : 0 : boost::mem_fn(&View::paintScreen) :
142 : 0 : boost::mem_fn(&View::updateScreen) );
143 : : }
144 : 0 : else if( !mpImpl->maViewUpdateRequests.empty() )
145 : : {
146 : : // update notified views only
147 : : UpdateRequestVector::const_iterator aIter(
148 : 0 : mpImpl->maViewUpdateRequests.begin() );
149 : : const UpdateRequestVector::const_iterator aEnd(
150 : 0 : mpImpl->maViewUpdateRequests.end() );
151 : 0 : while( aIter != aEnd )
152 : : {
153 : : // TODO(P1): this is O(n^2) in the number of views, if
154 : : // lots of views notify updates.
155 : : const UnoViewVector::const_iterator aEndOfViews(
156 : 0 : mpImpl->mrViewContainer.end() );
157 : 0 : UnoViewVector::const_iterator aFoundView;
158 : 0 : if( (aFoundView=std::find(mpImpl->mrViewContainer.begin(),
159 : : aEndOfViews,
160 : 0 : aIter->first)) != aEndOfViews )
161 : : {
162 : 0 : if( aIter->second )
163 : 0 : (*aFoundView)->paintScreen(); // force-paint
164 : : else
165 : 0 : (*aFoundView)->updateScreen(); // update changes only
166 : : }
167 : :
168 : 0 : ++aIter;
169 : : }
170 : : }
171 : :
172 : : // done - clear requests
173 : 0 : mpImpl->mbViewClobbered = false;
174 : 0 : mpImpl->mbUpdateAllRequest = false;
175 : 0 : UpdateRequestVector().swap( mpImpl->maViewUpdateRequests );
176 : : }
177 : :
178 : 0 : void ScreenUpdater::addViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
179 : : {
180 : 0 : mpImpl->maUpdaters.add( rViewUpdate );
181 : 0 : }
182 : :
183 : 0 : void ScreenUpdater::removeViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
184 : : {
185 : 0 : mpImpl->maUpdaters.remove( rViewUpdate );
186 : 0 : }
187 : :
188 : 0 : void ScreenUpdater::requestImmediateUpdate()
189 : : {
190 : 0 : if (mpImpl->mnLockCount > 0)
191 : 0 : return;
192 : :
193 : : // TODO(F2): This will interfere with other updates, since it
194 : : // happens out-of-sync with main animation loop. Might cause
195 : : // artifacts.
196 : 0 : std::for_each( mpImpl->mrViewContainer.begin(),
197 : 0 : mpImpl->mrViewContainer.end(),
198 : 0 : boost::mem_fn(&View::updateScreen) );
199 : : }
200 : :
201 : 0 : void ScreenUpdater::lockUpdates (void)
202 : : {
203 : 0 : ++mpImpl->mnLockCount;
204 : : OSL_ASSERT(mpImpl->mnLockCount>0);
205 : 0 : }
206 : :
207 : 0 : void ScreenUpdater::unlockUpdates (void)
208 : : {
209 : : OSL_ASSERT(mpImpl->mnLockCount>0);
210 : 0 : if (mpImpl->mnLockCount > 0)
211 : : {
212 : 0 : --mpImpl->mnLockCount;
213 : 0 : if (mpImpl->mnLockCount)
214 : 0 : commitUpdates();
215 : : }
216 : 0 : }
217 : :
218 : 0 : ::boost::shared_ptr<ScreenUpdater::UpdateLock> ScreenUpdater::createLock (const bool bStartLocked)
219 : : {
220 : 0 : return ::boost::shared_ptr<ScreenUpdater::UpdateLock>(new ::UpdateLock(*this, bStartLocked));
221 : : }
222 : :
223 : :
224 : : } // namespace internal
225 : : } // namespace slideshow
226 : :
227 : : namespace {
228 : :
229 : 0 : UpdateLock::UpdateLock (
230 : : ::slideshow::internal::ScreenUpdater& rUpdater,
231 : : const bool bStartLocked)
232 : : : mrUpdater(rUpdater),
233 : 0 : mbIsActivated(false)
234 : : {
235 : 0 : if (bStartLocked)
236 : 0 : Activate();
237 : 0 : }
238 : :
239 : :
240 : :
241 : :
242 : 0 : UpdateLock::~UpdateLock (void)
243 : : {
244 : 0 : if (mbIsActivated)
245 : 0 : mrUpdater.unlockUpdates();
246 : 0 : }
247 : :
248 : :
249 : :
250 : :
251 : 0 : void UpdateLock::Activate (void)
252 : : {
253 : 0 : if ( ! mbIsActivated)
254 : : {
255 : 0 : mbIsActivated = true;
256 : 0 : mrUpdater.lockUpdates();
257 : : }
258 : 0 : }
259 : :
260 : : }
261 : :
262 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|