xref: /trunk/main/sw/source/core/bastyp/swrect.cxx (revision 86e1cf34)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 #ifdef DBG_UTIL
29 #ifndef _STREAM_HXX //autogen
30 #include <tools/stream.hxx>
31 #endif
32 #endif
33 #include <stdlib.h>
34 #include "swrect.hxx"
35 #include <math.h>
36 
37 /*************************************************************************
38 |*
39 |*	SwRect::SwRect()
40 |*
41 |*	Ersterstellung		MA 02. Feb. 93
42 |*	Letzte Aenderung	MA 05. Sep. 93
43 |*
44 |*************************************************************************/
45 
46 
47 
SwRect(const Rectangle & rRect)48 SwRect::SwRect( const Rectangle &rRect ) :
49 	m_Point( rRect.Left(), rRect.Top() )
50 {
51 	m_Size.setWidth(rRect.Right() == RECT_EMPTY ? 0 :
52 							rRect.Right()  - rRect.Left() +1);
53 	m_Size.setHeight(rRect.Bottom() == RECT_EMPTY ? 0 :
54 							rRect.Bottom() - rRect.Top() + 1);
55 }
56 
57 /*************************************************************************
58 |*
59 |*	SwRect::Center()
60 |*
61 |*	Ersterstellung		MA 27. Jan. 93
62 |*	Letzte Aenderung	MA 27. Jan. 93
63 |*
64 |*************************************************************************/
Center() const65 Point SwRect::Center() const
66 {
67 	return Point( Left() + Width()  / 2,
68 				  Top()  + Height() / 2 );
69 
70 /*  Wer ruft schon ein Center auf ein "falsches" Rechteck?
71 	const long nRight = Right();
72 	const long nBottom= Bottom();
73 	return Point( min( Left(), nRight ) + long(abs( (nRight - Left())/2) ),
74 				  min( Top(),  nBottom) + long(abs( (nBottom - Top())/2)));
75 */
76 }
77 
78 /*************************************************************************
79 |*
80 |*	SwRect::Union()
81 |*
82 |*	Ersterstellung		MA 27. Jan. 93
83 |*	Letzte Aenderung	MA 27. Jan. 93
84 |*
85 |*************************************************************************/
86 
87 
88 
Union(const SwRect & rRect)89 SwRect& SwRect::Union( const SwRect& rRect )
90 {
91 	if ( Top() > rRect.Top() )
92 		Top( rRect.Top() );
93 	if ( Left() > rRect.Left() )
94 		Left( rRect.Left() );
95 	long n = rRect.Right();
96 	if ( Right() < n )
97 		Right( n );
98 	n = rRect.Bottom();
99 	if ( Bottom() < n )
100 		Bottom( n );
101 	return *this;
102 }
103 /*************************************************************************
104 |*
105 |*	SwRect::Intersection(), _Intersection()
106 |*
107 |*	Ersterstellung		MA 27. Jan. 93
108 |*	Letzte Aenderung	MA 05. Sep. 93
109 |*
110 |*************************************************************************/
111 
112 
113 
Intersection(const SwRect & rRect)114 SwRect& SwRect::Intersection( const SwRect& rRect )
115 {
116 	//Hat das Teil ueberhaupt Gemeinsamkeiten mit mir?
117 	if ( IsOver( rRect ) )
118 	{
119 		//Bestimmung der kleineren  rechten sowie unteren und
120 		//           der groesseren linken  sowie oberen Kante.
121 		if ( Left() < rRect.Left() )
122 			Left( rRect.Left() );
123 		if ( Top() < rRect.Top() )
124 			Top( rRect.Top() );
125 		long n = rRect.Right();
126 		if ( Right() > n )
127 			Right( n );
128 		n = rRect.Bottom();
129 		if ( Bottom() > n )
130 			Bottom( n );
131 	}
132 	else
133 		//Def.: Bei einer leeren Intersection wird nur die SSize genullt.
134 		SSize(0, 0);
135 
136 	return *this;
137 }
138 
139 
140 
_Intersection(const SwRect & rRect)141 SwRect& SwRect::_Intersection( const SwRect& rRect )
142 {
143 	//Bestimmung der kleineren  rechten sowie unteren und
144 	//           der groesseren linken  sowie oberen Kante.
145 	if ( Left() < rRect.Left() )
146 		Left( rRect.Left() );
147 	if ( Top() < rRect.Top() )
148 		Top( rRect.Top() );
149 	long n = rRect.Right();
150 	if ( Right() > n )
151 		Right( n );
152 	n = rRect.Bottom();
153 	if ( Bottom() > n )
154 		Bottom( n );
155 
156 	return *this;
157 }
158 /*************************************************************************
159 |*
160 |*	SwRect::IsInside()
161 |*
162 |*	Ersterstellung		MA 27. Jan. 93
163 |*	Letzte Aenderung	MA 27. Jan. 93
164 |*
165 |*************************************************************************/
166 
167 
168 
IsInside(const SwRect & rRect) const169 sal_Bool SwRect::IsInside( const SwRect& rRect ) const
170 {
171 	const long nRight  = Right();
172 	const long nBottom = Bottom();
173 	const long nrRight = rRect.Right();
174 	const long nrBottom= rRect.Bottom();
175 	return (Left() <= rRect.Left()) && (rRect.Left()<= nRight)  &&
176 		   (Left() <= nrRight)		&& (nrRight		<= nRight)  &&
177 		   (Top()  <= rRect.Top())	&& (rRect.Top() <= nBottom) &&
178 		   (Top()  <= nrBottom)		&& (nrBottom	<= nBottom);
179 }
180 
181 
182 
IsInside(const Point & rPoint) const183 sal_Bool SwRect::IsInside( const Point& rPoint ) const
184 {
185 	return    (Left()  <= rPoint.X())
186 		   && (Top()   <= rPoint.Y())
187 		   && (Right() >= rPoint.X())
188 		   && (Bottom()>= rPoint.Y());
189 }
190 /* -----------------------------11.04.00 15:46--------------------------------
191 	mouse moving of table borders
192  ---------------------------------------------------------------------------*/
IsNear(const Point & rPoint,long nTolerance) const193 sal_Bool SwRect::IsNear( const Point& rPoint, long nTolerance ) const
194 {
195 	return    IsInside(rPoint) ||
196 		(((Left() - nTolerance)  <= rPoint.X())
197 		   && ((Top()  - nTolerance)  <= rPoint.Y())
198 		   && ((Right() + nTolerance) >= rPoint.X())
199 		   && ((Bottom()  + nTolerance)>= rPoint.Y()));
200 }
201 
202 /*************************************************************************
203 |*
204 |*	SwRect::IsOver()
205 |*
206 |*	Ersterstellung		MA 25. Feb. 94
207 |*	Letzte Aenderung	MA 27. Jun. 96
208 |*
209 |*************************************************************************/
210 
211 
212 
IsOver(const SwRect & rRect) const213 sal_Bool SwRect::IsOver( const SwRect& rRect ) const
214 {
215 	return	  (Top()   <= rRect.Bottom())
216 		   && (Left()  <= rRect.Right())
217 		   && (Right() >= rRect.Left())
218 		   && (Bottom()>= rRect.Top()) ? sal_True : sal_False;
219 }
220 
221 /*************************************************************************
222 |*
223 |*	SwRect::Justify()
224 |*
225 |*	Ersterstellung		MA 10. Oct. 94
226 |*	Letzte Aenderung	MA 23. Oct. 96
227 |*
228 |*************************************************************************/
229 
230 
231 
Justify()232 void SwRect::Justify()
233 {
234 	if ( m_Size.getHeight() < 0 )
235 	{
236 		m_Point.Y() += m_Size.getHeight() + 1;
237 		m_Size.setHeight(-m_Size.getHeight());
238 	}
239 	if ( m_Size.getWidth() < 0 )
240 	{
241 		m_Point.X() += m_Size.getWidth() + 1;
242 		m_Size.setWidth(-m_Size.getWidth());
243 	}
244 }
245 
246 
247 // Similar to the inline methods, but we need the function pointers
248 
_Width(const long nNew)249 void SwRect::_Width( const long nNew ) { m_Size.setWidth(nNew); }
_Height(const long nNew)250 void SwRect::_Height( const long nNew ) { m_Size.setHeight(nNew); }
_Left(const long nLeft)251 void SwRect::_Left( const long nLeft ){ m_Size.Width() += m_Point.getX() - nLeft; m_Point.setX(nLeft); }
_Right(const long nRight)252 void SwRect::_Right( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
_Top(const long nTop)253 void SwRect::_Top( const long nTop ){ m_Size.Height() += m_Point.getY() - nTop; m_Point.setY(nTop); }
_Bottom(const long nBottom)254 void SwRect::_Bottom( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
255 
_Width() const256 long SwRect::_Width() const{ return m_Size.getWidth(); }
_Height() const257 long SwRect::_Height() const{ return m_Size.getHeight(); }
_Left() const258 long SwRect::_Left() const{ return m_Point.getX(); }
_Right() const259 long SwRect::_Right() const{ return m_Point.getX() + m_Size.getWidth(); }
_Top() const260 long SwRect::_Top() const{ return m_Point.getY(); }
_Bottom() const261 long SwRect::_Bottom() const{ return m_Point.getY() + m_Size.getHeight(); }
262 
AddWidth(const long nAdd)263 void SwRect::AddWidth( const long nAdd ) { m_Size.Width() += nAdd; }
AddHeight(const long nAdd)264 void SwRect::AddHeight( const long nAdd ) { m_Size.Height() += nAdd; }
SubLeft(const long nSub)265 void SwRect::SubLeft( const long nSub ){ m_Size.Width() += nSub; m_Point.X() -= nSub; }
AddRight(const long nAdd)266 void SwRect::AddRight( const long nAdd ){ m_Size.Width() += nAdd; }
SubTop(const long nSub)267 void SwRect::SubTop( const long nSub ){ m_Size.Height() += nSub; m_Point.Y() -= nSub; }
AddBottom(const long nAdd)268 void SwRect::AddBottom( const long nAdd ){ m_Size.Height() += nAdd; }
SetPosX(const long nNew)269 void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
SetPosY(const long nNew)270 void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
_Size() const271 const Size  SwRect::_Size() const { return SSize(); }
SwappedSize() const272 const Size  SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
TopLeft() const273 const Point SwRect::TopLeft() const { return Pos(); }
TopRight() const274 const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
BottomLeft() const275 const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
BottomRight() const276 const Point SwRect::BottomRight() const
277     { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
GetLeftDistance(long nLimit) const278 long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
GetBottomDistance(long nLim) const279 long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
GetTopDistance(long nLimit) const280 long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
GetRightDistance(long nLim) const281 long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
OverStepLeft(long nLimit) const282 sal_Bool SwRect::OverStepLeft( long nLimit ) const
283     { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
OverStepBottom(long nLimit) const284 sal_Bool SwRect::OverStepBottom( long nLimit ) const
285     { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
OverStepTop(long nLimit) const286 sal_Bool SwRect::OverStepTop( long nLimit ) const
287     { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
OverStepRight(long nLimit) const288 sal_Bool SwRect::OverStepRight( long nLimit ) const
289     { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
SetLeftAndWidth(long nLeft,long nNew)290 void SwRect::SetLeftAndWidth( long nLeft, long nNew )
291 {
292     m_Point.setX(nLeft);
293     m_Size.setWidth(nNew);
294 }
SetTopAndHeight(long nTop,long nNew)295 void SwRect::SetTopAndHeight( long nTop, long nNew )
296 {
297     m_Point.setY(nTop);
298     m_Size.setHeight(nNew);
299 }
SetRightAndWidth(long nRight,long nNew)300 void SwRect::SetRightAndWidth( long nRight, long nNew )
301 {
302     m_Point.setX(nRight - nNew);
303     m_Size.setWidth(nNew);
304 }
SetBottomAndHeight(long nBottom,long nNew)305 void SwRect::SetBottomAndHeight( long nBottom, long nNew )
306 {
307     m_Point.setY(nBottom - nNew);
308     m_Size.setHeight(nNew);
309 }
SetUpperLeftCorner(const Point & rNew)310 void SwRect::SetUpperLeftCorner(  const Point& rNew )
311     { m_Point = rNew; }
SetUpperRightCorner(const Point & rNew)312 void SwRect::SetUpperRightCorner(  const Point& rNew )
313     { m_Point = Point(rNew.nA - m_Size.getWidth(), rNew.nB); }
SetLowerLeftCorner(const Point & rNew)314 void SwRect::SetLowerLeftCorner(  const Point& rNew )
315     { m_Point = Point(rNew.nA, rNew.nB - m_Size.getHeight()); }
316 
317 #ifdef DBG_UTIL
318 /*************************************************************************
319  *					operator<<( ostream&, SwRect&)
320  *************************************************************************/
321 
322 
323 
operator <<(SvStream & rStream,const SwRect & rRect)324 SvStream &operator<<( SvStream &rStream, const SwRect &rRect )
325 {
326 	rStream << '[' << rRect.Top()   << '/' << rRect.Left()
327 			<< ',' << rRect.Width() << 'x' << rRect.Height() << "] ";
328 	return rStream;
329 }
330 #endif
331 
332 
333