Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef SC_RANGENAM_HXX
21 : #define SC_RANGENAM_HXX
22 :
23 : #include "global.hxx"
24 : #include "address.hxx"
25 : #include "formula/grammar.hxx"
26 : #include "scdllapi.h"
27 : #include "calcmacros.hxx"
28 :
29 : #include <map>
30 : #include <vector>
31 : #include <boost/ptr_container/ptr_set.hpp>
32 : #include <boost/ptr_container/ptr_map.hpp>
33 :
34 : class ScDocument;
35 : class ScTokenArray;
36 :
37 : namespace sc {
38 :
39 : struct RefUpdateContext;
40 : struct RefUpdateInsertTabContext;
41 : struct RefUpdateDeleteTabContext;
42 : struct RefUpdateMoveTabContext;
43 : class CompileFormulaContext;
44 :
45 : }
46 :
47 : typedef sal_uInt16 RangeType;
48 :
49 : #define RT_NAME ((RangeType)0x0000)
50 : #define RT_DATABASE ((RangeType)0x0001)
51 : #define RT_CRITERIA ((RangeType)0x0002)
52 : #define RT_PRINTAREA ((RangeType)0x0004)
53 : #define RT_COLHEADER ((RangeType)0x0008)
54 : #define RT_ROWHEADER ((RangeType)0x0010)
55 : #define RT_ABSAREA ((RangeType)0x0020)
56 : #define RT_REFAREA ((RangeType)0x0040)
57 : #define RT_ABSPOS ((RangeType)0x0080)
58 :
59 : class ScRangeData
60 : {
61 : private:
62 : OUString aName;
63 : OUString aUpperName; // #i62977# for faster searching (aName is never modified after ctor)
64 : ScTokenArray* pCode;
65 : ScAddress aPos;
66 : RangeType eType;
67 : ScDocument* pDoc;
68 : formula::FormulaGrammar::Grammar eTempGrammar; // needed for unresolved XML compiles
69 : sal_uInt16 nIndex;
70 : bool bModified; // is set/cleared by UpdateReference
71 :
72 : // max row and column to use for wrapping of references. If -1 use the
73 : // application's default.
74 : SCROW mnMaxRow;
75 : SCCOL mnMaxCol;
76 :
77 : void CompileRangeData( const OUString& rSymbol, bool bSetError );
78 : void InitCode();
79 : public:
80 :
81 : typedef ::std::map<sal_uInt16, sal_uInt16> IndexMap;
82 :
83 : SC_DLLPUBLIC ScRangeData( ScDocument* pDoc,
84 : const OUString& rName,
85 : const OUString& rSymbol,
86 : const ScAddress& rAdr = ScAddress(),
87 : RangeType nType = RT_NAME,
88 : const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT );
89 : SC_DLLPUBLIC ScRangeData( ScDocument* pDoc,
90 : const OUString& rName,
91 : const ScTokenArray& rArr,
92 : const ScAddress& rAdr = ScAddress(),
93 : RangeType nType = RT_NAME );
94 : SC_DLLPUBLIC ScRangeData( ScDocument* pDoc,
95 : const OUString& rName,
96 : const ScAddress& rTarget );
97 : // rTarget is ABSPOS jump label
98 : ScRangeData(const ScRangeData& rScRangeData, ScDocument* pDocument = NULL);
99 :
100 : SC_DLLPUBLIC ~ScRangeData();
101 :
102 : bool operator== (const ScRangeData& rData) const;
103 :
104 0 : void GetName( OUString& rName ) const { rName = aName; }
105 0 : const OUString& GetName( void ) const { return aName; }
106 0 : const OUString& GetUpperName( void ) const { return aUpperName; }
107 0 : ScAddress GetPos() const { return aPos; }
108 : // The index has to be unique. If index=0 a new index value is assigned.
109 0 : void SetIndex( sal_uInt16 nInd ) { nIndex = nInd; }
110 0 : sal_uInt16 GetIndex() const { return nIndex; }
111 0 : ScTokenArray* GetCode() { return pCode; }
112 : SC_DLLPUBLIC void SetCode( ScTokenArray& );
113 0 : const ScTokenArray* GetCode() const { return pCode; }
114 : SC_DLLPUBLIC sal_uInt16 GetErrCode() const;
115 : bool HasReferences() const;
116 : void SetDocument( ScDocument* pDocument){ pDoc = pDocument; }
117 : ScDocument* GetDocument() const { return pDoc; }
118 : void SetType( RangeType nType ) { eType = nType; }
119 0 : void AddType( RangeType nType ) { eType = eType|nType; }
120 0 : RangeType GetType() const { return eType; }
121 : bool HasType( RangeType nType ) const;
122 : sal_uInt32 GetUnoType() const;
123 : SC_DLLPUBLIC void GetSymbol( OUString& rSymbol, const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT ) const;
124 : SC_DLLPUBLIC void GetSymbol( OUString& rSymbol, const ScAddress& rPos, const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT ) const;
125 : void UpdateSymbol( OUStringBuffer& rBuffer, const ScAddress&,
126 : const formula::FormulaGrammar::Grammar eGrammar = formula::FormulaGrammar::GRAM_DEFAULT );
127 :
128 : /**
129 : * @param nLocalTab sheet index where this name belongs, or -1 for global
130 : * name.
131 : */
132 : void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab = -1 );
133 0 : bool IsModified() const { return bModified; }
134 :
135 : SC_DLLPUBLIC void GuessPosition();
136 :
137 : void UpdateTranspose( const ScRange& rSource, const ScAddress& rDest );
138 : void UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY );
139 :
140 : SC_DLLPUBLIC bool IsReference( ScRange& rRef ) const;
141 : bool IsReference( ScRange& rRef, const ScAddress& rPos ) const;
142 : SC_DLLPUBLIC bool IsValidReference( ScRange& rRef ) const;
143 : bool IsRangeAtBlock( const ScRange& ) const;
144 :
145 : void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab = -1 );
146 : void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab = -1 );
147 : void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab = -1 );
148 :
149 : void ValidateTabRefs();
150 :
151 : static void MakeValidName( OUString& rName );
152 : SC_DLLPUBLIC static bool IsNameValid( const OUString& rName, ScDocument* pDoc );
153 :
154 : SCROW GetMaxRow() const;
155 : SCCOL GetMaxCol() const;
156 :
157 : void CompileUnresolvedXML( sc::CompileFormulaContext& rCxt );
158 :
159 : #if DEBUG_FORMULA_COMPILER
160 : void Dump() const;
161 : #endif
162 : };
163 :
164 0 : inline bool ScRangeData::HasType( RangeType nType ) const
165 : {
166 0 : return ( ( eType & nType ) == nType );
167 : }
168 :
169 : extern "C" int SAL_CALL ScRangeData_QsortNameCompare( const void*, const void* );
170 :
171 : bool operator< (const ScRangeData& left, const ScRangeData& right);
172 :
173 0 : class ScRangeName
174 : {
175 : private:
176 : typedef std::vector<ScRangeData*> IndexDataType;
177 : typedef ::boost::ptr_map<OUString, ScRangeData> DataType;
178 : DataType maData;
179 : IndexDataType maIndexToData;
180 :
181 : public:
182 : /// Map that manages stored ScRangeName instances.
183 : typedef ::boost::ptr_map<SCTAB, ScRangeName> TabNameMap;
184 : /// Map that stores non-managed pointers to ScRangeName instances.
185 : typedef ::std::map<SCTAB, const ScRangeName*> TabNameCopyMap;
186 :
187 : typedef DataType::const_iterator const_iterator;
188 : typedef DataType::iterator iterator;
189 :
190 : ScRangeName();
191 : SC_DLLPUBLIC ScRangeName(const ScRangeName& r);
192 :
193 : SC_DLLPUBLIC const ScRangeData* findByRange(const ScRange& rRange) const;
194 : SC_DLLPUBLIC ScRangeData* findByUpperName(const OUString& rName);
195 : SC_DLLPUBLIC const ScRangeData* findByUpperName(const OUString& rName) const;
196 : SC_DLLPUBLIC ScRangeData* findByIndex(sal_uInt16 i) const;
197 : void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab = -1 );
198 : void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab = -1 );
199 : void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab = -1 );
200 : void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab = -1 );
201 : void UpdateTranspose(const ScRange& rSource, const ScAddress& rDest);
202 : void UpdateGrow(const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY);
203 :
204 : /** Compile those names that couldn't be resolved during loading and
205 : inserting because they may have referred a name that was inserted later.
206 : */
207 : void CompileUnresolvedXML( sc::CompileFormulaContext& rCxt );
208 :
209 : SC_DLLPUBLIC const_iterator begin() const;
210 : SC_DLLPUBLIC const_iterator end() const;
211 : SC_DLLPUBLIC iterator begin();
212 : SC_DLLPUBLIC iterator end();
213 : SC_DLLPUBLIC size_t size() const;
214 : bool empty() const;
215 :
216 : /** Insert object into set.
217 : @ATTENTION: The underlying ::boost::ptr_set_adapter::insert(p) takes
218 : ownership of p and if it can't insert it deletes the object! So, if
219 : this insert here returns false the object where p pointed to is gone!
220 : */
221 : SC_DLLPUBLIC bool insert(ScRangeData* p);
222 :
223 : void erase(const ScRangeData& r);
224 : void erase(const OUString& rName);
225 :
226 : /**
227 : * Erase by iterator position. Note that this method doesn't check for
228 : * iterator's validity. The caller must make sure that the iterator is
229 : * valid.
230 : */
231 : void erase(const iterator& itr);
232 : void clear();
233 : bool operator== (const ScRangeName& r) const;
234 : };
235 :
236 : #endif
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|