use insert function instead of for loop
[LibreOffice.git] / sfx2 / source / sidebar / Sidebar.cxx
blob3ebd59b924ada2dfc2cc3f7dce78986196da90d2
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/Sidebar.hxx>
21 #include <sfx2/sidebar/SidebarController.hxx>
22 #include <sfx2/sidebar/ResourceManager.hxx>
23 #include <sfx2/sidebar/SidebarDockingWindow.hxx>
24 #include <sidebar/PanelDescriptor.hxx>
25 #include <sidebar/Tools.hxx>
26 #include <sfx2/sidebar/FocusManager.hxx>
27 #include <sfx2/childwin.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <sfx2/viewsh.hxx>
30 #include <com/sun/star/frame/XDispatch.hpp>
32 using namespace css;
34 namespace sfx2::sidebar {
36 void Sidebar::ShowDeck(std::u16string_view rsDeckId, SfxViewFrame* pViewFrame, bool bToggle)
38 if (!pViewFrame)
39 return;
41 SfxChildWindow* pSidebarChildWindow = pViewFrame->GetChildWindow(SID_SIDEBAR);
42 bool bInitiallyVisible = pSidebarChildWindow && pSidebarChildWindow->IsVisible();
43 if (!bInitiallyVisible)
44 pViewFrame->ShowChildWindow(SID_SIDEBAR);
46 SidebarController* pController =
47 SidebarController::GetSidebarControllerForFrame(pViewFrame->GetFrame().GetFrameInterface());
48 if (!pController)
49 return;
51 if (bToggle && bInitiallyVisible && pController->IsDeckVisible(rsDeckId))
53 // close the sidebar if it was already visible and showing this sidebar deck
54 const util::URL aURL(Tools::GetURL(u".uno:Sidebar"_ustr));
55 css::uno::Reference<frame::XDispatch> xDispatch(Tools::GetDispatch(pViewFrame->GetFrame().GetFrameInterface(), aURL));
56 if (xDispatch.is())
57 xDispatch->dispatch(aURL, css::uno::Sequence<beans::PropertyValue>());
59 else
61 pController->OpenThenSwitchToDeck(rsDeckId);
62 pController->GetFocusManager().GrabFocusPanel();
66 void Sidebar::ShowPanel (
67 std::u16string_view rsPanelId,
68 const css::uno::Reference<frame::XFrame>& rxFrame, bool bFocus)
70 SidebarController* pController = SidebarController::GetSidebarControllerForFrame(rxFrame);
71 if (!pController)
72 return;
74 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pController->GetResourceManager()->GetPanelDescriptor(rsPanelId);
76 if (!xPanelDescriptor)
77 return;
79 // This should be a lot more sophisticated:
80 // - Make the deck switching asynchronous
81 // - Make sure to use a context that really shows the panel
83 // All that is not necessary for the current use cases so let's
84 // keep it simple for the time being.
85 pController->OpenThenSwitchToDeck(xPanelDescriptor->msDeckId);
87 if (bFocus)
88 pController->GetFocusManager().GrabFocusPanel();
91 void Sidebar::TogglePanel (
92 std::u16string_view rsPanelId,
93 const css::uno::Reference<frame::XFrame>& rxFrame)
95 SidebarController* pController = SidebarController::GetSidebarControllerForFrame(rxFrame);
96 if (!pController)
97 return;
99 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pController->GetResourceManager()->GetPanelDescriptor(rsPanelId);
101 if (!xPanelDescriptor)
102 return;
104 // This should be a lot more sophisticated:
105 // - Make the deck switching asynchronous
106 // - Make sure to use a context that really shows the panel
108 // All that is not necessary for the current use cases so let's
109 // keep it simple for the time being.
110 pController->OpenThenToggleDeck(xPanelDescriptor->msDeckId);
113 bool Sidebar::IsPanelVisible(
114 std::u16string_view rsPanelId,
115 const css::uno::Reference<frame::XFrame>& rxFrame)
117 SidebarController* pController = SidebarController::GetSidebarControllerForFrame(rxFrame);
118 if (!pController)
119 return false;
121 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pController->GetResourceManager()->GetPanelDescriptor(rsPanelId);
122 if (!xPanelDescriptor)
123 return false;
125 return pController->IsDeckVisible(xPanelDescriptor->msDeckId);
128 bool Sidebar::Setup(std::u16string_view sidebarDeckId)
130 SfxViewShell* pViewShell = SfxViewShell::Current();
131 SfxViewFrame* pViewFrame = pViewShell ? &pViewShell->GetViewFrame() : nullptr;
132 if (pViewFrame)
134 if (!pViewFrame->GetChildWindow(SID_SIDEBAR))
135 pViewFrame->SetChildWindow(SID_SIDEBAR, false /* create it */, true /* focus */);
137 pViewFrame->ShowChildWindow(SID_SIDEBAR, true);
139 // Force synchronous population of panels
140 SfxChildWindow *pChild = pViewFrame->GetChildWindow(SID_SIDEBAR);
141 if (!pChild)
142 return false;
144 auto pDockingWin = dynamic_cast<sfx2::sidebar::SidebarDockingWindow *>(pChild->GetWindow());
145 if (!pDockingWin)
146 return false;
148 pViewFrame->ShowChildWindow( SID_SIDEBAR );
150 const rtl::Reference<sfx2::sidebar::SidebarController>& xController
151 = pDockingWin->GetOrCreateSidebarController();
153 xController->FadeIn();
154 xController->RequestOpenDeck();
156 if (!sidebarDeckId.empty())
158 xController->SwitchToDeck(sidebarDeckId);
160 else
162 xController->SwitchToDefaultDeck();
165 pDockingWin->SyncUpdate();
166 return true;
168 else
169 return false;
172 } // end of namespace sfx2::sidebar
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */