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