Get the style color and number just once
[LibreOffice.git] / sd / source / ui / sidebar / CurrentMasterPagesSelector.cxx
blobc1ac330b731445de46efda39180d486f72f4fbcf
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 "CurrentMasterPagesSelector.hxx"
21 #include <ViewShellBase.hxx>
22 #include <DrawViewShell.hxx>
23 #include <drawdoc.hxx>
24 #include <sdpage.hxx>
25 #include "MasterPageContainer.hxx"
26 #include "MasterPageContainerProviders.hxx"
27 #include "MasterPageDescriptor.hxx"
28 #include <EventMultiplexer.hxx>
29 #include <DrawDocShell.hxx>
30 #include <osl/diagnose.h>
32 #include <helpids.h>
34 #include <set>
36 using namespace ::com::sun::star;
38 namespace sd::sidebar {
40 std::unique_ptr<PanelLayout> CurrentMasterPagesSelector::Create (
41 weld::Widget* pParent,
42 ViewShellBase& rViewShellBase,
43 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
45 SdDrawDocument* pDocument = rViewShellBase.GetDocument();
46 if (pDocument == nullptr)
47 return nullptr;
49 auto pContainer = std::make_shared<MasterPageContainer>();
51 auto xSelector(std::make_unique<CurrentMasterPagesSelector>(
52 pParent,
53 *pDocument,
54 rViewShellBase,
55 pContainer,
56 rxSidebar));
57 xSelector->LateInit();
58 xSelector->SetHelpId( HID_SD_TASK_PANE_PREVIEW_CURRENT );
60 return xSelector;
63 CurrentMasterPagesSelector::CurrentMasterPagesSelector(
64 weld::Widget* pParent, SdDrawDocument& rDocument, ViewShellBase& rBase,
65 const std::shared_ptr<MasterPageContainer>& rpContainer,
66 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
67 : MasterPagesSelector(pParent, rDocument, rBase, rpContainer, rxSidebar,
68 u"modules/simpress/ui/masterpagepanel.ui"_ustr,
69 u"masterpagecurrent_icons"_ustr)
71 Link<sd::tools::EventMultiplexerEvent&, void> aLink(
72 LINK(this, CurrentMasterPagesSelector, EventMultiplexerListener));
73 rBase.GetEventMultiplexer()->AddEventListener(aLink);
76 CurrentMasterPagesSelector::~CurrentMasterPagesSelector()
78 if (mrDocument.GetDocSh() != nullptr)
80 EndListening(*mrDocument.GetDocSh());
82 else
84 OSL_ASSERT(mrDocument.GetDocSh() != nullptr);
87 Link<sd::tools::EventMultiplexerEvent&,void> aLink (LINK(this,CurrentMasterPagesSelector,EventMultiplexerListener));
88 mrBase.GetEventMultiplexer()->RemoveEventListener(aLink);
91 void CurrentMasterPagesSelector::LateInit()
93 MasterPagesSelector::LateInit();
94 MasterPagesSelector::Fill();
95 if (mrDocument.GetDocSh() != nullptr)
97 StartListening(*mrDocument.GetDocSh());
99 else
101 OSL_ASSERT(mrDocument.GetDocSh() != nullptr);
105 void CurrentMasterPagesSelector::Fill (ItemList& rItemList)
107 sal_uInt16 nPageCount = mrDocument.GetMasterSdPageCount(PageKind::Standard);
108 // Remember the names of the master pages that have been inserted to
109 // avoid double insertion.
110 ::std::set<OUString> aMasterPageNames;
111 for (sal_uInt16 nIndex=0; nIndex<nPageCount; nIndex++)
113 SdPage* pMasterPage = mrDocument.GetMasterSdPage (nIndex, PageKind::Standard);
114 if (pMasterPage == nullptr)
115 continue;
117 // Use the name of the master page to avoid duplicate entries.
118 OUString sName (pMasterPage->GetName());
119 if (!aMasterPageNames.insert(sName).second)
120 continue;
122 // Look up the master page in the container and, when it is not yet
123 // in it, insert it.
124 MasterPageContainer::Token aToken = mpContainer->GetTokenForPageObject(pMasterPage);
125 if (aToken == MasterPageContainer::NIL_TOKEN)
127 SharedMasterPageDescriptor pDescriptor = std::make_shared<MasterPageDescriptor>(
128 MasterPageContainer::MASTERPAGE,
129 nIndex,
130 OUString(),
131 pMasterPage->GetName(),
132 OUString(),
133 pMasterPage->IsPrecious(),
134 std::make_shared<ExistingPageProvider>(pMasterPage),
135 std::make_shared<PagePreviewProvider>());
136 aToken = mpContainer->PutMasterPage(pDescriptor);
139 rItemList.push_back(aToken);
143 OUString CurrentMasterPagesSelector::GetContextMenuUIFile() const
145 return u"modules/simpress/ui/currentmastermenu.ui"_ustr;
148 void CurrentMasterPagesSelector::UpdateSelection()
150 // Iterate over all pages and for the selected ones put the name of
151 // their master page into a set.
152 sal_uInt16 nPageCount = mrDocument.GetSdPageCount(PageKind::Standard);
153 ::std::set<OUString> aNames;
154 sal_uInt16 nIndex;
155 bool bLoop (true);
156 for (nIndex=0; nIndex<nPageCount && bLoop; nIndex++)
158 SdPage* pPage = mrDocument.GetSdPage (nIndex, PageKind::Standard);
159 if (pPage != nullptr && pPage->IsSelected())
161 if ( ! pPage->TRG_HasMasterPage())
163 // One of the pages has no master page. This is an
164 // indicator for that this method is called in the middle of
165 // a document change and that the model is not in a valid
166 // state. Therefore we stop update the selection and wait
167 // for another call to UpdateSelection when the model is
168 // valid again.
169 bLoop = false;
171 else
173 SdrPage& rMasterPage (pPage->TRG_GetMasterPage());
174 assert(dynamic_cast<SdPage*>(&rMasterPage));
175 aNames.insert(static_cast<SdPage&>(rMasterPage).GetName());
181 void CurrentMasterPagesSelector::ExecuteCommand(const OUString &rIdent)
183 if (rIdent == "delete")
185 // Check once again that the master page can safely be deleted,
186 // i.e. is not used.
187 SdPage* pMasterPage = GetSelectedMasterPage();
188 if (pMasterPage != nullptr
189 && mrDocument.GetMasterPageUserCount(pMasterPage) == 0)
191 // Removing the precious flag so that the following call to
192 // RemoveUnnecessaryMasterPages() will remove this master page.
193 pMasterPage->SetPrecious(false);
194 mrDocument.RemoveUnnecessaryMasterPages(pMasterPage);
197 else
198 MasterPagesSelector::ExecuteCommand(rIdent);
201 void CurrentMasterPagesSelector::ProcessPopupMenu(weld::Menu& rMenu)
203 // Disable the delete entry when there is only one master page.
204 if (mrDocument.GetMasterPageUserCount(GetSelectedMasterPage()) > 0)
205 rMenu.set_sensitive(u"delete"_ustr, false);
207 std::shared_ptr<DrawViewShell> pDrawViewShell (
208 std::dynamic_pointer_cast<DrawViewShell>(mrBase.GetMainViewShell()));
209 if (pDrawViewShell && pDrawViewShell->GetEditMode() == EditMode::MasterPage)
211 rMenu.set_sensitive(u"edit"_ustr, false);
214 MasterPagesSelector::ProcessPopupMenu(rMenu);
217 IMPL_LINK(CurrentMasterPagesSelector,EventMultiplexerListener,
218 sd::tools::EventMultiplexerEvent&, rEvent, void)
220 switch (rEvent.meEventId)
222 case EventMultiplexerEventId::CurrentPageChanged:
223 case EventMultiplexerEventId::EditModeNormal:
224 case EventMultiplexerEventId::EditModeMaster:
225 case EventMultiplexerEventId::SlideSortedSelection:
226 UpdateSelection();
227 break;
229 case EventMultiplexerEventId::PageOrder:
230 // This is tricky. If a master page is removed, moved, or
231 // added we have to wait until both the notes master page
232 // and the standard master page have been removed, moved,
233 // or added. We do this by looking at the number of master
234 // pages which has to be odd in the consistent state (the
235 // handout master page is always present). If the number is
236 // even we ignore the hint.
237 if (mrBase.GetDocument()->GetMasterPageCount()%2 == 1)
238 MasterPagesSelector::Fill();
239 break;
241 case EventMultiplexerEventId::ShapeChanged:
242 case EventMultiplexerEventId::ShapeInserted:
243 case EventMultiplexerEventId::ShapeRemoved:
244 InvalidatePreview(static_cast<const SdPage*>(rEvent.mpUserData));
245 break;
246 default: break;
250 } // end of namespace sd::sidebar
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */