merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / bastyp / swrect.cxx
blobd241982182a0e31eb444dfea47ef40f9a5ad7e84
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: swrect.cxx,v $
10 * $Revision: 1.19 $
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_sw.hxx"
35 #ifndef PRODUCT
36 #ifndef _STREAM_HXX //autogen
37 #include <tools/stream.hxx>
38 #endif
39 #endif
40 #include <stdlib.h>
41 #include "swrect.hxx"
42 #include <math.h>
44 /*************************************************************************
46 |* SwRect::SwRect()
48 |* Ersterstellung MA 02. Feb. 93
49 |* Letzte Aenderung MA 05. Sep. 93
51 |*************************************************************************/
55 SwRect::SwRect( const Rectangle &rRect ) :
56 nX( rRect.Left() ),
57 nY( rRect.Top() )
59 nWidth = rRect.Right() == RECT_EMPTY ? 0 :
60 rRect.Right() - rRect.Left() +1;
61 nHeight = rRect.Bottom() == RECT_EMPTY ? 0 :
62 rRect.Bottom() - rRect.Top() + 1;
65 /*************************************************************************
67 |* SwRect::Center()
69 |* Ersterstellung MA 27. Jan. 93
70 |* Letzte Aenderung MA 27. Jan. 93
72 |*************************************************************************/
73 Point SwRect::Center() const
75 return Point( Left() + Width() / 2,
76 Top() + Height() / 2 );
78 /* Wer ruft schon ein Center auf ein "falsches" Rechteck?
79 const long nRight = Right();
80 const long nBottom= Bottom();
81 return Point( min( Left(), nRight ) + long(abs( (nRight - Left())/2) ),
82 min( Top(), nBottom) + long(abs( (nBottom - Top())/2)));
86 /*************************************************************************
88 |* SwRect::Union()
90 |* Ersterstellung MA 27. Jan. 93
91 |* Letzte Aenderung MA 27. Jan. 93
93 |*************************************************************************/
97 SwRect& SwRect::Union( const SwRect& rRect )
99 if ( Top() > rRect.Top() )
100 Top( rRect.Top() );
101 if ( Left() > rRect.Left() )
102 Left( rRect.Left() );
103 long n = rRect.Right();
104 if ( Right() < n )
105 Right( n );
106 n = rRect.Bottom();
107 if ( Bottom() < n )
108 Bottom( n );
109 return *this;
111 /*************************************************************************
113 |* SwRect::Intersection(), _Intersection()
115 |* Ersterstellung MA 27. Jan. 93
116 |* Letzte Aenderung MA 05. Sep. 93
118 |*************************************************************************/
122 SwRect& SwRect::Intersection( const SwRect& rRect )
124 //Hat das Teil ueberhaupt Gemeinsamkeiten mit mir?
125 if ( IsOver( rRect ) )
127 //Bestimmung der kleineren rechten sowie unteren und
128 // der groesseren linken sowie oberen Kante.
129 if ( Left() < rRect.Left() )
130 Left( rRect.Left() );
131 if ( Top() < rRect.Top() )
132 Top( rRect.Top() );
133 long n = rRect.Right();
134 if ( Right() > n )
135 Right( n );
136 n = rRect.Bottom();
137 if ( Bottom() > n )
138 Bottom( n );
140 else
141 //Def.: Bei einer leeren Intersection wird nur die SSize genullt.
142 nHeight = nWidth = 0;
144 return *this;
149 SwRect& SwRect::_Intersection( const SwRect& rRect )
151 //Bestimmung der kleineren rechten sowie unteren und
152 // der groesseren linken sowie oberen Kante.
153 if ( Left() < rRect.Left() )
154 Left( rRect.Left() );
155 if ( Top() < rRect.Top() )
156 Top( rRect.Top() );
157 long n = rRect.Right();
158 if ( Right() > n )
159 Right( n );
160 n = rRect.Bottom();
161 if ( Bottom() > n )
162 Bottom( n );
164 return *this;
166 /*************************************************************************
168 |* SwRect::IsInside()
170 |* Ersterstellung MA 27. Jan. 93
171 |* Letzte Aenderung MA 27. Jan. 93
173 |*************************************************************************/
177 BOOL SwRect::IsInside( const SwRect& rRect ) const
179 const long nRight = Right();
180 const long nBottom = Bottom();
181 const long nrRight = rRect.Right();
182 const long nrBottom= rRect.Bottom();
183 return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
184 (Left() <= nrRight) && (nrRight <= nRight) &&
185 (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
186 (Top() <= nrBottom) && (nrBottom <= nBottom);
191 BOOL SwRect::IsInside( const Point& rPoint ) const
193 return (Left() <= rPoint.X())
194 && (Top() <= rPoint.Y())
195 && (Right() >= rPoint.X())
196 && (Bottom()>= rPoint.Y());
198 /* -----------------------------11.04.00 15:46--------------------------------
199 mouse moving of table borders
200 ---------------------------------------------------------------------------*/
201 BOOL SwRect::IsNear( const Point& rPoint, long nTolerance ) const
203 return IsInside(rPoint) ||
204 (((Left() - nTolerance) <= rPoint.X())
205 && ((Top() - nTolerance) <= rPoint.Y())
206 && ((Right() + nTolerance) >= rPoint.X())
207 && ((Bottom() + nTolerance)>= rPoint.Y()));
210 /*************************************************************************
212 |* SwRect::IsOver()
214 |* Ersterstellung MA 25. Feb. 94
215 |* Letzte Aenderung MA 27. Jun. 96
217 |*************************************************************************/
221 BOOL SwRect::IsOver( const SwRect& rRect ) const
223 return (Top() <= rRect.Bottom())
224 && (Left() <= rRect.Right())
225 && (Right() >= rRect.Left())
226 && (Bottom()>= rRect.Top()) ? TRUE : FALSE;
229 /*************************************************************************
231 |* SwRect::Justify()
233 |* Ersterstellung MA 10. Oct. 94
234 |* Letzte Aenderung MA 23. Oct. 96
236 |*************************************************************************/
240 void SwRect::Justify()
242 if ( nHeight < 0 )
244 nY = nY + nHeight + 1;
245 nHeight = -nHeight;
247 if ( nWidth < 0 )
249 nX = nX + nWidth + 1;
250 nWidth = -nWidth;
255 // Similiar to the inline methods, but we need the function pointers
257 void SwRect::_Width( const long nNew ) { nWidth = nNew; }
258 void SwRect::_Height( const long nNew ) { nHeight = nNew; }
259 void SwRect::_Left( const long nLeft ){ nWidth += nX - nLeft; nX = nLeft; }
260 void SwRect::_Right( const long nRight ){ nWidth = nRight - nX; }
261 void SwRect::_Top( const long nTop ){ nHeight += nY - nTop; nY = nTop; }
262 void SwRect::_Bottom( const long nBottom ){ nHeight = nBottom - nY; }
264 long SwRect::_Width() const{ return nWidth; }
265 long SwRect::_Height() const{ return nHeight; }
266 long SwRect::_Left() const{ return nX; }
267 long SwRect::_Right() const{ return nX + nWidth; }
268 long SwRect::_Top() const{ return nY; }
269 long SwRect::_Bottom() const{ return nY + nHeight; }
271 void SwRect::AddWidth( const long nAdd ) { nWidth += nAdd; }
272 void SwRect::AddHeight( const long nAdd ) { nHeight += nAdd; }
273 void SwRect::SubLeft( const long nSub ){ nWidth += nSub; nX -= nSub; }
274 void SwRect::AddRight( const long nAdd ){ nWidth += nAdd; }
275 void SwRect::SubTop( const long nSub ){ nHeight += nSub; nY -= nSub; }
276 void SwRect::AddBottom( const long nAdd ){ nHeight += nAdd; }
277 void SwRect::SetPosX( const long nNew ){ nX = nNew; }
278 void SwRect::SetPosY( const long nNew ){ nY = nNew; }
279 const Size SwRect::_Size() const { return SSize(); }
280 const Size SwRect::SwappedSize() const { return Size( nHeight, nWidth ); }
281 const Point SwRect::TopLeft() const { return Pos(); }
282 const Point SwRect::TopRight() const { return Point( nX + nWidth, nY ); }
283 const Point SwRect::BottomLeft() const { return Point( nX, nY + nHeight ); }
284 const Point SwRect::BottomRight() const
285 { return Point( nX + nWidth, nY + nHeight ); }
286 long SwRect::GetLeftDistance( long nLimit ) const { return nX - nLimit; }
287 long SwRect::GetBottomDistance( long nLim ) const { return nLim - nY - nHeight;}
288 long SwRect::GetTopDistance( long nLimit ) const { return nY - nLimit; }
289 long SwRect::GetRightDistance( long nLim ) const { return nLim - nX - nWidth; }
290 BOOL SwRect::OverStepLeft( long nLimit ) const
291 { return nLimit > nX && nX + nWidth > nLimit; }
292 BOOL SwRect::OverStepBottom( long nLimit ) const
293 { return nLimit > nY && nY + nHeight > nLimit; }
294 BOOL SwRect::OverStepTop( long nLimit ) const
295 { return nLimit > nY && nY + nHeight > nLimit; }
296 BOOL SwRect::OverStepRight( long nLimit ) const
297 { return nLimit > nX && nX + nWidth > nLimit; }
298 void SwRect::SetLeftAndWidth( long nLeft, long nNew )
299 { nX = nLeft; nWidth = nNew; }
300 void SwRect::SetTopAndHeight( long nTop, long nNew )
301 { nY = nTop; nHeight = nNew; }
302 void SwRect::SetRightAndWidth( long nRight, long nNew )
303 { nX = nRight - nNew; nWidth = nNew; }
304 void SwRect::SetBottomAndHeight( long nBottom, long nNew )
305 { nY = nBottom - nNew; nHeight = nNew; }
306 void SwRect::SetUpperLeftCorner( const Point& rNew )
307 { nX = rNew.nA; nY = rNew.nB; }
308 void SwRect::SetUpperRightCorner( const Point& rNew )
309 { nX = rNew.nA - nWidth; nY = rNew.nB; }
310 void SwRect::SetLowerLeftCorner( const Point& rNew )
311 { nX = rNew.nA; nY = rNew.nB - nHeight; }
313 #ifndef PRODUCT
314 /*************************************************************************
315 * operator<<( ostream&, SwRect&)
316 *************************************************************************/
320 SvStream &operator<<( SvStream &rStream, const SwRect &rRect )
322 rStream << '[' << rRect.Top() << '/' << rRect.Left()
323 << ',' << rRect.Width() << 'x' << rRect.Height() << "] ";
324 return rStream;
326 #endif