Get the style color and number just once
[LibreOffice.git] / sfx2 / source / sidebar / UnoDeck.cxx
bloba2c45b27706aff0f55df13c928ef75cccdd48dde
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/UnoDeck.hxx>
13 #include <sidebar/UnoPanels.hxx>
15 #include <sfx2/sidebar/ResourceManager.hxx>
16 #include <sfx2/sidebar/SidebarController.hxx>
17 #include <sidebar/DeckTitleBar.hxx>
18 #include <sfx2/sidebar/Deck.hxx>
19 #include <sidebar/DeckDescriptor.hxx>
21 #include <utility>
22 #include <vcl/svapp.hxx>
24 using namespace css;
25 using namespace ::sfx2::sidebar;
27 SfxUnoDeck::SfxUnoDeck(uno::Reference<frame::XFrame> _xFrame, OUString deckId):
28 xFrame(std::move(_xFrame)),
29 mDeckId(std::move(deckId))
33 SidebarController* SfxUnoDeck::getSidebarController()
35 return SidebarController::GetSidebarControllerForFrame(xFrame);
38 OUString SAL_CALL SfxUnoDeck::getId()
40 return mDeckId;
43 OUString SAL_CALL SfxUnoDeck::getTitle()
45 SolarMutexGuard aGuard;
47 SidebarController* pSidebarController = getSidebarController();
48 VclPtr<Deck> pDeck = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId)->mpDeck;
50 if (!pDeck)
52 pSidebarController->CreateDeck(mDeckId);
53 pDeck = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId)->mpDeck;
56 DeckTitleBar* pTitleBar = pDeck->GetTitleBar();
57 return pTitleBar->GetTitle();
60 void SAL_CALL SfxUnoDeck::setTitle( const OUString& newTitle )
62 SolarMutexGuard aGuard;
64 SidebarController* pSidebarController = getSidebarController();
65 pSidebarController->CreateDeck(mDeckId);
67 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
69 if (xDeckDescriptor)
71 Deck* pDeck = xDeckDescriptor->mpDeck;
72 DeckTitleBar* pTitleBar = pDeck->GetTitleBar();
73 pTitleBar->SetTitle(newTitle);
75 xDeckDescriptor->msTitle = newTitle;
76 xDeckDescriptor->msHelpText = newTitle;
78 pSidebarController->notifyDeckTitle(mDeckId);
82 sal_Bool SAL_CALL SfxUnoDeck::isActive()
84 SolarMutexGuard aGuard;
86 SidebarController* pSidebarController = getSidebarController();
87 return pSidebarController->IsDeckVisible(mDeckId);
91 void SAL_CALL SfxUnoDeck::activate( const sal_Bool bActivate )
93 SolarMutexGuard aGuard;
95 SidebarController* pSidebarController = getSidebarController();
97 // tdf#138160: OpenThenToggleDeck takes care of minimal width
98 if (bActivate)
99 pSidebarController->OpenThenToggleDeck(mDeckId);
100 else
102 pSidebarController->SwitchToDefaultDeck();
103 // update the sidebar
104 pSidebarController->NotifyResize();
109 uno::Reference<ui::XPanels> SAL_CALL SfxUnoDeck::getPanels()
111 SolarMutexGuard aGuard;
113 uno::Reference<ui::XPanels> panels = new SfxUnoPanels(xFrame, mDeckId);
114 return panels;
117 sal_Int32 SAL_CALL SfxUnoDeck::getOrderIndex()
119 SolarMutexGuard aGuard;
120 SidebarController* pSidebarController = getSidebarController();
122 sal_Int32 index = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId)->mnOrderIndex;
123 return index;
126 void SAL_CALL SfxUnoDeck::setOrderIndex( const sal_Int32 newOrderIndex )
128 SolarMutexGuard aGuard;
129 SidebarController* pSidebarController = getSidebarController();
131 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
133 if (xDeckDescriptor)
135 xDeckDescriptor->mnOrderIndex = newOrderIndex;
136 // update the sidebar
137 pSidebarController->NotifyResize();
141 void SAL_CALL SfxUnoDeck::moveFirst()
143 SolarMutexGuard aGuard;
144 SidebarController* pSidebarController = getSidebarController();
146 ResourceManager::DeckContextDescriptorContainer aDecks = pSidebarController->GetMatchingDecks();
148 sal_Int32 minIndex = GetMinOrderIndex(aDecks);
149 sal_Int32 curOrderIndex = getOrderIndex();
151 if (curOrderIndex != minIndex) // is deck already in place ?
153 minIndex -= 1;
154 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
155 if (xDeckDescriptor)
157 xDeckDescriptor->mnOrderIndex = minIndex;
158 // update the sidebar
159 pSidebarController->NotifyResize();
164 void SAL_CALL SfxUnoDeck::moveLast()
166 SolarMutexGuard aGuard;
167 SidebarController* pSidebarController = getSidebarController();
169 ResourceManager::DeckContextDescriptorContainer aDecks = pSidebarController->GetMatchingDecks();
171 sal_Int32 maxIndex = GetMaxOrderIndex(aDecks);
172 sal_Int32 curOrderIndex = getOrderIndex();
174 if (curOrderIndex != maxIndex) // is deck already in place ?
176 maxIndex += 1;
177 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
178 if (xDeckDescriptor)
180 xDeckDescriptor->mnOrderIndex = maxIndex;
181 // update the sidebar
182 pSidebarController->NotifyResize();
187 void SAL_CALL SfxUnoDeck::moveUp()
189 SolarMutexGuard aGuard;
190 SidebarController* pSidebarController = getSidebarController();
192 // Search for previous deck OrderIndex
193 ResourceManager::DeckContextDescriptorContainer aDecks = pSidebarController->GetMatchingDecks();
195 sal_Int32 curOrderIndex = getOrderIndex();
196 sal_Int32 previousIndex = GetMinOrderIndex(aDecks);
198 for (auto const& deck : aDecks)
200 sal_Int32 index = pSidebarController->GetResourceManager()->GetDeckDescriptor(deck.msId)->mnOrderIndex;
201 if( index < curOrderIndex && index > previousIndex)
202 previousIndex = index;
205 if (curOrderIndex != previousIndex) // is deck already in place ?
207 previousIndex -= 1;
208 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
209 if (xDeckDescriptor)
211 xDeckDescriptor->mnOrderIndex = previousIndex;
212 // update the sidebar
213 pSidebarController->NotifyResize();
218 void SAL_CALL SfxUnoDeck::moveDown()
220 SolarMutexGuard aGuard;
221 SidebarController* pSidebarController = getSidebarController();
223 ResourceManager::DeckContextDescriptorContainer aDecks = pSidebarController->GetMatchingDecks();
225 // Search for next deck OrderIndex
226 sal_Int32 curOrderIndex = getOrderIndex();
227 sal_Int32 nextIndex = GetMaxOrderIndex(aDecks);
229 for (auto const& deck : aDecks)
231 sal_Int32 index = pSidebarController->GetResourceManager()->GetDeckDescriptor(deck.msId)->mnOrderIndex;
232 if( index > curOrderIndex && index < nextIndex)
233 nextIndex = index;
236 if (curOrderIndex != nextIndex) // is deck already in place ?
238 nextIndex += 1;
239 std::shared_ptr<DeckDescriptor> xDeckDescriptor = pSidebarController->GetResourceManager()->GetDeckDescriptor(mDeckId);
240 if (xDeckDescriptor)
242 xDeckDescriptor->mnOrderIndex = nextIndex;
243 // update the sidebar
244 pSidebarController->NotifyResize();
249 sal_Int32 SfxUnoDeck::GetMinOrderIndex(const ResourceManager::DeckContextDescriptorContainer& rDecks)
251 SidebarController* pSidebarController = getSidebarController();
253 ResourceManager::DeckContextDescriptorContainer::const_iterator iDeck = rDecks.begin();
254 sal_Int32 minIndex = pSidebarController->GetResourceManager()->GetDeckDescriptor(iDeck->msId)->mnOrderIndex;
256 for (auto const& deck : rDecks)
258 sal_Int32 index = pSidebarController->GetResourceManager()->GetDeckDescriptor(deck.msId)->mnOrderIndex;
259 if(minIndex > index)
260 minIndex = index;
262 return minIndex;
265 sal_Int32 SfxUnoDeck::GetMaxOrderIndex(const ResourceManager::DeckContextDescriptorContainer& rDecks)
267 SidebarController* pSidebarController = getSidebarController();
269 sal_Int32 maxIndex = pSidebarController->GetResourceManager()->GetDeckDescriptor(rDecks.begin()->msId)->mnOrderIndex;
271 for (auto const& deck : rDecks)
273 sal_Int32 index = pSidebarController->GetResourceManager()->GetDeckDescriptor(deck.msId)->mnOrderIndex;
274 if(maxIndex < index)
275 maxIndex = index;
277 return maxIndex;
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */