Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/sidebar/Panel.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 <sfx2/sidebar/Panel.hxx>
21
#include <sidebar/PanelTitleBar.hxx>
22
#include <sidebar/PanelDescriptor.hxx>
23
#include <sfx2/sidebar/ResourceManager.hxx>
24
#include <sfx2/sidebar/SidebarController.hxx>
25
#include <sfx2/sidebar/SidebarPanelBase.hxx>
26
#include <sfx2/viewfrm.hxx>
27
#include <sfx2/viewsh.hxx>
28
#include <tools/json_writer.hxx>
29
30
31
#include <sidebar/Tools.hxx>
32
#include <sfx2/sidebar/Deck.hxx>
33
34
#include <com/sun/star/ui/XToolPanel.hpp>
35
#include <com/sun/star/ui/XSidebarPanel.hpp>
36
#include <com/sun/star/ui/XUIElement.hpp>
37
38
#include <utility>
39
#include <vcl/svapp.hxx>
40
#include <vcl/weld/TransportAsXWindow.hxx>
41
42
using namespace css;
43
using namespace css::uno;
44
45
namespace sfx2::sidebar {
46
47
Panel::Panel(const PanelDescriptor& rPanelDescriptor,
48
             weld::Widget* pParentWindow,
49
             const bool bIsInitiallyExpanded,
50
             Deck* pDeck,
51
             std::function<Context()> aContextAccess,
52
             const css::uno::Reference<css::frame::XFrame>& rxFrame)
53
0
    : mxBuilder(Application::CreateBuilder(pParentWindow, u"sfx/ui/panel.ui"_ustr))
54
0
    , msPanelId(rPanelDescriptor.msId)
55
0
    , msTitle(rPanelDescriptor.msTitle)
56
0
    , mbIsTitleBarOptional(rPanelDescriptor.mbIsTitleBarOptional)
57
0
    , mbWantsAWT(rPanelDescriptor.mbWantsAWT)
58
0
    , mbIsExpanded(bIsInitiallyExpanded)
59
0
    , mbLurking(false)
60
0
    , maContextAccess(std::move(aContextAccess))
61
0
    , mxFrame(rxFrame)
62
0
    , mpParentWindow(pParentWindow)
63
0
    , mxDeck(pDeck)
64
0
    , mxContainer(mxBuilder->weld_box(u"Panel"_ustr))
65
0
    , mxTitleBar(new PanelTitleBar(rPanelDescriptor.msTitle, *mxBuilder, this))
66
0
    , mxContents(mxBuilder->weld_box(u"contents"_ustr))
67
0
{
68
0
    mxContents->set_visible(mbIsExpanded);
69
0
}
70
71
bool Panel::get_extents(tools::Rectangle &rExtents) const
72
0
{
73
    // Get vertical extent of the panel.
74
0
    int x, y, width, height;
75
0
    if (mxContainer->get_extents_relative_to(*mpParentWindow, x, y, width, height))
76
0
    {
77
0
        rExtents = tools::Rectangle(Point(x, y), Size(width, height));
78
0
        return true;
79
0
    }
80
0
    return false;
81
0
}
82
83
void Panel::SetHeightPixel(int nHeight)
84
0
{
85
0
    mxContainer->set_size_request(-1, nHeight);
86
0
}
87
88
void Panel::set_margin_top(int nMargin)
89
0
{
90
0
    mxContainer->set_margin_top(nMargin);
91
0
}
92
93
void Panel::set_margin_bottom(int nMargin)
94
0
{
95
0
    mxContainer->set_margin_bottom(nMargin);
96
0
}
97
98
void Panel::set_vexpand(bool bExpand)
99
0
{
100
0
    mxContainer->set_vexpand(bExpand);
101
0
}
102
103
void Panel::SetLurkMode(bool bLurk)
104
0
{
105
    // cf. DeckLayouter
106
0
    mbLurking = bLurk;
107
0
}
108
109
Panel::~Panel()
110
0
{
111
0
    mxPanelComponent = nullptr;
112
113
0
    {
114
0
        Reference<lang::XComponent> xComponent (mxElement, UNO_QUERY);
115
0
        mxElement = nullptr;
116
0
        if (xComponent.is())
117
0
            xComponent->dispose();
118
0
    }
119
120
0
    {
121
0
        Reference<lang::XComponent> xComponent = GetElementWindow();
122
0
        if (xComponent.is())
123
0
            xComponent->dispose();
124
0
    }
125
126
0
    mxTitleBar.reset();
127
128
0
    if (mxXWindow)
129
0
    {
130
0
        mxXWindow->dispose();
131
0
        mxXWindow.clear();
132
0
    }
133
0
    mxContents.reset();
134
135
0
    assert(!mxTitleBar);
136
0
}
137
138
PanelTitleBar* Panel::GetTitleBar() const
139
0
{
140
0
    return mxTitleBar.get();
141
0
}
142
143
weld::Box* Panel::GetContents() const
144
0
{
145
0
    return mxContents.get();
146
0
}
147
148
void Panel::Show(bool bShow)
149
0
{
150
0
    mxContainer->set_visible(bShow);
151
0
}
152
153
void Panel::SetUIElement (const Reference<ui::XUIElement>& rxElement)
154
0
{
155
0
    mxElement = rxElement;
156
0
    if (!mxElement.is())
157
0
        return;
158
0
    mxPanelComponent.set(mxElement->getRealInterface(), UNO_QUERY);
159
0
    if (mbWantsAWT)
160
0
        return;
161
0
    sfx2::sidebar::SidebarPanelBase* pPanelBase = dynamic_cast<sfx2::sidebar::SidebarPanelBase*>(mxElement.get());
162
0
    assert(pPanelBase && "internal panels are all expected to inherit from SidebarPanelBase");
163
0
    pPanelBase->SetParentPanel(this);
164
0
}
165
166
void Panel::TriggerDeckLayouting()
167
0
{
168
0
    mxDeck->RequestLayout();
169
0
}
170
171
weld::Window* Panel::GetFrameWeld()
172
0
{
173
0
    return mxDeck->GetFrameWeld();
174
0
}
175
176
void Panel::SetExpanded (const bool bIsExpanded)
177
0
{
178
0
    SidebarController* pSidebarController = SidebarController::GetSidebarControllerForFrame(mxFrame);
179
180
0
    if (mbIsExpanded == bIsExpanded)
181
0
        return;
182
183
0
    mbIsExpanded = bIsExpanded;
184
0
    mxTitleBar->UpdateExpandedState();
185
0
    TriggerDeckLayouting();
186
187
0
    if (maContextAccess && pSidebarController)
188
0
    {
189
0
        pSidebarController->GetResourceManager()->StorePanelExpansionState(
190
0
            msPanelId,
191
0
            bIsExpanded,
192
0
            maContextAccess());
193
0
    }
194
195
0
    mxContents->set_visible(mbIsExpanded);
196
0
}
197
198
bool Panel::HasIdPredicate (std::u16string_view rsId) const
199
0
{
200
0
    return msPanelId == rsId;
201
0
}
202
203
void Panel::DataChanged()
204
0
{
205
0
    mxTitleBar->DataChanged();
206
0
}
207
208
Reference<awt::XWindow> Panel::GetElementWindow()
209
0
{
210
0
    if (mxElement.is())
211
0
    {
212
0
        Reference<ui::XToolPanel> xToolPanel(mxElement->getRealInterface(), UNO_QUERY);
213
0
        if (xToolPanel.is())
214
0
            return xToolPanel->getWindow();
215
0
    }
216
217
0
    return nullptr;
218
0
}
219
220
const Reference<awt::XWindow>& Panel::GetElementParentWindow()
221
0
{
222
0
    if (!mxXWindow)
223
0
    {
224
0
        if (mbWantsAWT)
225
0
            mxXWindow = mxContents->CreateChildFrame();
226
0
        else
227
0
            mxXWindow = Reference<awt::XWindow>(new weld::TransportAsXWindow(mxContents.get()));
228
0
    }
229
0
    return mxXWindow;
230
0
}
231
232
} // end of namespace sfx2::sidebar
233
234
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */