1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include "MasterPageContainer.hxx"
27 #include "MasterPageDescriptor.hxx"
28 #include <EventMultiplexer.hxx>
30 #include <DrawDocShell.hxx>
34 #include <vcl/image.hxx>
35 #include <svx/svdmodel.hxx>
36 #include <sfx2/request.hxx>
40 using namespace ::com::sun::star
;
42 namespace sd
{ namespace sidebar
{
44 VclPtr
<vcl::Window
> CurrentMasterPagesSelector::Create (
46 ViewShellBase
& rViewShellBase
,
47 const css::uno::Reference
<css::ui::XSidebar
>& rxSidebar
)
49 SdDrawDocument
* pDocument
= rViewShellBase
.GetDocument();
50 if (pDocument
== nullptr)
53 std::shared_ptr
<MasterPageContainer
> pContainer (new MasterPageContainer());
55 VclPtrInstance
<CurrentMasterPagesSelector
> pSelector(
61 pSelector
->LateInit();
62 pSelector
->SetHelpId( HID_SD_TASK_PANE_PREVIEW_CURRENT
);
67 CurrentMasterPagesSelector::CurrentMasterPagesSelector (
69 SdDrawDocument
& rDocument
,
71 const std::shared_ptr
<MasterPageContainer
>& rpContainer
,
72 const css::uno::Reference
<css::ui::XSidebar
>& rxSidebar
)
73 : MasterPagesSelector (pParent
, rDocument
, rBase
, rpContainer
, rxSidebar
)
75 Link
<sd::tools::EventMultiplexerEvent
&,void> aLink (LINK(this,CurrentMasterPagesSelector
,EventMultiplexerListener
));
76 rBase
.GetEventMultiplexer()->AddEventListener(aLink
);
79 CurrentMasterPagesSelector::~CurrentMasterPagesSelector()
84 void CurrentMasterPagesSelector::dispose()
86 if (mrDocument
.GetDocSh() != nullptr)
88 EndListening(*mrDocument
.GetDocSh());
92 OSL_ASSERT(mrDocument
.GetDocSh() != nullptr);
95 Link
<sd::tools::EventMultiplexerEvent
&,void> aLink (LINK(this,CurrentMasterPagesSelector
,EventMultiplexerListener
));
96 mrBase
.GetEventMultiplexer()->RemoveEventListener(aLink
);
98 MasterPagesSelector::dispose();
101 void CurrentMasterPagesSelector::LateInit()
103 MasterPagesSelector::LateInit();
104 MasterPagesSelector::Fill();
105 if (mrDocument
.GetDocSh() != nullptr)
107 StartListening(*mrDocument
.GetDocSh());
111 OSL_ASSERT(mrDocument
.GetDocSh() != nullptr);
115 void CurrentMasterPagesSelector::Fill (ItemList
& rItemList
)
117 sal_uInt16 nPageCount
= mrDocument
.GetMasterSdPageCount(PageKind::Standard
);
118 // Remember the names of the master pages that have been inserted to
119 // avoid double insertion.
120 ::std::set
<OUString
> aMasterPageNames
;
121 for (sal_uInt16 nIndex
=0; nIndex
<nPageCount
; nIndex
++)
123 SdPage
* pMasterPage
= mrDocument
.GetMasterSdPage (nIndex
, PageKind::Standard
);
124 if (pMasterPage
== nullptr)
127 // Use the name of the master page to avoid duplicate entries.
128 OUString
sName (pMasterPage
->GetName());
129 if (aMasterPageNames
.find(sName
)!=aMasterPageNames
.end())
131 aMasterPageNames
.insert (sName
);
133 // Look up the master page in the container and, when it is not yet
135 MasterPageContainer::Token aToken
= mpContainer
->GetTokenForPageObject(pMasterPage
);
136 if (aToken
== MasterPageContainer::NIL_TOKEN
)
138 SharedMasterPageDescriptor
pDescriptor (new MasterPageDescriptor(
139 MasterPageContainer::MASTERPAGE
,
142 pMasterPage
->GetName(),
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
;
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
184 SdrPage
& rMasterPage (pPage
->TRG_GetMasterPage());
185 SdPage
* pMasterPage
= static_cast<SdPage
*>(&rMasterPage
);
186 if (pMasterPage
!= nullptr)
187 aNames
.insert (pMasterPage
->GetName());
192 // Find the items for the master pages in the set.
193 sal_uInt16
nItemCount (PreviewValueSet::GetItemCount());
194 for (nIndex
=1; nIndex
<=nItemCount
&& bLoop
; nIndex
++)
196 OUString
sName (PreviewValueSet::GetItemText (nIndex
));
197 if (aNames
.find(sName
) != aNames
.end())
199 PreviewValueSet::SelectItem (nIndex
);
204 void CurrentMasterPagesSelector::ExecuteCommand(const OString
&rIdent
)
206 if (rIdent
== "delete")
208 // Check once again that the master page can safely be deleted,
210 SdPage
* pMasterPage
= GetSelectedMasterPage();
211 if (pMasterPage
!= nullptr
212 && mrDocument
.GetMasterPageUserCount(pMasterPage
) == 0)
214 // Removing the precious flag so that the following call to
215 // RemoveUnnessesaryMasterPages() will remove this master page.
216 pMasterPage
->SetPrecious(false);
217 mrDocument
.RemoveUnnecessaryMasterPages(pMasterPage
);
221 MasterPagesSelector::ExecuteCommand(rIdent
);
224 void CurrentMasterPagesSelector::ProcessPopupMenu (Menu
& rMenu
)
226 // Disable the delete entry when there is only one master page.
227 if (mrDocument
.GetMasterPageUserCount(GetSelectedMasterPage()) > 0)
229 sal_uInt16 nItemid
= rMenu
.GetItemId("delete");
230 if (rMenu
.GetItemPos(nItemid
) != MENU_ITEM_NOTFOUND
)
231 rMenu
.EnableItem(nItemid
, false);
234 std::shared_ptr
<DrawViewShell
> pDrawViewShell (
235 std::dynamic_pointer_cast
<DrawViewShell
>(mrBase
.GetMainViewShell()));
237 && pDrawViewShell
->GetEditMode() == EditMode::MasterPage
)
239 sal_uInt16 nItemid
= rMenu
.GetItemId("edit");
240 if (rMenu
.GetItemPos(nItemid
) != MENU_ITEM_NOTFOUND
)
241 rMenu
.EnableItem(nItemid
, false);
244 MasterPagesSelector::ProcessPopupMenu(rMenu
);
247 IMPL_LINK(CurrentMasterPagesSelector
,EventMultiplexerListener
,
248 sd::tools::EventMultiplexerEvent
&, rEvent
, void)
250 switch (rEvent
.meEventId
)
252 case EventMultiplexerEventId::CurrentPageChanged
:
253 case EventMultiplexerEventId::EditModeNormal
:
254 case EventMultiplexerEventId::EditModeMaster
:
255 case EventMultiplexerEventId::SlideSortedSelection
:
259 case EventMultiplexerEventId::PageOrder
:
260 // This is tricky. If a master page is removed, moved, or
261 // added we have to wait until both the notes master page
262 // and the standard master page have been removed, moved,
263 // or added. We do this by looking at the number of master
264 // pages which has to be odd in the consistent state (the
265 // handout master page is always present). If the number is
266 // even we ignore the hint.
267 if (mrBase
.GetDocument()->GetMasterPageCount()%2 == 1)
268 MasterPagesSelector::Fill();
271 case EventMultiplexerEventId::ShapeChanged
:
272 case EventMultiplexerEventId::ShapeInserted
:
273 case EventMultiplexerEventId::ShapeRemoved
:
274 InvalidatePreview(static_cast<const SdPage
*>(rEvent
.mpUserData
));
280 } } // end of namespace sd::sidebar
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */