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/maptype.hxx>
13 #include <xmloff/xmlnamespace.hxx>
14 #include <xmloff/xmltoken.hxx>
15 #include <xmloff/xmlprcon.hxx>
16 #include <xmloff/xmlerror.hxx>
17 #include <xmloff/namespacemap.hxx>
18 #include <xmloff/xmlimp.hxx>
19 #include <xmloff/xmlement.hxx>
20 #include <xmloff/xmlprhdl.hxx>
22 #include <sal/log.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <sax/tools/converter.hxx>
26 #include <comphelper/sequence.hxx>
28 #include <docmodel/uno/UnoTheme.hxx>
29 #include <docmodel/theme/Theme.hxx>
32 using namespace xmloff::token
;
34 XMLThemeContext::XMLThemeContext(SvXMLImport
& rImport
,
35 const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
36 css::uno::Reference
<css::drawing::XDrawPage
> const& xPage
)
37 : SvXMLImportContext(rImport
)
39 , mpTheme(new model::Theme
)
41 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
43 switch (rAttribute
.getToken())
45 case XML_ELEMENT(LO_EXT
, XML_NAME
):
47 OUString aName
= rAttribute
.toString();
48 mpTheme
->SetName(aName
);
55 XMLThemeContext::~XMLThemeContext()
57 if (mpTheme
&& mpTheme
->getColorSet())
59 uno::Reference
<beans::XPropertySet
> xPropertySet(m_xPage
, uno::UNO_QUERY
);
60 auto xTheme
= model::theme::createXTheme(mpTheme
);
61 xPropertySet
->setPropertyValue("Theme", uno::Any(xTheme
));
65 uno::Reference
<xml::sax::XFastContextHandler
> SAL_CALL
XMLThemeContext::createFastChildContext(
66 sal_Int32 nElement
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttribs
)
68 if (nElement
== XML_ELEMENT(LO_EXT
, XML_THEME_COLORS
))
70 return new XMLThemeColorsContext(GetImport(), xAttribs
, *mpTheme
);
76 XMLThemeColorsContext::XMLThemeColorsContext(
77 SvXMLImport
& rImport
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
79 : SvXMLImportContext(rImport
)
82 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
84 switch (rAttribute
.getToken())
86 case XML_ELEMENT(LO_EXT
, XML_NAME
):
88 OUString aName
= rAttribute
.toString();
89 m_pColorSet
.reset(new model::ColorSet(aName
));
96 XMLThemeColorsContext::~XMLThemeColorsContext()
99 mrTheme
.setColorSet(m_pColorSet
);
102 uno::Reference
<xml::sax::XFastContextHandler
>
103 SAL_CALL
XMLThemeColorsContext::createFastChildContext(
104 sal_Int32 nElement
, const uno::Reference
<xml::sax::XFastAttributeList
>& xAttribs
)
106 if (nElement
== XML_ELEMENT(LO_EXT
, XML_COLOR
))
109 return new XMLColorContext(GetImport(), xAttribs
, m_pColorSet
);
115 XMLColorContext::XMLColorContext(SvXMLImport
& rImport
,
116 const uno::Reference
<xml::sax::XFastAttributeList
>& xAttrList
,
117 std::shared_ptr
<model::ColorSet
>& rpColorSet
)
118 : SvXMLImportContext(rImport
)
123 for (const auto& rAttribute
: sax_fastparser::castToFastAttributeList(xAttrList
))
125 switch (rAttribute
.getToken())
127 case XML_ELEMENT(LO_EXT
, XML_NAME
):
129 aName
= rAttribute
.toString();
132 case XML_ELEMENT(LO_EXT
, XML_COLOR
):
134 sax::Converter::convertColor(aColor
, rAttribute
.toView());
140 if (!aName
.isEmpty())
142 auto eType
= model::ThemeColorType::Unknown
;
143 if (aName
== u
"dark1")
144 eType
= model::ThemeColorType::Dark1
;
145 else if (aName
== u
"light1")
146 eType
= model::ThemeColorType::Light1
;
147 else if (aName
== u
"dark2")
148 eType
= model::ThemeColorType::Dark2
;
149 else if (aName
== u
"light2")
150 eType
= model::ThemeColorType::Light2
;
151 else if (aName
== u
"accent1")
152 eType
= model::ThemeColorType::Accent1
;
153 else if (aName
== u
"accent2")
154 eType
= model::ThemeColorType::Accent2
;
155 else if (aName
== u
"accent3")
156 eType
= model::ThemeColorType::Accent3
;
157 else if (aName
== u
"accent4")
158 eType
= model::ThemeColorType::Accent4
;
159 else if (aName
== u
"accent5")
160 eType
= model::ThemeColorType::Accent5
;
161 else if (aName
== u
"accent6")
162 eType
= model::ThemeColorType::Accent6
;
163 else if (aName
== u
"hyperlink")
164 eType
= model::ThemeColorType::Hyperlink
;
165 else if (aName
== u
"followed-hyperlink")
166 eType
= model::ThemeColorType::FollowedHyperlink
;
168 if (eType
!= model::ThemeColorType::Unknown
)
170 rpColorSet
->add(eType
, aColor
);
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */