1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <XMLThemeContext.hxx>
12 #include <xmloff/xmlnamespace.hxx>
13 #include <xmloff/xmltoken.hxx>
14 #include <xmloff/xmlimp.hxx>
16 #include <com/sun/star/beans/XPropertySet.hpp>
18 #include <sax/tools/converter.hxx>
20 #include <docmodel/uno/UnoTheme.hxx>
21 #include <docmodel/theme/Theme.hxx>
24 using namespace xmloff::token
;
26 XMLThemeContext::XMLThemeContext(
27 SvXMLImport
& rImport
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
28 css::uno::Reference
<css::uno::XInterface
> const& xObjectWithThemeProperty
)
29 : SvXMLImportContext(rImport
)
30 , m_xObjectWithThemeProperty(xObjectWithThemeProperty
)
31 , mpTheme(new model::Theme
)
33 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
35 switch (rAttribute
.getToken())
37 case XML_ELEMENT(LO_EXT
, XML_NAME
):
39 OUString aName
= rAttribute
.toString();
40 mpTheme
->SetName(aName
);
47 XMLThemeContext::~XMLThemeContext()
49 if (mpTheme
&& mpTheme
->getColorSet())
51 uno::Reference
<beans::XPropertySet
> xPropertySet(m_xObjectWithThemeProperty
,
53 auto xTheme
= model::theme::createXTheme(mpTheme
);
54 xPropertySet
->setPropertyValue(u
"Theme"_ustr
, uno::Any(xTheme
));
58 uno::Reference
<xml::sax::XFastContextHandler
> SAL_CALL
XMLThemeContext::createFastChildContext(
59 sal_Int32 nElement
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttribs
)
61 if (nElement
== XML_ELEMENT(LO_EXT
, XML_THEME_COLORS
))
63 return new XMLThemeColorsContext(GetImport(), xAttribs
, *mpTheme
);
69 XMLThemeColorsContext::XMLThemeColorsContext(
70 SvXMLImport
& rImport
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
72 : SvXMLImportContext(rImport
)
75 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
77 switch (rAttribute
.getToken())
79 case XML_ELEMENT(LO_EXT
, XML_NAME
):
81 OUString aName
= rAttribute
.toString();
82 m_pColorSet
.reset(new model::ColorSet(aName
));
89 XMLThemeColorsContext::~XMLThemeColorsContext()
92 mrTheme
.setColorSet(m_pColorSet
);
95 uno::Reference
<xml::sax::XFastContextHandler
>
96 SAL_CALL
XMLThemeColorsContext::createFastChildContext(
97 sal_Int32 nElement
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttribs
)
99 if (nElement
== XML_ELEMENT(LO_EXT
, XML_COLOR
))
102 return new XMLColorContext(GetImport(), xAttribs
, m_pColorSet
);
108 XMLColorContext::XMLColorContext(SvXMLImport
& rImport
,
109 const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
110 std::shared_ptr
<model::ColorSet
>& rpColorSet
)
111 : SvXMLImportContext(rImport
)
116 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
118 switch (rAttribute
.getToken())
120 case XML_ELEMENT(LO_EXT
, XML_NAME
):
122 aName
= rAttribute
.toString();
125 case XML_ELEMENT(LO_EXT
, XML_COLOR
):
127 sax::Converter::convertColor(aColor
, rAttribute
.toView());
133 if (!aName
.isEmpty())
135 auto eType
= model::ThemeColorType::Unknown
;
136 if (aName
== u
"dark1")
137 eType
= model::ThemeColorType::Dark1
;
138 else if (aName
== u
"light1")
139 eType
= model::ThemeColorType::Light1
;
140 else if (aName
== u
"dark2")
141 eType
= model::ThemeColorType::Dark2
;
142 else if (aName
== u
"light2")
143 eType
= model::ThemeColorType::Light2
;
144 else if (aName
== u
"accent1")
145 eType
= model::ThemeColorType::Accent1
;
146 else if (aName
== u
"accent2")
147 eType
= model::ThemeColorType::Accent2
;
148 else if (aName
== u
"accent3")
149 eType
= model::ThemeColorType::Accent3
;
150 else if (aName
== u
"accent4")
151 eType
= model::ThemeColorType::Accent4
;
152 else if (aName
== u
"accent5")
153 eType
= model::ThemeColorType::Accent5
;
154 else if (aName
== u
"accent6")
155 eType
= model::ThemeColorType::Accent6
;
156 else if (aName
== u
"hyperlink")
157 eType
= model::ThemeColorType::Hyperlink
;
158 else if (aName
== u
"followed-hyperlink")
159 eType
= model::ThemeColorType::FollowedHyperlink
;
161 if (eType
!= model::ThemeColorType::Unknown
)
163 rpColorSet
->add(eType
, aColor
);
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */