Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sdext / source / presenter / PresenterTheme.cxx
bloba1a06d617d12c0574ad18d290fd3377d73aa7723
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 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.get()!=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.get() != 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.get() == 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.get() != nullptr)
380 if (rsStyleName.isEmpty())
382 if (rsBitmapName == "Background")
384 std::shared_ptr<Theme> pTheme (mpTheme);
385 while (pTheme.get()!=nullptr && pTheme->mpBackground.get()==nullptr)
386 pTheme = pTheme->mpParentTheme;
387 if (pTheme.get() != 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.get() != nullptr)
421 if (rsBitmapName == "Background")
423 std::shared_ptr<Theme> pTheme (mpTheme);
424 while (pTheme.get()!=nullptr && pTheme->mpBackground.get()==nullptr)
425 pTheme = pTheme->mpParentTheme;
426 if (pTheme.get() != nullptr)
427 return pTheme->mpBackground;
428 else
429 return SharedBitmapDescriptor();
431 else
433 if (mpTheme->mpIconContainer.get() != nullptr)
434 return mpTheme->mpIconContainer->GetBitmap(rsBitmapName);
438 return SharedBitmapDescriptor();
441 std::shared_ptr<PresenterBitmapContainer> PresenterTheme::GetBitmapContainer() const
443 if (mpTheme.get() != 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.get() != 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.get() != 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.get() != 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(
614 new PresenterBitmapContainer(
615 Reference<container::XNameAccess>(
616 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Bitmaps"),
617 UNO_QUERY),
618 mpParentTheme.get()!=nullptr
619 ? mpParentTheme->mpIconContainer
620 : std::shared_ptr<PresenterBitmapContainer>(),
621 rReadContext.mxComponentContext,
622 rReadContext.mxCanvas));
624 // Read fonts.
625 Reference<container::XNameAccess> xFontNode(
626 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, "Fonts"),
627 UNO_QUERY);
628 PresenterConfigurationAccess::ForAll(
629 xFontNode,
630 [this] (OUString const& rKey, uno::Reference<beans::XPropertySet> const& xProps)
632 return this->ProcessFont(rKey, xProps);
636 SharedPaneStyle PresenterTheme::Theme::GetPaneStyle (const OUString& rsStyleName) const
638 SharedPaneStyle pPaneStyle (maPaneStyles.GetPaneStyle(rsStyleName));
639 if (pPaneStyle.get() != nullptr)
640 return pPaneStyle;
641 else if (mpParentTheme.get() != nullptr)
642 return mpParentTheme->GetPaneStyle(rsStyleName);
643 else
644 return SharedPaneStyle();
647 SharedViewStyle PresenterTheme::Theme::GetViewStyle (const OUString& rsStyleName) const
649 SharedViewStyle pViewStyle (maViewStyles.GetViewStyle(rsStyleName));
650 if (pViewStyle.get() != nullptr)
651 return pViewStyle;
652 else if (mpParentTheme.get() != nullptr)
653 return mpParentTheme->GetViewStyle(rsStyleName);
654 else
655 return SharedViewStyle();
658 void PresenterTheme::Theme::ProcessFont(
659 const OUString& rsKey,
660 const Reference<beans::XPropertySet>& rxProperties)
662 maFontContainer[rsKey] = ReadContext::ReadFont(rxProperties, SharedFontDescriptor());
665 namespace {
667 //===== ReadContext ===========================================================
669 ReadContext::ReadContext (
670 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
671 const Reference<rendering::XCanvas>& rxCanvas)
672 : mxComponentContext(rxContext),
673 mxCanvas(rxCanvas),
674 mxPresenterHelper()
676 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
677 if (xFactory.is())
679 mxPresenterHelper.set(
680 xFactory->createInstanceWithContext(
681 "com.sun.star.comp.Draw.PresenterHelper",
682 rxContext),
683 UNO_QUERY_THROW);
687 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
688 const Reference<container::XHierarchicalNameAccess>& rxNode,
689 const OUString& rsFontPath,
690 const PresenterTheme::SharedFontDescriptor& rpDefault)
692 if ( ! rxNode.is())
693 return PresenterTheme::SharedFontDescriptor();
697 Reference<container::XHierarchicalNameAccess> xFont (
698 PresenterConfigurationAccess::GetConfigurationNode(
699 rxNode,
700 rsFontPath),
701 UNO_QUERY_THROW);
703 Reference<beans::XPropertySet> xProperties (xFont, UNO_QUERY_THROW);
704 return ReadFont(xProperties, rpDefault);
706 catch (Exception&)
708 OSL_ASSERT(false);
711 return PresenterTheme::SharedFontDescriptor();
714 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
715 const Reference<beans::XPropertySet>& rxProperties,
716 const PresenterTheme::SharedFontDescriptor& rpDefault)
718 std::shared_ptr<PresenterTheme::FontDescriptor> pDescriptor (
719 new PresenterTheme::FontDescriptor(rpDefault));
721 PresenterConfigurationAccess::GetProperty(rxProperties, "FamilyName") >>= pDescriptor->msFamilyName;
722 PresenterConfigurationAccess::GetProperty(rxProperties, "Style") >>= pDescriptor->msStyleName;
723 PresenterConfigurationAccess::GetProperty(rxProperties, "Size") >>= pDescriptor->mnSize;
724 PresenterTheme::ConvertToColor(
725 PresenterConfigurationAccess::GetProperty(rxProperties, "Color"),
726 pDescriptor->mnColor);
727 PresenterConfigurationAccess::GetProperty(rxProperties, "Anchor") >>= pDescriptor->msAnchor;
728 PresenterConfigurationAccess::GetProperty(rxProperties, "XOffset") >>= pDescriptor->mnXOffset;
729 PresenterConfigurationAccess::GetProperty(rxProperties, "YOffset") >>= pDescriptor->mnYOffset;
731 return pDescriptor;
734 Any ReadContext::GetByName (
735 const Reference<container::XNameAccess>& rxNode,
736 const OUString& rsName)
738 OSL_ASSERT(rxNode.is());
739 if (rxNode->hasByName(rsName))
740 return rxNode->getByName(rsName);
741 else
742 return Any();
745 std::shared_ptr<PresenterTheme::Theme> ReadContext::ReadTheme (
746 PresenterConfigurationAccess& rConfiguration,
747 const OUString& rsThemeName)
749 std::shared_ptr<PresenterTheme::Theme> pTheme;
751 OUString sCurrentThemeName (rsThemeName);
752 if (sCurrentThemeName.isEmpty())
754 // No theme name given. Look up the CurrentTheme property.
755 rConfiguration.GetConfigurationNode("Presenter/CurrentTheme") >>= sCurrentThemeName;
756 if (sCurrentThemeName.isEmpty())
758 // Still no name. Use "DefaultTheme".
759 sCurrentThemeName = "DefaultTheme";
763 Reference<container::XNameAccess> xThemes (
764 rConfiguration.GetConfigurationNode("Presenter/Themes"),
765 UNO_QUERY);
766 if (xThemes.is())
768 // Iterate over all themes and search the one with the given name.
769 Sequence<OUString> aKeys (xThemes->getElementNames());
770 for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
772 const OUString& rsKey (aKeys[nItemIndex]);
773 Reference<container::XHierarchicalNameAccess> xTheme (
774 xThemes->getByName(rsKey), UNO_QUERY);
775 if (xTheme.is())
777 OUString sThemeName;
778 PresenterConfigurationAccess::GetConfigurationNode(xTheme, "ThemeName")
779 >>= sThemeName;
780 if (sThemeName == sCurrentThemeName)
782 pTheme.reset(new PresenterTheme::Theme(xTheme,rsKey));
783 break;
789 if (pTheme.get() != nullptr)
791 pTheme->Read(rConfiguration, *this);
794 return pTheme;
797 BorderSize ReadContext::ReadBorderSize (const Reference<container::XNameAccess>& rxNode)
799 BorderSize aBorderSize;
801 if (rxNode.is())
803 GetByName(rxNode, "Left") >>= aBorderSize.mnLeft;
804 GetByName(rxNode, "Top") >>= aBorderSize.mnTop;
805 GetByName(rxNode, "Right") >>= aBorderSize.mnRight;
806 GetByName(rxNode, "Bottom") >>= aBorderSize.mnBottom;
809 return aBorderSize;
812 //===== PaneStyleContainer ====================================================
814 void PaneStyleContainer::Read (
815 const ReadContext& rReadContext,
816 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
818 Reference<container::XNameAccess> xPaneStyleList (
819 PresenterConfigurationAccess::GetConfigurationNode(
820 rxThemeRoot,
821 "PaneStyles"),
822 UNO_QUERY);
823 if (xPaneStyleList.is())
825 ::std::vector<OUString> aProperties;
826 aProperties.reserve(6);
827 aProperties.emplace_back("StyleName");
828 aProperties.emplace_back("ParentStyle");
829 aProperties.emplace_back("TitleFont");
830 aProperties.emplace_back("InnerBorderSize");
831 aProperties.emplace_back("OuterBorderSize");
832 aProperties.emplace_back("BorderBitmapList");
833 PresenterConfigurationAccess::ForAll(
834 xPaneStyleList,
835 aProperties,
836 [this, &rReadContext] (std::vector<uno::Any> const& rValues)
838 return this->ProcessPaneStyle(rReadContext, rValues);
843 void PaneStyleContainer::ProcessPaneStyle(
844 ReadContext const & rReadContext,
845 const ::std::vector<Any>& rValues)
847 if (rValues.size() != 6)
848 return;
850 std::shared_ptr<PaneStyle> pStyle (new PaneStyle());
852 rValues[0] >>= pStyle->msStyleName;
854 OUString sParentStyleName;
855 if (rValues[1] >>= sParentStyleName)
857 // Find parent style.
858 ::std::vector<SharedPaneStyle>::const_iterator iStyle;
859 for (iStyle=mStyles.begin(); iStyle!=mStyles.end(); ++iStyle)
860 if ((*iStyle)->msStyleName == sParentStyleName)
862 pStyle->mpParentStyle = *iStyle;
863 break;
867 Reference<container::XHierarchicalNameAccess> xFontNode (rValues[2], UNO_QUERY);
868 pStyle->mpFont = ReadContext::ReadFont(
869 xFontNode, "", PresenterTheme::SharedFontDescriptor());
871 Reference<container::XNameAccess> xInnerBorderSizeNode (rValues[3], UNO_QUERY);
872 pStyle->maInnerBorderSize = ReadContext::ReadBorderSize(xInnerBorderSizeNode);
873 Reference<container::XNameAccess> xOuterBorderSizeNode (rValues[4], UNO_QUERY);
874 pStyle->maOuterBorderSize = ReadContext::ReadBorderSize(xOuterBorderSizeNode);
876 if (pStyle->mpParentStyle.get() != nullptr)
878 pStyle->maInnerBorderSize.Merge(pStyle->mpParentStyle->maInnerBorderSize);
879 pStyle->maOuterBorderSize.Merge(pStyle->mpParentStyle->maOuterBorderSize);
882 if (rReadContext.mxCanvas.is())
884 Reference<container::XNameAccess> xBitmapsNode (rValues[5], UNO_QUERY);
885 pStyle->mpBitmaps.reset(new PresenterBitmapContainer(
886 xBitmapsNode,
887 pStyle->mpParentStyle.get()!=nullptr
888 ? pStyle->mpParentStyle->mpBitmaps
889 : std::shared_ptr<PresenterBitmapContainer>(),
890 rReadContext.mxComponentContext,
891 rReadContext.mxCanvas,
892 rReadContext.mxPresenterHelper));
895 mStyles.push_back(pStyle);
898 SharedPaneStyle PaneStyleContainer::GetPaneStyle (const OUString& rsStyleName) const
900 ::std::vector<SharedPaneStyle>::const_iterator iEnd (mStyles.end());
901 for (::std::vector<SharedPaneStyle>::const_iterator iStyle=mStyles.begin(); iStyle!=iEnd; ++iStyle)
902 if ((*iStyle)->msStyleName == rsStyleName)
903 return *iStyle;
904 return SharedPaneStyle();
907 //===== PaneStyle =============================================================
909 PaneStyle::PaneStyle()
910 : msStyleName(),
911 mpParentStyle(),
912 mpFont(),
913 maInnerBorderSize(),
914 maOuterBorderSize(),
915 mpBitmaps()
919 const SharedBitmapDescriptor PaneStyle::GetBitmap (const OUString& rsBitmapName) const
921 if (mpBitmaps.get() != nullptr)
923 const SharedBitmapDescriptor pBitmap = mpBitmaps->GetBitmap(rsBitmapName);
924 if (pBitmap.get() != nullptr)
925 return pBitmap;
928 if (mpParentStyle.get() != nullptr)
929 return mpParentStyle->GetBitmap(rsBitmapName);
930 else
931 return SharedBitmapDescriptor();
934 PresenterTheme::SharedFontDescriptor PaneStyle::GetFont() const
936 if (mpFont.get() != nullptr)
937 return mpFont;
938 else if (mpParentStyle.get() != nullptr)
939 return mpParentStyle->GetFont();
940 else
941 return PresenterTheme::SharedFontDescriptor();
944 //===== ViewStyleContainer ====================================================
946 void ViewStyleContainer::Read (
947 const ReadContext& rReadContext,
948 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
950 Reference<container::XNameAccess> xViewStyleList (
951 PresenterConfigurationAccess::GetConfigurationNode(
952 rxThemeRoot,
953 "ViewStyles"),
954 UNO_QUERY);
955 if (xViewStyleList.is())
957 PresenterConfigurationAccess::ForAll(
958 xViewStyleList,
959 [this, &rReadContext] (OUString const&, uno::Reference<beans::XPropertySet> const& xProps)
961 return this->ProcessViewStyle(rReadContext, xProps);
966 void ViewStyleContainer::ProcessViewStyle(
967 ReadContext const & rReadContext,
968 const Reference<beans::XPropertySet>& rxProperties)
970 std::shared_ptr<ViewStyle> pStyle (new ViewStyle());
972 PresenterConfigurationAccess::GetProperty(rxProperties, "StyleName")
973 >>= pStyle->msStyleName;
975 OUString sParentStyleName;
976 if (PresenterConfigurationAccess::GetProperty(rxProperties, "ParentStyle")
977 >>= sParentStyleName)
979 // Find parent style.
980 ::std::vector<SharedViewStyle>::const_iterator iStyle;
981 for (iStyle=mStyles.begin(); iStyle!=mStyles.end(); ++iStyle)
982 if ((*iStyle)->msStyleName == sParentStyleName)
984 pStyle->mpParentStyle = *iStyle;
985 pStyle->mpFont = (*iStyle)->mpFont;
986 pStyle->mpBackground = (*iStyle)->mpBackground;
987 break;
991 const OUString sPathToFont; // empty string
992 Reference<container::XHierarchicalNameAccess> xFontNode (
993 PresenterConfigurationAccess::GetProperty(rxProperties, "Font"), UNO_QUERY);
994 PresenterTheme::SharedFontDescriptor pFont (
995 ReadContext::ReadFont(xFontNode, sPathToFont, PresenterTheme::SharedFontDescriptor()));
996 if (pFont.get() != nullptr)
997 pStyle->mpFont = pFont;
999 Reference<container::XHierarchicalNameAccess> xBackgroundNode (
1000 PresenterConfigurationAccess::GetProperty(rxProperties, "Background"),
1001 UNO_QUERY);
1002 SharedBitmapDescriptor pBackground (PresenterBitmapContainer::LoadBitmap(
1003 xBackgroundNode,
1004 OUString(),
1005 rReadContext.mxPresenterHelper,
1006 rReadContext.mxCanvas,
1007 SharedBitmapDescriptor()));
1008 if (pBackground.get() != nullptr && pBackground->GetNormalBitmap().is())
1009 pStyle->mpBackground = pBackground;
1011 mStyles.push_back(pStyle);
1014 SharedViewStyle ViewStyleContainer::GetViewStyle (const OUString& rsStyleName) const
1016 ::std::vector<SharedViewStyle>::const_iterator iEnd (mStyles.end());
1017 for (::std::vector<SharedViewStyle>::const_iterator iStyle=mStyles.begin(); iStyle!=iEnd; ++iStyle)
1018 if ((*iStyle)->msStyleName == rsStyleName)
1019 return *iStyle;
1020 return SharedViewStyle();
1023 //===== ViewStyle =============================================================
1025 ViewStyle::ViewStyle()
1026 : msStyleName(),
1027 mpParentStyle(),
1028 mpFont(),
1029 mpBackground()
1033 const SharedBitmapDescriptor ViewStyle::GetBitmap (const OUString& rsBitmapName) const
1035 if (rsBitmapName == "Background")
1036 return mpBackground;
1037 else
1038 return SharedBitmapDescriptor();
1041 PresenterTheme::SharedFontDescriptor ViewStyle::GetFont() const
1043 if (mpFont.get() != nullptr)
1044 return mpFont;
1045 else if (mpParentStyle.get() != nullptr)
1046 return mpParentStyle->GetFont();
1047 else
1048 return PresenterTheme::SharedFontDescriptor();
1051 //===== StyleAssociationContainer =============================================
1053 void StyleAssociationContainer::Read (
1054 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
1056 Reference<container::XNameAccess> xStyleAssociationList (
1057 PresenterConfigurationAccess::GetConfigurationNode(
1058 rxThemeRoot,
1059 "StyleAssociations"),
1060 UNO_QUERY);
1061 if (xStyleAssociationList.is())
1063 ::std::vector<OUString> aProperties (2);
1064 aProperties[0] = "ResourceURL";
1065 aProperties[1] = "StyleName";
1066 PresenterConfigurationAccess::ForAll(
1067 xStyleAssociationList,
1068 aProperties,
1069 [this] (std::vector<uno::Any> const& rValues)
1071 return this->ProcessStyleAssociation(rValues);
1076 OUString StyleAssociationContainer::GetStyleName (const OUString& rsResourceName) const
1078 StyleAssociations::const_iterator iAssociation (maStyleAssociations.find(rsResourceName));
1079 if (iAssociation != maStyleAssociations.end())
1080 return iAssociation->second;
1081 else
1082 return OUString();
1085 void StyleAssociationContainer::ProcessStyleAssociation(
1086 const ::std::vector<Any>& rValues)
1088 if (rValues.size() != 2)
1089 return;
1091 OUString sResourceURL;
1092 OUString sStyleName;
1093 if ((rValues[0] >>= sResourceURL)
1094 && (rValues[1] >>= sStyleName))
1096 maStyleAssociations[sResourceURL] = sStyleName;
1100 } // end of anonymous namespace
1102 } } // end of namespace ::sdext::presenter
1104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */