1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include "tools/toolsdllapi.h"
23 #include <tools/solar.h>
33 class SAL_WARN_UNUSED Pair
37 Pair( long nA
, long nB
);
39 long A() const { return nA
; }
40 long B() const { return nB
; }
42 long& A() { return nA
; }
43 long& B() { return nB
; }
45 sal_Bool
operator == ( const Pair
& rPair
) const;
46 sal_Bool
operator != ( const Pair
& rPair
) const;
48 TOOLS_DLLPUBLIC
friend SvStream
& operator>>( SvStream
& rIStream
, Pair
& rPair
);
49 TOOLS_DLLPUBLIC
friend SvStream
& operator<<( SvStream
& rOStream
, const Pair
& rPair
);
61 inline Pair::Pair( long _nA
, long _nB
)
67 inline sal_Bool
Pair::operator == ( const Pair
& rPair
) const
69 return ((nA
== rPair
.nA
) && (nB
== rPair
.nB
));
72 inline sal_Bool
Pair::operator != ( const Pair
& rPair
) const
74 return ((nA
!= rPair
.nA
) || (nB
!= rPair
.nB
));
79 class SAL_WARN_UNUSED Point
: public Pair
83 Point( long nX
, long nY
);
85 long X() const { return nA
; }
86 long Y() const { return nB
; }
88 long& X() { return nA
; }
89 long& Y() { return nB
; }
91 void Move( long nHorzMove
, long nVertMove
);
92 sal_Bool
IsAbove( const Point
& rPoint
) const;
93 sal_Bool
IsBelow( const Point
& rPoint
) const;
94 sal_Bool
IsLeft( const Point
& rPoint
) const;
95 sal_Bool
IsRight( const Point
& rPoint
) const;
97 Point
& operator += ( const Point
& rPoint
);
98 Point
& operator -= ( const Point
& rPoint
);
99 Point
& operator *= ( const long nVal
);
100 Point
& operator /= ( const long nVal
);
102 friend inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
);
103 friend inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
);
104 friend inline Point
operator*( const Point
&rVal1
, const long nVal2
);
105 friend inline Point
operator/( const Point
&rVal1
, const long nVal2
);
107 long getX() const { return X(); }
108 long getY() const { return Y(); }
109 void setX(long nX
) { X() = nX
; }
110 void setY(long nY
) { Y() = nY
; }
113 inline Point::Point()
117 inline Point::Point( long nX
, long nY
) : Pair( nX
, nY
)
121 inline void Point::Move( long nHorzMove
, long nVertMove
)
127 inline sal_Bool
Point::IsAbove( const Point
& rPoint
) const
129 return (nB
> rPoint
.nB
);
132 inline sal_Bool
Point::IsBelow( const Point
& rPoint
) const
134 return (nB
< rPoint
.nB
);
137 inline sal_Bool
Point::IsLeft( const Point
& rPoint
) const
139 return (nA
< rPoint
.nA
);
142 inline sal_Bool
Point::IsRight( const Point
& rPoint
) const
144 return (nA
> rPoint
.nA
);
147 inline Point
& Point::operator += ( const Point
& rPoint
)
154 inline Point
& Point::operator -= ( const Point
& rPoint
)
161 inline Point
& Point::operator *= ( const long nVal
)
168 inline Point
& Point::operator /= ( const long nVal
)
175 inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
)
177 return Point( rVal1
.nA
+rVal2
.nA
, rVal1
.nB
+rVal2
.nB
);
180 inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
)
182 return Point( rVal1
.nA
-rVal2
.nA
, rVal1
.nB
-rVal2
.nB
);
185 inline Point
operator*( const Point
&rVal1
, const long nVal2
)
187 return Point( rVal1
.nA
*nVal2
, rVal1
.nB
*nVal2
);
190 inline Point
operator/( const Point
&rVal1
, const long nVal2
)
192 return Point( rVal1
.nA
/nVal2
, rVal1
.nB
/nVal2
);
195 template< typename charT
, typename traits
>
196 inline std::basic_ostream
<charT
, traits
> & operator <<(
197 std::basic_ostream
<charT
, traits
> & stream
, const Point
& point
)
199 return stream
<< point
.X() << ',' << point
.Y();
204 class SAL_WARN_UNUSED Size
: public Pair
208 Size( long nWidth
, long nHeight
);
210 long Width() const { return nA
; }
211 long Height() const { return nB
; }
213 long& Width() { return nA
; }
214 long& Height() { return nB
; }
216 long getWidth() const { return Width(); }
217 long getHeight() const { return Height(); }
218 void setWidth(long nWidth
) { Width() = nWidth
; }
219 void setHeight(long nHeight
) { Height() = nHeight
; }
226 inline Size::Size( long nWidth
, long nHeight
) :
227 Pair( nWidth
, nHeight
)
231 template< typename charT
, typename traits
>
232 inline std::basic_ostream
<charT
, traits
> & operator <<(
233 std::basic_ostream
<charT
, traits
> & stream
, const Size
& size
)
235 return stream
<< size
.Width() << 'x' << size
.Height();
240 #define RANGE_MAX LONG_MAX
242 class SAL_WARN_UNUSED Range
: public Pair
246 Range( long nMin
, long nMax
);
248 long Min() const { return nA
; }
249 long Max() const { return nB
; }
250 long Len() const { return nB
- nA
+ 1; }
252 long& Min() { return nA
; }
253 long& Max() { return nB
; }
255 sal_Bool
IsInside( long nIs
) const;
260 inline Range::Range()
264 inline Range::Range( long nMin
, long nMax
) : Pair( nMin
, nMax
)
268 inline sal_Bool
Range::IsInside( long nIs
) const
270 return ((nA
<= nIs
) && (nIs
<= nB
));
273 inline void Range::Justify()
283 template< typename charT
, typename traits
>
284 inline std::basic_ostream
<charT
, traits
> & operator <<(
285 std::basic_ostream
<charT
, traits
> & stream
, const Range
& range
)
287 return stream
<< range
.Min() << '-' << range
.Max();
292 #define SELECTION_MIN LONG_MIN
293 #define SELECTION_MAX LONG_MAX
295 class SAL_WARN_UNUSED Selection
: public Pair
299 Selection( long nPos
);
300 Selection( long nMin
, long nMax
);
302 long Min() const { return nA
; }
303 long Max() const { return nB
; }
304 long Len() const { return nB
- nA
; }
306 long& Min() { return nA
; }
307 long& Max() { return nB
; }
309 sal_Bool
IsInside( long nIs
) const;
313 sal_Bool
operator !() const { return !Len(); }
315 long getMin() const { return Min(); }
316 long getMax() const { return Max(); }
317 void setMin(long nMin
) { Min() = nMin
; }
318 void setMax(long nMax
) { Max() = nMax
; }
321 inline Selection::Selection()
325 inline Selection::Selection( long nPos
) : Pair( nPos
, nPos
)
329 inline Selection::Selection( long nMin
, long nMax
) :
334 inline sal_Bool
Selection::IsInside( long nIs
) const
336 return ((nA
<= nIs
) && (nIs
< nB
));
339 inline void Selection::Justify()
349 template< typename charT
, typename traits
>
350 inline std::basic_ostream
<charT
, traits
> & operator <<(
351 std::basic_ostream
<charT
, traits
> & stream
, const Selection
& selection
)
353 return stream
<< selection
.Min() << '-' << selection
.Max();
357 #define RECT_EMPTY ((short)-32767)
359 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Rectangle
363 Rectangle( const Point
& rLT
, const Point
& rRB
);
364 Rectangle( long nLeft
, long nTop
,
365 long nRight
, long nBottom
);
366 Rectangle( const Point
& rLT
, const Size
& rSize
);
368 long Left() const { return nLeft
; }
369 long Right() const { return nRight
; }
370 long Top() const { return nTop
; }
371 long Bottom() const { return nBottom
; }
373 long& Left() { return nLeft
; }
374 long& Right() { return nRight
; }
375 long& Top() { return nTop
; }
376 long& Bottom() { return nBottom
; }
378 inline Point
TopLeft() const;
379 inline Point
TopRight() const;
380 inline Point
TopCenter() const;
381 inline Point
BottomLeft() const;
382 inline Point
BottomRight() const;
383 inline Point
BottomCenter() const;
384 inline Point
LeftCenter() const;
385 inline Point
RightCenter() const;
386 inline Point
Center() const;
388 inline void Move( long nHorzMove
, long nVertMove
);
389 inline void Transpose();
390 inline void SetPos( const Point
& rPoint
);
391 void SetSize( const Size
& rSize
);
392 inline Size
GetSize() const;
394 inline long GetWidth() const;
395 inline long GetHeight() const;
397 Rectangle
& Union( const Rectangle
& rRect
);
398 Rectangle
& Intersection( const Rectangle
& rRect
);
399 inline Rectangle
GetUnion( const Rectangle
& rRect
) const;
400 inline Rectangle
GetIntersection( const Rectangle
& rRect
) const;
404 sal_Bool
IsInside( const Point
& rPOINT
) const;
405 sal_Bool
IsInside( const Rectangle
& rRect
) const;
406 sal_Bool
IsOver( const Rectangle
& rRect
) const;
408 void SetEmpty() { nRight
= nBottom
= RECT_EMPTY
; }
409 inline sal_Bool
IsEmpty() const;
411 inline sal_Bool
operator == ( const Rectangle
& rRect
) const;
412 inline sal_Bool
operator != ( const Rectangle
& rRect
) const;
414 inline Rectangle
& operator += ( const Point
& rPt
);
415 inline Rectangle
& operator -= ( const Point
& rPt
);
417 friend inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
);
418 friend inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
);
420 TOOLS_DLLPUBLIC
friend SvStream
& operator>>( SvStream
& rIStream
, Rectangle
& rRect
);
421 TOOLS_DLLPUBLIC
friend SvStream
& operator<<( SvStream
& rOStream
, const Rectangle
& rRect
);
424 long getX() const { return nLeft
; }
425 long getY() const { return nTop
; }
426 long getWidth() const { return nRight
- nLeft
; }
427 long getHeight() const { return nBottom
- nTop
; }
428 void setX( long n
) { nRight
+= n
-nLeft
; nLeft
= n
; }
429 void setY( long n
) { nBottom
+= n
-nTop
; nTop
= n
; }
430 void setWidth( long n
) { nRight
= nLeft
+ n
; }
431 void setHeight( long n
) { nBottom
= nTop
+ n
; }
440 inline Rectangle::Rectangle()
443 nRight
= nBottom
= RECT_EMPTY
;
446 inline Rectangle::Rectangle( const Point
& rLT
, const Point
& rRB
)
454 inline Rectangle::Rectangle( long _nLeft
, long _nTop
,
455 long _nRight
, long _nBottom
)
463 inline Rectangle::Rectangle( const Point
& rLT
, const Size
& rSize
)
467 nRight
= rSize
.Width() ? nLeft
+rSize
.Width()-1 : RECT_EMPTY
;
468 nBottom
= rSize
.Height() ? nTop
+rSize
.Height()-1 : RECT_EMPTY
;
471 inline sal_Bool
Rectangle::IsEmpty() const
473 return ((nRight
== RECT_EMPTY
) || (nBottom
== RECT_EMPTY
));
476 inline Point
Rectangle::TopLeft() const
478 return Point( nLeft
, nTop
);
481 inline Point
Rectangle::TopRight() const
483 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
, nTop
);
486 inline Point
Rectangle::BottomLeft() const
488 return Point( nLeft
, (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
491 inline Point
Rectangle::BottomRight() const
493 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
,
494 (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
497 inline Point
Rectangle::TopCenter() const
500 return Point( nLeft
, nTop
);
502 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
503 std::min( nTop
, nBottom
) );
506 inline Point
Rectangle::BottomCenter() const
509 return Point( nLeft
, nTop
);
511 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
512 std::max( nTop
, nBottom
) );
515 inline Point
Rectangle::LeftCenter() const
518 return Point( nLeft
, nTop
);
520 return Point( std::min( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
523 inline Point
Rectangle::RightCenter() const
526 return Point( nLeft
, nTop
);
528 return Point( std::max( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
531 inline Point
Rectangle::Center() const
534 return Point( nLeft
, nTop
);
536 return Point( nLeft
+(nRight
-nLeft
)/2 , nTop
+(nBottom
-nTop
)/2 );
539 inline void Rectangle::Move( long nHorzMove
, long nVertMove
)
543 if ( nRight
!= RECT_EMPTY
)
545 if ( nBottom
!= RECT_EMPTY
)
546 nBottom
+= nVertMove
;
549 void Rectangle::Transpose()
563 inline void Rectangle::SetPos( const Point
& rPoint
)
565 if ( nRight
!= RECT_EMPTY
)
566 nRight
+= rPoint
.X() - nLeft
;
567 if ( nBottom
!= RECT_EMPTY
)
568 nBottom
+= rPoint
.Y() - nTop
;
573 inline long Rectangle::GetWidth() const
576 if ( nRight
== RECT_EMPTY
)
590 inline long Rectangle::GetHeight() const
593 if ( nBottom
== RECT_EMPTY
)
607 inline Size
Rectangle::GetSize() const
609 return Size( GetWidth(), GetHeight() );
612 inline Rectangle
Rectangle::GetUnion( const Rectangle
& rRect
) const
614 Rectangle
aTmpRect( *this );
615 return aTmpRect
.Union( rRect
);
618 inline Rectangle
Rectangle::GetIntersection( const Rectangle
& rRect
) const
620 Rectangle
aTmpRect( *this );
621 return aTmpRect
.Intersection( rRect
);
624 inline sal_Bool
Rectangle::operator == ( const Rectangle
& rRect
) const
626 return ((nLeft
== rRect
.nLeft
) &&
627 (nTop
== rRect
.nTop
) &&
628 (nRight
== rRect
.nRight
) &&
629 (nBottom
== rRect
.nBottom
));
632 inline sal_Bool
Rectangle::operator != ( const Rectangle
& rRect
) const
634 return ((nLeft
!= rRect
.nLeft
) ||
635 (nTop
!= rRect
.nTop
) ||
636 (nRight
!= rRect
.nRight
) ||
637 (nBottom
!= rRect
.nBottom
));
640 inline Rectangle
& Rectangle::operator +=( const Point
& rPt
)
644 if ( nRight
!= RECT_EMPTY
)
646 if ( nBottom
!= RECT_EMPTY
)
651 inline Rectangle
& Rectangle::operator -= ( const Point
& rPt
)
655 if ( nRight
!= RECT_EMPTY
)
657 if ( nBottom
!= RECT_EMPTY
)
662 inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
)
664 Rectangle
aRect( rRect
.nLeft
+ rPt
.X(), rRect
.nTop
+ rPt
.Y(),
665 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
+ rPt
.X(),
666 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
+ rPt
.Y() );
670 inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
)
672 Rectangle
aRect( rRect
.nLeft
- rPt
.X(),
673 rRect
.nTop
- rPt
.Y(),
674 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
- rPt
.X(),
675 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
- rPt
.Y() );
679 template< typename charT
, typename traits
>
680 inline std::basic_ostream
<charT
, traits
> & operator <<(
681 std::basic_ostream
<charT
, traits
> & stream
, const Rectangle
& rectangle
)
683 if (rectangle
.IsEmpty())
684 return stream
<< "EMPTY";
686 return stream
<< rectangle
.getWidth() << 'x' << rectangle
.getHeight()
687 << "@(" << rectangle
.getX() << ',' << rectangle
.getY() << ")";
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */