tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / sw / inc / swrect.hxx
blob3365434abb0778078806f24ff5ceacfd43bcf3db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
21 #include <ostream>
23 #include <sal/log.hxx>
24 #include <tools/gen.hxx>
25 #include "swdllapi.h"
27 class SvStream;
28 typedef struct _xmlTextWriter* xmlTextWriterPtr;
30 /// *Of course* Writer needs its own rectangles.
31 /// This is half-open so m_Point.X() + m_Size.getWidth() is *not* included.
32 /// Note the tools Rectangle is (usually? sometimes?) closed so there's a
33 /// SVRect() to subtract 1 for the conversion.
34 class SAL_WARN_UNUSED SwRect
36 Point m_Point;
37 Size m_Size;
39 public:
40 inline SwRect();
41 inline SwRect( const SwRect &rRect ) = default;
42 inline SwRect( const Point& rLT, const Size& rSize );
43 inline SwRect( const Point& rLT, const Point& rRB );
44 inline SwRect( tools::Long X, tools::Long Y, tools::Long Width, tools::Long Height );
46 //SV-SS e.g. SwRect( pWin->GetClipRect() );
47 explicit SwRect( const tools::Rectangle &rRect );
49 //Set-Methods
50 inline void Chg( const Point& rNP, const Size &rNS );
51 inline void Pos( const Point& rNew );
52 inline void Pos( const tools::Long nNewX, const tools::Long nNewY );
53 inline void SSize( const Size& rNew );
54 inline void SSize( const tools::Long nHeight, const tools::Long nWidth );
55 inline void Width( tools::Long nNew );
56 inline void Height( tools::Long nNew );
57 inline void Left( const tools::Long nLeft );
58 inline void Right( const tools::Long nRight );
59 inline void Top( const tools::Long nTop );
60 inline void Bottom( const tools::Long nBottom );
62 //Get-Methods
63 inline const Point &Pos() const;
64 inline const Size &SSize() const;
65 inline tools::Long Width() const;
66 inline tools::Long Height() const;
67 inline tools::Long Left() const;
68 inline tools::Long Right() const;
69 inline tools::Long Top() const;
70 inline tools::Long Bottom() const;
72 // In order to be able to access the members of Pos and SSize from the layout side.
73 inline Point &Pos();
75 Point Center() const;
77 void Justify();
79 SwRect &Union( const SwRect& rRect );
80 SwRect &Intersection( const SwRect& rRect );
82 SwRect GetUnion( const SwRect& rRect ) const;
83 SwRect GetIntersection( const SwRect& rRect ) const;
85 // Same as Intersection, only assume that Rects are overlapping!
86 SwRect &Intersection_( const SwRect &rRect );
88 bool Contains( const Point& rPOINT ) const;
89 bool Contains( const SwRect& rRect ) const;
90 bool Overlaps( const SwRect& rRect ) const;
91 bool IsNear(const Point& rPoint, tools::Long nTolerance ) const;
92 inline bool HasArea() const;
93 inline bool IsEmpty() const;
94 inline void Clear();
96 SwRect &operator = ( const SwRect &rRect ) = default;
98 inline bool operator == ( const SwRect& rRect ) const;
99 inline bool operator != ( const SwRect& rRect ) const;
101 inline SwRect &operator+=( const Point &rPt );
102 inline SwRect &operator-=( const Point &rPt );
104 //SV-SS e.g. pWin->DrawRect( aSwRect.SVRect() );
105 inline tools::Rectangle SVRect() const;
107 // Output operator for debugging.
108 friend SvStream& WriteSwRect( SvStream &rStream, const SwRect &rRect );
109 void dumpAsXmlAttributes(xmlTextWriterPtr writer) const;
111 void Top_( const tools::Long nTop );
112 void Bottom_( const tools::Long nBottom );
113 void Left_( const tools::Long nLeft );
114 void Right_( const tools::Long nRight );
115 void Width_( const tools::Long nNew );
116 void Height_( const tools::Long nNew );
117 tools::Long Top_() const;
118 tools::Long Bottom_() const;
119 tools::Long Left_() const;
120 tools::Long Right_() const;
121 tools::Long Width_() const;
122 tools::Long Height_() const;
123 void SubTop( const tools::Long nSub );
124 void AddTop( const tools::Long nAdd );
125 void AddBottom( const tools::Long nAdd );
126 void AddLeft( const tools::Long nAdd );
127 void SubLeft( const tools::Long nSub );
128 void AddRight( const tools::Long nAdd );
129 void AddWidth( const tools::Long nAdd );
130 void AddHeight( const tools::Long nAdd );
131 void SetPosX( const tools::Long nNew );
132 void SetPosY( const tools::Long nNew );
133 void SetLeftAndWidth( tools::Long nLeft, tools::Long nNew );
134 void SetTopAndHeight( tools::Long nTop, tools::Long nNew );
135 void SetRightAndWidth( tools::Long nRight, tools::Long nNew );
136 void SetBottomAndHeight( tools::Long nBottom, tools::Long nNew );
137 void SetUpperLeftCorner( const Point& rNew );
138 void SetUpperRightCorner( const Point& rNew );
139 void SetLowerLeftCorner( const Point& rNew );
140 Size Size_() const;
141 Point TopLeft() const;
142 Point TopRight() const;
143 Point BottomLeft() const;
144 Point BottomRight() const;
145 Size SwappedSize() const;
146 tools::Long GetLeftDistance( tools::Long ) const;
147 tools::Long GetBottomDistance( tools::Long ) const;
148 tools::Long GetRightDistance( tools::Long ) const;
149 tools::Long GetTopDistance( tools::Long ) const;
150 bool OverStepLeft( tools::Long ) const;
151 bool OverStepBottom( tools::Long ) const;
152 bool OverStepTop( tools::Long ) const;
153 bool OverStepRight( tools::Long ) const;
156 typedef void (SwRect::*SwRectSet)( const tools::Long nNew );
157 typedef tools::Long (SwRect::*SwRectGet)() const;
158 typedef Point (SwRect::*SwRectPoint)() const;
159 typedef Size (SwRect::*SwRectSize)() const;
160 typedef bool (SwRect::*SwRectMax)( tools::Long ) const;
161 typedef tools::Long (SwRect::*SwRectDist)( tools::Long ) const;
162 typedef void (SwRect::*SwRectSetTwice)( tools::Long, tools::Long );
163 typedef void (SwRect::*SwRectSetPos)( const Point& );
165 // Set-Methods
166 inline void SwRect::Chg( const Point& rNP, const Size &rNS )
168 m_Point = rNP;
169 m_Size = rNS;
171 inline void SwRect::Pos( const Point& rNew )
173 m_Point = rNew;
175 inline void SwRect::Pos( const tools::Long nNewX, const tools::Long nNewY )
177 m_Point.setX(nNewX);
178 m_Point.setY(nNewY);
180 inline void SwRect::SSize( const Size& rNew )
182 m_Size = rNew;
184 inline void SwRect::SSize( const tools::Long nNewHeight, const tools::Long nNewWidth )
186 m_Size.setWidth(nNewWidth);
187 m_Size.setHeight(nNewHeight);
189 inline void SwRect::Width( tools::Long nNew )
191 m_Size.setWidth(nNew);
193 inline void SwRect::Height( tools::Long nNew )
195 m_Size.setHeight(nNew);
197 inline void SwRect::Left( const tools::Long nLeft )
199 m_Size.AdjustWidth( m_Point.getX() - nLeft );
200 m_Point.setX(nLeft);
202 inline void SwRect::Right( const tools::Long nRight )
204 m_Size.setWidth(nRight - m_Point.getX() + 1);
206 inline void SwRect::Top( const tools::Long nTop )
208 m_Size.AdjustHeight( m_Point.getY() - nTop );
209 m_Point.setY(nTop);
211 inline void SwRect::Bottom( const tools::Long nBottom )
213 m_Size.setHeight(nBottom - m_Point.getY() + 1);
216 // Get-Methods
217 inline const Point &SwRect::Pos() const
219 return m_Point;
221 inline Point &SwRect::Pos()
223 return m_Point;
225 inline const Size &SwRect::SSize() const
227 return m_Size;
229 inline tools::Long SwRect::Width() const
231 return m_Size.Width();
233 inline tools::Long SwRect::Height() const
235 return m_Size.Height();
237 inline tools::Long SwRect::Left() const
239 return m_Point.X();
241 inline tools::Long SwRect::Right() const
243 return m_Size.getWidth() ? m_Point.getX() + m_Size.getWidth() - 1 : m_Point.getX();
245 inline tools::Long SwRect::Top() const
247 return m_Point.Y();
249 inline tools::Long SwRect::Bottom() const
251 return m_Size.getHeight() ? m_Point.getY() + m_Size.getHeight() - 1 : m_Point.getY();
254 inline Point SwRect::TopLeft() const
256 return Point( Left(), Top());
258 inline Point SwRect::TopRight() const
260 return Point( Right(), Top());
262 inline Point SwRect::BottomLeft() const
264 return Point( Left(), Bottom());
266 inline Point SwRect::BottomRight() const
268 return Point( Right(), Bottom());
271 inline bool SwRect::operator == ( const SwRect& rRect ) const
273 return (m_Point == rRect.m_Point && m_Size == rRect.m_Size);
275 inline bool SwRect::operator != ( const SwRect& rRect ) const
277 return (m_Point != rRect.m_Point || m_Size != rRect.m_Size);
280 inline SwRect &SwRect::operator+=( const Point &rPt )
282 m_Point += rPt;
283 return *this;
285 inline SwRect &SwRect::operator-=( const Point &rPt )
287 m_Point -= rPt;
288 return *this;
291 // other
292 inline tools::Rectangle SwRect::SVRect() const
294 SAL_INFO_IF( IsEmpty(), "sw.core", "SVRect() without Width or Height" );
295 return tools::Rectangle( m_Point.getX(), m_Point.getY(),
296 m_Point.getX() + m_Size.getWidth() - 1, //Right()
297 m_Point.getY() + m_Size.getHeight() - 1 ); //Bottom()
300 inline bool SwRect::HasArea() const
302 return !IsEmpty();
304 inline bool SwRect::IsEmpty() const
306 return !(m_Size.getHeight() && m_Size.getWidth());
308 inline void SwRect::Clear()
310 m_Point.setX(0);
311 m_Point.setY(0);
312 m_Size.setWidth(0);
313 m_Size.setHeight(0);
316 // constructors
317 inline SwRect::SwRect() :
318 m_Point( 0, 0 ),
319 m_Size( 0, 0 )
322 inline SwRect::SwRect( const Point& rLT, const Size& rSize ) :
323 m_Point( rLT ),
324 m_Size( rSize )
327 inline SwRect::SwRect( const Point& rLT, const Point& rRB ) :
328 m_Point( rLT ),
329 m_Size( rRB.X() - rLT.X() + 1, rRB.Y() - rLT.Y() + 1 )
332 inline SwRect::SwRect( tools::Long X, tools::Long Y, tools::Long W, tools::Long H ) :
333 m_Point( X, Y ),
334 m_Size( W, H )
338 inline Point SwRect::Center() const
340 return Point( Left() + Width() / 2,
341 Top() + Height() / 2 );
344 inline bool SwRect::Contains( const SwRect& rRect ) const
346 const tools::Long nRight = Right();
347 const tools::Long nBottom = Bottom();
348 const tools::Long nrRight = rRect.Right();
349 const tools::Long nrBottom= rRect.Bottom();
350 return (Left() <= rRect.Left()) && (rRect.Left()<= nRight) &&
351 (Left() <= nrRight) && (nrRight <= nRight) &&
352 (Top() <= rRect.Top()) && (rRect.Top() <= nBottom) &&
353 (Top() <= nrBottom) && (nrBottom <= nBottom);
356 inline bool SwRect::Contains( const Point& rPoint ) const
358 return (Left() <= rPoint.X()) &&
359 (Top() <= rPoint.Y()) &&
360 (Right() >= rPoint.X()) &&
361 (Bottom()>= rPoint.Y());
364 // mouse moving of table borders
365 inline bool SwRect::IsNear( const Point& rPoint, tools::Long nTolerance ) const
367 bool bIsNearby = (((Left() - nTolerance) <= rPoint.X()) &&
368 ((Top() - nTolerance) <= rPoint.Y()) &&
369 ((Right() + nTolerance) >= rPoint.X()) &&
370 ((Bottom() + nTolerance) >= rPoint.Y()));
371 return Contains(rPoint) || bIsNearby;
374 inline bool SwRect::Overlaps( const SwRect& rRect ) const
376 return (Top() <= rRect.Bottom()) &&
377 (Left() <= rRect.Right()) &&
378 (Right() >= rRect.Left()) &&
379 (Bottom()>= rRect.Top());
382 inline SwRect SwRect::GetUnion( const SwRect& rRect ) const
384 return IsEmpty() ? rRect : rRect.IsEmpty() ? *this : SwRect(
385 Point( std::min( Left(), rRect.Left()),
386 std::min( Top(), rRect.Top())),
387 Point( std::max( Right(), rRect.Right()),
388 std::max( Bottom(), rRect.Bottom())));
391 inline SwRect SwRect::GetIntersection( const SwRect& rRect ) const
393 return Overlaps( rRect )
394 ? SwRect(
395 Point( std::max( Left(), rRect.Left()),
396 std::max( Top(), rRect.Top())),
397 Point( std::min( Right(), rRect.Right()),
398 std::min( Bottom(), rRect.Bottom())))
399 : SwRect();
403 template< typename charT, typename traits >
404 inline std::basic_ostream<charT, traits> & operator <<(
405 std::basic_ostream<charT, traits> & stream, const SwRect& rectangle )
407 if (rectangle.IsEmpty())
408 stream << "EMPTY:";
409 return stream << rectangle.SSize()
410 << "@(" << rectangle.Pos() << ")";
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */