Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / inc / rangenam.hxx
blob31bdd1f5a23c78638335ce540ec2ef78d209ac8c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #pragma once
22 #include "global.hxx"
23 #include "address.hxx"
24 #include <formula/grammar.hxx>
25 #include "scdllapi.h"
26 #include "calcmacros.hxx"
28 #include <memory>
29 #include <map>
30 #include <vector>
32 class ScDocument;
33 class ScTokenArray;
35 namespace sc {
37 struct RefUpdateContext;
38 struct RefUpdateInsertTabContext;
39 struct RefUpdateDeleteTabContext;
40 struct RefUpdateMoveTabContext;
41 class CompileFormulaContext;
45 class ScRangeData
47 public:
48 enum class Type //specialization to typed_flags outside of class
50 Name = 0x0000,
51 Database = 0x0001,
52 Criteria = 0x0002,
53 PrintArea = 0x0004,
54 ColHeader = 0x0008,
55 RowHeader = 0x0010,
56 AbsArea = 0x0020,
57 RefArea = 0x0040,
58 AbsPos = 0x0080
61 enum class IsNameValidType
63 NAME_VALID,
64 NAME_INVALID_CELL_REF,
65 NAME_INVALID_BAD_STRING
68 private:
69 OUString aName;
70 OUString aUpperName; // #i62977# for faster searching (aName is never modified after ctor)
71 OUString maNewName; ///< used for formulas after changing names in the dialog
72 std::unique_ptr<ScTokenArray>
73 pCode;
74 ScAddress aPos;
75 Type eType;
76 ScDocument& rDoc;
77 formula::FormulaGrammar::Grammar eTempGrammar; // needed for unresolved XML compiles
78 sal_uInt16 nIndex;
79 bool bModified; // is set/cleared by UpdateReference
81 void CompileRangeData( const OUString& rSymbol, bool bSetError );
82 void InitCode();
83 public:
85 SC_DLLPUBLIC ScRangeData( ScDocument& rDoc,
86 const OUString& rName,
87 const OUString& rSymbol,
88 const ScAddress& rAdr = ScAddress(),
89 Type nType = Type::Name,
90 const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT );
91 SC_DLLPUBLIC ScRangeData( ScDocument& rDoc,
92 const OUString& rName,
93 const ScTokenArray& rArr,
94 const ScAddress& rAdr = ScAddress(),
95 Type nType = Type::Name );
96 SC_DLLPUBLIC ScRangeData( ScDocument& rDoc,
97 const OUString& rName,
98 const ScAddress& rTarget );
99 // rTarget is ABSPOS jump label
101 /* Exact copy, not recompiled, no other index (!), nothing... except if
102 * pDocument or pPos are passed, those values are assigned instead of the
103 * copies. */
104 ScRangeData( const ScRangeData& rScRangeData, ScDocument* pDocument = nullptr, const ScAddress* pPos = nullptr );
106 SC_DLLPUBLIC ~ScRangeData();
108 bool operator== (const ScRangeData& rData) const;
110 void GetName( OUString& rName ) const { rName = maNewName.isEmpty() ? aName : maNewName; }
111 const OUString& GetName() const { return maNewName.isEmpty() ? aName : maNewName; }
112 const OUString& GetUpperName() const { return aUpperName; }
113 const ScAddress& GetPos() const { return aPos; }
114 // The index has to be unique. If index=0 a new index value is assigned.
115 void SetIndex( sal_uInt16 nInd ) { nIndex = nInd; }
116 sal_uInt16 GetIndex() const { return nIndex; }
117 /// Does not change the name, but sets maNewName for formula update after dialog.
118 void SetNewName( const OUString& rNewName ) { maNewName = rNewName; }
119 ScTokenArray* GetCode() { return pCode.get(); }
120 SC_DLLPUBLIC void SetCode( const ScTokenArray& );
121 const ScTokenArray* GetCode() const { return pCode.get(); }
122 SC_DLLPUBLIC FormulaError GetErrCode() const;
123 bool HasReferences() const;
124 void AddType( Type nType );
125 Type GetType() const { return eType; }
126 bool HasType( Type nType ) const;
127 sal_uInt32 GetUnoType() const;
128 SC_DLLPUBLIC OUString GetSymbol( const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT ) const;
129 SC_DLLPUBLIC OUString GetSymbol( const ScAddress& rPos, const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT ) const;
130 void UpdateSymbol( OUStringBuffer& rBuffer, const ScAddress& );
133 * @param nLocalTab sheet index where this name belongs, or -1 for global
134 * name.
136 void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab );
137 bool IsModified() const { return bModified; }
139 SC_DLLPUBLIC void GuessPosition();
141 void UpdateTranspose( const ScRange& rSource, const ScAddress& rDest );
142 void UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY );
144 SC_DLLPUBLIC bool IsReference( ScRange& rRef ) const;
145 bool IsReference( ScRange& rRef, const ScAddress& rPos ) const;
146 SC_DLLPUBLIC bool IsValidReference( ScRange& rRef ) const;
147 bool IsRangeAtBlock( const ScRange& ) const;
149 void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab );
150 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab );
151 void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab );
153 void ValidateTabRefs();
155 static void MakeValidName( const ScDocument& rDoc, OUString& rName );
157 SC_DLLPUBLIC static IsNameValidType IsNameValid( const OUString& rName, const ScDocument& rDoc );
159 bool HasPossibleAddressConflict() const;
161 void CompileUnresolvedXML( sc::CompileFormulaContext& rCxt );
163 #if DEBUG_FORMULA_COMPILER
164 void Dump() const;
165 #endif
167 namespace o3tl
169 template<> struct typed_flags<ScRangeData::Type> : is_typed_flags<ScRangeData::Type, 0xff> {};
173 inline void ScRangeData::AddType( Type nType )
175 eType = eType|nType;
178 inline bool ScRangeData::HasType( Type nType ) const
180 return ( ( eType & nType ) == nType );
183 extern "C" int ScRangeData_QsortNameCompare( const void*, const void* );
185 class ScRangeName
187 private:
188 typedef std::vector<ScRangeData*> IndexDataType;
189 typedef ::std::map<OUString, std::unique_ptr<ScRangeData>> DataType;
190 DataType m_Data;
191 IndexDataType maIndexToData;
192 // Use for optimization, true if any of the contained names resolves
193 // as a valid cell address (e.g. 'day1' with 16k columns).
194 mutable bool mHasPossibleAddressConflict : 1;
195 mutable bool mHasPossibleAddressConflictDirty : 1;
197 void checkHasPossibleAddressConflict() const;
199 public:
200 /// Map that stores non-managed pointers to ScRangeName instances.
201 typedef ::std::map<SCTAB, const ScRangeName*> TabNameCopyMap;
203 typedef DataType::const_iterator const_iterator;
204 typedef DataType::iterator iterator;
206 ScRangeName();
207 SC_DLLPUBLIC ScRangeName(const ScRangeName& r);
209 SC_DLLPUBLIC const ScRangeData* findByRange(const ScRange& rRange) const;
210 SC_DLLPUBLIC ScRangeData* findByUpperName(const OUString& rName);
211 SC_DLLPUBLIC const ScRangeData* findByUpperName(const OUString& rName) const;
212 SC_DLLPUBLIC ScRangeData* findByIndex(sal_uInt16 i) const;
213 void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab = -1 );
214 void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab = -1 );
215 void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab = -1 );
216 void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab = -1 );
217 void UpdateTranspose(const ScRange& rSource, const ScAddress& rDest);
218 void UpdateGrow(const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY);
220 /** Compile those names that couldn't be resolved during loading and
221 inserting because they may have referred a name that was inserted later.
223 void CompileUnresolvedXML( sc::CompileFormulaContext& rCxt );
225 /** Copy names while copying a sheet if they reference the sheet to be copied.
227 Assumes that new sheet was already inserted, global names have been
228 updated/adjusted, but sheet-local names on nOldTab are not, as is the
229 case in ScDocument::CopyTab()
231 @param nLocalTab
232 -1 when operating on global names, else sheet/tab of
233 sheet-local name scope. The already adjusted tab on which to
234 find the name.
236 @param nOldTab
237 The original unadjusted tab position.
239 @param nNewTab
240 The new tab position.
242 void CopyUsedNames( const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab,
243 const ScDocument& rOldDoc, ScDocument& rNewDoc, const bool bGlobalNamesToLocal ) const;
245 SC_DLLPUBLIC const_iterator begin() const { return m_Data.begin(); }
246 SC_DLLPUBLIC const_iterator end() const { return m_Data.end(); }
247 SC_DLLPUBLIC size_t size() const { return m_Data.size(); }
248 SC_DLLPUBLIC size_t index_size() const { return maIndexToData.size(); }
249 bool empty() const { return m_Data.empty(); }
251 /** Insert object into set.
252 @ATTENTION: The underlying ::std::map<std::unique_ptr>::insert(p) takes
253 ownership of p and if it can't insert it deletes the object! So, if
254 this insert here returns false the object where p pointed to is gone!
256 @param bReuseFreeIndex
257 If the ScRangeData p points to has an index value of 0:
258 If `TRUE` then reuse a free index slot if available.
259 If `FALSE` then assign a new index slot. The Manage Names
260 dialog uses this so that deleting and adding ranges in the same
261 run is guaranteed to not reuse previously assigned indexes.
263 SC_DLLPUBLIC bool insert( ScRangeData* p, bool bReuseFreeIndex = true );
265 void erase(const ScRangeData& r);
266 void erase(const OUString& rName);
269 * Erase by iterator position. Note that this method doesn't check for
270 * iterator's validity. The caller must make sure that the iterator is
271 * valid.
273 void erase(const_iterator itr);
275 void clear();
276 bool operator== (const ScRangeName& r) const;
278 bool hasPossibleAddressConflict() const
280 if( mHasPossibleAddressConflictDirty )
281 checkHasPossibleAddressConflict();
282 return mHasPossibleAddressConflict;
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */