merge the formfield patch from ooo-build
[ooovba.git] / sc / source / core / tool / refdata.cxx
blob10d9c8a9d11cf7110b87e77d58d44a8eb49a34cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: refdata.cxx,v $
10 * $Revision: 1.7.32.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include "refdata.hxx"
39 void ScSingleRefData::CalcRelFromAbs( const ScAddress& rPos )
41 nRelCol = nCol - rPos.Col();
42 nRelRow = nRow - rPos.Row();
43 nRelTab = nTab - rPos.Tab();
47 void ScSingleRefData::SmartRelAbs( const ScAddress& rPos )
49 if ( Flags.bColRel )
50 nCol = nRelCol + rPos.Col();
51 else
52 nRelCol = nCol - rPos.Col();
54 if ( Flags.bRowRel )
55 nRow = nRelRow + rPos.Row();
56 else
57 nRelRow = nRow - rPos.Row();
59 if ( Flags.bTabRel )
60 nTab = nRelTab + rPos.Tab();
61 else
62 nRelTab = nTab - rPos.Tab();
66 void ScSingleRefData::CalcAbsIfRel( const ScAddress& rPos )
68 if ( Flags.bColRel )
70 nCol = nRelCol + rPos.Col();
71 if ( !VALIDCOL( nCol ) )
72 Flags.bColDeleted = TRUE;
74 if ( Flags.bRowRel )
76 nRow = nRelRow + rPos.Row();
77 if ( !VALIDROW( nRow ) )
78 Flags.bRowDeleted = TRUE;
80 if ( Flags.bTabRel )
82 nTab = nRelTab + rPos.Tab();
83 if ( !VALIDTAB( nTab ) )
84 Flags.bTabDeleted = TRUE;
88 //UNUSED2008-05 void ScSingleRefData::OldBoolsToNewFlags( const OldSingleRefBools& rBools )
89 //UNUSED2008-05 {
90 //UNUSED2008-05 switch ( rBools.bRelCol )
91 //UNUSED2008-05 {
92 //UNUSED2008-05 case SR_DELETED :
93 //UNUSED2008-05 Flags.bColRel = TRUE; // der war verlorengegangen
94 //UNUSED2008-05 Flags.bColDeleted = TRUE;
95 //UNUSED2008-05 break;
96 //UNUSED2008-05 case SR_ABSOLUTE :
97 //UNUSED2008-05 Flags.bColRel = FALSE;
98 //UNUSED2008-05 Flags.bColDeleted = FALSE;
99 //UNUSED2008-05 break;
100 //UNUSED2008-05 case SR_RELABS :
101 //UNUSED2008-05 case SR_RELATIVE :
102 //UNUSED2008-05 default:
103 //UNUSED2008-05 Flags.bColRel = TRUE;
104 //UNUSED2008-05 Flags.bColDeleted = FALSE;
105 //UNUSED2008-05 }
106 //UNUSED2008-05 switch ( rBools.bRelRow )
107 //UNUSED2008-05 {
108 //UNUSED2008-05 case SR_DELETED :
109 //UNUSED2008-05 Flags.bRowRel = TRUE; // der war verlorengegangen
110 //UNUSED2008-05 Flags.bRowDeleted = TRUE;
111 //UNUSED2008-05 break;
112 //UNUSED2008-05 case SR_ABSOLUTE :
113 //UNUSED2008-05 Flags.bRowRel = FALSE;
114 //UNUSED2008-05 Flags.bRowDeleted = FALSE;
115 //UNUSED2008-05 break;
116 //UNUSED2008-05 case SR_RELABS :
117 //UNUSED2008-05 case SR_RELATIVE :
118 //UNUSED2008-05 default:
119 //UNUSED2008-05 Flags.bRowRel = TRUE;
120 //UNUSED2008-05 Flags.bRowDeleted = FALSE;
121 //UNUSED2008-05 }
122 //UNUSED2008-05 switch ( rBools.bRelTab )
123 //UNUSED2008-05 {
124 //UNUSED2008-05 case SR_DELETED :
125 //UNUSED2008-05 Flags.bTabRel = TRUE; // der war verlorengegangen
126 //UNUSED2008-05 Flags.bTabDeleted = TRUE;
127 //UNUSED2008-05 break;
128 //UNUSED2008-05 case SR_ABSOLUTE :
129 //UNUSED2008-05 Flags.bTabRel = FALSE;
130 //UNUSED2008-05 Flags.bTabDeleted = FALSE;
131 //UNUSED2008-05 break;
132 //UNUSED2008-05 case SR_RELABS :
133 //UNUSED2008-05 case SR_RELATIVE :
134 //UNUSED2008-05 default:
135 //UNUSED2008-05 Flags.bTabRel = TRUE;
136 //UNUSED2008-05 Flags.bTabDeleted = FALSE;
137 //UNUSED2008-05 }
138 //UNUSED2008-05 Flags.bFlag3D = (rBools.bOldFlag3D & SRF_3D ? TRUE : FALSE);
139 //UNUSED2008-05 Flags.bRelName = (rBools.bOldFlag3D & SRF_RELNAME ? TRUE : FALSE);
140 //UNUSED2008-05 if ( !Flags.bFlag3D )
141 //UNUSED2008-05 Flags.bTabRel = TRUE; // ist bei einigen aelteren Dokumenten nicht gesetzt
142 //UNUSED2008-05 }
143 //UNUSED2008-05
144 //UNUSED2008-05
145 //UNUSED2008-05 /*
146 //UNUSED2008-05 bis Release 3.1 sah Store so aus
147 //UNUSED2008-05
148 //UNUSED2008-05 BYTE n = ( ( r.bOldFlag3D & 0x03 ) << 6 ) // RelName, 3D
149 //UNUSED2008-05 | ( ( r.bRelTab & 0x03 ) << 4 ) // Relative, RelAbs
150 //UNUSED2008-05 | ( ( r.bRelRow & 0x03 ) << 2 )
151 //UNUSED2008-05 | ( r.bRelCol & 0x03 );
152 //UNUSED2008-05
153 //UNUSED2008-05 bis Release 3.1 sah Load so aus
154 //UNUSED2008-05
155 //UNUSED2008-05 r.bRelCol = ( n & 0x03 );
156 //UNUSED2008-05 r.bRelRow = ( ( n >> 2 ) & 0x03 );
157 //UNUSED2008-05 r.bRelTab = ( ( n >> 4 ) & 0x03 );
158 //UNUSED2008-05 r.bOldFlag3D = ( ( n >> 6 ) & 0x03 );
159 //UNUSED2008-05
160 //UNUSED2008-05 bRelCol == SR_DELETED war identisch mit bRelCol == (SR_RELATIVE | SR_RELABS)
161 //UNUSED2008-05 leider..
162 //UNUSED2008-05 3.1 liest Zukunft: Deleted wird nicht unbedingt erkannt, nur wenn auch Relativ.
163 //UNUSED2008-05 Aber immer noch nCol > MAXCOL und gut sollte sein..
164 //UNUSED2008-05 */
165 //UNUSED2008-05
166 //UNUSED2008-05 BYTE ScSingleRefData::CreateStoreByteFromFlags() const
167 //UNUSED2008-05 {
168 //UNUSED2008-05 return (BYTE)(
169 //UNUSED2008-05 ( (Flags.bRelName & 0x01) << 7 )
170 //UNUSED2008-05 | ( (Flags.bFlag3D & 0x01) << 6 )
171 //UNUSED2008-05 | ( (Flags.bTabDeleted & 0x01) << 5 )
172 //UNUSED2008-05 | ( (Flags.bTabRel & 0x01) << 4 )
173 //UNUSED2008-05 | ( (Flags.bRowDeleted & 0x01) << 3 )
174 //UNUSED2008-05 | ( (Flags.bRowRel & 0x01) << 2 )
175 //UNUSED2008-05 | ( (Flags.bColDeleted & 0x01) << 1 )
176 //UNUSED2008-05 | (Flags.bColRel & 0x01)
177 //UNUSED2008-05 );
178 //UNUSED2008-05 }
179 //UNUSED2008-05
180 //UNUSED2008-05
181 //UNUSED2008-05 void ScSingleRefData::CreateFlagsFromLoadByte( BYTE n )
182 //UNUSED2008-05 {
183 //UNUSED2008-05 Flags.bColRel = (n & 0x01 );
184 //UNUSED2008-05 Flags.bColDeleted = ( (n >> 1) & 0x01 );
185 //UNUSED2008-05 Flags.bRowRel = ( (n >> 2) & 0x01 );
186 //UNUSED2008-05 Flags.bRowDeleted = ( (n >> 3) & 0x01 );
187 //UNUSED2008-05 Flags.bTabRel = ( (n >> 4) & 0x01 );
188 //UNUSED2008-05 Flags.bTabDeleted = ( (n >> 5) & 0x01 );
189 //UNUSED2008-05 Flags.bFlag3D = ( (n >> 6) & 0x01 );
190 //UNUSED2008-05 Flags.bRelName = ( (n >> 7) & 0x01 );
191 //UNUSED2008-05 }
194 BOOL ScSingleRefData::operator==( const ScSingleRefData& r ) const
196 return bFlags == r.bFlags &&
197 (Flags.bColRel ? nRelCol == r.nRelCol : nCol == r.nCol) &&
198 (Flags.bRowRel ? nRelRow == r.nRelRow : nRow == r.nRow) &&
199 (Flags.bTabRel ? nRelTab == r.nRelTab : nTab == r.nTab);
202 bool ScSingleRefData::operator!=( const ScSingleRefData& r ) const
204 return !operator==(r);
207 static void lcl_putInOrder( ScSingleRefData & rRef1, ScSingleRefData & rRef2 )
209 SCCOL nCol1, nCol2;
210 SCROW nRow1, nRow2;
211 SCTAB nTab1, nTab2;
212 BOOL bTmp;
213 BYTE nRelState1, nRelState2;
214 if ( rRef1.Flags.bRelName )
215 nRelState1 =
216 ((rRef1.Flags.bTabRel & 0x01) << 2)
217 | ((rRef1.Flags.bRowRel & 0x01) << 1)
218 | ((rRef1.Flags.bColRel & 0x01));
219 else
220 nRelState1 = 0;
221 if ( rRef2.Flags.bRelName )
222 nRelState2 =
223 ((rRef2.Flags.bTabRel & 0x01) << 2)
224 | ((rRef2.Flags.bRowRel & 0x01) << 1)
225 | ((rRef2.Flags.bColRel & 0x01));
226 else
227 nRelState2 = 0;
228 if ( (nCol1 = rRef1.nCol) > (nCol2 = rRef2.nCol) )
230 rRef1.nCol = nCol2;
231 rRef2.nCol = nCol1;
232 nCol1 = rRef1.nRelCol;
233 rRef1.nRelCol = rRef2.nRelCol;
234 rRef2.nRelCol = nCol1;
235 if ( rRef1.Flags.bRelName && rRef1.Flags.bColRel )
236 nRelState2 |= 1;
237 else
238 nRelState2 &= ~1;
239 if ( rRef2.Flags.bRelName && rRef2.Flags.bColRel )
240 nRelState1 |= 1;
241 else
242 nRelState1 &= ~1;
243 bTmp = rRef1.Flags.bColRel;
244 rRef1.Flags.bColRel = rRef2.Flags.bColRel;
245 rRef2.Flags.bColRel = bTmp;
246 bTmp = rRef1.Flags.bColDeleted;
247 rRef1.Flags.bColDeleted = rRef2.Flags.bColDeleted;
248 rRef2.Flags.bColDeleted = bTmp;
250 if ( (nRow1 = rRef1.nRow) > (nRow2 = rRef2.nRow) )
252 rRef1.nRow = nRow2;
253 rRef2.nRow = nRow1;
254 nRow1 = rRef1.nRelRow;
255 rRef1.nRelRow = rRef2.nRelRow;
256 rRef2.nRelRow = nRow1;
257 if ( rRef1.Flags.bRelName && rRef1.Flags.bRowRel )
258 nRelState2 |= 2;
259 else
260 nRelState2 &= ~2;
261 if ( rRef2.Flags.bRelName && rRef2.Flags.bRowRel )
262 nRelState1 |= 2;
263 else
264 nRelState1 &= ~2;
265 bTmp = rRef1.Flags.bRowRel;
266 rRef1.Flags.bRowRel = rRef2.Flags.bRowRel;
267 rRef2.Flags.bRowRel = bTmp;
268 bTmp = rRef1.Flags.bRowDeleted;
269 rRef1.Flags.bRowDeleted = rRef2.Flags.bRowDeleted;
270 rRef2.Flags.bRowDeleted = bTmp;
272 if ( (nTab1 = rRef1.nTab) > (nTab2 = rRef2.nTab) )
274 rRef1.nTab = nTab2;
275 rRef2.nTab = nTab1;
276 nTab1 = rRef1.nRelTab;
277 rRef1.nRelTab = rRef2.nRelTab;
278 rRef2.nRelTab = nTab1;
279 if ( rRef1.Flags.bRelName && rRef1.Flags.bTabRel )
280 nRelState2 |= 4;
281 else
282 nRelState2 &= ~4;
283 if ( rRef2.Flags.bRelName && rRef2.Flags.bTabRel )
284 nRelState1 |= 4;
285 else
286 nRelState1 &= ~4;
287 bTmp = rRef1.Flags.bTabRel;
288 rRef1.Flags.bTabRel = rRef2.Flags.bTabRel;
289 rRef2.Flags.bTabRel = bTmp;
290 bTmp = rRef1.Flags.bTabDeleted;
291 rRef1.Flags.bTabDeleted = rRef2.Flags.bTabDeleted;
292 rRef2.Flags.bTabDeleted = bTmp;
294 rRef1.Flags.bRelName = ( nRelState1 ? TRUE : FALSE );
295 rRef2.Flags.bRelName = ( nRelState2 ? TRUE : FALSE );
299 void ScComplexRefData::PutInOrder()
301 lcl_putInOrder( Ref1, Ref2);
305 static void lcl_adjustInOrder( ScSingleRefData & rRef1, ScSingleRefData & rRef2, bool bFirstLeader )
307 // a1:a2:a3, bFirstLeader: rRef1==a1==r1, rRef2==a3==r2
308 // else: rRef1==a3==r2, rRef2==a2==r1
309 ScSingleRefData& r1 = (bFirstLeader ? rRef1 : rRef2);
310 ScSingleRefData& r2 = (bFirstLeader ? rRef2 : rRef1);
311 if (r1.Flags.bFlag3D && !r2.Flags.bFlag3D)
313 // [$]Sheet1.A5:A6 on Sheet2 do still refer only Sheet1.
314 r2.nTab = r1.nTab;
315 r2.nRelTab = r1.nRelTab;
316 r2.Flags.bTabRel = r1.Flags.bTabRel;
318 lcl_putInOrder( rRef1, rRef2);
322 ScComplexRefData& ScComplexRefData::Extend( const ScSingleRefData & rRef, const ScAddress & rPos )
324 CalcAbsIfRel( rPos);
325 ScSingleRefData aRef = rRef;
326 aRef.CalcAbsIfRel( rPos);
327 bool bInherit3D = Ref1.IsFlag3D() && !Ref2.IsFlag3D();
328 bool bInherit3Dtemp = bInherit3D && !rRef.IsFlag3D();
329 if (aRef.nCol < Ref1.nCol || aRef.nRow < Ref1.nRow || aRef.nTab < Ref1.nTab)
331 lcl_adjustInOrder( Ref1, aRef, true);
332 aRef = rRef;
333 aRef.CalcAbsIfRel( rPos);
335 if (aRef.nCol > Ref2.nCol || aRef.nRow > Ref2.nRow || aRef.nTab > Ref2.nTab)
337 if (bInherit3D)
338 Ref2.SetFlag3D( true);
339 lcl_adjustInOrder( aRef, Ref2, false);
340 if (bInherit3Dtemp)
341 Ref2.SetFlag3D( false);
342 aRef = rRef;
343 aRef.CalcAbsIfRel( rPos);
345 // In Ref2 use absolute/relative addressing from non-extended parts if
346 // equal and therefor not adjusted.
347 // A$5:A5 => A$5:A$5:A5 => A$5:A5, and not A$5:A$5
348 // A$6:$A5 => A$6:A$6:$A5 => A5:$A$6
349 if (Ref2.nCol == aRef.nCol)
350 Ref2.SetColRel( aRef.IsColRel());
351 if (Ref2.nRow == aRef.nRow)
352 Ref2.SetRowRel( aRef.IsRowRel());
353 // $Sheet1.$A$5:$A$6 => $Sheet1.$A$5:$A$5:$A$6 => $Sheet1.$A$5:$A$6, and
354 // not $Sheet1.$A$5:Sheet1.$A$6 (with invisible second 3D, but relative).
355 if (Ref2.nTab == aRef.nTab)
356 Ref2.SetTabRel( bInherit3Dtemp ? Ref1.IsTabRel() : aRef.IsTabRel());
357 Ref2.CalcRelFromAbs( rPos);
358 // Force 3D if necessary. References to other sheets always.
359 if (Ref1.nTab != rPos.Tab())
360 Ref1.SetFlag3D( true);
361 // In the second part only if different sheet thus not inherited.
362 if (Ref2.nTab != Ref1.nTab)
363 Ref2.SetFlag3D( true);
364 // Merge Flag3D to Ref2 in case there was nothing to inherit and/or range
365 // wasn't extended as in A5:A5:Sheet1.A5 if on Sheet1.
366 if (rRef.IsFlag3D())
367 Ref2.SetFlag3D( true);
368 return *this;
372 ScComplexRefData& ScComplexRefData::Extend( const ScComplexRefData & rRef, const ScAddress & rPos )
374 return Extend( rRef.Ref1, rPos).Extend( rRef.Ref2, rPos);