tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsScrollBarManager.cxx
blob6a0244f4165539e39e80909d5e32cef5fca8555b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <controller/SlsScrollBarManager.hxx>
22 #include <SlideSorter.hxx>
23 #include <ViewShell.hxx>
24 #include <controller/SlideSorterController.hxx>
25 #include <controller/SlsVisibleAreaManager.hxx>
26 #include <model/SlideSorterModel.hxx>
27 #include <model/SlsPageDescriptor.hxx>
28 #include <view/SlideSorterView.hxx>
29 #include <view/SlsLayouter.hxx>
30 #include <Window.hxx>
31 #include <sdpage.hxx>
32 #include <osl/diagnose.h>
34 namespace sd::slidesorter::controller {
36 constexpr double gnHorizontalScrollFactor(0.15);
37 constexpr double gnVerticalScrollFactor(0.25);
39 ScrollBarManager::ScrollBarManager (SlideSorter& rSlideSorter)
40 : mrSlideSorter(rSlideSorter),
41 mpHorizontalScrollBar(mrSlideSorter.GetHorizontalScrollBar()),
42 mpVerticalScrollBar(mrSlideSorter.GetVerticalScrollBar()),
43 mnHorizontalPosition (0),
44 mnVerticalPosition (0),
45 maScrollBorder (20,20),
46 maAutoScrollTimer("sd ScrollBarManager maAutoScrollTimer"),
47 maAutoScrollOffset(0,0),
48 mbIsAutoScrollActive(false),
49 mpContentWindow(mrSlideSorter.GetContentWindow())
51 // Hide the scroll bars by default to prevent display errors while
52 // switching between view shells: In the short time between initiating
53 // such a switch and the final rearrangement of UI controls the scroll
54 // bars and the filler where displayed in the upper left corner of the
55 // ViewTabBar.
56 mpHorizontalScrollBar->Hide();
57 mpVerticalScrollBar->Hide();
59 maAutoScrollTimer.SetTimeout(25);
60 maAutoScrollTimer.SetInvokeHandler (
61 LINK(this, ScrollBarManager, AutoScrollTimeoutHandler));
64 ScrollBarManager::~ScrollBarManager()
68 void ScrollBarManager::Connect()
70 if (mpVerticalScrollBar != nullptr)
72 mpVerticalScrollBar->SetScrollHdl (
73 LINK(this, ScrollBarManager, VerticalScrollBarHandler));
75 if (mpHorizontalScrollBar != nullptr)
77 mpHorizontalScrollBar->SetScrollHdl(
78 LINK(this, ScrollBarManager, HorizontalScrollBarHandler));
82 void ScrollBarManager::Disconnect()
84 if (mpVerticalScrollBar != nullptr)
86 mpVerticalScrollBar->SetScrollHdl( Link<weld::Scrollbar&,void>() );
88 if (mpHorizontalScrollBar != nullptr)
90 mpHorizontalScrollBar->SetScrollHdl( Link<weld::Scrollbar&,void>() );
94 /** Placing the scroll bars is an iterative process. The visibility of one
95 scroll bar affects the remaining size and thus may lead to the other
96 scroll bar becoming visible.
98 First we determine the visibility of the horizontal scroll bar. After
99 that we do the same for the vertical scroll bar. To have an initial
100 value for the required size we call the layouter before that. When one
101 of the two scroll bars is made visible then the size of the browser
102 window changes and a second call to the layouter becomes necessary.
103 That call is made anyway after this method returns.
105 ::tools::Rectangle ScrollBarManager::PlaceScrollBars (
106 const ::tools::Rectangle& rAvailableArea,
107 const bool bIsHorizontalScrollBarAllowed,
108 const bool bIsVerticalScrollBarAllowed)
110 ::tools::Rectangle aRemainingSpace (DetermineScrollBarVisibilities(
111 rAvailableArea,
112 bIsHorizontalScrollBarAllowed,
113 bIsVerticalScrollBarAllowed));
115 if (mpHorizontalScrollBar!=nullptr && mpHorizontalScrollBar->IsVisible())
116 PlaceHorizontalScrollBar (rAvailableArea);
118 if (mpVerticalScrollBar!=nullptr && mpVerticalScrollBar->IsVisible())
119 PlaceVerticalScrollBar (rAvailableArea);
121 return aRemainingSpace;
124 void ScrollBarManager::PlaceHorizontalScrollBar (const ::tools::Rectangle& aAvailableArea)
126 // Save the current relative position.
127 mnHorizontalPosition = double(mpHorizontalScrollBar->GetThumbPos())
128 / double(mpHorizontalScrollBar->GetRange().Len());
130 // Place the scroll bar.
131 Size aScrollBarSize (mpHorizontalScrollBar->GetSizePixel());
132 mpHorizontalScrollBar->SetPosSizePixel (
133 Point(aAvailableArea.Left(),
134 aAvailableArea.Bottom()-aScrollBarSize.Height()+1),
135 Size (aAvailableArea.GetWidth() - GetVerticalScrollBarWidth(),
136 aScrollBarSize.Height()));
138 // Restore the relative position.
139 mpHorizontalScrollBar->SetThumbPos(
140 static_cast<::tools::Long>(0.5 + mnHorizontalPosition * mpHorizontalScrollBar->GetRange().Len()));
143 void ScrollBarManager::PlaceVerticalScrollBar (const ::tools::Rectangle& aArea)
145 const sal_Int32 nThumbPosition (mpVerticalScrollBar->GetThumbPos());
147 // Place the scroll bar.
148 Size aScrollBarSize (mpVerticalScrollBar->GetSizePixel());
149 Point aPosition (aArea.Right()-aScrollBarSize.Width()+1, aArea.Top());
150 Size aSize (aScrollBarSize.Width(), aArea.GetHeight() - GetHorizontalScrollBarHeight());
151 mpVerticalScrollBar->SetPosSizePixel(aPosition, aSize);
153 // Restore the position.
154 mpVerticalScrollBar->SetThumbPos(static_cast<::tools::Long>(nThumbPosition));
155 mnVerticalPosition = nThumbPosition / double(mpVerticalScrollBar->GetRange().Len());
158 void ScrollBarManager::UpdateScrollBars(bool bUseScrolling)
160 ::tools::Rectangle aModelArea (mrSlideSorter.GetView().GetModelArea());
161 sd::Window *pWindow (mrSlideSorter.GetContentWindow().get());
162 Size aWindowModelSize (pWindow->PixelToLogic(pWindow->GetSizePixel()));
164 // The horizontal scroll bar is only shown when the window is
165 // horizontally smaller than the view.
166 if (mpHorizontalScrollBar != nullptr && mpHorizontalScrollBar->IsVisible())
168 mpHorizontalScrollBar->Show();
169 mpHorizontalScrollBar->SetRange (
170 Range(aModelArea.Left(), aModelArea.Right()));
171 mnHorizontalPosition =
172 double(mpHorizontalScrollBar->GetThumbPos())
173 / double(mpHorizontalScrollBar->GetRange().Len());
175 mpHorizontalScrollBar->SetVisibleSize (aWindowModelSize.Width());
177 const ::tools::Long nWidth (mpContentWindow->PixelToLogic(
178 mpContentWindow->GetSizePixel()).Width());
179 // Make the line size about 10% of the visible width.
180 mpHorizontalScrollBar->SetLineSize (nWidth / 10);
181 // Make the page size about 90% of the visible width.
182 mpHorizontalScrollBar->SetPageSize ((nWidth * 9) / 10);
184 else
186 mnHorizontalPosition = 0;
189 // The vertical scroll bar is always shown.
190 if (mpVerticalScrollBar != nullptr && mpVerticalScrollBar->IsVisible())
192 mpVerticalScrollBar->SetRange (
193 Range(aModelArea.Top(), aModelArea.Bottom()));
194 mnVerticalPosition =
195 double(mpVerticalScrollBar->GetThumbPos())
196 / double(mpVerticalScrollBar->GetRange().Len());
198 mpVerticalScrollBar->SetVisibleSize (aWindowModelSize.Height());
200 const ::tools::Long nHeight (mpContentWindow->PixelToLogic(
201 mpContentWindow->GetSizePixel()).Height());
202 // Make the line size about 10% of the visible height.
203 mpVerticalScrollBar->SetLineSize (nHeight / 10);
204 // Make the page size about 90% of the visible height.
205 mpVerticalScrollBar->SetPageSize ((nHeight * 9) / 10);
207 else
209 mnVerticalPosition = 0;
212 double nEps (::std::numeric_limits<double>::epsilon());
213 if (fabs(mnHorizontalPosition-pWindow->GetVisibleX()) > nEps
214 || fabs(mnVerticalPosition-pWindow->GetVisibleY()) > nEps)
216 mrSlideSorter.GetView().InvalidatePageObjectVisibilities();
217 if (bUseScrolling)
218 pWindow->SetVisibleXY(mnHorizontalPosition, mnVerticalPosition);
219 else
220 SetWindowOrigin(mnHorizontalPosition, mnVerticalPosition);
224 IMPL_LINK_NOARG(ScrollBarManager, VerticalScrollBarHandler, weld::Scrollbar&, void)
226 if (mpVerticalScrollBar->IsVisible() && mrSlideSorter.GetContentWindow())
228 double nRelativePosition = double(mpVerticalScrollBar->GetThumbPos())
229 / double(mpVerticalScrollBar->GetRange().Len());
230 mrSlideSorter.GetView().InvalidatePageObjectVisibilities();
231 mrSlideSorter.GetContentWindow()->SetVisibleXY(-1, nRelativePosition);
232 mrSlideSorter.GetController().GetVisibleAreaManager().DeactivateCurrentSlideTracking();
236 IMPL_LINK_NOARG(ScrollBarManager, HorizontalScrollBarHandler, weld::Scrollbar&, void)
238 if (mpHorizontalScrollBar->IsVisible() && mrSlideSorter.GetContentWindow())
240 double nRelativePosition = double(mpHorizontalScrollBar->GetThumbPos())
241 / double(mpHorizontalScrollBar->GetRange().Len());
242 mrSlideSorter.GetView().InvalidatePageObjectVisibilities();
243 mrSlideSorter.GetContentWindow()->SetVisibleXY(nRelativePosition, -1);
244 mrSlideSorter.GetController().GetVisibleAreaManager().DeactivateCurrentSlideTracking();
248 void ScrollBarManager::SetWindowOrigin (
249 double nHorizontalPosition,
250 double nVerticalPosition)
252 mnHorizontalPosition = nHorizontalPosition;
253 mnVerticalPosition = nVerticalPosition;
255 sd::Window *pWindow (mrSlideSorter.GetContentWindow().get());
256 Size aViewSize (pWindow->GetViewSize());
257 Point aOrigin (
258 static_cast<::tools::Long>(mnHorizontalPosition * aViewSize.Width()),
259 static_cast<::tools::Long>(mnVerticalPosition * aViewSize.Height()));
261 pWindow->SetWinViewPos (aOrigin);
262 pWindow->UpdateMapMode ();
263 pWindow->Invalidate ();
266 /** Determining the visibility of the scroll bars is quite complicated. The
267 visibility of one influences that of the other because showing a scroll
268 bar makes the available space smaller and may lead to the need of
269 displaying the other.
270 To solve this we test all four combinations of showing or hiding each
271 scroll bar and use the best one. The best one is that combination that
272 a) shows the least number of scroll bars with preference of showing the
273 vertical over showing the horizontal and
274 b) when not showing a scroll bar the area used by the page objects fits
275 into the available area in the scroll bars orientation.
277 ::tools::Rectangle ScrollBarManager::DetermineScrollBarVisibilities (
278 const ::tools::Rectangle& rAvailableArea,
279 const bool bIsHorizontalScrollBarAllowed,
280 const bool bIsVerticalScrollBarAllowed)
282 // Test which combination of scroll bars is the best.
283 bool bShowHorizontal = false;
284 bool bShowVertical = false;
285 if (mrSlideSorter.GetModel().GetPageCount() == 0)
287 // No pages => no scroll bars.
289 else if (TestScrollBarVisibilities(false, false, rAvailableArea))
291 // Nothing to be done.
293 else if (bIsHorizontalScrollBarAllowed
294 && TestScrollBarVisibilities(true, false, rAvailableArea))
296 bShowHorizontal = true;
298 else if (bIsVerticalScrollBarAllowed
299 && TestScrollBarVisibilities(false, true, rAvailableArea))
301 bShowVertical = true;
303 else
305 bShowHorizontal = true;
306 bShowVertical = true;
309 // Make the visibility of the scroll bars permanent.
310 mpVerticalScrollBar->Show(bShowVertical);
311 mpHorizontalScrollBar->Show(bShowHorizontal);
313 // Adapt the remaining space accordingly.
314 ::tools::Rectangle aRemainingSpace (rAvailableArea);
315 if (bShowVertical)
316 aRemainingSpace.AdjustRight( -(mpVerticalScrollBar->GetSizePixel().Width()) );
317 if (bShowHorizontal)
318 aRemainingSpace.AdjustBottom( -(mpHorizontalScrollBar->GetSizePixel().Height()) );
320 return aRemainingSpace;
323 bool ScrollBarManager::TestScrollBarVisibilities (
324 bool bHorizontalScrollBarVisible,
325 bool bVerticalScrollBarVisible,
326 const ::tools::Rectangle& rAvailableArea)
328 model::SlideSorterModel& rModel (mrSlideSorter.GetModel());
330 // Adapt the available size by subtracting the sizes of the scroll bars
331 // visible in this combination.
332 Size aBrowserSize (rAvailableArea.GetSize());
333 if (bHorizontalScrollBarVisible)
334 aBrowserSize.AdjustHeight( -(mpHorizontalScrollBar->GetSizePixel().Height()) );
335 if (bVerticalScrollBarVisible)
336 aBrowserSize.AdjustWidth( -(mpVerticalScrollBar->GetSizePixel().Width()) );
338 // Tell the view to rearrange its page objects and check whether the
339 // page objects can be shown without clipping.
340 bool bRearrangeSuccess (mrSlideSorter.GetView().GetLayouter().Rearrange (
341 mrSlideSorter.GetView().GetOrientation(),
342 aBrowserSize,
343 rModel.GetPageDescriptor(0)->GetPage()->GetSize(),
344 rModel.GetPageCount()));
346 if (bRearrangeSuccess)
348 Size aPageSize = mrSlideSorter.GetView().GetLayouter().GetTotalBoundingBox().GetSize();
349 Size aWindowModelSize = mpContentWindow->PixelToLogic(aBrowserSize);
351 // The content may be clipped, i.e. not fully visible, in one
352 // direction only when the scroll bar is visible in that direction.
353 if (aPageSize.Width() > aWindowModelSize.Width())
354 if ( ! bHorizontalScrollBarVisible)
355 return false;
356 if (aPageSize.Height() > aWindowModelSize.Height())
357 if ( ! bVerticalScrollBarVisible)
358 return false;
360 return true;
362 else
363 return false;
366 void ScrollBarManager::SetTopLeft(const Point& rNewTopLeft)
368 if (( ! mpVerticalScrollBar
369 || mpVerticalScrollBar->GetThumbPos() == rNewTopLeft.Y())
370 && ( ! mpHorizontalScrollBar
371 || mpHorizontalScrollBar->GetThumbPos() == rNewTopLeft.X()))
372 return;
374 // Flush pending repaints before scrolling to avoid temporary artifacts.
375 mrSlideSorter.GetContentWindow()->PaintImmediately();
377 if (mpVerticalScrollBar)
379 mpVerticalScrollBar->SetThumbPos(rNewTopLeft.Y());
380 mnVerticalPosition = rNewTopLeft.Y() / double(mpVerticalScrollBar->GetRange().Len());
382 if (mpHorizontalScrollBar)
384 mpHorizontalScrollBar->SetThumbPos(rNewTopLeft.X());
385 mnHorizontalPosition = rNewTopLeft.X() / double(mpHorizontalScrollBar->GetRange().Len());
388 mrSlideSorter.GetContentWindow()->SetVisibleXY(mnHorizontalPosition, mnVerticalPosition);
389 mrSlideSorter.GetView().InvalidatePageObjectVisibilities();
392 int ScrollBarManager::GetVerticalScrollBarWidth() const
394 if (mpVerticalScrollBar != nullptr && mpVerticalScrollBar->IsVisible())
395 return mpVerticalScrollBar->GetSizePixel().Width();
396 else
397 return 0;
400 int ScrollBarManager::GetHorizontalScrollBarHeight() const
402 if (mpHorizontalScrollBar != nullptr && mpHorizontalScrollBar->IsVisible())
403 return mpHorizontalScrollBar->GetSizePixel().Height();
404 else
405 return 0;
408 void ScrollBarManager::CalcAutoScrollOffset (const Point& rMouseWindowPosition)
410 sd::Window *pWindow (mrSlideSorter.GetContentWindow().get());
412 int nDx = 0;
413 int nDy = 0;
415 Size aWindowSize = pWindow->GetOutputSizePixel();
416 ::tools::Rectangle aWindowArea (pWindow->GetPosPixel(), aWindowSize);
417 ::tools::Rectangle aViewPixelArea (
418 pWindow->LogicToPixel(mrSlideSorter.GetView().GetModelArea()));
420 if (aWindowSize.Width() > maScrollBorder.Width() * 3
421 && mpHorizontalScrollBar != nullptr
422 && mpHorizontalScrollBar->IsVisible())
424 if (rMouseWindowPosition.X() < maScrollBorder.Width()
425 && aWindowArea.Left() > aViewPixelArea.Left())
427 nDx = -1 + static_cast<int>(gnHorizontalScrollFactor
428 * (rMouseWindowPosition.X() - maScrollBorder.Width()));
431 if (rMouseWindowPosition.X() >= (aWindowSize.Width() - maScrollBorder.Width())
432 && aWindowArea.Right() < aViewPixelArea.Right())
434 nDx = 1 + static_cast<int>(gnHorizontalScrollFactor
435 * (rMouseWindowPosition.X() - aWindowSize.Width()
436 + maScrollBorder.Width()));
440 if (aWindowSize.Height() > maScrollBorder.Height() * 3
441 && aWindowSize.Height() < aViewPixelArea.GetHeight())
443 if (rMouseWindowPosition.Y() < maScrollBorder.Height()
444 && aWindowArea.Top() > aViewPixelArea.Top())
446 nDy = -1 + static_cast<int>(gnVerticalScrollFactor
447 * (rMouseWindowPosition.Y() - maScrollBorder.Height()));
450 if (rMouseWindowPosition.Y() >= (aWindowSize.Height() - maScrollBorder.Height())
451 && aWindowArea.Bottom() < aViewPixelArea.Bottom())
453 nDy = 1 + static_cast<int>(gnVerticalScrollFactor
454 * (rMouseWindowPosition.Y() - aWindowSize.Height()
455 + maScrollBorder.Height()));
459 maAutoScrollOffset = Size(nDx,nDy);
462 bool ScrollBarManager::AutoScroll (
463 const Point& rMouseWindowPosition,
464 const ::std::function<void ()>& rAutoScrollFunctor)
466 maAutoScrollFunctor = rAutoScrollFunctor;
467 CalcAutoScrollOffset(rMouseWindowPosition);
468 bool bResult (true);
469 if ( ! mbIsAutoScrollActive)
470 bResult = RepeatAutoScroll();
472 return bResult;
475 void ScrollBarManager::StopAutoScroll()
477 maAutoScrollTimer.Stop();
478 mbIsAutoScrollActive = false;
481 bool ScrollBarManager::RepeatAutoScroll()
483 if (maAutoScrollOffset != Size(0,0))
485 if (mrSlideSorter.GetViewShell() != nullptr)
487 mrSlideSorter.GetViewShell()->Scroll(
488 maAutoScrollOffset.Width(),
489 maAutoScrollOffset.Height());
490 mrSlideSorter.GetView().InvalidatePageObjectVisibilities();
492 if (maAutoScrollFunctor)
493 maAutoScrollFunctor();
495 mbIsAutoScrollActive = true;
496 maAutoScrollTimer.Start();
498 return true;
502 clearAutoScrollFunctor();
503 mbIsAutoScrollActive = false;
504 return false;
507 void ScrollBarManager::clearAutoScrollFunctor()
509 maAutoScrollFunctor = ::std::function<void ()>();
512 IMPL_LINK_NOARG(ScrollBarManager, AutoScrollTimeoutHandler, Timer *, void)
514 RepeatAutoScroll();
517 void ScrollBarManager::Scroll(
518 const Orientation eOrientation,
519 const double nDistance)
521 bool bIsVertical (false);
522 switch (eOrientation)
524 case Orientation_Horizontal: bIsVertical = false; break;
525 case Orientation_Vertical: bIsVertical = true; break;
526 default:
527 OSL_ASSERT(eOrientation==Orientation_Horizontal || eOrientation==Orientation_Vertical);
528 return;
531 // Get current scrolling view position
532 // Fix bug where 1) scrolling view is partially scrolled, 2) view window is snapped from vertical
533 // to horizontal or vice-versa, then 3) mouse wheel scrolled. Scrolling broke because we got an
534 // invalid starting position from the scrollbar that's no longer displayed. Check scrollbars
535 // visible before getting position from each.
536 bool bHorizontalScrollShown = mpHorizontalScrollBar && mpHorizontalScrollBar->IsVisible();
537 bool bVerticalScrollShown = mpVerticalScrollBar && mpVerticalScrollBar->IsVisible();
538 Point aNewTopLeft(
539 bHorizontalScrollShown ? mpHorizontalScrollBar->GetThumbPos() : 0,
540 bVerticalScrollShown ? mpVerticalScrollBar->GetThumbPos() : 0);
542 view::Layouter& rLayouter (mrSlideSorter.GetView().GetLayouter());
544 // Calculate new location
545 Size objectSize( rLayouter.GetPageObjectSize() ); // Size of a page in pane
546 Size objectAndGapSize = view::Layouter::AddGap(objectSize); // Add gap between pages to size
547 if (bIsVertical)
548 aNewTopLeft.AdjustY(nDistance * objectAndGapSize.Height() ); // New position
549 else
550 aNewTopLeft.AdjustX(nDistance * objectAndGapSize.Width() ); // New position
552 // Get displayable area for pages
553 ::tools::Rectangle scrollArea = rLayouter.GetTotalBoundingBox();
555 // Prevent scrolling out of bounds by limiting scroll destination point.
556 if (bIsVertical)
558 // Subtract size of view itself to get scrollable area.
559 ::tools::Long scrollbarSize = mpVerticalScrollBar->GetVisibleSize();
560 // Prevent (-) height. std::max needs type specified explicitly so params match.
561 ::tools::Long scrollHeight = std::max(static_cast<::tools::Long>(0), scrollArea.GetHeight() - scrollbarSize);
562 scrollArea.SetHeight(scrollHeight);
563 // Constrain scroll point to valid area
564 ::tools::Long constrainedY = std::clamp(aNewTopLeft.getY(), scrollArea.Top(), scrollArea.Bottom());
565 aNewTopLeft.setY(constrainedY);
567 else
569 // Subtract size of view itself to get scrollable area.
570 ::tools::Long scrollbarSize = mpHorizontalScrollBar->GetVisibleSize();
571 // Prevent (-) width. std::max needs type specified explicitly so params match.
572 ::tools::Long scrollWidth = std::max(static_cast<::tools::Long>(0), scrollArea.GetWidth() - scrollbarSize);
573 scrollArea.SetWidth(scrollWidth);
574 // Constrain scroll point to valid area
575 ::tools::Long constrainedX = std::clamp(aNewTopLeft.getX(), scrollArea.Left(), scrollArea.Right());
576 aNewTopLeft.setX(constrainedX);
579 mrSlideSorter.GetController().GetVisibleAreaManager().DeactivateCurrentSlideTracking();
580 SetTopLeft(aNewTopLeft);
583 } // end of namespace ::sd::slidesorter::controller
585 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */