Update ooo320-m1
[ooovba.git] / sd / source / ui / toolpanel / controls / MasterPageContainerProviders.cxx
blobf8f78664862facbb64a5c2cc1c8328e1d6a45e89
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: MasterPageContainerProviders.cxx,v $
10 * $Revision: 1.5 $
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 "MasterPageContainerProviders.hxx"
36 #include "DrawDocShell.hxx"
37 #include "drawdoc.hxx"
38 #include "PreviewRenderer.hxx"
39 #include <comphelper/processfactory.hxx>
40 #include <sfx2/app.hxx>
41 #include <sfx2/sfxsids.hrc>
42 #include <unotools/ucbstreamhelper.hxx>
43 #include <vcl/image.hxx>
44 #include <vcl/pngread.hxx>
45 #include <com/sun/star/embed/ElementModes.hpp>
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::sd::toolpanel::controls;
52 namespace sd { namespace toolpanel { namespace controls {
55 //===== PagePreviewProvider ===================================================
57 PagePreviewProvider::PagePreviewProvider (void)
64 Image PagePreviewProvider::operator () (
65 int nWidth,
66 SdPage* pPage,
67 ::sd::PreviewRenderer& rRenderer)
69 Image aPreview;
71 if (pPage != NULL)
73 // Use the given renderer to create a preview of the given page
74 // object.
75 aPreview = rRenderer.RenderPage(
76 pPage,
77 nWidth,
78 String::CreateFromAscii(""),
79 false);
82 return aPreview;
88 int PagePreviewProvider::GetCostIndex (void)
90 return 5;
96 bool PagePreviewProvider::NeedsPageObject (void)
98 return true;
104 //===== TemplatePreviewProvider ===============================================
106 TemplatePreviewProvider::TemplatePreviewProvider (const ::rtl::OUString& rsURL)
107 : msURL(rsURL)
114 Image TemplatePreviewProvider::operator() (
115 int nWidth,
116 SdPage* pPage,
117 ::sd::PreviewRenderer& rRenderer)
119 // Unused parameters.
120 (void)nWidth;
121 (void)pPage;
122 (void)rRenderer;
124 // Load the thumbnail from a template document.
125 uno::Reference<io::XInputStream> xIStream;
127 uno::Reference< lang::XMultiServiceFactory > xServiceManager (
128 ::comphelper::getProcessServiceFactory());
129 if (xServiceManager.is())
133 uno::Reference<lang::XSingleServiceFactory> xStorageFactory(
134 xServiceManager->createInstance(
135 ::rtl::OUString::createFromAscii(
136 "com.sun.star.embed.StorageFactory")),
137 uno::UNO_QUERY);
139 if (xStorageFactory.is())
141 uno::Sequence<uno::Any> aArgs (2);
142 aArgs[0] <<= msURL;
143 aArgs[1] <<= embed::ElementModes::READ;
144 uno::Reference<embed::XStorage> xDocStorage (
145 xStorageFactory->createInstanceWithArguments(aArgs),
146 uno::UNO_QUERY);
150 if (xDocStorage.is())
152 uno::Reference<embed::XStorage> xStorage (
153 xDocStorage->openStorageElement(
154 ::rtl::OUString::createFromAscii("Thumbnails"),
155 embed::ElementModes::READ));
156 if (xStorage.is())
158 uno::Reference<io::XStream> xThumbnailCopy (
159 xStorage->cloneStreamElement(
160 ::rtl::OUString::createFromAscii(
161 "thumbnail.png")));
162 if (xThumbnailCopy.is())
163 xIStream = xThumbnailCopy->getInputStream();
167 catch (uno::Exception& rException)
169 OSL_TRACE (
170 "caught exception while trying to access Thumbnail/thumbnail.png of %s: %s",
171 ::rtl::OUStringToOString(msURL,
172 RTL_TEXTENCODING_UTF8).getStr(),
173 ::rtl::OUStringToOString(rException.Message,
174 RTL_TEXTENCODING_UTF8).getStr());
179 // An (older) implementation had a bug - The storage
180 // name was "Thumbnail" instead of "Thumbnails". The
181 // old name is still used as fallback but this code can
182 // be removed soon.
183 if ( ! xIStream.is())
185 uno::Reference<embed::XStorage> xStorage (
186 xDocStorage->openStorageElement(
187 ::rtl::OUString::createFromAscii("Thumbnail"),
188 embed::ElementModes::READ));
189 if (xStorage.is())
191 uno::Reference<io::XStream> xThumbnailCopy (
192 xStorage->cloneStreamElement(
193 ::rtl::OUString::createFromAscii(
194 "thumbnail.png")));
195 if (xThumbnailCopy.is())
196 xIStream = xThumbnailCopy->getInputStream();
200 catch (uno::Exception& rException)
202 OSL_TRACE (
203 "caught exception while trying to access Thumbnails/thumbnail.png of %s: %s",
204 ::rtl::OUStringToOString(msURL,
205 RTL_TEXTENCODING_UTF8).getStr(),
206 ::rtl::OUStringToOString(rException.Message,
207 RTL_TEXTENCODING_UTF8).getStr());
211 catch (uno::Exception& rException)
213 OSL_TRACE (
214 "caught exception while trying to access tuhmbnail of %s: %s",
215 ::rtl::OUStringToOString(msURL,
216 RTL_TEXTENCODING_UTF8).getStr(),
217 ::rtl::OUStringToOString(rException.Message,
218 RTL_TEXTENCODING_UTF8).getStr());
222 // Extract the image from the stream.
223 BitmapEx aThumbnail;
224 if (xIStream.is())
226 ::std::auto_ptr<SvStream> pStream (
227 ::utl::UcbStreamHelper::CreateStream (xIStream));
228 ::vcl::PNGReader aReader (*pStream);
229 aThumbnail = aReader.Read ();
232 // Note that the preview is returned without scaling it to the desired
233 // width. This gives the caller the chance to take advantage of a
234 // possibly larger resolution then was asked for.
235 return aThumbnail;
241 int TemplatePreviewProvider::GetCostIndex (void)
243 return 10;
249 bool TemplatePreviewProvider::NeedsPageObject (void)
251 return false;
257 //===== TemplatePageObjectProvider =============================================
259 TemplatePageObjectProvider::TemplatePageObjectProvider (const ::rtl::OUString& rsURL)
260 : msURL(rsURL),
261 mxDocumentShell()
268 SdPage* TemplatePageObjectProvider::operator() (SdDrawDocument* pContainerDocument)
270 // Unused parameters.
271 (void)pContainerDocument;
273 SdPage* pPage = NULL;
275 mxDocumentShell = NULL;
276 ::sd::DrawDocShell* pDocumentShell = NULL;
279 // Load the template document and return its first page.
280 pDocumentShell = LoadDocument (msURL);
281 if (pDocumentShell != NULL)
283 SdDrawDocument* pDocument = pDocumentShell->GetDoc();
284 if (pDocument != NULL)
286 pPage = pDocument->GetMasterSdPage(0, PK_STANDARD);
287 // In order to make the newly loaded master page deletable
288 // when copied into documents it is marked as no "precious".
289 // When it is modified then it is marked as "precious".
290 if (pPage != NULL)
291 pPage->SetPrecious(false);
295 catch (uno::RuntimeException)
297 OSL_TRACE ("caught exception while loading page from template file");
298 pPage = NULL;
301 return pPage;
307 ::sd::DrawDocShell* TemplatePageObjectProvider::LoadDocument (const ::rtl::OUString& sFileName)
309 SfxApplication* pSfxApp = SFX_APP();
310 SfxItemSet* pSet = new SfxAllItemSet (pSfxApp->GetPool());
311 pSet->Put (SfxBoolItem (SID_TEMPLATE, TRUE));
312 pSet->Put (SfxBoolItem (SID_PREVIEW, TRUE));
313 if (pSfxApp->LoadTemplate (mxDocumentShell, sFileName, TRUE, pSet))
315 mxDocumentShell = NULL;
317 SfxObjectShell* pShell = mxDocumentShell;
318 return PTR_CAST(::sd::DrawDocShell,pShell);
324 int TemplatePageObjectProvider::GetCostIndex (void)
326 return 20;
332 bool TemplatePageObjectProvider::operator== (const PageObjectProvider& rProvider)
334 const TemplatePageObjectProvider* pTemplatePageObjectProvider
335 = dynamic_cast<const TemplatePageObjectProvider*>(&rProvider);
336 if (pTemplatePageObjectProvider != NULL)
337 return (msURL == pTemplatePageObjectProvider->msURL);
338 else
339 return false;
345 //===== DefaultPageObjectProvider ==============================================
347 DefaultPageObjectProvider::DefaultPageObjectProvider (void)
354 SdPage* DefaultPageObjectProvider::operator () (SdDrawDocument* pContainerDocument)
356 SdPage* pLocalMasterPage = NULL;
357 if (pContainerDocument != NULL)
359 sal_Int32 nIndex (0);
360 SdPage* pLocalSlide = pContainerDocument->GetSdPage((USHORT)nIndex, PK_STANDARD);
361 if (pLocalSlide!=NULL && pLocalSlide->TRG_HasMasterPage())
362 pLocalMasterPage = dynamic_cast<SdPage*>(&pLocalSlide->TRG_GetMasterPage());
365 if (pLocalMasterPage == NULL)
367 DBG_ASSERT(false, "can not create master page for slide");
370 return pLocalMasterPage;
376 int DefaultPageObjectProvider::GetCostIndex (void)
378 return 15;
384 bool DefaultPageObjectProvider::operator== (const PageObjectProvider& rProvider)
386 return (dynamic_cast<const DefaultPageObjectProvider*>(&rProvider) != NULL);
392 //===== DefaultPagePreviewProvider ============================================
394 DefaultPagePreviewProvider::DefaultPagePreviewProvider (void)
400 Image DefaultPagePreviewProvider::operator() (
401 int nWidth,
402 SdPage* pPage,
403 ::sd::PreviewRenderer& rRenderer)
405 // Unused parameters.
406 (void)nWidth;
407 (void)pPage;
408 (void)rRenderer;
410 return Image();
416 int DefaultPagePreviewProvider::GetCostIndex (void)
418 return 0;
424 bool DefaultPagePreviewProvider::NeedsPageObject (void)
426 return false;
432 //===== ExistingPageProvider ==================================================
434 ExistingPageProvider::ExistingPageProvider (SdPage* pPage)
435 : mpPage(pPage)
442 SdPage* ExistingPageProvider::operator() (SdDrawDocument* pDocument)
444 (void)pDocument; // Unused parameter.
446 return mpPage;
452 int ExistingPageProvider::GetCostIndex (void)
454 return 0;
460 bool ExistingPageProvider::operator== (const PageObjectProvider& rProvider)
462 const ExistingPageProvider* pExistingPageProvider
463 = dynamic_cast<const ExistingPageProvider*>(&rProvider);
464 if (pExistingPageProvider != NULL)
465 return (mpPage == pExistingPageProvider->mpPage);
466 else
467 return false;
471 } } } // end of namespace ::sd::toolpanel::controls