LibreOffice Module sw (master)  1
swrect.cxx
Go to the documentation of this file.
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 
30  m_Point( rRect.Left(), rRect.Top() )
31 {
32  m_Size.setWidth( rRect.IsWidthEmpty() ? 0 : rRect.Right() - rRect.Left() + 1);
33  m_Size.setHeight(rRect.IsHeightEmpty() ? 0 : rRect.Bottom() - rRect.Top() + 1);
34 }
35 
37 {
38  return Point( Left() + Width() / 2,
39  Top() + Height() / 2 );
40 }
41 
42 SwRect& SwRect::Union( const SwRect& rRect )
43 {
44  if ( Top() > rRect.Top() )
45  Top( rRect.Top() );
46  if ( Left() > rRect.Left() )
47  Left( rRect.Left() );
48  long n = rRect.Right();
49  if ( Right() < n )
50  Right( n );
51  n = rRect.Bottom();
52  if ( Bottom() < n )
53  Bottom( n );
54  return *this;
55 }
56 
58 {
59  // any similarity between me and given element?
60  if ( IsOver( rRect ) )
61  {
62  // get smaller right and lower, and greater left and upper edge
63  if ( Left() < rRect.Left() )
64  Left( rRect.Left() );
65  if ( Top() < rRect.Top() )
66  Top( rRect.Top() );
67  long n = rRect.Right();
68  if ( Right() > n )
69  Right( n );
70  n = rRect.Bottom();
71  if ( Bottom() > n )
72  Bottom( n );
73  }
74  else
75  // Def.: if intersection is empty, set only SSize to 0
76  SSize(0, 0);
77 
78  return *this;
79 }
80 
82 {
83  // get smaller right and lower, and greater left and upper edge
84  if ( Left() < rRect.Left() )
85  Left( rRect.Left() );
86  if ( Top() < rRect.Top() )
87  Top( rRect.Top() );
88  long n = rRect.Right();
89  if ( Right() > n )
90  Right( n );
91  n = rRect.Bottom();
92  if ( Bottom() > n )
93  Bottom( n );
94 
95  return *this;
96 }
97 
98 bool SwRect::IsInside( const SwRect& rRect ) const
99 {
100  const long nRight = Right();
101  const long nBottom = Bottom();
102  const long nrRight = rRect.Right();
103  const long nrBottom= rRect.Bottom();
104  return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
105  (Left() <= nrRight) && (nrRight <= nRight) &&
106  (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
107  (Top() <= nrBottom) && (nrBottom <= nBottom);
108 }
109 
110 bool SwRect::IsInside( const Point& rPoint ) const
111 {
112  return (Left() <= rPoint.X()) &&
113  (Top() <= rPoint.Y()) &&
114  (Right() >= rPoint.X()) &&
115  (Bottom()>= rPoint.Y());
116 }
117 
118 // mouse moving of table borders
119 bool SwRect::IsNear( const Point& rPoint, long nTolerance ) const
120 {
121  bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) &&
122  ((Top() - nTolerance) <= rPoint.Y()) &&
123  ((Right() + nTolerance) >= rPoint.X()) &&
124  ((Bottom() + nTolerance) >= rPoint.Y()));
125  return IsInside(rPoint) || bIsNearby;
126 }
127 
128 bool SwRect::IsOver( const SwRect& rRect ) const
129 {
130  return (Top() <= rRect.Bottom()) &&
131  (Left() <= rRect.Right()) &&
132  (Right() >= rRect.Left()) &&
133  (Bottom()>= rRect.Top());
134 }
135 
137 {
138  if ( m_Size.getHeight() < 0 )
139  {
142  }
143  if ( m_Size.getWidth() < 0 )
144  {
145  m_Point.setX(m_Point.getX() + m_Size.getWidth() + 1);
147  }
148 }
149 
150 // Similar to the inline methods, but we need the function pointers
151 void SwRect::Width_( const long nNew ) { m_Size.setWidth(nNew); }
152 void SwRect::Height_( const long nNew ) { m_Size.setHeight(nNew); }
153 void SwRect::Left_( const long nLeft ){ m_Size.AdjustWidth(m_Point.getX() - nLeft ); m_Point.setX(nLeft); }
154 void SwRect::Right_( const long nRight ){ m_Size.setWidth(nRight - m_Point.getX()); }
155 void SwRect::Top_( const long nTop ){ m_Size.AdjustHeight(m_Point.getY() - nTop ); m_Point.setY(nTop); }
156 void SwRect::Bottom_( const long nBottom ){ m_Size.setHeight(nBottom - m_Point.getY()); }
157 
158 long SwRect::Width_() const{ return m_Size.getWidth(); }
159 long SwRect::Height_() const{ return m_Size.getHeight(); }
160 long SwRect::Left_() const{ return m_Point.getX(); }
161 long SwRect::Right_() const{ return m_Point.getX() + m_Size.getWidth(); }
162 long SwRect::Top_() const{ return m_Point.getY(); }
163 long SwRect::Bottom_() const{ return m_Point.getY() + m_Size.getHeight(); }
164 
165 void SwRect::AddWidth( const long nAdd ) { m_Size.AdjustWidth(nAdd ); }
166 void SwRect::AddHeight( const long nAdd ) { m_Size.AdjustHeight(nAdd ); }
167 void SwRect::SubLeft( const long nSub ){ m_Size.AdjustWidth(nSub ); m_Point.setX(m_Point.getX() - nSub); }
168 void SwRect::AddRight( const long nAdd ){ m_Size.AdjustWidth(nAdd ); }
169 void SwRect::SubTop( const long nSub ){ m_Size.AdjustHeight(nSub ); m_Point.setY(m_Point.getY() - nSub); }
170 void SwRect::AddBottom( const long nAdd ){ m_Size.AdjustHeight(nAdd ); }
171 void SwRect::SetPosX( const long nNew ){ m_Point.setX(nNew); }
172 void SwRect::SetPosY( const long nNew ){ m_Point.setY(nNew); }
173 
174 const Size SwRect::Size_() const { return SSize(); }
175 const Size SwRect::SwappedSize() const { return Size( m_Size.getHeight(), m_Size.getWidth() ); }
176 
177 const Point SwRect::TopLeft() const { return Pos(); }
178 const Point SwRect::TopRight() const { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() ); }
179 const Point SwRect::BottomLeft() const { return Point( m_Point.getX(), m_Point.getY() + m_Size.getHeight() ); }
181  { return Point( m_Point.getX() + m_Size.getWidth(), m_Point.getY() + m_Size.getHeight() ); }
182 
183 long SwRect::GetLeftDistance( long nLimit ) const { return m_Point.getX() - nLimit; }
184 long SwRect::GetBottomDistance( long nLim ) const { return nLim - m_Point.getY() - m_Size.getHeight();}
185 long SwRect::GetTopDistance( long nLimit ) const { return m_Point.getY() - nLimit; }
186 long SwRect::GetRightDistance( long nLim ) const { return nLim - m_Point.getX() - m_Size.getWidth(); }
187 
188 bool SwRect::OverStepLeft( long nLimit ) const
189  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
190 bool SwRect::OverStepBottom( long nLimit ) const
191  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
192 bool SwRect::OverStepTop( long nLimit ) const
193  { return nLimit > m_Point.getY() && m_Point.getY() + m_Size.getHeight() > nLimit; }
194 bool SwRect::OverStepRight( long nLimit ) const
195  { return nLimit > m_Point.getX() && m_Point.getX() + m_Size.getWidth() > nLimit; }
196 
197 void SwRect::SetLeftAndWidth( long nLeft, long nNew )
198 {
199  m_Point.setX(nLeft);
200  m_Size.setWidth(nNew);
201 }
202 void SwRect::SetTopAndHeight( long nTop, long nNew )
203 {
204  m_Point.setY(nTop);
205  m_Size.setHeight(nNew);
206 }
207 void SwRect::SetRightAndWidth( long nRight, long nNew )
208 {
209  m_Point.setX(nRight - nNew);
210  m_Size.setWidth(nNew);
211 }
212 void SwRect::SetBottomAndHeight( long nBottom, long nNew )
213 {
214  m_Point.setY(nBottom - nNew);
215  m_Size.setHeight(nNew);
216 }
218  { m_Point = rNew; }
220  { m_Point = Point(rNew.X() - m_Size.getWidth(), rNew.Y()); }
222  { m_Point = Point(rNew.X(), rNew.Y() - m_Size.getHeight()); }
223 
224 #ifdef DBG_UTIL
225 SvStream& WriteSwRect(SvStream &rStream, const SwRect &rRect)
226 {
227  rStream.WriteChar('[').WriteInt32(rRect.Top()).
228  WriteChar('/').WriteInt32(rRect.Left()).
229  WriteChar(',').WriteInt32(rRect.Width()).
230  WriteChar('x').WriteInt32(rRect.Height()).
231  WriteCharPtr("] ");
232  return rStream;
233 }
234 #endif
235 
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size m_Size
Definition: swrect.hxx:37
long Right() const
Definition: swrect.hxx:241
bool OverStepLeft(long) const
Definition: swrect.cxx:188
bool IsHeightEmpty() const
long Bottom_() const
Definition: swrect.cxx:163
SwRect & Union(const SwRect &rRect)
Definition: swrect.cxx:42
long AdjustWidth(long n)
SvStream & WriteInt32(sal_Int32 nInt32)
long GetBottomDistance(long) const
Definition: swrect.cxx:184
void Height(long nNew)
Definition: swrect.hxx:189
bool IsNear(const Point &rPoint, long nTolerance) const
Definition: swrect.cxx:119
void SetPosX(const long nNew)
Definition: swrect.cxx:171
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
long Left() const
Definition: swrect.hxx:237
long GetLeftDistance(long) const
Definition: swrect.cxx:183
long AdjustHeight(long n)
long Width() const
Definition: swrect.hxx:229
bool OverStepTop(long) const
Definition: swrect.cxx:192
const Size Size_() const
Definition: swrect.cxx:174
SwRect & Intersection_(const SwRect &rRect)
Definition: swrect.cxx:81
long Left_() const
Definition: swrect.cxx:160
long Right() const
void Top(const long nTop)
Definition: swrect.hxx:202
const Point BottomLeft() const
Definition: swrect.cxx:179
void setX(long nX)
SwRect & Intersection(const SwRect &rRect)
Definition: swrect.cxx:57
long Top() const
void Right(const long nRight)
Definition: swrect.hxx:198
void setY(long nY)
long GetRightDistance(long) const
Definition: swrect.cxx:186
long Height() const
Definition: swrect.hxx:233
void SubLeft(const long nSub)
Definition: swrect.cxx:167
long getY() const
long getX() const
void SetLeftAndWidth(long nLeft, long nNew)
Definition: swrect.cxx:197
long Height_() const
Definition: swrect.cxx:159
const Size & SSize() const
Definition: swrect.hxx:221
long Width_() const
Definition: swrect.cxx:158
void SubTop(const long nSub)
Definition: swrect.cxx:169
void AddWidth(const long nAdd)
Definition: swrect.cxx:165
void SetPosY(const long nNew)
Definition: swrect.cxx:172
void AddRight(const long nAdd)
Definition: swrect.cxx:168
const Size SwappedSize() const
Definition: swrect.cxx:175
void AddHeight(const long nAdd)
Definition: swrect.cxx:166
long Bottom() const
const Point BottomRight() const
Definition: swrect.cxx:180
long X() const
Point Center() const
Definition: swrect.cxx:36
void SetUpperLeftCorner(const Point &rNew)
Definition: swrect.cxx:217
const Point TopLeft() const
Definition: swrect.cxx:177
void SetRightAndWidth(long nRight, long nNew)
Definition: swrect.cxx:207
bool OverStepBottom(long) const
Definition: swrect.cxx:190
void Left(const long nLeft)
Definition: swrect.hxx:193
void Bottom(const long nBottom)
Definition: swrect.hxx:207
void SetUpperRightCorner(const Point &rNew)
Definition: swrect.cxx:219
void Justify()
Definition: swrect.cxx:136
void SetTopAndHeight(long nTop, long nNew)
Definition: swrect.cxx:202
long Bottom() const
Definition: swrect.hxx:249
long Top() const
Definition: swrect.hxx:245
Point m_Point
Definition: swrect.hxx:36
void Width(long nNew)
Definition: swrect.hxx:185
long GetTopDistance(long) const
Definition: swrect.cxx:185
void SetLowerLeftCorner(const Point &rNew)
Definition: swrect.cxx:221
bool IsInside(const Point &rPOINT) const
Definition: swrect.cxx:110
const Point TopRight() const
Definition: swrect.cxx:178
bool IsWidthEmpty() const
long Left() const
SvStream & WriteChar(char nChar)
void AddBottom(const long nAdd)
Definition: swrect.cxx:170
void SetBottomAndHeight(long nBottom, long nNew)
Definition: swrect.cxx:212
Left
long getHeight() const
bool OverStepRight(long) const
Definition: swrect.cxx:194
long Top_() const
Definition: swrect.cxx:162
bool IsOver(const SwRect &rRect) const
Definition: swrect.cxx:128
long getWidth() const
void setWidth(long nWidth)
const Point & Pos() const
Definition: swrect.hxx:213
long Y() const
SwRect()
Definition: swrect.hxx:307
SvStream & WriteSwRect(SvStream &rStream, const SwRect &rRect)
Definition: swrect.cxx:225
long Right_() const
Definition: swrect.cxx:161
void setHeight(long nHeight)