merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / toolpanel / controls / MasterPageDescriptor.cxx
bloba956fd0b5f479e91e68d379c77e7160761dc8dd5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MasterPageDescriptor.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "MasterPageDescriptor.hxx"
36 #include "DocumentHelper.hxx"
37 #include "sdpage.hxx"
38 #include <tools/urlobj.hxx>
40 namespace sd { namespace toolpanel { namespace controls {
43 //===== MasterPageDescriptor ==================================================
45 MasterPageDescriptor::MasterPageDescriptor (
46 MasterPageContainer::Origin eOrigin,
47 const sal_Int32 nTemplateIndex,
48 const String& rsURL,
49 const String& rsPageName,
50 const String& rsStyleName,
51 const bool bIsPrecious,
52 const ::boost::shared_ptr<PageObjectProvider>& rpPageObjectProvider,
53 const ::boost::shared_ptr<PreviewProvider>& rpPreviewProvider)
54 : maToken(MasterPageContainer::NIL_TOKEN),
55 meOrigin(eOrigin),
56 msURL(INetURLObject(rsURL).GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS)),
57 msPageName(rsPageName),
58 msStyleName(rsStyleName),
59 mbIsPrecious(bIsPrecious),
60 mpMasterPage(NULL),
61 mpSlide(NULL),
62 maSmallPreview(),
63 maLargePreview(),
64 mpPreviewProvider(rpPreviewProvider),
65 mpPageObjectProvider(rpPageObjectProvider),
66 mnTemplateIndex(nTemplateIndex),
67 meURLClassification(URLCLASS_UNDETERMINED),
68 mnUseCount(0)
75 MasterPageDescriptor::MasterPageDescriptor (const MasterPageDescriptor& rDescriptor)
76 : maToken(rDescriptor.maToken),
77 meOrigin(rDescriptor.meOrigin),
78 msURL(rDescriptor.msURL),
79 msPageName(rDescriptor.msPageName),
80 msStyleName(rDescriptor.msStyleName),
81 mbIsPrecious(rDescriptor.mbIsPrecious),
82 mpMasterPage(rDescriptor.mpMasterPage),
83 mpSlide(rDescriptor.mpSlide),
84 maSmallPreview(rDescriptor.maSmallPreview),
85 maLargePreview(rDescriptor.maLargePreview),
86 mpPreviewProvider(rDescriptor.mpPreviewProvider),
87 mpPageObjectProvider(rDescriptor.mpPageObjectProvider),
88 mnTemplateIndex(rDescriptor.mnTemplateIndex),
89 meURLClassification(rDescriptor.meURLClassification),
90 mnUseCount(rDescriptor.mnUseCount)
97 MasterPageDescriptor::~MasterPageDescriptor (void)
104 void MasterPageDescriptor::SetToken (MasterPageContainer::Token aToken)
106 maToken = aToken;
112 Image MasterPageDescriptor::GetPreview (MasterPageContainer::PreviewSize eSize)
114 if (eSize == MasterPageContainer::SMALL)
115 return maSmallPreview;
116 else
117 return maLargePreview;
123 ::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> >
124 MasterPageDescriptor::Update (
125 const MasterPageDescriptor& rDescriptor)
127 bool bDataChanged (false);
128 bool bIndexChanged (false);
129 bool bPreviewChanged (false);
131 if (meOrigin==MasterPageContainer::UNKNOWN
132 && rDescriptor.meOrigin!=MasterPageContainer::UNKNOWN)
134 meOrigin = rDescriptor.meOrigin;
135 bIndexChanged = true;
138 if (msURL.getLength()==0 && rDescriptor.msURL.getLength()!=0)
140 msURL = rDescriptor.msURL;
141 bDataChanged = true;
144 if (msPageName.getLength()==0 && rDescriptor.msPageName.getLength()!=0)
146 msPageName = rDescriptor.msPageName;
147 bDataChanged = true;
150 if (msStyleName.getLength()==0 && rDescriptor.msStyleName.getLength()!=0)
152 msStyleName = rDescriptor.msStyleName;
153 bDataChanged = true;
156 if (mpPageObjectProvider.get()==NULL && rDescriptor.mpPageObjectProvider.get()!=NULL)
158 mpPageObjectProvider = rDescriptor.mpPageObjectProvider;
159 bDataChanged = true;
162 if (mpPreviewProvider.get()==NULL && rDescriptor.mpPreviewProvider.get()!=NULL)
164 mpPreviewProvider = rDescriptor.mpPreviewProvider;
165 bPreviewChanged = true;
168 if (mnTemplateIndex<0 && rDescriptor.mnTemplateIndex>=0)
170 mnTemplateIndex = rDescriptor.mnTemplateIndex;
171 bIndexChanged = true;
174 // Prepare the list of event types that will be returned.
175 ::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pResult;
176 if (bDataChanged || bIndexChanged || bPreviewChanged)
178 pResult.reset(new std::vector<MasterPageContainerChangeEvent::EventType>());
179 if (bDataChanged)
180 pResult->push_back(MasterPageContainerChangeEvent::DATA_CHANGED);
181 if (bIndexChanged)
182 pResult->push_back(MasterPageContainerChangeEvent::INDEX_CHANGED);
183 if (bPreviewChanged)
184 pResult->push_back(MasterPageContainerChangeEvent::PREVIEW_CHANGED);
187 return pResult;
193 bool MasterPageDescriptor::UpdatePageObject (
194 sal_Int32 nCostThreshold,
195 SdDrawDocument* pDocument)
197 bool bModified (false);
199 // Update the page object when that is not yet known.
200 if (mpMasterPage == NULL
201 && mpPageObjectProvider.get()!=NULL
202 && (nCostThreshold<0 || mpPageObjectProvider->GetCostIndex()<=nCostThreshold))
204 // Note that pDocument may be NULL.
206 SdPage* pPage = (*mpPageObjectProvider)(pDocument);
207 if (meOrigin == MasterPageContainer::MASTERPAGE)
209 mpMasterPage = pPage;
210 if (mpMasterPage != NULL)
211 mpMasterPage->SetPrecious(mbIsPrecious);
213 else
215 // Master pages from templates are copied into the local document.
216 if (pDocument != NULL)
217 mpMasterPage = DocumentHelper::CopyMasterPageToLocalDocument(*pDocument,pPage);
218 mpSlide = DocumentHelper::GetSlideForMasterPage(mpMasterPage);
221 if (mpMasterPage != NULL)
223 // Update page name and style name.
224 if (msPageName.getLength() == 0)
225 msPageName = mpMasterPage->GetName();
226 msStyleName = mpMasterPage->GetName();
228 // Delete an existing substitution. The next request for a preview
229 // will create the real one.
230 maSmallPreview = Image();
231 maLargePreview = Image();
232 mpPreviewProvider = ::boost::shared_ptr<PreviewProvider>(new PagePreviewProvider());
234 else
236 DBG_ASSERT(false, "UpdatePageObject: master page is NULL");
239 bModified = true;
242 return bModified;
248 bool MasterPageDescriptor::UpdatePreview (
249 sal_Int32 nCostThreshold,
250 const Size& rSmallSize,
251 const Size& rLargeSize,
252 ::sd::PreviewRenderer& rRenderer)
254 bool bModified (false);
256 // Update the preview when that is not yet known.
257 if (maLargePreview.GetSizePixel().Width()==0
258 && mpPreviewProvider.get()!=NULL
259 && (nCostThreshold<0 || mpPreviewProvider->GetCostIndex()<=nCostThreshold))
261 SdPage* pPage = mpSlide;
262 if (pPage == NULL)
264 pPage = mpMasterPage;
266 maLargePreview = (*mpPreviewProvider)(
267 rLargeSize.Width(),
268 pPage,
269 rRenderer);
270 if (maLargePreview.GetSizePixel().Width() > 0)
272 // Create the small preview by scaling the large one down.
273 maSmallPreview = rRenderer.ScaleBitmap(
274 maLargePreview.GetBitmapEx(),
275 rSmallSize.Width());
276 // The large preview may not have the desired width. Scale it
277 // accrodingly.
278 if (maLargePreview.GetSizePixel().Width() != rLargeSize.Width())
279 maLargePreview = rRenderer.ScaleBitmap(
280 maLargePreview.GetBitmapEx(),
281 rLargeSize.Width());
282 bModified = true;
286 return bModified;
292 MasterPageDescriptor::URLClassification MasterPageDescriptor::GetURLClassification (void)
294 if (meURLClassification == URLCLASS_UNDETERMINED)
296 if (msURL.getLength() == 0)
297 meURLClassification = URLCLASS_UNKNOWN;
298 else if (msURL.indexOf(::rtl::OUString::createFromAscii("presnt"))>=0)
300 meURLClassification = URLCLASS_PRESENTATION;
302 else if (msURL.indexOf(::rtl::OUString::createFromAscii("layout"))>=0)
304 meURLClassification = URLCLASS_LAYOUT;
306 else if (msURL.indexOf(::rtl::OUString::createFromAscii("educate"))>=0)
308 meURLClassification = URLCLASS_OTHER;
310 else
312 meURLClassification = URLCLASS_USER;
316 return meURLClassification;
321 //===== URLComparator =========================================================
323 MasterPageDescriptor::URLComparator::URLComparator (const ::rtl::OUString& sURL)
324 : msURL(sURL)
331 bool MasterPageDescriptor::URLComparator::operator() (
332 const SharedMasterPageDescriptor& rDescriptor)
334 if (rDescriptor.get() == NULL)
335 return false;
336 else
337 return rDescriptor->msURL.equals(msURL);
343 //===== PageNameComparator ====================================================
345 MasterPageDescriptor::PageNameComparator::PageNameComparator (const ::rtl::OUString& sPageName)
346 : msPageName(sPageName)
353 bool MasterPageDescriptor::PageNameComparator::operator() (
354 const SharedMasterPageDescriptor& rDescriptor)
356 if (rDescriptor.get() == NULL)
357 return false;
358 else
359 return rDescriptor->msPageName.equals(msPageName);
365 // ===== StyleNameComparator ==================================================
367 MasterPageDescriptor::StyleNameComparator::StyleNameComparator (const ::rtl::OUString& sStyleName)
368 : msStyleName(sStyleName)
375 bool MasterPageDescriptor::StyleNameComparator::operator() (
376 const SharedMasterPageDescriptor& rDescriptor)
378 if (rDescriptor.get() == NULL)
379 return false;
380 else
381 return rDescriptor->msStyleName.equals(msStyleName);
387 //===== PageObjectComparator ==================================================
389 MasterPageDescriptor::PageObjectComparator::PageObjectComparator (const SdPage* pPageObject)
390 : mpMasterPage(pPageObject)
397 bool MasterPageDescriptor::PageObjectComparator::operator() (
398 const SharedMasterPageDescriptor& rDescriptor)
400 if (rDescriptor.get() == NULL)
401 return false;
402 else
403 return rDescriptor->mpMasterPage==mpMasterPage;
409 //===== AllComparator =========================================================
411 MasterPageDescriptor::AllComparator::AllComparator(const SharedMasterPageDescriptor& rDescriptor)
412 : mpDescriptor(rDescriptor)
419 bool MasterPageDescriptor::AllComparator::operator() (const SharedMasterPageDescriptor&rDescriptor)
421 if (rDescriptor.get() == NULL)
422 return false;
423 else
425 // Take URL, page name, style name, and page object into account
426 // when comparing two descriptors. When two descriptors are
427 // identical in any of these values then their are thought of as
428 // equivalent. Only the Origin has to be the same in both
429 // descriptors.
430 return
431 mpDescriptor->meOrigin == rDescriptor->meOrigin
432 && (
433 (mpDescriptor->msURL.getLength()>0
434 && mpDescriptor->msURL.equals(rDescriptor->msURL))
435 || (mpDescriptor->msPageName.getLength()>0
436 && mpDescriptor->msPageName.equals(rDescriptor->msPageName))
437 || (mpDescriptor->msStyleName.getLength()>0
438 && mpDescriptor->msStyleName.equals(rDescriptor->msStyleName))
439 || (mpDescriptor->mpMasterPage!=NULL
440 && mpDescriptor->mpMasterPage==rDescriptor->mpMasterPage)
441 || (mpDescriptor->mpPageObjectProvider.get()!=NULL
442 && rDescriptor->mpPageObjectProvider.get()!=NULL
443 && mpDescriptor->mpPageObjectProvider==rDescriptor->mpPageObjectProvider));
448 } } } // end of namespace ::sd::toolpanel::controls