1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <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/viewsh.hxx>
27 #include <tools/json_writer.hxx>
30 #if OSL_DEBUG_LEVEL >= 2
31 #include <sidebar/Tools.hxx>
32 #include <sfx2/sidebar/Deck.hxx>
35 #include <com/sun/star/ui/XToolPanel.hpp>
36 #include <com/sun/star/ui/XSidebarPanel.hpp>
37 #include <com/sun/star/ui/XUIElement.hpp>
40 #include <vcl/weldutils.hxx>
43 using namespace css::uno
;
45 namespace sfx2::sidebar
{
47 Panel::Panel(const PanelDescriptor
& rPanelDescriptor
,
48 weld::Widget
* pParentWindow
,
49 const bool bIsInitiallyExpanded
,
51 std::function
<Context()> aContextAccess
,
52 const css::uno::Reference
<css::frame::XFrame
>& rxFrame
)
53 : mxBuilder(Application::CreateBuilder(pParentWindow
, u
"sfx/ui/panel.ui"_ustr
, false, reinterpret_cast<sal_uInt64
>(SfxViewShell::Current())))
54 , msPanelId(rPanelDescriptor
.msId
)
55 , msTitle(rPanelDescriptor
.msTitle
)
56 , mbIsTitleBarOptional(rPanelDescriptor
.mbIsTitleBarOptional
)
57 , mbWantsAWT(rPanelDescriptor
.mbWantsAWT
)
58 , mbIsExpanded(bIsInitiallyExpanded
)
60 , maContextAccess(std::move(aContextAccess
))
62 , mpParentWindow(pParentWindow
)
64 , mxContainer(mxBuilder
->weld_box(u
"Panel"_ustr
))
65 , mxTitleBar(new PanelTitleBar(rPanelDescriptor
.msTitle
, *mxBuilder
, this))
66 , mxContents(mxBuilder
->weld_box(u
"contents"_ustr
))
68 mxContents
->set_visible(mbIsExpanded
);
69 mxContainer
->connect_get_property_tree(LINK(this, Panel
, DumpAsPropertyTreeHdl
));
72 bool Panel::get_extents(tools::Rectangle
&rExtents
) const
74 // Get vertical extent of the panel.
75 int x
, y
, width
, height
;
76 if (mxContainer
->get_extents_relative_to(*mpParentWindow
, x
, y
, width
, height
))
78 rExtents
= tools::Rectangle(Point(x
, y
), Size(width
, height
));
84 void Panel::SetHeightPixel(int nHeight
)
86 mxContainer
->set_size_request(-1, nHeight
);
89 void Panel::set_margin_top(int nMargin
)
91 mxContainer
->set_margin_top(nMargin
);
94 void Panel::set_margin_bottom(int nMargin
)
96 mxContainer
->set_margin_bottom(nMargin
);
99 void Panel::set_vexpand(bool bExpand
)
101 mxContainer
->set_vexpand(bExpand
);
104 void Panel::SetLurkMode(bool bLurk
)
110 IMPL_LINK(Panel
, DumpAsPropertyTreeHdl
, tools::JsonWriter
&, rJsonWriter
, void)
113 rJsonWriter
.put("type", "panel");
118 mxPanelComponent
= nullptr;
121 Reference
<lang::XComponent
> xComponent (mxElement
, UNO_QUERY
);
124 xComponent
->dispose();
128 Reference
<lang::XComponent
> xComponent
= GetElementWindow();
130 xComponent
->dispose();
137 mxXWindow
->dispose();
145 PanelTitleBar
* Panel::GetTitleBar() const
147 return mxTitleBar
.get();
150 weld::Box
* Panel::GetContents() const
152 return mxContents
.get();
155 void Panel::Show(bool bShow
)
157 mxContainer
->set_visible(bShow
);
160 void Panel::SetUIElement (const Reference
<ui::XUIElement
>& rxElement
)
162 mxElement
= rxElement
;
165 mxPanelComponent
.set(mxElement
->getRealInterface(), UNO_QUERY
);
168 sfx2::sidebar::SidebarPanelBase
* pPanelBase
= dynamic_cast<sfx2::sidebar::SidebarPanelBase
*>(mxElement
.get());
169 assert(pPanelBase
&& "internal panels are all expected to inherit from SidebarPanelBase");
170 pPanelBase
->SetParentPanel(this);
173 void Panel::TriggerDeckLayouting()
175 mxDeck
->RequestLayout();
178 weld::Window
* Panel::GetFrameWeld()
180 return mxDeck
->GetFrameWeld();
183 void Panel::SetExpanded (const bool bIsExpanded
)
185 SidebarController
* pSidebarController
= SidebarController::GetSidebarControllerForFrame(mxFrame
);
187 if (mbIsExpanded
== bIsExpanded
)
190 mbIsExpanded
= bIsExpanded
;
191 mxTitleBar
->UpdateExpandedState();
192 TriggerDeckLayouting();
194 if (maContextAccess
&& pSidebarController
)
196 pSidebarController
->GetResourceManager()->StorePanelExpansionState(
202 mxContents
->set_visible(mbIsExpanded
);
205 bool Panel::HasIdPredicate (std::u16string_view rsId
) const
207 return msPanelId
== rsId
;
210 void Panel::DataChanged()
212 mxTitleBar
->DataChanged();
215 Reference
<awt::XWindow
> Panel::GetElementWindow()
219 Reference
<ui::XToolPanel
> xToolPanel(mxElement
->getRealInterface(), UNO_QUERY
);
221 return xToolPanel
->getWindow();
227 const Reference
<awt::XWindow
>& Panel::GetElementParentWindow()
232 mxXWindow
= mxContents
->CreateChildFrame();
234 mxXWindow
= Reference
<awt::XWindow
>(new weld::TransportAsXWindow(mxContents
.get()));
239 } // end of namespace sfx2::sidebar
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */