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 0 : SwRect::SwRect( const Rectangle &rRect ) :
30 0 : m_Point( rRect.Left(), rRect.Top() )
31 : {
32 0 : m_Size.setWidth(rRect.Right() == RECT_EMPTY ? 0 :
33 0 : rRect.Right() - rRect.Left() +1);
34 0 : m_Size.setHeight(rRect.Bottom() == RECT_EMPTY ? 0 :
35 0 : rRect.Bottom() - rRect.Top() + 1);
36 0 : }
37 :
38 0 : Point SwRect::Center() const
39 : {
40 0 : return Point( Left() + Width() / 2,
41 0 : Top() + Height() / 2 );
42 : }
43 :
44 0 : SwRect& SwRect::Union( const SwRect& rRect )
45 : {
46 0 : if ( Top() > rRect.Top() )
47 0 : Top( rRect.Top() );
48 0 : if ( Left() > rRect.Left() )
49 0 : Left( rRect.Left() );
50 0 : long n = rRect.Right();
51 0 : if ( Right() < n )
52 0 : Right( n );
53 0 : n = rRect.Bottom();
54 0 : if ( Bottom() < n )
55 0 : Bottom( n );
56 0 : return *this;
57 : }
58 :
59 0 : SwRect& SwRect::Intersection( const SwRect& rRect )
60 : {
61 : // any similarity between me and given element?
62 0 : if ( IsOver( rRect ) )
63 : {
64 : // get smaller right and lower, and greater left and upper edge
65 0 : if ( Left() < rRect.Left() )
66 0 : Left( rRect.Left() );
67 0 : if ( Top() < rRect.Top() )
68 0 : Top( rRect.Top() );
69 0 : long n = rRect.Right();
70 0 : if ( Right() > n )
71 0 : Right( n );
72 0 : n = rRect.Bottom();
73 0 : if ( Bottom() > n )
74 0 : Bottom( n );
75 : }
76 : else
77 : // Def.: if intersection is empty, set only SSize to 0
78 0 : SSize(0, 0);
79 :
80 0 : return *this;
81 : }
82 :
83 0 : SwRect& SwRect::_Intersection( const SwRect& rRect )
84 : {
85 : // get smaller right and lower, and greater left and upper edge
86 0 : if ( Left() < rRect.Left() )
87 0 : Left( rRect.Left() );
88 0 : if ( Top() < rRect.Top() )
89 0 : Top( rRect.Top() );
90 0 : long n = rRect.Right();
91 0 : if ( Right() > n )
92 0 : Right( n );
93 0 : n = rRect.Bottom();
94 0 : if ( Bottom() > n )
95 0 : Bottom( n );
96 :
97 0 : return *this;
98 : }
99 :
100 0 : sal_Bool SwRect::IsInside( const SwRect& rRect ) const
101 : {
102 0 : const long nRight = Right();
103 0 : const long nBottom = Bottom();
104 0 : const long nrRight = rRect.Right();
105 0 : const long nrBottom= rRect.Bottom();
106 0 : return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
107 0 : (Left() <= nrRight) && (nrRight <= nRight) &&
108 0 : (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
109 0 : (Top() <= nrBottom) && (nrBottom <= nBottom);
110 : }
111 :
112 0 : sal_Bool SwRect::IsInside( const Point& rPoint ) const
113 : {
114 0 : return (Left() <= rPoint.X()) &&
115 0 : (Top() <= rPoint.Y()) &&
116 0 : (Right() >= rPoint.X()) &&
117 0 : (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 0 : sal_Bool SwRect::IsOver( const SwRect& rRect ) const
131 : {
132 0 : return (Top() <= rRect.Bottom()) &&
133 0 : (Left() <= rRect.Right()) &&
134 0 : (Right() >= rRect.Left()) &&
135 0 : (Bottom()>= rRect.Top());
136 : }
137 :
138 0 : void SwRect::Justify()
139 : {
140 0 : if ( m_Size.getHeight() < 0 )
141 : {
142 0 : m_Point.setY(m_Point.getY() + m_Size.getHeight() + 1);
143 0 : m_Size.setHeight(-m_Size.getHeight());
144 : }
145 0 : if ( m_Size.getWidth() < 0 )
146 : {
147 0 : m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
148 0 : m_Size.setWidth(-m_Size.getWidth());
149 : }
150 0 : }
151 :
152 : // Similar to the inline methods, but we need the function pointers
153 0 : void SwRect::_Width( const long nNew ) { m_Size.setWidth(nNew); }
154 0 : void SwRect::_Height( const long nNew ) { m_Size.setHeight(nNew); }
155 0 : void SwRect::_Left( const long nLeft ){ m_Size.Width() += m_Point.getX() - nLeft; m_Point.setX(nLeft); }
156 0 : void SwRect::_Right( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
157 0 : void SwRect::_Top( const long nTop ){ m_Size.Height() += m_Point.getY() - nTop; m_Point.setY(nTop); }
158 0 : void SwRect::_Bottom( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
159 :
160 0 : long SwRect::_Width() const{ return m_Size.getWidth(); }
161 0 : long SwRect::_Height() const{ return m_Size.getHeight(); }
162 0 : long SwRect::_Left() const{ return m_Point.getX(); }
163 0 : long SwRect::_Right() const{ return m_Point.getX() + m_Size.getWidth(); }
164 0 : long SwRect::_Top() const{ return m_Point.getY(); }
165 0 : long SwRect::_Bottom() const{ return m_Point.getY() + m_Size.getHeight(); }
166 :
167 0 : void SwRect::AddWidth( const long nAdd ) { m_Size.Width() += nAdd; }
168 0 : void SwRect::AddHeight( const long nAdd ) { m_Size.Height() += nAdd; }
169 0 : void SwRect::SubLeft( const long nSub ){ m_Size.Width() += nSub; m_Point.setX(m_Point.getX() - nSub); }
170 0 : void SwRect::AddRight( const long nAdd ){ m_Size.Width() += nAdd; }
171 0 : void SwRect::SubTop( const long nSub ){ m_Size.Height() += nSub; m_Point.setY(m_Point.getY() - nSub); }
172 0 : void SwRect::AddBottom( const long nAdd ){ m_Size.Height() += nAdd; }
173 0 : void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
174 0 : void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
175 :
176 0 : const Size SwRect::_Size() const { return SSize(); }
177 0 : const Size SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
178 :
179 0 : const Point SwRect::TopLeft() const { return Pos(); }
180 0 : const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
181 0 : const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
182 0 : const Point SwRect::BottomRight() const
183 0 : { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
184 :
185 0 : long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
186 0 : long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
187 0 : long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
188 0 : long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
189 :
190 0 : sal_Bool SwRect::OverStepLeft( long nLimit ) const
191 0 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
192 0 : sal_Bool SwRect::OverStepBottom( long nLimit ) const
193 0 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
194 0 : sal_Bool SwRect::OverStepTop( long nLimit ) const
195 0 : { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
196 0 : sal_Bool SwRect::OverStepRight( long nLimit ) const
197 0 : { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
198 :
199 0 : void SwRect::SetLeftAndWidth( long nLeft, long nNew )
200 : {
201 0 : m_Point.setX(nLeft);
202 0 : m_Size.setWidth(nNew);
203 0 : }
204 0 : void SwRect::SetTopAndHeight( long nTop, long nNew )
205 : {
206 0 : m_Point.setY(nTop);
207 0 : m_Size.setHeight(nNew);
208 0 : }
209 0 : void SwRect::SetRightAndWidth( long nRight, long nNew )
210 : {
211 0 : m_Point.setX(nRight - nNew);
212 0 : m_Size.setWidth(nNew);
213 0 : }
214 0 : void SwRect::SetBottomAndHeight( long nBottom, long nNew )
215 : {
216 0 : m_Point.setY(nBottom - nNew);
217 0 : m_Size.setHeight(nNew);
218 0 : }
219 0 : void SwRect::SetUpperLeftCorner( const Point& rNew )
220 0 : { m_Point = rNew; }
221 0 : void SwRect::SetUpperRightCorner( const Point& rNew )
222 0 : { m_Point = Point(rNew.A() - m_Size.getWidth(), rNew.B()); }
223 0 : void SwRect::SetLowerLeftCorner( const Point& rNew )
224 0 : { m_Point = Point(rNew.A(), rNew.B() - m_Size.getHeight()); }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|