/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/viewsh.hxx> |
27 | | #include <tools/json_writer.hxx> |
28 | | |
29 | | |
30 | | #if OSL_DEBUG_LEVEL >= 2 |
31 | | #include <sidebar/Tools.hxx> |
32 | | #include <sfx2/sidebar/Deck.hxx> |
33 | | #endif |
34 | | |
35 | | #include <com/sun/star/ui/XToolPanel.hpp> |
36 | | #include <com/sun/star/ui/XSidebarPanel.hpp> |
37 | | #include <com/sun/star/ui/XUIElement.hpp> |
38 | | |
39 | | #include <utility> |
40 | | #include <vcl/weldutils.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, false, reinterpret_cast<sal_uInt64>(SfxViewShell::Current()))) |
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 | mxContainer->connect_get_property_tree(LINK(this, Panel, DumpAsPropertyTreeHdl)); |
70 | 0 | } |
71 | | |
72 | | bool Panel::get_extents(tools::Rectangle &rExtents) const |
73 | 0 | { |
74 | | // Get vertical extent of the panel. |
75 | 0 | int x, y, width, height; |
76 | 0 | if (mxContainer->get_extents_relative_to(*mpParentWindow, x, y, width, height)) |
77 | 0 | { |
78 | 0 | rExtents = tools::Rectangle(Point(x, y), Size(width, height)); |
79 | 0 | return true; |
80 | 0 | } |
81 | 0 | return false; |
82 | 0 | } |
83 | | |
84 | | void Panel::SetHeightPixel(int nHeight) |
85 | 0 | { |
86 | 0 | mxContainer->set_size_request(-1, nHeight); |
87 | 0 | } |
88 | | |
89 | | void Panel::set_margin_top(int nMargin) |
90 | 0 | { |
91 | 0 | mxContainer->set_margin_top(nMargin); |
92 | 0 | } |
93 | | |
94 | | void Panel::set_margin_bottom(int nMargin) |
95 | 0 | { |
96 | 0 | mxContainer->set_margin_bottom(nMargin); |
97 | 0 | } |
98 | | |
99 | | void Panel::set_vexpand(bool bExpand) |
100 | 0 | { |
101 | 0 | mxContainer->set_vexpand(bExpand); |
102 | 0 | } |
103 | | |
104 | | void Panel::SetLurkMode(bool bLurk) |
105 | 0 | { |
106 | | // cf. DeckLayouter |
107 | 0 | mbLurking = bLurk; |
108 | 0 | } |
109 | | |
110 | | IMPL_LINK(Panel, DumpAsPropertyTreeHdl, tools::JsonWriter&, rJsonWriter, void) |
111 | 0 | { |
112 | 0 | if (!IsLurking()) |
113 | 0 | rJsonWriter.put("type", "panel"); |
114 | 0 | } |
115 | | |
116 | | Panel::~Panel() |
117 | 0 | { |
118 | 0 | mxPanelComponent = nullptr; |
119 | |
|
120 | 0 | { |
121 | 0 | Reference<lang::XComponent> xComponent (mxElement, UNO_QUERY); |
122 | 0 | mxElement = nullptr; |
123 | 0 | if (xComponent.is()) |
124 | 0 | xComponent->dispose(); |
125 | 0 | } |
126 | |
|
127 | 0 | { |
128 | 0 | Reference<lang::XComponent> xComponent = GetElementWindow(); |
129 | 0 | if (xComponent.is()) |
130 | 0 | xComponent->dispose(); |
131 | 0 | } |
132 | |
|
133 | 0 | mxTitleBar.reset(); |
134 | |
|
135 | 0 | if (mxXWindow) |
136 | 0 | { |
137 | 0 | mxXWindow->dispose(); |
138 | 0 | mxXWindow.clear(); |
139 | 0 | } |
140 | 0 | mxContents.reset(); |
141 | |
|
142 | 0 | assert(!mxTitleBar); |
143 | 0 | } |
144 | | |
145 | | PanelTitleBar* Panel::GetTitleBar() const |
146 | 0 | { |
147 | 0 | return mxTitleBar.get(); |
148 | 0 | } |
149 | | |
150 | | weld::Box* Panel::GetContents() const |
151 | 0 | { |
152 | 0 | return mxContents.get(); |
153 | 0 | } |
154 | | |
155 | | void Panel::Show(bool bShow) |
156 | 0 | { |
157 | 0 | mxContainer->set_visible(bShow); |
158 | 0 | } |
159 | | |
160 | | void Panel::SetUIElement (const Reference<ui::XUIElement>& rxElement) |
161 | 0 | { |
162 | 0 | mxElement = rxElement; |
163 | 0 | if (!mxElement.is()) |
164 | 0 | return; |
165 | 0 | mxPanelComponent.set(mxElement->getRealInterface(), UNO_QUERY); |
166 | 0 | if (mbWantsAWT) |
167 | 0 | return; |
168 | 0 | sfx2::sidebar::SidebarPanelBase* pPanelBase = dynamic_cast<sfx2::sidebar::SidebarPanelBase*>(mxElement.get()); |
169 | 0 | assert(pPanelBase && "internal panels are all expected to inherit from SidebarPanelBase"); |
170 | 0 | pPanelBase->SetParentPanel(this); |
171 | 0 | } |
172 | | |
173 | | void Panel::TriggerDeckLayouting() |
174 | 0 | { |
175 | 0 | mxDeck->RequestLayout(); |
176 | 0 | } |
177 | | |
178 | | weld::Window* Panel::GetFrameWeld() |
179 | 0 | { |
180 | 0 | return mxDeck->GetFrameWeld(); |
181 | 0 | } |
182 | | |
183 | | void Panel::SetExpanded (const bool bIsExpanded) |
184 | 0 | { |
185 | 0 | SidebarController* pSidebarController = SidebarController::GetSidebarControllerForFrame(mxFrame); |
186 | |
|
187 | 0 | if (mbIsExpanded == bIsExpanded) |
188 | 0 | return; |
189 | | |
190 | 0 | mbIsExpanded = bIsExpanded; |
191 | 0 | mxTitleBar->UpdateExpandedState(); |
192 | 0 | TriggerDeckLayouting(); |
193 | |
|
194 | 0 | if (maContextAccess && pSidebarController) |
195 | 0 | { |
196 | 0 | pSidebarController->GetResourceManager()->StorePanelExpansionState( |
197 | 0 | msPanelId, |
198 | 0 | bIsExpanded, |
199 | 0 | maContextAccess()); |
200 | 0 | } |
201 | |
|
202 | 0 | mxContents->set_visible(mbIsExpanded); |
203 | 0 | } |
204 | | |
205 | | bool Panel::HasIdPredicate (std::u16string_view rsId) const |
206 | 0 | { |
207 | 0 | return msPanelId == rsId; |
208 | 0 | } |
209 | | |
210 | | void Panel::DataChanged() |
211 | 0 | { |
212 | 0 | mxTitleBar->DataChanged(); |
213 | 0 | } |
214 | | |
215 | | Reference<awt::XWindow> Panel::GetElementWindow() |
216 | 0 | { |
217 | 0 | if (mxElement.is()) |
218 | 0 | { |
219 | 0 | Reference<ui::XToolPanel> xToolPanel(mxElement->getRealInterface(), UNO_QUERY); |
220 | 0 | if (xToolPanel.is()) |
221 | 0 | return xToolPanel->getWindow(); |
222 | 0 | } |
223 | | |
224 | 0 | return nullptr; |
225 | 0 | } |
226 | | |
227 | | const Reference<awt::XWindow>& Panel::GetElementParentWindow() |
228 | 0 | { |
229 | 0 | if (!mxXWindow) |
230 | 0 | { |
231 | 0 | if (mbWantsAWT) |
232 | 0 | mxXWindow = mxContents->CreateChildFrame(); |
233 | 0 | else |
234 | 0 | mxXWindow = Reference<awt::XWindow>(new weld::TransportAsXWindow(mxContents.get())); |
235 | 0 | } |
236 | 0 | return mxXWindow; |
237 | 0 | } |
238 | | |
239 | | } // end of namespace sfx2::sidebar |
240 | | |
241 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |