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 :
21 : #include "controller/SlsVisibleAreaManager.hxx"
22 : #include "controller/SlideSorterController.hxx"
23 : #include "controller/SlsProperties.hxx"
24 : #include "controller/SlsAnimationFunction.hxx"
25 : #include "controller/SlsScrollBarManager.hxx"
26 : #include "controller/SlsCurrentSlideManager.hxx"
27 :
28 :
29 : namespace sd { namespace slidesorter { namespace controller {
30 :
31 : namespace {
32 0 : class VisibleAreaScroller
33 : {
34 : public:
35 : VisibleAreaScroller (
36 : SlideSorter& rSlideSorter,
37 : const Point aStart,
38 : const Point aEnd);
39 : void operator() (const double nValue);
40 : private:
41 : SlideSorter& mrSlideSorter;
42 : Point maStart;
43 : const Point maEnd;
44 : const ::boost::function<double(double)> maAccelerationFunction;
45 : };
46 :
47 : } // end of anonymous namespace
48 :
49 :
50 :
51 65 : VisibleAreaManager::VisibleAreaManager (SlideSorter& rSlideSorter)
52 : : mrSlideSorter(rSlideSorter),
53 : maVisibleRequests(),
54 : mnScrollAnimationId(Animator::NotAnAnimationId),
55 : maRequestedVisibleTopLeft(),
56 : meRequestedAnimationMode(Animator::AM_Immediate),
57 : mbIsCurrentSlideTrackingActive(true),
58 65 : mnDisableCount(0)
59 : {
60 65 : }
61 :
62 :
63 :
64 :
65 65 : VisibleAreaManager::~VisibleAreaManager (void)
66 : {
67 65 : }
68 :
69 :
70 :
71 :
72 0 : void VisibleAreaManager::ActivateCurrentSlideTracking (void)
73 : {
74 0 : mbIsCurrentSlideTrackingActive = true;
75 0 : }
76 :
77 :
78 :
79 :
80 0 : void VisibleAreaManager::DeactivateCurrentSlideTracking (void)
81 : {
82 0 : mbIsCurrentSlideTrackingActive = false;
83 0 : }
84 :
85 :
86 :
87 :
88 0 : bool VisibleAreaManager::IsCurrentSlideTrackingActive (void) const
89 : {
90 0 : return mbIsCurrentSlideTrackingActive;
91 : }
92 :
93 :
94 :
95 :
96 421 : void VisibleAreaManager::RequestVisible (
97 : const model::SharedPageDescriptor& rpDescriptor,
98 : const bool bForce)
99 : {
100 421 : if (rpDescriptor)
101 : {
102 421 : if (mnDisableCount == 0)
103 : {
104 : maVisibleRequests.push_back(
105 287 : mrSlideSorter.GetView().GetLayouter().GetPageObjectBox(
106 : rpDescriptor->GetPageIndex(),
107 574 : true));
108 : }
109 421 : if (bForce && ! mbIsCurrentSlideTrackingActive)
110 0 : ActivateCurrentSlideTracking();
111 421 : MakeVisible();
112 : }
113 421 : }
114 :
115 :
116 :
117 :
118 153 : void VisibleAreaManager::RequestCurrentSlideVisible (void)
119 : {
120 153 : if (mbIsCurrentSlideTrackingActive && mnDisableCount==0)
121 : RequestVisible(
122 153 : mrSlideSorter.GetController().GetCurrentSlideManager()->GetCurrentSlide());
123 153 : }
124 :
125 :
126 :
127 :
128 421 : void VisibleAreaManager::MakeVisible (void)
129 : {
130 421 : if (maVisibleRequests.empty())
131 555 : return;
132 :
133 287 : SharedSdWindow pWindow (mrSlideSorter.GetContentWindow());
134 287 : if ( ! pWindow)
135 0 : return;
136 287 : const Point aCurrentTopLeft (pWindow->PixelToLogic(Point(0,0)));
137 :
138 287 : const ::boost::optional<Point> aNewVisibleTopLeft (GetRequestedTopLeft());
139 287 : maVisibleRequests.clear();
140 287 : if ( ! aNewVisibleTopLeft)
141 287 : return;
142 :
143 : // We now know what the visible area shall be. Scroll accordingly
144 : // unless that is not already the visible area or a running scroll
145 : // animation has it as its target area.
146 0 : if (mnScrollAnimationId!=Animator::NotAnAnimationId
147 0 : && maRequestedVisibleTopLeft==aNewVisibleTopLeft)
148 0 : return;
149 :
150 : // Stop a running animation.
151 0 : if (mnScrollAnimationId != Animator::NotAnAnimationId)
152 0 : mrSlideSorter.GetController().GetAnimator()->RemoveAnimation(mnScrollAnimationId);
153 :
154 0 : maRequestedVisibleTopLeft = aNewVisibleTopLeft.get();
155 : VisibleAreaScroller aAnimation(
156 : mrSlideSorter,
157 : aCurrentTopLeft,
158 0 : maRequestedVisibleTopLeft);
159 0 : if (meRequestedAnimationMode==Animator::AM_Animated
160 0 : && mrSlideSorter.GetProperties()->IsSmoothSelectionScrolling())
161 : {
162 0 : mnScrollAnimationId = mrSlideSorter.GetController().GetAnimator()->AddAnimation(
163 : aAnimation,
164 : 0,
165 0 : 300);
166 : }
167 : else
168 : {
169 : // Execute the animation at its final value.
170 0 : aAnimation(1.0);
171 : }
172 0 : meRequestedAnimationMode = Animator::AM_Immediate;
173 : }
174 :
175 :
176 :
177 :
178 287 : ::boost::optional<Point> VisibleAreaManager::GetRequestedTopLeft (void) const
179 : {
180 287 : SharedSdWindow pWindow (mrSlideSorter.GetContentWindow());
181 287 : if ( ! pWindow)
182 0 : return ::boost::optional<Point>();
183 :
184 : // Get the currently visible area and the model area.
185 287 : const Rectangle aVisibleArea (pWindow->PixelToLogic(
186 : Rectangle(
187 : Point(0,0),
188 574 : pWindow->GetOutputSizePixel())));
189 287 : const Rectangle aModelArea (mrSlideSorter.GetView().GetModelArea());
190 :
191 287 : sal_Int32 nVisibleTop (aVisibleArea.Top());
192 287 : const sal_Int32 nVisibleWidth (aVisibleArea.GetWidth());
193 287 : sal_Int32 nVisibleLeft (aVisibleArea.Left());
194 287 : const sal_Int32 nVisibleHeight (aVisibleArea.GetHeight());
195 :
196 : // Find the longest run of boxes whose union fits into the visible area.
197 574 : for (::std::vector<Rectangle>::const_iterator
198 287 : iBox(maVisibleRequests.begin()),
199 287 : iEnd(maVisibleRequests.end());
200 : iBox!=iEnd;
201 : ++iBox)
202 : {
203 287 : if (nVisibleTop+nVisibleHeight <= iBox->Bottom())
204 130 : nVisibleTop = iBox->Bottom()-nVisibleHeight;
205 287 : if (nVisibleTop > iBox->Top())
206 130 : nVisibleTop = iBox->Top();
207 :
208 287 : if (nVisibleLeft+nVisibleWidth <= iBox->Right())
209 130 : nVisibleLeft = iBox->Right()-nVisibleWidth;
210 287 : if (nVisibleLeft > iBox->Left())
211 130 : nVisibleLeft = iBox->Left();
212 :
213 : // Make sure the visible area does not move outside the model area.
214 287 : if (nVisibleTop + nVisibleHeight > aModelArea.Bottom())
215 153 : nVisibleTop = aModelArea.Bottom() - nVisibleHeight;
216 287 : if (nVisibleTop < aModelArea.Top())
217 153 : nVisibleTop = aModelArea.Top();
218 :
219 287 : if (nVisibleLeft + nVisibleWidth > aModelArea.Right())
220 157 : nVisibleLeft = aModelArea.Right() - nVisibleWidth;
221 287 : if (nVisibleLeft < aModelArea.Left())
222 157 : nVisibleLeft = aModelArea.Left();
223 : }
224 :
225 287 : const Point aRequestedTopLeft (nVisibleLeft, nVisibleTop);
226 287 : if (aRequestedTopLeft == aVisibleArea.TopLeft())
227 287 : return ::boost::optional<Point>();
228 : else
229 0 : return ::boost::optional<Point>(aRequestedTopLeft);
230 : }
231 :
232 :
233 :
234 :
235 : //===== VisibleAreaManager::TemporaryDisabler =================================
236 :
237 134 : VisibleAreaManager::TemporaryDisabler::TemporaryDisabler (SlideSorter& rSlideSorter)
238 134 : : mrVisibleAreaManager(rSlideSorter.GetController().GetVisibleAreaManager())
239 : {
240 134 : ++mrVisibleAreaManager.mnDisableCount;
241 134 : }
242 :
243 :
244 :
245 :
246 134 : VisibleAreaManager::TemporaryDisabler::~TemporaryDisabler (void)
247 : {
248 134 : --mrVisibleAreaManager.mnDisableCount;
249 134 : }
250 :
251 :
252 :
253 : //===== VerticalVisibleAreaScroller ===========================================
254 :
255 : namespace {
256 :
257 : const static sal_Int32 gnMaxScrollDistance = 300;
258 :
259 0 : VisibleAreaScroller::VisibleAreaScroller (
260 : SlideSorter& rSlideSorter,
261 : const Point aStart,
262 : const Point aEnd)
263 : : mrSlideSorter(rSlideSorter),
264 : maStart(aStart),
265 : maEnd(aEnd),
266 : maAccelerationFunction(
267 : controller::AnimationParametricFunction(
268 0 : controller::AnimationBezierFunction (0.1,0.6)))
269 : {
270 : // When the distance to scroll is larger than a threshold then first
271 : // jump to within this distance of the final value and start the
272 : // animation from there.
273 0 : if (abs(aStart.X()-aEnd.X()) > gnMaxScrollDistance)
274 : {
275 0 : if (aStart.X() < aEnd.X())
276 0 : maStart.X() = aEnd.X()-gnMaxScrollDistance;
277 : else
278 0 : maStart.X() = aEnd.X()+gnMaxScrollDistance;
279 : }
280 0 : if (abs(aStart.Y()-aEnd.Y()) > gnMaxScrollDistance)
281 : {
282 0 : if (aStart.Y() < aEnd.Y())
283 0 : maStart.Y() = aEnd.Y()-gnMaxScrollDistance;
284 : else
285 0 : maStart.Y() = aEnd.Y()+gnMaxScrollDistance;
286 : }
287 0 : }
288 :
289 :
290 :
291 :
292 0 : void VisibleAreaScroller::operator() (const double nTime)
293 : {
294 0 : const double nLocalTime (maAccelerationFunction(nTime));
295 0 : mrSlideSorter.GetController().GetScrollBarManager().SetTopLeft(
296 : Point(
297 0 : sal_Int32(0.5 + maStart.X() * (1.0 - nLocalTime) + maEnd.X() * nLocalTime),
298 0 : sal_Int32 (0.5 + maStart.Y() * (1.0 - nLocalTime) + maEnd.Y() * nLocalTime)));
299 0 : }
300 :
301 : } // end of anonymous namespace
302 :
303 33 : } } } // end of namespace ::sd::slidesorter::controller
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|