Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / refdata.cxx
blob8a86a1cf1803b1a784cc19e52936a2a2c291bcb3
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 #include "refdata.hxx"
22 void ScSingleRefData::InitAddress( const ScAddress& rAdr )
24 InitAddress( rAdr.Col(), rAdr.Row(), rAdr.Tab());
27 void ScSingleRefData::InitAddress( SCCOL nColP, SCROW nRowP, SCTAB nTabP )
29 InitFlags();
30 mnCol = nColP;
31 mnRow = nRowP;
32 mnTab = nTabP;
35 void ScSingleRefData::InitAddressRel( const ScAddress& rAdr, const ScAddress& rPos )
37 InitFlags();
38 SetColRel(true);
39 SetRowRel(true);
40 SetTabRel(true);
41 SetAddress(rAdr, rPos);
44 sal_uInt8 ScSingleRefData::FlagValue() const
46 return mnFlagValue;
49 void ScSingleRefData::SetAbsCol( SCCOL nVal )
51 Flags.bColRel = false;
52 mnCol = nVal;
55 void ScSingleRefData::SetRelCol( SCCOL nVal )
57 Flags.bColRel = true;
58 mnCol = nVal;
61 void ScSingleRefData::IncCol( SCCOL nInc )
63 mnCol += nInc;
66 void ScSingleRefData::SetAbsRow( SCROW nVal )
68 Flags.bRowRel = false;
69 mnRow = nVal;
72 void ScSingleRefData::SetRelRow( SCROW nVal )
74 Flags.bRowRel = true;
75 mnRow = nVal;
78 void ScSingleRefData::IncRow( SCROW nInc )
80 mnRow += nInc;
83 void ScSingleRefData::SetAbsTab( SCTAB nVal )
85 Flags.bTabRel = false;
86 mnTab = nVal;
89 void ScSingleRefData::SetRelTab( SCTAB nVal )
91 Flags.bTabRel = true;
92 mnTab = nVal;
95 void ScSingleRefData::IncTab( SCTAB nInc )
97 mnTab += nInc;
100 void ScSingleRefData::SetColDeleted( bool bVal )
102 Flags.bColDeleted = bVal;
105 bool ScSingleRefData::IsColDeleted() const
107 return Flags.bColDeleted;
110 void ScSingleRefData::SetRowDeleted( bool bVal )
112 Flags.bRowDeleted = bVal;
115 bool ScSingleRefData::IsRowDeleted() const
117 return Flags.bRowDeleted;
120 void ScSingleRefData::SetTabDeleted( bool bVal )
122 Flags.bTabDeleted = bVal;
125 bool ScSingleRefData::IsTabDeleted() const
127 return Flags.bTabDeleted;
130 bool ScSingleRefData::IsDeleted() const
132 return IsColDeleted() || IsRowDeleted() || IsTabDeleted();
135 bool ScSingleRefData::Valid() const
137 return ColValid() && RowValid() && TabValid();
140 bool ScSingleRefData::ColValid() const
142 if (Flags.bColRel)
144 if (mnCol < -MAXCOL || MAXCOL < mnCol)
145 return false;
147 else
149 if (mnCol < 0 || MAXCOL < mnCol)
150 return false;
153 return true;
156 bool ScSingleRefData::RowValid() const
158 if (Flags.bRowRel)
160 if (mnRow < -MAXROW || MAXROW < mnRow)
161 return false;
163 else
165 if (mnRow < 0 || MAXROW < mnRow)
166 return false;
169 return true;
172 bool ScSingleRefData::TabValid() const
174 if (Flags.bTabRel)
176 if (mnTab < -MAXTAB || MAXTAB < mnTab)
177 return false;
179 else
181 if (mnTab < 0 || MAXTAB < mnTab)
182 return false;
185 return true;
188 bool ScSingleRefData::ValidExternal() const
190 return ColValid() && RowValid() && mnTab == -1;
193 ScAddress ScSingleRefData::toAbs( const ScAddress& rPos ) const
195 SCCOL nRetCol = Flags.bColRel ? mnCol + rPos.Col() : mnCol;
196 SCROW nRetRow = Flags.bRowRel ? mnRow + rPos.Row() : mnRow;
197 SCTAB nRetTab = Flags.bTabRel ? mnTab + rPos.Tab() : mnTab;
199 ScAddress aAbs(ScAddress::INITIALIZE_INVALID);
201 if (ValidCol(nRetCol))
202 aAbs.SetCol(nRetCol);
204 if (ValidRow(nRetRow))
205 aAbs.SetRow(nRetRow);
207 if (ValidTab(nRetTab))
208 aAbs.SetTab(nRetTab);
210 return aAbs;
213 void ScSingleRefData::SetAddress( const ScAddress& rAddr, const ScAddress& rPos )
215 if (Flags.bColRel)
216 mnCol = rAddr.Col() - rPos.Col();
217 else
218 mnCol = rAddr.Col();
220 if (Flags.bRowRel)
221 mnRow = rAddr.Row() - rPos.Row();
222 else
223 mnRow = rAddr.Row();
225 if (Flags.bTabRel)
226 mnTab = rAddr.Tab() - rPos.Tab();
227 else
228 mnTab = rAddr.Tab();
231 SCROW ScSingleRefData::Row() const
233 if (Flags.bRowDeleted)
234 return -1;
235 return mnRow;
238 SCCOL ScSingleRefData::Col() const
240 if (Flags.bColDeleted)
241 return -1;
242 return mnCol;
245 SCTAB ScSingleRefData::Tab() const
247 if (Flags.bTabDeleted)
248 return -1;
249 return mnTab;
252 bool ScSingleRefData::operator==( const ScSingleRefData& r ) const
254 return mnFlagValue == r.mnFlagValue && mnCol == r.mnCol && mnRow == r.mnRow && mnTab == r.mnTab;
257 bool ScSingleRefData::operator!=( const ScSingleRefData& r ) const
259 return !operator==(r);
262 #if DEBUG_FORMULA_COMPILER
263 void ScSingleRefData::Dump( int nIndent ) const
265 std::string aIndent;
266 for (int i = 0; i < nIndent; ++i)
267 aIndent += " ";
269 cout << aIndent << "address type column: " << (IsColRel()?"relative":"absolute")
270 << " row : " << (IsRowRel()?"relative":"absolute") << " sheet: "
271 << (IsTabRel()?"relative":"absolute") << endl;
272 cout << aIndent << "deleted column: " << (IsColDeleted()?"yes":"no")
273 << " row : " << (IsRowDeleted()?"yes":"no") << " sheet: "
274 << (IsTabDeleted()?"yes":"no") << endl;
275 cout << aIndent << "column: " << mnCol << " row: " << mnRow << " sheet: " << mnTab << endl;
276 cout << aIndent << "3d ref: " << (IsFlag3D()?"yes":"no") << endl;
278 #endif
280 ScComplexRefData& ScComplexRefData::Extend( const ScSingleRefData & rRef, const ScAddress & rPos )
282 ScRange aAbsRange = toAbs(rPos);
283 ScAddress aAbs = rRef.toAbs(rPos);
285 if (aAbs.Col() < aAbsRange.aStart.Col())
286 aAbsRange.aStart.SetCol(aAbs.Col());
288 if (aAbs.Row() < aAbsRange.aStart.Row())
289 aAbsRange.aStart.SetRow(aAbs.Row());
291 if (aAbs.Tab() < aAbsRange.aStart.Tab())
292 aAbsRange.aStart.SetTab(aAbs.Tab());
294 if (aAbsRange.aEnd.Col() < aAbs.Col())
295 aAbsRange.aEnd.SetCol(aAbs.Col());
297 if (aAbsRange.aEnd.Row() < aAbs.Row())
298 aAbsRange.aEnd.SetRow(aAbs.Row());
300 if (aAbsRange.aEnd.Tab() < aAbs.Tab())
301 aAbsRange.aEnd.SetTab(aAbs.Tab());
303 SetRange(aAbsRange, rPos);
305 return *this;
309 ScComplexRefData& ScComplexRefData::Extend( const ScComplexRefData & rRef, const ScAddress & rPos )
311 return Extend( rRef.Ref1, rPos).Extend( rRef.Ref2, rPos);
314 bool ScComplexRefData::IsDeleted() const
316 return Ref1.IsDeleted() || Ref2.IsDeleted();
319 bool ScComplexRefData::Valid() const
321 return Ref1.Valid() && Ref2.Valid();
324 bool ScComplexRefData::ValidExternal() const
326 return Ref1.ValidExternal() && Ref2.ColValid() && Ref2.RowValid() && Ref1.Tab() <= Ref2.Tab();
329 ScRange ScComplexRefData::toAbs( const ScAddress& rPos ) const
331 return ScRange(Ref1.toAbs(rPos), Ref2.toAbs(rPos));
334 void ScComplexRefData::SetRange( const ScRange& rRange, const ScAddress& rPos )
336 Ref1.SetAddress(rRange.aStart, rPos);
337 Ref2.SetAddress(rRange.aEnd, rPos);
340 #if DEBUG_FORMULA_COMPILER
341 void ScComplexRefData::Dump( int nIndent ) const
343 std::string aIndent;
344 for (int i = 0; i < nIndent; ++i)
345 aIndent += " ";
347 cout << aIndent << "ref 1" << endl;
348 Ref1.Dump(nIndent+1);
349 cout << aIndent << "ref 2" << endl;
350 Ref2.Dump(nIndent+1);
352 #endif
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */