xref: /AOO41X/main/sc/inc/bigrange.hxx (revision 38d50f7b14e1cf975d8c6468d9633894cd59b523)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SC_BIGRANGE_HXX
25 #define SC_BIGRANGE_HXX
26 
27 
28 #include "global.hxx"
29 #include "document.hxx"
30 
31 
32 static const sal_Int32 nInt32Min = 0x80000000;
33 static const sal_Int32 nInt32Max = 0x7fffffff;
34 
35 
36 class ScBigAddress
37 {
38     sal_Int32   nRow;
39     sal_Int32   nCol;
40     sal_Int32   nTab;
41 
42 public:
ScBigAddress()43             ScBigAddress() : nRow(0), nCol(0), nTab(0) {}
ScBigAddress(sal_Int32 nColP,sal_Int32 nRowP,sal_Int32 nTabP)44             ScBigAddress( sal_Int32 nColP, sal_Int32 nRowP, sal_Int32 nTabP )
45                 : nRow( nRowP ), nCol( nColP ), nTab( nTabP ) {}
ScBigAddress(const ScBigAddress & r)46             ScBigAddress( const ScBigAddress& r )
47                 : nRow( r.nRow ), nCol( r.nCol ), nTab( r.nTab ) {}
ScBigAddress(const ScAddress & r)48             ScBigAddress( const ScAddress& r )
49                 : nRow( r.Row() ), nCol( r.Col() ), nTab( r.Tab() ) {}
50 
Col() const51     sal_Int32   Col() const { return nCol; }
Row() const52     sal_Int32   Row() const { return nRow; }
Tab() const53     sal_Int32   Tab() const { return nTab; }
54 
Set(sal_Int32 nColP,sal_Int32 nRowP,sal_Int32 nTabP)55     void    Set( sal_Int32 nColP, sal_Int32 nRowP, sal_Int32 nTabP )
56                 { nCol = nColP; nRow = nRowP; nTab = nTabP; }
SetCol(sal_Int32 nColP)57     void    SetCol( sal_Int32 nColP ) { nCol = nColP; }
SetRow(sal_Int32 nRowP)58     void    SetRow( sal_Int32 nRowP ) { nRow = nRowP; }
SetTab(sal_Int32 nTabP)59     void    SetTab( sal_Int32 nTabP ) { nTab = nTabP; }
IncCol(sal_Int32 n=1)60     void    IncCol( sal_Int32 n = 1 ) { nCol += n; }
IncRow(sal_Int32 n=1)61     void    IncRow( sal_Int32 n = 1 ) { nRow += n; }
IncTab(sal_Int32 n=1)62     void    IncTab( sal_Int32 n = 1 ) { nTab += n; }
63 
GetVars(sal_Int32 & nColP,sal_Int32 & nRowP,sal_Int32 & nTabP) const64     void    GetVars( sal_Int32& nColP, sal_Int32& nRowP, sal_Int32& nTabP ) const
65                 { nColP = nCol; nRowP = nRow; nTabP = nTab; }
66 
67     inline void     PutInOrder( ScBigAddress& r );
68     inline sal_Bool     IsValid( const ScDocument* ) const;
69     inline ScAddress    MakeAddress() const;
70 
operator =(const ScBigAddress & r)71     ScBigAddress&   operator=( const ScBigAddress& r )
72                     { nCol = r.nCol; nRow = r.nRow; nTab = r.nTab; return *this; }
operator =(const ScAddress & r)73     ScBigAddress&   operator=( const ScAddress& r )
74                     { nCol = r.Col(); nRow = r.Row(); nTab = r.Tab(); return *this; }
operator ==(const ScBigAddress & r) const75     int             operator==( const ScBigAddress& r ) const
76                     { return nCol == r.nCol && nRow == r.nRow && nTab == r.nTab; }
operator !=(const ScBigAddress & r) const77     int             operator!=( const ScBigAddress& r ) const
78                     { return !operator==( r ); }
79 
80     friend inline SvStream& operator<< ( SvStream& rStream, const ScBigAddress& rAdr );
81     friend inline SvStream& operator>> ( SvStream& rStream, ScBigAddress& rAdr );
82 };
83 
84 
PutInOrder(ScBigAddress & r)85 inline void ScBigAddress::PutInOrder( ScBigAddress& r )
86 {
87     sal_Int32 nTmp;
88     if ( r.nCol < nCol )
89     {
90         nTmp = r.nCol;
91         r.nCol = nCol;
92         nCol = nTmp;
93     }
94     if ( r.nRow < nRow )
95     {
96         nTmp = r.nRow;
97         r.nRow = nRow;
98         nRow = nTmp;
99     }
100     if ( r.nTab < nTab )
101     {
102         nTmp = r.nTab;
103         r.nTab = nTab;
104         nTab = nTmp;
105     }
106 }
107 
108 
IsValid(const ScDocument * pDoc) const109 inline sal_Bool ScBigAddress::IsValid( const ScDocument* pDoc ) const
110 {   //! Min/Max sind ok, kennzeichnen ganze Col/Row/Tab
111     return
112         ((0 <= nCol && nCol <= MAXCOL)
113             || nCol == nInt32Min || nCol == nInt32Max) &&
114         ((0 <= nRow && nRow <= MAXROW)
115             || nRow == nInt32Min || nRow == nInt32Max) &&
116         ((0 <= nTab && nTab < pDoc->GetTableCount())
117             || nTab == nInt32Min || nTab == nInt32Max)
118         ;
119 }
120 
121 
MakeAddress() const122 inline ScAddress ScBigAddress::MakeAddress() const
123 {
124     SCCOL nColA;
125     SCROW nRowA;
126     SCTAB nTabA;
127 
128     if ( nCol < 0 )
129         nColA = 0;
130     else if ( nCol > MAXCOL )
131         nColA = MAXCOL;
132     else
133         nColA = (SCCOL) nCol;
134 
135     if ( nRow < 0 )
136         nRowA = 0;
137     else if ( nRow > MAXROW )
138         nRowA = MAXROW;
139     else
140         nRowA = (SCROW) nRow;
141 
142     if ( nTab < 0 )
143         nTabA = 0;
144     else if ( nTab > MAXTAB )
145         nTabA = MAXTAB;
146     else
147         nTabA = (SCTAB) nTab;
148 
149     return ScAddress( nColA, nRowA, nTabA );
150 }
151 
152 
operator <<(SvStream & rStream,const ScBigAddress & rAdr)153 inline SvStream& operator<< ( SvStream& rStream, const ScBigAddress& rAdr )
154 {
155     rStream << rAdr.nCol << rAdr.nRow << rAdr.nTab;
156     return rStream;
157 }
158 
159 
operator >>(SvStream & rStream,ScBigAddress & rAdr)160 inline SvStream& operator>> ( SvStream& rStream, ScBigAddress& rAdr )
161 {
162     rStream >> rAdr.nCol >> rAdr.nRow >> rAdr.nTab;
163     return rStream;
164 }
165 
166 
167 class ScBigRange
168 {
169 public:
170 
171     ScBigAddress    aStart;
172     ScBigAddress    aEnd;
173 
ScBigRange()174                     ScBigRange() : aStart(), aEnd() {}
ScBigRange(const ScBigAddress & s,const ScBigAddress & e)175                     ScBigRange( const ScBigAddress& s, const ScBigAddress& e )
176                         : aStart( s ), aEnd( e ) { aStart.PutInOrder( aEnd ); }
ScBigRange(const ScBigRange & r)177                     ScBigRange( const ScBigRange& r )
178                         : aStart( r.aStart ), aEnd( r.aEnd ) {}
ScBigRange(const ScRange & r)179                     ScBigRange( const ScRange& r )
180                         : aStart( r.aStart ), aEnd( r.aEnd ) {}
ScBigRange(const ScBigAddress & r)181                     ScBigRange( const ScBigAddress& r )
182                         : aStart( r ), aEnd( r ) {}
ScBigRange(const ScAddress & r)183                     ScBigRange( const ScAddress& r )
184                         : aStart( r ), aEnd( r ) {}
ScBigRange(sal_Int32 nCol,sal_Int32 nRow,sal_Int32 nTab)185                     ScBigRange( sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nTab )
186                         : aStart( nCol, nRow, nTab ), aEnd( aStart ) {}
ScBigRange(sal_Int32 nCol1,sal_Int32 nRow1,sal_Int32 nTab1,sal_Int32 nCol2,sal_Int32 nRow2,sal_Int32 nTab2)187                     ScBigRange( sal_Int32 nCol1, sal_Int32 nRow1, sal_Int32 nTab1,
188                             sal_Int32 nCol2, sal_Int32 nRow2, sal_Int32 nTab2 )
189                         : aStart( nCol1, nRow1, nTab1 ),
190                         aEnd( nCol2, nRow2, nTab2 ) {}
191 
Set(sal_Int32 nCol1,sal_Int32 nRow1,sal_Int32 nTab1,sal_Int32 nCol2,sal_Int32 nRow2,sal_Int32 nTab2)192     void    Set( sal_Int32 nCol1, sal_Int32 nRow1, sal_Int32 nTab1,
193                      sal_Int32 nCol2, sal_Int32 nRow2, sal_Int32 nTab2 )
194                 { aStart.Set( nCol1, nRow1, nTab1 );
195                     aEnd.Set( nCol2, nRow2, nTab2 ); }
196 
GetVars(sal_Int32 & nCol1,sal_Int32 & nRow1,sal_Int32 & nTab1,sal_Int32 & nCol2,sal_Int32 & nRow2,sal_Int32 & nTab2) const197     void    GetVars( sal_Int32& nCol1, sal_Int32& nRow1, sal_Int32& nTab1,
198                      sal_Int32& nCol2, sal_Int32& nRow2, sal_Int32& nTab2 ) const
199                 { aStart.GetVars( nCol1, nRow1, nTab1 );
200                     aEnd.GetVars( nCol2, nRow2, nTab2 ); }
201 
IsValid(const ScDocument * pDoc) const202     sal_Bool    IsValid( const ScDocument* pDoc ) const
203                 { return aStart.IsValid( pDoc ) && aEnd.IsValid( pDoc ); }
MakeRange() const204     inline ScRange  MakeRange() const
205                     { return ScRange( aStart.MakeAddress(),
206                         aEnd.MakeAddress() ); }
207 
208     inline sal_Bool In( const ScBigAddress& ) const;    // ist Address& in Range?
209     inline sal_Bool In( const ScBigRange& ) const;      // ist Range& in Range?
210     inline sal_Bool Intersects( const ScBigRange& ) const;  // ueberschneiden sich zwei Ranges?
211 
operator =(const ScBigRange & r)212     ScBigRange&     operator=( const ScBigRange& r )
213                         { aStart = r.aStart; aEnd = r.aEnd; return *this; }
operator ==(const ScBigRange & r) const214     int             operator==( const ScBigRange& r ) const
215                         { return (aStart == r.aStart) && (aEnd == r.aEnd); }
operator !=(const ScBigRange & r) const216     int             operator!=( const ScBigRange& r ) const
217                         { return !operator==( r ); }
218 
219     friend inline SvStream& operator<< ( SvStream& rStream, const ScBigRange& rRange );
220     friend inline SvStream& operator>> ( SvStream& rStream, ScBigRange& rRange );
221 };
222 
223 
In(const ScBigAddress & rAddr) const224 inline sal_Bool ScBigRange::In( const ScBigAddress& rAddr ) const
225 {
226     return
227         aStart.Col() <= rAddr.Col() && rAddr.Col() <= aEnd.Col() &&
228         aStart.Row() <= rAddr.Row() && rAddr.Row() <= aEnd.Row() &&
229         aStart.Tab() <= rAddr.Tab() && rAddr.Tab() <= aEnd.Tab();
230 }
231 
232 
In(const ScBigRange & r) const233 inline sal_Bool ScBigRange::In( const ScBigRange& r ) const
234 {
235     return
236         aStart.Col() <= r.aStart.Col() && r.aEnd.Col() <= aEnd.Col() &&
237         aStart.Row() <= r.aStart.Row() && r.aEnd.Row() <= aEnd.Row() &&
238         aStart.Tab() <= r.aStart.Tab() && r.aEnd.Tab() <= aEnd.Tab();
239 }
240 
241 
Intersects(const ScBigRange & r) const242 inline sal_Bool ScBigRange::Intersects( const ScBigRange& r ) const
243 {
244     return !(
245         Min( aEnd.Col(), r.aEnd.Col() ) < Max( aStart.Col(), r.aStart.Col() )
246      || Min( aEnd.Row(), r.aEnd.Row() ) < Max( aStart.Row(), r.aStart.Row() )
247      || Min( aEnd.Tab(), r.aEnd.Tab() ) < Max( aStart.Tab(), r.aStart.Tab() )
248         );
249 }
250 
251 
operator <<(SvStream & rStream,const ScBigRange & rRange)252 inline SvStream& operator<< ( SvStream& rStream, const ScBigRange& rRange )
253 {
254     rStream << rRange.aStart;
255     rStream << rRange.aEnd;
256     return rStream;
257 }
258 
259 
operator >>(SvStream & rStream,ScBigRange & rRange)260 inline SvStream& operator>> ( SvStream& rStream, ScBigRange& rRange )
261 {
262     rStream >> rRange.aStart;
263     rStream >> rRange.aEnd;
264     return rStream;
265 }
266 
267 
268 
269 #endif
270