xref: /AOO41X/main/sc/source/filter/xml/XMLStylesExportHelper.cxx (revision a9ab3c7b3d31474a75bf54404ada03e2f02464cb)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 // INCLUDE ---------------------------------------------------------------
29 #include "XMLStylesExportHelper.hxx"
30 #include "global.hxx"
31 #include "unonames.hxx"
32 #include "XMLConverter.hxx"
33 #include "xmlexprt.hxx"
34 #include "document.hxx"
35 #include "rangeutl.hxx"
36 #include <xmloff/xmltoken.hxx>
37 #include <xmloff/xmlnmspe.hxx>
38 #include <xmloff/XMLEventExport.hxx>
39 #include <xmloff/nmspmap.hxx>
40 #include <tools/debug.hxx>
41 #include <com/sun/star/uno/Reference.h>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/sheet/XSheetCondition.hpp>
44 #include <com/sun/star/sheet/TableValidationVisibility.hpp>
45 #include <comphelper/extract.hxx>
46 #include <sfx2/app.hxx>
47 
48 #include <algorithm>
49 
50 using namespace com::sun::star;
51 using namespace xmloff::token;
52 
ScMyValidation()53 ScMyValidation::ScMyValidation()
54     : sName(),
55     sErrorMessage(),
56     sErrorTitle(),
57     sImputMessage(),
58     sImputTitle(),
59     sFormula1(),
60     sFormula2(),
61     bShowErrorMessage(sal_False),
62     bShowImputMessage(sal_False),
63     bIgnoreBlanks(sal_False)
64 {
65 }
66 
~ScMyValidation()67 ScMyValidation::~ScMyValidation()
68 {
69 }
70 
IsEqual(const ScMyValidation & aVal) const71 sal_Bool ScMyValidation::IsEqual(const ScMyValidation& aVal) const
72 {
73     if (aVal.bIgnoreBlanks == bIgnoreBlanks &&
74         aVal.bShowImputMessage == bShowImputMessage &&
75         aVal.bShowErrorMessage == bShowErrorMessage &&
76         aVal.aBaseCell.Sheet == aBaseCell.Sheet &&
77         aVal.aBaseCell.Column == aBaseCell.Column &&
78         aVal.aBaseCell.Row == aBaseCell.Row &&
79         aVal.aAlertStyle == aAlertStyle &&
80         aVal.aValidationType == aValidationType &&
81         aVal.aOperator == aOperator &&
82         aVal.sErrorTitle == sErrorTitle &&
83         aVal.sImputTitle == sImputTitle &&
84         aVal.sErrorMessage == sErrorMessage &&
85         aVal.sImputMessage == sImputMessage &&
86         aVal.sFormula1 == sFormula1 &&
87         aVal.sFormula2 == sFormula2)
88         return sal_True;
89     else
90         return sal_False;
91 }
92 
ScMyValidationsContainer()93 ScMyValidationsContainer::ScMyValidationsContainer()
94     : aValidationVec(),
95     sEmptyString(),
96     sERRALSTY(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_ERRALSTY)),
97     sIGNOREBL(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_IGNOREBL)),
98     sSHOWLIST(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_SHOWLIST)),
99     sTYPE(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_TYPE)),
100     sSHOWINP(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_SHOWINP)),
101     sSHOWERR(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_SHOWERR)),
102     sINPTITLE(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_INPTITLE)),
103     sINPMESS(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_INPMESS)),
104     sERRTITLE(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_ERRTITLE)),
105     sERRMESS(RTL_CONSTASCII_USTRINGPARAM(SC_UNONAME_ERRMESS)),
106     sOnError(RTL_CONSTASCII_USTRINGPARAM("OnError")),
107     sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
108     sStarBasic(RTL_CONSTASCII_USTRINGPARAM("StarBasic")),
109     sScript(RTL_CONSTASCII_USTRINGPARAM("Script")),
110     sLibrary(RTL_CONSTASCII_USTRINGPARAM("Library")),
111     sMacroName(RTL_CONSTASCII_USTRINGPARAM("MacroName"))
112 {
113 }
114 
~ScMyValidationsContainer()115 ScMyValidationsContainer::~ScMyValidationsContainer()
116 {
117 }
118 
AddValidation(const uno::Any & aTempAny,sal_Int32 & nValidationIndex)119 sal_Bool ScMyValidationsContainer::AddValidation(const uno::Any& aTempAny,
120     sal_Int32& nValidationIndex)
121 {
122     sal_Bool bAdded(sal_False);
123     uno::Reference<beans::XPropertySet> xPropertySet(aTempAny, uno::UNO_QUERY);
124     if (xPropertySet.is())
125     {
126         rtl::OUString sErrorMessage;
127         xPropertySet->getPropertyValue(sERRMESS) >>= sErrorMessage;
128         rtl::OUString sErrorTitle;
129         xPropertySet->getPropertyValue(sERRTITLE) >>= sErrorTitle;
130         rtl::OUString sImputMessage;
131         xPropertySet->getPropertyValue(sINPMESS) >>= sImputMessage;
132         rtl::OUString sImputTitle;
133         xPropertySet->getPropertyValue(sINPTITLE) >>= sImputTitle;
134         sal_Bool bShowErrorMessage = ::cppu::any2bool(xPropertySet->getPropertyValue(sSHOWERR));
135         sal_Bool bShowImputMessage = ::cppu::any2bool(xPropertySet->getPropertyValue(sSHOWINP));
136         sheet::ValidationType aValidationType;
137         xPropertySet->getPropertyValue(sTYPE) >>= aValidationType;
138         if (bShowErrorMessage || bShowImputMessage || aValidationType != sheet::ValidationType_ANY ||
139             sErrorMessage.getLength() || sErrorTitle.getLength() || sImputMessage.getLength() || sImputTitle.getLength())
140         {
141             ScMyValidation aValidation;
142             aValidation.sErrorMessage = sErrorMessage;
143             aValidation.sErrorTitle = sErrorTitle;
144             aValidation.sImputMessage = sImputMessage;
145             aValidation.sImputTitle = sImputTitle;
146             aValidation.bShowErrorMessage = bShowErrorMessage;
147             aValidation.bShowImputMessage = bShowImputMessage;
148             aValidation.aValidationType = aValidationType;
149             aValidation.bIgnoreBlanks = ::cppu::any2bool(xPropertySet->getPropertyValue(sIGNOREBL));
150             xPropertySet->getPropertyValue(sSHOWLIST) >>= aValidation.nShowList;
151             xPropertySet->getPropertyValue(sERRALSTY) >>= aValidation.aAlertStyle;
152             uno::Reference<sheet::XSheetCondition> xCondition(xPropertySet, uno::UNO_QUERY);
153             if (xCondition.is())
154             {
155                 aValidation.sFormula1 = xCondition->getFormula1();
156                 aValidation.sFormula2 = xCondition->getFormula2();
157                 aValidation.aOperator = xCondition->getOperator();
158                 aValidation.aBaseCell = xCondition->getSourcePosition();
159             }
160             //ScMyValidationRange aValidationRange;
161             sal_Bool bEqualFound(sal_False);
162             sal_Int32 i(0);
163             sal_Int32 nCount(aValidationVec.size());
164             while (i < nCount && !bEqualFound)
165             {
166                 bEqualFound = aValidationVec[i].IsEqual(aValidation);
167                 if (!bEqualFound)
168                     ++i;
169             }
170             if (bEqualFound)
171                 nValidationIndex = i;
172             else
173             {
174                 sal_Int32 nNameIndex(nCount + 1);
175                 rtl::OUString sCount(rtl::OUString::valueOf(nNameIndex));
176                 rtl::OUString sPrefix(RTL_CONSTASCII_USTRINGPARAM("val"));
177                 aValidation.sName += sPrefix;
178                 aValidation.sName += sCount;
179                 aValidationVec.push_back(aValidation);
180                 nValidationIndex = nCount;
181                 bAdded = sal_True;
182             }
183         }
184     }
185     return bAdded;
186 }
187 
GetCondition(ScXMLExport & rExport,const ScMyValidation & aValidation)188 rtl::OUString ScMyValidationsContainer::GetCondition(ScXMLExport& rExport, const ScMyValidation& aValidation)
189 {
190     /* ATTENTION! Should the condition to not write sheet::ValidationType_ANY
191      * ever be changed, adapt the conditional call of
192      * MarkUsedExternalReferences() in
193      * ScTableValidationObj::ScTableValidationObj() accordingly! */
194     rtl::OUString sCondition;
195     if (aValidation.aValidationType != sheet::ValidationType_ANY)
196     {
197         switch (aValidation.aValidationType)
198         {
199             //case sheet::ValidationType_CUSTOM
200             case sheet::ValidationType_DATE :
201                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-date()"));
202             break;
203             case sheet::ValidationType_DECIMAL :
204                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-decimal-number()"));
205             break;
206             case sheet::ValidationType_LIST :
207                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-in-list("));
208                 sCondition += aValidation.sFormula1;
209                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
210             break;
211             case sheet::ValidationType_TEXT_LEN :
212                 if (aValidation.aOperator != sheet::ConditionOperator_BETWEEN &&
213                     aValidation.aOperator != sheet::ConditionOperator_NOT_BETWEEN)
214                     sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-text-length()"));
215             break;
216             case sheet::ValidationType_TIME :
217                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-time()"));
218             break;
219             case sheet::ValidationType_WHOLE :
220                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-whole-number()"));
221             break;
222             default:
223             {
224                 // added to avoid warnings
225             }
226         }
227         if (aValidation.aValidationType != sheet::ValidationType_LIST &&
228             (aValidation.sFormula1.getLength() ||
229             (aValidation.aOperator == sheet::ConditionOperator_BETWEEN &&
230             aValidation.aOperator == sheet::ConditionOperator_NOT_BETWEEN &&
231             aValidation.sFormula2.getLength())))
232         {
233             if (aValidation.aValidationType != sheet::ValidationType_TEXT_LEN)
234                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" and "));
235             if (aValidation.aOperator != sheet::ConditionOperator_BETWEEN &&
236                 aValidation.aOperator != sheet::ConditionOperator_NOT_BETWEEN)
237             {
238                 if (aValidation.aValidationType != sheet::ValidationType_TEXT_LEN)
239                     sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content()"));
240                 switch (aValidation.aOperator)
241                 {
242                     case sheet::ConditionOperator_EQUAL :
243                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("="));
244                     break;
245                     case sheet::ConditionOperator_GREATER :
246                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(">"));
247                     break;
248                     case sheet::ConditionOperator_GREATER_EQUAL :
249                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(">="));
250                     break;
251                     case sheet::ConditionOperator_LESS :
252                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("<"));
253                     break;
254                     case sheet::ConditionOperator_LESS_EQUAL :
255                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("<="));
256                     break;
257                     case sheet::ConditionOperator_NOT_EQUAL :
258                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("!="));
259                     break;
260                     default:
261                     {
262                         // added to avoid warnings
263                     }
264                 }
265                 sCondition += aValidation.sFormula1;
266             }
267             else
268             {
269                 if (aValidation.aValidationType == sheet::ValidationType_TEXT_LEN)
270                 {
271                     if (aValidation.aOperator == sheet::ConditionOperator_BETWEEN)
272                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-text-length-is-between("));
273                     else
274                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-text-length-is-not-between("));
275                 }
276                 else
277                 {
278                     if (aValidation.aOperator == sheet::ConditionOperator_BETWEEN)
279                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-between("));
280                     else
281                         sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cell-content-is-not-between("));
282                 }
283                 sCondition += aValidation.sFormula1;
284                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","));
285                 sCondition += aValidation.sFormula2;
286                 sCondition += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
287             }
288         }
289         else
290             if (aValidation.aValidationType == sheet::ValidationType_TEXT_LEN)
291                 sCondition = rtl::OUString();
292     }
293     if (sCondition.getLength())
294     {
295         const formula::FormulaGrammar::Grammar eGrammar = rExport.GetDocument()->GetStorageGrammar();
296         sal_uInt16 nNamespacePrefix = (eGrammar == formula::FormulaGrammar::GRAM_ODFF ? XML_NAMESPACE_OF : XML_NAMESPACE_OOOC);
297         sCondition = rExport.GetNamespaceMap().GetQNameByKey( nNamespacePrefix, sCondition, sal_False );
298     }
299 
300     return sCondition;
301 }
302 
GetBaseCellAddress(ScDocument * pDoc,const table::CellAddress & aCell)303 rtl::OUString ScMyValidationsContainer::GetBaseCellAddress(ScDocument* pDoc, const table::CellAddress& aCell)
304 {
305     rtl::OUString sAddress;
306     ScRangeStringConverter::GetStringFromAddress( sAddress, aCell, pDoc, ::formula::FormulaGrammar::CONV_OOO );
307     return sAddress;
308 }
309 
WriteMessage(ScXMLExport & rExport,const rtl::OUString & sTitle,const rtl::OUString & sOUMessage,const sal_Bool bShowMessage,const sal_Bool bIsHelpMessage)310 void ScMyValidationsContainer::WriteMessage(ScXMLExport& rExport,
311     const rtl::OUString& sTitle, const rtl::OUString& sOUMessage,
312     const sal_Bool bShowMessage, const sal_Bool bIsHelpMessage)
313 {
314     if (sTitle.getLength())
315         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_TITLE, sTitle);
316     if (bShowMessage)
317         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY, XML_TRUE);
318     else
319         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY, XML_FALSE);
320     SvXMLElementExport* pMessage(NULL);
321     if (bIsHelpMessage)
322         pMessage = new SvXMLElementExport(rExport, XML_NAMESPACE_TABLE, XML_HELP_MESSAGE, sal_True, sal_True);
323     else
324         pMessage = new SvXMLElementExport(rExport, XML_NAMESPACE_TABLE, XML_ERROR_MESSAGE, sal_True, sal_True);
325     if (sOUMessage.getLength())
326     {
327         sal_Int32 i(0);
328         rtl::OUStringBuffer sTemp;
329         String sMessage(sOUMessage);
330         rtl::OUString sText (sMessage.ConvertLineEnd(LINEEND_LF));
331         sal_Bool bPrevCharWasSpace(sal_True);
332         while(i < sText.getLength())
333         {
334             if( sText[i] == '\n')
335             {
336                 SvXMLElementExport aElemP(rExport, XML_NAMESPACE_TEXT, XML_P, sal_True, sal_False);
337                 rExport.GetTextParagraphExport()->exportText(sTemp.makeStringAndClear(), bPrevCharWasSpace);
338             }
339             else
340                 sTemp.append(sText[i]);
341             ++i;
342         }
343         if (sTemp.getLength())
344         {
345             SvXMLElementExport aElemP(rExport, XML_NAMESPACE_TEXT, XML_P, sal_True, sal_False);
346             rExport.GetTextParagraphExport()->exportText(sTemp.makeStringAndClear(), bPrevCharWasSpace);
347         }
348     }
349     if (pMessage)
350         delete pMessage;
351 }
352 
WriteValidations(ScXMLExport & rExport)353 void ScMyValidationsContainer::WriteValidations(ScXMLExport& rExport)
354 {
355     if (aValidationVec.size())
356     {
357         SvXMLElementExport aElemVs(rExport, XML_NAMESPACE_TABLE, XML_CONTENT_VALIDATIONS, sal_True, sal_True);
358         ScMyValidationVec::iterator aItr(aValidationVec.begin());
359         ScMyValidationVec::iterator aEndItr(aValidationVec.end());
360         while (aItr != aEndItr)
361         {
362             rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NAME, aItr->sName);
363             rtl::OUString sCondition(GetCondition(rExport, *aItr));
364             if (sCondition.getLength())
365             {
366                 rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_CONDITION, sCondition);
367                 if (aItr->bIgnoreBlanks)
368                     rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_ALLOW_EMPTY_CELL, XML_TRUE);
369                 else
370                     rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_ALLOW_EMPTY_CELL, XML_FALSE);
371                 if (aItr->aValidationType == sheet::ValidationType_LIST)
372                 {
373                     switch (aItr->nShowList)
374                     {
375                     case sheet::TableValidationVisibility::INVISIBLE:
376                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY_LIST, XML_NO);
377                     break;
378                     case sheet::TableValidationVisibility::UNSORTED:
379                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY_LIST, XML_UNSORTED);
380                     break;
381                     case sheet::TableValidationVisibility::SORTEDASCENDING:
382                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_DISPLAY_LIST, XML_SORTED_ASCENDING);
383                     break;
384                     default:
385                         DBG_ERROR("unknown ListType");
386                     }
387                 }
388             }
389             rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_BASE_CELL_ADDRESS, GetBaseCellAddress(rExport.GetDocument(), aItr->aBaseCell));
390             SvXMLElementExport aElemV(rExport, XML_NAMESPACE_TABLE, XML_CONTENT_VALIDATION, sal_True, sal_True);
391             if (aItr->bShowImputMessage || aItr->sImputMessage.getLength() || aItr->sImputTitle.getLength())
392             {
393                 WriteMessage(rExport, aItr->sImputTitle, aItr->sImputMessage, aItr->bShowImputMessage, sal_True);
394             }
395             if (aItr->bShowErrorMessage || aItr->sErrorMessage.getLength() || aItr->sErrorTitle.getLength())
396             {
397                 switch (aItr->aAlertStyle)
398                 {
399                     case sheet::ValidationAlertStyle_INFO :
400                     {
401                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_MESSAGE_TYPE, XML_INFORMATION);
402                         WriteMessage(rExport, aItr->sErrorTitle, aItr->sErrorMessage, aItr->bShowErrorMessage, sal_False);
403                     }
404                     break;
405                     case sheet::ValidationAlertStyle_WARNING :
406                     {
407                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_MESSAGE_TYPE, XML_WARNING);
408                         WriteMessage(rExport, aItr->sErrorTitle, aItr->sErrorMessage, aItr->bShowErrorMessage, sal_False);
409                     }
410                     break;
411                     case sheet::ValidationAlertStyle_STOP :
412                     {
413                         rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_MESSAGE_TYPE, XML_STOP);
414                         WriteMessage(rExport, aItr->sErrorTitle, aItr->sErrorMessage, aItr->bShowErrorMessage, sal_False);
415                     }
416                     break;
417                     case sheet::ValidationAlertStyle_MACRO :
418                     {
419                         {
420                             //rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_NAME, aItr->sErrorTitle);
421                             if (aItr->bShowErrorMessage)
422                                 rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_EXECUTE, XML_TRUE);
423                             else
424                                 rExport.AddAttribute(XML_NAMESPACE_TABLE, XML_EXECUTE, XML_FALSE);
425                             SvXMLElementExport aEMElem(rExport, XML_NAMESPACE_TABLE, XML_ERROR_MACRO, sal_True, sal_True);
426                         }
427                         {
428                             // #i47525# for a script URL the type and the property name for the URL
429                             // are both "Script", for a simple macro name the type is "StarBasic"
430                             // and the property name is "MacroName".
431                             bool bScriptURL = SfxApplication::IsXScriptURL( aItr->sErrorTitle );
432 
433                             uno::Sequence<beans::PropertyValue> aSeq(3);
434                             beans::PropertyValue* pArr(aSeq.getArray());
435                             pArr[0].Name = sEventType;
436                             pArr[0].Value <<= bScriptURL ? sScript : sStarBasic;
437                             pArr[1].Name = sLibrary;
438                             pArr[1].Value <<= sEmptyString;
439                             pArr[2].Name = bScriptURL ? sScript : sMacroName;
440                             pArr[2].Value <<= aItr->sErrorTitle;
441 
442                             // 2) export the sequence
443                             rExport.GetEventExport().ExportSingleEvent( aSeq, sOnError);
444                         }
445                     }
446                     break;
447                     default:
448                     {
449                         // added to avoid warnings
450                     }
451                 }
452             }
453             ++aItr;
454         }
455     }
456 }
457 
GetValidationName(const sal_Int32 nIndex)458 const rtl::OUString& ScMyValidationsContainer::GetValidationName(const sal_Int32 nIndex)
459 {
460     DBG_ASSERT( static_cast<size_t>(nIndex) < aValidationVec.size(), "out of range" );
461     return aValidationVec[nIndex].sName;
462 }
463 
464 //==============================================================================
465 
GetStyleNameIndex(const ScFormatRangeStyles * pCellStyles,const sal_Int32 nTable,const sal_Int32 nPos,const sal_Int32 i,const sal_Bool bRow,sal_Bool & bIsAutoStyle)466 sal_Int32 ScMyDefaultStyles::GetStyleNameIndex(const ScFormatRangeStyles* pCellStyles,
467     const sal_Int32 nTable, const sal_Int32 nPos,
468     const sal_Int32 i, const sal_Bool bRow, sal_Bool& bIsAutoStyle)
469 {
470     if (bRow)
471         return pCellStyles->GetStyleNameIndex(nTable, nPos, i,
472                                 bIsAutoStyle);
473     else
474         return pCellStyles->GetStyleNameIndex(nTable, i, nPos,
475                                 bIsAutoStyle);
476 }
477 
FillDefaultStyles(const sal_Int32 nTable,const sal_Int32 nLastRow,const sal_Int32 nLastCol,const ScFormatRangeStyles * pCellStyles,ScDocument * pDoc,const sal_Bool bRow)478 void ScMyDefaultStyles::FillDefaultStyles(const sal_Int32 nTable,
479     const sal_Int32 nLastRow, const sal_Int32 nLastCol,
480     const ScFormatRangeStyles* pCellStyles, ScDocument* pDoc,
481     const sal_Bool bRow)
482 {
483     if (pDoc)
484     {
485         SCTAB nTab = static_cast<SCTAB>(nTable);
486         sal_Int32 nPos;
487         sal_Int32 nLast;
488         ScMyDefaultStyleList* pDefaults;
489         if (bRow)
490         {
491             pDefaults = pRowDefaults;
492             nLast = nLastRow;
493         }
494         else
495         {
496             pDefaults = pColDefaults;
497             nLast = nLastCol;
498         }
499         sal_Bool bPrevAutoStyle(sal_False);
500         sal_Bool bIsAutoStyle;
501         sal_Bool bResult;
502         sal_Int32 nPrevIndex(0);
503         sal_Int32 nIndex;
504         sal_Int32 nRepeat(0);
505         sal_Int32 nEmptyRepeat(0);
506         for (sal_Int32 i = nLast; i >= 0; --i)
507         {
508             if (bRow)
509             {
510                 SCCOL nCol;
511                 bResult = pDoc->GetRowDefault(nTab,
512                     static_cast<SCROW>(i), static_cast<SCCOL>(nLastCol), nCol);
513                 nPos = static_cast<sal_Int32>(nCol);
514             }
515             else
516             {
517                 SCROW nRow;
518                 bResult = pDoc->GetColDefault(nTab,
519                     static_cast<SCCOL>(i), static_cast<SCROW>(nLastRow), nRow);
520                 nPos = static_cast<sal_Int32>(nRow);
521             }
522             if (bResult)
523             {
524                 nEmptyRepeat = 0;
525                 if (!nRepeat)
526                 {
527                     nPrevIndex = GetStyleNameIndex(pCellStyles, nTab, nPos, i,
528                                                 bRow, bPrevAutoStyle);
529                     (*pDefaults)[i].nIndex = nPrevIndex;
530                     (*pDefaults)[i].bIsAutoStyle = bPrevAutoStyle;
531                     nRepeat = 1;
532                 }
533                 else
534                 {
535                     nIndex = GetStyleNameIndex(pCellStyles, nTab, nPos, i,
536                                             bRow, bIsAutoStyle);
537                     if ((nIndex != nPrevIndex) || (bIsAutoStyle != bPrevAutoStyle))
538                     {
539                         nRepeat = 1;
540                         nPrevIndex = GetStyleNameIndex(pCellStyles, nTab, nPos, i,
541                                                 bRow, bPrevAutoStyle);
542                         (*pDefaults)[i].nIndex = nPrevIndex;
543                         (*pDefaults)[i].bIsAutoStyle = bPrevAutoStyle;
544                     }
545                     else
546                     {
547                         (*pDefaults)[i].nIndex = nPrevIndex;
548                         (*pDefaults)[i].bIsAutoStyle = bPrevAutoStyle;
549                         ++nRepeat;
550                         if (nRepeat > 1)
551                             (*pDefaults)[i].nRepeat = nRepeat;
552                     }
553                 }
554             }
555             else
556             {
557                 nRepeat = 0;
558                 if (!nEmptyRepeat)
559                     nEmptyRepeat = 1;
560                 else
561                 {
562                     ++nEmptyRepeat;
563                     if (nEmptyRepeat > 1)
564                         (*pDefaults)[i].nRepeat = nEmptyRepeat;
565                 }
566             }
567         }
568     }
569 }
570 
FillDefaultStyles(const sal_Int32 nTable,const sal_Int32 nLastRow,const sal_Int32 nLastCol,const ScFormatRangeStyles * pCellStyles,ScDocument * pDoc)571 void ScMyDefaultStyles::FillDefaultStyles(const sal_Int32 nTable,
572     const sal_Int32 nLastRow, const sal_Int32 nLastCol,
573     const ScFormatRangeStyles* pCellStyles, ScDocument* pDoc)
574 {
575     if (pRowDefaults)
576         delete pRowDefaults;
577     pRowDefaults = new ScMyDefaultStyleList(nLastRow + 1);
578     FillDefaultStyles(nTable, nLastRow, nLastCol, pCellStyles, pDoc, sal_True);
579     if (pColDefaults)
580         delete pColDefaults;
581     pColDefaults = new ScMyDefaultStyleList(nLastCol + 1);
582     FillDefaultStyles(nTable, nLastRow, nLastCol, pCellStyles, pDoc, sal_False);
583 }
584 
~ScMyDefaultStyles()585 ScMyDefaultStyles::~ScMyDefaultStyles()
586 {
587     if (pRowDefaults)
588         delete pRowDefaults;
589     if (pColDefaults)
590         delete pColDefaults;
591 }
592 
ScMyRowFormatRange()593 ScMyRowFormatRange::ScMyRowFormatRange()
594     : nStartColumn(0),
595     nRepeatColumns(0),
596     nRepeatRows(0),
597     nIndex(-1),
598     nValidationIndex(-1),
599     bIsAutoStyle(sal_True)
600 {
601 }
602 
operator <(const ScMyRowFormatRange & rRange) const603 sal_Bool ScMyRowFormatRange::operator< (const ScMyRowFormatRange& rRange) const
604 {
605     return (nStartColumn < rRange.nStartColumn);
606 }
607 
ScRowFormatRanges()608 ScRowFormatRanges::ScRowFormatRanges()
609     : aRowFormatRanges(),
610     pRowDefaults(NULL),
611     pColDefaults(NULL),
612     nSize(0)
613 {
614 }
615 
ScRowFormatRanges(const ScRowFormatRanges * pRanges)616 ScRowFormatRanges::ScRowFormatRanges(const ScRowFormatRanges* pRanges)
617     : aRowFormatRanges(pRanges->aRowFormatRanges),
618     pRowDefaults(pRanges->pRowDefaults),
619     pColDefaults(pRanges->pColDefaults),
620     nSize(pRanges->nSize)
621 {
622 }
623 
~ScRowFormatRanges()624 ScRowFormatRanges::~ScRowFormatRanges()
625 {
626 }
627 
Clear()628 void ScRowFormatRanges::Clear()
629 {
630     aRowFormatRanges.clear();
631     nSize = 0;
632 }
633 
AddRange(const sal_Int32 nPrevStartCol,const sal_Int32 nRepeat,const sal_Int32 nPrevIndex,const sal_Bool bPrevAutoStyle,const ScMyRowFormatRange & rFormatRange)634 void ScRowFormatRanges::AddRange(const sal_Int32 nPrevStartCol, const sal_Int32 nRepeat, const sal_Int32 nPrevIndex,
635     const sal_Bool bPrevAutoStyle, const ScMyRowFormatRange& rFormatRange)
636 {
637     sal_Int32 nIndex(-1);
638     if ((nPrevIndex != rFormatRange.nIndex) ||
639         (bPrevAutoStyle != rFormatRange.bIsAutoStyle))
640         nIndex = rFormatRange.nIndex;
641 
642     sal_Bool bInserted(sal_False);
643     if (!aRowFormatRanges.empty())
644     {
645         ScMyRowFormatRange* pRange(&aRowFormatRanges.back());
646         if (pRange)
647         {
648             if ((nPrevStartCol == (pRange->nStartColumn + pRange->nRepeatColumns)) &&
649                 (pRange->bIsAutoStyle == rFormatRange.bIsAutoStyle) &&
650                 (pRange->nIndex == nIndex) &&
651                 (pRange->nValidationIndex == rFormatRange.nValidationIndex))
652             {
653                 if (rFormatRange.nRepeatRows < pRange->nRepeatRows)
654                     pRange->nRepeatRows = rFormatRange.nRepeatRows;
655                 pRange->nRepeatColumns += nRepeat;
656                 bInserted = sal_True;
657             }
658         }
659     }
660     if (!bInserted)
661     {
662         ScMyRowFormatRange aRange;
663         aRange.nStartColumn = nPrevStartCol;
664         aRange.nRepeatColumns = nRepeat;
665         aRange.nRepeatRows = rFormatRange.nRepeatRows;
666         aRange.nValidationIndex = rFormatRange.nValidationIndex;
667         aRange.bIsAutoStyle = rFormatRange.bIsAutoStyle;
668         aRange.nIndex = nIndex;
669         aRowFormatRanges.push_back(aRange);
670         ++nSize;
671     }
672 }
673 
AddRange(ScMyRowFormatRange & rFormatRange,const sal_Int32 nRow)674 void ScRowFormatRanges::AddRange(ScMyRowFormatRange& rFormatRange,
675     const sal_Int32 nRow)
676 {
677     DBG_ASSERT(pRowDefaults, "no row defaults");
678     DBG_ASSERT(pColDefaults, "no column defaults");
679     sal_uInt32 nEnd (rFormatRange.nRepeatRows + nRow - 1);
680     sal_Int32 nPrevIndex((*pRowDefaults)[nRow].nIndex);
681     sal_Bool bPrevAutoStyle((*pRowDefaults)[nRow].bIsAutoStyle);
682     sal_uInt32 i(nRow + 1);
683     sal_Bool bReady(sal_False);
684     while ((i < nEnd) && !bReady && (i < pRowDefaults->size()))
685     {
686         if ((nPrevIndex != (*pRowDefaults)[i].nIndex) ||
687             (bPrevAutoStyle != (*pRowDefaults)[i].bIsAutoStyle))
688             bReady = sal_True;
689         else
690             i += (*pRowDefaults)[i].nRepeat;
691     }
692     if (i > nEnd)
693         i = nEnd;
694     if (bReady)
695         rFormatRange.nRepeatRows = i - nRow + 1;
696     if (nPrevIndex == -1)
697     {
698         nPrevIndex = (*pColDefaults)[rFormatRange.nStartColumn].nIndex;
699         bPrevAutoStyle = (*pColDefaults)[rFormatRange.nStartColumn].bIsAutoStyle;
700         sal_uInt32 nPrevStartCol(rFormatRange.nStartColumn);
701         sal_uInt32 nRepeat((*pColDefaults)[rFormatRange.nStartColumn].nRepeat);
702         nEnd = rFormatRange.nStartColumn + rFormatRange.nRepeatColumns;
703         for(i = nPrevStartCol + nRepeat; i < nEnd; i += (*pColDefaults)[i].nRepeat)
704         {
705             DBG_ASSERT(sal_uInt32(nPrevStartCol + nRepeat) <= nEnd, "something wents wrong");
706             if ((nPrevIndex != (*pColDefaults)[i].nIndex) ||
707                 (bPrevAutoStyle != (*pColDefaults)[i].bIsAutoStyle))
708             {
709                 AddRange(nPrevStartCol, nRepeat, nPrevIndex, bPrevAutoStyle, rFormatRange);
710                 nPrevStartCol = i;
711                 nRepeat = (*pColDefaults)[i].nRepeat;
712                 nPrevIndex = (*pColDefaults)[i].nIndex;
713                 bPrevAutoStyle = (*pColDefaults)[i].bIsAutoStyle;
714             }
715             else
716                 nRepeat += (*pColDefaults)[i].nRepeat;
717         }
718         if (sal_uInt32(nPrevStartCol + nRepeat) > nEnd)
719             nRepeat = nEnd - nPrevStartCol;
720         AddRange(nPrevStartCol, nRepeat, nPrevIndex, bPrevAutoStyle, rFormatRange);
721     }
722     else if ((nPrevIndex == rFormatRange.nIndex) &&
723         (bPrevAutoStyle == rFormatRange.bIsAutoStyle))
724     {
725         rFormatRange.nIndex = -1;
726         aRowFormatRanges.push_back(rFormatRange);
727         ++nSize;
728     }
729 }
730 
GetNext(ScMyRowFormatRange & aFormatRange)731 sal_Bool ScRowFormatRanges::GetNext(ScMyRowFormatRange& aFormatRange)
732 {
733     ScMyRowFormatRangesList::iterator aItr(aRowFormatRanges.begin());
734     if (aItr != aRowFormatRanges.end())
735     {
736         aFormatRange = (*aItr);
737         aRowFormatRanges.erase(aItr);
738         --nSize;
739         return sal_True;
740     }
741     return sal_False;
742 }
743 
GetMaxRows()744 sal_Int32 ScRowFormatRanges::GetMaxRows()
745 {
746     ScMyRowFormatRangesList::iterator aItr(aRowFormatRanges.begin());
747     ScMyRowFormatRangesList::iterator aEndItr(aRowFormatRanges.end());
748     sal_Int32 nMaxRows = MAXROW + 1;
749     if (aItr != aEndItr)
750         while (aItr != aEndItr)
751         {
752             if ((*aItr).nRepeatRows < nMaxRows)
753                 nMaxRows = (*aItr).nRepeatRows;
754             ++aItr;
755         }
756     else
757     {
758         DBG_ERROR("no ranges found");
759     }
760     return nMaxRows;
761 }
762 
GetSize()763 sal_Int32 ScRowFormatRanges::GetSize()
764 {
765     return nSize;
766 }
767 
Sort()768 void ScRowFormatRanges::Sort()
769 {
770     aRowFormatRanges.sort();
771 }
772 
773 // ============================================================================
ScMyFormatRange()774 ScMyFormatRange::ScMyFormatRange()
775     : nStyleNameIndex(-1),
776     nValidationIndex(-1),
777     bIsAutoStyle(sal_True)
778 {
779 }
780 
operator <(const ScMyFormatRange & rRange) const781 sal_Bool ScMyFormatRange::operator<(const ScMyFormatRange& rRange) const
782 {
783     if (aRangeAddress.StartRow < rRange.aRangeAddress.StartRow)
784         return sal_True;
785     else
786         if (aRangeAddress.StartRow == rRange.aRangeAddress.StartRow)
787             return (aRangeAddress.StartColumn < rRange.aRangeAddress.StartColumn);
788         else
789             return sal_False;
790 }
791 
ScFormatRangeStyles()792 ScFormatRangeStyles::ScFormatRangeStyles()
793     : aTables(),
794     aStyleNames(),
795     aAutoStyleNames()
796 {
797 }
798 
~ScFormatRangeStyles()799 ScFormatRangeStyles::~ScFormatRangeStyles()
800 {
801     ScMyOUStringVec::iterator i(aStyleNames.begin());
802     ScMyOUStringVec::iterator endi(aStyleNames.end());
803     while (i != endi)
804     {
805         delete *i;
806         ++i;
807     }
808     i = aAutoStyleNames.begin();
809     endi = aAutoStyleNames.end();
810     while (i != endi)
811     {
812         delete *i;
813         ++i;
814     }
815     ScMyFormatRangeListVec::iterator j(aTables.begin());
816     ScMyFormatRangeListVec::iterator endj(aTables.end());
817     while (j != endj)
818     {
819         delete *j;
820         ++j;
821     }
822 }
823 
AddNewTable(const sal_Int32 nTable)824 void ScFormatRangeStyles::AddNewTable(const sal_Int32 nTable)
825 {
826     sal_Int32 nSize = aTables.size() - 1;
827     if (nTable > nSize)
828         for (sal_Int32 i = nSize; i < nTable; ++i)
829         {
830             ScMyFormatRangeAddresses* aRangeAddresses(new ScMyFormatRangeAddresses);
831             aTables.push_back(aRangeAddresses);
832         }
833 }
834 
AddStyleName(rtl::OUString * rpString,sal_Int32 & rIndex,const sal_Bool bIsAutoStyle)835 sal_Bool ScFormatRangeStyles::AddStyleName(rtl::OUString* rpString, sal_Int32& rIndex, const sal_Bool bIsAutoStyle)
836 {
837     if (bIsAutoStyle)
838     {
839         aAutoStyleNames.push_back(rpString);
840         rIndex = aAutoStyleNames.size() - 1;
841         return sal_True;
842     }
843     else
844     {
845         sal_Int32 nCount(aStyleNames.size());
846         sal_Bool bFound(sal_False);
847         sal_Int32 i(nCount - 1);
848         while ((i >= 0) && (!bFound))
849         {
850             if (aStyleNames.at(i)->equals(*rpString))
851                 bFound = sal_True;
852             else
853                 i--;
854         }
855         if (bFound)
856         {
857             rIndex = i;
858             return sal_False;
859         }
860         else
861         {
862             aStyleNames.push_back(rpString);
863             rIndex = aStyleNames.size() - 1;
864             return sal_True;
865         }
866     }
867 }
868 
GetIndexOfStyleName(const rtl::OUString & rString,const rtl::OUString & rPrefix,sal_Bool & bIsAutoStyle)869 sal_Int32 ScFormatRangeStyles::GetIndexOfStyleName(const rtl::OUString& rString, const rtl::OUString& rPrefix, sal_Bool& bIsAutoStyle)
870 {
871     sal_Int32 nPrefixLength(rPrefix.getLength());
872     rtl::OUString sTemp(rString.copy(nPrefixLength));
873     sal_Int32 nIndex(sTemp.toInt32());
874     if (nIndex > 0 && static_cast<size_t>(nIndex-1) < aAutoStyleNames.size() && aAutoStyleNames.at(nIndex - 1)->equals(rString))
875     {
876         bIsAutoStyle = sal_True;
877         return nIndex - 1;
878     }
879     else
880     {
881         sal_Int32 i(0);
882         sal_Bool bFound(sal_False);
883         while (!bFound && static_cast<size_t>(i) < aStyleNames.size())
884         {
885             if (aStyleNames[i]->equals(rString))
886                 bFound = sal_True;
887             else
888                 ++i;
889         }
890         if (bFound)
891         {
892             bIsAutoStyle = sal_False;
893             return i;
894         }
895         else
896         {
897             i = 0;
898             while (!bFound && static_cast<size_t>(i) < aAutoStyleNames.size())
899             {
900                 if (aAutoStyleNames[i]->equals(rString))
901                     bFound = sal_True;
902                 else
903                     ++i;
904             }
905             if (bFound)
906             {
907                 bIsAutoStyle = sal_True;
908                 return i;
909             }
910             else
911                 return -1;
912         }
913     }
914 }
915 
GetStyleNameIndex(const sal_Int32 nTable,const sal_Int32 nColumn,const sal_Int32 nRow,sal_Bool & bIsAutoStyle) const916 sal_Int32 ScFormatRangeStyles::GetStyleNameIndex(const sal_Int32 nTable,
917     const sal_Int32 nColumn, const sal_Int32 nRow, sal_Bool& bIsAutoStyle) const
918 {
919     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
920     ScMyFormatRangeAddresses* pFormatRanges(aTables[nTable]);
921     ScMyFormatRangeAddresses::iterator aItr(pFormatRanges->begin());
922     ScMyFormatRangeAddresses::iterator aEndItr(pFormatRanges->end());
923     while (aItr != aEndItr)
924     {
925         if (((*aItr).aRangeAddress.StartColumn <= nColumn) &&
926             ((*aItr).aRangeAddress.EndColumn >= nColumn) &&
927             ((*aItr).aRangeAddress.StartRow <= nRow) &&
928             ((*aItr).aRangeAddress.EndRow >= nRow))
929         {
930             bIsAutoStyle = aItr->bIsAutoStyle;
931             return (*aItr).nStyleNameIndex;
932         }
933         else
934             ++aItr;
935     }
936     return -1;
937 }
938 
GetStyleNameIndex(const sal_Int32 nTable,const sal_Int32 nColumn,const sal_Int32 nRow,sal_Bool & bIsAutoStyle,sal_Int32 & nValidationIndex,sal_Int32 & nNumberFormat,const sal_Int32 nRemoveBeforeRow)939 sal_Int32 ScFormatRangeStyles::GetStyleNameIndex(const sal_Int32 nTable, const sal_Int32 nColumn, const sal_Int32 nRow,
940     sal_Bool& bIsAutoStyle, sal_Int32& nValidationIndex, sal_Int32& nNumberFormat, const sal_Int32 nRemoveBeforeRow)
941 {
942     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
943     ScMyFormatRangeAddresses* pFormatRanges(aTables[nTable]);
944     ScMyFormatRangeAddresses::iterator aItr(pFormatRanges->begin());
945     ScMyFormatRangeAddresses::iterator aEndItr(pFormatRanges->end());
946     while (aItr != aEndItr)
947     {
948         if (((*aItr).aRangeAddress.StartColumn <= nColumn) &&
949             ((*aItr).aRangeAddress.EndColumn >= nColumn) &&
950             ((*aItr).aRangeAddress.StartRow <= nRow) &&
951             ((*aItr).aRangeAddress.EndRow >= nRow))
952         {
953             bIsAutoStyle = aItr->bIsAutoStyle;
954             nValidationIndex = aItr->nValidationIndex;
955             nNumberFormat = aItr->nNumberFormat;
956             if (((*pRowDefaults)[nRow].nIndex != -1))
957             {
958                 if (((*pRowDefaults)[nRow].nIndex == (*aItr).nStyleNameIndex) &&
959                     ((*pRowDefaults)[nRow].bIsAutoStyle == (*aItr).bIsAutoStyle))
960                     return -1;
961                 else
962                     return (*aItr).nStyleNameIndex;
963             }
964             else if (((*pColDefaults)[nColumn].nIndex != -1) &&
965                 ((*pColDefaults)[nColumn].nIndex == (*aItr).nStyleNameIndex) &&
966                 ((*pColDefaults)[nColumn].bIsAutoStyle == (*aItr).bIsAutoStyle))
967                 return -1;
968             else
969                 return (*aItr).nStyleNameIndex;
970         }
971         else
972         {
973             if ((*aItr).aRangeAddress.EndRow < nRemoveBeforeRow)
974                 aItr = pFormatRanges->erase(aItr);
975             else
976                 ++aItr;
977         }
978     }
979     return -1;
980 }
981 
GetFormatRanges(const sal_Int32 nStartColumn,const sal_Int32 nEndColumn,const sal_Int32 nRow,const sal_Int32 nTable,ScRowFormatRanges * pRowFormatRanges)982 void ScFormatRangeStyles::GetFormatRanges(const sal_Int32 nStartColumn, const sal_Int32 nEndColumn, const sal_Int32 nRow,
983                     const sal_Int32 nTable, ScRowFormatRanges* pRowFormatRanges)
984 {
985     sal_Int32 nTotalColumns(nEndColumn - nStartColumn + 1);
986     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
987     ScMyFormatRangeAddresses* pFormatRanges(aTables[nTable]);
988     ScMyFormatRangeAddresses::iterator aItr(pFormatRanges->begin());
989     ScMyFormatRangeAddresses::iterator aEndItr(pFormatRanges->end());
990     sal_Int32 nColumns = 0;
991     while (aItr != aEndItr && nColumns < nTotalColumns)
992     {
993 #if OSL_DEBUG_LEVEL > 1
994         table::CellRangeAddress aTempRangeAddress((*aItr).aRangeAddress);
995 #endif
996         if (((*aItr).aRangeAddress.StartRow <= nRow) &&
997             ((*aItr).aRangeAddress.EndRow >= nRow))
998         {
999             if ((((*aItr).aRangeAddress.StartColumn <= nStartColumn) &&
1000                 ((*aItr).aRangeAddress.EndColumn >= nStartColumn)) ||
1001                 (((*aItr).aRangeAddress.StartColumn <= nEndColumn) &&
1002                 ((*aItr).aRangeAddress.EndColumn >= nEndColumn)) ||
1003                 (((*aItr).aRangeAddress.StartColumn >= nStartColumn) &&
1004                 ((*aItr).aRangeAddress.EndColumn <= nEndColumn)))
1005             {
1006                 ScMyRowFormatRange aRange;
1007                 aRange.nIndex = aItr->nStyleNameIndex;
1008                 aRange.nValidationIndex = aItr->nValidationIndex;
1009                 aRange.bIsAutoStyle = aItr->bIsAutoStyle;
1010                 if ((aItr->aRangeAddress.StartColumn < nStartColumn) &&
1011                     (aItr->aRangeAddress.EndColumn >= nStartColumn))
1012                 {
1013                     if (aItr->aRangeAddress.EndColumn >= nEndColumn)
1014                         aRange.nRepeatColumns = nTotalColumns;
1015                     else
1016                         aRange.nRepeatColumns = aItr->aRangeAddress.EndColumn - nStartColumn + 1;
1017                     aRange.nStartColumn = nStartColumn;
1018                 }
1019                 else if ((aItr->aRangeAddress.StartColumn >= nStartColumn) &&
1020                     (aItr->aRangeAddress.EndColumn <= nEndColumn))
1021                 {
1022                     aRange.nRepeatColumns = aItr->aRangeAddress.EndColumn - aItr->aRangeAddress.StartColumn + 1;
1023                     aRange.nStartColumn = aItr->aRangeAddress.StartColumn;
1024                 }
1025                 else if ((aItr->aRangeAddress.StartColumn >= nStartColumn) &&
1026                     (aItr->aRangeAddress.StartColumn <= nEndColumn) &&
1027                     (aItr->aRangeAddress.EndColumn > nEndColumn))
1028                 {
1029                     aRange.nRepeatColumns = nEndColumn - aItr->aRangeAddress.StartColumn + 1;
1030                     aRange.nStartColumn = aItr->aRangeAddress.StartColumn;
1031                 }
1032                 aRange.nRepeatRows = aItr->aRangeAddress.EndRow - nRow + 1;
1033                 pRowFormatRanges->AddRange(aRange, nRow);
1034                 nColumns += aRange.nRepeatColumns;
1035             }
1036             ++aItr;
1037         }
1038         else
1039             if(aItr->aRangeAddress.EndRow < nRow)
1040                 aItr = pFormatRanges->erase(aItr);
1041             else
1042                 ++aItr;
1043     }
1044     pRowFormatRanges->Sort();
1045 }
1046 
AddRangeStyleName(const table::CellRangeAddress aCellRangeAddress,const sal_Int32 nStringIndex,const sal_Bool bIsAutoStyle,const sal_Int32 nValidationIndex,const sal_Int32 nNumberFormat)1047 void ScFormatRangeStyles::AddRangeStyleName(const table::CellRangeAddress aCellRangeAddress,
1048     const sal_Int32 nStringIndex, const sal_Bool bIsAutoStyle, const sal_Int32 nValidationIndex,
1049     const sal_Int32 nNumberFormat)
1050 {
1051     ScMyFormatRange aFormatRange;
1052     aFormatRange.aRangeAddress = aCellRangeAddress;
1053     aFormatRange.nStyleNameIndex = nStringIndex;
1054     aFormatRange.nValidationIndex = nValidationIndex;
1055     aFormatRange.nNumberFormat = nNumberFormat;
1056     aFormatRange.bIsAutoStyle = bIsAutoStyle;
1057     DBG_ASSERT(static_cast<size_t>(aCellRangeAddress.Sheet) < aTables.size(), "wrong table");
1058     ScMyFormatRangeAddresses* pFormatRanges(aTables[aCellRangeAddress.Sheet]);
1059     pFormatRanges->push_back(aFormatRange);
1060 }
1061 
GetStyleNameByIndex(const sal_Int32 nIndex,const sal_Bool bIsAutoStyle)1062 rtl::OUString* ScFormatRangeStyles::GetStyleNameByIndex(const sal_Int32 nIndex, const sal_Bool bIsAutoStyle)
1063 {
1064     if (bIsAutoStyle)
1065         return aAutoStyleNames[nIndex];
1066     else
1067         return aStyleNames[nIndex];
1068 }
1069 
Sort()1070 void ScFormatRangeStyles::Sort()
1071 {
1072     sal_Int32 nTables = aTables.size();
1073     for (sal_Int16 i = 0; i < nTables; ++i)
1074         if (!aTables[i]->empty())
1075             aTables[i]->sort();
1076 }
1077 
1078 //===========================================================================
1079 
ScColumnRowStylesBase()1080 ScColumnRowStylesBase::ScColumnRowStylesBase()
1081     : aStyleNames()
1082 {
1083 }
1084 
~ScColumnRowStylesBase()1085 ScColumnRowStylesBase::~ScColumnRowStylesBase()
1086 {
1087     ScMyOUStringVec::iterator i(aStyleNames.begin());
1088     ScMyOUStringVec::iterator endi(aStyleNames.end());
1089     while (i != endi)
1090     {
1091         delete *i;
1092         ++i;
1093     }
1094 }
1095 
AddStyleName(rtl::OUString * pString)1096 sal_Int32 ScColumnRowStylesBase::AddStyleName(rtl::OUString* pString)
1097 {
1098     aStyleNames.push_back(pString);
1099     return aStyleNames.size() - 1;
1100 }
1101 
GetIndexOfStyleName(const rtl::OUString & rString,const rtl::OUString & rPrefix)1102 sal_Int32 ScColumnRowStylesBase::GetIndexOfStyleName(const rtl::OUString& rString, const rtl::OUString& rPrefix)
1103 {
1104     sal_Int32 nPrefixLength(rPrefix.getLength());
1105     rtl::OUString sTemp(rString.copy(nPrefixLength));
1106     sal_Int32 nIndex(sTemp.toInt32());
1107     if (nIndex > 0 && static_cast<size_t>(nIndex-1) < aStyleNames.size() && aStyleNames.at(nIndex - 1)->equals(rString))
1108         return nIndex - 1;
1109     else
1110     {
1111         sal_Int32 i(0);
1112         sal_Bool bFound(sal_False);
1113         while (!bFound && static_cast<size_t>(i) < aStyleNames.size())
1114         {
1115             if (aStyleNames.at(i)->equals(rString))
1116                 bFound = sal_True;
1117             else
1118                 ++i;
1119         }
1120         if (bFound)
1121             return i;
1122         else
1123             return -1;
1124     }
1125 }
1126 
GetStyleNameByIndex(const sal_Int32 nIndex)1127 rtl::OUString* ScColumnRowStylesBase::GetStyleNameByIndex(const sal_Int32 nIndex)
1128 {
1129     if ( nIndex < 0 || nIndex >= sal::static_int_cast<sal_Int32>( aStyleNames.size() ) )
1130     {
1131         // #123981# should no longer happen, use first style then
1132         DBG_ERRORFILE("GetStyleNameByIndex: invalid index");
1133         return aStyleNames[0];
1134     }
1135 
1136     return aStyleNames[nIndex];
1137 }
1138 
1139 //===========================================================================
1140 
ScColumnStyles()1141 ScColumnStyles::ScColumnStyles()
1142     : ScColumnRowStylesBase(),
1143     aTables()
1144 {
1145 }
1146 
~ScColumnStyles()1147 ScColumnStyles::~ScColumnStyles()
1148 {
1149 }
1150 
AddNewTable(const sal_Int32 nTable,const sal_Int32 nFields)1151 void ScColumnStyles::AddNewTable(const sal_Int32 nTable, const sal_Int32 nFields)
1152 {
1153     sal_Int32 nSize(aTables.size() - 1);
1154     if (nTable > nSize)
1155         for (sal_Int32 i = nSize; i < nTable; ++i)
1156         {
1157             ScMyColumnStyleVec aFieldsVec(nFields + 1, ScColumnStyle());
1158             aTables.push_back(aFieldsVec);
1159         }
1160 }
1161 
GetStyleNameIndex(const sal_Int32 nTable,const sal_Int32 nField,sal_Bool & bIsVisible)1162 sal_Int32 ScColumnStyles::GetStyleNameIndex(const sal_Int32 nTable, const sal_Int32 nField,
1163     sal_Bool& bIsVisible)
1164 {
1165     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
1166     if (static_cast<size_t>(nField) < aTables[nTable].size())
1167     {
1168         bIsVisible = aTables[nTable][nField].bIsVisible;
1169         return aTables[nTable][nField].nIndex;
1170     }
1171     else
1172     {
1173         bIsVisible = aTables[nTable][aTables[nTable].size() - 1].bIsVisible;
1174         return aTables[nTable][aTables[nTable].size() - 1].nIndex;
1175     }
1176 }
1177 
AddFieldStyleName(const sal_Int32 nTable,const sal_Int32 nField,const sal_Int32 nStringIndex,const sal_Bool bIsVisible)1178 void ScColumnStyles::AddFieldStyleName(const sal_Int32 nTable, const sal_Int32 nField,
1179     const sal_Int32 nStringIndex, const sal_Bool bIsVisible)
1180 {
1181     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
1182     DBG_ASSERT(aTables[nTable].size() >= static_cast<sal_uInt32>(nField), "wrong field");
1183     ScColumnStyle aStyle;
1184     aStyle.nIndex = nStringIndex;
1185     aStyle.bIsVisible = bIsVisible;
1186     if (aTables[nTable].size() == static_cast<sal_uInt32>(nField))
1187         aTables[nTable].push_back(aStyle);
1188     aTables[nTable][nField] = aStyle;
1189 }
1190 
GetStyleName(const sal_Int32 nTable,const sal_Int32 nField)1191 rtl::OUString* ScColumnStyles::GetStyleName(const sal_Int32 nTable, const sal_Int32 nField)
1192 {
1193     sal_Bool bTemp;
1194     return GetStyleNameByIndex(GetStyleNameIndex(nTable, nField, bTemp));
1195 }
1196 
1197 //===========================================================================
1198 
ScRowStyles()1199 ScRowStyles::ScRowStyles()
1200     : ScColumnRowStylesBase(),
1201     aTables()
1202 {
1203 }
1204 
~ScRowStyles()1205 ScRowStyles::~ScRowStyles()
1206 {
1207 }
1208 
AddNewTable(const sal_Int32 nTable,const sal_Int32 nFields)1209 void ScRowStyles::AddNewTable(const sal_Int32 nTable, const sal_Int32 nFields)
1210 {
1211     sal_Int32 nSize(aTables.size() - 1);
1212     if (nTable > nSize)
1213         for (sal_Int32 i = nSize; i < nTable; ++i)
1214         {
1215             ScMysalInt32Vec aFieldsVec(nFields + 1, -1);
1216             aTables.push_back(aFieldsVec);
1217         }
1218 }
1219 
GetStyleNameIndex(const sal_Int32 nTable,const sal_Int32 nField)1220 sal_Int32 ScRowStyles::GetStyleNameIndex(const sal_Int32 nTable, const sal_Int32 nField)
1221 {
1222     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
1223     if (static_cast<size_t>(nField) < aTables[nTable].size())
1224         return aTables[nTable][nField];
1225     else
1226         return aTables[nTable][aTables[nTable].size() - 1];
1227 }
1228 
AddFieldStyleName(const sal_Int32 nTable,const sal_Int32 nField,const sal_Int32 nStringIndex)1229 void ScRowStyles::AddFieldStyleName(const sal_Int32 nTable, const sal_Int32 nField,
1230     const sal_Int32 nStringIndex)
1231 {
1232     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
1233     DBG_ASSERT(aTables[nTable].size() >= static_cast<size_t>(nField), "wrong field");
1234     if (aTables[nTable].size() == static_cast<size_t>(nField))
1235         aTables[nTable].push_back(nStringIndex);
1236     else
1237         aTables[nTable][nField] = nStringIndex;
1238 }
1239 
AddFieldStyleName(const sal_Int32 nTable,const sal_Int32 nStartField,const sal_Int32 nStringIndex,const sal_Int32 nEndField)1240 void ScRowStyles::AddFieldStyleName(const sal_Int32 nTable, const sal_Int32 nStartField,
1241         const sal_Int32 nStringIndex, const sal_Int32 nEndField)
1242 {
1243     DBG_ASSERT( nStartField <= nEndField, "bad field range");
1244     DBG_ASSERT(static_cast<size_t>(nTable) < aTables.size(), "wrong table");
1245     DBG_ASSERT(aTables[nTable].size() >= static_cast<size_t>(nStartField), "wrong field");
1246     ScMysalInt32Vec& rTable = aTables[nTable];
1247     size_t nSize = rTable.size();
1248     if (nSize == static_cast<size_t>(nStartField))
1249         rTable.insert( rTable.end(), static_cast<size_t>(nEndField - nStartField + 1), nStringIndex);
1250     else
1251     {
1252         size_t nField = static_cast<size_t>(nStartField);
1253         for ( ; nField < nSize && nField <= static_cast<size_t>(nEndField); ++nField)
1254             rTable[nField] = nStringIndex;
1255         if (nField <= static_cast<size_t>(nEndField))
1256             rTable.insert( rTable.end(), static_cast<size_t>(nEndField - nField + 1), nStringIndex);
1257     }
1258 }
1259 
GetStyleName(const sal_Int32 nTable,const sal_Int32 nField)1260 rtl::OUString* ScRowStyles::GetStyleName(const sal_Int32 nTable, const sal_Int32 nField)
1261 {
1262     return GetStyleNameByIndex(GetStyleNameIndex(nTable, nField));
1263 }
1264