nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / filter / xml / xmlstyli.cxx
blob29d97922ca317b0d0354421fa41f4f6d5a4a6e0d
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 "xmlstyli.hxx"
21 #include <xmloff/namespacemap.hxx>
22 #include <xmloff/xmlnamespace.hxx>
23 #include <xmloff/xmlimppr.hxx>
24 #include <xmloff/families.hxx>
25 #include <xmloff/xmlnumfi.hxx>
26 #include <xmloff/XMLGraphicsDefaultStyle.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/table/BorderLine2.hpp>
30 #include <comphelper/extract.hxx>
31 #include <xmloff/xmlprcon.hxx>
32 #include "XMLTableHeaderFooterContext.hxx"
33 #include "XMLConverter.hxx"
34 #include "XMLTableShapeImportHelper.hxx"
35 #include <sheetdata.hxx>
36 #include "xmlannoi.hxx"
37 #include <textuno.hxx>
38 #include <cellsuno.hxx>
39 #include "xmlstyle.hxx"
41 #include <docuno.hxx>
42 #include <unonames.hxx>
43 #include <document.hxx>
44 #include <conditio.hxx>
45 #include <rangelst.hxx>
47 #define XML_LINE_LEFT 0
48 #define XML_LINE_RIGHT 1
49 #define XML_LINE_TOP 2
50 #define XML_LINE_BOTTOM 3
52 #define XML_LINE_TLBR 0
53 #define XML_LINE_BLTR 1
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::xml::sax;
57 using namespace ::com::sun::star::style;
58 using namespace ::com::sun::star::beans;
59 using namespace ::com::sun::star::container;
60 using namespace xmloff::token;
61 using namespace ::formula;
63 using com::sun::star::uno::UNO_QUERY;
64 ScXMLCellImportPropertyMapper::ScXMLCellImportPropertyMapper(
65 const rtl::Reference< XMLPropertySetMapper >& rMapper,
66 SvXMLImport& rImportP) :
67 SvXMLImportPropertyMapper( rMapper, rImportP )
71 ScXMLCellImportPropertyMapper::~ScXMLCellImportPropertyMapper()
75 void ScXMLCellImportPropertyMapper::finished(::std::vector< XMLPropertyState >& rProperties, sal_Int32 nStartIndex, sal_Int32 nEndIndex ) const
77 static const sal_Int16 aPaddingCTF[4] = { CTF_SC_LEFTPADDING, CTF_SC_RIGHTPADDING,
78 CTF_SC_TOPPADDING, CTF_SC_BOTTOMPADDING };
79 static const sal_Int16 aBorderCTF[4] = { CTF_SC_LEFTBORDER, CTF_SC_RIGHTBORDER,
80 CTF_SC_TOPBORDER, CTF_SC_BOTTOMBORDER };
82 SvXMLImportPropertyMapper::finished(rProperties, nStartIndex, nEndIndex);
83 XMLPropertyState* pAllPaddingProperty(nullptr);
84 XMLPropertyState* pPadding[4] = { nullptr, nullptr, nullptr, nullptr };
85 XMLPropertyState* pNewPadding[4] = { nullptr, nullptr, nullptr, nullptr };
86 XMLPropertyState* pAllBorderProperty = nullptr;
87 XMLPropertyState* pBorders[4] = { nullptr, nullptr, nullptr, nullptr };
88 XMLPropertyState* pNewBorders[4] = { nullptr, nullptr, nullptr, nullptr };
89 XMLPropertyState* pAllBorderWidthProperty = nullptr;
90 XMLPropertyState* pBorderWidths[4] = { nullptr, nullptr, nullptr, nullptr };
91 XMLPropertyState* pDiagBorders[2] = { nullptr };
92 XMLPropertyState* pOldDiagBorderWidths[2] = { nullptr }; // old attribute names without "s"
93 XMLPropertyState* pDiagBorderWidths[2] = { nullptr };
95 for (auto& rProperty : rProperties)
97 XMLPropertyState*property = &rProperty;
98 if (property->mnIndex != -1)
100 sal_Int16 nContextID = getPropertySetMapper()->GetEntryContextId(property->mnIndex);
101 switch (nContextID)
103 case CTF_SC_ALLPADDING : pAllPaddingProperty = property; break;
104 case CTF_SC_LEFTPADDING : pPadding[XML_LINE_LEFT] = property; break;
105 case CTF_SC_RIGHTPADDING : pPadding[XML_LINE_RIGHT] = property; break;
106 case CTF_SC_TOPPADDING : pPadding[XML_LINE_TOP] = property; break;
107 case CTF_SC_BOTTOMPADDING : pPadding[XML_LINE_BOTTOM] = property; break;
108 case CTF_SC_ALLBORDER : pAllBorderProperty = property; break;
109 case CTF_SC_LEFTBORDER : pBorders[XML_LINE_LEFT] = property; break;
110 case CTF_SC_RIGHTBORDER : pBorders[XML_LINE_RIGHT] = property; break;
111 case CTF_SC_TOPBORDER : pBorders[XML_LINE_TOP] = property; break;
112 case CTF_SC_BOTTOMBORDER : pBorders[XML_LINE_BOTTOM] = property; break;
113 case CTF_SC_ALLBORDERWIDTH : pAllBorderWidthProperty = property; break;
114 case CTF_SC_LEFTBORDERWIDTH : pBorderWidths[XML_LINE_LEFT] = property; break;
115 case CTF_SC_RIGHTBORDERWIDTH : pBorderWidths[XML_LINE_RIGHT] = property; break;
116 case CTF_SC_TOPBORDERWIDTH : pBorderWidths[XML_LINE_TOP] = property; break;
117 case CTF_SC_BOTTOMBORDERWIDTH : pBorderWidths[XML_LINE_BOTTOM] = property; break;
118 case CTF_SC_DIAGONALTLBR : pDiagBorders[XML_LINE_TLBR] = property; break;
119 case CTF_SC_DIAGONALBLTR : pDiagBorders[XML_LINE_BLTR] = property; break;
120 case CTF_SC_DIAGONALTLBRWIDTH : pOldDiagBorderWidths[XML_LINE_TLBR] = property; break;
121 case CTF_SC_DIAGONALTLBRWIDTHS : pDiagBorderWidths[XML_LINE_TLBR] = property; break;
122 case CTF_SC_DIAGONALBLTRWIDTH : pOldDiagBorderWidths[XML_LINE_BLTR] = property; break;
123 case CTF_SC_DIAGONALBLTRWIDTHS : pDiagBorderWidths[XML_LINE_BLTR] = property; break;
127 sal_uInt16 i;
129 // #i27594#; copy Value, but don't insert
130 if (pAllBorderWidthProperty)
131 pAllBorderWidthProperty->mnIndex = -1;
132 if (pAllBorderProperty)
133 pAllBorderProperty->mnIndex = -1;
134 if (pAllPaddingProperty)
135 pAllPaddingProperty->mnIndex = -1;
137 for (i = 0; i < 4; ++i)
139 if (pAllPaddingProperty && !pPadding[i])
140 pNewPadding[i] = new XMLPropertyState(maPropMapper->FindEntryIndex(aPaddingCTF[i]), pAllPaddingProperty->maValue);
141 if (pAllBorderProperty && !pBorders[i])
143 pNewBorders[i] = new XMLPropertyState(maPropMapper->FindEntryIndex(aBorderCTF[i]), pAllBorderProperty->maValue);
144 pBorders[i] = pNewBorders[i];
146 if( !pBorderWidths[i] )
147 pBorderWidths[i] = pAllBorderWidthProperty;
148 else
149 pBorderWidths[i]->mnIndex = -1;
150 if( pBorders[i] )
152 table::BorderLine2 aBorderLine;
153 pBorders[i]->maValue >>= aBorderLine;
154 if( pBorderWidths[i] )
156 // Merge style:border-line-width values to fo:border values. Do
157 // not override fo:border line width or line style with an
158 // empty value!
159 table::BorderLine2 aBorderLineWidth;
160 pBorderWidths[i]->maValue >>= aBorderLineWidth;
161 aBorderLine.OuterLineWidth = aBorderLineWidth.OuterLineWidth;
162 aBorderLine.InnerLineWidth = aBorderLineWidth.InnerLineWidth;
163 aBorderLine.LineDistance = aBorderLineWidth.LineDistance;
164 pBorders[i]->maValue <<= aBorderLine;
168 for( i = 0; i < 2; ++i )
170 if( pDiagBorders[i] && ( pDiagBorderWidths[i] || pOldDiagBorderWidths[i] ) )
172 table::BorderLine2 aBorderLine;
173 pDiagBorders[i]->maValue >>= aBorderLine;
174 table::BorderLine2 aBorderLineWidth;
175 if (pDiagBorderWidths[i])
176 pDiagBorderWidths[i]->maValue >>= aBorderLineWidth; // prefer new attribute
177 else
178 pOldDiagBorderWidths[i]->maValue >>= aBorderLineWidth;
179 aBorderLine.OuterLineWidth = aBorderLineWidth.OuterLineWidth;
180 aBorderLine.InnerLineWidth = aBorderLineWidth.InnerLineWidth;
181 aBorderLine.LineDistance = aBorderLineWidth.LineDistance;
182 pDiagBorders[i]->maValue <<= aBorderLine;
183 if (pDiagBorderWidths[i])
184 pDiagBorderWidths[i]->mnIndex = -1;
185 if (pOldDiagBorderWidths[i])
186 pOldDiagBorderWidths[i]->mnIndex = -1; // reset mnIndex for old and new attribute if both are present
190 for (i = 0; i < 4; ++i)
192 if (pNewPadding[i])
194 rProperties.push_back(*pNewPadding[i]);
195 delete pNewPadding[i];
197 if (pNewBorders[i])
199 rProperties.push_back(*pNewBorders[i]);
200 delete pNewBorders[i];
205 ScXMLRowImportPropertyMapper::ScXMLRowImportPropertyMapper(
206 const rtl::Reference< XMLPropertySetMapper >& rMapper,
207 SvXMLImport& rImportP) :
208 SvXMLImportPropertyMapper( rMapper, rImportP )
212 ScXMLRowImportPropertyMapper::~ScXMLRowImportPropertyMapper()
216 void ScXMLRowImportPropertyMapper::finished(::std::vector< XMLPropertyState >& rProperties, sal_Int32 nStartIndex, sal_Int32 nEndIndex ) const
218 SvXMLImportPropertyMapper::finished(rProperties, nStartIndex, nEndIndex);
219 XMLPropertyState* pHeight(nullptr);
220 XMLPropertyState* pOptimalHeight(nullptr);
221 XMLPropertyState* pPageBreak(nullptr);
222 for (auto& rProperty : rProperties)
224 XMLPropertyState* property = &rProperty;
225 if (property->mnIndex != -1)
227 sal_Int16 nContextID = getPropertySetMapper()->GetEntryContextId(property->mnIndex);
228 switch (nContextID)
230 case CTF_SC_ROWHEIGHT : pHeight = property; break;
231 case CTF_SC_ROWOPTIMALHEIGHT : pOptimalHeight = property; break;
232 case CTF_SC_ROWBREAKBEFORE : pPageBreak = property; break;
236 if (pPageBreak)
238 if(!(::cppu::any2bool(pPageBreak->maValue)))
239 pPageBreak->mnIndex = -1;
241 if (pOptimalHeight)
243 if (::cppu::any2bool(pOptimalHeight->maValue))
245 if (pHeight)
247 // set the stored height, but keep "optimal" flag:
248 // pass the height value as OptimalHeight property (only allowed while loading!)
249 pOptimalHeight->maValue = pHeight->maValue;
250 pHeight->mnIndex = -1;
252 else
253 pOptimalHeight->mnIndex = -1;
256 else if (pHeight)
258 rProperties.emplace_back(maPropMapper->FindEntryIndex(CTF_SC_ROWOPTIMALHEIGHT), css::uno::Any(false));
260 // don't access pointers to rProperties elements after push_back!
263 namespace {
265 class XMLTableCellPropsContext : public SvXMLPropertySetContext
267 using SvXMLPropertySetContext::CreateChildContext;
268 public:
269 XMLTableCellPropsContext(
270 SvXMLImport& rImport, sal_Int32 nElement,
271 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
272 sal_uInt32 nFamily,
273 ::std::vector< XMLPropertyState > &rProps,
274 const rtl::Reference < SvXMLImportPropertyMapper > &rMap);
276 using SvXMLPropertySetContext::createFastChildContext;
277 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > createFastChildContext(
278 sal_Int32 nElement,
279 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList,
280 ::std::vector< XMLPropertyState > &rProperties,
281 const XMLPropertyState& rProp ) override;
286 XMLTableCellPropsContext::XMLTableCellPropsContext(
287 SvXMLImport& rImport, sal_Int32 nElement,
288 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList,
289 sal_uInt32 nFamily,
290 ::std::vector< XMLPropertyState > &rProps,
291 const rtl::Reference < SvXMLImportPropertyMapper > &rMap)
292 : SvXMLPropertySetContext( rImport, nElement, xAttrList, nFamily,
293 rProps, rMap )
297 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableCellPropsContext::createFastChildContext(
298 sal_Int32 nElement,
299 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList,
300 ::std::vector< XMLPropertyState > &rProperties,
301 const XMLPropertyState& rProp )
303 // no need for a custom context or indeed a SvXMLTokenMap to grab just the
304 // single attribute ( href ) that we are interested in.
305 // still though, we will check namespaces etc.
306 if (nElement == XML_ELEMENT(STYLE, XML_HYPERLINK) ||
307 nElement == XML_ELEMENT(LO_EXT, XML_HYPERLINK) )
309 OUString sURL;
310 for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
312 if ( aIter.getToken() == XML_ELEMENT(XLINK, XML_HREF) )
313 sURL = aIter.toString();
314 else
315 XMLOFF_WARN_UNKNOWN("sc", aIter);
317 if ( !sURL.isEmpty() )
319 XMLPropertyState aProp( rProp );
320 aProp.maValue <<= sURL;
321 rProperties.push_back( aProp );
324 return SvXMLPropertySetContext::createFastChildContext( nElement, xAttrList, rProperties, rProp );
327 namespace {
329 class ScXMLMapContext : public SvXMLImportContext
331 OUString msApplyStyle;
332 OUString msCondition;
333 OUString msBaseCell;
335 ScXMLImport& GetScImport() { return static_cast<ScXMLImport&>(GetImport()); }
336 public:
338 ScXMLMapContext(
339 SvXMLImport& rImport, sal_Int32 nElement,
340 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList );
342 ScCondFormatEntry* CreateConditionEntry();
347 ScXMLMapContext::ScXMLMapContext(SvXMLImport& rImport, sal_Int32 /*nElement*/,
348 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
349 : SvXMLImportContext( rImport )
351 for( auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ) )
353 OUString sValue = aIter.toString();
354 switch (aIter.getToken())
356 case XML_ELEMENT(STYLE, XML_CONDITION):
357 msCondition = sValue;
358 break;
359 case XML_ELEMENT(STYLE, XML_APPLY_STYLE_NAME):
360 msApplyStyle = GetImport().GetStyleDisplayName( XmlStyleFamily::TABLE_CELL, sValue);
361 break;
362 case XML_ELEMENT(STYLE, XML_BASE_CELL_ADDRESS):
363 msBaseCell = sValue;
364 break;
365 default:
366 XMLOFF_WARN_UNKNOWN("sc", aIter);
371 ScCondFormatEntry* ScXMLMapContext::CreateConditionEntry()
373 OUString aCondition, aConditionNmsp;
374 FormulaGrammar::Grammar eGrammar = FormulaGrammar::GRAM_UNSPECIFIED;
375 GetScImport().ExtractFormulaNamespaceGrammar( aCondition, aConditionNmsp, eGrammar, msCondition );
376 bool bHasNmsp = aCondition.getLength() < msCondition.getLength();
378 // parse a condition from the attribute string
379 ScXMLConditionParseResult aParseResult;
380 ScXMLConditionHelper::parseCondition( aParseResult, aCondition, 0 );
382 if( !bHasNmsp )
384 // the attribute does not contain a namespace: try to find a namespace of an external grammar
385 FormulaGrammar::Grammar eNewGrammar = FormulaGrammar::GRAM_UNSPECIFIED;
386 GetScImport().ExtractFormulaNamespaceGrammar( aCondition, aConditionNmsp, eNewGrammar, aCondition, true );
387 if( eNewGrammar != FormulaGrammar::GRAM_EXTERNAL )
388 eGrammar = eNewGrammar;
391 ScConditionMode eMode = ScConditionEntry::GetModeFromApi(aParseResult.meOperator);
392 ScDocument* pDoc = GetScImport().GetDocument();
394 ScCondFormatEntry* pEntry = new ScCondFormatEntry(eMode, aParseResult.maOperand1, aParseResult.maOperand2, *pDoc, ScAddress(), msApplyStyle,
395 OUString(), OUString(), eGrammar, eGrammar);
397 pEntry->SetSrcString(msBaseCell);
398 return pEntry;
401 void XMLTableStyleContext::SetAttribute( sal_uInt16 nPrefixKey,
402 const OUString& rLocalName,
403 const OUString& rValue )
405 // TODO: use a map here
406 if( IsXMLToken(rLocalName, XML_DATA_STYLE_NAME ) )
407 sDataStyleName = rValue;
408 else if ( IsXMLToken(rLocalName, XML_MASTER_PAGE_NAME ) )
409 sPageStyle = rValue;
410 else
411 XMLPropStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
415 XMLTableStyleContext::XMLTableStyleContext( ScXMLImport& rImport,
416 SvXMLStylesContext& rStyles, XmlStyleFamily nFamily, bool bDefaultStyle ) :
417 XMLPropStyleContext( rImport, rStyles, nFamily, bDefaultStyle ),
418 sDataStyleName(),
419 pStyles(&rStyles),
420 nNumberFormat(-1),
421 nLastSheet(-1),
422 bParentSet(false),
423 mpCondFormat(nullptr),
424 mbDeleteCondFormat(true)
428 XMLTableStyleContext::~XMLTableStyleContext()
430 if(mbDeleteCondFormat)
431 delete mpCondFormat;
434 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTableStyleContext::createFastChildContext(
435 sal_Int32 nElement,
436 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
438 css::uno::Reference< css::xml::sax::XFastContextHandler > xContext;
440 if( nElement == XML_ELEMENT(STYLE, XML_MAP) )
442 if(!mpCondFormat)
443 mpCondFormat = new ScConditionalFormat( 0, GetScImport().GetDocument() );
444 ScXMLMapContext* pMapContext = new ScXMLMapContext(GetImport(), nElement, xAttrList);
445 xContext = pMapContext;
446 mpCondFormat->AddEntry(pMapContext->CreateConditionEntry());
448 else if ( nElement == XML_ELEMENT(STYLE, XML_TABLE_CELL_PROPERTIES) )
450 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
451 GetStyles()->GetImportPropertyMapper(
452 GetFamily() );
453 if( xImpPrMap.is() )
454 xContext = new XMLTableCellPropsContext( GetImport(), nElement,
455 xAttrList,
456 XML_TYPE_PROP_TABLE_CELL,
457 GetProperties(),
458 xImpPrMap );
461 if (!xContext)
462 xContext = XMLPropStyleContext::createFastChildContext( nElement, xAttrList );
463 return xContext;
466 void XMLTableStyleContext::ApplyCondFormat( const uno::Sequence<table::CellRangeAddress>& xCellRanges )
468 if(!mpCondFormat || GetScImport().HasNewCondFormatData())
469 return;
471 ScRangeList aRangeList;
472 for(const table::CellRangeAddress& aAddress : xCellRanges)
474 ScRange aRange( aAddress.StartColumn, aAddress.StartRow, aAddress.Sheet, aAddress.EndColumn, aAddress.EndRow, aAddress.Sheet );
475 aRangeList.Join( aRange );
478 ScDocument* pDoc = GetScImport().GetDocument();
479 SCTAB nTab = GetScImport().GetTables().GetCurrentSheet();
480 ScConditionalFormatList* pFormatList = pDoc->GetCondFormList(nTab);
481 auto itr = std::find_if(pFormatList->begin(), pFormatList->end(),
482 [this](const std::unique_ptr<ScConditionalFormat>& rxFormat) { return rxFormat->EqualEntries(*mpCondFormat); });
483 if (itr != pFormatList->end())
485 ScRangeList& rRangeList = (*itr)->GetRangeList();
486 sal_uInt32 nCondId = (*itr)->GetKey();
487 size_t n = aRangeList.size();
488 for(size_t i = 0; i < n; ++i)
490 const ScRange & rRange = aRangeList[i];
491 rRangeList.Join(rRange);
494 pDoc->AddCondFormatData( aRangeList, nTab, nCondId );
495 return;
498 if(mpCondFormat && mbDeleteCondFormat)
500 sal_uLong nIndex = pDoc->AddCondFormat(std::unique_ptr<ScConditionalFormat>(mpCondFormat), nTab );
501 mpCondFormat->SetKey(nIndex);
502 mpCondFormat->SetRange(aRangeList);
504 pDoc->AddCondFormatData( aRangeList, nTab, nIndex );
505 mbDeleteCondFormat = false;
510 void XMLTableStyleContext::FillPropertySet(
511 const uno::Reference< XPropertySet > & rPropSet )
513 if (!IsDefaultStyle())
515 if (GetFamily() == XmlStyleFamily::TABLE_CELL)
517 if (!bParentSet)
519 AddProperty(CTF_SC_CELLSTYLE, uno::makeAny(GetImport().GetStyleDisplayName( XmlStyleFamily::TABLE_CELL, GetParentName() )));
520 bParentSet = true;
522 sal_Int32 nNumFmt = GetNumberFormat();
523 if (nNumFmt >= 0)
524 AddProperty(CTF_SC_NUMBERFORMAT, uno::makeAny(nNumFmt));
526 else if (GetFamily() == XmlStyleFamily::TABLE_TABLE)
528 if (!sPageStyle.isEmpty())
529 AddProperty(CTF_SC_MASTERPAGENAME, uno::makeAny(GetImport().GetStyleDisplayName( XmlStyleFamily::MASTER_PAGE, sPageStyle )));
532 XMLPropStyleContext::FillPropertySet(rPropSet);
535 void XMLTableStyleContext::SetDefaults()
537 if ((GetFamily() == XmlStyleFamily::TABLE_CELL) && GetImport().GetModel().is())
539 uno::Reference <lang::XMultiServiceFactory> xMultiServiceFactory(GetImport().GetModel(), uno::UNO_QUERY);
540 if (xMultiServiceFactory.is())
542 uno::Reference <beans::XPropertySet> xProperties(xMultiServiceFactory->createInstance("com.sun.star.sheet.Defaults"), uno::UNO_QUERY);
543 if (xProperties.is())
544 FillPropertySet(xProperties);
549 void XMLTableStyleContext::AddProperty(const sal_Int16 nContextID, const uno::Any& rValue)
551 XMLPropertyState* property = FindProperty(nContextID);
552 if (property)
553 property->mnIndex = -1; // #i46996# remove old property, so it isn't double
554 sal_Int32 nIndex(static_cast<XMLTableStylesContext *>(pStyles)->GetIndex(nContextID));
555 OSL_ENSURE(nIndex != -1, "Property not found in Map");
556 XMLPropertyState aPropState(nIndex, rValue);
557 GetProperties().push_back(aPropState); // has to be inserted in a sort order later
560 XMLPropertyState* XMLTableStyleContext::FindProperty(const sal_Int16 nContextID)
562 XMLPropertyState* pRet = nullptr;
563 rtl::Reference < XMLPropertySetMapper > xPrMap;
564 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap =
565 pStyles->GetImportPropertyMapper( GetFamily() );
566 OSL_ENSURE( xImpPrMap.is(), "There is the import prop mapper" );
567 if( xImpPrMap.is() )
568 xPrMap = xImpPrMap->getPropertySetMapper();
569 if( xPrMap.is() )
571 auto aIter = std::find_if(GetProperties().begin(), GetProperties().end(),
572 [&xPrMap, &nContextID](const XMLPropertyState& rProp) {
573 return rProp.mnIndex != -1 && xPrMap->GetEntryContextId(rProp.mnIndex) == nContextID;
575 if (aIter != GetProperties().end())
576 pRet = &(*aIter);
578 return pRet;
581 sal_Int32 XMLTableStyleContext::GetNumberFormat()
583 if (nNumberFormat < 0 && !sDataStyleName.isEmpty())
585 const SvXMLNumFormatContext* pStyle = static_cast<const SvXMLNumFormatContext*>(
586 pStyles->FindStyleChildContext(XmlStyleFamily::DATA_STYLE, sDataStyleName, true));
588 if (!pStyle)
590 XMLTableStylesContext* pMyStyles = static_cast<XMLTableStylesContext*>(GetScImport().GetStyles());
591 if (pMyStyles)
592 pStyle = static_cast<const SvXMLNumFormatContext*>(
593 pMyStyles->FindStyleChildContext(XmlStyleFamily::DATA_STYLE, sDataStyleName, true));
594 else
596 OSL_FAIL("not possible to get style");
599 if (pStyle)
600 nNumberFormat = const_cast<SvXMLNumFormatContext*>(pStyle)->GetKey();
602 return nNumberFormat;
605 SvXMLStyleContext *XMLTableStylesContext::CreateStyleStyleChildContext(
606 XmlStyleFamily nFamily, sal_Int32 nElement,
607 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
609 SvXMLStyleContext *pStyle;
610 // use own wrapper for text and paragraph, to record style usage
611 if (nFamily == XmlStyleFamily::TEXT_PARAGRAPH || nFamily == XmlStyleFamily::TEXT_TEXT)
612 pStyle = new ScCellTextStyleContext( GetImport(),*this, nFamily );
613 else
614 pStyle = SvXMLStylesContext::CreateStyleStyleChildContext(
615 nFamily, nElement, xAttrList );
617 if (!pStyle)
619 switch( nFamily )
621 case XmlStyleFamily::TABLE_CELL:
622 case XmlStyleFamily::TABLE_COLUMN:
623 case XmlStyleFamily::TABLE_ROW:
624 case XmlStyleFamily::TABLE_TABLE:
625 pStyle = new XMLTableStyleContext( GetScImport(), *this, nFamily );
626 break;
627 default: break;
631 return pStyle;
634 SvXMLStyleContext *XMLTableStylesContext::CreateDefaultStyleStyleChildContext(
635 XmlStyleFamily nFamily, sal_Int32 nElement,
636 const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
638 SvXMLStyleContext *pStyle(SvXMLStylesContext::CreateDefaultStyleStyleChildContext( nFamily, nElement,
639 xAttrList ));
640 if (!pStyle)
642 switch( nFamily )
644 case XmlStyleFamily::TABLE_CELL:
645 pStyle = new XMLTableStyleContext( GetScImport(), *this, nFamily, true);
646 break;
647 case XmlStyleFamily::SD_GRAPHICS_ID:
648 pStyle = new XMLGraphicsDefaultStyle( GetScImport(), *this);
649 break;
650 default: break;
654 return pStyle;
657 constexpr OUStringLiteral gsCellStyleServiceName(u"com.sun.star.style.CellStyle");
658 constexpr OUStringLiteral gsColumnStyleServiceName(u"" XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_NAME);
659 constexpr OUStringLiteral gsRowStyleServiceName(u"" XML_STYLE_FAMILY_TABLE_ROW_STYLES_NAME);
660 constexpr OUStringLiteral gsTableStyleServiceName(u"" XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME);
662 XMLTableStylesContext::XMLTableStylesContext( SvXMLImport& rImport,
663 const bool bTempAutoStyles )
664 : SvXMLStylesContext( rImport )
665 , nNumberFormatIndex(-1)
666 , nConditionalFormatIndex(-1)
667 , nCellStyleIndex(-1)
668 , nMasterPageNameIndex(-1)
669 , bAutoStyles(bTempAutoStyles)
673 XMLTableStylesContext::~XMLTableStylesContext()
677 void XMLTableStylesContext::endFastElement(sal_Int32 )
679 if (bAutoStyles)
680 GetImport().GetTextImport()->SetAutoStyles( this );
681 else
682 GetScImport().InsertStyles();
685 rtl::Reference < SvXMLImportPropertyMapper >
686 XMLTableStylesContext::GetImportPropertyMapper(
687 XmlStyleFamily nFamily ) const
689 rtl::Reference < SvXMLImportPropertyMapper > xMapper(SvXMLStylesContext::GetImportPropertyMapper(nFamily));
691 if (!xMapper.is())
693 switch( nFamily )
695 case XmlStyleFamily::TABLE_CELL:
697 if( !xCellImpPropMapper.is() )
699 const_cast<XMLTableStylesContext *>(this)->xCellImpPropMapper =
700 new ScXMLCellImportPropertyMapper( GetScImport().GetCellStylesPropertySetMapper(), const_cast<SvXMLImport&>(GetImport()) );
701 xCellImpPropMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(const_cast<SvXMLImport&>(GetImport())));
703 xMapper = xCellImpPropMapper;
705 break;
706 case XmlStyleFamily::TABLE_COLUMN:
708 if( !xColumnImpPropMapper.is() )
709 const_cast<XMLTableStylesContext *>(this)->xColumnImpPropMapper =
710 new SvXMLImportPropertyMapper( GetScImport().GetColumnStylesPropertySetMapper(), const_cast<SvXMLImport&>(GetImport()) );
711 xMapper = xColumnImpPropMapper;
713 break;
714 case XmlStyleFamily::TABLE_ROW:
716 if( !xRowImpPropMapper.is() )
717 const_cast<XMLTableStylesContext *>(this)->xRowImpPropMapper =
718 new ScXMLRowImportPropertyMapper( GetScImport().GetRowStylesPropertySetMapper(), const_cast<SvXMLImport&>(GetImport()) );
719 xMapper = xRowImpPropMapper;
721 break;
722 case XmlStyleFamily::TABLE_TABLE:
724 if( !xTableImpPropMapper.is() )
725 const_cast<XMLTableStylesContext *>(this)->xTableImpPropMapper =
726 new SvXMLImportPropertyMapper( GetScImport().GetTableStylesPropertySetMapper(), const_cast<SvXMLImport&>(GetImport()) );
727 xMapper = xTableImpPropMapper;
729 break;
730 default: break;
734 return xMapper;
737 uno::Reference < XNameContainer >
738 XMLTableStylesContext::GetStylesContainer( XmlStyleFamily nFamily ) const
740 uno::Reference < XNameContainer > xStyles(SvXMLStylesContext::GetStylesContainer(nFamily));
741 if (!xStyles.is())
743 OUString sName;
744 switch( nFamily )
746 case XmlStyleFamily::TABLE_TABLE:
748 if( xTableStyles.is() )
749 xStyles.set(xTableStyles);
750 else
751 sName = "TableStyles";
753 break;
754 case XmlStyleFamily::TABLE_CELL:
756 if( xCellStyles.is() )
757 xStyles.set(xCellStyles);
758 else
759 sName = "CellStyles";
761 break;
762 case XmlStyleFamily::TABLE_COLUMN:
764 if( xColumnStyles.is() )
765 xStyles.set(xColumnStyles);
766 else
767 sName = "ColumnStyles";
769 break;
770 case XmlStyleFamily::TABLE_ROW:
772 if( xRowStyles.is() )
773 xStyles.set(xRowStyles);
774 else
775 sName = "RowStyles";
777 break;
778 default: break;
780 if( !xStyles.is() && !sName.isEmpty() && GetScImport().GetModel().is() )
782 uno::Reference< XStyleFamiliesSupplier > xFamiliesSupp(
783 GetScImport().GetModel(), UNO_QUERY );
784 if (xFamiliesSupp.is())
786 uno::Reference< XNameAccess > xFamilies(xFamiliesSupp->getStyleFamilies());
790 xStyles.set(xFamilies->getByName( sName ), uno::UNO_QUERY);
792 catch ( uno::Exception& )
794 // #i97680# Named table/column/row styles aren't supported, getByName will throw an exception.
795 // For better interoperability, these styles should then be handled as automatic styles.
796 // For now, NULL is returned (and the style is ignored).
798 switch( nFamily )
800 case XmlStyleFamily::TABLE_TABLE:
801 const_cast<XMLTableStylesContext *>(this)->xTableStyles.set(xStyles);
802 break;
803 case XmlStyleFamily::TABLE_CELL:
804 const_cast<XMLTableStylesContext *>(this)->xCellStyles.set(xStyles);
805 break;
806 case XmlStyleFamily::TABLE_COLUMN:
807 const_cast<XMLTableStylesContext *>(this)->xColumnStyles.set(xStyles);
808 break;
809 case XmlStyleFamily::TABLE_ROW:
810 const_cast<XMLTableStylesContext *>(this)->xRowStyles.set(xStyles);
811 break;
812 default: break;
818 return xStyles;
821 OUString XMLTableStylesContext::GetServiceName( XmlStyleFamily nFamily ) const
823 OUString sServiceName(SvXMLStylesContext::GetServiceName(nFamily));
824 if (sServiceName.isEmpty())
826 switch( nFamily )
828 case XmlStyleFamily::TABLE_COLUMN:
829 sServiceName = gsColumnStyleServiceName;
830 break;
831 case XmlStyleFamily::TABLE_ROW:
832 sServiceName = gsRowStyleServiceName;
833 break;
834 case XmlStyleFamily::TABLE_CELL:
835 sServiceName = gsCellStyleServiceName;
836 break;
837 case XmlStyleFamily::TABLE_TABLE:
838 sServiceName = gsTableStyleServiceName;
839 break;
840 default: break;
843 return sServiceName;
846 sal_Int32 XMLTableStylesContext::GetIndex(const sal_Int16 nContextID)
848 if (nContextID == CTF_SC_CELLSTYLE)
850 if (nCellStyleIndex == -1)
851 nCellStyleIndex =
852 GetImportPropertyMapper(XmlStyleFamily::TABLE_CELL)->getPropertySetMapper()->FindEntryIndex(nContextID);
853 return nCellStyleIndex;
855 else if (nContextID == CTF_SC_NUMBERFORMAT)
857 if (nNumberFormatIndex == -1)
858 nNumberFormatIndex =
859 GetImportPropertyMapper(XmlStyleFamily::TABLE_CELL)->getPropertySetMapper()->FindEntryIndex(nContextID);
860 return nNumberFormatIndex;
862 else if (nContextID == CTF_SC_IMPORT_MAP)
864 if (nConditionalFormatIndex == -1)
865 nConditionalFormatIndex =
866 GetImportPropertyMapper(XmlStyleFamily::TABLE_CELL)->getPropertySetMapper()->FindEntryIndex(nContextID);
867 return nConditionalFormatIndex;
869 else if (nContextID == CTF_SC_MASTERPAGENAME)
871 if (nMasterPageNameIndex == -1)
872 nMasterPageNameIndex =
873 GetImportPropertyMapper(XmlStyleFamily::TABLE_TABLE)->getPropertySetMapper()->FindEntryIndex(nContextID);
874 return nMasterPageNameIndex;
876 else
877 return -1;
881 bool ScXMLMasterStylesContext::InsertStyleFamily( XmlStyleFamily ) const
883 return true;
886 ScXMLMasterStylesContext::ScXMLMasterStylesContext( SvXMLImport& rImport ) :
887 SvXMLStylesContext( rImport )
891 ScXMLMasterStylesContext::~ScXMLMasterStylesContext()
895 SvXMLStyleContext *ScXMLMasterStylesContext::CreateStyleChildContext(
896 sal_Int32 nElement,
897 const uno::Reference< XFastAttributeList > & xAttrList )
899 SvXMLStyleContext *pContext(nullptr);
901 if( nElement == XML_ELEMENT(STYLE, XML_MASTER_PAGE) &&
902 InsertStyleFamily( XmlStyleFamily::MASTER_PAGE ) )
903 pContext = new ScMasterPageContext(
904 GetImport(), nElement, xAttrList,
905 !GetImport().GetTextImport()->IsInsertMode() );
907 // any other style will be ignored here!
909 return pContext;
912 SvXMLStyleContext *ScXMLMasterStylesContext::CreateStyleStyleChildContext(
913 XmlStyleFamily /* nFamily */,
914 sal_Int32 /* nElement */,
915 const uno::Reference< XFastAttributeList > & /* xAttrList */ )
917 return nullptr;
920 void ScXMLMasterStylesContext::endFastElement(sal_Int32 )
922 FinishStyles(true);
926 ScMasterPageContext::ScMasterPageContext( SvXMLImport& rImport,
927 sal_Int32 nElement,
928 const uno::Reference< XFastAttributeList > & xAttrList,
929 bool bOverwrite ) :
930 XMLTextMasterPageContext( rImport, nElement, xAttrList, bOverwrite ),
931 bContainsRightHeader(false),
932 bContainsRightFooter(false)
936 ScMasterPageContext::~ScMasterPageContext()
940 SvXMLImportContext *ScMasterPageContext::CreateHeaderFooterContext(
941 sal_Int32 nElement,
942 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList,
943 const bool bFooter,
944 const bool bLeft,
945 const bool /*bFirst*/ )
947 if (!bLeft)
949 if (bFooter)
950 bContainsRightFooter = true;
951 else
952 bContainsRightHeader = true;
954 if (!xPropSet.is())
955 xPropSet.set(GetStyle(), UNO_QUERY );
956 return new XMLTableHeaderFooterContext( GetImport(),
957 nElement,
958 xAttrList,
959 xPropSet,
960 bFooter, bLeft );
963 void ScMasterPageContext::ClearContent(const OUString& rContent)
965 if (!xPropSet.is())
966 xPropSet.set(GetStyle(), UNO_QUERY );
968 if (xPropSet.is())
970 uno::Reference < sheet::XHeaderFooterContent > xHeaderFooterContent(xPropSet->getPropertyValue( rContent ), uno::UNO_QUERY);
971 if (xHeaderFooterContent.is())
973 xHeaderFooterContent->getLeftText()->setString("");
974 xHeaderFooterContent->getCenterText()->setString("");
975 xHeaderFooterContent->getRightText()->setString("");
976 xPropSet->setPropertyValue( rContent, uno::makeAny(xHeaderFooterContent) );
981 void ScMasterPageContext::Finish( bool bOverwrite )
983 XMLTextMasterPageContext::Finish(bOverwrite);
984 if (!bContainsRightFooter)
985 ClearContent(SC_UNO_PAGE_RIGHTFTRCON);
986 if (!bContainsRightHeader)
987 ClearContent(SC_UNO_PAGE_RIGHTHDRCON);
990 ScCellTextStyleContext::ScCellTextStyleContext( SvXMLImport& rImport,
991 SvXMLStylesContext& rStyles, XmlStyleFamily nFamily ) :
992 XMLTextStyleContext( rImport, rStyles, nFamily, false/*bDefaultStyle*/ ),
993 nLastSheet(-1)
997 ScCellTextStyleContext::~ScCellTextStyleContext()
1001 void ScCellTextStyleContext::FillPropertySet( const uno::Reference<beans::XPropertySet>& xPropSet )
1003 XMLTextStyleContext::FillPropertySet( xPropSet );
1005 ScXMLImport& rXMLImport = GetScImport();
1007 ScCellTextCursor* pCellImp = comphelper::getUnoTunnelImplementation<ScCellTextCursor>( xPropSet );
1008 if (pCellImp)
1010 ScAddress aPos = pCellImp->GetCellObj().GetPosition();
1011 if ( aPos.Tab() != nLastSheet )
1013 ESelection aSel = pCellImp->GetSelection();
1015 ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(GetImport().GetModel())->GetSheetSaveData();
1016 pSheetData->AddTextStyle( GetName(), aPos, aSel );
1018 nLastSheet = aPos.Tab();
1021 else if ( rXMLImport.GetTables().GetCurrentSheet() != nLastSheet )
1023 ScDrawTextCursor* pDrawImp = comphelper::getUnoTunnelImplementation<ScDrawTextCursor>( xPropSet );
1024 if (pDrawImp)
1026 XMLTableShapeImportHelper* pTableShapeImport = static_cast<XMLTableShapeImportHelper*>(GetScImport().GetShapeImport().get());
1027 ScXMLAnnotationContext* pAnnotationContext = pTableShapeImport->GetAnnotationContext();
1028 if (pAnnotationContext)
1030 pAnnotationContext->AddContentStyle( GetFamily(), GetName(), pDrawImp->GetSelection() );
1031 nLastSheet = rXMLImport.GetTables().GetCurrentSheet();
1035 // if it's a different shape, BlockSheet is called from XMLTableShapeImportHelper::finishShape
1036 // formatted text in page headers/footers can be ignored
1040 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */