bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / control / PriorityMergedHBox.cxx
blobfd5aa5814dacb5baa6e3139eb758ff1d18a9947e
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/toolkit/button.hxx>
21 #include <vcl/layout.hxx>
22 #include <bitmaps.hlst>
23 #include <NotebookbarPopup.hxx>
24 #include <PriorityHBox.hxx>
25 #include <PriorityMergedHBox.hxx>
26 #include <comphelper/lok.hxx>
28 #define DUMMY_WIDTH 50
29 #define BUTTON_WIDTH 30
32 * PriorityMergedHBox is a VclHBox which hides its own children if there is no sufficient space.
35 PriorityMergedHBox::PriorityMergedHBox(vcl::Window* pParent)
36 : PriorityHBox(pParent)
38 m_pButton = VclPtr<PushButton>::Create(this, WB_FLATBUTTON);
39 m_pButton->SetClickHdl(LINK(this, PriorityMergedHBox, PBClickHdl));
40 m_pButton->SetModeImage(Image(StockImage::Yes, CHEVRON));
41 m_pButton->set_width_request(25);
42 m_pButton->set_pack_type(VclPackType::End);
43 m_pButton->Show();
46 void PriorityMergedHBox::Resize()
48 if (comphelper::LibreOfficeKit::isActive())
49 return VclHBox::Resize();
51 if (!m_bInitialized)
52 Initialize();
54 if (!m_bInitialized)
56 return VclHBox::Resize();
59 tools::Long nWidth = GetSizePixel().Width();
60 tools::Long nCurrentWidth = VclHBox::calculateRequisition().getWidth() + BUTTON_WIDTH;
62 // Hide lower priority controls
63 for (int i = GetChildCount() - 1; i >= 0; i--)
65 vcl::Window* pWindow = GetChild(i);
67 if (nCurrentWidth <= nWidth)
68 break;
70 if (pWindow && pWindow->GetParent() == this && pWindow->IsVisible())
72 tools::Long nWindowWidth = pWindow->GetOutputSizePixel().Width();
73 if (!nWindowWidth)
74 nWindowWidth = getLayoutRequisition(*pWindow).Width() + get_spacing();
76 if (nWindowWidth)
77 nCurrentWidth -= nWindowWidth;
78 else
79 nCurrentWidth -= DUMMY_WIDTH;
80 pWindow->Hide();
84 // Show higher priority controls if we already have enough space
85 for (int i = 0; i < GetChildCount(); i++)
87 vcl::Window* pWindow = GetChild(i);
89 if (pWindow->GetParent() != this)
91 continue;
94 if (pWindow && !pWindow->IsVisible())
96 pWindow->Show();
97 nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
99 if (nCurrentWidth > nWidth)
101 pWindow->Hide();
102 break;
107 VclHBox::Resize();
109 if (GetHiddenCount())
110 m_pButton->Show();
111 else
112 m_pButton->Hide();
115 void PriorityMergedHBox::dispose()
117 m_pButton.disposeAndClear();
118 if (m_pPopup)
119 m_pPopup.disposeAndClear();
120 PriorityHBox::dispose();
123 int PriorityMergedHBox::GetHiddenCount() const
125 int nCount = 0;
127 for (int i = GetChildCount() - 1; i >= 0; i--)
129 vcl::Window* pWindow = GetChild(i);
130 if (pWindow && pWindow->GetParent() == this && !pWindow->IsVisible())
131 nCount++;
134 return nCount;
137 Size PriorityMergedHBox::calculateRequisition() const
139 if (!m_bInitialized)
141 return VclHBox::calculateRequisition();
144 sal_uInt16 nVisibleChildren = 0;
146 Size aSize;
147 // find max height and total width
148 for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
149 pChild = pChild->GetWindow(GetWindowType::Next))
151 Size aChildSize = getLayoutRequisition(*pChild);
152 if (!pChild->IsVisible())
153 setPrimaryDimension(aChildSize, 0);
154 else
156 ++nVisibleChildren;
157 tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
158 nPrimaryDimension += pChild->get_padding() * 2;
159 setPrimaryDimension(aChildSize, nPrimaryDimension);
161 accumulateMaxes(aChildSize, aSize);
164 return finalizeMaxes(aSize, nVisibleChildren);
167 IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button*, /*pButton*/, void)
169 if (m_pPopup)
170 m_pPopup.disposeAndClear();
172 m_pPopup = VclPtr<NotebookbarPopup>::Create(this);
174 for (int i = 0; i < GetChildCount(); i++)
176 vcl::Window* pWindow = GetChild(i);
177 if (pWindow != m_pButton)
179 if (!pWindow->IsVisible())
181 pWindow->Show();
182 pWindow->SetParent(m_pPopup->getBox());
183 // count is decreased because we moved child
184 i--;
189 m_pPopup->hideSeparators(true);
191 tools::Long x = m_pButton->GetPosPixel().getX();
192 tools::Long y = m_pButton->GetPosPixel().getY() + GetSizePixel().Height();
193 tools::Rectangle aRect(x, y, x, y);
195 m_pPopup->StartPopupMode(aRect, FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus
196 | FloatWinPopupFlags::AllMouseButtonClose);
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */