bump product version to 4.1.6.2
[LibreOffice.git] / include / tools / gen.hxx
blobc1a1e359bccc86c2df75085e797fc9ba2de39395
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 _SV_GEN_HXX
20 #define _SV_GEN_HXX
22 #include "tools/toolsdllapi.h"
23 #include <tools/solar.h>
25 #include <limits.h>
26 #include <ostream>
27 #include <cstdlib>
29 class SvStream;
31 // Pair
33 class SAL_WARN_UNUSED Pair
35 public:
36 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 );
51 protected:
52 long nA;
53 long nB;
56 inline Pair::Pair()
58 nA = nB = 0;
61 inline Pair::Pair( long _nA, long _nB )
63 Pair::nA = _nA;
64 Pair::nB = _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));
77 // Point
79 class SAL_WARN_UNUSED Point : public Pair
81 public:
82 Point();
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 )
123 nA += nHorzMove;
124 nB += 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 )
149 nA += rPoint.nA;
150 nB += rPoint.nB;
151 return *this;
154 inline Point& Point::operator -= ( const Point& rPoint )
156 nA -= rPoint.nA;
157 nB -= rPoint.nB;
158 return *this;
161 inline Point& Point::operator *= ( const long nVal )
163 nA *= nVal;
164 nB *= nVal;
165 return *this;
168 inline Point& Point::operator /= ( const long nVal )
170 nA /= nVal;
171 nB /= nVal;
172 return *this;
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();
202 // Size
204 class SAL_WARN_UNUSED Size : public Pair
206 public:
207 Size();
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; }
222 inline Size::Size()
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();
238 // Range
240 #define RANGE_MAX LONG_MAX
242 class SAL_WARN_UNUSED Range : public Pair
244 public:
245 Range();
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;
257 void Justify();
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()
275 if ( nA > nB )
277 long nHelp = nA;
278 nA = nB;
279 nB = nHelp;
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();
290 // Selection
292 #define SELECTION_MIN LONG_MIN
293 #define SELECTION_MAX LONG_MAX
295 class SAL_WARN_UNUSED Selection : public Pair
297 public:
298 Selection();
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;
311 void Justify();
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 ) :
330 Pair( nMin, nMax )
334 inline sal_Bool Selection::IsInside( long nIs ) const
336 return ((nA <= nIs) && (nIs < nB ));
339 inline void Selection::Justify()
341 if ( nA > nB )
343 long nHelp = nA;
344 nA = nB;
345 nB = nHelp;
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();
355 // Rectangle
357 #define RECT_EMPTY ((short)-32767)
359 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Rectangle
361 public:
362 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;
402 void Justify();
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 );
423 // ONE
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; }
433 private:
434 long nLeft;
435 long nTop;
436 long nRight;
437 long nBottom;
440 inline Rectangle::Rectangle()
442 nLeft = nTop = 0;
443 nRight = nBottom = RECT_EMPTY;
446 inline Rectangle::Rectangle( const Point& rLT, const Point& rRB )
448 nLeft = rLT.X();
449 nTop = rLT.Y();
450 nRight = rRB.X();
451 nBottom = rRB.Y();
454 inline Rectangle::Rectangle( long _nLeft, long _nTop,
455 long _nRight, long _nBottom )
457 nLeft = _nLeft;
458 nTop = _nTop;
459 nRight = _nRight;
460 nBottom = _nBottom;
463 inline Rectangle::Rectangle( const Point& rLT, const Size& rSize )
465 nLeft = rLT.X();
466 nTop = rLT.Y();
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
499 if ( IsEmpty() )
500 return Point( nLeft, nTop );
501 else
502 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
503 std::min( nTop, nBottom) );
506 inline Point Rectangle::BottomCenter() const
508 if ( IsEmpty() )
509 return Point( nLeft, nTop );
510 else
511 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
512 std::max( nTop, nBottom) );
515 inline Point Rectangle::LeftCenter() const
517 if ( IsEmpty() )
518 return Point( nLeft, nTop );
519 else
520 return Point( std::min( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
523 inline Point Rectangle::RightCenter() const
525 if ( IsEmpty() )
526 return Point( nLeft, nTop );
527 else
528 return Point( std::max( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
531 inline Point Rectangle::Center() const
533 if ( IsEmpty() )
534 return Point( nLeft, nTop );
535 else
536 return Point( nLeft+(nRight-nLeft)/2 , nTop+(nBottom-nTop)/2 );
539 inline void Rectangle::Move( long nHorzMove, long nVertMove )
541 nLeft += nHorzMove;
542 nTop += nVertMove;
543 if ( nRight != RECT_EMPTY )
544 nRight += nHorzMove;
545 if ( nBottom != RECT_EMPTY )
546 nBottom += nVertMove;
549 void Rectangle::Transpose()
551 if ( !IsEmpty() )
553 long swap( nLeft );
554 nLeft = nTop;
555 nTop = swap;
557 swap = nRight;
558 nRight = nBottom;
559 nBottom = swap;
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;
569 nLeft = rPoint.X();
570 nTop = rPoint.Y();
573 inline long Rectangle::GetWidth() const
575 long n;
576 if ( nRight == RECT_EMPTY )
577 n = 0;
578 else
580 n = nRight - nLeft;
581 if( n < 0 )
582 n--;
583 else
584 n++;
587 return n;
590 inline long Rectangle::GetHeight() const
592 long n;
593 if ( nBottom == RECT_EMPTY )
594 n = 0;
595 else
597 n = nBottom - nTop;
598 if ( n < 0 )
599 n--;
600 else
601 n++;
604 return n;
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 )
642 nLeft += rPt.X();
643 nTop += rPt.Y();
644 if ( nRight != RECT_EMPTY )
645 nRight += rPt.X();
646 if ( nBottom != RECT_EMPTY )
647 nBottom += rPt.Y();
648 return *this;
651 inline Rectangle& Rectangle::operator -= ( const Point& rPt )
653 nLeft -= rPt.X();
654 nTop -= rPt.Y();
655 if ( nRight != RECT_EMPTY )
656 nRight -= rPt.X();
657 if ( nBottom != RECT_EMPTY )
658 nBottom -= rPt.Y();
659 return *this;
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() );
667 return aRect;
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() );
676 return aRect;
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";
685 else
686 return stream << rectangle.getWidth() << 'x' << rectangle.getHeight()
687 << "@(" << rectangle.getX() << ',' << rectangle.getY() << ")";
690 #endif
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */