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