LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / sidebar / UnoPanel.cxx
blobd47399b85b5dcdfd8e8479ab1d5e4c66a4520e2d
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 */
11 #include <sidebar/UnoPanel.hxx>
13 #include <sfx2/sidebar/ResourceManager.hxx>
14 #include <sfx2/sidebar/SidebarController.hxx>
16 #include <sidebar/PanelDescriptor.hxx>
17 #include <sidebar/PanelTitleBar.hxx>
18 #include <sfx2/sidebar/Panel.hxx>
19 #include <sfx2/sidebar/Deck.hxx>
20 #include <sidebar/DeckDescriptor.hxx>
22 #include <vcl/svapp.hxx>
24 using namespace css;
25 using namespace ::sfx2::sidebar;
27 SfxUnoPanel::SfxUnoPanel(const uno::Reference<frame::XFrame>& rFrame, const OUString& panelId, const OUString& deckId):
28 xFrame(rFrame),
29 mPanelId(panelId),
30 mDeckId(deckId)
32 SidebarController* pSidebarController = getSidebarController();
34 pSidebarController->CreateDeck(mDeckId); // creates deck object is not already
35 mpDeck = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId)->mpDeck;
36 mxPanel = mpDeck->GetPanel(mPanelId);
39 SidebarController* SfxUnoPanel::getSidebarController()
41 return SidebarController::GetSidebarControllerForFrame(xFrame);
44 OUString SAL_CALL SfxUnoPanel::getId()
46 SolarMutexGuard aGuard;
48 return mPanelId;
51 OUString SAL_CALL SfxUnoPanel::getTitle()
53 SolarMutexGuard aGuard;
55 auto xPanel = mxPanel.lock();
56 PanelTitleBar* pTitleBar = xPanel ? xPanel->GetTitleBar() : nullptr;
57 if (pTitleBar)
58 return pTitleBar->GetTitle();
59 else
60 return OUString();
63 void SAL_CALL SfxUnoPanel::setTitle( const OUString& newTitle )
65 SolarMutexGuard aGuard;
67 SidebarController* pSidebarController = getSidebarController();
68 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
70 if (xPanelDescriptor)
72 xPanelDescriptor->msTitle = newTitle;
73 auto xPanel = mxPanel.lock();
74 PanelTitleBar* pTitleBar = xPanel ? xPanel->GetTitleBar() : nullptr;
75 if (pTitleBar)
76 pTitleBar->SetTitle(newTitle);
80 sal_Bool SAL_CALL SfxUnoPanel::isExpanded()
82 SolarMutexGuard aGuard;
84 auto xPanel = mxPanel.lock();
85 return xPanel && xPanel->IsExpanded();
89 void SAL_CALL SfxUnoPanel::expand( const sal_Bool bCollapseOther )
92 SolarMutexGuard aGuard;
94 auto xPanel = mxPanel.lock();
95 if (xPanel)
96 xPanel->SetExpanded(true);
98 if (bCollapseOther)
100 SharedPanelContainer aPanels = mpDeck->GetPanels();
101 for (auto const& panel : aPanels)
103 if (! panel->HasIdPredicate(mPanelId))
104 panel->SetExpanded(false);
108 SidebarController* pSidebarController = getSidebarController();
109 pSidebarController->NotifyResize();
113 void SAL_CALL SfxUnoPanel::collapse()
115 SolarMutexGuard aGuard;
117 auto xPanel = mxPanel.lock();
118 if (xPanel)
119 xPanel->SetExpanded(false);
120 SidebarController* pSidebarController = getSidebarController();
121 pSidebarController->NotifyResize();
124 uno::Reference<awt::XWindow> SAL_CALL SfxUnoPanel::getDialog()
126 SolarMutexGuard aGuard;
128 auto xPanel = mxPanel.lock();
129 return xPanel ? xPanel->GetElementWindow() : nullptr;
132 sal_Int32 SAL_CALL SfxUnoPanel::getOrderIndex()
134 SolarMutexGuard aGuard;
135 SidebarController* pSidebarController = getSidebarController();
137 sal_Int32 index = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId)->mnOrderIndex;
138 return index;
141 void SAL_CALL SfxUnoPanel::setOrderIndex( const sal_Int32 newOrderIndex )
143 SolarMutexGuard aGuard;
144 SidebarController* pSidebarController = getSidebarController();
146 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
148 if (xPanelDescriptor)
150 xPanelDescriptor->mnOrderIndex = newOrderIndex;
151 // update the sidebar
152 pSidebarController->NotifyResize();
156 void SAL_CALL SfxUnoPanel::moveFirst()
158 SolarMutexGuard aGuard;
159 SidebarController* pSidebarController = getSidebarController();
161 ResourceManager::PanelContextDescriptorContainer aPanels = pSidebarController->GetMatchingPanels(mDeckId);
163 sal_Int32 curOrderIndex = getOrderIndex();
164 sal_Int32 minIndex = GetMinOrderIndex(aPanels);
166 if (curOrderIndex != minIndex) // is current panel already in place ?
168 minIndex -= 1;
169 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
170 if (xPanelDescriptor)
172 xPanelDescriptor->mnOrderIndex = minIndex;
173 // update the sidebar
174 pSidebarController->NotifyResize();
179 void SAL_CALL SfxUnoPanel::moveLast()
181 SolarMutexGuard aGuard;
182 SidebarController* pSidebarController = getSidebarController();
184 ResourceManager::PanelContextDescriptorContainer aPanels = pSidebarController->GetMatchingPanels(mDeckId);
186 sal_Int32 curOrderIndex = getOrderIndex();
187 sal_Int32 maxIndex = GetMaxOrderIndex(aPanels);
189 if (curOrderIndex != maxIndex) // is current panel already in place ?
191 maxIndex += 1;
192 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
193 if (xPanelDescriptor)
195 xPanelDescriptor->mnOrderIndex = maxIndex;
196 // update the sidebar
197 pSidebarController->NotifyResize();
202 void SAL_CALL SfxUnoPanel::moveUp()
204 SolarMutexGuard aGuard;
205 SidebarController* pSidebarController = getSidebarController();
207 // Search for previous panel OrderIndex
208 ResourceManager::PanelContextDescriptorContainer aPanels = pSidebarController->GetMatchingPanels(mDeckId);
210 sal_Int32 curOrderIndex = getOrderIndex();
211 sal_Int32 previousIndex = GetMinOrderIndex(aPanels);
213 for (auto const& panel : aPanels)
215 sal_Int32 index = pSidebarController->GetResourceManager()->GetPanelDescriptor(panel.msId)->mnOrderIndex;
216 if( index < curOrderIndex && index > previousIndex)
217 previousIndex = index;
220 if (curOrderIndex != previousIndex) // is current panel already in place ?
222 previousIndex -= 1;
223 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
224 if (xPanelDescriptor)
226 xPanelDescriptor->mnOrderIndex = previousIndex;
227 // update the sidebar
228 pSidebarController->NotifyResize();
233 void SAL_CALL SfxUnoPanel::moveDown()
235 SolarMutexGuard aGuard;
236 SidebarController* pSidebarController = getSidebarController();
238 ResourceManager::PanelContextDescriptorContainer aPanels = pSidebarController->GetMatchingPanels(mDeckId);
240 // Search for next panel OrderIndex
241 sal_Int32 curOrderIndex = getOrderIndex();
242 sal_Int32 nextIndex = GetMaxOrderIndex(aPanels);
244 for (auto const& panel : aPanels)
246 sal_Int32 index = pSidebarController->GetResourceManager()->GetPanelDescriptor(panel.msId)->mnOrderIndex;
247 if( index > curOrderIndex && index < nextIndex)
248 nextIndex = index;
251 if (curOrderIndex != nextIndex) // is current panel already in place ?
253 nextIndex += 1;
254 std::shared_ptr<PanelDescriptor> xPanelDescriptor = pSidebarController->GetResourceManager()->GetPanelDescriptor(mPanelId);
255 if (xPanelDescriptor)
257 xPanelDescriptor->mnOrderIndex = nextIndex;
258 // update the sidebar
259 pSidebarController->NotifyResize();
264 sal_Int32 SfxUnoPanel::GetMinOrderIndex(ResourceManager::PanelContextDescriptorContainer aPanels)
266 SidebarController* pSidebarController = getSidebarController();
268 sal_Int32 minIndex = pSidebarController->GetResourceManager()->GetPanelDescriptor(aPanels.begin()->msId)->mnOrderIndex;
270 for (auto const& panel : aPanels)
272 sal_Int32 index = pSidebarController->GetResourceManager()->GetPanelDescriptor(panel.msId)->mnOrderIndex;
273 if(minIndex > index)
274 minIndex = index;
276 return minIndex;
279 sal_Int32 SfxUnoPanel::GetMaxOrderIndex(ResourceManager::PanelContextDescriptorContainer aPanels)
281 SidebarController* pSidebarController = getSidebarController();
283 sal_Int32 maxIndex = pSidebarController->GetResourceManager()->GetPanelDescriptor(aPanels.begin()->msId)->mnOrderIndex;
285 for (auto const& panel : aPanels)
287 sal_Int32 index = pSidebarController->GetResourceManager()->GetPanelDescriptor(panel.msId)->mnOrderIndex;
288 if(maxIndex < index)
289 maxIndex = index;
291 return maxIndex;
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */