update dev300-m58
[ooovba.git] / sc / inc / cell.hxx
blob41a441317de4bc682981b003c6703ab1f440d732
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 ************************************************************************/
31 #ifndef SC_CELL_HXX
32 #define SC_CELL_HXX
34 #include <stddef.h>
36 #include <set>
37 #include <vector>
38 #include <boost/shared_ptr.hpp>
40 #include <tools/mempool.hxx>
41 #include <svtools/listener.hxx>
42 #include "global.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>
49 #include "scdllapi.h"
51 #define USE_MEMPOOL
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
58 class ScDocument;
59 class EditTextObject;
60 class ScMatrix;
61 class SvtBroadcaster;
62 class ScCodeArray;
63 class ScProgress;
64 class ScPostIt;
65 class ScPatternAttr;
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
88 protected:
89 ~ScBaseCell(); // nicht virtuell -> darf nicht direkt aufgerufen werden
91 public:
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. */
111 void Delete();
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. */
137 void DeleteNote();
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 );
170 private:
171 ScBaseCell& operator=( const ScBaseCell& );
173 private:
174 ScPostIt* mpNote; /// The cell note. Cell takes ownership!
175 SvtBroadcaster* mpBroadcaster; /// Broadcaster for changed values. Cell takes ownership!
177 protected:
178 USHORT nTextWidth;
179 BYTE eCellType; // enum CellType - BYTE spart Speicher
180 BYTE nScriptType;
183 // ============================================================================
185 class SC_DLLPUBLIC ScNoteCell : public ScBaseCell
187 public:
188 #ifdef USE_MEMPOOL
189 DECL_FIXEDMEMPOOL_NEWDEL( ScNoteCell )
190 #endif
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 );
197 #ifdef DBG_UTIL
198 ~ScNoteCell();
199 #endif
201 private:
202 ScNoteCell( const ScNoteCell& );
205 // ============================================================================
207 class SC_DLLPUBLIC ScValueCell : public ScBaseCell
209 public:
210 #ifdef USE_MEMPOOL
211 DECL_FIXEDMEMPOOL_NEWDEL( ScValueCell )
212 #endif
213 ScValueCell();
214 explicit ScValueCell( double fValue );
216 #ifdef DBG_UTIL
217 ~ScValueCell();
218 #endif
220 inline void SetValue( double fValue ) { mfValue = fValue; }
221 inline double GetValue() const { return mfValue; }
223 private:
224 double mfValue;
227 // ============================================================================
229 class SC_DLLPUBLIC ScStringCell : public ScBaseCell
231 public:
232 #ifdef USE_MEMPOOL
233 DECL_FIXEDMEMPOOL_NEWDEL( ScStringCell )
234 #endif
236 ScStringCell();
237 explicit ScStringCell( const String& rString );
239 #ifdef DBG_UTIL
240 ~ScStringCell();
241 #endif
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; }
247 private:
248 String maString;
251 // ============================================================================
253 class SC_DLLPUBLIC ScEditCell : public ScBaseCell
255 private:
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 );
263 // not implemented
264 ScEditCell( const ScEditCell& );
266 public:
268 #ifdef USE_MEMPOOL
269 DECL_FIXEDMEMPOOL_NEWDEL( ScEditCell )
270 #endif
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
295 public:
296 class Item
298 public:
299 explicit Item(SCTAB nTab, SCCOL nCol, SCROW nRow,
300 EditTextObject* pOldData, EditTextObject* pNewData);
301 ~Item();
303 const EditTextObject* GetOldData() const;
304 const EditTextObject* GetNewData() const;
305 SCTAB GetTab() const;
306 SCCOL GetCol() const;
307 SCROW GetRow() const;
309 private:
310 Item(); // disabled
312 private:
313 ::boost::shared_ptr<EditTextObject> mpOldData;
314 ::boost::shared_ptr<EditTextObject> mpNewData;
315 SCTAB mnTab;
316 SCCOL mnCol;
317 SCROW mnRow;
321 ScEditDataArray();
322 ~ScEditDataArray();
324 void AddItem(SCTAB nTab, SCCOL nCol, SCROW nRow,
325 EditTextObject* pOldData, EditTextObject* pNewData);
327 const Item* First();
328 const Item* Next();
330 private:
331 ::std::vector<Item>::const_iterator maIter;
332 ::std::vector<Item> maArray;
335 // ============================================================================
337 enum ScMatrixMode {
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
346 private:
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
371 SCITP_NORMAL,
372 SCITP_FROM_ITERATION,
373 SCITP_CLOSE_ITERATION_CIRCLE
375 void InterpretTail( ScInterpretTailParameter );
377 ScFormulaCell( const ScFormulaCell& );
379 public:
381 #ifdef USE_MEMPOOL
382 DECL_FIXEDMEMPOOL_NEWDEL( ScFormulaCell )
383 #endif
385 ScAddress aPos;
387 ~ScFormulaCell();
388 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,
393 BYTE = MM_NONE );
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;
411 void SetDirty();
412 void SetDirtyVar();
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();
428 void Interpret();
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,
446 const ScRange& r,
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
474 double GetValue();
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
549 private:
550 ScTokenArray* pCode;
551 ScAddress aPos;
552 public:
553 ScDetectiveRefIter( ScFormulaCell* pCell );
554 BOOL GetNextRef( ScRange& rRange );
555 ScToken* GetNextRefToken();
558 // ============================================================================
560 #endif