Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / sidebar / AllMasterPagesSelector.cxx
blob6c768d00d2aed02b6a63a6e258c7cf73f662c2f1
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 "AllMasterPagesSelector.hxx"
21 #include "PreviewValueSet.hxx"
22 #include <ViewShellBase.hxx>
23 #include "MasterPageContainer.hxx"
24 #include "MasterPageDescriptor.hxx"
25 #include <app.hrc>
26 #include <helpids.h>
28 #include <tools/link.hxx>
29 #include <set>
31 namespace {
33 using namespace sd::sidebar;
35 int GetURLPriority (const SharedMasterPageDescriptor& rpDescriptor)
37 int nPriority (0);
38 switch (rpDescriptor->GetURLClassification())
40 case MasterPageDescriptor::URLCLASS_USER: nPriority = 0; break;
41 case MasterPageDescriptor::URLCLASS_LAYOUT: nPriority = 1; break;
42 case MasterPageDescriptor::URLCLASS_PRESENTATION: nPriority = 2; break;
43 case MasterPageDescriptor::URLCLASS_OTHER: nPriority = 3; break;
44 case MasterPageDescriptor::URLCLASS_UNKNOWN: nPriority = 4; break;
45 default:
46 case MasterPageDescriptor::URLCLASS_UNDETERMINED: nPriority = 5; break;
48 return nPriority;
51 class MasterPageDescriptorOrder
53 public:
54 bool operator() (
55 const SharedMasterPageDescriptor& rp1,
56 const SharedMasterPageDescriptor& rp2) const
58 if (rp1->meOrigin == MasterPageContainer::DEFAULT)
59 return true;
60 else if (rp2->meOrigin == MasterPageContainer::DEFAULT)
61 return false;
62 else if (rp1->GetURLClassification() == rp2->GetURLClassification())
63 return rp1->mnTemplateIndex < rp2->mnTemplateIndex;
64 else
65 return GetURLPriority(rp1) < GetURLPriority(rp2);
69 } // end of anonymous namespace
71 namespace sd { namespace sidebar {
73 class AllMasterPagesSelector::SortedMasterPageDescriptorList
74 : public ::std::set<SharedMasterPageDescriptor,MasterPageDescriptorOrder>
76 public:
77 SortedMasterPageDescriptorList() {}
80 VclPtr<vcl::Window> AllMasterPagesSelector::Create (
81 vcl::Window* pParent,
82 ViewShellBase& rViewShellBase,
83 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
85 SdDrawDocument* pDocument = rViewShellBase.GetDocument();
86 if (pDocument == nullptr)
87 return nullptr;
89 std::shared_ptr<MasterPageContainer> pContainer (new MasterPageContainer());
91 VclPtrInstance<AllMasterPagesSelector> pSelector(
92 pParent,
93 *pDocument,
94 rViewShellBase,
95 pContainer,
96 rxSidebar);
97 pSelector->LateInit();
98 pSelector->SetHelpId(HID_SD_TASK_PANE_PREVIEW_ALL);
100 return pSelector;
103 AllMasterPagesSelector::AllMasterPagesSelector (
104 vcl::Window* pParent,
105 SdDrawDocument& rDocument,
106 ViewShellBase& rBase,
107 const std::shared_ptr<MasterPageContainer>& rpContainer,
108 const css::uno::Reference<css::ui::XSidebar>& rxSidebar)
109 : MasterPagesSelector(pParent, rDocument, rBase, rpContainer, rxSidebar),
110 mpSortedMasterPages(new SortedMasterPageDescriptorList())
112 MasterPagesSelector::Fill();
115 AllMasterPagesSelector::~AllMasterPagesSelector()
119 void AllMasterPagesSelector::Fill (ItemList& rItemList)
121 if (mpSortedMasterPages->empty())
122 UpdateMasterPageList();
123 UpdatePageSet(rItemList);
126 void AllMasterPagesSelector::NotifyContainerChangeEvent (
127 const MasterPageContainerChangeEvent& rEvent)
129 switch (rEvent.meEventType)
131 case MasterPageContainerChangeEvent::EventType::CHILD_ADDED:
132 AddItem(rEvent.maChildToken);
133 MasterPagesSelector::Fill();
134 break;
136 case MasterPageContainerChangeEvent::EventType::INDEX_CHANGED:
137 mpSortedMasterPages->clear();
138 MasterPagesSelector::Fill();
139 break;
141 default:
142 MasterPagesSelector::NotifyContainerChangeEvent(rEvent);
143 break;
147 void AllMasterPagesSelector::UpdateMasterPageList()
149 mpSortedMasterPages->clear();
150 int nTokenCount = mpContainer->GetTokenCount();
151 for (int i=0; i<nTokenCount; i++)
152 AddItem(mpContainer->GetTokenForIndex(i));
155 void AllMasterPagesSelector::AddItem (MasterPageContainer::Token aToken)
157 switch (mpContainer->GetOriginForToken(aToken))
159 case MasterPageContainer::DEFAULT:
160 case MasterPageContainer::TEMPLATE:
161 // Templates are added only when coming from the
162 // MasterPageContainerFiller so that they have an id which
163 // defines their place in the list. Templates (pre) loaded from
164 // RecentlyUsedMasterPages are ignored (they will be loaded
165 // later by the MasterPageContainerFiller.)
166 if (mpContainer->GetTemplateIndexForToken(aToken) >= 0)
167 mpSortedMasterPages->insert(mpContainer->GetDescriptorForToken(aToken));
168 break;
170 default:
171 break;
175 void AllMasterPagesSelector::UpdatePageSet (ItemList& rItemList)
177 SortedMasterPageDescriptorList::const_iterator iDescriptor;
178 SortedMasterPageDescriptorList::const_iterator iEnd (mpSortedMasterPages->end());
179 for (iDescriptor=mpSortedMasterPages->begin(); iDescriptor!=iEnd; ++iDescriptor)
180 rItemList.push_back((*iDescriptor)->maToken);
183 } } // end of namespace sd::sidebar
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */