sdext: adapt xpdfwrapper to poppler 24.12
[LibreOffice.git] / vcl / source / control / PriorityHBox.cxx
blob5b4b19660c4c38dc35020c6478f72940607e6ff2
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 <vcl/layout.hxx>
21 #include <PriorityHBox.hxx>
22 #include <comphelper/lok.hxx>
24 namespace
26 bool lcl_comparePriority(const vcl::IPrioritable* a, const vcl::IPrioritable* b)
28 return a->GetPriority() < b->GetPriority();
32 PriorityHBox::PriorityHBox(vcl::Window* pParent)
33 : VclHBox(pParent)
34 , m_bInitialized(false)
38 PriorityHBox::~PriorityHBox() { disposeOnce(); }
40 void PriorityHBox::Initialize()
42 m_bInitialized = true;
44 GetChildrenWithPriorities();
45 SetSizeFromParent();
48 void PriorityHBox::SetSizeFromParent()
50 vcl::Window* pParent = GetParent();
51 if (pParent)
53 Size aParentSize = pParent->GetSizePixel();
54 SetSizePixel(Size(aParentSize.getWidth(), aParentSize.getHeight()));
58 Size PriorityHBox::calculateRequisition() const
60 if (!m_bInitialized)
62 return VclHBox::calculateRequisition();
65 sal_uInt16 nVisibleChildren = 0;
67 Size aSize;
68 for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
69 pChild = pChild->GetWindow(GetWindowType::Next))
71 if (!pChild->IsVisible())
72 continue;
73 ++nVisibleChildren;
74 Size aChildSize = getLayoutRequisition(*pChild);
76 bool bAlwaysExpanded = true;
78 vcl::IPrioritable* pPrioritable = dynamic_cast<vcl::IPrioritable*>(pChild);
79 if (pPrioritable && pPrioritable->GetPriority() != VCL_PRIORITY_DEFAULT)
80 bAlwaysExpanded = false;
82 if (bAlwaysExpanded)
84 tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
85 nPrimaryDimension += pChild->get_padding() * 2;
86 setPrimaryDimension(aChildSize, nPrimaryDimension);
88 else
89 setPrimaryDimension(aChildSize, 0);
91 accumulateMaxes(aChildSize, aSize);
94 return finalizeMaxes(aSize, nVisibleChildren);
97 void PriorityHBox::Resize()
99 if (!m_bInitialized)
100 Initialize();
102 if (!m_bInitialized || comphelper::LibreOfficeKit::isActive())
104 return VclHBox::Resize();
107 tools::Long nWidth = GetSizePixel().Width();
108 tools::Long nCurrentWidth = VclHBox::calculateRequisition().getWidth();
110 // Hide lower priority controls
111 for (vcl::IPrioritable* pPrioritable : m_aSortedChildren)
113 if (nCurrentWidth <= nWidth)
114 break;
116 vcl::Window* pWindow = dynamic_cast<vcl::Window*>(pPrioritable);
118 if (pWindow && pWindow->GetParent() == this)
120 nCurrentWidth -= pWindow->GetOutputSizePixel().Width() + get_spacing();
121 pWindow->Show();
122 pPrioritable->HideContent();
123 nCurrentWidth += pWindow->GetOutputSizePixel().Width() + get_spacing();
127 auto pChildR = m_aSortedChildren.rbegin();
128 // Show higher priority controls if we already have enough space
129 while (pChildR != m_aSortedChildren.rend())
131 vcl::Window* pWindow = dynamic_cast<vcl::Window*>(*pChildR);
132 vcl::IPrioritable* pPrioritable = *pChildR;
134 if (pWindow->GetParent() != this)
136 pChildR++;
137 continue;
140 if (pWindow)
142 nCurrentWidth -= pWindow->GetOutputSizePixel().Width() + get_spacing();
143 pWindow->Show();
144 pPrioritable->ShowContent();
145 nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
147 if (nCurrentWidth > nWidth)
149 pPrioritable->HideContent();
150 break;
154 pChildR++;
157 VclHBox::Resize();
160 void PriorityHBox::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
162 if (!m_bInitialized)
163 Initialize();
165 VclHBox::Paint(rRenderContext, rRect);
168 void PriorityHBox::GetChildrenWithPriorities()
170 for (sal_uInt16 i = 0; i < GetChildCount(); ++i)
172 vcl::Window* pChild = GetChild(i);
174 // Add only containers which have explicitly assigned priority.
175 vcl::IPrioritable* pPrioritable = dynamic_cast<vcl::IPrioritable*>(pChild);
176 if (pPrioritable && pPrioritable->GetPriority() != VCL_PRIORITY_DEFAULT)
177 m_aSortedChildren.push_back(pPrioritable);
180 if (m_aSortedChildren.empty())
181 m_bInitialized = false;
183 std::sort(m_aSortedChildren.begin(), m_aSortedChildren.end(), lcl_comparePriority);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */