1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef SC_CONDITIO_HXX
21 #define SC_CONDITIO_HXX
24 #include "address.hxx"
25 #include "formula/grammar.hxx"
27 #include "rangelst.hxx"
29 #include <rtl/math.hxx>
30 #include <tools/date.hxx>
34 #include <boost/noncopyable.hpp>
35 #include <boost/ptr_container/ptr_set.hpp>
36 #include <boost/ptr_container/ptr_vector.hpp>
37 #include <boost/scoped_ptr.hpp>
41 struct ScRefCellValue
;
45 struct RefUpdateContext
;
46 struct RefUpdateInsertTabContext
;
47 struct RefUpdateDeleteTabContext
;
48 struct RefUpdateMoveTabContext
;
53 #define SC_COND_NOBLANKS 1
71 SC_COND_BOTTOM_PERCENT
,
72 SC_COND_ABOVE_AVERAGE
,
73 SC_COND_BELOW_AVERAGE
,
74 SC_COND_ABOVE_EQUAL_AVERAGE
,
75 SC_COND_BELOW_EQUAL_AVERAGE
,
80 SC_COND_CONTAINS_TEXT
,
81 SC_COND_NOT_CONTAINS_TEXT
,
85 class ScConditionalFormat
;
92 enum ScFormatEntryType
103 struct ScCondFormatData
111 ScDataBarInfo
* pDataBar
;
112 ScIconSetInfo
* pIconSet
;
116 class SC_DLLPUBLIC ScFormatEntry
119 ScFormatEntry(ScDocument
* pDoc
);
120 virtual ~ScFormatEntry() {}
122 virtual condformat::ScFormatEntryType
GetType() const = 0;
123 virtual void UpdateReference( sc::RefUpdateContext
& rCxt
) = 0;
124 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
) = 0;
125 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
) = 0;
126 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
) = 0;
128 virtual ScFormatEntry
* Clone( ScDocument
* pDoc
= NULL
) const = 0;
130 virtual void SetParent( ScConditionalFormat
* pNew
) = 0;
132 bool operator==( const ScFormatEntry
& ) const;
134 virtual void startRendering();
135 virtual void endRendering();
141 class approx_less
: public std::binary_function
<double, double, bool>
144 bool operator() (double nVal1
, double nVal2
) const
146 if(nVal1
< nVal2
&& !rtl::math::approxEqual(nVal1
, nVal2
))
153 class SC_DLLPUBLIC ScConditionEntry
: public ScFormatEntry
158 double nVal1
; // input or calculated
160 OUString aStrVal1
; // input or calculated
162 OUString aStrNmsp1
; // namespace to be used on (re)compilation, e.g. in XML import
163 OUString aStrNmsp2
; // namespace to be used on (re)compilation, e.g. in XML import
164 formula::FormulaGrammar::Grammar eTempGrammar1
; // grammar to be used on (re)compilation, e.g. in XML import
165 formula::FormulaGrammar::Grammar eTempGrammar2
; // grammar to be used on (re)compilation, e.g. in XML import
166 bool bIsStr1
; // for recognition of empty strings
168 ScTokenArray
* pFormula1
; // entered formula
169 ScTokenArray
* pFormula2
;
170 ScAddress aSrcPos
; // source position for formulas
172 OUString aSrcString
; // formula source position as text during XML import
173 ScFormulaCell
* pFCell1
;
174 ScFormulaCell
* pFCell2
;
179 void MakeCells( const ScAddress
& rPos
);
180 void Compile( const OUString
& rExpr1
, const OUString
& rExpr2
,
181 const OUString
& rExprNmsp1
, const OUString
& rExprNmsp2
,
182 formula::FormulaGrammar::Grammar eGrammar1
,
183 formula::FormulaGrammar::Grammar eGrammar2
,
185 void Interpret( const ScAddress
& rPos
);
187 bool IsValid( double nArg
, const ScAddress
& rPos
) const;
188 bool IsValidStr( const OUString
& rArg
, const ScAddress
& rPos
) const;
191 ScConditionEntry( ScConditionMode eOper
,
192 const OUString
& rExpr1
, const OUString
& rExpr2
,
193 ScDocument
* pDocument
, const ScAddress
& rPos
,
194 const OUString
& rExprNmsp1
, const OUString
& rExprNmsp2
,
195 formula::FormulaGrammar::Grammar eGrammar1
,
196 formula::FormulaGrammar::Grammar eGrammar2
);
197 ScConditionEntry( ScConditionMode eOper
,
198 const ScTokenArray
* pArr1
, const ScTokenArray
* pArr2
,
199 ScDocument
* pDocument
, const ScAddress
& rPos
);
200 ScConditionEntry( const ScConditionEntry
& r
); // flat copy of formulas
201 // true copy of formulas (for Ref-Undo):
202 ScConditionEntry( ScDocument
* pDocument
, const ScConditionEntry
& r
);
203 virtual ~ScConditionEntry();
205 int operator== ( const ScConditionEntry
& r
) const;
207 virtual void SetParent( ScConditionalFormat
* pNew
) { pCondFormat
= pNew
; }
209 bool IsCellValid( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
211 ScConditionMode
GetOperation() const { return eOp
; }
212 bool IsIgnoreBlank() const { return ( nOptions
& SC_COND_NOBLANKS
) == 0; }
213 void SetIgnoreBlank(bool bSet
);
214 ScAddress
GetSrcPos() const { return aSrcPos
; }
216 ScAddress
GetValidSrcPos() const; // adjusted to allow textual representation of expressions
218 void SetSrcString( const OUString
& rNew
); // for XML import
220 void SetFormula1( const ScTokenArray
& rArray
);
221 void SetFormula2( const ScTokenArray
& rArray
);
223 OUString
GetExpression( const ScAddress
& rCursor
, sal_uInt16 nPos
, sal_uLong nNumFmt
= 0,
224 const formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
226 ScTokenArray
* CreateTokenArry( sal_uInt16 nPos
) const;
230 virtual void UpdateReference( sc::RefUpdateContext
& rCxt
);
231 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
);
232 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
);
233 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
);
235 void SourceChanged( const ScAddress
& rChanged
);
237 bool MarkUsedExternalReferences() const;
239 virtual condformat::ScFormatEntryType
GetType() const { return condformat::CONDITION
; }
241 virtual ScFormatEntry
* Clone(ScDocument
* pDoc
= NULL
) const;
243 static ScConditionMode
GetModeFromApi(sal_Int32 nOperator
);
245 virtual void endRendering();
246 virtual void startRendering();
249 virtual void DataChanged( const ScRange
* pModified
) const;
250 ScDocument
* GetDocument() const { return mpDoc
; }
251 ScConditionalFormat
* pCondFormat
;
255 bool IsDuplicate(double nArg
, const OUString
& rStr
) const;
256 bool IsTopNElement( double nArg
) const;
257 bool IsTopNPercent( double nArg
) const;
258 bool IsBottomNElement( double nArg
) const;
259 bool IsBottomNPercent( double nArg
) const;
260 bool IsAboveAverage( double nArg
, bool bEqual
) const;
261 bool IsBelowAverage( double nArg
, bool bEqual
) const;
263 bool IsError( const ScAddress
& rPos
) const;
265 void FillCache() const;
267 struct ScConditionEntryCache
269 typedef std::map
<OUString
, sal_Int32
> StringCacheType
;
270 StringCacheType maStrings
;
271 typedef std::map
<double, sal_Int32
, approx_less
> ValueCacheType
;
272 ValueCacheType maValues
;
274 // cache them for easier access
277 ScConditionEntryCache():
281 mutable boost::scoped_ptr
<ScConditionEntryCache
> mpCache
;
285 // single entry for conditional formatting
288 class SC_DLLPUBLIC ScCondFormatEntry
: public ScConditionEntry
292 using ScConditionEntry::operator==;
295 ScCondFormatEntry( ScConditionMode eOper
,
296 const OUString
& rExpr1
, const OUString
& rExpr2
,
297 ScDocument
* pDocument
, const ScAddress
& rPos
,
298 const OUString
& rStyle
,
299 const OUString
& rExprNmsp1
= EMPTY_OUSTRING
,
300 const OUString
& rExprNmsp2
= EMPTY_OUSTRING
,
301 formula::FormulaGrammar::Grammar eGrammar1
= formula::FormulaGrammar::GRAM_DEFAULT
,
302 formula::FormulaGrammar::Grammar eGrammar2
= formula::FormulaGrammar::GRAM_DEFAULT
);
303 ScCondFormatEntry( ScConditionMode eOper
,
304 const ScTokenArray
* pArr1
, const ScTokenArray
* pArr2
,
305 ScDocument
* pDocument
, const ScAddress
& rPos
,
306 const OUString
& rStyle
);
307 ScCondFormatEntry( const ScCondFormatEntry
& r
);
308 ScCondFormatEntry( ScDocument
* pDocument
, const ScCondFormatEntry
& r
);
309 virtual ~ScCondFormatEntry();
311 int operator== ( const ScCondFormatEntry
& r
) const;
313 const OUString
& GetStyle() const { return aStyleName
; }
314 void UpdateStyleName(const OUString
& rNew
) { aStyleName
=rNew
; }
315 virtual ScFormatEntry
* Clone(ScDocument
* pDoc
) const;
318 virtual void DataChanged( const ScRange
* pModified
) const;
321 namespace condformat
{
323 enum ScCondFormatDateType
342 class SC_DLLPUBLIC ScCondDateFormatEntry
: public ScFormatEntry
345 ScCondDateFormatEntry(ScDocument
* pDoc
);
346 ScCondDateFormatEntry(ScDocument
* pDoc
, const ScCondDateFormatEntry
& rEntry
);
348 bool IsValid( const ScAddress
& rPos
) const;
350 void SetDateType(condformat::ScCondFormatDateType eType
);
351 condformat::ScCondFormatDateType
GetDateType() const;
353 const OUString
& GetStyleName() const;
354 void SetStyleName( const OUString
& rStyleName
);
356 virtual condformat::ScFormatEntryType
GetType() const { return condformat::DATE
; }
357 virtual void UpdateReference( sc::RefUpdateContext
& ) {}
358 virtual void UpdateInsertTab( sc::RefUpdateInsertTabContext
& ) {}
359 virtual void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& ) {}
360 virtual void UpdateMoveTab( sc::RefUpdateMoveTabContext
& ) {}
362 virtual ScFormatEntry
* Clone( ScDocument
* pDoc
= NULL
) const;
364 virtual void SetParent( ScConditionalFormat
* ) {}
366 bool operator==( const ScFormatEntry
& ) const;
368 virtual void startRendering();
369 virtual void endRendering();
372 condformat::ScCondFormatDateType meType
;
374 mutable boost::scoped_ptr
<Date
> mpCache
;
376 OUString maStyleName
;
380 // complete conditional formatting
383 class SC_DLLPUBLIC ScConditionalFormat
: private boost::noncopyable
386 sal_uInt32 nKey
; // Index in attributes
388 typedef boost::ptr_vector
<ScFormatEntry
> CondFormatContainer
;
389 CondFormatContainer maEntries
;
390 ScRangeList maRanges
; // Ranges for conditional format
393 ScConditionalFormat(sal_uInt32 nNewKey
, ScDocument
* pDocument
);
394 ~ScConditionalFormat();
396 // true copy of formulas (for Ref-Undo / between documents)
397 ScConditionalFormat
* Clone(ScDocument
* pNewDoc
= NULL
) const;
399 void AddEntry( ScFormatEntry
* pNew
);
400 void AddRange( const ScRangeList
& rRanges
);
401 const ScRangeList
& GetRange() const { return maRanges
; }
402 // don't use the same name as for the const version
403 ScRangeList
& GetRangeList() { return maRanges
; }
405 ScDocument
* GetDocument() { return pDoc
; }
407 bool IsEmpty() const;
412 void UpdateReference( sc::RefUpdateContext
& rCxt
, bool bCopyAsMove
= false );
413 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
);
414 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
);
415 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
);
417 void InsertRow(SCTAB nTab
, SCCOL nColStart
, SCCOL nColEnd
, SCROW nRowStart
, SCSIZE nSize
);
418 void InsertCol(SCTAB nTab
, SCROW nRowStart
, SCROW nRowEnd
, SCCOL nColStart
, SCSIZE nSize
);
420 void DeleteArea( SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
);
421 void RenameCellStyle( const OUString
& rOld
, const OUString
& rNew
);
423 void SourceChanged( const ScAddress
& rAddr
);
425 const ScFormatEntry
* GetEntry( sal_uInt16 nPos
) const;
427 const OUString
& GetCellStyle( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
429 ScCondFormatData
GetData( ScRefCellValue
& rCell
, const ScAddress
& rPos
) const;
431 bool EqualEntries( const ScConditionalFormat
& r
) const;
433 void DoRepaint( const ScRange
* pModified
);
435 sal_uInt32
GetKey() const { return nKey
; }
436 void SetKey(sal_uInt32 nNew
) { nKey
= nNew
; } // only if not inserted!
438 bool MarkUsedExternalReferences() const;
440 // sorted (via PTRARR) by Index
441 // operator== only for sorting
442 bool operator ==( const ScConditionalFormat
& r
) const { return nKey
== r
.nKey
; }
443 bool operator < ( const ScConditionalFormat
& r
) const { return nKey
< r
.nKey
; }
445 void startRendering();
450 // List of areas and formats:
453 class SC_DLLPUBLIC ScConditionalFormatList
456 typedef boost::ptr_set
<ScConditionalFormat
> ConditionalFormatContainer
;
457 ConditionalFormatContainer maConditionalFormats
;
459 void operator =(ScConditionalFormatList
const &) SAL_DELETED_FUNCTION
;
462 ScConditionalFormatList() {}
463 ScConditionalFormatList(const ScConditionalFormatList
& rList
);
464 ScConditionalFormatList(ScDocument
* pDoc
, const ScConditionalFormatList
& rList
);
465 ~ScConditionalFormatList() {}
467 void InsertNew( ScConditionalFormat
* pNew
);
470 * Checks that all cond formats have a non empty range.
471 * Deletes empty cond formats.
472 * @return true if all cond formats were valid
474 bool CheckAllEntries();
476 ScConditionalFormat
* GetFormat( sal_uInt32 nKey
);
477 const ScConditionalFormat
* GetFormat( sal_uInt32 nKey
) const;
481 void UpdateReference( sc::RefUpdateContext
& rCxt
);
482 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
);
483 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
);
484 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
);
486 void InsertRow(SCTAB nTab
, SCCOL nColStart
, SCCOL nColEnd
, SCROW nRowStart
, SCSIZE nSize
);
487 void InsertCol(SCTAB nTab
, SCROW nRowStart
, SCROW nRowEnd
, SCCOL nColStart
, SCSIZE nSize
);
489 void RenameCellStyle( const OUString
& rOld
, const OUString
& rNew
);
490 void DeleteArea( SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
);
492 void SourceChanged( const ScAddress
& rAddr
);
494 bool operator==( const ScConditionalFormatList
& r
) const; // for Ref-Undo
496 typedef ConditionalFormatContainer::iterator iterator
;
497 typedef ConditionalFormatContainer::const_iterator const_iterator
;
500 const_iterator
begin() const;
502 const_iterator
end() const;
506 void erase(sal_uLong nIndex
);
508 void startRendering();
515 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */