tdf#161411 - UI: Add Better wording for ASCII-only characters
[LibreOffice.git] / sd / source / console / PresenterTheme.cxx
blobef8cd5a3a29d49c1212b7f97484e17802725352e
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 <com/sun/star/drawing/XPresenterHelper.hpp>
25 #include <com/sun/star/rendering/PanoseWeight.hpp>
26 #include <osl/diagnose.h>
27 #include <map>
28 #include <numeric>
29 #include <utility>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
34 namespace sdext::presenter {
36 namespace {
38 class BorderSize
40 public:
41 const static sal_Int32 mnInvalidValue = -10000;
43 BorderSize() : mnLeft(mnInvalidValue),
44 mnTop(mnInvalidValue),
45 mnRight(mnInvalidValue),
46 mnBottom(mnInvalidValue) {}
48 sal_Int32 mnLeft;
49 sal_Int32 mnTop;
50 sal_Int32 mnRight;
51 sal_Int32 mnBottom;
53 std::vector<sal_Int32> ToVector()
55 return
57 mnLeft == mnInvalidValue ? 0 : mnLeft,
58 mnTop == mnInvalidValue ? 0 : mnTop,
59 mnRight == mnInvalidValue ? 0 : mnRight,
60 mnBottom == mnInvalidValue ? 0 : mnBottom
64 void Merge (const BorderSize& rBorderSize)
66 if (mnLeft == mnInvalidValue)
67 mnLeft = rBorderSize.mnLeft;
68 if (mnTop == mnInvalidValue)
69 mnTop = rBorderSize.mnTop;
70 if (mnRight == mnInvalidValue)
71 mnRight = rBorderSize.mnRight;
72 if (mnBottom == mnInvalidValue)
73 mnBottom = rBorderSize.mnBottom;
77 /** Reading a theme from the configurations is done in various classes. The
78 ReadContext gives access to frequently used objects and functions to make
79 the configuration handling easier.
81 class ReadContext
83 public:
84 Reference<XComponentContext> mxComponentContext;
85 Reference<rendering::XCanvas> mxCanvas;
86 Reference<drawing::XPresenterHelper> mxPresenterHelper;
88 ReadContext (
89 const Reference<XComponentContext>& rxContext,
90 const Reference<rendering::XCanvas>& rxCanvas);
92 /** Read data describing a font from the node that can be reached from
93 the given root via the given path.
94 @param rsFontPath
95 May be empty.
97 static PresenterTheme::SharedFontDescriptor ReadFont (
98 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxTheme,
99 const PresenterTheme::SharedFontDescriptor& rpDefault);
100 static PresenterTheme::SharedFontDescriptor ReadFont (
101 const Reference<beans::XPropertySet>& rxFontProperties,
102 const PresenterTheme::SharedFontDescriptor& rpDefault);
104 std::shared_ptr<PresenterTheme::Theme> ReadTheme (
105 PresenterConfigurationAccess& rConfiguration,
106 const OUString& rsThemeName);
108 static BorderSize ReadBorderSize (const Reference<container::XNameAccess>& rxNode);
110 private:
111 static Any GetByName (
112 const Reference<container::XNameAccess>& rxNode,
113 const OUString& rsName);
116 /** A PaneStyle describes how a pane is rendered.
118 class PaneStyle
120 public:
121 PaneStyle();
123 SharedBitmapDescriptor GetBitmap (const OUString& sBitmapName) const;
125 OUString msStyleName;
126 std::shared_ptr<PaneStyle> mpParentStyle;
127 PresenterTheme::SharedFontDescriptor mpFont;
128 BorderSize maInnerBorderSize;
129 BorderSize maOuterBorderSize;
130 std::shared_ptr<PresenterBitmapContainer> mpBitmaps;
132 PresenterTheme::SharedFontDescriptor GetFont() const;
135 typedef std::shared_ptr<PaneStyle> SharedPaneStyle;
137 class PaneStyleContainer
139 private:
140 ::std::vector<SharedPaneStyle> mStyles;
142 public:
143 void Read (
144 const ReadContext& rReadContext,
145 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
147 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
149 private:
150 void ProcessPaneStyle (
151 ReadContext const & rReadContext,
152 const ::std::vector<css::uno::Any>& rValues);
155 /** A ViewStyle describes how a view is displayed.
157 class ViewStyle
159 public:
160 ViewStyle();
162 SharedBitmapDescriptor GetBitmap (std::u16string_view sBitmapName) const;
164 PresenterTheme::SharedFontDescriptor GetFont() const;
166 OUString msStyleName;
167 std::shared_ptr<ViewStyle> mpParentStyle;
168 PresenterTheme::SharedFontDescriptor mpFont;
169 SharedBitmapDescriptor mpBackground;
172 typedef std::shared_ptr<ViewStyle> SharedViewStyle;
174 class ViewStyleContainer
176 private:
177 ::std::vector<SharedViewStyle> mStyles;
179 public:
180 void Read (
181 const ReadContext& rReadContext,
182 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
184 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
186 private:
187 void ProcessViewStyle(
188 ReadContext const & rReadContext,
189 const Reference<beans::XPropertySet>& rxProperties);
192 class StyleAssociationContainer
194 public:
195 void Read (
196 const Reference<container::XHierarchicalNameAccess>& rThemeRoot);
198 OUString GetStyleName (const OUString& rsResourceName) const;
200 private:
201 typedef std::map<OUString, OUString> StyleAssociations;
202 StyleAssociations maStyleAssociations;
204 void ProcessStyleAssociation(
205 const ::std::vector<css::uno::Any>& rValues);
208 } // end of anonymous namespace
210 class PresenterTheme::Theme
212 public:
213 Theme (
214 const Reference<container::XHierarchicalNameAccess>& rThemeRoot,
215 OUString sNodeName);
217 void Read (
218 PresenterConfigurationAccess& rConfiguration,
219 ReadContext& rReadContext);
221 OUString msConfigurationNodeName;
222 std::shared_ptr<Theme> mpParentTheme;
223 SharedBitmapDescriptor mpBackground;
224 PaneStyleContainer maPaneStyles;
225 ViewStyleContainer maViewStyles;
226 StyleAssociationContainer maStyleAssociations;
227 Reference<container::XHierarchicalNameAccess> mxThemeRoot;
228 std::shared_ptr<PresenterBitmapContainer> mpIconContainer;
229 typedef std::map<OUString,SharedFontDescriptor> FontContainer;
230 FontContainer maFontContainer;
232 SharedPaneStyle GetPaneStyle (const OUString& rsStyleName) const;
233 SharedViewStyle GetViewStyle (const OUString& rsStyleName) const;
235 private:
236 void ProcessFont(
237 const OUString& rsKey,
238 const Reference<beans::XPropertySet>& rxProperties);
241 //===== PresenterTheme ========================================================
243 PresenterTheme::PresenterTheme (
244 css::uno::Reference<css::uno::XComponentContext> xContext,
245 css::uno::Reference<css::rendering::XCanvas> xCanvas)
246 : mxContext(std::move(xContext)),
247 mxCanvas(std::move(xCanvas))
249 mpTheme = ReadTheme();
252 PresenterTheme::~PresenterTheme()
256 std::shared_ptr<PresenterTheme::Theme> PresenterTheme::ReadTheme()
258 ReadContext aReadContext(mxContext, mxCanvas);
260 PresenterConfigurationAccess aConfiguration (
261 mxContext,
262 u"/org.openoffice.Office.PresenterScreen/"_ustr,
263 PresenterConfigurationAccess::READ_ONLY);
265 return aReadContext.ReadTheme(aConfiguration, OUString());
268 bool PresenterTheme::HasCanvas() const
270 return mxCanvas.is();
273 void PresenterTheme::ProvideCanvas (const Reference<rendering::XCanvas>& rxCanvas)
275 if ( ! mxCanvas.is() && rxCanvas.is())
277 mxCanvas = rxCanvas;
278 ReadTheme();
282 OUString PresenterTheme::GetStyleName (const OUString& rsResourceURL) const
284 OUString sStyleName;
285 std::shared_ptr<Theme> pTheme (mpTheme);
286 while (sStyleName.isEmpty() && pTheme != nullptr)
288 sStyleName = pTheme->maStyleAssociations.GetStyleName(rsResourceURL);
289 pTheme = pTheme->mpParentTheme;
291 return sStyleName;
294 ::std::vector<sal_Int32> PresenterTheme::GetBorderSize (
295 const OUString& rsStyleName,
296 const bool bOuter) const
298 OSL_ASSERT(mpTheme != nullptr);
300 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
301 if (pPaneStyle)
302 if (bOuter)
303 return pPaneStyle->maOuterBorderSize.ToVector();
304 else
305 return pPaneStyle->maInnerBorderSize.ToVector();
306 else
308 return ::std::vector<sal_Int32>(4,0);
312 PresenterTheme::SharedFontDescriptor PresenterTheme::ReadFont (
313 const Reference<container::XHierarchicalNameAccess>& rxNode,
314 const PresenterTheme::SharedFontDescriptor& rpDefault)
316 return ReadContext::ReadFont(rxNode, rpDefault);
319 bool PresenterTheme::ConvertToColor (
320 const Any& rColorSequence,
321 sal_uInt32& rColor)
323 Sequence<sal_Int8> aByteSequence;
324 if (rColorSequence >>= aByteSequence)
326 rColor = std::accumulate(std::cbegin(aByteSequence), std::cend(aByteSequence), sal_uInt32(0),
327 [](const sal_uInt32 nRes, const sal_uInt8 nByte) { return (nRes << 8) | nByte; });
328 return true;
330 else
331 return false;
334 std::shared_ptr<PresenterConfigurationAccess> PresenterTheme::GetNodeForViewStyle (
335 const OUString& rsStyleName) const
337 if (mpTheme == nullptr)
338 return std::shared_ptr<PresenterConfigurationAccess>();
340 // Open configuration for writing.
341 auto pConfiguration = std::make_shared<PresenterConfigurationAccess>(
342 mxContext,
343 "/org.openoffice.Office.PresenterScreen/",
344 PresenterConfigurationAccess::READ_WRITE);
346 // Get configuration node for the view style container of the current
347 // theme.
348 if (pConfiguration->GoToChild( OUString(
349 "Presenter/Themes/" + mpTheme->msConfigurationNodeName + "/ViewStyles")))
351 pConfiguration->GoToChild(
352 [&rsStyleName] (OUString const&, uno::Reference<beans::XPropertySet> const& xProps)
354 return PresenterConfigurationAccess::IsStringPropertyEqual(
355 rsStyleName, u"StyleName"_ustr, xProps);
358 return pConfiguration;
361 SharedBitmapDescriptor PresenterTheme::GetBitmap (
362 const OUString& rsStyleName,
363 const OUString& rsBitmapName) const
365 if (mpTheme != nullptr)
367 if (rsStyleName.isEmpty())
369 if (rsBitmapName == "Background")
371 std::shared_ptr<Theme> pTheme (mpTheme);
372 while (pTheme != nullptr && !pTheme->mpBackground)
373 pTheme = pTheme->mpParentTheme;
374 if (pTheme != nullptr)
375 return pTheme->mpBackground;
376 else
377 return SharedBitmapDescriptor();
380 else
382 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
383 if (pPaneStyle)
385 SharedBitmapDescriptor pBitmap (pPaneStyle->GetBitmap(rsBitmapName));
386 if (pBitmap)
387 return pBitmap;
390 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
391 if (pViewStyle)
393 SharedBitmapDescriptor pBitmap (pViewStyle->GetBitmap(rsBitmapName));
394 if (pBitmap)
395 return pBitmap;
400 return SharedBitmapDescriptor();
403 SharedBitmapDescriptor PresenterTheme::GetBitmap (
404 const OUString& rsBitmapName) const
406 if (mpTheme != nullptr)
408 if (rsBitmapName == "Background")
410 std::shared_ptr<Theme> pTheme (mpTheme);
411 while (pTheme != nullptr && !pTheme->mpBackground)
412 pTheme = pTheme->mpParentTheme;
413 if (pTheme != nullptr)
414 return pTheme->mpBackground;
415 else
416 return SharedBitmapDescriptor();
418 else
420 if (mpTheme->mpIconContainer != nullptr)
421 return mpTheme->mpIconContainer->GetBitmap(rsBitmapName);
425 return SharedBitmapDescriptor();
428 std::shared_ptr<PresenterBitmapContainer> PresenterTheme::GetBitmapContainer() const
430 if (mpTheme != nullptr)
431 return mpTheme->mpIconContainer;
432 else
433 return std::shared_ptr<PresenterBitmapContainer>();
436 PresenterTheme::SharedFontDescriptor PresenterTheme::GetFont (
437 const OUString& rsStyleName) const
439 if (mpTheme != nullptr)
441 SharedPaneStyle pPaneStyle (mpTheme->GetPaneStyle(rsStyleName));
442 if (pPaneStyle)
443 return pPaneStyle->GetFont();
445 SharedViewStyle pViewStyle (mpTheme->GetViewStyle(rsStyleName));
446 if (pViewStyle)
447 return pViewStyle->GetFont();
449 std::shared_ptr<Theme> pTheme (mpTheme);
450 while (pTheme != nullptr)
452 Theme::FontContainer::const_iterator iFont (pTheme->maFontContainer.find(rsStyleName));
453 if (iFont != pTheme->maFontContainer.end())
454 return iFont->second;
456 pTheme = pTheme->mpParentTheme;
460 return SharedFontDescriptor();
463 //===== FontDescriptor ========================================================
465 PresenterTheme::FontDescriptor::FontDescriptor (
466 const std::shared_ptr<FontDescriptor>& rpDescriptor)
467 : mnSize(12),
468 mnColor(0x00000000),
469 msAnchor(u"Left"_ustr),
470 mnXOffset(0),
471 mnYOffset(0)
473 if (rpDescriptor != nullptr)
475 msFamilyName = rpDescriptor->msFamilyName;
476 msStyleName = rpDescriptor->msStyleName;
477 mnSize = rpDescriptor->mnSize;
478 mnColor = rpDescriptor->mnColor;
479 msAnchor = rpDescriptor->msAnchor;
480 mnXOffset = rpDescriptor->mnXOffset;
481 mnYOffset = rpDescriptor->mnYOffset;
485 bool PresenterTheme::FontDescriptor::PrepareFont (
486 const Reference<rendering::XCanvas>& rxCanvas)
488 if (mxFont.is())
489 return true;
491 if ( ! rxCanvas.is())
492 return false;
494 const double nCellSize (GetCellSizeForDesignSize(rxCanvas, mnSize));
495 mxFont = CreateFont(rxCanvas, nCellSize);
497 return mxFont.is();
500 Reference<rendering::XCanvasFont> PresenterTheme::FontDescriptor::CreateFont (
501 const Reference<rendering::XCanvas>& rxCanvas,
502 const double nCellSize) const
504 rendering::FontRequest aFontRequest;
505 aFontRequest.FontDescription.FamilyName = msFamilyName;
506 if (msFamilyName.isEmpty())
507 aFontRequest.FontDescription.FamilyName = "Tahoma";
508 aFontRequest.FontDescription.StyleName = msStyleName;
509 aFontRequest.CellSize = nCellSize;
511 // Make an attempt at translating the style name(s)into a corresponding
512 // font description.
513 if (msStyleName == "Bold")
514 aFontRequest.FontDescription.FontDescription.Weight = rendering::PanoseWeight::HEAVY;
516 return rxCanvas->createFont(
517 aFontRequest,
518 Sequence<beans::PropertyValue>(),
519 geometry::Matrix2D(1,0,0,1));
522 double PresenterTheme::FontDescriptor::GetCellSizeForDesignSize (
523 const Reference<rendering::XCanvas>& rxCanvas,
524 const double nDesignSize) const
526 // Use the given design size as initial value in calculating the cell
527 // size.
528 double nCellSize (nDesignSize);
530 if ( ! rxCanvas.is())
532 // We need the canvas to do the conversion. Return the design size,
533 // it is the our best guess in this circumstance.
534 return nDesignSize;
537 Reference<rendering::XCanvasFont> xFont (CreateFont(rxCanvas, nCellSize));
538 if ( ! xFont.is())
539 return nDesignSize;
541 geometry::RealRectangle2D aBox (PresenterCanvasHelper::GetTextBoundingBox (xFont, u"X"_ustr));
543 const double nAscent (-aBox.Y1);
544 //tdf#112408
545 if (nAscent == 0)
546 return nDesignSize;
547 const double nDescent (aBox.Y2);
548 const double nScale = (nAscent+nDescent) / nAscent;
549 return nDesignSize * nScale;
552 //===== Theme =================================================================
554 PresenterTheme::Theme::Theme (
555 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot,
556 OUString sNodeName)
557 : msConfigurationNodeName(std::move(sNodeName)),
558 maPaneStyles(),
559 maViewStyles(),
560 maStyleAssociations(),
561 mxThemeRoot(rxThemeRoot)
565 void PresenterTheme::Theme::Read (
566 PresenterConfigurationAccess& rConfiguration,
567 ReadContext& rReadContext)
569 // Parent theme name.
570 OUString sParentThemeName;
571 if ((PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, u"ParentTheme"_ustr)
572 >>= sParentThemeName)
573 && !sParentThemeName.isEmpty())
575 mpParentTheme = rReadContext.ReadTheme(rConfiguration, sParentThemeName);
578 // Background.
579 mpBackground = PresenterBitmapContainer::LoadBitmap(
580 mxThemeRoot,
581 u"Background"_ustr,
582 rReadContext.mxPresenterHelper,
583 rReadContext.mxCanvas,
584 SharedBitmapDescriptor());
586 // Style associations.
587 maStyleAssociations.Read(mxThemeRoot);
589 // Pane styles.
590 maPaneStyles.Read(rReadContext, mxThemeRoot);
592 // View styles.
593 maViewStyles.Read(rReadContext, mxThemeRoot);
595 // Read bitmaps.
596 mpIconContainer = std::make_shared<PresenterBitmapContainer>(
597 Reference<container::XNameAccess>(
598 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, u"Bitmaps"_ustr), UNO_QUERY),
599 mpParentTheme != nullptr ? mpParentTheme->mpIconContainer
600 : std::shared_ptr<PresenterBitmapContainer>(),
601 rReadContext.mxComponentContext, rReadContext.mxCanvas);
603 // Read fonts.
604 Reference<container::XNameAccess> xFontNode(
605 PresenterConfigurationAccess::GetConfigurationNode(mxThemeRoot, u"Fonts"_ustr),
606 UNO_QUERY);
607 PresenterConfigurationAccess::ForAll(
608 xFontNode,
609 [this] (OUString const& rKey, uno::Reference<beans::XPropertySet> const& xProps)
611 return this->ProcessFont(rKey, xProps);
615 SharedPaneStyle PresenterTheme::Theme::GetPaneStyle (const OUString& rsStyleName) const
617 SharedPaneStyle pPaneStyle (maPaneStyles.GetPaneStyle(rsStyleName));
618 if (pPaneStyle)
619 return pPaneStyle;
620 else if (mpParentTheme != nullptr)
621 return mpParentTheme->GetPaneStyle(rsStyleName);
622 else
623 return SharedPaneStyle();
626 SharedViewStyle PresenterTheme::Theme::GetViewStyle (const OUString& rsStyleName) const
628 SharedViewStyle pViewStyle (maViewStyles.GetViewStyle(rsStyleName));
629 if (pViewStyle)
630 return pViewStyle;
631 else if (mpParentTheme != nullptr)
632 return mpParentTheme->GetViewStyle(rsStyleName);
633 else
634 return SharedViewStyle();
637 void PresenterTheme::Theme::ProcessFont(
638 const OUString& rsKey,
639 const Reference<beans::XPropertySet>& rxProperties)
641 maFontContainer[rsKey] = ReadContext::ReadFont(rxProperties, SharedFontDescriptor());
644 namespace {
646 //===== ReadContext ===========================================================
648 ReadContext::ReadContext (
649 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
650 const Reference<rendering::XCanvas>& rxCanvas)
651 : mxComponentContext(rxContext),
652 mxCanvas(rxCanvas)
654 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
655 if (xFactory.is())
657 mxPresenterHelper.set(
658 xFactory->createInstanceWithContext(
659 u"com.sun.star.comp.Draw.PresenterHelper"_ustr,
660 rxContext),
661 UNO_QUERY_THROW);
665 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
666 const Reference<container::XHierarchicalNameAccess>& rxNode,
667 const PresenterTheme::SharedFontDescriptor& rpDefault)
669 if ( ! rxNode.is())
670 return PresenterTheme::SharedFontDescriptor();
674 Reference<container::XHierarchicalNameAccess> xFont (
675 PresenterConfigurationAccess::GetConfigurationNode(
676 rxNode,
677 /*rsFontPath*/u""_ustr),
678 UNO_QUERY_THROW);
680 Reference<beans::XPropertySet> xProperties (xFont, UNO_QUERY_THROW);
681 return ReadFont(xProperties, rpDefault);
683 catch (Exception&)
685 OSL_ASSERT(false);
688 return PresenterTheme::SharedFontDescriptor();
691 PresenterTheme::SharedFontDescriptor ReadContext::ReadFont (
692 const Reference<beans::XPropertySet>& rxProperties,
693 const PresenterTheme::SharedFontDescriptor& rpDefault)
695 auto pDescriptor = std::make_shared<PresenterTheme::FontDescriptor>(rpDefault);
697 PresenterConfigurationAccess::GetProperty(rxProperties, u"FamilyName"_ustr) >>= pDescriptor->msFamilyName;
698 PresenterConfigurationAccess::GetProperty(rxProperties, u"Style"_ustr) >>= pDescriptor->msStyleName;
699 PresenterConfigurationAccess::GetProperty(rxProperties, u"Size"_ustr) >>= pDescriptor->mnSize;
700 PresenterTheme::ConvertToColor(
701 PresenterConfigurationAccess::GetProperty(rxProperties, u"Color"_ustr),
702 pDescriptor->mnColor);
703 PresenterConfigurationAccess::GetProperty(rxProperties, u"Anchor"_ustr) >>= pDescriptor->msAnchor;
704 PresenterConfigurationAccess::GetProperty(rxProperties, u"XOffset"_ustr) >>= pDescriptor->mnXOffset;
705 PresenterConfigurationAccess::GetProperty(rxProperties, u"YOffset"_ustr) >>= pDescriptor->mnYOffset;
707 return pDescriptor;
710 Any ReadContext::GetByName (
711 const Reference<container::XNameAccess>& rxNode,
712 const OUString& rsName)
714 OSL_ASSERT(rxNode.is());
715 if (rxNode->hasByName(rsName))
716 return rxNode->getByName(rsName);
717 else
718 return Any();
721 std::shared_ptr<PresenterTheme::Theme> ReadContext::ReadTheme (
722 PresenterConfigurationAccess& rConfiguration,
723 const OUString& rsThemeName)
725 std::shared_ptr<PresenterTheme::Theme> pTheme;
727 OUString sCurrentThemeName (rsThemeName);
728 if (sCurrentThemeName.isEmpty())
730 // No theme name given. Look up the CurrentTheme property.
731 rConfiguration.GetConfigurationNode(u"Presenter/CurrentTheme"_ustr) >>= sCurrentThemeName;
732 if (sCurrentThemeName.isEmpty())
734 // Still no name. Use "DefaultTheme".
735 sCurrentThemeName = "DefaultTheme";
739 Reference<container::XNameAccess> xThemes (
740 rConfiguration.GetConfigurationNode(u"Presenter/Themes"_ustr),
741 UNO_QUERY);
742 if (xThemes.is())
744 // Iterate over all themes and search the one with the given name.
745 const Sequence<OUString> aKeys (xThemes->getElementNames());
746 for (const OUString& rsKey : aKeys)
748 Reference<container::XHierarchicalNameAccess> xTheme (
749 xThemes->getByName(rsKey), UNO_QUERY);
750 if (xTheme.is())
752 OUString sThemeName;
753 PresenterConfigurationAccess::GetConfigurationNode(xTheme, u"ThemeName"_ustr)
754 >>= sThemeName;
755 if (sThemeName == sCurrentThemeName)
757 pTheme = std::make_shared<PresenterTheme::Theme>(xTheme,rsKey);
758 break;
764 if (pTheme != nullptr)
766 pTheme->Read(rConfiguration, *this);
769 return pTheme;
772 BorderSize ReadContext::ReadBorderSize (const Reference<container::XNameAccess>& rxNode)
774 BorderSize aBorderSize;
776 if (rxNode.is())
778 GetByName(rxNode, u"Left"_ustr) >>= aBorderSize.mnLeft;
779 GetByName(rxNode, u"Top"_ustr) >>= aBorderSize.mnTop;
780 GetByName(rxNode, u"Right"_ustr) >>= aBorderSize.mnRight;
781 GetByName(rxNode, u"Bottom"_ustr) >>= aBorderSize.mnBottom;
784 return aBorderSize;
787 //===== PaneStyleContainer ====================================================
789 void PaneStyleContainer::Read (
790 const ReadContext& rReadContext,
791 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
793 Reference<container::XNameAccess> xPaneStyleList (
794 PresenterConfigurationAccess::GetConfigurationNode(
795 rxThemeRoot,
796 u"PaneStyles"_ustr),
797 UNO_QUERY);
798 if (!xPaneStyleList.is())
799 return;
801 ::std::vector<OUString> aProperties;
802 aProperties.reserve(6);
803 aProperties.emplace_back("StyleName");
804 aProperties.emplace_back("ParentStyle");
805 aProperties.emplace_back("TitleFont");
806 aProperties.emplace_back("InnerBorderSize");
807 aProperties.emplace_back("OuterBorderSize");
808 aProperties.emplace_back("BorderBitmapList");
809 PresenterConfigurationAccess::ForAll(
810 xPaneStyleList,
811 aProperties,
812 [this, &rReadContext] (std::vector<uno::Any> const& rValues)
814 return this->ProcessPaneStyle(rReadContext, rValues);
818 void PaneStyleContainer::ProcessPaneStyle(
819 ReadContext const & rReadContext,
820 const ::std::vector<Any>& rValues)
822 if (rValues.size() != 6)
823 return;
825 auto pStyle = std::make_shared<PaneStyle>();
827 rValues[0] >>= pStyle->msStyleName;
829 OUString sParentStyleName;
830 if (rValues[1] >>= sParentStyleName)
832 // Find parent style.
833 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
834 [&sParentStyleName](const SharedPaneStyle& rxStyle) { return rxStyle->msStyleName == sParentStyleName; });
835 if (iStyle != mStyles.end())
836 pStyle->mpParentStyle = *iStyle;
839 Reference<container::XHierarchicalNameAccess> xFontNode (rValues[2], UNO_QUERY);
840 pStyle->mpFont = ReadContext::ReadFont(
841 xFontNode, PresenterTheme::SharedFontDescriptor());
843 Reference<container::XNameAccess> xInnerBorderSizeNode (rValues[3], UNO_QUERY);
844 pStyle->maInnerBorderSize = ReadContext::ReadBorderSize(xInnerBorderSizeNode);
845 Reference<container::XNameAccess> xOuterBorderSizeNode (rValues[4], UNO_QUERY);
846 pStyle->maOuterBorderSize = ReadContext::ReadBorderSize(xOuterBorderSizeNode);
848 if (pStyle->mpParentStyle != nullptr)
850 pStyle->maInnerBorderSize.Merge(pStyle->mpParentStyle->maInnerBorderSize);
851 pStyle->maOuterBorderSize.Merge(pStyle->mpParentStyle->maOuterBorderSize);
854 if (rReadContext.mxCanvas.is())
856 Reference<container::XNameAccess> xBitmapsNode (rValues[5], UNO_QUERY);
857 pStyle->mpBitmaps = std::make_shared<PresenterBitmapContainer>(
858 xBitmapsNode,
859 pStyle->mpParentStyle != nullptr ? pStyle->mpParentStyle->mpBitmaps
860 : std::shared_ptr<PresenterBitmapContainer>(),
861 rReadContext.mxComponentContext, rReadContext.mxCanvas,
862 rReadContext.mxPresenterHelper);
865 mStyles.push_back(pStyle);
868 SharedPaneStyle PaneStyleContainer::GetPaneStyle (const OUString& rsStyleName) const
870 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
871 [&rsStyleName](const SharedPaneStyle& rxStyle) { return rxStyle->msStyleName == rsStyleName; });
872 if (iStyle != mStyles.end())
873 return *iStyle;
874 return SharedPaneStyle();
877 //===== PaneStyle =============================================================
879 PaneStyle::PaneStyle()
883 SharedBitmapDescriptor PaneStyle::GetBitmap (const OUString& rsBitmapName) const
885 if (mpBitmaps != nullptr)
887 SharedBitmapDescriptor pBitmap = mpBitmaps->GetBitmap(rsBitmapName);
888 if (pBitmap)
889 return pBitmap;
892 if (mpParentStyle != nullptr)
893 return mpParentStyle->GetBitmap(rsBitmapName);
894 else
895 return SharedBitmapDescriptor();
898 PresenterTheme::SharedFontDescriptor PaneStyle::GetFont() const
900 if (mpFont)
901 return mpFont;
902 else if (mpParentStyle != nullptr)
903 return mpParentStyle->GetFont();
904 else
905 return PresenterTheme::SharedFontDescriptor();
908 //===== ViewStyleContainer ====================================================
910 void ViewStyleContainer::Read (
911 const ReadContext& rReadContext,
912 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
914 Reference<container::XNameAccess> xViewStyleList (
915 PresenterConfigurationAccess::GetConfigurationNode(
916 rxThemeRoot,
917 u"ViewStyles"_ustr),
918 UNO_QUERY);
919 if (xViewStyleList.is())
921 PresenterConfigurationAccess::ForAll(
922 xViewStyleList,
923 [this, &rReadContext] (OUString const&, uno::Reference<beans::XPropertySet> const& xProps)
925 return this->ProcessViewStyle(rReadContext, xProps);
930 void ViewStyleContainer::ProcessViewStyle(
931 ReadContext const & rReadContext,
932 const Reference<beans::XPropertySet>& rxProperties)
934 auto pStyle = std::make_shared<ViewStyle>();
936 PresenterConfigurationAccess::GetProperty(rxProperties, u"StyleName"_ustr)
937 >>= pStyle->msStyleName;
939 OUString sParentStyleName;
940 if (PresenterConfigurationAccess::GetProperty(rxProperties, u"ParentStyle"_ustr)
941 >>= sParentStyleName)
943 // Find parent style.
944 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
945 [&sParentStyleName](const SharedViewStyle& rxStyle) { return rxStyle->msStyleName == sParentStyleName; });
946 if (iStyle != mStyles.end())
948 pStyle->mpParentStyle = *iStyle;
949 pStyle->mpFont = (*iStyle)->mpFont;
950 pStyle->mpBackground = (*iStyle)->mpBackground;
954 Reference<container::XHierarchicalNameAccess> xFontNode (
955 PresenterConfigurationAccess::GetProperty(rxProperties, u"Font"_ustr), UNO_QUERY);
956 PresenterTheme::SharedFontDescriptor pFont (
957 ReadContext::ReadFont(xFontNode, PresenterTheme::SharedFontDescriptor()));
958 if (pFont)
959 pStyle->mpFont = std::move(pFont);
961 Reference<container::XHierarchicalNameAccess> xBackgroundNode (
962 PresenterConfigurationAccess::GetProperty(rxProperties, u"Background"_ustr),
963 UNO_QUERY);
964 SharedBitmapDescriptor pBackground (PresenterBitmapContainer::LoadBitmap(
965 xBackgroundNode,
966 OUString(),
967 rReadContext.mxPresenterHelper,
968 rReadContext.mxCanvas,
969 SharedBitmapDescriptor()));
970 if (pBackground && pBackground->GetNormalBitmap().is())
971 pStyle->mpBackground = std::move(pBackground);
973 mStyles.push_back(pStyle);
976 SharedViewStyle ViewStyleContainer::GetViewStyle (const OUString& rsStyleName) const
978 auto iStyle = std::find_if(mStyles.begin(), mStyles.end(),
979 [&rsStyleName](const SharedViewStyle& rxStyle) { return rxStyle->msStyleName == rsStyleName; });
980 if (iStyle != mStyles.end())
981 return *iStyle;
982 return SharedViewStyle();
985 //===== ViewStyle =============================================================
987 ViewStyle::ViewStyle()
991 SharedBitmapDescriptor ViewStyle::GetBitmap (std::u16string_view rsBitmapName) const
993 if (rsBitmapName == u"Background")
994 return mpBackground;
995 else
996 return SharedBitmapDescriptor();
999 PresenterTheme::SharedFontDescriptor ViewStyle::GetFont() const
1001 if (mpFont)
1002 return mpFont;
1003 else if (mpParentStyle != nullptr)
1004 return mpParentStyle->GetFont();
1005 else
1006 return PresenterTheme::SharedFontDescriptor();
1009 //===== StyleAssociationContainer =============================================
1011 void StyleAssociationContainer::Read (
1012 const Reference<container::XHierarchicalNameAccess>& rxThemeRoot)
1014 Reference<container::XNameAccess> xStyleAssociationList (
1015 PresenterConfigurationAccess::GetConfigurationNode(
1016 rxThemeRoot,
1017 u"StyleAssociations"_ustr),
1018 UNO_QUERY);
1019 if (!xStyleAssociationList.is())
1020 return;
1022 ::std::vector<OUString> aProperties { u"ResourceURL"_ustr, u"StyleName"_ustr };
1023 PresenterConfigurationAccess::ForAll(
1024 xStyleAssociationList,
1025 aProperties,
1026 [this] (std::vector<uno::Any> const& rValues)
1028 return this->ProcessStyleAssociation(rValues);
1032 OUString StyleAssociationContainer::GetStyleName (const OUString& rsResourceName) const
1034 StyleAssociations::const_iterator iAssociation (maStyleAssociations.find(rsResourceName));
1035 if (iAssociation != maStyleAssociations.end())
1036 return iAssociation->second;
1037 else
1038 return OUString();
1041 void StyleAssociationContainer::ProcessStyleAssociation(
1042 const ::std::vector<Any>& rValues)
1044 if (rValues.size() != 2)
1045 return;
1047 OUString sResourceURL;
1048 OUString sStyleName;
1049 if ((rValues[0] >>= sResourceURL)
1050 && (rValues[1] >>= sStyleName))
1052 maStyleAssociations[sResourceURL] = sStyleName;
1056 } // end of anonymous namespace
1058 } // end of namespace ::sdext::presenter
1060 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */