Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "swrect.hxx"
30 : :
31 : : #ifdef DBG_UTIL
32 : : #include <tools/stream.hxx>
33 : : #endif
34 : :
35 : : #include <math.h>
36 : : #include <stdlib.h>
37 : :
38 : 647336 : SwRect::SwRect( const Rectangle &rRect ) :
39 : 647336 : m_Point( rRect.Left(), rRect.Top() )
40 : : {
41 : 647336 : m_Size.setWidth(rRect.Right() == RECT_EMPTY ? 0 :
42 [ + + ]: 647336 : rRect.Right() - rRect.Left() +1);
43 : 647336 : m_Size.setHeight(rRect.Bottom() == RECT_EMPTY ? 0 :
44 [ + + ]: 647336 : rRect.Bottom() - rRect.Top() + 1);
45 : 647336 : }
46 : :
47 : 139355 : Point SwRect::Center() const
48 : : {
49 : 139355 : return Point( Left() + Width() / 2,
50 : 278710 : Top() + Height() / 2 );
51 : : }
52 : :
53 : 16785 : SwRect& SwRect::Union( const SwRect& rRect )
54 : : {
55 [ + + ]: 16785 : if ( Top() > rRect.Top() )
56 : 2420 : Top( rRect.Top() );
57 [ + + ]: 16785 : if ( Left() > rRect.Left() )
58 : 2063 : Left( rRect.Left() );
59 : 16785 : long n = rRect.Right();
60 [ + + ]: 16785 : if ( Right() < n )
61 : 4503 : Right( n );
62 : 16785 : n = rRect.Bottom();
63 [ + + ]: 16785 : if ( Bottom() < n )
64 : 13207 : Bottom( n );
65 : 16785 : return *this;
66 : : }
67 : :
68 : 51209 : SwRect& SwRect::Intersection( const SwRect& rRect )
69 : : {
70 : : // any similarity between me and given element?
71 [ + + ]: 51209 : if ( IsOver( rRect ) )
72 : : {
73 : : // get smaller right and lower, and greater left and upper edge
74 [ + + ]: 36617 : if ( Left() < rRect.Left() )
75 : 1361 : Left( rRect.Left() );
76 [ + + ]: 36617 : if ( Top() < rRect.Top() )
77 : 7777 : Top( rRect.Top() );
78 : 36617 : long n = rRect.Right();
79 [ + + ]: 36617 : if ( Right() > n )
80 : 10400 : Right( n );
81 : 36617 : n = rRect.Bottom();
82 [ + + ]: 36617 : if ( Bottom() > n )
83 : 13447 : Bottom( n );
84 : : }
85 : : else
86 : : // Def.: if intersection is empty, set only SSize to 0
87 : 14592 : SSize(0, 0);
88 : :
89 : 51209 : return *this;
90 : : }
91 : :
92 : 177379 : SwRect& SwRect::_Intersection( const SwRect& rRect )
93 : : {
94 : : // get smaller right and lower, and greater left and upper edge
95 [ + + ]: 177379 : if ( Left() < rRect.Left() )
96 : 87926 : Left( rRect.Left() );
97 [ + + ]: 177379 : if ( Top() < rRect.Top() )
98 : 85117 : Top( rRect.Top() );
99 : 177379 : long n = rRect.Right();
100 [ + + ]: 177379 : if ( Right() > n )
101 : 114943 : Right( n );
102 : 177379 : n = rRect.Bottom();
103 [ + + ]: 177379 : if ( Bottom() > n )
104 : 91305 : Bottom( n );
105 : :
106 : 177379 : return *this;
107 : : }
108 : :
109 : 89020 : sal_Bool SwRect::IsInside( const SwRect& rRect ) const
110 : : {
111 : 89020 : const long nRight = Right();
112 : 89020 : const long nBottom = Bottom();
113 : 89020 : const long nrRight = rRect.Right();
114 : 89020 : const long nrBottom= rRect.Bottom();
115 : 154675 : return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
116 : 60421 : (Left() <= nrRight) && (nrRight <= nRight) &&
117 : 101513 : (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
118 [ + + + + : 316609 : (Top() <= nrBottom) && (nrBottom <= nBottom);
+ + ][ + - ]
[ + + ][ + +
+ + + - ]
119 : : }
120 : :
121 : 250626 : sal_Bool SwRect::IsInside( const Point& rPoint ) const
122 : : {
123 : 250626 : return (Left() <= rPoint.X()) &&
124 : 140939 : (Top() <= rPoint.Y()) &&
125 : 139801 : (Right() >= rPoint.X()) &&
126 [ + + ][ + + : 531366 : (Bottom()>= rPoint.Y());
+ + + + ]
127 : : }
128 : :
129 : : // mouse moving of table borders
130 : 0 : sal_Bool SwRect::IsNear( const Point& rPoint, long nTolerance ) const
131 : : {
132 : 0 : bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) &&
133 : 0 : ((Top() - nTolerance) <= rPoint.Y()) &&
134 : 0 : ((Right() + nTolerance) >= rPoint.X()) &&
135 [ # # ][ # # : 0 : ((Bottom() + nTolerance) >= rPoint.Y()));
# # # # ]
136 [ # # ][ # # ]: 0 : return IsInside(rPoint) || bIsNearby;
137 : : }
138 : :
139 : :
140 : 687478 : sal_Bool SwRect::IsOver( const SwRect& rRect ) const
141 : : {
142 : 687478 : return (Top() <= rRect.Bottom()) &&
143 : 560137 : (Left() <= rRect.Right()) &&
144 : 529939 : (Right() >= rRect.Left()) &&
145 [ + + ][ + + : 1777554 : (Bottom()>= rRect.Top());
+ + + + ]
146 : : }
147 : :
148 : 431 : void SwRect::Justify()
149 : : {
150 [ - + ]: 431 : if ( m_Size.getHeight() < 0 )
151 : : {
152 : 0 : m_Point.Y() += m_Size.getHeight() + 1;
153 : 0 : m_Size.setHeight(-m_Size.getHeight());
154 : : }
155 [ - + ]: 431 : if ( m_Size.getWidth() < 0 )
156 : : {
157 : 0 : m_Point.X() += m_Size.getWidth() + 1;
158 : 0 : m_Size.setWidth(-m_Size.getWidth());
159 : : }
160 : 431 : }
161 : :
162 : : // Similiar to the inline methods, but we need the function pointers
163 : 67190 : void SwRect::_Width( const long nNew ) { m_Size.setWidth(nNew); }
164 : 37888 : void SwRect::_Height( const long nNew ) { m_Size.setHeight(nNew); }
165 : 168285 : void SwRect::_Left( const long nLeft ){ m_Size.Width() += m_Point.getX() - nLeft; m_Point.setX(nLeft); }
166 : 163807 : void SwRect::_Right( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
167 : 6898 : void SwRect::_Top( const long nTop ){ m_Size.Height() += m_Point.getY() - nTop; m_Point.setY(nTop); }
168 : 1506 : void SwRect::_Bottom( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
169 : :
170 : 466831 : long SwRect::_Width() const{ return m_Size.getWidth(); }
171 : 594119 : long SwRect::_Height() const{ return m_Size.getHeight(); }
172 : 1033615 : long SwRect::_Left() const{ return m_Point.getX(); }
173 : 713052 : long SwRect::_Right() const{ return m_Point.getX() + m_Size.getWidth(); }
174 : 515767 : long SwRect::_Top() const{ return m_Point.getY(); }
175 : 132020 : long SwRect::_Bottom() const{ return m_Point.getY() + m_Size.getHeight(); }
176 : :
177 : 0 : void SwRect::AddWidth( const long nAdd ) { m_Size.Width() += nAdd; }
178 : 0 : void SwRect::AddHeight( const long nAdd ) { m_Size.Height() += nAdd; }
179 : 5397 : void SwRect::SubLeft( const long nSub ){ m_Size.Width() += nSub; m_Point.X() -= nSub; }
180 : 18977 : void SwRect::AddRight( const long nAdd ){ m_Size.Width() += nAdd; }
181 : 8709 : void SwRect::SubTop( const long nSub ){ m_Size.Height() += nSub; m_Point.Y() -= nSub; }
182 : 22449 : void SwRect::AddBottom( const long nAdd ){ m_Size.Height() += nAdd; }
183 : 44928 : void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
184 : 18435 : void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
185 : :
186 : 0 : const Size SwRect::_Size() const { return SSize(); }
187 : 0 : const Size SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
188 : :
189 : 144260 : const Point SwRect::TopLeft() const { return Pos(); }
190 : 12479 : const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
191 : 12464 : const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
192 : 12464 : const Point SwRect::BottomRight() const
193 : 12464 : { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
194 : :
195 : 20251 : long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
196 : 33906 : long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
197 : 105244 : long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
198 : 2 : long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
199 : :
200 : 1593 : sal_Bool SwRect::OverStepLeft( long nLimit ) const
201 [ + + ][ - + ]: 1593 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
202 : 19213 : sal_Bool SwRect::OverStepBottom( long nLimit ) const
203 [ + + ][ + + ]: 19213 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
204 : 0 : sal_Bool SwRect::OverStepTop( long nLimit ) const
205 [ # # ][ # # ]: 0 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
206 : 0 : sal_Bool SwRect::OverStepRight( long nLimit ) const
207 [ # # ][ # # ]: 0 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
208 : :
209 : 4 : void SwRect::SetLeftAndWidth( long nLeft, long nNew )
210 : : {
211 : 4 : m_Point.setX(nLeft);
212 : 4 : m_Size.setWidth(nNew);
213 : 4 : }
214 : 833 : void SwRect::SetTopAndHeight( long nTop, long nNew )
215 : : {
216 : 833 : m_Point.setY(nTop);
217 : 833 : m_Size.setHeight(nNew);
218 : 833 : }
219 : 0 : void SwRect::SetRightAndWidth( long nRight, long nNew )
220 : : {
221 : 0 : m_Point.setX(nRight - nNew);
222 : 0 : m_Size.setWidth(nNew);
223 : 0 : }
224 : 0 : void SwRect::SetBottomAndHeight( long nBottom, long nNew )
225 : : {
226 : 0 : m_Point.setY(nBottom - nNew);
227 : 0 : m_Size.setHeight(nNew);
228 : 0 : }
229 : 7926 : void SwRect::SetUpperLeftCorner( const Point& rNew )
230 : 7926 : { m_Point = rNew; }
231 : 0 : void SwRect::SetUpperRightCorner( const Point& rNew )
232 : 0 : { m_Point = Point(rNew.nA - m_Size.getWidth(), rNew.nB); }
233 : 0 : void SwRect::SetLowerLeftCorner( const Point& rNew )
234 : 0 : { m_Point = Point(rNew.nA, rNew.nB - m_Size.getHeight()); }
235 : :
236 : : #ifdef DBG_UTIL
237 : : SvStream &operator<<( SvStream &rStream, const SwRect &rRect )
238 : : {
239 : : rStream << '[' << static_cast<sal_Int32>(rRect.Top())
240 : : << '/' << static_cast<sal_Int32>(rRect.Left())
241 : : << ',' << static_cast<sal_Int32>(rRect.Width())
242 : : << 'x' << static_cast<sal_Int32>(rRect.Height())
243 : : << "] ";
244 : : return rStream;
245 : : }
246 : : #endif
247 : :
248 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|