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 
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>
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  if (pWindow->GetOutDev()->GetOutputWidthPixel())
73  nCurrentWidth -= pWindow->GetOutDev()->GetOutputWidthPixel();
74  else
75  nCurrentWidth -= DUMMY_WIDTH;
76  pWindow->Hide();
77  }
78  }
79 
80  // Show higher priority controls if we already have enough space
81  for (int i = 0; i < GetChildCount(); i++)
82  {
83  vcl::Window* pWindow = GetChild(i);
84 
85  if (pWindow->GetParent() != this)
86  {
87  continue;
88  }
89 
90  if (pWindow && !pWindow->IsVisible())
91  {
92  pWindow->Show();
93  nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
94 
95  if (nCurrentWidth > nWidth)
96  {
97  pWindow->Hide();
98  break;
99  }
100  }
101  }
102 
103  VclHBox::Resize();
104 
105  if (GetHiddenCount())
106  m_pButton->Show();
107  else
108  m_pButton->Hide();
109 }
110 
112 {
114  if (m_pPopup)
117 }
118 
120 {
121  int nCount = 0;
122 
123  for (int i = GetChildCount() - 1; i >= 0; i--)
124  {
125  vcl::Window* pWindow = GetChild(i);
126  if (pWindow && pWindow->GetParent() == this && !pWindow->IsVisible())
127  nCount++;
128  }
129 
130  return nCount;
131 }
132 
134 {
135  if (!m_bInitialized)
136  {
138  }
139 
140  sal_uInt16 nVisibleChildren = 0;
141 
142  Size aSize;
143  for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
144  pChild = pChild->GetWindow(GetWindowType::Next))
145  {
146  if (!pChild->IsVisible())
147  continue;
148  ++nVisibleChildren;
149  Size aChildSize = getLayoutRequisition(*pChild);
150 
151  tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
152  nPrimaryDimension += pChild->get_padding() * 2;
153  setPrimaryDimension(aChildSize, nPrimaryDimension);
154 
155  accumulateMaxes(aChildSize, aSize);
156  }
157 
158  setPrimaryDimension(aSize, 200);
159  return finalizeMaxes(aSize, nVisibleChildren);
160 }
161 
162 IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button*, /*pButton*/, void)
163 {
164  if (m_pPopup)
165  m_pPopup.disposeAndClear();
166 
167  m_pPopup = VclPtr<NotebookbarPopup>::Create(this);
168 
169  for (int i = 0; i < GetChildCount(); i++)
170  {
171  vcl::Window* pWindow = GetChild(i);
172  if (pWindow != m_pButton)
173  {
174  if (!pWindow->IsVisible())
175  {
176  pWindow->Show();
177  pWindow->SetParent(m_pPopup->getBox());
178  // count is decreased because we moved child
179  i--;
180  }
181  }
182  }
183 
184  m_pPopup->hideSeparators(true);
185 
186  tools::Long x = m_pButton->GetPosPixel().getX();
187  tools::Long y = m_pButton->GetPosPixel().getY() + GetSizePixel().Height();
188  tools::Rectangle aRect(x, y, x, y);
189 
190  m_pPopup->StartPopupMode(aRect, FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus
192 }
193 
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetClickHdl(const Link< Button *, void > &rLink)
Definition: button.hxx:79
void SetModeImage(const Image &rImage)
Definition: button.cxx:131
static Size getLayoutRequisition(const vcl::Window &rWindow)
Definition: layout.cxx:145
long Long
void disposeAndClear()
Definition: vclptr.hxx:200
virtual Size GetSizePixel() const
Definition: window.cxx:2397
void set_pack_type(VclPackType ePackType)
Definition: window2.cxx:1756
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: window.cxx:137
float x
constexpr tools::Long Width() const
virtual void setPrimaryDimension(Size &rSize, tools::Long nWidth) const override
Definition: layout.hxx:172
int nCount
void Hide()
Definition: window.hxx:897
void set_width_request(sal_Int32 nWidthRequest)
Definition: window2.cxx:1623
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
float y
virtual tools::Long getPrimaryDimension(const Size &rSize) const override
Definition: layout.hxx:168
VclPtr< NotebookbarPopup > m_pPopup
#define BUTTON_WIDTH
Size finalizeMaxes(const Size &rSize, sal_uInt16 nVisibleChildren) const
Definition: layout.cxx:398
virtual void Resize() override
int i
void SetParent(vcl::Window *pNewParent)
Definition: stacking.cxx:831
virtual void Resize()
Definition: window.cxx:1831
IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button *,, void)
Size calculateRequisition() const override
vcl::Window * GetParent() const
Definition: window2.cxx:1095
vcl::Window * GetWindow(GetWindowType nType) const
Definition: stacking.cxx:1035
sal_uInt16 GetChildCount() const
Definition: stacking.cxx:1001
PriorityMergedHBox(vcl::Window *pParent)
bool IsVisible() const
Definition: window2.cxx:1100
::OutputDevice const * GetOutDev() const
Definition: window.cxx:565
#define DUMMY_WIDTH
Definition: image.hxx:39
static VclPtr< reference_type > Create(Arg &&...arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
int GetHiddenCount() const override
VclPtr< PushButton > m_pButton
virtual Size calculateRequisition() const override
Definition: layout.cxx:226
constexpr tools::Long getWidth() const
void accumulateMaxes(const Size &rChildSize, Size &rSize) const
Definition: layout.cxx:212
WinBits const WB_FLATBUTTON
tools::Long GetOutputWidthPixel() const
Definition: outdev.hxx:305
int get_spacing() const
Definition: layout.hxx:88
vcl::Window * GetChild(sal_uInt16 nChild) const
Definition: stacking.cxx:1017
void Initialize()
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2183