bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sdext / source / presenter / PresenterTheme.cxx
blob751332d5e00ee8fd2ab3d20e3ab687b9ae3908b7
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 <map>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::std;
40 namespace sdext { namespace presenter {
42 namespace {
44 class BorderSize
46 public:
47 const static sal_Int32 mnInvalidValue = -10000;
49 BorderSize() : mnLeft(mnInvalidValue),
50 mnTop(mnInvalidValue),
51 mnRight(mnInvalidValue),
52 mnBottom(mnInvalidValue) {}
54 sal_Int32 mnLeft;
55 sal_Int32 mnTop;
56 sal_Int32 mnRight;
57 sal_Int32 mnBottom;
59 vector<sal_Int32> ToVector()
61 vector<sal_Int32> aSequence (4);
62 aSequence[0] = mnLeft == mnInvalidValue ? 0 : mnLeft;
63 aSequence[1] = mnTop == mnInvalidValue ? 0 : mnTop;
64 aSequence[2] = mnRight == mnInvalidValue ? 0 : mnRight;
65 aSequence[3] = mnBottom == mnInvalidValue ? 0 : mnBottom;
66 return aSequence;
69 void Merge (const BorderSize& rBorderSize)
71 if (mnLeft == mnInvalidValue)
72 mnLeft = rBorderSize.mnLeft;
73 if (mnTop == mnInvalidValue)
74 mnTop = rBorderSize.mnTop;
75 if (mnRight == mnInvalidValue)
76 mnRight = rBorderSize.mnRight;
77 if (mnBottom == mnInvalidValue)
78 mnBottom = rBorderSize.mnBottom;
82 /** Reading a theme from the configurations is done in various classes. The
83 ReadContext gives access to frequently used objects and functions to make
84 the configuration handling easier.
86 class ReadContext
88 public:
89 Reference<XComponentContext> mxComponentContext;
90 Reference<rendering::XCanvas> mxCanvas;
91 Reference<drawing::XPresenterHelper> mxPresenterHelper;
93 ReadContext (
94 const Reference<XComponentContext>& rxContext,
95 const Reference<rendering::XCanvas>& rxCanvas);
97 /** Read data describing a font from the node that can be reached from
98 the given root via the given path.
99 @param rsFontPath
100 May be empty.
102 static PresenterTheme::SharedFontDescriptor ReadFont (
103 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxTheme,
104 const OUString& rsFontPath,
105 const PresenterTheme::SharedFontDescriptor& rpDefault);
106 static PresenterTheme::SharedFontDescriptor ReadFont (
107 const Reference<beans::XPropertySet>& rxFontProperties,
108 const PresenterTheme::SharedFontDescriptor& rpDefault);
110 std::shared_ptr<PresenterTheme::Theme> ReadTheme (
111 PresenterConfigurationAccess& rConfiguration,
112 const OUString& rsThemeName);
114 static BorderSize ReadBorderSize (const Reference<container::XNameAccess>& rxNode);
116 private:
117 static Any GetByName (
118 const Reference<container::XNameAccess>& rxNode,
119 const OUString& rsName);
122 /** A PaneStyle describes how a pane is rendered.
124 class PaneStyle
126 public:
127 PaneStyle();
129 const SharedBitmapDescriptor GetBitmap (const OUString& sBitmapName) const;
131 OUString msStyleName;
132 std::shared_ptr<PaneStyle> mpParentStyle;
133 PresenterTheme::SharedFontDescriptor mpFont;
134 BorderSize maInnerBorderSize;
135 BorderSize maOuterBorderSize;
136 std::shared_ptr<PresenterBitmapContainer> mpBitmaps;
138 PresenterTheme::SharedFontDescriptor GetFont() const;
141 typedef std::shared_ptr<PaneStyle> SharedPaneStyle;
143 class PaneStyleContainer
145 private:
146 ::std::vector<SharedPaneStyle> mStyles;
148 public:
149 void Read (
150 const ReadContext& rReadContext,
151 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
153 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
155 private:
156 void ProcessPaneStyle (
157 ReadContext const & rReadContext,
158 const ::std::vector<css::uno::Any>& rValues);
161 /** A ViewStyle describes how a view is displayed.
163 class ViewStyle
165 public:
166 ViewStyle();
168 const SharedBitmapDescriptor GetBitmap (const OUString& sBitmapName) const;
170 PresenterTheme::SharedFontDescriptor GetFont() const;
172 OUString msStyleName;
173 std::shared_ptr<ViewStyle> mpParentStyle;
174 PresenterTheme::SharedFontDescriptor mpFont;
175 SharedBitmapDescriptor mpBackground;
178 typedef std::shared_ptr<ViewStyle> SharedViewStyle;
180 class ViewStyleContainer
182 private:
183 ::std::vector<SharedViewStyle> mStyles;
185 public:
186 void Read (
187 const ReadContext& rReadContext,
188 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
190 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
192 private:
193 void ProcessViewStyle(
194 ReadContext const & rReadContext,
195 const Reference<beans::XPropertySet>& rxProperties);
198 class StyleAssociationContainer
200 public:
201 void Read (
202 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
204 OUString GetStyleName (const OUString& rsResourceName) const;
206 private:
207 typedef map<OUString, OUString> StyleAssociations;
208 StyleAssociations maStyleAssociations;
210 void ProcessStyleAssociation(
211 const ::std::vector<css::uno::Any>& rValues);
214 } // end of anonymous namespace
216 class PresenterTheme::Theme
218 public:
219 Theme (
220 const Reference<container::XHierarchicalNameAccess>& rThemeRoot,
221 const OUString& rsNodeName);
223 void Read (
224 PresenterConfigurationAccess& rConfiguration,
225 ReadContext& rReadContext);
227 OUString const msConfigurationNodeName;
228 std::shared_ptr<Theme> mpParentTheme;
229 SharedBitmapDescriptor mpBackground;
230 PaneStyleContainer maPaneStyles;
231 ViewStyleContainer maViewStyles;
232 StyleAssociationContainer maStyleAssociations;
233 Reference<container::XHierarchicalNameAccess> mxThemeRoot;
234 std::shared_ptr<PresenterBitmapContainer> mpIconContainer;
235 typedef map<OUString,SharedFontDescriptor> FontContainer;
236 FontContainer maFontContainer;
238 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
239 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
241 private:
242 void ProcessFont(
243 const OUString& rsKey,
244 const Reference<beans::XPropertySet>& rxProperties);
247 //===== PresenterTheme ========================================================
249 PresenterTheme::PresenterTheme (
250 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
251 const css::uno::Reference<css::rendering::XCanvas>& rxCanvas)
252 : mxContext(rxContext),
253 mpTheme(),
254 mxCanvas(rxCanvas)
256 mpTheme = ReadTheme();
259 PresenterTheme::~PresenterTheme()
263 std::shared_ptr<PresenterTheme::Theme> PresenterTheme::ReadTheme()
265 ReadContext aReadContext(mxContext, mxCanvas);
267 PresenterConfigurationAccess aConfiguration (
268 mxContext,
269 "/org.openoffice.Office.PresenterScreen/",
270 PresenterConfigurationAccess::READ_ONLY);
272 return aReadContext.ReadTheme(aConfiguration, OUString());
275 bool PresenterTheme::HasCanvas() const
277 return mxCanvas.is();
280 void PresenterTheme::ProvideCanvas (const Reference<rendering::XCanvas>& rxCanvas)
282 if ( ! mxCanvas.is() && rxCanvas.is())
284 mxCanvas = rxCanvas;
285 ReadTheme();
289 OUString PresenterTheme::GetStyleName (const OUString& rsResourceURL) const
291 OUString sStyleName;
292 std::shared_ptr<Theme> pTheme (mpTheme);
293 while (sStyleName.isEmpty() && pTheme != nullptr)
295 sStyleName = pTheme->maStyleAssociations.GetStyleName(rsResourceURL);
296 pTheme = pTheme->mpParentTheme;
298 return sStyleName;
301 ::std::vector<sal_Int32> PresenterTheme::GetBorderSize (
302 const OUString& rsStyleName,
303 const bool bOuter) const
305 OSL_ASSERT(mpTheme != nullptr);
307 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
308 if (pPaneStyle.get() != nullptr)
309 if (bOuter)
310 return pPaneStyle->maOuterBorderSize.ToVector();
311 else
312 return pPaneStyle->maInnerBorderSize.ToVector();
313 else
315 return ::std::vector<sal_Int32>(4,0);
319 PresenterTheme::SharedFontDescriptor PresenterTheme::ReadFont (
320 const Reference<container::XHierarchicalNameAccess>& rxNode,
321 const PresenterTheme::SharedFontDescriptor& rpDefault)
323 return ReadContext::ReadFont(rxNode, OUString(), rpDefault);
326 bool PresenterTheme::ConvertToColor (
327 const Any& rColorSequence,
328 sal_uInt32& rColor)
330 Sequence<sal_Int8> aByteSequence;
331 if (rColorSequence >>= aByteSequence)
333 const sal_Int32 nByteCount (aByteSequence.getLength());
334 const sal_uInt8* pArray = reinterpret_cast<const sal_uInt8*>(aByteSequence.getConstArray());
335 rColor = 0;
336 for (sal_Int32 nIndex=0; nIndex<nByteCount; ++nIndex)
338 rColor = (rColor << 8) | *pArray++;
340 return true;
342 else
343 return false;
346 std::shared_ptr<PresenterConfigurationAccess> PresenterTheme::GetNodeForViewStyle (
347 const OUString& rsStyleName) const
349 if (mpTheme == nullptr)
350 return std::shared_ptr<PresenterConfigurationAccess>();
352 // Open configuration for writing.
353 std::shared_ptr<PresenterConfigurationAccess> pConfiguration (
354 new PresenterConfigurationAccess(
355 mxContext,
356 "/org.openoffice.Office.PresenterScreen/",
357 PresenterConfigurationAccess::READ_WRITE));
359 // Get configuration node for the view style container of the current
360 // theme.
361 if (pConfiguration->GoToChild( OUString(
362 "Presenter/Themes/" + mpTheme->msConfigurationNodeName + "/ViewStyles")))
364 pConfiguration->GoToChild(
365 [&rsStyleName] (OUString const&, uno::Reference<beans::XPropertySet> const& xProps)
367 return PresenterConfigurationAccess::IsStringPropertyEqual(
368 rsStyleName, "StyleName", xProps);
371 return pConfiguration;
374 SharedBitmapDescriptor PresenterTheme::GetBitmap (
375 const OUString& rsStyleName,
376 const OUString& rsBitmapName) const
378 if (mpTheme != nullptr)
380 if (rsStyleName.isEmpty())
382 if (rsBitmapName == "Background")
384 std::shared_ptr<Theme> pTheme (mpTheme);
385 while (pTheme != nullptr && pTheme->mpBackground.get() == nullptr)
386 pTheme = pTheme->mpParentTheme;
387 if (pTheme != nullptr)
388 return pTheme->mpBackground;
389 else
390 return SharedBitmapDescriptor();
393 else
395 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
396 if (pPaneStyle.get() != nullptr)
398 SharedBitmapDescriptor pBitmap (pPaneStyle->GetBitmap(rsBitmapName));
399 if (pBitmap.get() != nullptr)
400 return pBitmap;
403 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
404 if (pViewStyle.get() != nullptr)
406 SharedBitmapDescriptor pBitmap (pViewStyle->GetBitmap(rsBitmapName));
407 if (pBitmap.get() != nullptr)
408 return pBitmap;
413 return SharedBitmapDescriptor();
416 SharedBitmapDescriptor PresenterTheme::GetBitmap (
417 const OUString& rsBitmapName) const
419 if (mpTheme != nullptr)
421 if (rsBitmapName == "Background")
423 std::shared_ptr<Theme> pTheme (mpTheme);
424 while (pTheme != nullptr && pTheme->mpBackground.get() == nullptr)
425 pTheme = pTheme->mpParentTheme;
426 if (pTheme != nullptr)
427 return pTheme->mpBackground;
428 else
429 return SharedBitmapDescriptor();
431 else
433 if (mpTheme->mpIconContainer != nullptr)
434 return mpTheme->mpIconContainer->GetBitmap(rsBitmapName);
438 return SharedBitmapDescriptor();
441 std::shared_ptr<PresenterBitmapContainer> PresenterTheme::GetBitmapContainer() const
443 if (mpTheme != nullptr)
444 return mpTheme->mpIconContainer;
445 else
446 return std::shared_ptr<PresenterBitmapContainer>();
449 PresenterTheme::SharedFontDescriptor PresenterTheme::GetFont (
450 const OUString& rsStyleName) const
452 if (mpTheme != nullptr)
454 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
455 if (pPaneStyle.get() != nullptr)
456 return pPaneStyle->GetFont();
458 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
459 if (pViewStyle.get() != nullptr)
460 return pViewStyle->GetFont();
462 std::shared_ptr<Theme> pTheme (mpTheme);
463 while (pTheme != nullptr)
465 Theme::FontContainer::const_iterator iFont (pTheme->maFontContainer.find(rsStyleName));
466 if (iFont != pTheme->maFontContainer.end())
467 return iFont->second;
469 pTheme = pTheme->mpParentTheme;
473 return SharedFontDescriptor();
476 //===== FontDescriptor ========================================================
478 PresenterTheme::FontDescriptor::FontDescriptor (
479 const std::shared_ptr<FontDescriptor>& rpDescriptor)
480 : msFamilyName(),
481 msStyleName(),
482 mnSize(12),
483 mnColor(0x00000000),
484 msAnchor(OUString("Left")),
485 mnXOffset(0),
486 mnYOffset(0)
488 if (rpDescriptor != nullptr)
490 msFamilyName = rpDescriptor->msFamilyName;
491 msStyleName = rpDescriptor->msStyleName;
492 mnSize = rpDescriptor->mnSize;
493 mnColor = rpDescriptor->mnColor;
494 msAnchor = rpDescriptor->msAnchor;
495 mnXOffset = rpDescriptor->mnXOffset;
496 mnYOffset = rpDescriptor->mnYOffset;
500 bool PresenterTheme::FontDescriptor::PrepareFont (
501 const Reference<rendering::XCanvas>& rxCanvas)
503 if (mxFont.is())
504 return true;
506 if ( ! rxCanvas.is())
507 return false;
509 const double nCellSize (GetCellSizeForDesignSize(rxCanvas, mnSize));
510 mxFont = CreateFont(rxCanvas, nCellSize);
512 return mxFont.is();
515 Reference<rendering::XCanvasFont> PresenterTheme::FontDescriptor::CreateFont (
516 const Reference<rendering::XCanvas>& rxCanvas,
517 const double nCellSize) const
519 rendering::FontRequest aFontRequest;
520 aFontRequest.FontDescription.FamilyName = msFamilyName;
521 if (msFamilyName.isEmpty())
522 aFontRequest.FontDescription.FamilyName = "Tahoma";
523 aFontRequest.FontDescription.StyleName = msStyleName;
524 aFontRequest.CellSize = nCellSize;
526 // Make an attempt at translating the style name(s)into a corresponding
527 // font description.
528 if (msStyleName == "Bold")
529 aFontRequest.FontDescription.FontDescription.Weight = rendering::PanoseWeight::HEAVY;
531 return rxCanvas->createFont(
532 aFontRequest,
533 Sequence<beans::PropertyValue>(),
534 geometry::Matrix2D(1,0,0,1));
537 double PresenterTheme::FontDescriptor::GetCellSizeForDesignSize (
538 const Reference<rendering::XCanvas>& rxCanvas,
539 const double nDesignSize) const
541 // Use the given design size as initial value in calculating the cell
542 // size.
543 double nCellSize (nDesignSize);
545 if ( ! rxCanvas.is())
547 // We need the canvas to do the conversion. Return the design size,
548 // it is the our best guess in this circumstance.
549 return nDesignSize;
552 Reference<rendering::XCanvasFont> xFont (CreateFont(rxCanvas, nCellSize));
553 if ( ! xFont.is())
554 return nDesignSize;
556 geometry::RealRectangle2D aBox (PresenterCanvasHelper::GetTextBoundingBox (xFont, "X"));
558 const double nAscent (-aBox.Y1);
559 //tdf#112408
560 if (nAscent == 0)
561 return nDesignSize;
562 const double nDescent (aBox.Y2);
563 const double nScale = (nAscent+nDescent) / nAscent;
564 return nDesignSize * nScale;
567 //===== Theme =================================================================
569 PresenterTheme::Theme::Theme (
570 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot,
571 const OUString& rsNodeName)
572 : msConfigurationNodeName(rsNodeName),
573 mpParentTheme(),
574 maPaneStyles(),
575 maViewStyles(),
576 maStyleAssociations(),
577 mxThemeRoot(rxThemeRoot),
578 mpIconContainer()
582 void PresenterTheme::Theme::Read (
583 PresenterConfigurationAccess& rConfiguration,
584 ReadContext& rReadContext)
586 // Parent theme name.
587 OUString sParentThemeName;
588 if ((PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "ParentTheme")
589 >>= sParentThemeName)
590 && !sParentThemeName.isEmpty())
592 mpParentTheme = rReadContext.ReadTheme(rConfiguration, sParentThemeName);
595 // Background.
596 mpBackground = PresenterBitmapContainer::LoadBitmap(
597 mxThemeRoot,
598 "Background",
599 rReadContext.mxPresenterHelper,
600 rReadContext.mxCanvas,
601 SharedBitmapDescriptor());
603 // Style associations.
604 maStyleAssociations.Read(mxThemeRoot);
606 // Pane styles.
607 maPaneStyles.Read(rReadContext, mxThemeRoot);
609 // View styles.
610 maViewStyles.Read(rReadContext, mxThemeRoot);
612 // Read bitmaps.
613 mpIconContainer.reset(new PresenterBitmapContainer(
614 Reference<container::XNameAccess>(
615 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Bitmaps"), UNO_QUERY),
616 mpParentTheme != nullptr ? mpParentTheme->mpIconContainer
617 : std::shared_ptr<PresenterBitmapContainer>(),
618 rReadContext.mxComponentContext, rReadContext.mxCanvas));
620 // Read fonts.
621 Reference<container::XNameAccess> xFontNode(
622 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Fonts"),
623 UNO_QUERY);
624 PresenterConfigurationAccess::ForAll(
625 xFontNode,
626 [this] (OUString const& rKey, uno::Reference<beans::XPropertySet> const& xProps)
628 return this->ProcessFont(rKey, xProps);
632 SharedPaneStyle PresenterTheme::Theme::GetPaneStyle (const OUString& rsStyleName) const
634 SharedPaneStyle pPaneStyle (maPaneStyles.GetPaneStyle(rsStyleName));
635 if (pPaneStyle.get() != nullptr)
636 return pPaneStyle;
637 else if (mpParentTheme != nullptr)
638 return mpParentTheme->GetPaneStyle(rsStyleName);
639 else
640 return SharedPaneStyle();
643 SharedViewStyle PresenterTheme::Theme::GetViewStyle (const OUString& rsStyleName) const
645 SharedViewStyle pViewStyle (maViewStyles.GetViewStyle(rsStyleName));
646 if (pViewStyle.get() != nullptr)
647 return pViewStyle;
648 else if (mpParentTheme != nullptr)
649 return mpParentTheme->GetViewStyle(rsStyleName);
650 else
651 return SharedViewStyle();
654 void PresenterTheme::Theme::ProcessFont(
655 const OUString& rsKey,
656 const Reference<beans::XPropertySet>& rxProperties)
658 maFontContainer[rsKey] = ReadContext::ReadFont(rxProperties, SharedFontDescriptor());
661 namespace {
663 //===== ReadContext ===========================================================
665 ReadContext::ReadContext (
666 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
667 const Reference<rendering::XCanvas>& rxCanvas)
668 : mxComponentContext(rxContext),
669 mxCanvas(rxCanvas),
670 mxPresenterHelper()
672 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
673 if (xFactory.is())
675 mxPresenterHelper.set(
676 xFactory->createInstanceWithContext(
677 "com.sun.star.comp.Draw.PresenterHelper",
678 rxContext),
679 UNO_QUERY_THROW);
683 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
684 const Reference<container::XHierarchicalNameAccess>& rxNode,
685 const OUString& rsFontPath,
686 const PresenterTheme::SharedFontDescriptor& rpDefault)
688 if ( ! rxNode.is())
689 return PresenterTheme::SharedFontDescriptor();
693 Reference<container::XHierarchicalNameAccess> xFont (
694 PresenterConfigurationAccess::GetConfigurationNode(
695 rxNode,
696 rsFontPath),
697 UNO_QUERY_THROW);
699 Reference<beans::XPropertySet> xProperties (xFont, UNO_QUERY_THROW);
700 return ReadFont(xProperties, rpDefault);
702 catch (Exception&)
704 OSL_ASSERT(false);
707 return PresenterTheme::SharedFontDescriptor();
710 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
711 const Reference<beans::XPropertySet>& rxProperties,
712 const PresenterTheme::SharedFontDescriptor& rpDefault)
714 std::shared_ptr<PresenterTheme::FontDescriptor> pDescriptor (
715 new PresenterTheme::FontDescriptor(rpDefault));
717 PresenterConfigurationAccess::GetProperty(rxProperties, "FamilyName") >>= pDescriptor->msFamilyName;
718 PresenterConfigurationAccess::GetProperty(rxProperties, "Style") >>= pDescriptor->msStyleName;
719 PresenterConfigurationAccess::GetProperty(rxProperties, "Size") >>= pDescriptor->mnSize;
720 PresenterTheme::ConvertToColor(
721 PresenterConfigurationAccess::GetProperty(rxProperties, "Color"),
722 pDescriptor->mnColor);
723 PresenterConfigurationAccess::GetProperty(rxProperties, "Anchor") >>= pDescriptor->msAnchor;
724 PresenterConfigurationAccess::GetProperty(rxProperties, "XOffset") >>= pDescriptor->mnXOffset;
725 PresenterConfigurationAccess::GetProperty(rxProperties, "YOffset") >>= pDescriptor->mnYOffset;
727 return pDescriptor;
730 Any ReadContext::GetByName (
731 const Reference<container::XNameAccess>& rxNode,
732 const OUString& rsName)
734 OSL_ASSERT(rxNode.is());
735 if (rxNode->hasByName(rsName))
736 return rxNode->getByName(rsName);
737 else
738 return Any();
741 std::shared_ptr<PresenterTheme::Theme> ReadContext::ReadTheme (
742 PresenterConfigurationAccess& rConfiguration,
743 const OUString& rsThemeName)
745 std::shared_ptr<PresenterTheme::Theme> pTheme;
747 OUString sCurrentThemeName (rsThemeName);
748 if (sCurrentThemeName.isEmpty())
750 // No theme name given. Look up the CurrentTheme property.
751 rConfiguration.GetConfigurationNode("Presenter/CurrentTheme") >>= sCurrentThemeName;
752 if (sCurrentThemeName.isEmpty())
754 // Still no name. Use "DefaultTheme".
755 sCurrentThemeName = "DefaultTheme";
759 Reference<container::XNameAccess> xThemes (
760 rConfiguration.GetConfigurationNode("Presenter/Themes"),
761 UNO_QUERY);
762 if (xThemes.is())
764 // Iterate over all themes and search the one with the given name.
765 Sequence<OUString> aKeys (xThemes->getElementNames());
766 for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
768 const OUString& rsKey (aKeys[nItemIndex]);
769 Reference<container::XHierarchicalNameAccess> xTheme (
770 xThemes->getByName(rsKey), UNO_QUERY);
771 if (xTheme.is())
773 OUString sThemeName;
774 PresenterConfigurationAccess::GetConfigurationNode(xTheme, "ThemeName")
775 >>= sThemeName;
776 if (sThemeName == sCurrentThemeName)
778 pTheme.reset(new PresenterTheme::Theme(xTheme,rsKey));
779 break;
785 if (pTheme != nullptr)
787 pTheme->Read(rConfiguration, *this);
790 return pTheme;
793 BorderSize ReadContext::ReadBorderSize (const Reference<container::XNameAccess>& rxNode)
795 BorderSize aBorderSize;
797 if (rxNode.is())
799 GetByName(rxNode, "Left") >>= aBorderSize.mnLeft;
800 GetByName(rxNode, "Top") >>= aBorderSize.mnTop;
801 GetByName(rxNode, "Right") >>= aBorderSize.mnRight;
802 GetByName(rxNode, "Bottom") >>= aBorderSize.mnBottom;
805 return aBorderSize;
808 //===== PaneStyleContainer ====================================================
810 void PaneStyleContainer::Read (
811 const ReadContext& rReadContext,
812 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
814 Reference<container::XNameAccess> xPaneStyleList (
815 PresenterConfigurationAccess::GetConfigurationNode(
816 rxThemeRoot,
817 "PaneStyles"),
818 UNO_QUERY);
819 if (!xPaneStyleList.is())
820 return;
822 ::std::vector<OUString> aProperties;
823 aProperties.reserve(6);
824 aProperties.emplace_back("StyleName");
825 aProperties.emplace_back("ParentStyle");
826 aProperties.emplace_back("TitleFont");
827 aProperties.emplace_back("InnerBorderSize");
828 aProperties.emplace_back("OuterBorderSize");
829 aProperties.emplace_back("BorderBitmapList");
830 PresenterConfigurationAccess::ForAll(
831 xPaneStyleList,
832 aProperties,
833 [this, &rReadContext] (std::vector<uno::Any> const& rValues)
835 return this->ProcessPaneStyle(rReadContext, rValues);
839 void PaneStyleContainer::ProcessPaneStyle(
840 ReadContext const & rReadContext,
841 const ::std::vector<Any>& rValues)
843 if (rValues.size() != 6)
844 return;
846 std::shared_ptr<PaneStyle> pStyle (new PaneStyle());
848 rValues[0] >>= pStyle->msStyleName;
850 OUString sParentStyleName;
851 if (rValues[1] >>= sParentStyleName)
853 // Find parent style.
854 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
855 [&sParentStyleName](const SharedPaneStyle& rxStyle) { return rxStyle->msStyleName == sParentStyleName; });
856 if (iStyle != mStyles.end())
857 pStyle->mpParentStyle = *iStyle;
860 Reference<container::XHierarchicalNameAccess> xFontNode (rValues[2], UNO_QUERY);
861 pStyle->mpFont = ReadContext::ReadFont(
862 xFontNode, "", PresenterTheme::SharedFontDescriptor());
864 Reference<container::XNameAccess> xInnerBorderSizeNode (rValues[3], UNO_QUERY);
865 pStyle->maInnerBorderSize = ReadContext::ReadBorderSize(xInnerBorderSizeNode);
866 Reference<container::XNameAccess> xOuterBorderSizeNode (rValues[4], UNO_QUERY);
867 pStyle->maOuterBorderSize = ReadContext::ReadBorderSize(xOuterBorderSizeNode);
869 if (pStyle->mpParentStyle != nullptr)
871 pStyle->maInnerBorderSize.Merge(pStyle->mpParentStyle->maInnerBorderSize);
872 pStyle->maOuterBorderSize.Merge(pStyle->mpParentStyle->maOuterBorderSize);
875 if (rReadContext.mxCanvas.is())
877 Reference<container::XNameAccess> xBitmapsNode (rValues[5], UNO_QUERY);
878 pStyle->mpBitmaps.reset(new PresenterBitmapContainer(
879 xBitmapsNode,
880 pStyle->mpParentStyle != nullptr ? pStyle->mpParentStyle->mpBitmaps
881 : std::shared_ptr<PresenterBitmapContainer>(),
882 rReadContext.mxComponentContext, rReadContext.mxCanvas,
883 rReadContext.mxPresenterHelper));
886 mStyles.push_back(pStyle);
889 SharedPaneStyle PaneStyleContainer::GetPaneStyle (const OUString& rsStyleName) const
891 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
892 [&rsStyleName](const SharedPaneStyle& rxStyle) { return rxStyle->msStyleName == rsStyleName; });
893 if (iStyle != mStyles.end())
894 return *iStyle;
895 return SharedPaneStyle();
898 //===== PaneStyle =============================================================
900 PaneStyle::PaneStyle()
901 : msStyleName(),
902 mpParentStyle(),
903 mpFont(),
904 maInnerBorderSize(),
905 maOuterBorderSize(),
906 mpBitmaps()
910 const SharedBitmapDescriptor PaneStyle::GetBitmap (const OUString& rsBitmapName) const
912 if (mpBitmaps != nullptr)
914 const SharedBitmapDescriptor pBitmap = mpBitmaps->GetBitmap(rsBitmapName);
915 if (pBitmap.get() != nullptr)
916 return pBitmap;
919 if (mpParentStyle != nullptr)
920 return mpParentStyle->GetBitmap(rsBitmapName);
921 else
922 return SharedBitmapDescriptor();
925 PresenterTheme::SharedFontDescriptor PaneStyle::GetFont() const
927 if (mpFont.get() != nullptr)
928 return mpFont;
929 else if (mpParentStyle != nullptr)
930 return mpParentStyle->GetFont();
931 else
932 return PresenterTheme::SharedFontDescriptor();
935 //===== ViewStyleContainer ====================================================
937 void ViewStyleContainer::Read (
938 const ReadContext& rReadContext,
939 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
941 Reference<container::XNameAccess> xViewStyleList (
942 PresenterConfigurationAccess::GetConfigurationNode(
943 rxThemeRoot,
944 "ViewStyles"),
945 UNO_QUERY);
946 if (xViewStyleList.is())
948 PresenterConfigurationAccess::ForAll(
949 xViewStyleList,
950 [this, &rReadContext] (OUString const&, uno::Reference<beans::XPropertySet> const& xProps)
952 return this->ProcessViewStyle(rReadContext, xProps);
957 void ViewStyleContainer::ProcessViewStyle(
958 ReadContext const & rReadContext,
959 const Reference<beans::XPropertySet>& rxProperties)
961 std::shared_ptr<ViewStyle> pStyle (new ViewStyle());
963 PresenterConfigurationAccess::GetProperty(rxProperties, "StyleName")
964 >>= pStyle->msStyleName;
966 OUString sParentStyleName;
967 if (PresenterConfigurationAccess::GetProperty(rxProperties, "ParentStyle")
968 >>= sParentStyleName)
970 // Find parent style.
971 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
972 [&sParentStyleName](const SharedViewStyle& rxStyle) { return rxStyle->msStyleName == sParentStyleName; });
973 if (iStyle != mStyles.end())
975 pStyle->mpParentStyle = *iStyle;
976 pStyle->mpFont = (*iStyle)->mpFont;
977 pStyle->mpBackground = (*iStyle)->mpBackground;
981 const OUString sPathToFont; // empty string
982 Reference<container::XHierarchicalNameAccess> xFontNode (
983 PresenterConfigurationAccess::GetProperty(rxProperties, "Font"), UNO_QUERY);
984 PresenterTheme::SharedFontDescriptor pFont (
985 ReadContext::ReadFont(xFontNode, sPathToFont, PresenterTheme::SharedFontDescriptor()));
986 if (pFont.get() != nullptr)
987 pStyle->mpFont = pFont;
989 Reference<container::XHierarchicalNameAccess> xBackgroundNode (
990 PresenterConfigurationAccess::GetProperty(rxProperties, "Background"),
991 UNO_QUERY);
992 SharedBitmapDescriptor pBackground (PresenterBitmapContainer::LoadBitmap(
993 xBackgroundNode,
994 OUString(),
995 rReadContext.mxPresenterHelper,
996 rReadContext.mxCanvas,
997 SharedBitmapDescriptor()));
998 if (pBackground.get() != nullptr && pBackground->GetNormalBitmap().is())
999 pStyle->mpBackground = pBackground;
1001 mStyles.push_back(pStyle);
1004 SharedViewStyle ViewStyleContainer::GetViewStyle (const OUString& rsStyleName) const
1006 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
1007 [&rsStyleName](const SharedViewStyle& rxStyle) { return rxStyle->msStyleName == rsStyleName; });
1008 if (iStyle != mStyles.end())
1009 return *iStyle;
1010 return SharedViewStyle();
1013 //===== ViewStyle =============================================================
1015 ViewStyle::ViewStyle()
1016 : msStyleName(),
1017 mpParentStyle(),
1018 mpFont(),
1019 mpBackground()
1023 const SharedBitmapDescriptor ViewStyle::GetBitmap (const OUString& rsBitmapName) const
1025 if (rsBitmapName == "Background")
1026 return mpBackground;
1027 else
1028 return SharedBitmapDescriptor();
1031 PresenterTheme::SharedFontDescriptor ViewStyle::GetFont() const
1033 if (mpFont.get() != nullptr)
1034 return mpFont;
1035 else if (mpParentStyle != nullptr)
1036 return mpParentStyle->GetFont();
1037 else
1038 return PresenterTheme::SharedFontDescriptor();
1041 //===== StyleAssociationContainer =============================================
1043 void StyleAssociationContainer::Read (
1044 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
1046 Reference<container::XNameAccess> xStyleAssociationList (
1047 PresenterConfigurationAccess::GetConfigurationNode(
1048 rxThemeRoot,
1049 "StyleAssociations"),
1050 UNO_QUERY);
1051 if (!xStyleAssociationList.is())
1052 return;
1054 ::std::vector<OUString> aProperties (2);
1055 aProperties[0] = "ResourceURL";
1056 aProperties[1] = "StyleName";
1057 PresenterConfigurationAccess::ForAll(
1058 xStyleAssociationList,
1059 aProperties,
1060 [this] (std::vector<uno::Any> const& rValues)
1062 return this->ProcessStyleAssociation(rValues);
1066 OUString StyleAssociationContainer::GetStyleName (const OUString& rsResourceName) const
1068 StyleAssociations::const_iterator iAssociation (maStyleAssociations.find(rsResourceName));
1069 if (iAssociation != maStyleAssociations.end())
1070 return iAssociation->second;
1071 else
1072 return OUString();
1075 void StyleAssociationContainer::ProcessStyleAssociation(
1076 const ::std::vector<Any>& rValues)
1078 if (rValues.size() != 2)
1079 return;
1081 OUString sResourceURL;
1082 OUString sStyleName;
1083 if ((rValues[0] >>= sResourceURL)
1084 && (rValues[1] >>= sStyleName))
1086 maStyleAssociations[sResourceURL] = sStyleName;
1090 } // end of anonymous namespace
1092 } } // end of namespace ::sdext::presenter
1094 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */