1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sd.hxx"
23 
24 #include "AllMasterPagesSelector.hxx"
25 #include "PreviewValueSet.hxx"
26 #include "ViewShellBase.hxx"
27 #include "SidebarShellManager.hxx"
28 #include "MasterPageContainer.hxx"
29 #include "MasterPageDescriptor.hxx"
30 #include "app.hrc"
31 #include "helpids.h"
32 
33 #include <tools/link.hxx>
34 #include <set>
35 
36 namespace {
37 
38 using namespace sd::sidebar;
39 
GetURLPriority(const SharedMasterPageDescriptor & rpDescriptor)40 int GetURLPriority (const SharedMasterPageDescriptor& rpDescriptor)
41 {
42     int nPriority (0);
43     switch (rpDescriptor->GetURLClassification())
44     {
45         case MasterPageDescriptor::URLCLASS_USER:         nPriority = 0; break;
46         case MasterPageDescriptor::URLCLASS_LAYOUT:       nPriority = 1; break;
47         case MasterPageDescriptor::URLCLASS_PRESENTATION: nPriority = 2; break;
48         case MasterPageDescriptor::URLCLASS_OTHER:        nPriority = 3; break;
49         case MasterPageDescriptor::URLCLASS_UNKNOWN:      nPriority = 4; break;
50         default:
51         case MasterPageDescriptor::URLCLASS_UNDETERMINED: nPriority = 5; break;
52     }
53     return nPriority;
54 }
55 
56 
57 class MasterPageDescriptorOrder
58 {
59 public:
operator ()(const SharedMasterPageDescriptor & rp1,const SharedMasterPageDescriptor & rp2)60     bool operator() (
61         const SharedMasterPageDescriptor& rp1,
62         const SharedMasterPageDescriptor& rp2)
63     {
64         if (rp1->meOrigin == MasterPageContainer::DEFAULT)
65             return true;
66         else if (rp2->meOrigin == MasterPageContainer::DEFAULT)
67             return false;
68         else if (rp1->GetURLClassification() == rp2->GetURLClassification())
69             return rp1->mnTemplateIndex < rp2->mnTemplateIndex;
70         else
71             return GetURLPriority(rp1) < GetURLPriority(rp2);
72     }
73 };
74 
75 } // end of anonymous namespace
76 
77 
78 
79 namespace sd { namespace sidebar {
80 
81 class AllMasterPagesSelector::SortedMasterPageDescriptorList
82     : public ::std::set<SharedMasterPageDescriptor,MasterPageDescriptorOrder>
83 {
84 public:
SortedMasterPageDescriptorList(void)85     SortedMasterPageDescriptorList (void) {}
86 };
87 
88 
89 
90 
Create(::Window * pParent,ViewShellBase & rViewShellBase,const cssu::Reference<css::ui::XSidebar> & rxSidebar)91 MasterPagesSelector* AllMasterPagesSelector::Create (
92     ::Window* pParent,
93     ViewShellBase& rViewShellBase,
94     const cssu::Reference<css::ui::XSidebar>& rxSidebar)
95 {
96     SdDrawDocument* pDocument = rViewShellBase.GetDocument();
97     if (pDocument == NULL)
98         return NULL;
99 
100     ::boost::shared_ptr<MasterPageContainer> pContainer (new MasterPageContainer());
101 
102     MasterPagesSelector* pSelector(
103         new AllMasterPagesSelector (
104             pParent,
105             *pDocument,
106             rViewShellBase,
107             pContainer,
108             rxSidebar));
109     pSelector->LateInit();
110     pSelector->SetHelpId(HID_SD_TASK_PANE_PREVIEW_ALL);
111 
112     return pSelector;
113 }
114 
115 
116 
117 
AllMasterPagesSelector(::Window * pParent,SdDrawDocument & rDocument,ViewShellBase & rBase,const::boost::shared_ptr<MasterPageContainer> & rpContainer,const cssu::Reference<css::ui::XSidebar> & rxSidebar)118 AllMasterPagesSelector::AllMasterPagesSelector (
119     ::Window* pParent,
120     SdDrawDocument& rDocument,
121     ViewShellBase& rBase,
122     const ::boost::shared_ptr<MasterPageContainer>& rpContainer,
123     const cssu::Reference<css::ui::XSidebar>& rxSidebar)
124     : MasterPagesSelector(pParent, rDocument, rBase, rpContainer, rxSidebar),
125       mpSortedMasterPages(new SortedMasterPageDescriptorList())
126 {
127     MasterPagesSelector::Fill();
128 }
129 
130 
131 
132 
~AllMasterPagesSelector(void)133 AllMasterPagesSelector::~AllMasterPagesSelector (void)
134 {
135 }
136 
137 
138 
139 
Fill(ItemList & rItemList)140 void AllMasterPagesSelector::Fill (ItemList& rItemList)
141 {
142     if (mpSortedMasterPages->empty())
143         UpdateMasterPageList();
144     UpdatePageSet(rItemList);
145 }
146 
147 
148 
149 
NotifyContainerChangeEvent(const MasterPageContainerChangeEvent & rEvent)150 void AllMasterPagesSelector::NotifyContainerChangeEvent (
151     const MasterPageContainerChangeEvent& rEvent)
152 {
153     switch (rEvent.meEventType)
154     {
155         case MasterPageContainerChangeEvent::CHILD_ADDED:
156             AddItem(rEvent.maChildToken);
157             MasterPagesSelector::Fill();
158             break;
159 
160         case MasterPageContainerChangeEvent::INDEX_CHANGED:
161         case MasterPageContainerChangeEvent::INDEXES_CHANGED:
162             mpSortedMasterPages->clear();
163             MasterPagesSelector::Fill();
164             break;
165 
166         default:
167             MasterPagesSelector::NotifyContainerChangeEvent(rEvent);
168             break;
169     }
170 }
171 
172 
173 
174 
UpdateMasterPageList(void)175 void AllMasterPagesSelector::UpdateMasterPageList (void)
176 {
177     mpSortedMasterPages->clear();
178     int nTokenCount = mpContainer->GetTokenCount();
179     for (int i=0; i<nTokenCount; i++)
180         AddItem(mpContainer->GetTokenForIndex(i));
181 }
182 
183 
184 
185 
AddItem(MasterPageContainer::Token aToken)186 void AllMasterPagesSelector::AddItem (MasterPageContainer::Token aToken)
187 {
188     switch (mpContainer->GetOriginForToken(aToken))
189     {
190         case MasterPageContainer::DEFAULT:
191         case MasterPageContainer::TEMPLATE:
192             // Templates are added only when coming from the
193             // MasterPageContainerFiller so that they have an id which
194             // defines their place in the list.  Templates (pre) loaded from
195             // RecentlyUsedMasterPages are ignored (they will be loaded
196             // later by the MasterPageContainerFiller.)
197             if (mpContainer->GetTemplateIndexForToken(aToken) >= 0)
198                 mpSortedMasterPages->insert(mpContainer->GetDescriptorForToken(aToken));
199             break;
200 
201         default:
202             break;
203     }
204 }
205 
206 
207 
208 
UpdatePageSet(ItemList & rItemList)209 void AllMasterPagesSelector::UpdatePageSet (ItemList& rItemList)
210 {
211     SortedMasterPageDescriptorList::const_iterator iDescriptor;
212     SortedMasterPageDescriptorList::const_iterator iEnd (mpSortedMasterPages->end());
213     for (iDescriptor=mpSortedMasterPages->begin(); iDescriptor!=iEnd; ++iDescriptor)
214         rItemList.push_back((*iDescriptor)->maToken);
215 }
216 
217 
218 
219 
GetState(SfxItemSet & rItemSet)220 void AllMasterPagesSelector::GetState (SfxItemSet& rItemSet)
221 {
222     //    MasterPagesSelector::GetState(rItemSet);
223 
224 	if (rItemSet.GetItemState(SID_TP_EDIT_MASTER) == SFX_ITEM_AVAILABLE)
225         rItemSet.DisableItem(SID_TP_EDIT_MASTER);
226 }
227 
228 
229 
230 
231 } } // end of namespace sd::sidebar
232