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_RANGENAM_HXX
21 #define INCLUDED_SC_INC_RANGENAM_HXX
24 #include "address.hxx"
25 #include <formula/grammar.hxx>
27 #include "calcmacros.hxx"
38 struct RefUpdateContext
;
39 struct RefUpdateInsertTabContext
;
40 struct RefUpdateDeleteTabContext
;
41 struct RefUpdateMoveTabContext
;
42 class CompileFormulaContext
;
49 enum class Type
//specialization to typed_flags outside of class
65 NAME_INVALID_CELL_REF
,
66 NAME_INVALID_BAD_STRING
71 OUString aUpperName
; // #i62977# for faster searching (aName is never modified after ctor)
72 OUString maNewName
; ///< used for formulas after changing names in the dialog
73 std::unique_ptr
<ScTokenArray
>
78 formula::FormulaGrammar::Grammar eTempGrammar
; // needed for unresolved XML compiles
80 bool bModified
; // is set/cleared by UpdateReference
82 // max row and column to use for wrapping of references. If -1 use the
83 // application's default.
87 void CompileRangeData( const OUString
& rSymbol
, bool bSetError
);
91 SC_DLLPUBLIC
ScRangeData( ScDocument
* pDoc
,
92 const OUString
& rName
,
93 const OUString
& rSymbol
,
94 const ScAddress
& rAdr
= ScAddress(),
95 Type nType
= Type::Name
,
96 const formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_DEFAULT
);
97 SC_DLLPUBLIC
ScRangeData( ScDocument
* pDoc
,
98 const OUString
& rName
,
99 const ScTokenArray
& rArr
,
100 const ScAddress
& rAdr
= ScAddress(),
101 Type nType
= Type::Name
);
102 SC_DLLPUBLIC
ScRangeData( ScDocument
* pDoc
,
103 const OUString
& rName
,
104 const ScAddress
& rTarget
);
105 // rTarget is ABSPOS jump label
107 /* Exact copy, not recompiled, no other index (!), nothing.. except if
108 * pDocument or pPos are passed, those values are assigned instead of the
110 ScRangeData( const ScRangeData
& rScRangeData
, ScDocument
* pDocument
= nullptr, const ScAddress
* pPos
= nullptr );
112 SC_DLLPUBLIC
~ScRangeData();
114 bool operator== (const ScRangeData
& rData
) const;
116 void GetName( OUString
& rName
) const { rName
= maNewName
.isEmpty() ? aName
: maNewName
; }
117 const OUString
& GetName() const { return maNewName
.isEmpty() ? aName
: maNewName
; }
118 const OUString
& GetUpperName() const { return aUpperName
; }
119 const ScAddress
& GetPos() const { return aPos
; }
120 // The index has to be unique. If index=0 a new index value is assigned.
121 void SetIndex( sal_uInt16 nInd
) { nIndex
= nInd
; }
122 sal_uInt16
GetIndex() const { return nIndex
; }
123 /// Does not change the name, but sets maNewName for formula update after dialog.
124 void SetNewName( const OUString
& rNewName
) { maNewName
= rNewName
; }
125 ScTokenArray
* GetCode() { return pCode
.get(); }
126 SC_DLLPUBLIC
void SetCode( const ScTokenArray
& );
127 const ScTokenArray
* GetCode() const { return pCode
.get(); }
128 SC_DLLPUBLIC FormulaError
GetErrCode() const;
129 bool HasReferences() const;
130 void AddType( Type nType
);
131 Type
GetType() const { return eType
; }
132 bool HasType( Type nType
) const;
133 sal_uInt32
GetUnoType() const;
134 SC_DLLPUBLIC
void GetSymbol( OUString
& rSymbol
, const formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
135 SC_DLLPUBLIC
void GetSymbol( OUString
& rSymbol
, const ScAddress
& rPos
, const formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_DEFAULT
) const;
136 void UpdateSymbol( OUStringBuffer
& rBuffer
, const ScAddress
& );
139 * @param nLocalTab sheet index where this name belongs, or -1 for global
142 void UpdateReference( sc::RefUpdateContext
& rCxt
, SCTAB nLocalTab
);
143 bool IsModified() const { return bModified
; }
145 SC_DLLPUBLIC
void GuessPosition();
147 void UpdateTranspose( const ScRange
& rSource
, const ScAddress
& rDest
);
148 void UpdateGrow( const ScRange
& rArea
, SCCOL nGrowX
, SCROW nGrowY
);
150 SC_DLLPUBLIC
bool IsReference( ScRange
& rRef
) const;
151 bool IsReference( ScRange
& rRef
, const ScAddress
& rPos
) const;
152 SC_DLLPUBLIC
bool IsValidReference( ScRange
& rRef
) const;
153 bool IsRangeAtBlock( const ScRange
& ) const;
155 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
, SCTAB nLocalTab
);
156 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
, SCTAB nLocalTab
);
157 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
, SCTAB nLocalTab
);
159 void ValidateTabRefs();
161 static void MakeValidName( OUString
& rName
);
163 SC_DLLPUBLIC
static IsNameValidType
IsNameValid( const OUString
& rName
, const ScDocument
* pDoc
);
165 SCROW
GetMaxRow() const;
166 SCCOL
GetMaxCol() const;
168 void CompileUnresolvedXML( sc::CompileFormulaContext
& rCxt
);
170 #if DEBUG_FORMULA_COMPILER
176 template<> struct typed_flags
<ScRangeData::Type
> : is_typed_flags
<ScRangeData::Type
, 0xff> {};
180 inline void ScRangeData::AddType( Type nType
)
185 inline bool ScRangeData::HasType( Type nType
) const
187 return ( ( eType
& nType
) == nType
);
190 extern "C" int ScRangeData_QsortNameCompare( const void*, const void* );
195 typedef std::vector
<ScRangeData
*> IndexDataType
;
196 typedef ::std::map
<OUString
, std::unique_ptr
<ScRangeData
>> DataType
;
198 IndexDataType maIndexToData
;
201 /// Map that stores non-managed pointers to ScRangeName instances.
202 typedef ::std::map
<SCTAB
, const ScRangeName
*> TabNameCopyMap
;
204 typedef DataType::const_iterator const_iterator
;
205 typedef DataType::iterator iterator
;
208 SC_DLLPUBLIC
ScRangeName(const ScRangeName
& r
);
210 SC_DLLPUBLIC
const ScRangeData
* findByRange(const ScRange
& rRange
) const;
211 SC_DLLPUBLIC ScRangeData
* findByUpperName(const OUString
& rName
);
212 SC_DLLPUBLIC
const ScRangeData
* findByUpperName(const OUString
& rName
) const;
213 SC_DLLPUBLIC ScRangeData
* findByIndex(sal_uInt16 i
) const;
214 void UpdateReference( sc::RefUpdateContext
& rCxt
, SCTAB nLocalTab
= -1 );
215 void UpdateInsertTab( sc::RefUpdateInsertTabContext
& rCxt
, SCTAB nLocalTab
= -1 );
216 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext
& rCxt
, SCTAB nLocalTab
= -1 );
217 void UpdateMoveTab( sc::RefUpdateMoveTabContext
& rCxt
, SCTAB nLocalTab
= -1 );
218 void UpdateTranspose(const ScRange
& rSource
, const ScAddress
& rDest
);
219 void UpdateGrow(const ScRange
& rArea
, SCCOL nGrowX
, SCROW nGrowY
);
221 /** Compile those names that couldn't be resolved during loading and
222 inserting because they may have referred a name that was inserted later.
224 void CompileUnresolvedXML( sc::CompileFormulaContext
& rCxt
);
226 /** Copy names while copying a sheet if they reference the sheet to be copied.
228 Assumes that new sheet was already inserted, global names have been
229 updated/adjusted, but sheet-local names on nOldTab are not, as is the
230 case in ScDocument::CopyTab()
233 -1 when operating on global names, else sheet/tab of
234 sheet-local name scope. The already adjusted tab on which to
238 The original unadjusted tab position.
241 The new tab position.
243 void CopyUsedNames( const SCTAB nLocalTab
, const SCTAB nOldTab
, const SCTAB nNewTab
,
244 const ScDocument
& rOldDoc
, ScDocument
& rNewDoc
, const bool bGlobalNamesToLocal
) const;
246 SC_DLLPUBLIC const_iterator
begin() const;
247 SC_DLLPUBLIC const_iterator
end() const;
248 SC_DLLPUBLIC iterator
begin();
249 SC_DLLPUBLIC iterator
end();
250 SC_DLLPUBLIC
size_t size() const;
253 /** Insert object into set.
254 @ATTENTION: The underlying ::std::map<std::unique_ptr>::insert(p) takes
255 ownership of p and if it can't insert it deletes the object! So, if
256 this insert here returns false the object where p pointed to is gone!
258 @param bReuseFreeIndex
259 If the ScRangeData p points to has an index value of 0:
260 If `TRUE` then reuse a free index slot if available.
261 If `FALSE` then assign a new index slot. The Manage Names
262 dialog uses this so that deleting and adding ranges in the same
263 run is guaranteed to not reuse previously assigned indexes.
265 SC_DLLPUBLIC
bool insert( ScRangeData
* p
, bool bReuseFreeIndex
= true );
267 void erase(const ScRangeData
& r
);
268 void erase(const OUString
& rName
);
271 * Erase by iterator position. Note that this method doesn't check for
272 * iterator's validity. The caller must make sure that the iterator is
275 void erase(const iterator
& itr
);
277 bool operator== (const ScRangeName
& r
) const;
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */