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/Deck.hxx>
21 #include <sidebar/DeckDescriptor.hxx>
22 #include <sidebar/DeckLayouter.hxx>
23 #include <sidebar/DeckTitleBar.hxx>
24 #include <sidebar/PanelTitleBar.hxx>
25 #include <sfx2/sidebar/Panel.hxx>
26 #include <sfx2/sidebar/SidebarDockingWindow.hxx>
27 #include <sfx2/sidebar/Theme.hxx>
28 #include <sfx2/viewsh.hxx>
30 #include <vcl/event.hxx>
31 #include <comphelper/lok.hxx>
32 #include <tools/json_writer.hxx>
35 using namespace css::uno
;
37 namespace sfx2::sidebar
{
39 Deck::Deck(const DeckDescriptor
& rDeckDescriptor
, SidebarDockingWindow
* pParentWindow
,
40 const std::function
<void()>& rCloserAction
)
41 : InterimItemWindow(pParentWindow
, "sfx/ui/deck.ui", "Deck")
42 , msId(rDeckDescriptor
.msId
)
44 , mnScrolledWindowExtraWidth(0)
47 , mxParentWindow(pParentWindow
)
48 , mxTitleBar(new DeckTitleBar(rDeckDescriptor
.msTitle
, *m_xBuilder
, rCloserAction
))
49 , mxVerticalScrollBar(m_xBuilder
->weld_scrolled_window("scrolledwindow"))
50 , mxContents(m_xBuilder
->weld_box("contents"))
52 SetStyle(GetStyle() | WB_DIALOGCONTROL
);
54 m_xContainer
->set_background(Theme::GetColor(Theme::Color_DeckBackground
));
56 mxVerticalScrollBar
->vadjustment_set_step_increment(10);
57 mxVerticalScrollBar
->vadjustment_set_page_increment(100);
59 // tdf#142458 Measure the preferred width of an empty ScrolledWindow
60 // to add to the width of the union of panel widths when calculating
61 // the minimal width of the deck
62 mxVerticalScrollBar
->set_hpolicy(VclPolicyType::NEVER
);
63 mxVerticalScrollBar
->set_vpolicy(VclPolicyType::NEVER
);
64 mnScrolledWindowExtraWidth
= mxVerticalScrollBar
->get_preferred_size().Width();
65 mxVerticalScrollBar
->set_hpolicy(VclPolicyType::AUTOMATIC
);
66 mxVerticalScrollBar
->set_vpolicy(VclPolicyType::AUTOMATIC
);
76 SharedPanelContainer aPanels
;
77 aPanels
.swap(maPanels
);
79 // We have to explicitly trigger the destruction of panels.
80 // Otherwise that is done by one of our base class destructors
81 // without updating maPanels.
82 for (auto& rpPanel
: aPanels
)
88 mxVerticalScrollBar
.reset();
90 mxParentWindow
.clear();
92 InterimItemWindow::dispose();
95 DeckTitleBar
* Deck::GetTitleBar() const
97 return mxTitleBar
.get();
100 tools::Rectangle
Deck::GetContentArea() const
102 const Size
aWindowSize (GetSizePixel());
103 const int nBorderSize (Theme::GetInteger(Theme::Int_DeckBorderSize
));
104 if (aWindowSize
.IsEmpty())
105 return tools::Rectangle();
107 return tools::Rectangle(
108 Theme::GetInteger(Theme::Int_DeckLeftPadding
) + nBorderSize
,
109 Theme::GetInteger(Theme::Int_DeckTopPadding
) + nBorderSize
,
110 aWindowSize
.Width() - 1 - Theme::GetInteger(Theme::Int_DeckRightPadding
) - nBorderSize
,
111 aWindowSize
.Height() - 1 - Theme::GetInteger(Theme::Int_DeckBottomPadding
) - nBorderSize
);
114 void Deck::DataChanged(const DataChangedEvent
&)
116 for (auto& rpPanel
: maPanels
)
117 rpPanel
->DataChanged();
119 RequestLayoutInternal();
123 * Get the ordering as is shown in the layout, and our type as 'deck'
124 * also elide nested panel windows.
126 void Deck::DumpAsPropertyTree(tools::JsonWriter
& rJsonWriter
)
128 rJsonWriter
.put("id", get_id().isEmpty() ? msId
: get_id());
129 rJsonWriter
.put("type", "deck");
130 rJsonWriter
.put("text", GetText());
131 rJsonWriter
.put("enabled", IsEnabled());
133 rJsonWriter
.put("visible", false);
135 auto childrenNode
= rJsonWriter
.startArray("children");
136 for (const auto &it
: maPanels
)
138 // collapse the panel itself out
139 auto xContent
= it
->GetContents();
143 auto childNode
= rJsonWriter
.startStruct();
144 rJsonWriter
.put("id", it
->GetId());
145 rJsonWriter
.put("type", "panel");
146 rJsonWriter
.put("text", it
->GetTitle());
147 rJsonWriter
.put("enabled", true);
148 rJsonWriter
.put("hidden", it
->IsLurking());
149 rJsonWriter
.put("expanded", it
->IsExpanded());
151 if (it
->GetTitleBar() && !it
->GetTitleBar()->GetMoreOptionsCommand().isEmpty())
152 rJsonWriter
.put("command", it
->GetTitleBar()->GetMoreOptionsCommand());
155 auto children2Node
= rJsonWriter
.startArray("children");
157 auto child2Node
= rJsonWriter
.startStruct();
158 xContent
->get_property_tree(rJsonWriter
);
165 * This container may contain existing panels that are
166 * being re-used, and new ones too.
168 void Deck::ResetPanels(SharedPanelContainer
&& rPanelContainer
)
170 SharedPanelContainer aHiddens
;
172 // First hide old panels we don't need just now.
173 for (auto& rpPanel
: maPanels
)
176 for (const auto & i
: rPanelContainer
)
177 bFound
= bFound
|| (rpPanel
.get() == i
.get());
178 if (!bFound
) // this one didn't survive.
180 rpPanel
->SetLurkMode(true);
181 aHiddens
.push_back(rpPanel
);
184 maPanels
= std::move(rPanelContainer
);
186 // Hidden ones always at the end
187 maPanels
.insert(std::end(maPanels
), std::begin(aHiddens
), std::end(aHiddens
));
189 RequestLayoutInternal();
192 void Deck::RequestLayoutInternal()
197 DeckLayouter::LayoutDeck(mxParentWindow
.get(), GetContentArea(),
198 mnMinimalWidth
, mnMinimalHeight
, maPanels
,
199 *GetTitleBar(), *mxVerticalScrollBar
);
203 // tdf#142458 at this point mnMinimalWidth contains the width required
204 // by the panels, but extra space may be needed by the scrolledwindow
205 // that will contain the panels
206 mnMinimalWidth
+= mnScrolledWindowExtraWidth
;
210 void Deck::RequestLayout()
212 RequestLayoutInternal();
214 if (!comphelper::LibreOfficeKit::isActive())
217 bool bChangeNeeded
= false;
218 Size aParentSize
= mxParentWindow
->GetSizePixel();
220 if (mnMinimalHeight
> 0 && (mnMinimalHeight
!= aParentSize
.Height() || GetSizePixel().Height() != mnMinimalHeight
))
222 aParentSize
.setHeight(mnMinimalHeight
);
223 bChangeNeeded
= true;
225 const SfxViewShell
* pViewShell
= SfxViewShell::Current();
226 if (mnMinimalWidth
> 0 && (mnMinimalWidth
!= aParentSize
.Width() || GetSizePixel().Width() != mnMinimalWidth
)
227 && pViewShell
&& pViewShell
->isLOKMobilePhone())
229 aParentSize
.setWidth(mnMinimalWidth
);
230 bChangeNeeded
= true;
235 mxParentWindow
->SetSizePixel(aParentSize
);
236 setPosSizePixel(0, 0, aParentSize
.Width(), aParentSize
.Height());
238 else if (aParentSize
!= GetSizePixel()) //Sync parent & child sizes
239 setPosSizePixel(0, 0, aParentSize
.Width(), aParentSize
.Height());
242 weld::Widget
* Deck::GetPanelParentWindow()
244 return mxContents
.get();
247 std::shared_ptr
<Panel
> Deck::GetPanel(std::u16string_view panelId
)
249 for (const auto& pPanel
: maPanels
)
251 if(pPanel
->GetId() == panelId
)
260 void Deck::ShowPanel(const Panel
& rPanel
)
262 if (!mxVerticalScrollBar
|| mxVerticalScrollBar
->get_vpolicy() == VclPolicyType::NEVER
)
265 // Get vertical extent of the panel.
266 tools::Rectangle aExtents
;
267 if (!rPanel
.get_extents(aExtents
))
270 auto nPanelTop
= aExtents
.Top();
271 auto nPanelBottom
= aExtents
.Bottom() - 1;
273 // Determine what the new thumb position should be like.
274 // When the whole panel does not fit then make its top visible
275 // and it off at the bottom.
276 sal_Int32
nNewThumbPos(mxVerticalScrollBar
->vadjustment_get_value());
277 if (nPanelBottom
>= nNewThumbPos
+ mxVerticalScrollBar
->vadjustment_get_page_size())
278 nNewThumbPos
= nPanelBottom
- mxVerticalScrollBar
->vadjustment_get_page_size();
279 if (nPanelTop
< nNewThumbPos
)
280 nNewThumbPos
= nPanelTop
;
282 mxVerticalScrollBar
->vadjustment_set_value(nNewThumbPos
);
285 } // end of namespace sfx2::sidebar
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */