LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / sidebar / Panel.cxx
blob519c6eb52f43299eee8b23829499a42f20d213c7
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/Theme.hxx>
24 #include <sfx2/sidebar/ResourceManager.hxx>
25 #include <sfx2/sidebar/SidebarController.hxx>
26 #include <sfx2/sidebar/SidebarPanelBase.hxx>
27 #include <sfx2/viewsh.hxx>
28 #include <tools/json_writer.hxx>
31 #ifdef DEBUG
32 #include <sfx2/sidebar/Tools.hxx>
33 #include <sfx2/sidebar/Deck.hxx>
34 #endif
36 #include <com/sun/star/awt/PosSize.hpp>
37 #include <com/sun/star/ui/XToolPanel.hpp>
38 #include <com/sun/star/ui/XSidebarPanel.hpp>
39 #include <com/sun/star/ui/XUIElement.hpp>
41 #include <vcl/weldutils.hxx>
43 using namespace css;
44 using namespace css::uno;
46 namespace sfx2::sidebar {
48 Panel::Panel(const PanelDescriptor& rPanelDescriptor,
49 weld::Widget* pParentWindow,
50 const bool bIsInitiallyExpanded,
51 Deck* pDeck,
52 const std::function<Context()>& rContextAccess,
53 const css::uno::Reference<css::frame::XFrame>& rxFrame)
54 : mxBuilder(Application::CreateBuilder(pParentWindow, "sfx/ui/panel.ui", false, reinterpret_cast<sal_uInt64>(SfxViewShell::Current())))
55 , msPanelId(rPanelDescriptor.msId)
56 , msTitle(rPanelDescriptor.msTitle)
57 , mbIsTitleBarOptional(rPanelDescriptor.mbIsTitleBarOptional)
58 , mbWantsAWT(rPanelDescriptor.mbWantsAWT)
59 , mbIsExpanded(bIsInitiallyExpanded)
60 , mbLurking(false)
61 , maContextAccess(rContextAccess)
62 , mxFrame(rxFrame)
63 , mpParentWindow(pParentWindow)
64 , mxDeck(pDeck)
65 , mxContainer(mxBuilder->weld_box("Panel"))
66 , mxTitleBar(new PanelTitleBar(rPanelDescriptor.msTitle, *mxBuilder, this))
67 , mxContents(mxBuilder->weld_box("contents"))
69 mxContents->set_visible(mbIsExpanded);
70 mxContainer->connect_get_property_tree(LINK(this, Panel, DumpAsPropertyTreeHdl));
73 bool Panel::get_extents(tools::Rectangle &rExtents) const
75 // Get vertical extent of the panel.
76 int x, y, width, height;
77 if (mxContainer->get_extents_relative_to(*mpParentWindow, x, y, width, height))
79 rExtents = tools::Rectangle(Point(x, y), Size(width, height));
80 return true;
82 return false;
85 void Panel::SetHeightPixel(int nHeight)
87 mxContainer->set_size_request(-1, nHeight);
90 void Panel::set_margin_top(int nMargin)
92 mxContainer->set_margin_top(nMargin);
95 void Panel::set_margin_bottom(int nMargin)
97 mxContainer->set_margin_bottom(nMargin);
100 void Panel::set_vexpand(bool bExpand)
102 mxContainer->set_vexpand(bExpand);
105 void Panel::SetLurkMode(bool bLurk)
107 // cf. DeckLayouter
108 mbLurking = bLurk;
111 IMPL_LINK(Panel, DumpAsPropertyTreeHdl, tools::JsonWriter&, rJsonWriter, void)
113 if (!IsLurking())
114 rJsonWriter.put("type", "panel");
117 Panel::~Panel()
119 mxPanelComponent = nullptr;
122 Reference<lang::XComponent> xComponent (mxElement, UNO_QUERY);
123 mxElement = nullptr;
124 if (xComponent.is())
125 xComponent->dispose();
129 Reference<lang::XComponent> xComponent = GetElementWindow();
130 if (xComponent.is())
131 xComponent->dispose();
134 mxTitleBar.reset();
136 if (mxXWindow)
138 mxXWindow->dispose();
139 mxXWindow.clear();
141 mxContents.reset();
143 assert(!mxTitleBar);
146 PanelTitleBar* Panel::GetTitleBar() const
148 return mxTitleBar.get();
151 weld::Box* Panel::GetContents() const
153 return mxContents.get();
156 void Panel::Show(bool bShow)
158 mxContainer->set_visible(bShow);
161 void Panel::SetUIElement (const Reference<ui::XUIElement>& rxElement)
163 mxElement = rxElement;
164 if (!mxElement.is())
165 return;
166 mxPanelComponent.set(mxElement->getRealInterface(), UNO_QUERY);
167 if (mbWantsAWT)
168 return;
169 sfx2::sidebar::SidebarPanelBase* pPanelBase = dynamic_cast<sfx2::sidebar::SidebarPanelBase*>(mxElement.get());
170 assert(pPanelBase && "internal panels are all expected to inherit from SidebarPanelBase");
171 pPanelBase->SetParentPanel(this);
174 void Panel::TriggerDeckLayouting()
176 mxDeck->RequestLayout();
179 weld::Window* Panel::GetFrameWeld()
181 return mxDeck->GetFrameWeld();
184 void Panel::SetExpanded (const bool bIsExpanded)
186 SidebarController* pSidebarController = SidebarController::GetSidebarControllerForFrame(mxFrame);
188 if (mbIsExpanded == bIsExpanded)
189 return;
191 mbIsExpanded = bIsExpanded;
192 mxTitleBar->UpdateExpandedState();
193 TriggerDeckLayouting();
195 if (maContextAccess && pSidebarController)
197 pSidebarController->GetResourceManager()->StorePanelExpansionState(
198 msPanelId,
199 bIsExpanded,
200 maContextAccess());
203 mxContents->set_visible(mbIsExpanded);
206 bool Panel::HasIdPredicate (std::u16string_view rsId) const
208 return msPanelId == rsId;
211 void Panel::DataChanged()
213 mxTitleBar->DataChanged();
216 Reference<awt::XWindow> Panel::GetElementWindow()
218 if (mxElement.is())
220 Reference<ui::XToolPanel> xToolPanel(mxElement->getRealInterface(), UNO_QUERY);
221 if (xToolPanel.is())
222 return xToolPanel->getWindow();
225 return nullptr;
228 const Reference<awt::XWindow>& Panel::GetElementParentWindow()
230 if (!mxXWindow)
232 if (mbWantsAWT)
233 mxXWindow = mxContents->CreateChildFrame();
234 else
235 mxXWindow = Reference<awt::XWindow>(new weld::TransportAsXWindow(mxContents.get()));
237 return mxXWindow;
240 } // end of namespace sfx2::sidebar
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */