Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/control/PriorityMergedHBox.cxx
Line
Count
Source
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
0
#define DUMMY_WIDTH 50
29
0
#define BUTTON_WIDTH 30
30
31
/*
32
* PriorityMergedHBox is a VclHBox which hides its own children if there is no sufficient space.
33
*/
34
35
PriorityMergedHBox::PriorityMergedHBox(vcl::Window* pParent)
36
0
    : PriorityHBox(pParent)
37
0
{
38
0
    m_pButton = VclPtr<PushButton>::Create(this, WB_FLATBUTTON);
39
0
    m_pButton->SetClickHdl(LINK(this, PriorityMergedHBox, PBClickHdl));
40
0
    m_pButton->SetModeImage(Image(StockImage::Yes, CHEVRON));
41
0
    m_pButton->set_width_request(25);
42
0
    m_pButton->set_pack_type(VclPackType::End);
43
0
    m_pButton->Show();
44
0
}
Unexecuted instantiation: PriorityMergedHBox::PriorityMergedHBox(vcl::Window*)
Unexecuted instantiation: PriorityMergedHBox::PriorityMergedHBox(vcl::Window*)
45
46
void PriorityMergedHBox::Resize()
47
0
{
48
0
    if (comphelper::LibreOfficeKit::isActive())
49
0
        return VclHBox::Resize();
50
51
0
    if (!m_bInitialized)
52
0
        Initialize();
53
54
0
    if (!m_bInitialized)
55
0
    {
56
0
        return VclHBox::Resize();
57
0
    }
58
59
0
    tools::Long nWidth = GetSizePixel().Width();
60
0
    tools::Long nCurrentWidth = VclHBox::calculateRequisition().getWidth() + BUTTON_WIDTH;
61
62
    // Hide lower priority controls
63
0
    for (int i = GetChildCount() - 1; i >= 0; i--)
64
0
    {
65
0
        vcl::Window* pWindow = GetChild(i);
66
67
0
        if (nCurrentWidth <= nWidth)
68
0
            break;
69
70
0
        if (pWindow && pWindow->GetParent() == this && pWindow->IsVisible())
71
0
        {
72
0
            tools::Long nWindowWidth = pWindow->GetOutputSizePixel().Width();
73
0
            if (!nWindowWidth)
74
0
                nWindowWidth = getLayoutRequisition(*pWindow).Width() + get_spacing();
75
76
0
            if (nWindowWidth)
77
0
                nCurrentWidth -= nWindowWidth;
78
0
            else
79
0
                nCurrentWidth -= DUMMY_WIDTH;
80
0
            pWindow->Hide();
81
0
        }
82
0
    }
83
84
    // Show higher priority controls if we already have enough space
85
0
    for (int i = 0; i < GetChildCount(); i++)
86
0
    {
87
0
        vcl::Window* pWindow = GetChild(i);
88
89
0
        if (pWindow->GetParent() != this)
90
0
        {
91
0
            continue;
92
0
        }
93
94
0
        if (pWindow && !pWindow->IsVisible())
95
0
        {
96
0
            pWindow->Show();
97
0
            nCurrentWidth += getLayoutRequisition(*pWindow).Width() + get_spacing();
98
99
0
            if (nCurrentWidth > nWidth)
100
0
            {
101
0
                pWindow->Hide();
102
0
                break;
103
0
            }
104
0
        }
105
0
    }
106
107
0
    VclHBox::Resize();
108
109
0
    if (HasHiddenChildren())
110
0
        m_pButton->Show();
111
0
    else
112
0
        m_pButton->Hide();
113
0
}
114
115
void PriorityMergedHBox::dispose()
116
0
{
117
0
    m_pButton.disposeAndClear();
118
0
    if (m_pPopup)
119
0
        m_pPopup.disposeAndClear();
120
0
    PriorityHBox::dispose();
121
0
}
122
123
bool PriorityMergedHBox::HasHiddenChildren() const
124
0
{
125
0
    for (int i = GetChildCount() - 1; i >= 0; i--)
126
0
    {
127
0
        vcl::Window* pWindow = GetChild(i);
128
0
        if (pWindow && pWindow->GetParent() == this && !pWindow->IsVisible())
129
0
            return true;
130
0
    }
131
132
0
    return false;
133
0
}
134
135
Size PriorityMergedHBox::calculateRequisition() const
136
0
{
137
0
    if (!m_bInitialized)
138
0
    {
139
0
        return VclHBox::calculateRequisition();
140
0
    }
141
142
0
    sal_uInt16 nVisibleChildren = 0;
143
144
0
    Size aSize;
145
    // find max height and total width
146
0
    for (vcl::Window* pChild = GetWindow(GetWindowType::FirstChild); pChild;
147
0
         pChild = pChild->GetWindow(GetWindowType::Next))
148
0
    {
149
0
        Size aChildSize = getLayoutRequisition(*pChild);
150
0
        if (!pChild->IsVisible())
151
0
            setPrimaryDimension(aChildSize, 0);
152
0
        else
153
0
        {
154
0
            ++nVisibleChildren;
155
0
            tools::Long nPrimaryDimension = getPrimaryDimension(aChildSize);
156
0
            nPrimaryDimension += pChild->get_padding() * 2;
157
0
            setPrimaryDimension(aChildSize, nPrimaryDimension);
158
0
        }
159
0
        accumulateMaxes(aChildSize, aSize);
160
0
    }
161
162
0
    return finalizeMaxes(aSize, nVisibleChildren);
163
0
}
164
165
IMPL_LINK(PriorityMergedHBox, PBClickHdl, Button*, /*pButton*/, void)
166
0
{
167
0
    if (m_pPopup)
168
0
        m_pPopup.disposeAndClear();
169
170
0
    m_pPopup = VclPtr<NotebookbarPopup>::Create(this);
171
172
0
    for (int i = 0; i < GetChildCount(); i++)
173
0
    {
174
0
        vcl::Window* pWindow = GetChild(i);
175
0
        if (pWindow != m_pButton)
176
0
        {
177
0
            if (!pWindow->IsVisible())
178
0
            {
179
0
                pWindow->Show();
180
0
                pWindow->SetParent(m_pPopup->getBox());
181
                // count is decreased because we moved child
182
0
                i--;
183
0
            }
184
0
        }
185
0
    }
186
187
0
    m_pPopup->hideSeparators(true);
188
189
0
    tools::Long x = m_pButton->GetPosPixel().getX();
190
0
    tools::Long y = m_pButton->GetPosPixel().getY() + GetSizePixel().Height();
191
0
    tools::Rectangle aRect(x, y, x, y);
192
193
0
    m_pPopup->StartPopupMode(aRect, FloatWinPopupFlags::Down | FloatWinPopupFlags::GrabFocus
194
0
                                        | FloatWinPopupFlags::AllMouseButtonClose);
195
0
}
196
197
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */