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/.
11 #include <extlstcontext.hxx>
12 #include <worksheethelper.hxx>
13 #include <oox/core/contexthandler.hxx>
14 #include <oox/helper/attributelist.hxx>
15 #include <oox/token/namespaces.hxx>
16 #include <oox/token/tokens.hxx>
17 #include <colorscale.hxx>
18 #include <condformatbuffer.hxx>
19 #include <condformatcontext.hxx>
20 #include <document.hxx>
21 #include <worksheetfragment.hxx>
22 #include <workbookfragment.hxx>
23 #include <stylesbuffer.hxx>
24 #include <stylesfragment.hxx>
26 #include <rangeutl.hxx>
27 #include <sal/log.hxx>
29 using ::oox::core::ContextHandlerRef
;
30 using ::oox::xls::CondFormatBuffer
;
32 sal_Int32 rStyleIdx
= 0;
37 ExtCfRuleContext::ExtCfRuleContext( WorksheetContextBase
& rFragment
, ScDataBarFormatData
* pTarget
):
38 WorksheetContextBase( rFragment
),
44 ContextHandlerRef
ExtCfRuleContext::onCreateContext( sal_Int32
, const AttributeList
& )
49 void ExtCfRuleContext::onStartElement( const AttributeList
& rAttribs
)
51 switch( getCurrentElement() )
53 case XLS14_TOKEN( dataBar
):
55 ExtCfDataBarRuleRef xRule
= getCondFormats().createExtCfDataBarRule(mpTarget
);
56 xRule
->importDataBar( rAttribs
);
59 case XLS14_TOKEN( negativeFillColor
):
61 ExtCfDataBarRuleRef xRule
= getCondFormats().createExtCfDataBarRule(mpTarget
);
62 xRule
->importNegativeFillColor( rAttribs
);
65 case XLS14_TOKEN( axisColor
):
67 ExtCfDataBarRuleRef xRule
= getCondFormats().createExtCfDataBarRule(mpTarget
);
68 xRule
->importAxisColor( rAttribs
);
71 case XLS14_TOKEN( cfvo
):
73 ExtCfDataBarRuleRef xRule
= getCondFormats().createExtCfDataBarRule(mpTarget
);
74 xRule
->importCfvo( rAttribs
);
75 xRule
->getModel().mbIsLower
= mbFirstEntry
;
84 ExtConditionalFormattingContext::ExtConditionalFormattingContext(WorksheetContextBase
& rFragment
)
85 : WorksheetContextBase(rFragment
)
87 , eOperator(ScConditionMode::NONE
)
88 , isPreviousElementF(false)
92 ContextHandlerRef
ExtConditionalFormattingContext::onCreateContext(sal_Int32 nElement
, const AttributeList
& rAttribs
)
96 ScFormatEntry
& rFormat
= **maEntries
.rbegin();
97 assert(rFormat
.GetType() == ScFormatEntry::Type::Iconset
);
98 ScIconSetFormat
& rIconSet
= static_cast<ScIconSetFormat
&>(rFormat
);
99 ScDocument
* pDoc
= &getScDocument();
100 SCTAB nTab
= getSheetIndex();
101 ScAddress
aPos(0, 0, nTab
);
102 mpCurrentRule
->SetData(&rIconSet
, pDoc
, aPos
);
103 mpCurrentRule
.reset();
105 if (nElement
== XLS14_TOKEN(cfRule
))
107 OUString aType
= rAttribs
.getString(XML_type
, OUString());
108 OUString aId
= rAttribs
.getString(XML_id
, OUString());
109 nPriority
= rAttribs
.getInteger( XML_priority
, -1 );
110 maPriorities
.push_back(nPriority
);
112 if (aType
== "dataBar")
114 // an ext entry does not need to have an existing corresponding entry
115 ExtLst::const_iterator aExt
= getExtLst().find( aId
);
116 if(aExt
== getExtLst().end())
119 ScDataBarFormatData
* pInfo
= aExt
->second
;
124 return new ExtCfRuleContext( *this, pInfo
);
126 else if (aType
== "iconSet")
128 ScDocument
* pDoc
= &getScDocument();
129 mpCurrentRule
.reset(new IconSetRule(*this));
130 maEntries
.push_back(std::make_unique
<ScIconSetFormat
>(pDoc
));
131 return new IconSetContext(*this, mpCurrentRule
.get());
133 else if (aType
== "cellIs")
135 sal_Int32 aToken
= rAttribs
.getToken( XML_operator
, XML_TOKEN_INVALID
);
136 eOperator
= CondFormatBuffer::convertToInternalOperator(aToken
);
141 SAL_WARN("sc", "unhandled XLS14_TOKEN(cfRule) with type: " << aType
);
144 else if (nElement
== XLS14_TOKEN( dxf
))
146 return new DxfContext( *this, getStyles().createExtDxf() );
148 else if (nElement
== XM_TOKEN( sqref
) || nElement
== XM_TOKEN( f
))
156 void ExtConditionalFormattingContext::onStartElement(const AttributeList
& /*Attribs*/)
160 void ExtConditionalFormattingContext::onCharacters(const OUString
& rCharacters
)
162 switch (getCurrentElement())
166 aChars
= rCharacters
;
167 isPreviousElementF
= true;
170 case XM_TOKEN(sqref
):
172 aChars
= rCharacters
;
179 void ExtConditionalFormattingContext::onEndElement()
181 switch (getCurrentElement())
185 rFormulas
.push_back(aChars
);
188 case XLS14_TOKEN( cfRule
):
190 getStyles().getExtDxfs().forEachMem( &Dxf::finalizeImport
);
193 case XM_TOKEN(sqref
):
196 ScDocument
* pDoc
= &getScDocument();
197 bool bSuccess
= ScRangeStringConverter::GetRangeListFromString(aRange
, aChars
, pDoc
, formula::FormulaGrammar::CONV_XL_OOX
);
198 if (!bSuccess
|| aRange
.empty())
201 SCTAB nTab
= getSheetIndex();
202 for (size_t i
= 0; i
< aRange
.size(); ++i
)
204 aRange
[i
].aStart
.SetTab(nTab
);
205 aRange
[i
].aEnd
.SetTab(nTab
);
208 if (isPreviousElementF
) // sqref can be alone in some cases.
210 for (const OUString
& rFormula
: rFormulas
)
212 ScAddress rPos
= aRange
.GetTopLeftCorner();
213 rStyle
= getStyles().createExtDxfStyle(rStyleIdx
);
214 ScCondFormatEntry
* pEntry
= new ScCondFormatEntry(eOperator
, rFormula
, "", pDoc
,
215 rPos
, rStyle
, "", "",
216 formula::FormulaGrammar::GRAM_OOXML
,
217 formula::FormulaGrammar::GRAM_OOXML
,
218 ScFormatEntry::Type::ExtCondition
);
219 maEntries
.push_back(std::unique_ptr
<ScFormatEntry
>(pEntry
));
223 assert(rFormulas
.size() == maPriorities
.size());
227 std::vector
< std::unique_ptr
<ExtCfCondFormat
> >& rExtFormats
= getCondFormats().importExtCondFormat();
228 rExtFormats
.push_back(std::make_unique
<ExtCfCondFormat
>(aRange
, maEntries
, &maPriorities
));
230 maPriorities
.clear();
231 isPreviousElementF
= false;
239 ExtLstLocalContext::ExtLstLocalContext( WorksheetContextBase
& rFragment
, ScDataBarFormatData
* pTarget
):
240 WorksheetContextBase(rFragment
),
245 ContextHandlerRef
ExtLstLocalContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& )
247 switch( getCurrentElement() )
249 case XLS_TOKEN( extLst
):
250 if(nElement
== XLS_TOKEN( ext
))
255 case XLS_TOKEN( ext
):
256 if (nElement
== XLS14_TOKEN( id
))
264 void ExtLstLocalContext::onStartElement( const AttributeList
& )
266 switch( getCurrentElement() )
268 case XLS14_TOKEN( id
):
273 void ExtLstLocalContext::onCharacters( const OUString
& rChars
)
275 if (getCurrentElement() == XLS14_TOKEN( id
))
277 getExtLst().insert( std::pair
< OUString
, ScDataBarFormatData
*>(rChars
, mpTarget
) );
281 ExtGlobalContext::ExtGlobalContext( WorksheetContextBase
& rFragment
):
282 WorksheetContextBase(rFragment
)
286 ContextHandlerRef
ExtGlobalContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& /*rAttribs*/ )
290 case XLS14_TOKEN(conditionalFormatting
): return new ExtConditionalFormattingContext(*this);
291 case XLS14_TOKEN(dataValidations
): return new ExtDataValidationsContext(*this);
296 void ExtGlobalContext::onStartElement( const AttributeList
& /*rAttribs*/ )
300 ExtLstGlobalContext::ExtLstGlobalContext( WorksheetFragment
& rFragment
):
301 WorksheetContextBase(rFragment
)
305 ContextHandlerRef
ExtLstGlobalContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& )
307 if (nElement
== XLS_TOKEN( ext
))
308 return new ExtGlobalContext( *this );
313 ExtGlobalWorkbookContext::ExtGlobalWorkbookContext( WorkbookContextBase
& rFragment
):
314 WorkbookContextBase(rFragment
)
318 ContextHandlerRef
ExtGlobalWorkbookContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& rAttribs
)
320 if (nElement
== LOEXT_TOKEN(extCalcPr
))
322 ScDocument
* pDoc
= &getScDocument();
323 sal_Int32 nToken
= rAttribs
.getToken( XML_stringRefSyntax
, XML_CalcA1
);
324 ScCalcConfig aCalcConfig
= pDoc
->GetCalcConfig();
329 aCalcConfig
.SetStringRefSyntax( formula::FormulaGrammar::CONV_OOO
);
332 aCalcConfig
.SetStringRefSyntax( formula::FormulaGrammar::CONV_XL_A1
);
335 aCalcConfig
.SetStringRefSyntax( formula::FormulaGrammar::CONV_XL_R1C1
);
337 case XML_CalcA1ExcelA1
:
338 aCalcConfig
.SetStringRefSyntax( formula::FormulaGrammar::CONV_A1_XL_A1
);
341 aCalcConfig
.SetStringRefSyntax( formula::FormulaGrammar::CONV_UNSPECIFIED
);
344 pDoc
->SetCalcConfig(aCalcConfig
);
350 void ExtGlobalWorkbookContext::onStartElement( const AttributeList
& /*rAttribs*/ )
354 ExtLstGlobalWorkbookContext::ExtLstGlobalWorkbookContext( WorkbookFragment
& rFragment
):
355 WorkbookContextBase(rFragment
)
359 ContextHandlerRef
ExtLstGlobalWorkbookContext::onCreateContext( sal_Int32 nElement
, const AttributeList
& )
361 if (nElement
== XLS_TOKEN( ext
))
362 return new ExtGlobalWorkbookContext( *this );
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */