nss: upgrade to release 3.73
[LibreOffice.git] / sfx2 / source / notebookbar / PriorityHBox.cxx
blob3c6564aa889097b691013840e193608b3673dc3c
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/builderfactory.hxx>
21 #include <vcl/layout.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include "PriorityHBox.hxx"
24 #include <comphelper/lok.hxx>
26 namespace
28 bool lcl_comparePriority(const vcl::IPrioritable* a, const vcl::IPrioritable* b)
30 return a->GetPriority() < b->GetPriority();
34 PriorityHBox::PriorityHBox(vcl::Window* pParent)
35 : VclHBox(pParent)
36 , m_bInitialized(false)
40 PriorityHBox::~PriorityHBox() { disposeOnce(); }
42 void PriorityHBox::Initialize()
44 m_bInitialized = true;
46 GetChildrenWithPriorities();
47 SetSizeFromParent();
50 int PriorityHBox::GetHiddenCount() const
52 int nCount = 0;
54 for (auto pWindow : m_aSortedChildren)
55 if (pWindow->IsHidden())
56 nCount++;
58 return nCount;
61 void PriorityHBox::SetSizeFromParent()
63 vcl::Window* pParent = GetParent();
64 if (pParent)
66 Size aParentSize = pParent->GetSizePixel();
67 SetSizePixel(Size(aParentSize.getWidth(), aParentSize.getHeight()));
71 Size PriorityHBox::calculateRequisition() const
73 if (!m_bInitialized)
75 return VclHBox::calculateRequisition();
78 sal_uInt16 nVisibleChildren = 0;
80 Size aSize;
81 for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
82 pChild = pChild->GetWindow(GetWindowType::Next))
84 if (!pChild->IsVisible())
85 continue;
86 ++nVisibleChildren;
87 Size aChildSize = getLayoutRequisition(*pChild);
89 bool bAlwaysExpanded = true;
91 vcl::IPrioritable* pPrioritable = dynamic_cast<vcl::IPrioritable*>(pChild);
92 if (pPrioritable && pPrioritable->GetPriority() != VCL_PRIORITY_DEFAULT)
93 bAlwaysExpanded = false;
95 if (bAlwaysExpanded)
97 tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
98 nPrimaryDimension += pChild->get_padding() * 2;
99 setPrimaryDimension(aChildSize, nPrimaryDimension);
101 else
102 setPrimaryDimension(aChildSize, 0);
104 accumulateMaxes(aChildSize, aSize);
107 return finalizeMaxes(aSize, nVisibleChildren);
110 void PriorityHBox::Resize()
112 if (!m_bInitialized && SfxViewFrame::Current())
113 Initialize();
115 if (!m_bInitialized || comphelper::LibreOfficeKit::isActive())
117 return VclHBox::Resize();
120 tools::Long nWidth = GetSizePixel().Width();
121 tools::Long nCurrentWidth = VclHBox::calculateRequisition().getWidth();
123 // Hide lower priority controls
124 for (vcl::IPrioritable* pPrioritable : m_aSortedChildren)
126 if (nCurrentWidth <= nWidth)
127 break;
129 vcl::Window* pWindow = dynamic_cast<vcl::Window*>(pPrioritable);
131 if (pWindow && pWindow->GetParent() == this)
133 nCurrentWidth -= pWindow->GetOutputWidthPixel() + get_spacing();
134 pWindow->Show();
135 pPrioritable->HideContent();
136 nCurrentWidth += pWindow->GetOutputWidthPixel() + get_spacing();
140 auto pChildR = m_aSortedChildren.rbegin();
141 // Show higher priority controls if we already have enough space
142 while (pChildR != m_aSortedChildren.rend())
144 vcl::Window* pWindow = dynamic_cast<vcl::Window*>(*pChildR);
145 vcl::IPrioritable* pPrioritable = *pChildR;
147 if (pWindow->GetParent() != this)
149 pChildR++;
150 continue;
153 if (pWindow)
155 nCurrentWidth -= pWindow->GetOutputWidthPixel() + get_spacing();
156 pWindow->Show();
157 pPrioritable->ShowContent();
158 nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
160 if (nCurrentWidth > nWidth)
162 pPrioritable->HideContent();
163 break;
167 pChildR++;
170 VclHBox::Resize();
173 void PriorityHBox::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
175 if (!m_bInitialized && SfxViewFrame::Current())
176 Initialize();
178 VclHBox::Paint(rRenderContext, rRect);
181 void PriorityHBox::GetChildrenWithPriorities()
183 for (sal_uInt16 i = 0; i < GetChildCount(); ++i)
185 vcl::Window* pChild = GetChild(i);
187 // Add only containers which have explicitly assigned priority.
188 vcl::IPrioritable* pPrioritable = dynamic_cast<vcl::IPrioritable*>(pChild);
189 if (pPrioritable && pPrioritable->GetPriority() != VCL_PRIORITY_DEFAULT)
190 m_aSortedChildren.push_back(pPrioritable);
193 if (m_aSortedChildren.empty())
194 m_bInitialized = false;
196 std::sort(m_aSortedChildren.begin(), m_aSortedChildren.end(), lcl_comparePriority);
199 VCL_BUILDER_FACTORY(PriorityHBox)
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */