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 76715 : SwRect::SwRect( const Rectangle &rRect ) :
30 76715 : m_Point( rRect.Left(), rRect.Top() )
31 : {
32 76715 : m_Size.setWidth(rRect.Right() == RECT_EMPTY ? 0 :
33 76715 : rRect.Right() - rRect.Left() +1);
34 76715 : m_Size.setHeight(rRect.Bottom() == RECT_EMPTY ? 0 :
35 76715 : rRect.Bottom() - rRect.Top() + 1);
36 76715 : }
37 :
38 3573 : Point SwRect::Center() const
39 : {
40 3573 : return Point( Left() + Width() / 2,
41 7146 : Top() + Height() / 2 );
42 : }
43 :
44 767 : SwRect& SwRect::Union( const SwRect& rRect )
45 : {
46 767 : if ( Top() > rRect.Top() )
47 25 : Top( rRect.Top() );
48 767 : if ( Left() > rRect.Left() )
49 1 : Left( rRect.Left() );
50 767 : long n = rRect.Right();
51 767 : if ( Right() < n )
52 29 : Right( n );
53 767 : n = rRect.Bottom();
54 767 : if ( Bottom() < n )
55 640 : Bottom( n );
56 767 : return *this;
57 : }
58 :
59 2583 : SwRect& SwRect::Intersection( const SwRect& rRect )
60 : {
61 : // any similarity between me and given element?
62 2583 : if ( IsOver( rRect ) )
63 : {
64 : // get smaller right and lower, and greater left and upper edge
65 2545 : if ( Left() < rRect.Left() )
66 18 : Left( rRect.Left() );
67 2545 : if ( Top() < rRect.Top() )
68 86 : Top( rRect.Top() );
69 2545 : long n = rRect.Right();
70 2545 : if ( Right() > n )
71 649 : Right( n );
72 2545 : n = rRect.Bottom();
73 2545 : if ( Bottom() > n )
74 1064 : Bottom( n );
75 : }
76 : else
77 : // Def.: if intersection is empty, set only SSize to 0
78 38 : SSize(0, 0);
79 :
80 2583 : return *this;
81 : }
82 :
83 12351 : SwRect& SwRect::_Intersection( const SwRect& rRect )
84 : {
85 : // get smaller right and lower, and greater left and upper edge
86 12351 : if ( Left() < rRect.Left() )
87 1654 : Left( rRect.Left() );
88 12351 : if ( Top() < rRect.Top() )
89 1877 : Top( rRect.Top() );
90 12351 : long n = rRect.Right();
91 12351 : if ( Right() > n )
92 6605 : Right( n );
93 12351 : n = rRect.Bottom();
94 12351 : if ( Bottom() > n )
95 4058 : Bottom( n );
96 :
97 12351 : return *this;
98 : }
99 :
100 4593 : sal_Bool SwRect::IsInside( const SwRect& rRect ) const
101 : {
102 4593 : const long nRight = Right();
103 4593 : const long nBottom = Bottom();
104 4593 : const long nrRight = rRect.Right();
105 4593 : const long nrBottom= rRect.Bottom();
106 8449 : return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
107 3604 : (Left() <= nrRight) && (nrRight <= nRight) &&
108 5935 : (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
109 17988 : (Top() <= nrBottom) && (nrBottom <= nBottom);
110 : }
111 :
112 13925 : sal_Bool SwRect::IsInside( const Point& rPoint ) const
113 : {
114 13925 : return (Left() <= rPoint.X()) &&
115 10189 : (Top() <= rPoint.Y()) &&
116 10137 : (Right() >= rPoint.X()) &&
117 34251 : (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 43963 : sal_Bool SwRect::IsOver( const SwRect& rRect ) const
132 : {
133 43963 : return (Top() <= rRect.Bottom()) &&
134 41097 : (Left() <= rRect.Right()) &&
135 40832 : (Right() >= rRect.Left()) &&
136 125892 : (Bottom()>= rRect.Top());
137 : }
138 :
139 450 : void SwRect::Justify()
140 : {
141 450 : 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 450 : 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 450 : }
152 :
153 : // Similiar to the inline methods, but we need the function pointers
154 5649 : void SwRect::_Width( const long nNew ) { m_Size.setWidth(nNew); }
155 10322 : void SwRect::_Height( const long nNew ) { m_Size.setHeight(nNew); }
156 11820 : void SwRect::_Left( const long nLeft ){ m_Size.Width() += m_Point.getX() - nLeft; m_Point.setX(nLeft); }
157 10739 : void SwRect::_Right( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
158 422 : void SwRect::_Top( const long nTop ){ m_Size.Height() += m_Point.getY() - nTop; m_Point.setY(nTop); }
159 214 : void SwRect::_Bottom( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
160 :
161 77301 : long SwRect::_Width() const{ return m_Size.getWidth(); }
162 117119 : long SwRect::_Height() const{ return m_Size.getHeight(); }
163 97563 : long SwRect::_Left() const{ return m_Point.getX(); }
164 45142 : long SwRect::_Right() const{ return m_Point.getX() + m_Size.getWidth(); }
165 83863 : long SwRect::_Top() const{ return m_Point.getY(); }
166 9244 : 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 1383 : void SwRect::SubLeft( const long nSub ){ m_Size.Width() += nSub; m_Point.X() -= nSub; }
171 4987 : void SwRect::AddRight( const long nAdd ){ m_Size.Width() += nAdd; }
172 1780 : void SwRect::SubTop( const long nSub ){ m_Size.Height() += nSub; m_Point.Y() -= nSub; }
173 5670 : void SwRect::AddBottom( const long nAdd ){ m_Size.Height() += nAdd; }
174 2526 : void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
175 4207 : 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 19884 : const Point SwRect::TopLeft() const { return Pos(); }
181 709 : const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
182 693 : const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
183 693 : const Point SwRect::BottomRight() const
184 693 : { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
185 :
186 844 : long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
187 3986 : long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
188 10131 : 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 118 : sal_Bool SwRect::OverStepLeft( long nLimit ) const
192 118 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
193 2078 : sal_Bool SwRect::OverStepBottom( long nLimit ) const
194 2078 : { 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 6 : void SwRect::SetLeftAndWidth( long nLeft, long nNew )
201 : {
202 6 : m_Point.setX(nLeft);
203 6 : m_Size.setWidth(nNew);
204 6 : }
205 1148 : void SwRect::SetTopAndHeight( long nTop, long nNew )
206 : {
207 1148 : m_Point.setY(nTop);
208 1148 : m_Size.setHeight(nNew);
209 1148 : }
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 3256 : void SwRect::SetUpperLeftCorner( const Point& rNew )
221 3256 : { 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: */
|