sdext: adapt xpdfwrapper to poppler 24.12
[LibreOffice.git] / vcl / source / control / PriorityMergedHBox.cxx
blob64fad5acb48806f462e780cb91a7c501f44138a6
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 (HasHiddenChildren())
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 bool PriorityMergedHBox::HasHiddenChildren() const
125 for (int i = GetChildCount() - 1; i >= 0; i--)
127 vcl::Window* pWindow = GetChild(i);
128 if (pWindow && pWindow->GetParent() == this && !pWindow->IsVisible())
129 return true;
132 return false;
135 Size PriorityMergedHBox::calculateRequisition() const
137 if (!m_bInitialized)
139 return VclHBox::calculateRequisition();
142 sal_uInt16 nVisibleChildren = 0;
144 Size aSize;
145 // find max height and total width
146 for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
147 pChild = pChild->GetWindow(GetWindowType::Next))
149 Size aChildSize = getLayoutRequisition(*pChild);
150 if (!pChild->IsVisible())
151 setPrimaryDimension(aChildSize, 0);
152 else
154 ++nVisibleChildren;
155 tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
156 nPrimaryDimension += pChild->get_padding() * 2;
157 setPrimaryDimension(aChildSize, nPrimaryDimension);
159 accumulateMaxes(aChildSize, aSize);
162 return finalizeMaxes(aSize, nVisibleChildren);
165 IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button*, /*pButton*/, void)
167 if (m_pPopup)
168 m_pPopup.disposeAndClear();
170 m_pPopup = VclPtr<NotebookbarPopup>::Create(this);
172 for (int i = 0; i < GetChildCount(); i++)
174 vcl::Window* pWindow = GetChild(i);
175 if (pWindow != m_pButton)
177 if (!pWindow->IsVisible())
179 pWindow->Show();
180 pWindow->SetParent(m_pPopup->getBox());
181 // count is decreased because we moved child
182 i--;
187 m_pPopup->hideSeparators(true);
189 tools::Long x = m_pButton->GetPosPixel().getX();
190 tools::Long y = m_pButton->GetPosPixel().getY() + GetSizePixel().Height();
191 tools::Rectangle aRect(x, y, x, y);
193 m_pPopup->StartPopupMode(aRect, FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus
194 | FloatWinPopupFlags::AllMouseButtonClose);
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */