1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cell.hxx,v $
10 * $Revision: 1.30.38.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
38 #include <boost/shared_ptr.hpp>
40 #include <tools/mempool.hxx>
41 #include <svtools/listener.hxx>
43 #include "rangenam.hxx"
44 #include "formula/grammar.hxx"
45 #include "tokenarray.hxx"
46 #include "formularesult.hxx"
47 #include <rtl/ustrbuf.hxx>
48 #include <vcl/fontcvt.hxx>
52 #define TEXTWIDTH_DIRTY 0xffff
54 // in addition to SCRIPTTYPE_... flags from scripttypeitem.hxx:
55 // set (in nScriptType) if type has not been determined yet
56 #define SC_SCRIPTTYPE_UNKNOWN 0x08
67 // ============================================================================
69 /** Default cell clone flags: do not start listening, do not adjust 3D refs to
70 old position, clone note captions of cell notes. */
71 const int SC_CLONECELL_DEFAULT
= 0x0000;
73 /** If set, cloned formula cells will start to listen to the document. */
74 const int SC_CLONECELL_STARTLISTENING
= 0x0001;
76 /** If set, relative 3D references of cloned formula cells will be adjusted to
77 old position (used while swapping cells for sorting a cell range). */
78 const int SC_CLONECELL_ADJUST3DREL
= 0x0002;
80 /** If set, the caption object of a cell note will not be cloned (used while
81 copying cells to undo document, where captions are handled in drawing undo). */
82 const int SC_CLONECELL_NOCAPTION
= 0x0004;
84 // ============================================================================
86 class SC_DLLPUBLIC ScBaseCell
89 ~ScBaseCell(); // nicht virtuell -> darf nicht direkt aufgerufen werden
92 explicit ScBaseCell( CellType eNewType
);
94 /** Base copy constructor. Does NOT clone cell note or broadcaster! */
95 ScBaseCell( const ScBaseCell
& rCell
);
97 /** Returns a clone of this cell at the same position, cell note and
98 broadcaster will not be cloned. */
99 ScBaseCell
* CloneWithoutNote( ScDocument
& rDestDoc
, int nCloneFlags
= SC_CLONECELL_DEFAULT
) const;
101 /** Returns a clone of this cell for the passed document position, cell
102 note and broadcaster will not be cloned. */
103 ScBaseCell
* CloneWithoutNote( ScDocument
& rDestDoc
, const ScAddress
& rDestPos
, int nCloneFlags
= SC_CLONECELL_DEFAULT
) const;
105 /** Returns a clone of this cell, clones cell note and caption object too
106 (unless SC_CLONECELL_NOCAPTION flag is set). Broadcaster will not be cloned. */
107 ScBaseCell
* CloneWithNote( const ScAddress
& rOwnPos
, ScDocument
& rDestDoc
, const ScAddress
& rDestPos
, int nCloneFlags
= SC_CLONECELL_DEFAULT
) const;
109 /** Due to the fact that ScBaseCell does not have a vtable, this function
110 deletes the cell by calling the appropriate d'tor of the derived class. */
113 inline CellType
GetCellType() const { return (CellType
)eCellType
; }
115 /** Returns true, if the cell is empty (neither value nor formula nor cell note).
116 Returns false for formula cells returning nothing, use HasEmptyData() for that. */
117 bool IsBlank( bool bIgnoreNotes
= false ) const;
119 // fuer Idle-Berechnung
120 inline USHORT
GetTextWidth() const { return nTextWidth
; }
121 inline void SetTextWidth( USHORT nNew
) { nTextWidth
= nNew
; }
123 inline BYTE
GetScriptType() const { return nScriptType
; }
124 inline void SetScriptType( BYTE nNew
) { nScriptType
= nNew
; }
126 /** Returns true, if the cell contains a note. */
127 inline bool HasNote() const { return mpNote
!= 0; }
128 /** Returns the pointer to a cell note object (read-only). */
129 inline const ScPostIt
* GetNote() const { return mpNote
; }
130 /** Returns the pointer to a cell note object. */
131 inline ScPostIt
* GetNote() { return mpNote
; }
132 /** Takes ownership of the passed cell note object. */
133 void TakeNote( ScPostIt
* pNote
);
134 /** Returns and forgets the own cell note object. Caller takes ownership! */
135 ScPostIt
* ReleaseNote();
136 /** Deletes the own cell note object. */
139 /** Returns true, if the cell contains a broadcaster. */
140 inline bool HasBroadcaster() const { return mpBroadcaster
!= 0; }
141 /** Returns the pointer to the cell broadcaster. */
142 inline SvtBroadcaster
* GetBroadcaster() const { return mpBroadcaster
; }
143 /** Takes ownership of the passed cell broadcaster. */
144 void TakeBroadcaster( SvtBroadcaster
* pBroadcaster
);
145 /** Returns and forgets the own cell broadcaster. Caller takes ownership! */
146 SvtBroadcaster
* ReleaseBroadcaster();
147 /** Deletes the own cell broadcaster. */
148 void DeleteBroadcaster();
150 // String- oder EditCell
151 static ScBaseCell
* CreateTextCell( const String
& rString
, ScDocument
* );
153 // nOnlyNames may be one or more of SC_LISTENING_NAMES_*
154 void StartListeningTo( ScDocument
* pDoc
);
155 void EndListeningTo( ScDocument
* pDoc
,
156 ScTokenArray
* pArr
= NULL
,
157 ScAddress aPos
= ScAddress() );
159 /** Error code if ScFormulaCell, else 0. */
160 USHORT
GetErrorCode() const;
161 /** ScFormulaCell with formula::svEmptyCell result, or ScNoteCell (may have been
162 created due to reference to empty cell). */
163 BOOL
HasEmptyData() const;
164 BOOL
HasValueData() const;
165 BOOL
HasStringData() const;
166 String
GetStringData() const; // nur echte Strings
168 static BOOL
CellEqual( const ScBaseCell
* pCell1
, const ScBaseCell
* pCell2
);
171 ScBaseCell
& operator=( const ScBaseCell
& );
174 ScPostIt
* mpNote
; /// The cell note. Cell takes ownership!
175 SvtBroadcaster
* mpBroadcaster
; /// Broadcaster for changed values. Cell takes ownership!
179 BYTE eCellType
; // enum CellType - BYTE spart Speicher
183 // ============================================================================
185 class SC_DLLPUBLIC ScNoteCell
: public ScBaseCell
189 DECL_FIXEDMEMPOOL_NEWDEL( ScNoteCell
)
192 /** Cell takes ownership of the passed broadcaster. */
193 explicit ScNoteCell( SvtBroadcaster
* pBC
= 0 );
194 /** Cell takes ownership of the passed note and broadcaster. */
195 explicit ScNoteCell( ScPostIt
* pNote
, SvtBroadcaster
* pBC
= 0 );
202 ScNoteCell( const ScNoteCell
& );
205 // ============================================================================
207 class SC_DLLPUBLIC ScValueCell
: public ScBaseCell
211 DECL_FIXEDMEMPOOL_NEWDEL( ScValueCell
)
214 explicit ScValueCell( double fValue
);
220 inline void SetValue( double fValue
) { mfValue
= fValue
; }
221 inline double GetValue() const { return mfValue
; }
227 // ============================================================================
229 class SC_DLLPUBLIC ScStringCell
: public ScBaseCell
233 DECL_FIXEDMEMPOOL_NEWDEL( ScStringCell
)
237 explicit ScStringCell( const String
& rString
);
243 inline void SetString( const String
& rString
) { maString
= rString
; }
244 inline void GetString( String
& rString
) const { rString
= maString
; }
245 inline const String
& GetString() const { return maString
; }
251 // ============================================================================
253 class SC_DLLPUBLIC ScEditCell
: public ScBaseCell
256 EditTextObject
* pData
;
257 String
* pString
; // fuer schnelleren Zugriff von Formeln
258 ScDocument
* pDoc
; // fuer EditEngine Zugriff mit Pool
260 void SetTextObject( const EditTextObject
* pObject
,
261 const SfxItemPool
* pFromPool
);
264 ScEditCell( const ScEditCell
& );
269 DECL_FIXEDMEMPOOL_NEWDEL( ScEditCell
)
272 ~ScEditCell(); // wegen pData immer!
274 ScEditCell( const EditTextObject
* pObject
, ScDocument
*,
275 const SfxItemPool
* pFromPool
/* = NULL */ );
276 ScEditCell( const ScEditCell
& rCell
, ScDocument
& rDoc
);
277 // fuer Zeilenumbrueche
278 ScEditCell( const String
& rString
, ScDocument
* );
280 void SetData( const EditTextObject
* pObject
,
281 const SfxItemPool
* pFromPool
/* = NULL */ );
282 void GetData( const EditTextObject
*& rpObject
) const;
283 void GetString( String
& rString
) const;
285 const EditTextObject
* GetData() const { return pData
; }
287 /** Removes character attribute based on new pattern attributes. */
288 void RemoveCharAttribs( const ScPatternAttr
& rAttr
);
291 // ============================================================================
293 class ScEditDataArray
299 explicit Item(SCTAB nTab
, SCCOL nCol
, SCROW nRow
,
300 EditTextObject
* pOldData
, EditTextObject
* pNewData
);
303 const EditTextObject
* GetOldData() const;
304 const EditTextObject
* GetNewData() const;
305 SCTAB
GetTab() const;
306 SCCOL
GetCol() const;
307 SCROW
GetRow() const;
313 ::boost::shared_ptr
<EditTextObject
> mpOldData
;
314 ::boost::shared_ptr
<EditTextObject
> mpNewData
;
324 void AddItem(SCTAB nTab
, SCCOL nCol
, SCROW nRow
,
325 EditTextObject
* pOldData
, EditTextObject
* pNewData
);
331 ::std::vector
<Item
>::const_iterator maIter
;
332 ::std::vector
<Item
> maArray
;
335 // ============================================================================
338 MM_NONE
= 0, // No matrix formula
339 MM_FORMULA
= 1, // Upper left matrix formula cell
340 MM_REFERENCE
= 2, // Remaining cells, via ocMatRef reference token
341 MM_FAKE
= 3 // Interpret "as-if" matrix formula (legacy)
344 class SC_DLLPUBLIC ScFormulaCell
: public ScBaseCell
, public SvtListener
347 ScFormulaResult aResult
;
348 formula::FormulaGrammar::Grammar eTempGrammar
; // used between string (creation) and (re)compilation
349 ScTokenArray
* pCode
; // The (new) token array
350 ScDocument
* pDocument
;
351 ScFormulaCell
* pPrevious
;
352 ScFormulaCell
* pNext
;
353 ScFormulaCell
* pPreviousTrack
;
354 ScFormulaCell
* pNextTrack
;
355 ULONG nFormatIndex
; // Number format set by calculation
356 short nFormatType
; // Number format type set by calculation
357 USHORT nSeenInIteration
; // Iteration cycle in which the cell was last encountered
358 BYTE cMatrixFlag
; // One of ScMatrixMode
359 BOOL bDirty
: 1; // Must be (re)calculated
360 BOOL bChanged
: 1; // Whether something changed regarding display/representation
361 BOOL bRunning
: 1; // Already interpreting right now
362 BOOL bCompile
: 1; // Must be (re)compiled
363 BOOL bSubTotal
: 1; // Cell is part of or contains a SubTotal
364 BOOL bIsIterCell
: 1; // Cell is part of a circular reference
365 BOOL bInChangeTrack
: 1; // Cell is in ChangeTrack
366 BOOL bTableOpDirty
: 1; // Dirty flag for TableOp
367 BOOL bNeedListening
: 1; // Listeners need to be re-established after UpdateReference
369 enum ScInterpretTailParameter
372 SCITP_FROM_ITERATION
,
373 SCITP_CLOSE_ITERATION_CIRCLE
375 void InterpretTail( ScInterpretTailParameter
);
377 ScFormulaCell( const ScFormulaCell
& );
382 DECL_FIXEDMEMPOOL_NEWDEL( ScFormulaCell
)
390 /** Empty formula cell, or with a preconstructed token array. */
391 ScFormulaCell( ScDocument
*, const ScAddress
&, const ScTokenArray
* = NULL
,
392 const formula::FormulaGrammar::Grammar
= formula::FormulaGrammar::GRAM_DEFAULT
,
395 /** With formula string and grammar to compile with.
396 formula::FormulaGrammar::GRAM_DEFAULT effectively isformula::FormulaGrammar::GRAM_NATIVE_UI that
397 also includes formula::FormulaGrammar::CONV_UNSPECIFIED, therefor uses the address
398 convention associated with rPos::nTab by default. */
399 ScFormulaCell( ScDocument
* pDoc
, const ScAddress
& rPos
,
400 const String
& rFormula
,
401 const formula::FormulaGrammar::Grammar
= formula::FormulaGrammar::GRAM_DEFAULT
,
402 BYTE cMatInd
= MM_NONE
);
404 ScFormulaCell( const ScFormulaCell
& rCell
, ScDocument
& rDoc
, const ScAddress
& rPos
, int nCloneFlags
= SC_CLONECELL_DEFAULT
);
406 void GetFormula( String
& rFormula
,
407 const formula::FormulaGrammar::Grammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
408 void GetFormula( rtl::OUStringBuffer
& rBuffer
,
409 const formula::FormulaGrammar::Grammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
413 // If setting entire document dirty after load, no broadcasts but still append to FormulaTree.
414 void SetDirtyAfterLoad();
415 inline void ResetTableOpDirtyVar() { bTableOpDirty
= FALSE
; }
416 void SetTableOpDirty();
417 BOOL
IsDirtyOrInTableOpDirty() const;
418 BOOL
GetDirty() const { return bDirty
; }
419 BOOL
NeedsListening() const { return bNeedListening
; }
420 void SetNeedsListening( BOOL bVar
) { bNeedListening
= bVar
; }
421 void Compile(const String
& rFormula
,
422 BOOL bNoListening
= FALSE
,
423 const formula::FormulaGrammar::Grammar
= formula::FormulaGrammar::GRAM_DEFAULT
);
424 void CompileTokenArray( BOOL bNoListening
= FALSE
);
425 void CompileXML( ScProgress
& rProgress
); // compile temporary string tokens
426 void CalcAfterLoad();
427 bool MarkUsedExternalReferences();
429 inline BOOL
IsIterCell() const { return bIsIterCell
; }
430 inline USHORT
GetSeenInIteration() const { return nSeenInIteration
; }
432 BOOL
HasOneReference( ScRange
& r
) const;
433 /* Checks if the formula contains reference list that can be
434 expressed by one reference (like A1;A2;A3:A5 -> A1:A5). The
435 reference list is not required to be sorted (i.e. A3;A1;A2 is
436 still recognized as A1:A3), but no overlapping is allowed.
437 If one reference is recognized, the rRange is filled.
439 It is similar to HasOneReference(), but more general.
441 bool HasRefListExpressibleAsOneReference(ScRange
& rRange
) const;
442 BOOL
HasRelNameReference() const;
443 BOOL
HasColRowName() const;
445 void UpdateReference(UpdateRefMode eUpdateRefMode
,
447 SCsCOL nDx
, SCsROW nDy
, SCsTAB nDz
,
448 ScDocument
* pUndoDoc
= NULL
,
449 const ScAddress
* pUndoCellPos
= NULL
);
451 void TransposeReference();
452 void UpdateTranspose( const ScRange
& rSource
, const ScAddress
& rDest
,
453 ScDocument
* pUndoDoc
);
455 void UpdateGrow( const ScRange
& rArea
, SCCOL nGrowX
, SCROW nGrowY
);
457 void UpdateInsertTab(SCTAB nTable
);
458 void UpdateInsertTabAbs(SCTAB nTable
);
459 BOOL
UpdateDeleteTab(SCTAB nTable
, BOOL bIsMove
= FALSE
);
460 void UpdateMoveTab(SCTAB nOldPos
, SCTAB nNewPos
, SCTAB nTabNo
);
461 void UpdateRenameTab(SCTAB nTable
, const String
& rName
);
462 BOOL
TestTabRefAbs(SCTAB nTable
);
463 void UpdateCompile( BOOL bForceIfNameInUse
= FALSE
);
464 BOOL
IsRangeNameInUse(USHORT nIndex
) const;
465 void FindRangeNamesInUse(std::set
<USHORT
>& rIndexes
) const;
466 void ReplaceRangeNamesInUse( const ScRangeData::IndexMap
& rMap
);
467 BOOL
IsSubTotal() const { return bSubTotal
; }
468 BOOL
IsChanged() const { return bChanged
; }
469 void ResetChanged() { bChanged
= FALSE
; }
470 BOOL
IsEmpty(); // formula::svEmptyCell result
471 // display as empty string if formula::svEmptyCell result
472 BOOL
IsEmptyDisplayedAsString();
473 BOOL
IsValue(); // also TRUE if formula::svEmptyCell
475 double GetValueAlways(); // ignore errors
476 void GetString( String
& rString
);
477 const ScMatrix
* GetMatrix();
478 BOOL
GetMatrixOrigin( ScAddress
& rPos
) const;
479 void GetResultDimensions( SCSIZE
& rCols
, SCSIZE
& rRows
);
480 USHORT
GetMatrixEdge( ScAddress
& rOrgPos
);
481 USHORT
GetErrCode(); // interpret first if necessary
482 USHORT
GetRawError(); // don't interpret, just return code or result error
483 short GetFormatType() const { return nFormatType
; }
484 ULONG
GetFormatIndex() const { return nFormatIndex
; }
485 void GetFormatInfo( short& nType
, ULONG
& nIndex
) const
486 { nType
= nFormatType
; nIndex
= nFormatIndex
; }
487 BYTE
GetMatrixFlag() const { return cMatrixFlag
; }
488 ScTokenArray
* GetCode() const { return pCode
; }
490 BOOL
IsRunning() const { return bRunning
; }
491 void SetRunning( BOOL bVal
) { bRunning
= bVal
; }
492 void CompileDBFormula();
493 void CompileDBFormula( BOOL bCreateFormulaString
);
494 void CompileNameFormula( BOOL bCreateFormulaString
);
495 void CompileColRowNameFormula();
496 ScFormulaCell
* GetPrevious() const { return pPrevious
; }
497 ScFormulaCell
* GetNext() const { return pNext
; }
498 void SetPrevious( ScFormulaCell
* pF
) { pPrevious
= pF
; }
499 void SetNext( ScFormulaCell
* pF
) { pNext
= pF
; }
500 ScFormulaCell
* GetPreviousTrack() const { return pPreviousTrack
; }
501 ScFormulaCell
* GetNextTrack() const { return pNextTrack
; }
502 void SetPreviousTrack( ScFormulaCell
* pF
) { pPreviousTrack
= pF
; }
503 void SetNextTrack( ScFormulaCell
* pF
) { pNextTrack
= pF
; }
505 virtual void Notify( SvtBroadcaster
& rBC
, const SfxHint
& rHint
);
506 void SetCompile( BOOL bVal
) { bCompile
= bVal
; }
507 ScDocument
* GetDocument() const { return pDocument
; }
508 void SetMatColsRows( SCCOL nCols
, SCROW nRows
);
509 void GetMatColsRows( SCCOL
& nCols
, SCROW
& nRows
) const;
511 // ob Zelle im ChangeTrack und nicht im echten Dokument ist
512 void SetInChangeTrack( BOOL bVal
) { bInChangeTrack
= bVal
; }
513 BOOL
IsInChangeTrack() const { return bInChangeTrack
; }
515 // Zu Typ und Format das entsprechende Standardformat.
516 // Bei Format "Standard" evtl. das in die Formelzelle
517 // uebernommene Format.
518 ULONG
GetStandardFormat( SvNumberFormatter
& rFormatter
, ULONG nFormat
) const;
520 // For import filters!
521 void AddRecalcMode( formula::ScRecalcMode
);
522 /** For import only: set a double result. */
523 void SetHybridDouble( double n
) { aResult
.SetHybridDouble( n
); }
524 /** For import only: set a string result.
525 If for whatever reason you have to use both, SetHybridDouble() and
526 SetHybridString() or SetHybridFormula(), use SetHybridDouble() first
527 for performance reasons.*/
528 void SetHybridString( const String
& r
)
529 { aResult
.SetHybridString( r
); }
530 /** For import only: set a temporary formula string to be compiled later.
531 If for whatever reason you have to use both, SetHybridDouble() and
532 SetHybridString() or SetHybridFormula(), use SetHybridDouble() first
533 for performance reasons.*/
534 void SetHybridFormula( const String
& r
,
535 const formula::FormulaGrammar::Grammar eGrammar
)
536 { aResult
.SetHybridFormula( r
); eTempGrammar
= eGrammar
; }
537 void SetErrCode( USHORT n
);
538 inline BOOL
IsHyperLinkCell() const { return pCode
&& pCode
->IsHyperLink(); }
539 EditTextObject
* CreateURLObject() ;
540 void GetURLResult( String
& rURL
, String
& rCellText
);
542 /** Determines whether or not the result string contains more than one paragraph */
543 bool IsMultilineResult();
546 // Iterator fuer Referenzen in einer Formelzelle
547 class ScDetectiveRefIter
553 ScDetectiveRefIter( ScFormulaCell
* pCell
);
554 BOOL
GetNextRef( ScRange
& rRange
);
555 ScToken
* GetNextRefToken();
558 // ============================================================================