1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "DeckLayouter.hxx"
25 #include "sfx2/sidebar/Theme.hxx"
26 #include "Panel.hxx"
27 #include "TitleBar.hxx"
28 #include "Deck.hxx"
29 
30 #include <vcl/window.hxx>
31 #include <vcl/scrbar.hxx>
32 
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 
36 
37 namespace sfx2 { namespace sidebar {
38 
39 
40 namespace {
41     static const sal_Int32 MinimalPanelHeight (25);
42 }
43 
44 #define IterateLayoutItems(iterator_name,container)                     \
45     for(::std::vector<LayoutItem>::iterator                             \
46                    iterator_name(container.begin()),                    \
47                    iEnd(container.end());                               \
48         iterator_name!=iEnd;                                            \
49         ++iterator_name)
50 
51 
52 
53 void DeckLayouter::LayoutDeck (
54     const Rectangle aContentArea,
55     SharedPanelContainer& rPanels,
56     Window& rDeckTitleBar,
57     Window& rScrollClipWindow,
58     Window& rScrollContainer,
59     Window& rFiller,
60     ScrollBar& rVerticalScrollBar)
61 {
62     if (aContentArea.GetWidth()<=0 || aContentArea.GetHeight()<=0)
63         return;
64     Rectangle aBox (PlaceDeckTitle(rDeckTitleBar, aContentArea));
65 
66     if ( ! rPanels.empty())
67     {
68         // Prepare the layout item container.
69         ::std::vector<LayoutItem> aLayoutItems;
70         aLayoutItems.resize(rPanels.size());
71         for (sal_Int32 nIndex(0),nCount(rPanels.size()); nIndex<nCount; ++nIndex)
72         {
73             aLayoutItems[nIndex].mpPanel = rPanels[nIndex];
74             aLayoutItems[nIndex].mnPanelIndex = nIndex;
75         }
76         aBox = LayoutPanels(
77             aBox,
78             aLayoutItems,
79             rScrollClipWindow,
80             rScrollContainer,
81             rVerticalScrollBar,
82             false);
83     }
84     UpdateFiller(rFiller, aBox);
85 }
86 
87 
88 
89 
90 Rectangle DeckLayouter::LayoutPanels (
91     const Rectangle aContentArea,
92     ::std::vector<LayoutItem>& rLayoutItems,
93     Window& rScrollClipWindow,
94     Window& rScrollContainer,
95     ScrollBar& rVerticalScrollBar,
96     const bool bShowVerticalScrollBar)
97 {
98     Rectangle aBox (PlaceVerticalScrollBar(rVerticalScrollBar, aContentArea, bShowVerticalScrollBar));
99 
100     const sal_Int32 nWidth (aBox.GetWidth());
101     // const sal_Int32 nPanelTitleBarHeight (Theme::GetInteger(Theme::Int_PanelTitleBarHeight));
102 
103     // Prepare the separators, horizontal lines above and below the
104     // panel titels.
105     // const sal_Int32 nDeckSeparatorHeight (Theme::GetInteger(Theme::Int_DeckSeparatorHeight));
106 
107     // Get the requested heights of the panels and the available
108     // height that is left when all panel titles and separators are
109     // taken into account.
110     sal_Int32 nAvailableHeight (aBox.GetHeight());
111     GetRequestedSizes(rLayoutItems, nAvailableHeight, aBox);
112     const sal_Int32 nTotalDecorationHeight (aBox.GetHeight() - nAvailableHeight);
113 
114     // Analyze the requested heights.
115     // Determine the height that is available for panel content
116     // and count the different layouts.
117     sal_Int32 nTotalPreferredHeight (0);
118     sal_Int32 nTotalMinimumHeight (0);
119     IterateLayoutItems(iItem,rLayoutItems)
120     {
121         nTotalMinimumHeight += iItem->maLayoutSize.Minimum;
122         nTotalPreferredHeight += iItem->maLayoutSize.Preferred;
123     }
124 
125     if (nTotalMinimumHeight > nAvailableHeight
126         && ! bShowVerticalScrollBar)
127     {
128         // Not enough space, even when all panels are shrunk to their
129         // minimum height.
130         // Show a vertical scrollbar.
131         return LayoutPanels(
132             aContentArea,
133             rLayoutItems,
134             rScrollClipWindow,
135             rScrollContainer,
136             rVerticalScrollBar,
137             true);
138     }
139 
140     // We are now in one of three modes.
141     // - The preferred height fits into the available size:
142     //   Use the preferred size, distribute the remaining height bei
143     //   enlarging panels.
144     // - The total minimum height fits into the available size:
145     //   Use the minimum size, distribute the remaining height bei
146     //   enlarging panels.
147     // - The total minimum height does not fit into the available
148     //   size:
149     //   Use the unmodified preferred height for all panels.
150 
151     LayoutMode eMode (MinimumOrLarger);
152     if (bShowVerticalScrollBar)
153         eMode = Preferred;
154     else if (nTotalPreferredHeight <= nAvailableHeight)
155         eMode = PreferredOrLarger;
156     else
157         eMode = MinimumOrLarger;
158 
159     if (eMode != Preferred)
160     {
161         const sal_Int32 nTotalHeight (eMode==MinimumOrLarger ? nTotalMinimumHeight : nTotalPreferredHeight);
162 
163         DistributeHeights(
164             rLayoutItems,
165             nAvailableHeight-nTotalHeight,
166             aBox.GetHeight(),
167             eMode==MinimumOrLarger);
168     }
169 
170     // Set position and size of the mpScrollClipWindow to the available
171     // size.  Its child, the mpScrollContainer, may have a bigger
172     // height.
173     rScrollClipWindow.SetPosSizePixel(aBox.Left(), aBox.Top(), aBox.GetWidth(), aBox.GetHeight());
174 
175     const sal_Int32 nContentHeight (
176         eMode==Preferred
177             ? nTotalPreferredHeight + nTotalDecorationHeight
178             : aBox.GetHeight());
179     sal_Int32 nY = rVerticalScrollBar.GetThumbPos();
180     if (nContentHeight-nY < aBox.GetHeight())
181         nY = nContentHeight-aBox.GetHeight();
182     if (nY < 0)
183         nY = 0;
184     rScrollContainer.SetPosSizePixel(
185         0,
186         -nY,
187         nWidth,
188         nContentHeight);
189 
190     if (bShowVerticalScrollBar)
191         SetupVerticalScrollBar(rVerticalScrollBar, nContentHeight, aBox.GetHeight());
192 
193     const sal_Int32 nUsedHeight (PlacePanels(rLayoutItems, nWidth, eMode, rScrollContainer));
194     aBox.Top() += nUsedHeight;
195     return aBox;
196 }
197 
198 
199 
200 
201 sal_Int32 DeckLayouter::PlacePanels (
202     ::std::vector<LayoutItem>& rLayoutItems,
203     const sal_Int32 nWidth,
204     const LayoutMode eMode,
205     Window& rScrollContainer)
206 {
207     ::std::vector<sal_Int32> aSeparators;
208     const sal_Int32 nDeckSeparatorHeight (Theme::GetInteger(Theme::Int_DeckSeparatorHeight));
209     const sal_Int32 nPanelTitleBarHeight (Theme::GetInteger(Theme::Int_PanelTitleBarHeight));
210     sal_Int32 nY (0);
211 
212     // Assign heights and places.
213     IterateLayoutItems(iItem,rLayoutItems)
214     {
215         if (iItem->mpPanel == NULL)
216             continue;
217 
218         Panel& rPanel (*iItem->mpPanel);
219 
220         // Separator above the panel title bar.
221         aSeparators.push_back(nY);
222         nY += nDeckSeparatorHeight;
223 
224         // Place the title bar.
225         TitleBar* pTitleBar = rPanel.GetTitleBar();
226         if (pTitleBar != NULL)
227         {
228             if (iItem->mbShowTitleBar)
229             {
230                 pTitleBar->SetPosSizePixel(0, nY, nWidth, nPanelTitleBarHeight);
231                 pTitleBar->Show();
232                 nY += nPanelTitleBarHeight;
233             }
234             else
235             {
236                 pTitleBar->Hide();
237             }
238         }
239 
240         if (rPanel.IsExpanded())
241         {
242             rPanel.Show();
243 
244             // Determine the height of the panel depending on layout
245             // mode and distributed heights.
246             sal_Int32 nPanelHeight (0);
247             switch(eMode)
248             {
249                 case MinimumOrLarger:
250                     nPanelHeight = iItem->maLayoutSize.Minimum + iItem->mnDistributedHeight;
251                     break;
252                 case PreferredOrLarger:
253                     nPanelHeight = iItem->maLayoutSize.Preferred + iItem->mnDistributedHeight;
254                     break;
255                 case Preferred:
256                     nPanelHeight = iItem->maLayoutSize.Preferred;
257                     break;
258                 default:
259                     OSL_ASSERT(false);
260                     break;
261             }
262 
263             // Place the panel.
264             rPanel.SetPosSizePixel(0, nY, nWidth, nPanelHeight);
265 
266             nY += nPanelHeight;
267         }
268         else
269         {
270             rPanel.Hide();
271 
272             // Add a separator below the collapsed panel, if it is the
273             // last panel in the deck.
274             if (iItem == rLayoutItems.end()-1)
275             {
276                 // Separator below the panel title bar.
277                 aSeparators.push_back(nY);
278                 nY += nDeckSeparatorHeight;
279             }
280         }
281     }
282 
283     Deck::ScrollContainerWindow* pScrollContainerWindow
284         = dynamic_cast<Deck::ScrollContainerWindow*>(&rScrollContainer);
285     if (pScrollContainerWindow != NULL)
286         pScrollContainerWindow->SetSeparators(aSeparators);
287 
288     return nY;
289 }
290 
291 
292 
293 
294 void DeckLayouter::GetRequestedSizes (
295     ::std::vector<LayoutItem>& rLayoutItems,
296     sal_Int32& rAvailableHeight,
297     const Rectangle& rContentBox)
298 {
299     rAvailableHeight = rContentBox.GetHeight();
300 
301     const sal_Int32 nPanelTitleBarHeight (Theme::GetInteger(Theme::Int_PanelTitleBarHeight));
302     const sal_Int32 nDeckSeparatorHeight (Theme::GetInteger(Theme::Int_DeckSeparatorHeight));
303 
304     IterateLayoutItems(iItem,rLayoutItems)
305     {
306         ui::LayoutSize aLayoutSize (ui::LayoutSize(0,0,0));
307         if (iItem->mpPanel != NULL)
308         {
309             if (rLayoutItems.size() == 1
310                 && iItem->mpPanel->IsTitleBarOptional())
311             {
312                 // There is only one panel and its title bar is
313                 // optional => hide it.
314                 rAvailableHeight -= nDeckSeparatorHeight;
315                 iItem->mbShowTitleBar = false;
316             }
317             else
318             {
319                 // Show the title bar and a separator above and below
320                 // the title bar.
321                 rAvailableHeight -= nPanelTitleBarHeight;
322                 rAvailableHeight -= nDeckSeparatorHeight;
323             }
324 
325             if (iItem->mpPanel->IsExpanded())
326             {
327                 Reference<ui::XSidebarPanel> xPanel (iItem->mpPanel->GetPanelComponent());
328                 if (xPanel.is())
329                     aLayoutSize = xPanel->getHeightForWidth(rContentBox.GetWidth());
330                 else
331                     aLayoutSize = ui::LayoutSize(MinimalPanelHeight, 0, -1);
332             }
333         }
334         iItem->maLayoutSize = aLayoutSize;
335     }
336 }
337 
338 
339 
340 
341 void DeckLayouter::DistributeHeights (
342     ::std::vector<LayoutItem>& rLayoutItems,
343     const sal_Int32 nHeightToDistribute,
344     const sal_Int32 nContainerHeight,
345     const bool bMinimumHeightIsBase)
346 {
347     if (nHeightToDistribute <= 0)
348         return;
349 
350     sal_Int32 nRemainingHeightToDistribute (nHeightToDistribute);
351 
352     // Compute the weights as difference between panel base height
353     // (either its minimum or preferred height) and the container height.
354     sal_Int32 nTotalWeight (0);
355     sal_Int32 nNoMaximumCount (0);
356     sal_Int32 nIndex (0);
357     IterateLayoutItems(iItem,rLayoutItems)
358     {
359         if (iItem->maLayoutSize.Maximum == 0)
360             continue;
361         if (iItem->maLayoutSize.Maximum < 0)
362             ++nNoMaximumCount;
363 
364         const sal_Int32 nBaseHeight (
365             bMinimumHeightIsBase
366                 ? iItem->maLayoutSize.Minimum
367                 : iItem->maLayoutSize.Preferred);
368         if (nBaseHeight < nContainerHeight)
369         {
370             iItem->mnWeight = nContainerHeight - nBaseHeight;
371             nTotalWeight += iItem->mnWeight;
372         }
373     }
374 
375 	if (nTotalWeight == 0)
376 		return;
377 
378     // First pass of height distribution.
379     nIndex = 0;
380     IterateLayoutItems(iItem,rLayoutItems)
381     {
382         const sal_Int32 nBaseHeight (
383             bMinimumHeightIsBase
384                 ? iItem->maLayoutSize.Minimum
385                 : iItem->maLayoutSize.Preferred);
386         sal_Int32 nDistributedHeight (iItem->mnWeight * nHeightToDistribute / nTotalWeight);
387         if (nBaseHeight+nDistributedHeight > iItem->maLayoutSize.Maximum
388             && iItem->maLayoutSize.Maximum >= 0)
389         {
390             nDistributedHeight = ::std::max<sal_Int32>(0,iItem->maLayoutSize.Maximum - nBaseHeight);
391         }
392         iItem->mnDistributedHeight = nDistributedHeight;
393         nRemainingHeightToDistribute -= nDistributedHeight;
394     }
395 
396     if (nRemainingHeightToDistribute == 0)
397         return;
398     OSL_ASSERT(nRemainingHeightToDistribute > 0);
399 
400     // It is possible that not all of the height could be distributed
401     // because of Maximum heights being smaller than expected.
402     // Distribute the remaining height between the panels that have no
403     // Maximum (ie Maximum==-1).
404     if (nNoMaximumCount == 0)
405     {
406         // There are no panels with unrestricted height.
407         return;
408     }
409     const sal_Int32 nAdditionalHeightPerPanel (nRemainingHeightToDistribute / nNoMaximumCount);
410     // Handle rounding error.
411     sal_Int32 nAdditionalHeightForFirstPanel (nRemainingHeightToDistribute
412         - nNoMaximumCount*nAdditionalHeightPerPanel);
413     nIndex = 0;
414     IterateLayoutItems(iItem,rLayoutItems)
415     {
416         if (iItem->maLayoutSize.Maximum < 0)
417         {
418             iItem->mnDistributedHeight += nAdditionalHeightPerPanel + nAdditionalHeightForFirstPanel;
419             nRemainingHeightToDistribute -= nAdditionalHeightPerPanel + nAdditionalHeightForFirstPanel;
420         }
421     }
422 
423     OSL_ASSERT(nRemainingHeightToDistribute==0);
424 }
425 
426 
427 
428 
429 Rectangle DeckLayouter::PlaceDeckTitle (
430     Window& rDeckTitleBar,
431     const Rectangle& rAvailableSpace)
432 {
433     if (static_cast<DockingWindow*>(rDeckTitleBar.GetParent()->GetParent())->IsFloatingMode())
434     {
435         // When the side bar is undocked then the outer system window displays the deck title.
436         rDeckTitleBar.Hide();
437         return rAvailableSpace;
438     }
439     else
440     {
441         const sal_Int32 nDeckTitleBarHeight (Theme::GetInteger(Theme::Int_DeckTitleBarHeight));
442         rDeckTitleBar.SetPosSizePixel(
443             rAvailableSpace.Left(),
444             rAvailableSpace.Top(),
445             rAvailableSpace.GetWidth(),
446             nDeckTitleBarHeight);
447         rDeckTitleBar.Show();
448         return Rectangle(
449             rAvailableSpace.Left(),
450             rAvailableSpace.Top() + nDeckTitleBarHeight,
451             rAvailableSpace.Right(),
452             rAvailableSpace.Bottom());
453     }
454 }
455 
456 
457 
458 
459 Rectangle DeckLayouter::PlaceVerticalScrollBar (
460     ScrollBar& rVerticalScrollBar,
461     const Rectangle& rAvailableSpace,
462     const bool bShowVerticalScrollBar)
463 {
464     if (bShowVerticalScrollBar)
465     {
466         const sal_Int32 nScrollBarWidth (rVerticalScrollBar.GetSizePixel().Width());
467         rVerticalScrollBar.SetPosSizePixel(
468             rAvailableSpace.Right() - nScrollBarWidth + 1,
469             rAvailableSpace.Top(),
470             nScrollBarWidth,
471             rAvailableSpace.GetHeight());
472         rVerticalScrollBar.Show();
473         return Rectangle(
474             rAvailableSpace.Left(),
475             rAvailableSpace.Top(),
476             rAvailableSpace.Right() - nScrollBarWidth,
477             rAvailableSpace.Bottom());
478     }
479     else
480     {
481         rVerticalScrollBar.Hide();
482         return rAvailableSpace;
483     }
484 }
485 
486 
487 
488 
489 void DeckLayouter::SetupVerticalScrollBar(
490     ScrollBar& rVerticalScrollBar,
491     const sal_Int32 nContentHeight,
492     const sal_Int32 nVisibleHeight)
493 {
494     OSL_ASSERT(nContentHeight > nVisibleHeight);
495 
496     rVerticalScrollBar.SetRangeMin(0);
497     rVerticalScrollBar.SetRangeMax(nContentHeight-1);
498     rVerticalScrollBar.SetVisibleSize(nVisibleHeight);
499 }
500 
501 
502 
503 
504 void DeckLayouter::UpdateFiller (
505     Window& rFiller,
506     const Rectangle& rBox)
507 {
508     if (rBox.GetHeight() > 0)
509     {
510         // Show the filler.
511         rFiller.SetBackground(Theme::GetPaint(Theme::Paint_PanelBackground).GetWallpaper());
512         rFiller.SetPosSizePixel(rBox.TopLeft(), rBox.GetSize());
513         rFiller.Show();
514     }
515     else
516     {
517         // Hide the filler.
518         rFiller.Hide();
519     }
520 }
521 
522 
523 
524 } } // end of namespace sfx2::sidebar
525