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