bump product version to 5.0.4.1
[LibreOffice.git] / sdext / source / presenter / PresenterTheme.cxx
blob0e8af211d1da9739bf7add9f21a8ea12d80c51e6
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 "PresenterTheme.hxx"
21 #include "PresenterBitmapContainer.hxx"
22 #include "PresenterCanvasHelper.hxx"
23 #include "PresenterConfigurationAccess.hxx"
24 #include "PresenterHelper.hxx"
25 #include <com/sun/star/awt/Point.hpp>
26 #include <com/sun/star/beans/UnknownPropertyException.hpp>
27 #include <com/sun/star/deployment/XPackageInformationProvider.hpp>
28 #include <com/sun/star/drawing/XPresenterHelper.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/rendering/PanoseWeight.hpp>
31 #include <com/sun/star/rendering/XBitmap.hpp>
32 #include <com/sun/star/util/Color.hpp>
33 #include <osl/diagnose.h>
34 #include <boost/bind.hpp>
35 #include <map>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::std;
41 namespace sdext { namespace presenter {
43 namespace {
45 class BorderSize
47 public:
48 const static sal_Int32 mnInvalidValue = -10000;
50 BorderSize() : mnLeft(mnInvalidValue),
51 mnTop(mnInvalidValue),
52 mnRight(mnInvalidValue),
53 mnBottom(mnInvalidValue) {}
55 sal_Int32 mnLeft;
56 sal_Int32 mnTop;
57 sal_Int32 mnRight;
58 sal_Int32 mnBottom;
60 vector<sal_Int32> ToVector()
62 vector<sal_Int32> aSequence (4);
63 aSequence[0] = mnLeft == mnInvalidValue ? 0 : mnLeft;
64 aSequence[1] = mnTop == mnInvalidValue ? 0 : mnTop;
65 aSequence[2] = mnRight == mnInvalidValue ? 0 : mnRight;
66 aSequence[3] = mnBottom == mnInvalidValue ? 0 : mnBottom;
67 return aSequence;
70 void Merge (const BorderSize& rBorderSize)
72 if (mnLeft == mnInvalidValue)
73 mnLeft = rBorderSize.mnLeft;
74 if (mnTop == mnInvalidValue)
75 mnTop = rBorderSize.mnTop;
76 if (mnRight == mnInvalidValue)
77 mnRight = rBorderSize.mnRight;
78 if (mnBottom == mnInvalidValue)
79 mnBottom = rBorderSize.mnBottom;
83 /** Reading a theme from the configurations is done in various classes. The
84 ReadContext gives access to frequently used objects and functions to make
85 the configuration handling easier.
87 class ReadContext
89 public:
90 Reference<XComponentContext> mxComponentContext;
91 Reference<rendering::XCanvas> mxCanvas;
92 Reference<drawing::XPresenterHelper> mxPresenterHelper;
94 ReadContext (
95 const Reference<XComponentContext>& rxContext,
96 const Reference<rendering::XCanvas>& rxCanvas);
97 ~ReadContext();
99 /** Read data describing a font from the node that can be reached from
100 the given root via the given path.
101 @param rsFontPath
102 May be empty.
104 static PresenterTheme::SharedFontDescriptor ReadFont (
105 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxTheme,
106 const OUString& rsFontPath,
107 const PresenterTheme::SharedFontDescriptor& rpDefault);
108 static PresenterTheme::SharedFontDescriptor ReadFont (
109 const Reference<beans::XPropertySet>& rxFontProperties,
110 const PresenterTheme::SharedFontDescriptor& rpDefault);
112 ::boost::shared_ptr<PresenterTheme::Theme> ReadTheme (
113 PresenterConfigurationAccess& rConfiguration,
114 const OUString& rsThemeName);
116 static BorderSize ReadBorderSize (const Reference<container::XNameAccess>& rxNode);
118 private:
119 static Any GetByName (
120 const Reference<container::XNameAccess>& rxNode,
121 const OUString& rsName);
124 /** A PaneStyle describes how a pane is rendered.
126 class PaneStyle
128 public:
129 PaneStyle();
130 ~PaneStyle();
132 const SharedBitmapDescriptor GetBitmap (const OUString& sBitmapName) const;
134 OUString msStyleName;
135 ::boost::shared_ptr<PaneStyle> mpParentStyle;
136 PresenterTheme::SharedFontDescriptor mpFont;
137 BorderSize maInnerBorderSize;
138 BorderSize maOuterBorderSize;
139 ::boost::shared_ptr<PresenterBitmapContainer> mpBitmaps;
141 PresenterTheme::SharedFontDescriptor GetFont() const;
144 typedef ::boost::shared_ptr<PaneStyle> SharedPaneStyle;
146 class PaneStyleContainer
148 private:
149 ::std::vector<SharedPaneStyle> mStyles;
151 public:
152 void Read (
153 ReadContext& rReadContext,
154 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
156 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
158 private:
159 void ProcessPaneStyle (
160 ReadContext& rReadContext,
161 const OUString& rsKey,
162 const ::std::vector<css::uno::Any>& rValues);
165 /** A ViewStyle describes how a view is displayed.
167 class ViewStyle
169 public:
170 ViewStyle();
171 ~ViewStyle();
173 const SharedBitmapDescriptor GetBitmap (const OUString& sBitmapName) const;
175 PresenterTheme::SharedFontDescriptor GetFont() const;
177 OUString msStyleName;
178 ::boost::shared_ptr<ViewStyle> mpParentStyle;
179 PresenterTheme::SharedFontDescriptor mpFont;
180 ::boost::shared_ptr<PresenterBitmapContainer> mpBitmaps;
181 SharedBitmapDescriptor mpBackground;
184 typedef ::boost::shared_ptr<ViewStyle> SharedViewStyle;
186 class ViewStyleContainer
188 private:
189 ::std::vector<SharedViewStyle> mStyles;
191 public:
192 void Read (
193 ReadContext& rReadContext,
194 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
196 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
198 private:
199 void ProcessViewStyle(
200 ReadContext& rReadContext,
201 const Reference<beans::XPropertySet>& rxProperties);
204 class ViewDescriptor
207 typedef ::boost::shared_ptr<ViewDescriptor> SharedViewDescriptor;
208 typedef ::std::vector<SharedViewDescriptor> ViewDescriptorContainer;
210 class StyleAssociationContainer
212 public:
213 void Read (
214 ReadContext& rReadContext,
215 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
217 OUString GetStyleName (const OUString& rsResourceName) const;
219 private:
220 typedef map<OUString, OUString> StyleAssociations;
221 StyleAssociations maStyleAssociations;
223 void ProcessStyleAssociation(
224 ReadContext& rReadContext,
225 const OUString& rsKey,
226 const ::std::vector<css::uno::Any>& rValues);
229 } // end of anonymous namespace
231 class PresenterTheme::Theme
233 public:
234 Theme (
235 const OUString& rsName,
236 const Reference<container::XHierarchicalNameAccess>& rThemeRoot,
237 const OUString& rsNodeName);
238 ~Theme();
240 void Read (
241 PresenterConfigurationAccess& rConfiguration,
242 ReadContext& rReadContext);
244 OUString msThemeName;
245 OUString msConfigurationNodeName;
246 ::boost::shared_ptr<Theme> mpParentTheme;
247 SharedBitmapDescriptor mpBackground;
248 PaneStyleContainer maPaneStyles;
249 ViewStyleContainer maViewStyles;
250 ViewDescriptorContainer maViewDescriptors;
251 StyleAssociationContainer maStyleAssociations;
252 Reference<container::XHierarchicalNameAccess> mxThemeRoot;
253 ::boost::shared_ptr<PresenterBitmapContainer> mpIconContainer;
254 typedef map<OUString,SharedFontDescriptor> FontContainer;
255 FontContainer maFontContainer;
257 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
258 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
260 private:
261 void ProcessFont(
262 ReadContext& rReadContext,
263 const OUString& rsKey,
264 const Reference<beans::XPropertySet>& rxProperties);
267 //===== PresenterTheme ========================================================
269 PresenterTheme::PresenterTheme (
270 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
271 const OUString& rsThemeName,
272 const css::uno::Reference<css::rendering::XCanvas>& rxCanvas)
273 : mxContext(rxContext),
274 msThemeName(rsThemeName),
275 mpTheme(),
276 mpBitmapContainer(),
277 mxCanvas(rxCanvas)
279 mpTheme = ReadTheme();
282 PresenterTheme::~PresenterTheme()
286 ::boost::shared_ptr<PresenterTheme::Theme> PresenterTheme::ReadTheme()
288 ReadContext aReadContext(mxContext, mxCanvas);
290 PresenterConfigurationAccess aConfiguration (
291 mxContext,
292 OUString("/org.openoffice.Office.PresenterScreen/"),
293 PresenterConfigurationAccess::READ_ONLY);
295 return aReadContext.ReadTheme(aConfiguration, msThemeName);
298 bool PresenterTheme::HasCanvas() const
300 return mxCanvas.is();
303 void PresenterTheme::ProvideCanvas (const Reference<rendering::XCanvas>& rxCanvas)
305 if ( ! mxCanvas.is() && rxCanvas.is())
307 mxCanvas = rxCanvas;
308 ReadTheme();
312 OUString PresenterTheme::GetStyleName (const OUString& rsResourceURL) const
314 OUString sStyleName;
315 ::boost::shared_ptr<Theme> pTheme (mpTheme);
316 while (sStyleName.isEmpty() && pTheme.get()!=NULL)
318 sStyleName = pTheme->maStyleAssociations.GetStyleName(rsResourceURL);
319 pTheme = pTheme->mpParentTheme;
321 return sStyleName;
324 ::std::vector<sal_Int32> PresenterTheme::GetBorderSize (
325 const OUString& rsStyleName,
326 const bool bOuter) const
328 OSL_ASSERT(mpTheme.get() != NULL);
330 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
331 if (pPaneStyle.get() != NULL)
332 if (bOuter)
333 return pPaneStyle->maOuterBorderSize.ToVector();
334 else
335 return pPaneStyle->maInnerBorderSize.ToVector();
336 else
338 return ::std::vector<sal_Int32>(4,0);
342 PresenterTheme::SharedFontDescriptor PresenterTheme::ReadFont (
343 const Reference<container::XHierarchicalNameAccess>& rxNode,
344 const OUString& rsFontPath,
345 const PresenterTheme::SharedFontDescriptor& rpDefault)
347 return ReadContext::ReadFont(rxNode, rsFontPath, rpDefault);
350 bool PresenterTheme::ConvertToColor (
351 const Any& rColorSequence,
352 sal_uInt32& rColor)
354 Sequence<sal_Int8> aByteSequence;
355 if (rColorSequence >>= aByteSequence)
357 const sal_Int32 nByteCount (aByteSequence.getLength());
358 const sal_uInt8* pArray = reinterpret_cast<const sal_uInt8*>(aByteSequence.getConstArray());
359 rColor = 0;
360 for (sal_Int32 nIndex=0; nIndex<nByteCount; ++nIndex)
362 rColor = (rColor << 8) | *pArray++;
364 return true;
366 else
367 return false;
370 ::boost::shared_ptr<PresenterConfigurationAccess> PresenterTheme::GetNodeForViewStyle (
371 const OUString& rsStyleName) const
373 if (mpTheme.get() == NULL)
374 return ::boost::shared_ptr<PresenterConfigurationAccess>();
376 // Open configuration for writing.
377 ::boost::shared_ptr<PresenterConfigurationAccess> pConfiguration (
378 new PresenterConfigurationAccess(
379 mxContext,
380 OUString("/org.openoffice.Office.PresenterScreen/"),
381 PresenterConfigurationAccess::READ_WRITE));
383 // Get configuration node for the view style container of the current
384 // theme.
385 if (pConfiguration->GoToChild( OUString(
386 "Presenter/Themes/" + mpTheme->msConfigurationNodeName + "/ViewStyles")))
388 pConfiguration->GoToChild(
389 ::boost::bind(&PresenterConfigurationAccess::IsStringPropertyEqual,
390 rsStyleName,
391 OUString("StyleName"),
392 _2));
394 return pConfiguration;
397 SharedBitmapDescriptor PresenterTheme::GetBitmap (
398 const OUString& rsStyleName,
399 const OUString& rsBitmapName) const
401 if (mpTheme.get() != NULL)
403 if (rsStyleName.isEmpty())
405 if (rsBitmapName == "Background")
407 ::boost::shared_ptr<Theme> pTheme (mpTheme);
408 while (pTheme.get()!=NULL && pTheme->mpBackground.get()==NULL)
409 pTheme = pTheme->mpParentTheme;
410 if (pTheme.get() != NULL)
411 return pTheme->mpBackground;
412 else
413 return SharedBitmapDescriptor();
416 else
418 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
419 if (pPaneStyle.get() != NULL)
421 SharedBitmapDescriptor pBitmap (pPaneStyle->GetBitmap(rsBitmapName));
422 if (pBitmap.get() != NULL)
423 return pBitmap;
426 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
427 if (pViewStyle.get() != NULL)
429 SharedBitmapDescriptor pBitmap (pViewStyle->GetBitmap(rsBitmapName));
430 if (pBitmap.get() != NULL)
431 return pBitmap;
436 return SharedBitmapDescriptor();
439 SharedBitmapDescriptor PresenterTheme::GetBitmap (
440 const OUString& rsBitmapName) const
442 if (mpTheme.get() != NULL)
444 if (rsBitmapName == "Background")
446 ::boost::shared_ptr<Theme> pTheme (mpTheme);
447 while (pTheme.get()!=NULL && pTheme->mpBackground.get()==NULL)
448 pTheme = pTheme->mpParentTheme;
449 if (pTheme.get() != NULL)
450 return pTheme->mpBackground;
451 else
452 return SharedBitmapDescriptor();
454 else
456 if (mpTheme->mpIconContainer.get() != NULL)
457 return mpTheme->mpIconContainer->GetBitmap(rsBitmapName);
461 return SharedBitmapDescriptor();
464 ::boost::shared_ptr<PresenterBitmapContainer> PresenterTheme::GetBitmapContainer() const
466 if (mpTheme.get() != NULL)
467 return mpTheme->mpIconContainer;
468 else
469 return ::boost::shared_ptr<PresenterBitmapContainer>();
472 PresenterTheme::SharedFontDescriptor PresenterTheme::GetFont (
473 const OUString& rsStyleName) const
475 if (mpTheme.get() != NULL)
477 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
478 if (pPaneStyle.get() != NULL)
479 return pPaneStyle->GetFont();
481 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
482 if (pViewStyle.get() != NULL)
483 return pViewStyle->GetFont();
485 ::boost::shared_ptr<Theme> pTheme (mpTheme);
486 while (pTheme.get() != NULL)
488 Theme::FontContainer::const_iterator iFont (pTheme->maFontContainer.find(rsStyleName));
489 if (iFont != pTheme->maFontContainer.end())
490 return iFont->second;
492 pTheme = pTheme->mpParentTheme;
496 return SharedFontDescriptor();
499 //===== FontDescriptor ========================================================
501 PresenterTheme::FontDescriptor::FontDescriptor (
502 const ::boost::shared_ptr<FontDescriptor>& rpDescriptor)
503 : msFamilyName(),
504 msStyleName(),
505 mnSize(12),
506 mnColor(0x00000000),
507 msAnchor(OUString("Left")),
508 mnXOffset(0),
509 mnYOffset(0)
511 if (rpDescriptor.get() != NULL)
513 msFamilyName = rpDescriptor->msFamilyName;
514 msStyleName = rpDescriptor->msStyleName;
515 mnSize = rpDescriptor->mnSize;
516 mnColor = rpDescriptor->mnColor;
517 msAnchor = rpDescriptor->msAnchor;
518 mnXOffset = rpDescriptor->mnXOffset;
519 mnYOffset = rpDescriptor->mnYOffset;
523 bool PresenterTheme::FontDescriptor::PrepareFont (
524 const Reference<rendering::XCanvas>& rxCanvas)
526 if (mxFont.is())
527 return true;
529 if ( ! rxCanvas.is())
530 return false;
532 const double nCellSize (GetCellSizeForDesignSize(rxCanvas, mnSize));
533 mxFont = CreateFont(rxCanvas, nCellSize);
535 return mxFont.is();
538 Reference<rendering::XCanvasFont> PresenterTheme::FontDescriptor::CreateFont (
539 const Reference<rendering::XCanvas>& rxCanvas,
540 const double nCellSize) const
542 rendering::FontRequest aFontRequest;
543 aFontRequest.FontDescription.FamilyName = msFamilyName;
544 if (msFamilyName.isEmpty())
545 aFontRequest.FontDescription.FamilyName = "Tahoma";
546 aFontRequest.FontDescription.StyleName = msStyleName;
547 aFontRequest.CellSize = nCellSize;
549 // Make an attempt at translating the style name(s)into a corresponding
550 // font description.
551 if (msStyleName == "Bold")
552 aFontRequest.FontDescription.FontDescription.Weight = rendering::PanoseWeight::HEAVY;
554 return rxCanvas->createFont(
555 aFontRequest,
556 Sequence<beans::PropertyValue>(),
557 geometry::Matrix2D(1,0,0,1));
560 double PresenterTheme::FontDescriptor::GetCellSizeForDesignSize (
561 const Reference<rendering::XCanvas>& rxCanvas,
562 const double nDesignSize) const
564 // Use the given design size as initial value in calculating the cell
565 // size.
566 double nCellSize (nDesignSize);
568 if ( ! rxCanvas.is())
570 // We need the canvas to do the conversion. Return the design size,
571 // it is the our best guess in this circumstance.
572 return nDesignSize;
575 Reference<rendering::XCanvasFont> xFont (CreateFont(rxCanvas, nCellSize));
576 if ( ! xFont.is())
577 return nDesignSize;
579 geometry::RealRectangle2D aBox (PresenterCanvasHelper::GetTextBoundingBox (xFont, "X"));
581 const double nAscent (-aBox.Y1);
582 const double nDescent (aBox.Y2);
583 const double nScale = (nAscent+nDescent) / nAscent;
584 return nDesignSize * nScale;
587 //===== Theme =================================================================
589 PresenterTheme::Theme::Theme (
590 const OUString& rsName,
591 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot,
592 const OUString& rsNodeName)
593 : msThemeName(rsName),
594 msConfigurationNodeName(rsNodeName),
595 mpParentTheme(),
596 maPaneStyles(),
597 maViewStyles(),
598 maStyleAssociations(),
599 mxThemeRoot(rxThemeRoot),
600 mpIconContainer()
604 PresenterTheme::Theme::~Theme()
608 void PresenterTheme::Theme::Read (
609 PresenterConfigurationAccess& rConfiguration,
610 ReadContext& rReadContext)
612 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "ThemeName")
613 >>= msThemeName;
615 // Parent theme name.
616 OUString sParentThemeName;
617 if ((PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "ParentTheme")
618 >>= sParentThemeName)
619 && !sParentThemeName.isEmpty())
621 mpParentTheme = rReadContext.ReadTheme(rConfiguration, sParentThemeName);
624 // Background.
625 mpBackground = PresenterBitmapContainer::LoadBitmap(
626 mxThemeRoot,
627 "Background",
628 rReadContext.mxPresenterHelper,
629 rReadContext.mxCanvas,
630 SharedBitmapDescriptor());
632 // Style associations.
633 maStyleAssociations.Read(rReadContext, mxThemeRoot);
635 // Pane styles.
636 maPaneStyles.Read(rReadContext, mxThemeRoot);
638 // View styles.
639 maViewStyles.Read(rReadContext, mxThemeRoot);
641 // Read bitmaps.
642 mpIconContainer.reset(
643 new PresenterBitmapContainer(
644 Reference<container::XNameAccess>(
645 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Bitmaps"),
646 UNO_QUERY),
647 mpParentTheme.get()!=NULL
648 ? mpParentTheme->mpIconContainer
649 : ::boost::shared_ptr<PresenterBitmapContainer>(),
650 rReadContext.mxComponentContext,
651 rReadContext.mxCanvas));
653 // Read fonts.
654 Reference<container::XNameAccess> xFontNode(
655 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Fonts"),
656 UNO_QUERY);
657 PresenterConfigurationAccess::ForAll(
658 xFontNode,
659 ::boost::bind(&PresenterTheme::Theme::ProcessFont,
660 this, ::boost::ref(rReadContext), _1, _2));
663 SharedPaneStyle PresenterTheme::Theme::GetPaneStyle (const OUString& rsStyleName) const
665 SharedPaneStyle pPaneStyle (maPaneStyles.GetPaneStyle(rsStyleName));
666 if (pPaneStyle.get() != NULL)
667 return pPaneStyle;
668 else if (mpParentTheme.get() != NULL)
669 return mpParentTheme->GetPaneStyle(rsStyleName);
670 else
671 return SharedPaneStyle();
674 SharedViewStyle PresenterTheme::Theme::GetViewStyle (const OUString& rsStyleName) const
676 SharedViewStyle pViewStyle (maViewStyles.GetViewStyle(rsStyleName));
677 if (pViewStyle.get() != NULL)
678 return pViewStyle;
679 else if (mpParentTheme.get() != NULL)
680 return mpParentTheme->GetViewStyle(rsStyleName);
681 else
682 return SharedViewStyle();
685 void PresenterTheme::Theme::ProcessFont(
686 ReadContext& rReadContext,
687 const OUString& rsKey,
688 const Reference<beans::XPropertySet>& rxProperties)
690 (void)rReadContext;
691 maFontContainer[rsKey] = ReadContext::ReadFont(rxProperties, SharedFontDescriptor());
694 namespace {
696 //===== ReadContext ===========================================================
698 ReadContext::ReadContext (
699 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
700 const Reference<rendering::XCanvas>& rxCanvas)
701 : mxComponentContext(rxContext),
702 mxCanvas(rxCanvas),
703 mxPresenterHelper()
705 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
706 if (xFactory.is())
708 mxPresenterHelper = Reference<drawing::XPresenterHelper>(
709 xFactory->createInstanceWithContext(
710 OUString("com.sun.star.comp.Draw.PresenterHelper"),
711 rxContext),
712 UNO_QUERY_THROW);
716 ReadContext::~ReadContext()
720 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
721 const Reference<container::XHierarchicalNameAccess>& rxNode,
722 const OUString& rsFontPath,
723 const PresenterTheme::SharedFontDescriptor& rpDefault)
725 if ( ! rxNode.is())
726 return PresenterTheme::SharedFontDescriptor();
730 Reference<container::XHierarchicalNameAccess> xFont (
731 PresenterConfigurationAccess::GetConfigurationNode(
732 rxNode,
733 rsFontPath),
734 UNO_QUERY_THROW);
736 Reference<beans::XPropertySet> xProperties (xFont, UNO_QUERY_THROW);
737 return ReadFont(xProperties, rpDefault);
739 catch (Exception&)
741 OSL_ASSERT(false);
744 return PresenterTheme::SharedFontDescriptor();
747 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
748 const Reference<beans::XPropertySet>& rxProperties,
749 const PresenterTheme::SharedFontDescriptor& rpDefault)
751 ::boost::shared_ptr<PresenterTheme::FontDescriptor> pDescriptor (
752 new PresenterTheme::FontDescriptor(rpDefault));
754 PresenterConfigurationAccess::GetProperty(rxProperties, "FamilyName") >>= pDescriptor->msFamilyName;
755 PresenterConfigurationAccess::GetProperty(rxProperties, "Style") >>= pDescriptor->msStyleName;
756 PresenterConfigurationAccess::GetProperty(rxProperties, "Size") >>= pDescriptor->mnSize;
757 PresenterTheme::ConvertToColor(
758 PresenterConfigurationAccess::GetProperty(rxProperties, "Color"),
759 pDescriptor->mnColor);
760 PresenterConfigurationAccess::GetProperty(rxProperties, "Anchor") >>= pDescriptor->msAnchor;
761 PresenterConfigurationAccess::GetProperty(rxProperties, "XOffset") >>= pDescriptor->mnXOffset;
762 PresenterConfigurationAccess::GetProperty(rxProperties, "YOffset") >>= pDescriptor->mnYOffset;
764 return pDescriptor;
767 Any ReadContext::GetByName (
768 const Reference<container::XNameAccess>& rxNode,
769 const OUString& rsName)
771 OSL_ASSERT(rxNode.is());
772 if (rxNode->hasByName(rsName))
773 return rxNode->getByName(rsName);
774 else
775 return Any();
778 ::boost::shared_ptr<PresenterTheme::Theme> ReadContext::ReadTheme (
779 PresenterConfigurationAccess& rConfiguration,
780 const OUString& rsThemeName)
782 ::boost::shared_ptr<PresenterTheme::Theme> pTheme;
784 OUString sCurrentThemeName (rsThemeName);
785 if (sCurrentThemeName.isEmpty())
787 // No theme name given. Look up the CurrentTheme property.
788 rConfiguration.GetConfigurationNode("Presenter/CurrentTheme") >>= sCurrentThemeName;
789 if (sCurrentThemeName.isEmpty())
791 // Still no name. Use "DefaultTheme".
792 sCurrentThemeName = "DefaultTheme";
796 Reference<container::XNameAccess> xThemes (
797 rConfiguration.GetConfigurationNode("Presenter/Themes"),
798 UNO_QUERY);
799 if (xThemes.is())
801 // Iterate over all themes and search the one with the given name.
802 Sequence<OUString> aKeys (xThemes->getElementNames());
803 for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
805 const OUString& rsKey (aKeys[nItemIndex]);
806 Reference<container::XHierarchicalNameAccess> xTheme (
807 xThemes->getByName(rsKey), UNO_QUERY);
808 if (xTheme.is())
810 OUString sThemeName;
811 PresenterConfigurationAccess::GetConfigurationNode(xTheme, "ThemeName")
812 >>= sThemeName;
813 if (sThemeName == sCurrentThemeName)
815 pTheme.reset(new PresenterTheme::Theme(sThemeName,xTheme,rsKey));
816 break;
822 if (pTheme.get() != NULL)
824 pTheme->Read(rConfiguration, *this);
827 return pTheme;
830 BorderSize ReadContext::ReadBorderSize (const Reference<container::XNameAccess>& rxNode)
832 BorderSize aBorderSize;
834 if (rxNode.is())
836 GetByName(rxNode, "Left") >>= aBorderSize.mnLeft;
837 GetByName(rxNode, "Top") >>= aBorderSize.mnTop;
838 GetByName(rxNode, "Right") >>= aBorderSize.mnRight;
839 GetByName(rxNode, "Bottom") >>= aBorderSize.mnBottom;
842 return aBorderSize;
845 //===== PaneStyleContainer ====================================================
847 void PaneStyleContainer::Read (
848 ReadContext& rReadContext,
849 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
851 Reference<container::XNameAccess> xPaneStyleList (
852 PresenterConfigurationAccess::GetConfigurationNode(
853 rxThemeRoot,
854 "PaneStyles"),
855 UNO_QUERY);
856 if (xPaneStyleList.is())
858 ::std::vector<OUString> aProperties;
859 aProperties.reserve(6);
860 aProperties.push_back("StyleName");
861 aProperties.push_back("ParentStyle");
862 aProperties.push_back("TitleFont");
863 aProperties.push_back("InnerBorderSize");
864 aProperties.push_back("OuterBorderSize");
865 aProperties.push_back("BorderBitmapList");
866 PresenterConfigurationAccess::ForAll(
867 xPaneStyleList,
868 aProperties,
869 ::boost::bind(&PaneStyleContainer::ProcessPaneStyle,
870 this, ::boost::ref(rReadContext), _1, _2));
874 void PaneStyleContainer::ProcessPaneStyle(
875 ReadContext& rReadContext,
876 const OUString& rsKey,
877 const ::std::vector<Any>& rValues)
879 (void)rsKey;
881 if (rValues.size() != 6)
882 return;
884 ::boost::shared_ptr<PaneStyle> pStyle (new PaneStyle());
886 rValues[0] >>= pStyle->msStyleName;
888 OUString sParentStyleName;
889 if (rValues[1] >>= sParentStyleName)
891 // Find parent style.
892 ::std::vector<SharedPaneStyle>::const_iterator iStyle;
893 for (iStyle=mStyles.begin(); iStyle!=mStyles.end(); ++iStyle)
894 if ((*iStyle)->msStyleName.equals(sParentStyleName))
896 pStyle->mpParentStyle = *iStyle;
897 break;
901 Reference<container::XHierarchicalNameAccess> xFontNode (rValues[2], UNO_QUERY);
902 pStyle->mpFont = ReadContext::ReadFont(
903 xFontNode, "", PresenterTheme::SharedFontDescriptor());
905 Reference<container::XNameAccess> xInnerBorderSizeNode (rValues[3], UNO_QUERY);
906 pStyle->maInnerBorderSize = ReadContext::ReadBorderSize(xInnerBorderSizeNode);
907 Reference<container::XNameAccess> xOuterBorderSizeNode (rValues[4], UNO_QUERY);
908 pStyle->maOuterBorderSize = ReadContext::ReadBorderSize(xOuterBorderSizeNode);
910 if (pStyle->mpParentStyle.get() != NULL)
912 pStyle->maInnerBorderSize.Merge(pStyle->mpParentStyle->maInnerBorderSize);
913 pStyle->maOuterBorderSize.Merge(pStyle->mpParentStyle->maOuterBorderSize);
916 if (rReadContext.mxCanvas.is())
918 Reference<container::XNameAccess> xBitmapsNode (rValues[5], UNO_QUERY);
919 pStyle->mpBitmaps.reset(new PresenterBitmapContainer(
920 xBitmapsNode,
921 pStyle->mpParentStyle.get()!=NULL
922 ? pStyle->mpParentStyle->mpBitmaps
923 : ::boost::shared_ptr<PresenterBitmapContainer>(),
924 rReadContext.mxComponentContext,
925 rReadContext.mxCanvas,
926 rReadContext.mxPresenterHelper));
929 mStyles.push_back(pStyle);
932 SharedPaneStyle PaneStyleContainer::GetPaneStyle (const OUString& rsStyleName) const
934 ::std::vector<SharedPaneStyle>::const_iterator iEnd (mStyles.end());
935 for (::std::vector<SharedPaneStyle>::const_iterator iStyle=mStyles.begin(); iStyle!=iEnd; ++iStyle)
936 if ((*iStyle)->msStyleName == rsStyleName)
937 return *iStyle;
938 return SharedPaneStyle();
941 //===== PaneStyle =============================================================
943 PaneStyle::PaneStyle()
944 : msStyleName(),
945 mpParentStyle(),
946 mpFont(),
947 maInnerBorderSize(),
948 maOuterBorderSize(),
949 mpBitmaps()
953 PaneStyle::~PaneStyle()
957 const SharedBitmapDescriptor PaneStyle::GetBitmap (const OUString& rsBitmapName) const
959 if (mpBitmaps.get() != NULL)
961 const SharedBitmapDescriptor pBitmap = mpBitmaps->GetBitmap(rsBitmapName);
962 if (pBitmap.get() != NULL)
963 return pBitmap;
966 if (mpParentStyle.get() != NULL)
967 return mpParentStyle->GetBitmap(rsBitmapName);
968 else
969 return SharedBitmapDescriptor();
972 PresenterTheme::SharedFontDescriptor PaneStyle::GetFont() const
974 if (mpFont.get() != NULL)
975 return mpFont;
976 else if (mpParentStyle.get() != NULL)
977 return mpParentStyle->GetFont();
978 else
979 return PresenterTheme::SharedFontDescriptor();
982 //===== ViewStyleContainer ====================================================
984 void ViewStyleContainer::Read (
985 ReadContext& rReadContext,
986 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
988 (void)rReadContext;
990 Reference<container::XNameAccess> xViewStyleList (
991 PresenterConfigurationAccess::GetConfigurationNode(
992 rxThemeRoot,
993 "ViewStyles"),
994 UNO_QUERY);
995 if (xViewStyleList.is())
997 PresenterConfigurationAccess::ForAll(
998 xViewStyleList,
999 ::boost::bind(&ViewStyleContainer::ProcessViewStyle,
1000 this, ::boost::ref(rReadContext), _2));
1004 void ViewStyleContainer::ProcessViewStyle(
1005 ReadContext& rReadContext,
1006 const Reference<beans::XPropertySet>& rxProperties)
1008 ::boost::shared_ptr<ViewStyle> pStyle (new ViewStyle());
1010 PresenterConfigurationAccess::GetProperty(rxProperties, "StyleName")
1011 >>= pStyle->msStyleName;
1013 OUString sParentStyleName;
1014 if (PresenterConfigurationAccess::GetProperty(rxProperties, "ParentStyle")
1015 >>= sParentStyleName)
1017 // Find parent style.
1018 ::std::vector<SharedViewStyle>::const_iterator iStyle;
1019 for (iStyle=mStyles.begin(); iStyle!=mStyles.end(); ++iStyle)
1020 if ((*iStyle)->msStyleName.equals(sParentStyleName))
1022 pStyle->mpParentStyle = *iStyle;
1023 pStyle->mpFont = (*iStyle)->mpFont;
1024 pStyle->mpBackground = (*iStyle)->mpBackground;
1025 break;
1029 const OUString sPathToFont; // empty string
1030 Reference<container::XHierarchicalNameAccess> xFontNode (
1031 PresenterConfigurationAccess::GetProperty(rxProperties, "Font"), UNO_QUERY);
1032 PresenterTheme::SharedFontDescriptor pFont (
1033 ReadContext::ReadFont(xFontNode, sPathToFont, PresenterTheme::SharedFontDescriptor()));
1034 if (pFont.get() != NULL)
1035 pStyle->mpFont = pFont;
1037 Reference<container::XHierarchicalNameAccess> xBackgroundNode (
1038 PresenterConfigurationAccess::GetProperty(rxProperties, "Background"),
1039 UNO_QUERY);
1040 SharedBitmapDescriptor pBackground (PresenterBitmapContainer::LoadBitmap(
1041 xBackgroundNode,
1042 OUString(),
1043 rReadContext.mxPresenterHelper,
1044 rReadContext.mxCanvas,
1045 SharedBitmapDescriptor()));
1046 if (pBackground.get() != NULL && pBackground->GetNormalBitmap().is())
1047 pStyle->mpBackground = pBackground;
1049 mStyles.push_back(pStyle);
1052 SharedViewStyle ViewStyleContainer::GetViewStyle (const OUString& rsStyleName) const
1054 ::std::vector<SharedViewStyle>::const_iterator iEnd (mStyles.end());
1055 for (::std::vector<SharedViewStyle>::const_iterator iStyle=mStyles.begin(); iStyle!=iEnd; ++iStyle)
1056 if ((*iStyle)->msStyleName == rsStyleName)
1057 return *iStyle;
1058 return SharedViewStyle();
1061 //===== ViewStyle =============================================================
1063 ViewStyle::ViewStyle()
1064 : msStyleName(),
1065 mpParentStyle(),
1066 mpFont(),
1067 mpBackground()
1071 ViewStyle::~ViewStyle()
1075 const SharedBitmapDescriptor ViewStyle::GetBitmap (const OUString& rsBitmapName) const
1077 if (rsBitmapName == "Background")
1078 return mpBackground;
1079 else
1080 return SharedBitmapDescriptor();
1083 PresenterTheme::SharedFontDescriptor ViewStyle::GetFont() const
1085 if (mpFont.get() != NULL)
1086 return mpFont;
1087 else if (mpParentStyle.get() != NULL)
1088 return mpParentStyle->GetFont();
1089 else
1090 return PresenterTheme::SharedFontDescriptor();
1093 //===== StyleAssociationContainer =============================================
1095 void StyleAssociationContainer::Read (
1096 ReadContext& rReadContext,
1097 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
1099 Reference<container::XNameAccess> xStyleAssociationList (
1100 PresenterConfigurationAccess::GetConfigurationNode(
1101 rxThemeRoot,
1102 "StyleAssociations"),
1103 UNO_QUERY);
1104 if (xStyleAssociationList.is())
1106 ::std::vector<OUString> aProperties (2);
1107 aProperties[0] = "ResourceURL";
1108 aProperties[1] = "StyleName";
1109 PresenterConfigurationAccess::ForAll(
1110 xStyleAssociationList,
1111 aProperties,
1112 ::boost::bind(&StyleAssociationContainer::ProcessStyleAssociation,
1113 this, ::boost::ref(rReadContext), _1, _2));
1117 OUString StyleAssociationContainer::GetStyleName (const OUString& rsResourceName) const
1119 StyleAssociations::const_iterator iAssociation (maStyleAssociations.find(rsResourceName));
1120 if (iAssociation != maStyleAssociations.end())
1121 return iAssociation->second;
1122 else
1123 return OUString();
1126 void StyleAssociationContainer::ProcessStyleAssociation(
1127 ReadContext& rReadContext,
1128 const OUString& rsKey,
1129 const ::std::vector<Any>& rValues)
1131 (void)rReadContext;
1132 (void)rsKey;
1134 if (rValues.size() != 2)
1135 return;
1137 OUString sResourceURL;
1138 OUString sStyleName;
1139 if ((rValues[0] >>= sResourceURL)
1140 && (rValues[1] >>= sStyleName))
1142 maStyleAssociations[sResourceURL] = sStyleName;
1146 } // end of anonymous namespace
1148 } } // end of namespace ::sdext::presenter
1150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */