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 <comphelper/lok.hxx>
12 #include <docmodel/theme/ColorSet.hxx>
14 #include <editeng/colritem.hxx>
15 #include <editeng/editeng.hxx>
16 #include <editeng/eeitem.hxx>
17 #include <editeng/section.hxx>
19 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
21 #include <sal/config.h>
23 #include <sfx2/lokhelper.hxx>
25 #include <svx/PaletteManager.hxx>
26 #include <svx/svdmodel.hxx>
27 #include <svx/svdotext.hxx>
28 #include <svx/svdundo.hxx>
29 #include <svx/theme/ThemeColorChangerCommon.hxx>
30 #include <svx/theme/ThemeColorPaletteManager.hxx>
31 #include <svx/xdef.hxx>
32 #include <svx/xlnclit.hxx>
33 #include <svx/xflclit.hxx>
34 #include <tools/json_writer.hxx>
42 const SvxColorItem
* getColorItem(const editeng::Section
& rSection
)
44 auto iterator
= std::find_if(
45 rSection
.maAttributes
.begin(), rSection
.maAttributes
.end(),
46 [](const SfxPoolItem
* pPoolItem
) { return pPoolItem
->Which() == EE_CHAR_COLOR
; });
48 if (iterator
!= rSection
.maAttributes
.end())
49 return static_cast<const SvxColorItem
*>(*iterator
);
53 bool updateEditEngTextSections(model::ColorSet
const& rColorSet
, SdrObject
* pObject
, SdrView
& rView
)
55 SdrTextObj
* pTextObject
= DynCastSdrTextObj(pObject
);
60 rView
.SdrBeginTextEdit(pTextObject
);
62 auto* pOutlinerView
= rView
.GetTextEditOutlinerView();
66 auto& rEditEngine
= pOutlinerView
->GetEditView().getEditEngine();
68 OutlinerParaObject
* pOutlinerParagraphObject
= pTextObject
->GetOutlinerParaObject();
69 if (pOutlinerParagraphObject
)
71 const EditTextObject
& rEditText
= pOutlinerParagraphObject
->GetTextObject();
72 std::vector
<editeng::Section
> aSections
;
73 rEditText
.GetAllSections(aSections
);
75 for (editeng::Section
const& rSection
: aSections
)
77 const SvxColorItem
* pItem
= getColorItem(rSection
);
81 model::ComplexColor
const& rComplexColor
= pItem
->getComplexColor();
82 if (rComplexColor
.isValidThemeType())
84 SfxItemSet
aSet(rEditEngine
.GetAttribs(rSection
.mnParagraph
, rSection
.mnStart
,
86 GetAttribsFlags::CHARATTRIBS
));
87 Color aNewColor
= rColorSet
.resolveColor(rComplexColor
);
88 std::unique_ptr
<SvxColorItem
> pNewItem(pItem
->Clone());
89 pNewItem
->setColor(aNewColor
);
92 ESelection
aSelection(rSection
.mnParagraph
, rSection
.mnStart
, rSection
.mnParagraph
,
94 rEditEngine
.QuickSetAttribs(aSet
, aSelection
);
99 rView
.SdrEndTextEdit();
104 bool updateObjectAttributes(model::ColorSet
const& rColorSet
, SdrObject
& rObject
,
105 SfxUndoManager
* pUndoManager
)
107 bool bChanged
= false;
109 auto aItemSet
= rObject
.GetMergedItemSet();
111 if (const XFillColorItem
* pItem
= aItemSet
.GetItemIfSet(XATTR_FILLCOLOR
, false))
113 model::ComplexColor
const& rComplexColor
= pItem
->getComplexColor();
114 if (rComplexColor
.isValidThemeType())
116 Color aNewColor
= rColorSet
.resolveColor(rComplexColor
);
117 std::unique_ptr
<XFillColorItem
> pNewItem(pItem
->Clone());
118 pNewItem
->SetColorValue(aNewColor
);
119 aItemSet
.Put(*pNewItem
);
123 if (const XLineColorItem
* pItem
= aItemSet
.GetItemIfSet(XATTR_LINECOLOR
, false))
125 model::ComplexColor
const& rComplexColor
= pItem
->getComplexColor();
126 if (rComplexColor
.isValidThemeType())
128 Color aNewColor
= rColorSet
.resolveColor(rComplexColor
);
129 std::unique_ptr
<XLineColorItem
> pNewItem(pItem
->Clone());
130 pNewItem
->SetColorValue(aNewColor
);
131 aItemSet
.Put(*pNewItem
);
135 if (const SvxColorItem
* pItem
= aItemSet
.GetItemIfSet(EE_CHAR_COLOR
, false))
137 model::ComplexColor
const& rComplexColor
= pItem
->getComplexColor();
138 if (rComplexColor
.isValidThemeType())
140 Color aNewColor
= rColorSet
.resolveColor(rComplexColor
);
141 std::unique_ptr
<SvxColorItem
> pNewItem(pItem
->Clone());
142 pNewItem
->setColor(aNewColor
);
143 aItemSet
.Put(*pNewItem
);
149 const bool bUndo
= pUndoManager
&& pUndoManager
->IsInListAction();
152 pUndoManager
->AddUndoAction(
153 rObject
.getSdrModelFromSdrObject().GetSdrUndoFactory().CreateUndoAttrObject(
156 rObject
.SetMergedItemSetAndBroadcast(aItemSet
);
161 } // end anonymous namespace
163 /// Updates properties of the SdrObject
164 void updateSdrObject(model::ColorSet
const& rColorSet
, SdrObject
* pObject
, SdrView
* pView
,
165 SfxUndoManager
* pUndoManager
)
169 updateObjectAttributes(rColorSet
, *pObject
, pUndoManager
);
171 updateEditEngTextSections(rColorSet
, pObject
, *pView
);
174 void notifyLOK(std::shared_ptr
<model::ColorSet
> const& pColorSet
,
175 const std::set
<Color
>& rDocumentColors
)
177 if (comphelper::LibreOfficeKit::isActive())
179 svx::ThemeColorPaletteManager
aManager(pColorSet
);
180 tools::JsonWriter aTree
;
183 aManager
.generateJSON(aTree
);
184 if (rDocumentColors
.size())
185 PaletteManager::generateJSON(aTree
, rDocumentColors
);
187 SfxLokHelper::notifyAllViews(LOK_CALLBACK_COLOR_PALETTES
, aTree
.finishAndGetAsOString());
191 } // end svx::theme namespace
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */