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