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
42 Pair() : nA(0), nB(0) {}
43 Pair( long _nA
, long _nB
) : nA(_nA
), nB(_nB
) {}
45 long A() const { return nA
; }
46 long B() const { return nB
; }
48 long& A() { return nA
; }
49 long& B() { return nB
; }
51 TOOLS_DLLPUBLIC
rtl::OString
toString() const;
52 TOOLS_DLLPUBLIC
friend SvStream
& ReadPair( SvStream
& rIStream
, Pair
& rPair
);
53 TOOLS_DLLPUBLIC
friend SvStream
& WritePair( SvStream
& rOStream
, const Pair
& rPair
);
60 namespace tools
{ namespace detail
{
62 // Used to implement operator == for subclasses of Pair:
63 inline bool equal(Pair
const & p1
, Pair
const & p2
)
65 return p1
.A() == p2
.A() && p1
.B() == p2
.B();
72 class SAL_WARN_UNUSED SAL_DLLPUBLIC_EXPORT Point
: public Pair
76 Point( long nX
, long nY
) : Pair( nX
, nY
) {}
78 long X() const { return nA
; }
79 long Y() const { return nB
; }
81 long& X() { return nA
; }
82 long& Y() { return nB
; }
84 void Move( long nHorzMove
, long nVertMove
);
86 void RotateAround( long& rX
, long& rY
, short nOrientation
) const;
89 Point
& operator += ( const Point
& rPoint
);
90 Point
& operator -= ( const Point
& rPoint
);
91 Point
& operator *= ( const long nVal
);
92 Point
& operator /= ( const long nVal
);
94 friend inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
);
95 friend inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
);
96 friend inline Point
operator*( const Point
&rVal1
, const long nVal2
);
97 friend inline Point
operator/( const Point
&rVal1
, const long nVal2
);
99 long getX() const { return X(); }
100 long getY() const { return Y(); }
101 void setX(long nX
) { X() = nX
; }
102 void setY(long nY
) { Y() = nY
; }
105 inline void Point::Move( long nHorzMove
, long nVertMove
)
111 inline Point
& Point::operator += ( const Point
& rPoint
)
118 inline Point
& Point::operator -= ( const Point
& rPoint
)
125 inline Point
& Point::operator *= ( const long nVal
)
132 inline Point
& Point::operator /= ( const long nVal
)
139 inline Point
operator+( const Point
&rVal1
, const Point
&rVal2
)
141 return Point( rVal1
.nA
+rVal2
.nA
, rVal1
.nB
+rVal2
.nB
);
144 inline Point
operator-( const Point
&rVal1
, const Point
&rVal2
)
146 return Point( rVal1
.nA
-rVal2
.nA
, rVal1
.nB
-rVal2
.nB
);
149 inline Point
operator*( const Point
&rVal1
, const long nVal2
)
151 return Point( rVal1
.nA
*nVal2
, rVal1
.nB
*nVal2
);
154 inline Point
operator/( const Point
&rVal1
, const long nVal2
)
156 return Point( rVal1
.nA
/nVal2
, rVal1
.nB
/nVal2
);
159 inline bool operator ==(Point
const & p1
, Point
const & p2
)
161 return tools::detail::equal(p1
, p2
);
164 inline bool operator !=(Point
const & p1
, Point
const & p2
)
169 template< typename charT
, typename traits
>
170 inline std::basic_ostream
<charT
, traits
> & operator <<(
171 std::basic_ostream
<charT
, traits
> & stream
, const Point
& point
)
173 return stream
<< point
.X() << ',' << point
.Y();
178 class SAL_WARN_UNUSED Size
: public Pair
182 Size( long nWidth
, long nHeight
) : Pair( nWidth
, nHeight
) {}
184 long Width() const { return nA
; }
185 long Height() const { return nB
; }
187 long& Width() { return nA
; }
188 long& Height() { return nB
; }
190 long getWidth() const { return Width(); }
191 long getHeight() const { return Height(); }
192 void setWidth(long nWidth
) { Width() = nWidth
; }
193 void setHeight(long nHeight
) { Height() = nHeight
; }
196 inline bool operator ==(Size
const & s1
, Size
const & s2
)
198 return tools::detail::equal(s1
, s2
);
201 inline bool operator !=(Size
const & s1
, Size
const & s2
)
206 template< typename charT
, typename traits
>
207 inline std::basic_ostream
<charT
, traits
> & operator <<(
208 std::basic_ostream
<charT
, traits
> & stream
, const Size
& size
)
210 return stream
<< size
.Width() << 'x' << size
.Height();
215 #define RANGE_MAX LONG_MAX
217 class SAL_WARN_UNUSED Range
: public Pair
221 Range( long nMin
, long nMax
) : Pair( nMin
, nMax
) {}
223 long Min() const { return nA
; }
224 long Max() const { return nB
; }
225 long Len() const { return nB
- nA
+ 1; }
227 long& Min() { return nA
; }
228 long& Max() { return nB
; }
230 bool IsInside( long nIs
) const;
235 inline bool Range::IsInside( long nIs
) const
237 return ((nA
<= nIs
) && (nIs
<= nB
));
240 inline void Range::Justify()
250 inline bool operator ==(Range
const & r1
, Range
const & r2
)
252 return tools::detail::equal(r1
, r2
);
255 inline bool operator !=(Range
const & r1
, Range
const & r2
)
260 template< typename charT
, typename traits
>
261 inline std::basic_ostream
<charT
, traits
> & operator <<(
262 std::basic_ostream
<charT
, traits
> & stream
, const Range
& range
)
264 return stream
<< range
.Min() << '-' << range
.Max();
269 #define SELECTION_MIN LONG_MIN
270 #define SELECTION_MAX LONG_MAX
272 class SAL_WARN_UNUSED Selection
: public Pair
276 Selection( long nPos
) : Pair( nPos
, nPos
) {}
277 Selection( long nMin
, long nMax
) : Pair( nMin
, nMax
) {}
279 long Min() const { return nA
; }
280 long Max() const { return nB
; }
281 long Len() const { return nB
- nA
; }
283 long& Min() { return nA
; }
284 long& Max() { return nB
; }
286 bool IsInside( long nIs
) const;
290 bool operator !() const { return !Len(); }
292 long getMin() const { return Min(); }
293 void setMin(long nMin
) { Min() = nMin
; }
294 void setMax(long nMax
) { Max() = nMax
; }
297 inline bool Selection::IsInside( long nIs
) const
299 return ((nA
<= nIs
) && (nIs
< nB
));
302 inline void Selection::Justify()
312 inline bool operator ==(Selection
const & s1
, Selection
const & s2
)
314 return tools::detail::equal(s1
, s2
);
317 inline bool operator !=(Selection
const & s1
, Selection
const & s2
)
322 template< typename charT
, typename traits
>
323 inline std::basic_ostream
<charT
, traits
> & operator <<(
324 std::basic_ostream
<charT
, traits
> & stream
, const Selection
& selection
)
326 return stream
<< selection
.Min() << '-' << selection
.Max();
330 #define RECT_EMPTY ((short)-32767)
331 #define RECT_MAX LONG_MAX
332 #define RECT_MIN LONG_MIN
334 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Rectangle
338 Rectangle( const Point
& rLT
, const Point
& rRB
);
339 Rectangle( long nLeft
, long nTop
,
340 long nRight
, long nBottom
);
341 Rectangle( const Point
& rLT
, const Size
& rSize
);
343 long Left() const { return nLeft
; }
344 long Right() const { return nRight
; }
345 long Top() const { return nTop
; }
346 long Bottom() const { return nBottom
; }
348 long& Left() { return nLeft
; }
349 long& Right() { return nRight
; }
350 long& Top() { return nTop
; }
351 long& Bottom() { return nBottom
; }
353 inline Point
TopLeft() const;
354 inline Point
TopRight() const;
355 inline Point
TopCenter() const;
356 inline Point
BottomLeft() const;
357 inline Point
BottomRight() const;
358 inline Point
BottomCenter() const;
359 inline Point
LeftCenter() const;
360 inline Point
RightCenter() const;
361 inline Point
Center() const;
363 /// Move the top and left edges by a delta, preserving width and height
364 inline void Move( long nHorzMoveDelta
, long nVertMoveDelta
);
365 inline void SetPos( const Point
& rPoint
);
366 void SetSize( const Size
& rSize
);
367 inline Size
GetSize() const;
369 /// Returns the difference between right and left, assuming the range is inclusive.
370 inline long GetWidth() const;
371 /// Returns the difference between bottom and top, assuming the range is inclusive.
372 inline long GetHeight() const;
374 Rectangle
& Union( const Rectangle
& rRect
);
375 Rectangle
& Intersection( const Rectangle
& rRect
);
376 inline Rectangle
GetUnion( const Rectangle
& rRect
) const;
377 inline Rectangle
GetIntersection( const Rectangle
& rRect
) const;
381 bool IsInside( const Point
& rPOINT
) const;
382 bool IsInside( const Rectangle
& rRect
) const;
383 bool IsOver( const Rectangle
& rRect
) const;
385 void SetEmpty() { nRight
= nBottom
= RECT_EMPTY
; }
386 inline bool IsEmpty() const;
388 inline bool operator == ( const Rectangle
& rRect
) const;
389 inline bool operator != ( const Rectangle
& rRect
) const;
391 inline Rectangle
& operator += ( const Point
& rPt
);
392 inline Rectangle
& operator -= ( const Point
& rPt
);
394 friend inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
);
395 friend inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
);
397 TOOLS_DLLPUBLIC
friend SvStream
& ReadRectangle( SvStream
& rIStream
, Rectangle
& rRect
);
398 TOOLS_DLLPUBLIC
friend SvStream
& WriteRectangle( SvStream
& rOStream
, const Rectangle
& rRect
);
401 long getX() const { return nLeft
; }
402 long getY() const { return nTop
; }
403 /// Returns the difference between right and left, assuming the range includes one end, but not the other.
404 long getWidth() const { return nRight
- nLeft
; }
405 /// Returns the difference between bottom and top, assuming the range includes one end, but not the other.
406 long getHeight() const { return nBottom
- nTop
; }
407 /// Set the left edge of the rectangle to x, preserving the width
408 void setX( long x
) { nRight
+= x
- nLeft
; nLeft
= x
; }
409 /// Set the top edge of the rectangle to y, preserving the height
410 void setY( long y
) { nBottom
+= y
- nTop
; nTop
= y
; }
411 void setWidth( long n
) { nRight
= nLeft
+ n
; }
412 void setHeight( long n
) { nBottom
= nTop
+ n
; }
413 /// Returns the string representation of the rectangle, format is "x, y, width, height".
414 rtl::OString
toString() const;
417 * Expands the rectangle in all directions by the input value.
419 inline void expand(long nExpandBy
);
420 inline void shrink(long nShrinkBy
);
429 inline Rectangle::Rectangle()
432 nRight
= nBottom
= RECT_EMPTY
;
435 inline Rectangle::Rectangle( const Point
& rLT
, const Point
& rRB
)
443 inline Rectangle::Rectangle( long _nLeft
, long _nTop
,
444 long _nRight
, long _nBottom
)
452 inline Rectangle::Rectangle( const Point
& rLT
, const Size
& rSize
)
456 nRight
= rSize
.Width() ? nLeft
+rSize
.Width()-1 : RECT_EMPTY
;
457 nBottom
= rSize
.Height() ? nTop
+rSize
.Height()-1 : RECT_EMPTY
;
460 inline bool Rectangle::IsEmpty() const
462 return (nRight
== RECT_EMPTY
) || (nBottom
== RECT_EMPTY
);
465 inline Point
Rectangle::TopLeft() const
467 return Point( nLeft
, nTop
);
470 inline Point
Rectangle::TopRight() const
472 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
, nTop
);
475 inline Point
Rectangle::BottomLeft() const
477 return Point( nLeft
, (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
480 inline Point
Rectangle::BottomRight() const
482 return Point( (nRight
== RECT_EMPTY
) ? nLeft
: nRight
,
483 (nBottom
== RECT_EMPTY
) ? nTop
: nBottom
);
486 inline Point
Rectangle::TopCenter() const
489 return Point( nLeft
, nTop
);
491 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
492 std::min( nTop
, nBottom
) );
495 inline Point
Rectangle::BottomCenter() const
498 return Point( nLeft
, nTop
);
500 return Point( std::min( nLeft
, nRight
) + std::abs( (nRight
- nLeft
)/2 ),
501 std::max( nTop
, nBottom
) );
504 inline Point
Rectangle::LeftCenter() const
507 return Point( nLeft
, nTop
);
509 return Point( std::min( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
512 inline Point
Rectangle::RightCenter() const
515 return Point( nLeft
, nTop
);
517 return Point( std::max( nLeft
, nRight
), nTop
+ (nBottom
- nTop
)/2 );
520 inline Point
Rectangle::Center() const
523 return Point( nLeft
, nTop
);
525 return Point( nLeft
+(nRight
-nLeft
)/2 , nTop
+(nBottom
-nTop
)/2 );
528 inline void Rectangle::Move( long nHorzMove
, long nVertMove
)
532 if ( nRight
!= RECT_EMPTY
)
534 if ( nBottom
!= RECT_EMPTY
)
535 nBottom
+= nVertMove
;
538 inline void Rectangle::SetPos( const Point
& rPoint
)
540 if ( nRight
!= RECT_EMPTY
)
541 nRight
+= rPoint
.X() - nLeft
;
542 if ( nBottom
!= RECT_EMPTY
)
543 nBottom
+= rPoint
.Y() - nTop
;
548 inline long Rectangle::GetWidth() const
551 if ( nRight
== RECT_EMPTY
)
565 inline long Rectangle::GetHeight() const
568 if ( nBottom
== RECT_EMPTY
)
582 inline Size
Rectangle::GetSize() const
584 return Size( GetWidth(), GetHeight() );
587 inline Rectangle
Rectangle::GetUnion( const Rectangle
& rRect
) const
589 Rectangle
aTmpRect( *this );
590 return aTmpRect
.Union( rRect
);
593 inline Rectangle
Rectangle::GetIntersection( const Rectangle
& rRect
) const
595 Rectangle
aTmpRect( *this );
596 return aTmpRect
.Intersection( rRect
);
599 inline bool Rectangle::operator == ( const Rectangle
& rRect
) const
601 return (nLeft
== rRect
.nLeft
) &&
602 (nTop
== rRect
.nTop
) &&
603 (nRight
== rRect
.nRight
) &&
604 (nBottom
== rRect
.nBottom
);
607 inline bool Rectangle::operator != ( const Rectangle
& rRect
) const
609 return (nLeft
!= rRect
.nLeft
) ||
610 (nTop
!= rRect
.nTop
) ||
611 (nRight
!= rRect
.nRight
) ||
612 (nBottom
!= rRect
.nBottom
);
615 inline Rectangle
& Rectangle::operator +=( const Point
& rPt
)
619 if ( nRight
!= RECT_EMPTY
)
621 if ( nBottom
!= RECT_EMPTY
)
626 inline Rectangle
& Rectangle::operator -= ( const Point
& rPt
)
630 if ( nRight
!= RECT_EMPTY
)
632 if ( nBottom
!= RECT_EMPTY
)
637 inline Rectangle
operator + ( const Rectangle
& rRect
, const Point
& rPt
)
639 Rectangle
aRect( rRect
.nLeft
+ rPt
.X(), rRect
.nTop
+ rPt
.Y(),
640 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
+ rPt
.X(),
641 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
+ rPt
.Y() );
645 inline Rectangle
operator - ( const Rectangle
& rRect
, const Point
& rPt
)
647 Rectangle
aRect( rRect
.nLeft
- rPt
.X(),
648 rRect
.nTop
- rPt
.Y(),
649 (rRect
.nRight
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nRight
- rPt
.X(),
650 (rRect
.nBottom
== RECT_EMPTY
) ? RECT_EMPTY
: rRect
.nBottom
- rPt
.Y() );
654 inline void Rectangle::expand(long nExpandBy
)
659 nBottom
+= nExpandBy
;
662 inline void Rectangle::shrink(long nShrinkBy
)
667 nBottom
-= nShrinkBy
;
670 template< typename charT
, typename traits
>
671 inline std::basic_ostream
<charT
, traits
> & operator <<(
672 std::basic_ostream
<charT
, traits
> & stream
, const Rectangle
& rectangle
)
674 if (rectangle
.IsEmpty())
675 return stream
<< "EMPTY";
677 return stream
<< rectangle
.getWidth() << 'x' << rectangle
.getHeight()
678 << "@(" << rectangle
.getX() << ',' << rectangle
.getY() << ")";
683 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */