bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / sidebar / CurrentMasterPagesSelector.cxx
bloba7a8880bcbf63b32452d262cb673ca10360de023
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 "PreviewValueSet.hxx"
22 #include <ViewShellBase.hxx>
23 #include <DrawViewShell.hxx>
24 #include <drawdoc.hxx>
25 #include <sdpage.hxx>
26 #include "MasterPageContainer.hxx"
27 #include "MasterPageContainerProviders.hxx"
28 #include "MasterPageDescriptor.hxx"
29 #include <EventMultiplexer.hxx>
30 #include <app.hrc>
31 #include <DrawDocShell.hxx>
33 #include <helpids.h>
35 #include <vcl/image.hxx>
36 #include <svx/svdmodel.hxx>
37 #include <sfx2/request.hxx>
39 #include <set>
41 using namespace ::com::sun::star;
43 namespace sd { namespace sidebar {
45 VclPtr<vcl::Window> CurrentMasterPagesSelector::Create (
46 vcl::Window* pParent,
47 ViewShellBase& rViewShellBase,
48 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
50 SdDrawDocument* pDocument = rViewShellBase.GetDocument();
51 if (pDocument == nullptr)
52 return nullptr;
54 std::shared_ptr<MasterPageContainer> pContainer (new MasterPageContainer());
56 VclPtrInstance<CurrentMasterPagesSelector> pSelector(
57 pParent,
58 *pDocument,
59 rViewShellBase,
60 pContainer,
61 rxSidebar);
62 pSelector->LateInit();
63 pSelector->SetHelpId( HID_SD_TASK_PANE_PREVIEW_CURRENT );
65 return pSelector;
68 CurrentMasterPagesSelector::CurrentMasterPagesSelector (
69 vcl::Window* pParent,
70 SdDrawDocument& rDocument,
71 ViewShellBase& rBase,
72 const std::shared_ptr<MasterPageContainer>& rpContainer,
73 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
74 : MasterPagesSelector (pParent, rDocument, rBase, rpContainer, rxSidebar)
76 Link<sd::tools::EventMultiplexerEvent&,void> aLink (LINK(this,CurrentMasterPagesSelector,EventMultiplexerListener));
77 rBase.GetEventMultiplexer()->AddEventListener(aLink);
80 CurrentMasterPagesSelector::~CurrentMasterPagesSelector()
82 disposeOnce();
85 void CurrentMasterPagesSelector::dispose()
87 if (mrDocument.GetDocSh() != nullptr)
89 EndListening(*mrDocument.GetDocSh());
91 else
93 OSL_ASSERT(mrDocument.GetDocSh() != nullptr);
96 Link<sd::tools::EventMultiplexerEvent&,void> aLink (LINK(this,CurrentMasterPagesSelector,EventMultiplexerListener));
97 mrBase.GetEventMultiplexer()->RemoveEventListener(aLink);
99 MasterPagesSelector::dispose();
102 void CurrentMasterPagesSelector::LateInit()
104 MasterPagesSelector::LateInit();
105 MasterPagesSelector::Fill();
106 if (mrDocument.GetDocSh() != nullptr)
108 StartListening(*mrDocument.GetDocSh());
110 else
112 OSL_ASSERT(mrDocument.GetDocSh() != nullptr);
116 void CurrentMasterPagesSelector::Fill (ItemList& rItemList)
118 sal_uInt16 nPageCount = mrDocument.GetMasterSdPageCount(PageKind::Standard);
119 // Remember the names of the master pages that have been inserted to
120 // avoid double insertion.
121 ::std::set<OUString> aMasterPageNames;
122 for (sal_uInt16 nIndex=0; nIndex<nPageCount; nIndex++)
124 SdPage* pMasterPage = mrDocument.GetMasterSdPage (nIndex, PageKind::Standard);
125 if (pMasterPage == nullptr)
126 continue;
128 // Use the name of the master page to avoid duplicate entries.
129 OUString sName (pMasterPage->GetName());
130 if (!aMasterPageNames.insert(sName).second)
131 continue;
133 // Look up the master page in the container and, when it is not yet
134 // in it, insert it.
135 MasterPageContainer::Token aToken = mpContainer->GetTokenForPageObject(pMasterPage);
136 if (aToken == MasterPageContainer::NIL_TOKEN)
138 SharedMasterPageDescriptor pDescriptor (new MasterPageDescriptor(
139 MasterPageContainer::MASTERPAGE,
140 nIndex,
141 OUString(),
142 pMasterPage->GetName(),
143 OUString(),
144 pMasterPage->IsPrecious(),
145 std::shared_ptr<PageObjectProvider>(new ExistingPageProvider(pMasterPage)),
146 std::shared_ptr<PreviewProvider>(new PagePreviewProvider())));
147 aToken = mpContainer->PutMasterPage(pDescriptor);
150 rItemList.push_back(aToken);
154 OUString CurrentMasterPagesSelector::GetContextMenuUIFile() const
156 return OUString("modules/simpress/ui/currentmastermenu.ui");
159 void CurrentMasterPagesSelector::UpdateSelection()
161 // Iterate over all pages and for the selected ones put the name of
162 // their master page into a set.
163 sal_uInt16 nPageCount = mrDocument.GetSdPageCount(PageKind::Standard);
164 ::std::set<OUString> aNames;
165 sal_uInt16 nIndex;
166 bool bLoop (true);
167 for (nIndex=0; nIndex<nPageCount && bLoop; nIndex++)
169 SdPage* pPage = mrDocument.GetSdPage (nIndex, PageKind::Standard);
170 if (pPage != nullptr && pPage->IsSelected())
172 if ( ! pPage->TRG_HasMasterPage())
174 // One of the pages has no master page. This is an
175 // indicator for that this method is called in the middle of
176 // a document change and that the model is not in a valid
177 // state. Therefore we stop update the selection and wait
178 // for another call to UpdateSelection when the model is
179 // valid again.
180 bLoop = false;
182 else
184 SdrPage& rMasterPage (pPage->TRG_GetMasterPage());
185 assert(dynamic_cast<SdPage*>(&rMasterPage));
186 aNames.insert(static_cast<SdPage&>(rMasterPage).GetName());
191 // Find the items for the master pages in the set.
192 sal_uInt16 nItemCount (PreviewValueSet::GetItemCount());
193 for (nIndex=1; nIndex<=nItemCount && bLoop; nIndex++)
195 OUString sName (PreviewValueSet::GetItemText (nIndex));
196 if (aNames.find(sName) != aNames.end())
198 PreviewValueSet::SelectItem (nIndex);
203 void CurrentMasterPagesSelector::ExecuteCommand(const OString &rIdent)
205 if (rIdent == "delete")
207 // Check once again that the master page can safely be deleted,
208 // i.e. is not used.
209 SdPage* pMasterPage = GetSelectedMasterPage();
210 if (pMasterPage != nullptr
211 && mrDocument.GetMasterPageUserCount(pMasterPage) == 0)
213 // Removing the precious flag so that the following call to
214 // RemoveUnnessesaryMasterPages() will remove this master page.
215 pMasterPage->SetPrecious(false);
216 mrDocument.RemoveUnnecessaryMasterPages(pMasterPage);
219 else
220 MasterPagesSelector::ExecuteCommand(rIdent);
223 void CurrentMasterPagesSelector::ProcessPopupMenu (Menu& rMenu)
225 // Disable the delete entry when there is only one master page.
226 if (mrDocument.GetMasterPageUserCount(GetSelectedMasterPage()) > 0)
228 sal_uInt16 nItemid = rMenu.GetItemId("delete");
229 if (rMenu.GetItemPos(nItemid) != MENU_ITEM_NOTFOUND)
230 rMenu.EnableItem(nItemid, false);
233 std::shared_ptr<DrawViewShell> pDrawViewShell (
234 std::dynamic_pointer_cast<DrawViewShell>(mrBase.GetMainViewShell()));
235 if (pDrawViewShell
236 && pDrawViewShell->GetEditMode() == EditMode::MasterPage)
238 sal_uInt16 nItemid = rMenu.GetItemId("edit");
239 if (rMenu.GetItemPos(nItemid) != MENU_ITEM_NOTFOUND)
240 rMenu.EnableItem(nItemid, false);
243 MasterPagesSelector::ProcessPopupMenu(rMenu);
246 IMPL_LINK(CurrentMasterPagesSelector,EventMultiplexerListener,
247 sd::tools::EventMultiplexerEvent&, rEvent, void)
249 switch (rEvent.meEventId)
251 case EventMultiplexerEventId::CurrentPageChanged:
252 case EventMultiplexerEventId::EditModeNormal:
253 case EventMultiplexerEventId::EditModeMaster:
254 case EventMultiplexerEventId::SlideSortedSelection:
255 UpdateSelection();
256 break;
258 case EventMultiplexerEventId::PageOrder:
259 // This is tricky. If a master page is removed, moved, or
260 // added we have to wait until both the notes master page
261 // and the standard master page have been removed, moved,
262 // or added. We do this by looking at the number of master
263 // pages which has to be odd in the consistent state (the
264 // handout master page is always present). If the number is
265 // even we ignore the hint.
266 if (mrBase.GetDocument()->GetMasterPageCount()%2 == 1)
267 MasterPagesSelector::Fill();
268 break;
270 case EventMultiplexerEventId::ShapeChanged:
271 case EventMultiplexerEventId::ShapeInserted:
272 case EventMultiplexerEventId::ShapeRemoved:
273 InvalidatePreview(static_cast<const SdPage*>(rEvent.mpUserData));
274 break;
275 default: break;
279 } } // end of namespace sd::sidebar
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */