update emoji autocorrect entries from po-files
[LibreOffice.git] / include / tools / gen.hxx
blob3e3ec8ecf97b64e04952b62dd023ac1658b8eaf0
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();
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 );
57 protected:
58 long nA;
59 long nB;
62 inline Pair::Pair()
64 nA = nB = 0;
67 inline Pair::Pair( long _nA, long _nB )
69 Pair::nA = _nA;
70 Pair::nB = _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));
83 // Point
85 class SAL_DLLPUBLIC_EXPORT SAL_WARN_UNUSED Point : public Pair
87 public:
88 Point();
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 )
132 nA += nHorzMove;
133 nB += 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 )
158 nA += rPoint.nA;
159 nB += rPoint.nB;
160 return *this;
163 inline Point& Point::operator -= ( const Point& rPoint )
165 nA -= rPoint.nA;
166 nB -= rPoint.nB;
167 return *this;
170 inline Point& Point::operator *= ( const long nVal )
172 nA *= nVal;
173 nB *= nVal;
174 return *this;
177 inline Point& Point::operator /= ( const long nVal )
179 nA /= nVal;
180 nB /= nVal;
181 return *this;
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();
211 // Size
213 class SAL_WARN_UNUSED Size : public Pair
215 public:
216 Size();
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; }
231 inline Size::Size()
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();
247 // Range
249 #define RANGE_MAX LONG_MAX
251 class SAL_WARN_UNUSED Range : public Pair
253 public:
254 Range();
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;
266 void Justify();
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()
284 if ( nA > nB )
286 long nHelp = nA;
287 nA = nB;
288 nB = nHelp;
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();
299 // Selection
301 #define SELECTION_MIN LONG_MIN
302 #define SELECTION_MAX LONG_MAX
304 class SAL_WARN_UNUSED Selection : public Pair
306 public:
307 Selection();
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;
320 void Justify();
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 ) :
339 Pair( nMin, nMax )
343 inline bool Selection::IsInside( long nIs ) const
345 return ((nA <= nIs) && (nIs < nB ));
348 inline void Selection::Justify()
350 if ( nA > nB )
352 long nHelp = nA;
353 nA = nB;
354 nB = nHelp;
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();
364 // Rectangle
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
372 public:
373 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;
413 void Justify();
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 );
434 // ONE
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);
455 private:
456 long nLeft;
457 long nTop;
458 long nRight;
459 long nBottom;
462 inline Rectangle::Rectangle()
464 nLeft = nTop = 0;
465 nRight = nBottom = RECT_EMPTY;
468 inline Rectangle::Rectangle( const Point& rLT, const Point& rRB )
470 nLeft = rLT.X();
471 nTop = rLT.Y();
472 nRight = rRB.X();
473 nBottom = rRB.Y();
476 inline Rectangle::Rectangle( long _nLeft, long _nTop,
477 long _nRight, long _nBottom )
479 nLeft = _nLeft;
480 nTop = _nTop;
481 nRight = _nRight;
482 nBottom = _nBottom;
485 inline Rectangle::Rectangle( const Point& rLT, const Size& rSize )
487 nLeft = rLT.X();
488 nTop = rLT.Y();
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
521 if ( IsEmpty() )
522 return Point( nLeft, nTop );
523 else
524 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
525 std::min( nTop, nBottom) );
528 inline Point Rectangle::BottomCenter() const
530 if ( IsEmpty() )
531 return Point( nLeft, nTop );
532 else
533 return Point( std::min( nLeft, nRight ) + std::abs( (nRight - nLeft)/2 ),
534 std::max( nTop, nBottom) );
537 inline Point Rectangle::LeftCenter() const
539 if ( IsEmpty() )
540 return Point( nLeft, nTop );
541 else
542 return Point( std::min( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
545 inline Point Rectangle::RightCenter() const
547 if ( IsEmpty() )
548 return Point( nLeft, nTop );
549 else
550 return Point( std::max( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
553 inline Point Rectangle::Center() const
555 if ( IsEmpty() )
556 return Point( nLeft, nTop );
557 else
558 return Point( nLeft+(nRight-nLeft)/2 , nTop+(nBottom-nTop)/2 );
561 inline void Rectangle::Move( long nHorzMove, long nVertMove )
563 nLeft += nHorzMove;
564 nTop += nVertMove;
565 if ( nRight != RECT_EMPTY )
566 nRight += nHorzMove;
567 if ( nBottom != RECT_EMPTY )
568 nBottom += nVertMove;
571 void Rectangle::Transpose()
573 if ( !IsEmpty() )
575 long swap( nLeft );
576 nLeft = nTop;
577 nTop = swap;
579 swap = nRight;
580 nRight = nBottom;
581 nBottom = swap;
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;
591 nLeft = rPoint.X();
592 nTop = rPoint.Y();
595 inline long Rectangle::GetWidth() const
597 long n;
598 if ( nRight == RECT_EMPTY )
599 n = 0;
600 else
602 n = nRight - nLeft;
603 if( n < 0 )
604 n--;
605 else
606 n++;
609 return n;
612 inline long Rectangle::GetHeight() const
614 long n;
615 if ( nBottom == RECT_EMPTY )
616 n = 0;
617 else
619 n = nBottom - nTop;
620 if ( n < 0 )
621 n--;
622 else
623 n++;
626 return n;
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 )
664 nLeft += rPt.X();
665 nTop += rPt.Y();
666 if ( nRight != RECT_EMPTY )
667 nRight += rPt.X();
668 if ( nBottom != RECT_EMPTY )
669 nBottom += rPt.Y();
670 return *this;
673 inline Rectangle& Rectangle::operator -= ( const Point& rPt )
675 nLeft -= rPt.X();
676 nTop -= rPt.Y();
677 if ( nRight != RECT_EMPTY )
678 nRight -= rPt.X();
679 if ( nBottom != RECT_EMPTY )
680 nBottom -= rPt.Y();
681 return *this;
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() );
689 return aRect;
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() );
698 return aRect;
701 inline void Rectangle::expand(long nExpandBy)
703 nLeft -= nExpandBy;
704 nTop -= nExpandBy;
705 nRight += nExpandBy;
706 nBottom += nExpandBy;
709 inline void Rectangle::contract(long nContractBy)
711 nLeft += nContractBy;
712 nTop += 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";
723 else
724 return stream << rectangle.getWidth() << 'x' << rectangle.getHeight()
725 << "@(" << rectangle.getX() << ',' << rectangle.getY() << ")";
728 #endif
730 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */