LibreOffice Module vcl (master) 1
PriorityMergedHBox.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3* This file is part of the LibreOffice project.
4*
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/.
8*
9* This file incorporates work covered by the following license notice:
10*
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 .
18*/
19
21#include <vcl/layout.hxx>
22#include <bitmaps.hlst>
23#include <NotebookbarPopup.hxx>
24#include <PriorityHBox.hxx>
26#include <comphelper/lok.hxx>
27
28#define DUMMY_WIDTH 50
29#define BUTTON_WIDTH 30
30
31/*
32* PriorityMergedHBox is a VclHBox which hides its own children if there is no sufficient space.
33*/
34
36 : PriorityHBox(pParent)
37{
39 m_pButton->SetClickHdl(LINK(this, PriorityMergedHBox, PBClickHdl));
43 m_pButton->Show();
44}
45
47{
49 return VclHBox::Resize();
50
51 if (!m_bInitialized)
52 Initialize();
53
54 if (!m_bInitialized)
55 {
56 return VclHBox::Resize();
57 }
58
59 tools::Long nWidth = GetSizePixel().Width();
61
62 // Hide lower priority controls
63 for (int i = GetChildCount() - 1; i >= 0; i--)
64 {
65 vcl::Window* pWindow = GetChild(i);
66
67 if (nCurrentWidth <= nWidth)
68 break;
69
70 if (pWindow && pWindow->GetParent() == this && pWindow->IsVisible())
71 {
72 tools::Long nWindowWidth = pWindow->GetOutputSizePixel().Width();
73 if (!nWindowWidth)
74 nWindowWidth = getLayoutRequisition(*pWindow).Width() + get_spacing();
75
76 if (nWindowWidth)
77 nCurrentWidth -= nWindowWidth;
78 else
79 nCurrentWidth -= DUMMY_WIDTH;
80 pWindow->Hide();
81 }
82 }
83
84 // Show higher priority controls if we already have enough space
85 for (int i = 0; i < GetChildCount(); i++)
86 {
87 vcl::Window* pWindow = GetChild(i);
88
89 if (pWindow->GetParent() != this)
90 {
91 continue;
92 }
93
94 if (pWindow && !pWindow->IsVisible())
95 {
96 pWindow->Show();
97 nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
98
99 if (nCurrentWidth > nWidth)
100 {
101 pWindow->Hide();
102 break;
103 }
104 }
105 }
106
108
109 if (GetHiddenCount())
110 m_pButton->Show();
111 else
112 m_pButton->Hide();
113}
114
116{
118 if (m_pPopup)
121}
122
124{
125 int nCount = 0;
126
127 for (int i = GetChildCount() - 1; i >= 0; i--)
128 {
129 vcl::Window* pWindow = GetChild(i);
130 if (pWindow && pWindow->GetParent() == this && !pWindow->IsVisible())
131 nCount++;
132 }
133
134 return nCount;
135}
136
138{
139 if (!m_bInitialized)
140 {
142 }
143
144 sal_uInt16 nVisibleChildren = 0;
145
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))
150 {
151 Size aChildSize = getLayoutRequisition(*pChild);
152 if (!pChild->IsVisible())
153 setPrimaryDimension(aChildSize, 0);
154 else
155 {
156 ++nVisibleChildren;
157 tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
158 nPrimaryDimension += pChild->get_padding() * 2;
159 setPrimaryDimension(aChildSize, nPrimaryDimension);
160 }
161 accumulateMaxes(aChildSize, aSize);
162 }
163
164 return finalizeMaxes(aSize, nVisibleChildren);
165}
166
167IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button*, /*pButton*/, void)
168{
169 if (m_pPopup)
170 m_pPopup.disposeAndClear();
171
172 m_pPopup = VclPtr<NotebookbarPopup>::Create(this);
173
174 for (int i = 0; i < GetChildCount(); i++)
175 {
176 vcl::Window* pWindow = GetChild(i);
177 if (pWindow != m_pButton)
178 {
179 if (!pWindow->IsVisible())
180 {
181 pWindow->Show();
182 pWindow->SetParent(m_pPopup->getBox());
183 // count is decreased because we moved child
184 i--;
185 }
186 }
187 }
188
189 m_pPopup->hideSeparators(true);
190
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);
194
195 m_pPopup->StartPopupMode(aRect, FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus
197}
198
199/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define DUMMY_WIDTH
IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button *,, void)
#define BUTTON_WIDTH
void SetClickHdl(const Link< Button *, void > &rLink)
Definition: button.hxx:79
void SetModeImage(const Image &rImage)
Definition: button.cxx:133
Definition: image.hxx:40
void Initialize()
Size calculateRequisition() const override
VclPtr< PushButton > m_pButton
int GetHiddenCount() const override
virtual void Resize() override
VclPtr< NotebookbarPopup > m_pPopup
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects.
PriorityMergedHBox(vcl::Window *pParent)
constexpr tools::Long getWidth() const
constexpr tools::Long Width() const
Size finalizeMaxes(const Size &rSize, sal_uInt16 nVisibleChildren) const
Definition: layout.cxx:421
void accumulateMaxes(const Size &rChildSize, Size &rSize) const
Definition: layout.cxx:237
int get_spacing() const
Definition: layout.hxx:88
virtual Size calculateRequisition() const override
Definition: layout.cxx:251
static Size getLayoutRequisition(const vcl::Window &rWindow)
Definition: layout.cxx:170
virtual tools::Long getPrimaryDimension(const Size &rSize) const override
Definition: layout.hxx:168
virtual void setPrimaryDimension(Size &rSize, tools::Long nWidth) const override
Definition: layout.hxx:172
void disposeAndClear()
Definition: vclptr.hxx:200
static VclPtr< reference_type > Create(Arg &&... arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects.
Definition: window.cxx:140
vcl::Window * GetParent() const
Definition: window2.cxx:1123
sal_uInt16 GetChildCount() const
Definition: stacking.cxx:1002
virtual void Resize()
Definition: window.cxx:1835
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1036
void SetParent(vcl::Window *pNewParent)
Definition: stacking.cxx:832
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2187
void set_pack_type(VclPackType ePackType)
Definition: window2.cxx:1791
void Hide()
Definition: window.hxx:879
void set_width_request(sal_Int32 nWidthRequest)
Definition: window2.cxx:1658
virtual Size GetSizePixel() const
Definition: window.cxx:2402
Size GetOutputSizePixel() const
Definition: window3.cxx:89
bool IsVisible() const
Definition: window2.cxx:1128
vcl::Window * GetChild(sal_uInt16 nChild) const
Definition: stacking.cxx:1018
int nCount
float y
float x
int i
long Long
WinBits const WB_FLATBUTTON
Definition: wintypes.hxx:186