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 .
19 #ifndef INCLUDED_TOOLS_GEN_HXX
20 #define INCLUDED_TOOLS_GEN_HXX
22 #include <tools/toolsdllapi.h>
35 enum TriState
{ TRISTATE_FALSE
, TRISTATE_TRUE
, TRISTATE_INDET
};
39 class SAL_WARN_UNUSED Pair
43 Pair( long nA
, long nB
);
45 long A() const { return nA
; }
46 long B() const { return nB
; }
48 long& A() { return nA
; }
49 long& B() { return nB
; }
51 bool operator == ( const Pair
& rPair
) const;
52 bool operator != ( const Pair
& rPair
) const;
54 TOOLS_DLLPUBLIC
friend SvStream
& ReadPair( SvStream
& rIStream
, Pair
& rPair
);
55 TOOLS_DLLPUBLIC
friend SvStream
& WritePair( SvStream
& rOStream
, const Pair
& rPair
);
67 inline Pair::Pair( long _nA
, long _nB
)
73 inline bool Pair::operator == ( const Pair
& rPair
) const
75 return ((nA
== rPair
.nA
) && (nB
== rPair
.nB
));
78 inline bool Pair::operator != ( const Pair
& rPair
) const
80 return ((nA
!= rPair
.nA
) || (nB
!= rPair
.nB
));
85 class SAL_DLLPUBLIC_EXPORT SAL_WARN_UNUSED Point
: public Pair
89 Point( long nX
, long nY
);
91 long X() const { return nA
; }
92 long Y() const { return nB
; }
94 long& X() { return nA
; }
95 long& Y() { return nB
; }
97 void Move( long nHorzMove
, long nVertMove
);
98 bool IsAbove( const Point
& rPoint
) const;
99 bool IsBelow( const Point
& rPoint
) const;
100 bool IsLeft( const Point
& rPoint
) const;
101 bool IsRight( const Point
& rPoint
) const;
103 void RotateAround( long& rX
, long& rY
, short nOrientation
) const;
106 Point
& operator += ( const Point
& rPoint
);
107 Point
& operator -= ( const Point
& rPoint
);
108 Point
& operator *= ( const long nVal
);
109 Point
& operator /= ( const long nVal
);
111 friend inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
);
112 friend inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
);
113 friend inline Point
operator*( const Point
&rVal1
, const long nVal2
);
114 friend inline Point
operator/( const Point
&rVal1
, const long nVal2
);
116 long getX() const { return X(); }
117 long getY() const { return Y(); }
118 void setX(long nX
) { X() = nX
; }
119 void setY(long nY
) { Y() = nY
; }
122 inline Point::Point()
126 inline Point::Point( long nX
, long nY
) : Pair( nX
, nY
)
130 inline void Point::Move( long nHorzMove
, long nVertMove
)
136 inline bool Point::IsAbove( const Point
& rPoint
) const
138 return (nB
> rPoint
.nB
);
141 inline bool Point::IsBelow( const Point
& rPoint
) const
143 return (nB
< rPoint
.nB
);
146 inline bool Point::IsLeft( const Point
& rPoint
) const
148 return (nA
< rPoint
.nA
);
151 inline bool Point::IsRight( const Point
& rPoint
) const
153 return (nA
> rPoint
.nA
);
156 inline Point
& Point::operator += ( const Point
& rPoint
)
163 inline Point
& Point::operator -= ( const Point
& rPoint
)
170 inline Point
& Point::operator *= ( const long nVal
)
177 inline Point
& Point::operator /= ( const long nVal
)
184 inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
)
186 return Point( rVal1
.nA
+rVal2
.nA
, rVal1
.nB
+rVal2
.nB
);
189 inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
)
191 return Point( rVal1
.nA
-rVal2
.nA
, rVal1
.nB
-rVal2
.nB
);
194 inline Point
operator*( const Point
&rVal1
, const long nVal2
)
196 return Point( rVal1
.nA
*nVal2
, rVal1
.nB
*nVal2
);
199 inline Point
operator/( const Point
&rVal1
, const long nVal2
)
201 return Point( rVal1
.nA
/nVal2
, rVal1
.nB
/nVal2
);
204 template< typename charT
, typename traits
>
205 inline std::basic_ostream
<charT
, traits
> & operator <<(
206 std::basic_ostream
<charT
, traits
> & stream
, const Point
& point
)
208 return stream
<< point
.X() << ',' << point
.Y();
213 class SAL_WARN_UNUSED Size
: public Pair
217 Size( long nWidth
, long nHeight
);
219 long Width() const { return nA
; }
220 long Height() const { return nB
; }
222 long& Width() { return nA
; }
223 long& Height() { return nB
; }
225 long getWidth() const { return Width(); }
226 long getHeight() const { return Height(); }
227 void setWidth(long nWidth
) { Width() = nWidth
; }
228 void setHeight(long nHeight
) { Height() = nHeight
; }
235 inline Size::Size( long nWidth
, long nHeight
) :
236 Pair( nWidth
, nHeight
)
240 template< typename charT
, typename traits
>
241 inline std::basic_ostream
<charT
, traits
> & operator <<(
242 std::basic_ostream
<charT
, traits
> & stream
, const Size
& size
)
244 return stream
<< size
.Width() << 'x' << size
.Height();
249 #define RANGE_MAX LONG_MAX
251 class SAL_WARN_UNUSED Range
: public Pair
255 Range( long nMin
, long nMax
);
257 long Min() const { return nA
; }
258 long Max() const { return nB
; }
259 long Len() const { return nB
- nA
+ 1; }
261 long& Min() { return nA
; }
262 long& Max() { return nB
; }
264 bool IsInside( long nIs
) const;
269 inline Range::Range()
273 inline Range::Range( long nMin
, long nMax
) : Pair( nMin
, nMax
)
277 inline bool Range::IsInside( long nIs
) const
279 return ((nA
<= nIs
) && (nIs
<= nB
));
282 inline void Range::Justify()
292 template< typename charT
, typename traits
>
293 inline std::basic_ostream
<charT
, traits
> & operator <<(
294 std::basic_ostream
<charT
, traits
> & stream
, const Range
& range
)
296 return stream
<< range
.Min() << '-' << range
.Max();
301 #define SELECTION_MIN LONG_MIN
302 #define SELECTION_MAX LONG_MAX
304 class SAL_WARN_UNUSED Selection
: public Pair
308 Selection( long nPos
);
309 Selection( long nMin
, long nMax
);
311 long Min() const { return nA
; }
312 long Max() const { return nB
; }
313 long Len() const { return nB
- nA
; }
315 long& Min() { return nA
; }
316 long& Max() { return nB
; }
318 bool IsInside( long nIs
) const;
322 bool operator !() const { return !Len(); }
324 long getMin() const { return Min(); }
325 long getMax() const { return Max(); }
326 void setMin(long nMin
) { Min() = nMin
; }
327 void setMax(long nMax
) { Max() = nMax
; }
330 inline Selection::Selection()
334 inline Selection::Selection( long nPos
) : Pair( nPos
, nPos
)
338 inline Selection::Selection( long nMin
, long nMax
) :
343 inline bool Selection::IsInside( long nIs
) const
345 return ((nA
<= nIs
) && (nIs
< nB
));
348 inline void Selection::Justify()
358 template< typename charT
, typename traits
>
359 inline std::basic_ostream
<charT
, traits
> & operator <<(
360 std::basic_ostream
<charT
, traits
> & stream
, const Selection
& selection
)
362 return stream
<< selection
.Min() << '-' << selection
.Max();
366 #define RECT_EMPTY ((short)-32767)
367 #define RECT_MAX LONG_MAX
368 #define RECT_MIN LONG_MIN
370 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Rectangle
374 Rectangle( const Point
& rLT
, const Point
& rRB
);
375 Rectangle( long nLeft
, long nTop
,
376 long nRight
, long nBottom
);
377 Rectangle( const Point
& rLT
, const Size
& rSize
);
379 long Left() const { return nLeft
; }
380 long Right() const { return nRight
; }
381 long Top() const { return nTop
; }
382 long Bottom() const { return nBottom
; }
384 long& Left() { return nLeft
; }
385 long& Right() { return nRight
; }
386 long& Top() { return nTop
; }
387 long& Bottom() { return nBottom
; }
389 inline Point
TopLeft() const;
390 inline Point
TopRight() const;
391 inline Point
TopCenter() const;
392 inline Point
BottomLeft() const;
393 inline Point
BottomRight() const;
394 inline Point
BottomCenter() const;
395 inline Point
LeftCenter() const;
396 inline Point
RightCenter() const;
397 inline Point
Center() const;
399 inline void Move( long nHorzMove
, long nVertMove
);
400 inline void Transpose();
401 inline void SetPos( const Point
& rPoint
);
402 void SetSize( const Size
& rSize
);
403 inline Size
GetSize() const;
405 inline long GetWidth() const;
406 inline long GetHeight() const;
408 Rectangle
& Union( const Rectangle
& rRect
);
409 Rectangle
& Intersection( const Rectangle
& rRect
);
410 inline Rectangle
GetUnion( const Rectangle
& rRect
) const;
411 inline Rectangle
GetIntersection( const Rectangle
& rRect
) const;
415 bool IsInside( const Point
& rPOINT
) const;
416 bool IsInside( const Rectangle
& rRect
) const;
417 bool IsOver( const Rectangle
& rRect
) const;
419 void SetEmpty() { nRight
= nBottom
= RECT_EMPTY
; }
420 inline bool IsEmpty() const;
422 inline bool operator == ( const Rectangle
& rRect
) const;
423 inline bool operator != ( const Rectangle
& rRect
) const;
425 inline Rectangle
& operator += ( const Point
& rPt
);
426 inline Rectangle
& operator -= ( const Point
& rPt
);
428 friend inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
);
429 friend inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
);
431 TOOLS_DLLPUBLIC
friend SvStream
& ReadRectangle( SvStream
& rIStream
, Rectangle
& rRect
);
432 TOOLS_DLLPUBLIC
friend SvStream
& WriteRectangle( SvStream
& rOStream
, const Rectangle
& rRect
);
435 long getX() const { return nLeft
; }
436 long getY() const { return nTop
; }
437 long getWidth() const { return nRight
- nLeft
; }
438 long getHeight() const { return nBottom
- nTop
; }
439 void setX( long n
) { nRight
+= n
-nLeft
; nLeft
= n
; }
440 void setY( long n
) { nBottom
+= n
-nTop
; nTop
= n
; }
441 void setWidth( long n
) { nRight
= nLeft
+ n
; }
442 void setHeight( long n
) { nBottom
= nTop
+ n
; }
443 /// Returns the string representation of the rectangle, format is "x, y, width, height".
444 rtl::OString
toString() const;
447 * Expands the rectangle in all directions by the input value.
449 inline void expand(long nExpandBy
);
451 * Contracts the rectangle in all directions by the input value.
453 inline void contract(long nContractBy
);
462 inline Rectangle::Rectangle()
465 nRight
= nBottom
= RECT_EMPTY
;
468 inline Rectangle::Rectangle( const Point
& rLT
, const Point
& rRB
)
476 inline Rectangle::Rectangle( long _nLeft
, long _nTop
,
477 long _nRight
, long _nBottom
)
485 inline Rectangle::Rectangle( const Point
& rLT
, const Size
& rSize
)
489 nRight
= rSize
.Width() ? nLeft
+rSize
.Width()-1 : RECT_EMPTY
;
490 nBottom
= rSize
.Height() ? nTop
+rSize
.Height()-1 : RECT_EMPTY
;
493 inline bool Rectangle::IsEmpty() const
495 return ((nRight
== RECT_EMPTY
) || (nBottom
== RECT_EMPTY
));
498 inline Point
Rectangle::TopLeft() const
500 return Point( nLeft
, nTop
);
503 inline Point
Rectangle::TopRight() const
505 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
, nTop
);
508 inline Point
Rectangle::BottomLeft() const
510 return Point( nLeft
, (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
513 inline Point
Rectangle::BottomRight() const
515 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
,
516 (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
519 inline Point
Rectangle::TopCenter() const
522 return Point( nLeft
, nTop
);
524 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
525 std::min( nTop
, nBottom
) );
528 inline Point
Rectangle::BottomCenter() const
531 return Point( nLeft
, nTop
);
533 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
534 std::max( nTop
, nBottom
) );
537 inline Point
Rectangle::LeftCenter() const
540 return Point( nLeft
, nTop
);
542 return Point( std::min( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
545 inline Point
Rectangle::RightCenter() const
548 return Point( nLeft
, nTop
);
550 return Point( std::max( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
553 inline Point
Rectangle::Center() const
556 return Point( nLeft
, nTop
);
558 return Point( nLeft
+(nRight
-nLeft
)/2 , nTop
+(nBottom
-nTop
)/2 );
561 inline void Rectangle::Move( long nHorzMove
, long nVertMove
)
565 if ( nRight
!= RECT_EMPTY
)
567 if ( nBottom
!= RECT_EMPTY
)
568 nBottom
+= nVertMove
;
571 void Rectangle::Transpose()
585 inline void Rectangle::SetPos( const Point
& rPoint
)
587 if ( nRight
!= RECT_EMPTY
)
588 nRight
+= rPoint
.X() - nLeft
;
589 if ( nBottom
!= RECT_EMPTY
)
590 nBottom
+= rPoint
.Y() - nTop
;
595 inline long Rectangle::GetWidth() const
598 if ( nRight
== RECT_EMPTY
)
612 inline long Rectangle::GetHeight() const
615 if ( nBottom
== RECT_EMPTY
)
629 inline Size
Rectangle::GetSize() const
631 return Size( GetWidth(), GetHeight() );
634 inline Rectangle
Rectangle::GetUnion( const Rectangle
& rRect
) const
636 Rectangle
aTmpRect( *this );
637 return aTmpRect
.Union( rRect
);
640 inline Rectangle
Rectangle::GetIntersection( const Rectangle
& rRect
) const
642 Rectangle
aTmpRect( *this );
643 return aTmpRect
.Intersection( rRect
);
646 inline bool Rectangle::operator == ( const Rectangle
& rRect
) const
648 return ((nLeft
== rRect
.nLeft
) &&
649 (nTop
== rRect
.nTop
) &&
650 (nRight
== rRect
.nRight
) &&
651 (nBottom
== rRect
.nBottom
));
654 inline bool Rectangle::operator != ( const Rectangle
& rRect
) const
656 return ((nLeft
!= rRect
.nLeft
) ||
657 (nTop
!= rRect
.nTop
) ||
658 (nRight
!= rRect
.nRight
) ||
659 (nBottom
!= rRect
.nBottom
));
662 inline Rectangle
& Rectangle::operator +=( const Point
& rPt
)
666 if ( nRight
!= RECT_EMPTY
)
668 if ( nBottom
!= RECT_EMPTY
)
673 inline Rectangle
& Rectangle::operator -= ( const Point
& rPt
)
677 if ( nRight
!= RECT_EMPTY
)
679 if ( nBottom
!= RECT_EMPTY
)
684 inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
)
686 Rectangle
aRect( rRect
.nLeft
+ rPt
.X(), rRect
.nTop
+ rPt
.Y(),
687 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
+ rPt
.X(),
688 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
+ rPt
.Y() );
692 inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
)
694 Rectangle
aRect( rRect
.nLeft
- rPt
.X(),
695 rRect
.nTop
- rPt
.Y(),
696 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
- rPt
.X(),
697 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
- rPt
.Y() );
701 inline void Rectangle::expand(long nExpandBy
)
706 nBottom
+= nExpandBy
;
709 inline void Rectangle::contract(long nContractBy
)
711 nLeft
+= nContractBy
;
713 nRight
-= nContractBy
;
714 nBottom
-= nContractBy
;
717 template< typename charT
, typename traits
>
718 inline std::basic_ostream
<charT
, traits
> & operator <<(
719 std::basic_ostream
<charT
, traits
> & stream
, const Rectangle
& rectangle
)
721 if (rectangle
.IsEmpty())
722 return stream
<< "EMPTY";
724 return stream
<< rectangle
.getWidth() << 'x' << rectangle
.getHeight()
725 << "@(" << rectangle
.getX() << ',' << rectangle
.getY() << ")";
730 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */