build fix
[LibreOffice.git] / include / tools / gen.hxx
blobbbe095704bb81310b90a5d756983991849ddf3db
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 #ifndef INCLUDED_TOOLS_GEN_HXX
20 #define INCLUDED_TOOLS_GEN_HXX
22 #include <tools/toolsdllapi.h>
24 #include <limits.h>
25 #include <algorithm>
26 #include <ostream>
27 #include <cstdlib>
29 class SvStream;
30 namespace rtl
32 class OString;
35 enum TriState { TRISTATE_FALSE, TRISTATE_TRUE, TRISTATE_INDET };
37 // Pair
39 class SAL_WARN_UNUSED Pair
41 public:
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 );
55 protected:
56 long nA;
57 long nB;
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();
68 } }
70 // Point
72 class SAL_WARN_UNUSED SAL_DLLPUBLIC_EXPORT Point : public Pair
74 public:
75 Point() {}
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 )
107 nA += nHorzMove;
108 nB += nVertMove;
111 inline Point& Point::operator += ( const Point& rPoint )
113 nA += rPoint.nA;
114 nB += rPoint.nB;
115 return *this;
118 inline Point& Point::operator -= ( const Point& rPoint )
120 nA -= rPoint.nA;
121 nB -= rPoint.nB;
122 return *this;
125 inline Point& Point::operator *= ( const long nVal )
127 nA *= nVal;
128 nB *= nVal;
129 return *this;
132 inline Point& Point::operator /= ( const long nVal )
134 nA /= nVal;
135 nB /= nVal;
136 return *this;
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)
166 return !(p1 == 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();
176 // Size
178 class SAL_WARN_UNUSED Size : public Pair
180 public:
181 Size() {}
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)
203 return !(s1 == 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();
213 // Range
215 #define RANGE_MAX LONG_MAX
217 class SAL_WARN_UNUSED Range : public Pair
219 public:
220 Range() {}
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;
232 void Justify();
235 inline bool Range::IsInside( long nIs ) const
237 return ((nA <= nIs) && (nIs <= nB ));
240 inline void Range::Justify()
242 if ( nA > nB )
244 long nHelp = nA;
245 nA = nB;
246 nB = nHelp;
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)
257 return !(r1 == 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();
267 // Selection
269 #define SELECTION_MIN LONG_MIN
270 #define SELECTION_MAX LONG_MAX
272 class SAL_WARN_UNUSED Selection : public Pair
274 public:
275 Selection() {}
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;
288 void Justify();
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()
304 if ( nA > nB )
306 long nHelp = nA;
307 nA = nB;
308 nB = nHelp;
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)
319 return !(s1 == 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();
328 // Rectangle
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
336 public:
337 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;
379 void Justify();
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 );
400 // ONE
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);
422 private:
423 long nLeft;
424 long nTop;
425 long nRight;
426 long nBottom;
429 inline Rectangle::Rectangle()
431 nLeft = nTop = 0;
432 nRight = nBottom = RECT_EMPTY;
435 inline Rectangle::Rectangle( const Point& rLT, const Point& rRB )
437 nLeft = rLT.X();
438 nTop = rLT.Y();
439 nRight = rRB.X();
440 nBottom = rRB.Y();
443 inline Rectangle::Rectangle( long _nLeft, long _nTop,
444 long _nRight, long _nBottom )
446 nLeft = _nLeft;
447 nTop = _nTop;
448 nRight = _nRight;
449 nBottom = _nBottom;
452 inline Rectangle::Rectangle( const Point& rLT, const Size& rSize )
454 nLeft = rLT.X();
455 nTop = rLT.Y();
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
488 if ( IsEmpty() )
489 return Point( nLeft, nTop );
490 else
491 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
492 std::min( nTop, nBottom) );
495 inline Point Rectangle::BottomCenter() const
497 if ( IsEmpty() )
498 return Point( nLeft, nTop );
499 else
500 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
501 std::max( nTop, nBottom) );
504 inline Point Rectangle::LeftCenter() const
506 if ( IsEmpty() )
507 return Point( nLeft, nTop );
508 else
509 return Point( std::min( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
512 inline Point Rectangle::RightCenter() const
514 if ( IsEmpty() )
515 return Point( nLeft, nTop );
516 else
517 return Point( std::max( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
520 inline Point Rectangle::Center() const
522 if ( IsEmpty() )
523 return Point( nLeft, nTop );
524 else
525 return Point( nLeft+(nRight-nLeft)/2 , nTop+(nBottom-nTop)/2 );
528 inline void Rectangle::Move( long nHorzMove, long nVertMove )
530 nLeft += nHorzMove;
531 nTop += nVertMove;
532 if ( nRight != RECT_EMPTY )
533 nRight += nHorzMove;
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;
544 nLeft = rPoint.X();
545 nTop = rPoint.Y();
548 inline long Rectangle::GetWidth() const
550 long n;
551 if ( nRight == RECT_EMPTY )
552 n = 0;
553 else
555 n = nRight - nLeft;
556 if( n < 0 )
557 n--;
558 else
559 n++;
562 return n;
565 inline long Rectangle::GetHeight() const
567 long n;
568 if ( nBottom == RECT_EMPTY )
569 n = 0;
570 else
572 n = nBottom - nTop;
573 if ( n < 0 )
574 n--;
575 else
576 n++;
579 return n;
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 )
617 nLeft += rPt.X();
618 nTop += rPt.Y();
619 if ( nRight != RECT_EMPTY )
620 nRight += rPt.X();
621 if ( nBottom != RECT_EMPTY )
622 nBottom += rPt.Y();
623 return *this;
626 inline Rectangle& Rectangle::operator -= ( const Point& rPt )
628 nLeft -= rPt.X();
629 nTop -= rPt.Y();
630 if ( nRight != RECT_EMPTY )
631 nRight -= rPt.X();
632 if ( nBottom != RECT_EMPTY )
633 nBottom -= rPt.Y();
634 return *this;
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() );
642 return aRect;
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() );
651 return aRect;
654 inline void Rectangle::expand(long nExpandBy)
656 nLeft -= nExpandBy;
657 nTop -= nExpandBy;
658 nRight += nExpandBy;
659 nBottom += nExpandBy;
662 inline void Rectangle::shrink(long nShrinkBy)
664 nLeft += nShrinkBy;
665 nTop += nShrinkBy;
666 nRight -= 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";
676 else
677 return stream << rectangle.getWidth() << 'x' << rectangle.getHeight()
678 << "@(" << rectangle.getX() << ',' << rectangle.getY() << ")";
681 #endif
683 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */