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 void PriorityHBox::SetSizeFromParent()
50 vcl::Window
* pParent
= GetParent();
53 Size aParentSize
= pParent
->GetSizePixel();
54 SetSizePixel(Size(aParentSize
.getWidth(), aParentSize
.getHeight()));
58 Size
PriorityHBox::calculateRequisition() const
62 return VclHBox::calculateRequisition();
65 sal_uInt16 nVisibleChildren
= 0;
68 for (vcl::Window
* pChild
= GetWindow(GetWindowType::FirstChild
); pChild
;
69 pChild
= pChild
->GetWindow(GetWindowType::Next
))
71 if (!pChild
->IsVisible())
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;
84 tools::Long nPrimaryDimension
= getPrimaryDimension(aChildSize
);
85 nPrimaryDimension
+= pChild
->get_padding() * 2;
86 setPrimaryDimension(aChildSize
, nPrimaryDimension
);
89 setPrimaryDimension(aChildSize
, 0);
91 accumulateMaxes(aChildSize
, aSize
);
94 return finalizeMaxes(aSize
, nVisibleChildren
);
97 void PriorityHBox::Resize()
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
)
116 vcl::Window
* pWindow
= dynamic_cast<vcl::Window
*>(pPrioritable
);
118 if (pWindow
&& pWindow
->GetParent() == this)
120 nCurrentWidth
-= pWindow
->GetOutputSizePixel().Width() + get_spacing();
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)
142 nCurrentWidth
-= pWindow
->GetOutputSizePixel().Width() + get_spacing();
144 pPrioritable
->ShowContent();
145 nCurrentWidth
+= getLayoutRequisition(*pWindow
).Width() + get_spacing();
147 if (nCurrentWidth
> nWidth
)
149 pPrioritable
->HideContent();
160 void PriorityHBox::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
& rRect
)
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: */