bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / sidebar / CurrentMasterPagesSelector.cxx
blobc7729447d13021d0082e3894e8abbcfe3abe833a
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 "MasterPageDescriptor.hxx"
28 #include "EventMultiplexer.hxx"
29 #include "app.hrc"
30 #include "DrawDocShell.hxx"
31 #include "res_bmp.hrc"
32 #include "sdresid.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 == NULL)
52 return NULL;
54 ::boost::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 ::boost::shared_ptr<MasterPageContainer>& rpContainer,
73 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
74 : MasterPagesSelector (pParent, rDocument, rBase, rpContainer, rxSidebar)
76 // For this master page selector only we change the default action for
77 // left clicks.
78 mnDefaultClickAction = SID_TP_APPLY_TO_SELECTED_SLIDES;
80 Link<> aLink (LINK(this,CurrentMasterPagesSelector,EventMultiplexerListener));
81 rBase.GetEventMultiplexer()->AddEventListener(aLink,
82 sd::tools::EventMultiplexerEvent::EID_CURRENT_PAGE
83 | sd::tools::EventMultiplexerEvent::EID_EDIT_MODE_NORMAL
84 | sd::tools::EventMultiplexerEvent::EID_EDIT_MODE_MASTER
85 | sd::tools::EventMultiplexerEvent::EID_PAGE_ORDER
86 | sd::tools::EventMultiplexerEvent::EID_SHAPE_CHANGED
87 | sd::tools::EventMultiplexerEvent::EID_SHAPE_INSERTED
88 | sd::tools::EventMultiplexerEvent::EID_SHAPE_REMOVED);
91 CurrentMasterPagesSelector::~CurrentMasterPagesSelector()
93 disposeOnce();
96 void CurrentMasterPagesSelector::dispose()
98 if (mrDocument.GetDocSh() != NULL)
100 EndListening(*mrDocument.GetDocSh());
102 else
104 OSL_ASSERT(mrDocument.GetDocSh() != NULL);
107 Link<> aLink (LINK(this,CurrentMasterPagesSelector,EventMultiplexerListener));
108 mrBase.GetEventMultiplexer()->RemoveEventListener(aLink);
110 MasterPagesSelector::dispose();
113 void CurrentMasterPagesSelector::LateInit()
115 MasterPagesSelector::LateInit();
116 MasterPagesSelector::Fill();
117 if (mrDocument.GetDocSh() != NULL)
119 StartListening(*mrDocument.GetDocSh());
121 else
123 OSL_ASSERT(mrDocument.GetDocSh() != NULL);
127 void CurrentMasterPagesSelector::Fill (ItemList& rItemList)
129 sal_uInt16 nPageCount = mrDocument.GetMasterSdPageCount(PK_STANDARD);
130 // Remember the names of the master pages that have been inserted to
131 // avoid double insertion.
132 ::std::set<OUString> aMasterPageNames;
133 for (sal_uInt16 nIndex=0; nIndex<nPageCount; nIndex++)
135 SdPage* pMasterPage = mrDocument.GetMasterSdPage (nIndex, PK_STANDARD);
136 if (pMasterPage == NULL)
137 continue;
139 // Use the name of the master page to avoid duplicate entries.
140 OUString sName (pMasterPage->GetName());
141 if (aMasterPageNames.find(sName)!=aMasterPageNames.end())
142 continue;
143 aMasterPageNames.insert (sName);
145 // Look up the master page in the container and, when it is not yet
146 // in it, insert it.
147 MasterPageContainer::Token aToken = mpContainer->GetTokenForPageObject(pMasterPage);
148 if (aToken == MasterPageContainer::NIL_TOKEN)
150 SharedMasterPageDescriptor pDescriptor (new MasterPageDescriptor(
151 MasterPageContainer::MASTERPAGE,
152 nIndex,
153 OUString(),
154 pMasterPage->GetName(),
155 OUString(),
156 pMasterPage->IsPrecious(),
157 ::boost::shared_ptr<PageObjectProvider>(new ExistingPageProvider(pMasterPage)),
158 ::boost::shared_ptr<PreviewProvider>(new PagePreviewProvider())));
159 aToken = mpContainer->PutMasterPage(pDescriptor);
162 rItemList.push_back(aToken);
166 ResId CurrentMasterPagesSelector::GetContextMenuResId() const
168 return SdResId(RID_TASKPANE_CURRENT_MASTERPAGESSELECTOR_POPUP);
171 void CurrentMasterPagesSelector::UpdateSelection()
173 // Iterate over all pages and for the selected ones put the name of
174 // their master page into a set.
175 sal_uInt16 nPageCount = mrDocument.GetSdPageCount(PK_STANDARD);
176 ::std::set<OUString> aNames;
177 sal_uInt16 nIndex;
178 bool bLoop (true);
179 for (nIndex=0; nIndex<nPageCount && bLoop; nIndex++)
181 SdPage* pPage = mrDocument.GetSdPage (nIndex, PK_STANDARD);
182 if (pPage != NULL && pPage->IsSelected())
184 if ( ! pPage->TRG_HasMasterPage())
186 // One of the pages has no master page. This is an
187 // indicator for that this method is called in the middle of
188 // a document change and that the model is not in a valid
189 // state. Therefore we stop update the selection and wait
190 // for another call to UpdateSelection when the model is
191 // valid again.
192 bLoop = false;
194 else
196 SdrPage& rMasterPage (pPage->TRG_GetMasterPage());
197 SdPage* pMasterPage = static_cast<SdPage*>(&rMasterPage);
198 if (pMasterPage != NULL)
199 aNames.insert (pMasterPage->GetName());
204 // Find the items for the master pages in the set.
205 sal_uInt16 nItemCount (PreviewValueSet::GetItemCount());
206 for (nIndex=1; nIndex<=nItemCount && bLoop; nIndex++)
208 OUString sName (PreviewValueSet::GetItemText (nIndex));
209 if (aNames.find(sName) != aNames.end())
211 PreviewValueSet::SelectItem (nIndex);
216 void CurrentMasterPagesSelector::ExecuteCommand (const sal_Int32 nCommandId)
218 if (nCommandId == SID_DELETE_MASTER_PAGE)
220 // Check once again that the master page can safely be deleted,
221 // i.e. is not used.
222 SdPage* pMasterPage = GetSelectedMasterPage();
223 if (pMasterPage != NULL
224 && mrDocument.GetMasterPageUserCount(pMasterPage) == 0)
226 // Removing the precious flag so that the following call to
227 // RemoveUnnessesaryMasterPages() will remove this master page.
228 pMasterPage->SetPrecious(false);
229 mrDocument.RemoveUnnecessaryMasterPages(pMasterPage, false, true);
232 else
233 MasterPagesSelector::ExecuteCommand(nCommandId);
236 void CurrentMasterPagesSelector::ProcessPopupMenu (Menu& rMenu)
238 // Disable the SID_DELTE_MASTER slot when there is only one master page.
239 if (mrDocument.GetMasterPageUserCount(GetSelectedMasterPage()) > 0)
241 if (rMenu.GetItemPos(SID_DELETE_MASTER_PAGE) != MENU_ITEM_NOTFOUND)
242 rMenu.EnableItem(SID_DELETE_MASTER_PAGE, false);
245 ::boost::shared_ptr<DrawViewShell> pDrawViewShell (
246 ::boost::dynamic_pointer_cast<DrawViewShell>(mrBase.GetMainViewShell()));
247 if (pDrawViewShell
248 && pDrawViewShell->GetEditMode() == EM_MASTERPAGE)
250 if (rMenu.GetItemPos(SID_TP_EDIT_MASTER) != MENU_ITEM_NOTFOUND)
251 rMenu.EnableItem(SID_TP_EDIT_MASTER, false);
254 MasterPagesSelector::ProcessPopupMenu(rMenu);
257 IMPL_LINK(CurrentMasterPagesSelector,EventMultiplexerListener,
258 sd::tools::EventMultiplexerEvent*,pEvent)
260 if (pEvent != NULL)
262 switch (pEvent->meEventId)
264 case sd::tools::EventMultiplexerEvent::EID_CURRENT_PAGE:
265 case sd::tools::EventMultiplexerEvent::EID_EDIT_MODE_NORMAL:
266 case sd::tools::EventMultiplexerEvent::EID_EDIT_MODE_MASTER:
267 case sd::tools::EventMultiplexerEvent::EID_SLIDE_SORTER_SELECTION:
268 UpdateSelection();
269 break;
271 case sd::tools::EventMultiplexerEvent::EID_PAGE_ORDER:
272 // This is tricky. If a master page is removed, moved, or
273 // added we have to wait until both the notes master page
274 // and the standard master page have been removed, moved,
275 // or added. We do this by looking at the number of master
276 // pages which has to be odd in the consistent state (the
277 // handout master page is always present). If the number is
278 // even we ignore the hint.
279 if (mrBase.GetDocument()->GetMasterPageCount()%2 == 1)
280 MasterPagesSelector::Fill();
281 break;
283 case sd::tools::EventMultiplexerEvent::EID_SHAPE_CHANGED:
284 case sd::tools::EventMultiplexerEvent::EID_SHAPE_INSERTED:
285 case sd::tools::EventMultiplexerEvent::EID_SHAPE_REMOVED:
286 InvalidatePreview(static_cast<const SdPage*>(pEvent->mpUserData));
287 break;
291 return 0;
294 } } // end of namespace sd::sidebar
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */