build fix
[LibreOffice.git] / sfx2 / source / notebookbar / PriorityHBox.cxx
blobfd8243f3f4ea85fec2e472f5f667fa2bbf99077f
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/dllapi.h>
23 #include <sfx2/viewfrm.hxx>
24 #include "DropdownBox.hxx"
26 #include <vector>
28 bool lcl_comparePriority(const vcl::IPrioritable* a, const vcl::IPrioritable* b)
30 return a->GetPriority() < b->GetPriority();
34 * PriorityHBox is a VclHBox which hides own childs if there is no sufficient space.
35 * Hiding order can be modified using child's priorities. If a control have default
36 * priority assigned (VCL_PRIORITY_DEFAULT), it is always shown.
39 class SFX2_DLLPUBLIC PriorityHBox : public VclHBox
41 private:
42 bool m_bInitialized;
43 long m_nNeededWidth;
45 ScopedVclPtr<SystemWindow> m_pSystemWindow;
47 std::vector<IPrioritable*> m_aSortedChilds;
49 public:
50 explicit PriorityHBox(vcl::Window *pParent)
51 : VclHBox(pParent)
52 , m_bInitialized(false)
53 , m_nNeededWidth(0)
57 virtual ~PriorityHBox() override
59 disposeOnce();
62 virtual void dispose() override
64 if (m_pSystemWindow)
66 m_pSystemWindow->RemoveEventListener(LINK(this, PriorityHBox, WindowEventListener));
67 m_pSystemWindow.clear();
69 VclHBox::dispose();
72 virtual void Resize() override
74 long nWidth = GetSizePixel().Width();
75 long nCurrentWidth = m_nNeededWidth;
77 // Hide lower priority controls
78 auto pChild = m_aSortedChilds.begin();
79 while (nCurrentWidth > nWidth && pChild != m_aSortedChilds.end())
81 DropdownBox* pContainer = static_cast<DropdownBox*>(*pChild);
82 nCurrentWidth -= pContainer->GetSizePixel().Width() + get_spacing();
83 pContainer->HideContent();
84 nCurrentWidth += pContainer->GetSizePixel().Width() + get_spacing();
85 pChild++;
88 // Show higher priority controls if we already have enough space
89 while (pChild != m_aSortedChilds.end())
91 static_cast<DropdownBox*>(*pChild)->ShowContent();
92 pChild++;
95 VclHBox::Resize();
98 virtual void Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect) override
100 if (!m_bInitialized && SfxViewFrame::Current())
102 m_bInitialized = true;
104 m_pSystemWindow = SfxViewFrame::Current()->GetFrame().GetSystemWindow();
105 if (m_pSystemWindow)
107 m_pSystemWindow->AddEventListener(LINK(this, PriorityHBox, WindowEventListener));
109 CalcNeededWidth();
111 long nWidth = m_pSystemWindow->GetSizePixel().Width();
112 SetSizePixel(Size(nWidth, GetSizePixel().Height()));
116 VclHBox::Paint(rRenderContext, rRect);
119 void CalcNeededWidth()
121 int spacing = get_spacing();
123 for (sal_uInt16 i = 0; i < GetChildCount(); ++i)
125 vcl::Window* pChild = GetChild(i);
126 m_nNeededWidth += pChild->GetSizePixel().Width() + spacing;
128 // Add only containers which have explicitly assigned priority.
129 IPrioritable* pPrioritable = pChild->GetType() == WINDOW_CONTAINER ?
130 dynamic_cast<IPrioritable*>(pChild) : nullptr;
131 if (pPrioritable && pPrioritable->GetPriority() != VCL_PRIORITY_DEFAULT)
132 m_aSortedChilds.push_back(pPrioritable);
135 std::sort(m_aSortedChilds.begin(), m_aSortedChilds.end(), lcl_comparePriority);
138 private:
139 DECL_LINK( WindowEventListener, VclWindowEvent&, void );
142 IMPL_LINK( PriorityHBox, WindowEventListener, VclWindowEvent&, rEvent, void )
144 if (rEvent.GetId() == VCLEVENT_WINDOW_RESIZE)
146 vcl::Window* pEventWindow = rEvent.GetWindow();
148 OSL_ENSURE(pEventWindow, "PriorityHBox::WindowEventListener: no window!");
150 long nWidth = pEventWindow->GetSizePixel().Width();
151 SetSizePixel(Size(nWidth, GetSizePixel().Height()));
155 VCL_BUILDER_FACTORY(PriorityHBox)
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */