Get the style color and number just once
[LibreOffice.git] / sfx2 / source / sidebar / Panel.cxx
blob096f11f6a287e6e979799ff5a3bdbaabf03db7ff
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 <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>
33 #endif
35 #include <com/sun/star/ui/XToolPanel.hpp>
36 #include <com/sun/star/ui/XSidebarPanel.hpp>
37 #include <com/sun/star/ui/XUIElement.hpp>
39 #include <utility>
40 #include <vcl/weldutils.hxx>
42 using namespace css;
43 using namespace css::uno;
45 namespace sfx2::sidebar {
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 : 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)
59 , mbLurking(false)
60 , maContextAccess(std::move(aContextAccess))
61 , mxFrame(rxFrame)
62 , mpParentWindow(pParentWindow)
63 , mxDeck(pDeck)
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));
79 return true;
81 return false;
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)
106 // cf. DeckLayouter
107 mbLurking = bLurk;
110 IMPL_LINK(Panel, DumpAsPropertyTreeHdl, tools::JsonWriter&, rJsonWriter, void)
112 if (!IsLurking())
113 rJsonWriter.put("type", "panel");
116 Panel::~Panel()
118 mxPanelComponent = nullptr;
121 Reference<lang::XComponent> xComponent (mxElement, UNO_QUERY);
122 mxElement = nullptr;
123 if (xComponent.is())
124 xComponent->dispose();
128 Reference<lang::XComponent> xComponent = GetElementWindow();
129 if (xComponent.is())
130 xComponent->dispose();
133 mxTitleBar.reset();
135 if (mxXWindow)
137 mxXWindow->dispose();
138 mxXWindow.clear();
140 mxContents.reset();
142 assert(!mxTitleBar);
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;
163 if (!mxElement.is())
164 return;
165 mxPanelComponent.set(mxElement->getRealInterface(), UNO_QUERY);
166 if (mbWantsAWT)
167 return;
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)
188 return;
190 mbIsExpanded = bIsExpanded;
191 mxTitleBar->UpdateExpandedState();
192 TriggerDeckLayouting();
194 if (maContextAccess && pSidebarController)
196 pSidebarController->GetResourceManager()->StorePanelExpansionState(
197 msPanelId,
198 bIsExpanded,
199 maContextAccess());
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()
217 if (mxElement.is())
219 Reference<ui::XToolPanel> xToolPanel(mxElement->getRealInterface(), UNO_QUERY);
220 if (xToolPanel.is())
221 return xToolPanel->getWindow();
224 return nullptr;
227 const Reference<awt::XWindow>& Panel::GetElementParentWindow()
229 if (!mxXWindow)
231 if (mbWantsAWT)
232 mxXWindow = mxContents->CreateChildFrame();
233 else
234 mxXWindow = Reference<awt::XWindow>(new weld::TransportAsXWindow(mxContents.get()));
236 return mxXWindow;
239 } // end of namespace sfx2::sidebar
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */