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 <vcl/layout.hxx>
21 #include <PriorityHBox.hxx>
22 #include <comphelper/lok.hxx>
26 bool lcl_comparePriority(const vcl::IPrioritable
* a
, const vcl::IPrioritable
* b
)
28 return a
->GetPriority() < b
->GetPriority();
32 PriorityHBox::PriorityHBox(vcl::Window
* pParent
)
34 , m_bInitialized(false)
38 PriorityHBox::~PriorityHBox() { disposeOnce(); }
40 void PriorityHBox::Initialize()
42 m_bInitialized
= true;
44 GetChildrenWithPriorities();
48 int PriorityHBox::GetHiddenCount() const
52 for (auto pWindow
: m_aSortedChildren
)
53 if (pWindow
->IsHidden())
59 void PriorityHBox::SetSizeFromParent()
61 vcl::Window
* pParent
= GetParent();
64 Size aParentSize
= pParent
->GetSizePixel();
65 SetSizePixel(Size(aParentSize
.getWidth(), aParentSize
.getHeight()));
69 Size
PriorityHBox::calculateRequisition() const
73 return VclHBox::calculateRequisition();
76 sal_uInt16 nVisibleChildren
= 0;
79 for (vcl::Window
* pChild
= GetWindow(GetWindowType::FirstChild
); pChild
;
80 pChild
= pChild
->GetWindow(GetWindowType::Next
))
82 if (!pChild
->IsVisible())
85 Size aChildSize
= getLayoutRequisition(*pChild
);
87 bool bAlwaysExpanded
= true;
89 vcl::IPrioritable
* pPrioritable
= dynamic_cast<vcl::IPrioritable
*>(pChild
);
90 if (pPrioritable
&& pPrioritable
->GetPriority() != VCL_PRIORITY_DEFAULT
)
91 bAlwaysExpanded
= false;
95 tools::Long nPrimaryDimension
= getPrimaryDimension(aChildSize
);
96 nPrimaryDimension
+= pChild
->get_padding() * 2;
97 setPrimaryDimension(aChildSize
, nPrimaryDimension
);
100 setPrimaryDimension(aChildSize
, 0);
102 accumulateMaxes(aChildSize
, aSize
);
105 return finalizeMaxes(aSize
, nVisibleChildren
);
108 void PriorityHBox::Resize()
113 if (!m_bInitialized
|| comphelper::LibreOfficeKit::isActive())
115 return VclHBox::Resize();
118 tools::Long nWidth
= GetSizePixel().Width();
119 tools::Long nCurrentWidth
= VclHBox::calculateRequisition().getWidth();
121 // Hide lower priority controls
122 for (vcl::IPrioritable
* pPrioritable
: m_aSortedChildren
)
124 if (nCurrentWidth
<= nWidth
)
127 vcl::Window
* pWindow
= dynamic_cast<vcl::Window
*>(pPrioritable
);
129 if (pWindow
&& pWindow
->GetParent() == this)
131 nCurrentWidth
-= pWindow
->GetOutputSizePixel().Width() + get_spacing();
133 pPrioritable
->HideContent();
134 nCurrentWidth
+= pWindow
->GetOutputSizePixel().Width() + get_spacing();
138 auto pChildR
= m_aSortedChildren
.rbegin();
139 // Show higher priority controls if we already have enough space
140 while (pChildR
!= m_aSortedChildren
.rend())
142 vcl::Window
* pWindow
= dynamic_cast<vcl::Window
*>(*pChildR
);
143 vcl::IPrioritable
* pPrioritable
= *pChildR
;
145 if (pWindow
->GetParent() != this)
153 nCurrentWidth
-= pWindow
->GetOutputSizePixel().Width() + get_spacing();
155 pPrioritable
->ShowContent();
156 nCurrentWidth
+= getLayoutRequisition(*pWindow
).Width() + get_spacing();
158 if (nCurrentWidth
> nWidth
)
160 pPrioritable
->HideContent();
171 void PriorityHBox::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rRect
)
176 VclHBox::Paint(rRenderContext
, rRect
);
179 void PriorityHBox::GetChildrenWithPriorities()
181 for (sal_uInt16 i
= 0; i
< GetChildCount(); ++i
)
183 vcl::Window
* pChild
= GetChild(i
);
185 // Add only containers which have explicitly assigned priority.
186 vcl::IPrioritable
* pPrioritable
= dynamic_cast<vcl::IPrioritable
*>(pChild
);
187 if (pPrioritable
&& pPrioritable
->GetPriority() != VCL_PRIORITY_DEFAULT
)
188 m_aSortedChildren
.push_back(pPrioritable
);
191 if (m_aSortedChildren
.empty())
192 m_bInitialized
= false;
194 std::sort(m_aSortedChildren
.begin(), m_aSortedChildren
.end(), lcl_comparePriority
);
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */