Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "swrect.hxx"
21 :
22 : #ifdef DBG_UTIL
23 : #include <tools/stream.hxx>
24 : #endif
25 :
26 : #include <math.h>
27 : #include <stdlib.h>
28 :
29 33360 : SwRect::SwRect( const Rectangle &rRect ) :
30 33360 : m_Point( rRect.Left(), rRect.Top() )
31 : {
32 33360 : m_Size.setWidth(rRect.Right() == RECT_EMPTY ? 0 :
33 33360 : rRect.Right() - rRect.Left() +1);
34 33360 : m_Size.setHeight(rRect.Bottom() == RECT_EMPTY ? 0 :
35 33360 : rRect.Bottom() - rRect.Top() + 1);
36 33360 : }
37 :
38 1674 : Point SwRect::Center() const
39 : {
40 1674 : return Point( Left() + Width() / 2,
41 3348 : Top() + Height() / 2 );
42 : }
43 :
44 367 : SwRect& SwRect::Union( const SwRect& rRect )
45 : {
46 367 : if ( Top() > rRect.Top() )
47 12 : Top( rRect.Top() );
48 367 : if ( Left() > rRect.Left() )
49 0 : Left( rRect.Left() );
50 367 : long n = rRect.Right();
51 367 : if ( Right() < n )
52 14 : Right( n );
53 367 : n = rRect.Bottom();
54 367 : if ( Bottom() < n )
55 312 : Bottom( n );
56 367 : return *this;
57 : }
58 :
59 1117 : SwRect& SwRect::Intersection( const SwRect& rRect )
60 : {
61 : // any similarity between me and given element?
62 1117 : if ( IsOver( rRect ) )
63 : {
64 : // get smaller right and lower, and greater left and upper edge
65 1096 : if ( Left() < rRect.Left() )
66 8 : Left( rRect.Left() );
67 1096 : if ( Top() < rRect.Top() )
68 38 : Top( rRect.Top() );
69 1096 : long n = rRect.Right();
70 1096 : if ( Right() > n )
71 262 : Right( n );
72 1096 : n = rRect.Bottom();
73 1096 : if ( Bottom() > n )
74 441 : Bottom( n );
75 : }
76 : else
77 : // Def.: if intersection is empty, set only SSize to 0
78 21 : SSize(0, 0);
79 :
80 1117 : return *this;
81 : }
82 :
83 5320 : SwRect& SwRect::_Intersection( const SwRect& rRect )
84 : {
85 : // get smaller right and lower, and greater left and upper edge
86 5320 : if ( Left() < rRect.Left() )
87 658 : Left( rRect.Left() );
88 5320 : if ( Top() < rRect.Top() )
89 756 : Top( rRect.Top() );
90 5320 : long n = rRect.Right();
91 5320 : if ( Right() > n )
92 2596 : Right( n );
93 5320 : n = rRect.Bottom();
94 5320 : if ( Bottom() > n )
95 1787 : Bottom( n );
96 :
97 5320 : return *this;
98 : }
99 :
100 2075 : sal_Bool SwRect::IsInside( const SwRect& rRect ) const
101 : {
102 2075 : const long nRight = Right();
103 2075 : const long nBottom = Bottom();
104 2075 : const long nrRight = rRect.Right();
105 2075 : const long nrBottom= rRect.Bottom();
106 3866 : return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
107 1662 : (Left() <= nrRight) && (nrRight <= nRight) &&
108 2870 : (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
109 8398 : (Top() <= nrBottom) && (nrBottom <= nBottom);
110 : }
111 :
112 6407 : sal_Bool SwRect::IsInside( const Point& rPoint ) const
113 : {
114 6407 : return (Left() <= rPoint.X()) &&
115 4666 : (Top() <= rPoint.Y()) &&
116 4640 : (Right() >= rPoint.X()) &&
117 15713 : (Bottom()>= rPoint.Y());
118 : }
119 :
120 : // mouse moving of table borders
121 0 : sal_Bool SwRect::IsNear( const Point& rPoint, long nTolerance ) const
122 : {
123 0 : bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) &&
124 0 : ((Top() - nTolerance) <= rPoint.Y()) &&
125 0 : ((Right() + nTolerance) >= rPoint.X()) &&
126 0 : ((Bottom() + nTolerance) >= rPoint.Y()));
127 0 : return IsInside(rPoint) || bIsNearby;
128 : }
129 :
130 :
131 19868 : sal_Bool SwRect::IsOver( const SwRect& rRect ) const
132 : {
133 19868 : return (Top() <= rRect.Bottom()) &&
134 18835 : (Left() <= rRect.Right()) &&
135 18712 : (Right() >= rRect.Left()) &&
136 57415 : (Bottom()>= rRect.Top());
137 : }
138 :
139 165 : void SwRect::Justify()
140 : {
141 165 : if ( m_Size.getHeight() < 0 )
142 : {
143 0 : m_Point.Y() += m_Size.getHeight() + 1;
144 0 : m_Size.setHeight(-m_Size.getHeight());
145 : }
146 165 : if ( m_Size.getWidth() < 0 )
147 : {
148 0 : m_Point.X() += m_Size.getWidth() + 1;
149 0 : m_Size.setWidth(-m_Size.getWidth());
150 : }
151 165 : }
152 :
153 : // Similiar to the inline methods, but we need the function pointers
154 2415 : void SwRect::_Width( const long nNew ) { m_Size.setWidth(nNew); }
155 4494 : void SwRect::_Height( const long nNew ) { m_Size.setHeight(nNew); }
156 5840 : void SwRect::_Left( const long nLeft ){ m_Size.Width() += m_Point.getX() - nLeft; m_Point.setX(nLeft); }
157 5348 : void SwRect::_Right( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
158 172 : void SwRect::_Top( const long nTop ){ m_Size.Height() += m_Point.getY() - nTop; m_Point.setY(nTop); }
159 100 : void SwRect::_Bottom( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
160 :
161 35121 : long SwRect::_Width() const{ return m_Size.getWidth(); }
162 52852 : long SwRect::_Height() const{ return m_Size.getHeight(); }
163 49128 : long SwRect::_Left() const{ return m_Point.getX(); }
164 25741 : long SwRect::_Right() const{ return m_Point.getX() + m_Size.getWidth(); }
165 39299 : long SwRect::_Top() const{ return m_Point.getY(); }
166 4828 : long SwRect::_Bottom() const{ return m_Point.getY() + m_Size.getHeight(); }
167 :
168 0 : void SwRect::AddWidth( const long nAdd ) { m_Size.Width() += nAdd; }
169 0 : void SwRect::AddHeight( const long nAdd ) { m_Size.Height() += nAdd; }
170 622 : void SwRect::SubLeft( const long nSub ){ m_Size.Width() += nSub; m_Point.X() -= nSub; }
171 2349 : void SwRect::AddRight( const long nAdd ){ m_Size.Width() += nAdd; }
172 850 : void SwRect::SubTop( const long nSub ){ m_Size.Height() += nSub; m_Point.Y() -= nSub; }
173 2717 : void SwRect::AddBottom( const long nAdd ){ m_Size.Height() += nAdd; }
174 1150 : void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
175 1884 : void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
176 :
177 0 : const Size SwRect::_Size() const { return SSize(); }
178 0 : const Size SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
179 :
180 8745 : const Point SwRect::TopLeft() const { return Pos(); }
181 282 : const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
182 276 : const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
183 276 : const Point SwRect::BottomRight() const
184 276 : { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
185 :
186 976 : long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
187 1671 : long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
188 4316 : long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
189 0 : long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
190 :
191 59 : sal_Bool SwRect::OverStepLeft( long nLimit ) const
192 59 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
193 919 : sal_Bool SwRect::OverStepBottom( long nLimit ) const
194 919 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
195 0 : sal_Bool SwRect::OverStepTop( long nLimit ) const
196 0 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
197 0 : sal_Bool SwRect::OverStepRight( long nLimit ) const
198 0 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
199 :
200 3 : void SwRect::SetLeftAndWidth( long nLeft, long nNew )
201 : {
202 3 : m_Point.setX(nLeft);
203 3 : m_Size.setWidth(nNew);
204 3 : }
205 438 : void SwRect::SetTopAndHeight( long nTop, long nNew )
206 : {
207 438 : m_Point.setY(nTop);
208 438 : m_Size.setHeight(nNew);
209 438 : }
210 0 : void SwRect::SetRightAndWidth( long nRight, long nNew )
211 : {
212 0 : m_Point.setX(nRight - nNew);
213 0 : m_Size.setWidth(nNew);
214 0 : }
215 0 : void SwRect::SetBottomAndHeight( long nBottom, long nNew )
216 : {
217 0 : m_Point.setY(nBottom - nNew);
218 0 : m_Size.setHeight(nNew);
219 0 : }
220 1490 : void SwRect::SetUpperLeftCorner( const Point& rNew )
221 1490 : { m_Point = rNew; }
222 0 : void SwRect::SetUpperRightCorner( const Point& rNew )
223 0 : { m_Point = Point(rNew.A() - m_Size.getWidth(), rNew.B()); }
224 0 : void SwRect::SetLowerLeftCorner( const Point& rNew )
225 0 : { m_Point = Point(rNew.A(), rNew.B() - m_Size.getHeight()); }
226 :
227 : #ifdef DBG_UTIL
228 : SvStream &operator<<( SvStream &rStream, const SwRect &rRect )
229 : {
230 : rStream << '[' << static_cast<sal_Int32>(rRect.Top())
231 : << '/' << static_cast<sal_Int32>(rRect.Left())
232 : << ',' << static_cast<sal_Int32>(rRect.Width())
233 : << 'x' << static_cast<sal_Int32>(rRect.Height())
234 : << "] ";
235 : return rStream;
236 : }
237 : #endif
238 :
239 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|