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 INCLUDED_SC_INC_TOKENARRAY_HXX
21 #define INCLUDED_SC_INC_TOKENARRAY_HXX
23 #include <formula/token.hxx>
26 #include "calcmacros.hxx"
27 #include "address.hxx"
29 #include <formula/tokenarray.hxx>
33 struct RefUpdateContext
;
34 struct RefUpdateInsertTabContext
;
35 struct RefUpdateDeleteTabContext
;
36 struct RefUpdateMoveTabContext
;
37 struct RefUpdateResult
;
38 struct TokenStringContext
;
39 class ColRowReorderMapType
;
44 struct ScSingleRefData
;
45 struct ScComplexRefData
;
47 class SC_DLLPUBLIC ScTokenArray
: public formula::FormulaTokenArray
49 friend class ScCompiler
;
51 bool ImplGetReference( ScRange
& rRange
, const ScAddress
& rPos
, bool bValidOnly
) const;
54 ScFormulaVectorState meVectorState
;
58 /// Assignment with references to FormulaToken entries (not copied!)
59 ScTokenArray( const ScTokenArray
& );
60 virtual ~ScTokenArray();
61 void ClearScTokenArray();
62 ScTokenArray
* Clone() const; /// True copy!
65 size_t GetHash() const { return mnHashValue
;}
67 ScFormulaVectorState
GetVectorState() const { return meVectorState
;}
70 * If the array contains at least one relative row reference or named
71 * expression, it's variant. Otherwise invariant.
73 bool IsInvariant() const;
75 /// Exactly and only one range (valid or deleted)
76 bool IsReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
77 /// Exactly and only one valid range (no #REF!s)
78 bool IsValidReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
80 /** Determines the extent of direct adjacent
81 references. Only use with real functions, e.g.
82 GetOuterFuncOpCode() == ocSum ! */
83 bool GetAdjacentExtendOfOuterFuncRefs( SCCOLROW
& nExtend
,
84 const ScAddress
& rPos
, ScDirection
);
86 formula::FormulaToken
* AddRawToken( const ScRawToken
& );
87 virtual bool AddFormulaToken(
88 const css::sheet::FormulaToken
& rToken
,
89 svl::SharedStringPool
& rSPool
,
90 formula::ExternalReferenceHelper
* _pRef
) override
;
91 virtual void CheckToken( const formula::FormulaToken
& r
) override
;
92 virtual formula::FormulaToken
* AddOpCode( OpCode eCode
) override
;
93 /** ScSingleRefToken with ocPush. */
94 formula::FormulaToken
* AddSingleReference( const ScSingleRefData
& rRef
);
95 /** ScSingleRefOpToken with ocMatRef. */
96 formula::FormulaToken
* AddMatrixSingleReference( const ScSingleRefData
& rRef
);
97 formula::FormulaToken
* AddDoubleReference( const ScComplexRefData
& rRef
);
98 formula::FormulaToken
* AddRangeName( sal_uInt16 n
, sal_Int16 nSheet
);
99 formula::FormulaToken
* AddDBRange( sal_uInt16 n
);
100 formula::FormulaToken
* AddExternalName( sal_uInt16 nFileId
, const OUString
& rName
);
101 void AddExternalSingleReference( sal_uInt16 nFileId
, const OUString
& rTabName
, const ScSingleRefData
& rRef
);
102 formula::FormulaToken
* AddExternalDoubleReference( sal_uInt16 nFileId
, const OUString
& rTabName
, const ScComplexRefData
& rRef
);
103 formula::FormulaToken
* AddMatrix( const ScMatrixRef
& p
);
104 /** ScSingleRefOpToken with ocColRowName. */
105 formula::FormulaToken
* AddColRowName( const ScSingleRefData
& rRef
);
106 virtual formula::FormulaToken
* MergeArray( ) override
;
108 /** Merge very last SingleRef+ocRange+SingleRef combination into DoubleRef
109 and adjust pCode array, or do nothing if conditions not met. */
110 void MergeRangeReference( const ScAddress
& rPos
);
112 /// Assign XML string placeholder to the array
113 void AssignXMLString( const OUString
&rText
, const OUString
&rFormulaNmsp
);
115 /// Assignment with references to FormulaToken entries (not copied!)
116 ScTokenArray
& operator=( const ScTokenArray
& );
118 /// Make 3D references point to old referenced position even if relative
119 void ReadjustRelative3DReferences(
120 const ScAddress
& rOldPos
,
121 const ScAddress
& rNewPos
);
124 * Make all absolute references external references pointing to the old document
126 * @param pOldDoc old document
127 * @param pNewDoc new document
128 * @param rPos position of the cell to determine if the reference is in the copied area
129 * @param bRangeName set for range names, range names have special handling for absolute sheet ref + relative col/row ref
131 void ReadjustAbsolute3DReferences( const ScDocument
* pOldDoc
, const ScDocument
* pNewDoc
, const ScAddress
& rPos
, bool bRangeName
= false );
134 * Make all absolute references pointing to the copied range if the range is copied too
135 * @param bCheckCopyArea should references pointing into the copy area be adjusted independently from being absolute, should be true only for copy&paste between documents
137 void AdjustAbsoluteRefs( const ScDocument
* pOldDoc
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
, bool bCheckCopyArea
= false );
139 /** When copying a sheet-local named expression, move sheet references that
140 point to the originating sheet to point to the new sheet instead.
142 void AdjustSheetLocalNameReferences( SCTAB nOldTab
, SCTAB nNewTab
);
144 /** Returns true if the sheet nTab is referenced in code. Relative sheet
145 references are evaluated using nPosTab.
147 bool ReferencesSheet( SCTAB nTab
, SCTAB nPosTab
) const;
150 * Adjust all references in response to shifting of cells during cell
151 * insertion and deletion.
153 * @param rCxt context that stores details of shifted region.
154 * @param rOldPos old cell position prior to shifting.
156 sc::RefUpdateResult
AdjustReferenceOnShift( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
);
158 sc::RefUpdateResult
AdjustReferenceOnMove(
159 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
162 * Move reference positions that are within specified moved range.
164 * @param rPos position of this formula cell
166 sc::RefUpdateResult
MoveReference( const ScAddress
& rPos
, const sc::RefUpdateContext
& rCxt
);
169 * Move reference positions in response to column reordering. A range
170 * reference gets moved only when the whole range fits in a single column.
172 * @param rPos position of this formula cell
173 * @param nTab sheet where columns are reordered.
174 * @param nRow1 top row of reordered range.
175 * @param nRow2 bottom row of reordered range.
176 * @param rColMap old-to-new column mapping.
178 void MoveReferenceColReorder(
179 const ScAddress
& rPos
, SCTAB nTab
, SCROW nRow1
, SCROW nRow2
,
180 const sc::ColRowReorderMapType
& rColMap
);
182 void MoveReferenceRowReorder(
183 const ScAddress
& rPos
, SCTAB nTab
, SCCOL nCol1
, SCCOL nCol2
,
184 const sc::ColRowReorderMapType
& rRowMap
);
187 * Adjust all references in named expression. In named expression, we only
188 * update absolute positions, and leave relative positions intact.
190 * @param rCxt context that stores details of shifted region
192 * @return update result.
194 sc::RefUpdateResult
AdjustReferenceInName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
196 sc::RefUpdateResult
AdjustReferenceInMovedName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
199 * Adjust all references on sheet deletion.
201 * @param nDelPos position of sheet being deleted.
202 * @param nSheets number of sheets to delete.
203 * @param rOldPos position of formula cell prior to the deletion.
205 * @return true if at least one reference has changed its sheet reference.
207 sc::RefUpdateResult
AdjustReferenceOnDeletedTab( sc::RefUpdateDeleteTabContext
& rCxt
, const ScAddress
& rOldPos
);
209 sc::RefUpdateResult
AdjustReferenceOnInsertedTab( sc::RefUpdateInsertTabContext
& rCxt
, const ScAddress
& rOldPos
);
211 sc::RefUpdateResult
AdjustReferenceOnMovedTab( sc::RefUpdateMoveTabContext
& rCxt
, const ScAddress
& rOldPos
);
214 * Adjust all internal references on base position change.
216 void AdjustReferenceOnMovedOrigin( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
219 * Adjust all internal references on base position change if they point to
220 * a sheet other than the one of rOldPos.
222 void AdjustReferenceOnMovedOriginIfOtherSheet( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
225 * Adjust internal range references on base position change to justify /
226 * put in order the relative references.
228 void AdjustReferenceOnCopy( const ScAddress
& rNewPos
);
231 * Clear sheet deleted flag from internal reference tokens if the sheet
232 * index falls within specified range. Note that when a reference is on a
233 * sheet that's been deleted, its referenced sheet index retains the
234 * original index of the deleted sheet.
236 * @param rPos position of formula cell
237 * @param nStartTab index of first sheet, inclusive.
238 * @param nEndTab index of last sheet, inclusive.
240 void ClearTabDeleted( const ScAddress
& rPos
, SCTAB nStartTab
, SCTAB nEndTab
);
242 void CheckRelativeReferenceBounds(
243 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
245 void CheckRelativeReferenceBounds(
246 const ScAddress
& rPos
, SCROW nGroupLen
, const ScRange
& rRange
, std::vector
<SCROW
>& rBounds
) const;
248 void CheckExpandReferenceBounds(
249 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
252 * Create a string representation of formula token array without modifying
253 * the internal state of the token array.
255 OUString
CreateString( sc::TokenStringContext
& rCxt
, const ScAddress
& rPos
) const;
257 void WrapReference( const ScAddress
& rPos
, SCCOL nMaxCol
, SCROW nMaxRow
);
258 bool NeedsWrapReference( const ScAddress
& rPos
, SCCOL nMaxCol
, SCROW nMaxRow
) const;
260 #if DEBUG_FORMULA_COMPILER
265 #endif // INCLUDED_SC_INC_TOKENARRAY_HXX
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */