1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sidebar/DeckLayouter.hxx>
21 #include <sidebar/DeckTitleBar.hxx>
22 #include <sidebar/PanelTitleBar.hxx>
23 #include <sfx2/sidebar/Deck.hxx>
24 #include <sfx2/sidebar/Panel.hxx>
25 #include <sfx2/sidebar/Theme.hxx>
26 #include <sfx2/sidebar/SidebarDockingWindow.hxx>
27 #include <sfx2/sidebar/SidebarController.hxx>
28 #include <sfx2/viewsh.hxx>
29 #include <comphelper/lok.hxx>
31 #include <comphelper/processfactory.hxx>
33 #include <com/sun/star/uno/Reference.hxx>
34 #include <com/sun/star/frame/Desktop.hpp>
35 #include <com/sun/star/frame/XDesktop2.hpp>
36 #include <com/sun/star/frame/XFrame.hpp>
37 #include <com/sun/star/ui/XSidebarPanel.hpp>
39 #include <vcl/jsdialog/executor.hxx>
42 using namespace css::uno
;
44 namespace sfx2::sidebar
{
47 const sal_Int32
MinimalPanelHeight (25);
58 std::shared_ptr
<Panel
> mpPanel
;
59 css::ui::LayoutSize maLayoutSize
;
60 sal_Int32 mnDistributedHeight
;
64 LayoutItem(const std::shared_ptr
<Panel
>& pPanel
)
66 , maLayoutSize(0, 0, 0)
67 , mnDistributedHeight(0)
69 , mbShowTitleBar(true)
74 const tools::Rectangle
& rContentArea
,
75 sal_Int32
& rMinimalWidth
,
76 sal_Int32
& rMinimalHeight
,
77 ::std::vector
<LayoutItem
>& rLayoutItems
,
78 weld::ScrolledWindow
& pVerticalScrollBar
,
79 const bool bShowVerticalScrollBar
);
80 void GetRequestedSizes (
81 ::std::vector
<LayoutItem
>& rLayoutItem
,
82 sal_Int32
& rAvailableHeight
,
83 sal_Int32
& rMinimalWidth
,
84 const tools::Rectangle
& rContentBox
);
85 void DistributeHeights (
86 ::std::vector
<LayoutItem
>& rLayoutItems
,
87 const sal_Int32 nHeightToDistribute
,
88 const sal_Int32 nContainerHeight
,
89 const bool bMinimumHeightIsBase
);
90 sal_Int32
PlacePanels (
91 ::std::vector
<LayoutItem
>& rLayoutItems
,
92 const LayoutMode eMode_
);
93 tools::Rectangle
PlaceDeckTitle (
94 const SidebarDockingWindow
* pDockingWindow
,
95 DeckTitleBar
& rTitleBar
,
96 const tools::Rectangle
& rAvailableSpace
);
97 tools::Rectangle
PlaceVerticalScrollBar (
98 weld::ScrolledWindow
& rVerticalScrollBar
,
99 const tools::Rectangle
& rAvailableSpace
,
100 const bool bShowVerticalScrollBar
);
101 void SetupVerticalScrollBar(
102 weld::ScrolledWindow
& rVerticalScrollBar
,
103 const sal_Int32 nContentHeight
,
104 const sal_Int32 nVisibleHeight
);
107 void DeckLayouter::LayoutDeck (
108 const SidebarDockingWindow
* pDockingWindow
,
109 const tools::Rectangle
& rContentArea
,
110 sal_Int32
& rMinimalWidth
,
111 sal_Int32
& rMinimalHeight
,
112 SharedPanelContainer
& rPanels
,
113 DeckTitleBar
& rDeckTitleBar
,
114 weld::ScrolledWindow
& rVerticalScrollBar
)
116 if (rContentArea
.GetWidth()<=0 || rContentArea
.GetHeight()<=0)
118 tools::Rectangle
aBox(PlaceDeckTitle(pDockingWindow
, rDeckTitleBar
, rContentArea
));
120 if ( rPanels
.empty())
123 // Prepare the layout item container.
124 ::std::vector
<LayoutItem
> aLayoutItems
;
125 aLayoutItems
.reserve(rPanels
.size());
126 for (auto& rPanel
: rPanels
)
127 aLayoutItems
.emplace_back(rPanel
);
141 const tools::Rectangle
& rContentArea
,
142 sal_Int32
& rMinimalWidth
,
143 sal_Int32
& rMinimalHeight
,
144 ::std::vector
<LayoutItem
>& rLayoutItems
,
145 weld::ScrolledWindow
& rVerticalScrollBar
,
146 const bool bShowVerticalScrollBar
)
148 tools::Rectangle
aBox (PlaceVerticalScrollBar(rVerticalScrollBar
, rContentArea
, bShowVerticalScrollBar
));
150 // Get the requested heights of the panels and the available
151 // height that is left when all panel titles and separators are
152 // taken into account.
153 sal_Int32
nAvailableHeight (aBox
.GetHeight());
154 GetRequestedSizes(rLayoutItems
, nAvailableHeight
, rMinimalWidth
, aBox
);
155 const sal_Int32
nTotalDecorationHeight (aBox
.GetHeight() - nAvailableHeight
);
157 // Analyze the requested heights.
158 // Determine the height that is available for panel content
159 // and count the different layouts.
160 sal_Int32
nTotalPreferredHeight (0);
161 sal_Int32
nTotalMinimumHeight (0);
163 for (const auto& rItem
: rLayoutItems
)
165 nTotalMinimumHeight
+= rItem
.maLayoutSize
.Minimum
;
166 nTotalPreferredHeight
+= rItem
.maLayoutSize
.Preferred
;
169 if (nTotalMinimumHeight
> nAvailableHeight
&& !bShowVerticalScrollBar
170 && !comphelper::LibreOfficeKit::isActive())
172 // Not enough space, even when all panels are shrunk to their
174 // Show a vertical scrollbar.
185 // We are now in one of three modes.
186 // - The preferred height fits into the available size:
187 // Use the preferred size, distribute the remaining height by
189 // - The total minimum height fits into the available size:
190 // Use the minimum size, distribute the remaining height by
192 // - The total minimum height does not fit into the available
194 // Use the unmodified preferred height for all panels.
196 LayoutMode
eMode(MinimumOrLarger
);
197 if (bShowVerticalScrollBar
)
201 const sal_Int32
nContentHeight(nTotalPreferredHeight
+ nTotalDecorationHeight
);
202 SetupVerticalScrollBar(rVerticalScrollBar
, nContentHeight
, aBox
.GetHeight());
206 if (nTotalPreferredHeight
<= nAvailableHeight
)
207 eMode
= PreferredOrLarger
;
209 eMode
= MinimumOrLarger
;
211 const sal_Int32
nTotalHeight (eMode
==MinimumOrLarger
? nTotalMinimumHeight
: nTotalPreferredHeight
);
215 nAvailableHeight
-nTotalHeight
,
217 eMode
==MinimumOrLarger
);
220 const sal_Int32
nUsedHeight(PlacePanels(rLayoutItems
, eMode
));
221 rMinimalHeight
= nUsedHeight
;
224 sal_Int32
PlacePanels (
225 ::std::vector
<LayoutItem
>& rLayoutItems
,
226 const LayoutMode eMode
)
228 const sal_Int32
nDeckSeparatorHeight (Theme::GetInteger(Theme::Int_DeckSeparatorHeight
));
231 // Assign heights and places.
232 for(::std::vector
<LayoutItem
>::const_iterator
iItem(rLayoutItems
.begin()),
233 iEnd(rLayoutItems
.end());
240 Panel
& rPanel (*iItem
->mpPanel
);
242 rPanel
.set_margin_top(nDeckSeparatorHeight
);
243 rPanel
.set_margin_bottom(0);
245 // Separator above the panel title bar.
246 if (!rPanel
.IsLurking())
248 nY
+= nDeckSeparatorHeight
;
251 bool bShowTitlebar
= iItem
->mbShowTitleBar
;
252 PanelTitleBar
* pTitleBar
= rPanel
.GetTitleBar();
253 pTitleBar
->Show(bShowTitlebar
);
254 rPanel
.set_vexpand(!bShowTitlebar
);
255 weld::Container
* pContents
= rPanel
.GetContents();
256 pContents
->set_vexpand(true);
258 bool bExpanded
= rPanel
.IsExpanded() && !rPanel
.IsLurking();
259 if (bShowTitlebar
|| bExpanded
)
263 sal_Int32
nPanelHeight(0);
266 // Determine the height of the panel depending on layout
267 // mode and distributed heights.
270 case MinimumOrLarger
:
271 nPanelHeight
= iItem
->maLayoutSize
.Minimum
+ iItem
->mnDistributedHeight
;
273 case PreferredOrLarger
:
274 nPanelHeight
= iItem
->maLayoutSize
.Preferred
+ iItem
->mnDistributedHeight
;
277 nPanelHeight
= iItem
->maLayoutSize
.Preferred
;
285 nPanelHeight
+= pTitleBar
->get_preferred_size().Height();
287 rPanel
.SetHeightPixel(nPanelHeight
);
298 // Add a separator below the collapsed panel, if it is the
299 // last panel in the deck.
300 if (iItem
== rLayoutItems
.end()-1)
302 // Separator below the panel title bar.
303 rPanel
.set_margin_bottom(nDeckSeparatorHeight
);
304 nY
+= nDeckSeparatorHeight
;
309 if (comphelper::LibreOfficeKit::isActive())
311 sal_uInt64 nShellId
= reinterpret_cast<sal_uInt64
>(SfxViewShell::Current());
312 jsdialog::SendFullUpdate(std::to_string(nShellId
) + "sidebar", "Panel");
318 void GetRequestedSizes (
319 ::std::vector
<LayoutItem
>& rLayoutItems
,
320 sal_Int32
& rAvailableHeight
,
321 sal_Int32
& rMinimalWidth
,
322 const tools::Rectangle
& rContentBox
)
324 rAvailableHeight
= rContentBox
.GetHeight();
326 const sal_Int32
nDeckSeparatorHeight (Theme::GetInteger(Theme::Int_DeckSeparatorHeight
));
328 for (auto& rItem
: rLayoutItems
)
330 rItem
.maLayoutSize
= ui::LayoutSize(0,0,0);
332 if (rItem
.mpPanel
== nullptr)
335 if (rItem
.mpPanel
->IsLurking())
337 rItem
.mbShowTitleBar
= false;
341 if (rLayoutItems
.size() == 1
342 && rItem
.mpPanel
->IsTitleBarOptional())
344 // There is only one panel and its title bar is
345 // optional => hide it.
346 rAvailableHeight
-= nDeckSeparatorHeight
;
347 rItem
.mbShowTitleBar
= false;
351 // Show the title bar and a separator above and below
353 PanelTitleBar
* pTitleBar
= rItem
.mpPanel
->GetTitleBar();
354 const sal_Int32 nPanelTitleBarHeight
= pTitleBar
->get_preferred_size().Height();
356 rAvailableHeight
-= nPanelTitleBarHeight
;
357 rAvailableHeight
-= nDeckSeparatorHeight
;
360 if (rItem
.mpPanel
->IsExpanded() && rItem
.mpPanel
->GetPanelComponent().is())
362 Reference
<ui::XSidebarPanel
> xPanel (rItem
.mpPanel
->GetPanelComponent());
364 rItem
.maLayoutSize
= xPanel
->getHeightForWidth(rContentBox
.GetWidth());
365 if (!(0 <= rItem
.maLayoutSize
.Minimum
&& rItem
.maLayoutSize
.Minimum
<= rItem
.maLayoutSize
.Preferred
366 && rItem
.maLayoutSize
.Preferred
<= rItem
.maLayoutSize
.Maximum
))
368 SAL_INFO("sfx.sidebar", "Please follow LayoutSize constraints: 0 ≤ "
369 "Minimum ≤ Preferred ≤ Maximum."
370 " Currently: Minimum: "
371 << rItem
.maLayoutSize
.Minimum
372 << " Preferred: " << rItem
.maLayoutSize
.Preferred
373 << " Maximum: " << rItem
.maLayoutSize
.Maximum
);
376 sal_Int32 nWidth
= rMinimalWidth
;
379 // The demo sidebar extension "Analog Clock" fails with
380 // java.lang.AbstractMethodError here
381 nWidth
= xPanel
->getMinimalWidth();
387 uno::Reference
<frame::XDesktop2
> xDesktop
388 = frame::Desktop::create(comphelper::getProcessComponentContext());
389 uno::Reference
<frame::XFrame
> xFrame
= xDesktop
->getActiveFrame();
392 SidebarController
* pController
393 = SidebarController::GetSidebarControllerForFrame(xFrame
);
394 if (pController
&& pController
->getMaximumWidth() < nWidth
)
396 // Add 100 extra pixels to still have the sidebar resizable
397 // (See also documentation of XSidebarPanel::getMinimalWidth)
398 pController
->setMaximumWidth(nWidth
+ 100);
402 if (nWidth
> rMinimalWidth
)
403 rMinimalWidth
= nWidth
;
406 rItem
.maLayoutSize
= ui::LayoutSize(MinimalPanelHeight
, -1, 0);
410 void DistributeHeights (
411 ::std::vector
<LayoutItem
>& rLayoutItems
,
412 const sal_Int32 nHeightToDistribute
,
413 const sal_Int32 nContainerHeight
,
414 const bool bMinimumHeightIsBase
)
416 if (nHeightToDistribute
<= 0)
419 sal_Int32
nRemainingHeightToDistribute (nHeightToDistribute
);
421 // Compute the weights as difference between panel base height
422 // (either its minimum or preferred height) and the container height.
423 sal_Int32
nTotalWeight (0);
424 sal_Int32
nNoMaximumCount (0);
426 for (auto& rItem
: rLayoutItems
)
428 if (rItem
.maLayoutSize
.Maximum
== 0)
430 if (rItem
.maLayoutSize
.Maximum
< 0)
433 const sal_Int32
nBaseHeight (
435 ? rItem
.maLayoutSize
.Minimum
436 : rItem
.maLayoutSize
.Preferred
);
437 if (nBaseHeight
< nContainerHeight
)
439 rItem
.mnWeight
= nContainerHeight
- nBaseHeight
;
440 nTotalWeight
+= rItem
.mnWeight
;
444 if (nTotalWeight
== 0)
447 // First pass of height distribution.
448 for (auto& rItem
: rLayoutItems
)
450 const sal_Int32
nBaseHeight (
452 ? rItem
.maLayoutSize
.Minimum
453 : rItem
.maLayoutSize
.Preferred
);
454 sal_Int32
nDistributedHeight (rItem
.mnWeight
* nHeightToDistribute
/ nTotalWeight
);
455 if (nBaseHeight
+nDistributedHeight
> rItem
.maLayoutSize
.Maximum
456 && rItem
.maLayoutSize
.Maximum
>= 0)
458 nDistributedHeight
= ::std::max
<sal_Int32
>(0, rItem
.maLayoutSize
.Maximum
- nBaseHeight
);
460 rItem
.mnDistributedHeight
= nDistributedHeight
;
461 nRemainingHeightToDistribute
-= nDistributedHeight
;
464 if (nRemainingHeightToDistribute
== 0)
466 OSL_ASSERT(nRemainingHeightToDistribute
> 0);
468 // It is possible that not all of the height could be distributed
469 // because of Maximum heights being smaller than expected.
470 // Distribute the remaining height between the panels that have no
471 // Maximum (ie Maximum==-1).
472 if (nNoMaximumCount
== 0)
474 // There are no panels with unrestricted height.
478 const sal_Int32
nAdditionalHeightPerPanel(nRemainingHeightToDistribute
/ nNoMaximumCount
);
479 // Handle rounding error.
480 sal_Int32
nAdditionalHeightForFirstPanel (nRemainingHeightToDistribute
481 - nNoMaximumCount
*nAdditionalHeightPerPanel
);
483 for (auto& rItem
: rLayoutItems
)
485 if (rItem
.maLayoutSize
.Maximum
< 0)
487 rItem
.mnDistributedHeight
+= nAdditionalHeightPerPanel
+ nAdditionalHeightForFirstPanel
;
488 nRemainingHeightToDistribute
-= nAdditionalHeightPerPanel
+ nAdditionalHeightForFirstPanel
;
492 OSL_ASSERT(nRemainingHeightToDistribute
==0);
495 tools::Rectangle
PlaceDeckTitle(
496 const SidebarDockingWindow
* pDockingWindow
,
497 DeckTitleBar
& rDeckTitleBar
,
498 const tools::Rectangle
& rAvailableSpace
)
500 if (pDockingWindow
->IsFloatingMode())
502 // When the side bar is undocked then the outer system window displays the deck title.
503 rDeckTitleBar
.Show(false);
504 return rAvailableSpace
;
508 rDeckTitleBar
.Show(true);
509 const sal_Int32
nDeckTitleBarHeight(rDeckTitleBar
.get_preferred_size().Height());
510 return tools::Rectangle(
511 rAvailableSpace
.Left(),
512 rAvailableSpace
.Top() + nDeckTitleBarHeight
,
513 rAvailableSpace
.Right(),
514 rAvailableSpace
.Bottom());
518 tools::Rectangle
PlaceVerticalScrollBar (
519 weld::ScrolledWindow
& rVerticalScrollBar
,
520 const tools::Rectangle
& rAvailableSpace
,
521 const bool bShowVerticalScrollBar
)
523 if (bShowVerticalScrollBar
)
525 const sal_Int32
nScrollBarWidth(rVerticalScrollBar
.get_scroll_thickness());
526 rVerticalScrollBar
.set_vpolicy(VclPolicyType::ALWAYS
);
527 return tools::Rectangle(
528 rAvailableSpace
.Left(),
529 rAvailableSpace
.Top(),
530 rAvailableSpace
.Right() - nScrollBarWidth
,
531 rAvailableSpace
.Bottom());
535 rVerticalScrollBar
.set_vpolicy(VclPolicyType::NEVER
);
536 return rAvailableSpace
;
540 void SetupVerticalScrollBar(
541 weld::ScrolledWindow
& rVerticalScrollBar
,
542 const sal_Int32 nContentHeight
,
543 const sal_Int32 nVisibleHeight
)
545 OSL_ASSERT(nContentHeight
> nVisibleHeight
);
547 rVerticalScrollBar
.vadjustment_set_upper(nContentHeight
-1);
548 rVerticalScrollBar
.vadjustment_set_page_size(nVisibleHeight
);
553 } // end of namespace sfx2::sidebar
555 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */