Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef _SV_GEN_HXX
20 : #define _SV_GEN_HXX
21 :
22 : #include "tools/toolsdllapi.h"
23 : #include <tools/solar.h>
24 :
25 : #include <limits.h>
26 : #include <ostream>
27 :
28 : class SvStream;
29 :
30 : // Pair
31 :
32 : class SAL_WARN_UNUSED Pair
33 : {
34 : public:
35 : Pair();
36 : Pair( long nA, long nB );
37 :
38 0 : long A() const { return nA; }
39 0 : long B() const { return nB; }
40 :
41 : long& A() { return nA; }
42 : long& B() { return nB; }
43 :
44 : sal_Bool operator == ( const Pair& rPair ) const;
45 : sal_Bool operator != ( const Pair& rPair ) const;
46 :
47 : TOOLS_DLLPUBLIC friend SvStream& operator>>( SvStream& rIStream, Pair& rPair );
48 : TOOLS_DLLPUBLIC friend SvStream& operator<<( SvStream& rOStream, const Pair& rPair );
49 :
50 : protected:
51 : long nA;
52 : long nB;
53 : };
54 :
55 400 : inline Pair::Pair()
56 : {
57 400 : nA = nB = 0;
58 400 : }
59 :
60 31497 : inline Pair::Pair( long _nA, long _nB )
61 : {
62 31497 : Pair::nA = _nA;
63 31497 : Pair::nB = _nB;
64 31497 : }
65 :
66 0 : inline sal_Bool Pair::operator == ( const Pair& rPair ) const
67 : {
68 0 : return ((nA == rPair.nA) && (nB == rPair.nB));
69 : }
70 :
71 0 : inline sal_Bool Pair::operator != ( const Pair& rPair ) const
72 : {
73 0 : return ((nA != rPair.nA) || (nB != rPair.nB));
74 : }
75 :
76 : // Point
77 :
78 : class SAL_WARN_UNUSED Point : public Pair
79 : {
80 : public:
81 : Point();
82 : Point( long nX, long nY );
83 :
84 80978 : long X() const { return nA; }
85 80967 : long Y() const { return nB; }
86 :
87 308398 : long& X() { return nA; }
88 308398 : long& Y() { return nB; }
89 :
90 : void Move( long nHorzMove, long nVertMove );
91 : sal_Bool IsAbove( const Point& rPoint ) const;
92 : sal_Bool IsBelow( const Point& rPoint ) const;
93 : sal_Bool IsLeft( const Point& rPoint ) const;
94 : sal_Bool IsRight( const Point& rPoint ) const;
95 :
96 : Point& operator += ( const Point& rPoint );
97 : Point& operator -= ( const Point& rPoint );
98 : Point& operator *= ( const long nVal );
99 : Point& operator /= ( const long nVal );
100 :
101 : friend inline Point operator+( const Point &rVal1, const Point &rVal2 );
102 : friend inline Point operator-( const Point &rVal1, const Point &rVal2 );
103 : friend inline Point operator*( const Point &rVal1, const long nVal2 );
104 : friend inline Point operator/( const Point &rVal1, const long nVal2 );
105 :
106 : long getX() const { return X(); }
107 : long getY() const { return Y(); }
108 : void setX(long nX) { X() = nX; }
109 : void setY(long nY) { Y() = nY; }
110 : };
111 :
112 400 : inline Point::Point()
113 : {
114 400 : }
115 :
116 31495 : inline Point::Point( long nX, long nY ) : Pair( nX, nY )
117 : {
118 31495 : }
119 :
120 : inline void Point::Move( long nHorzMove, long nVertMove )
121 : {
122 : nA += nHorzMove;
123 : nB += nVertMove;
124 : }
125 :
126 : inline sal_Bool Point::IsAbove( const Point& rPoint ) const
127 : {
128 : return (nB > rPoint.nB);
129 : }
130 :
131 : inline sal_Bool Point::IsBelow( const Point& rPoint ) const
132 : {
133 : return (nB < rPoint.nB);
134 : }
135 :
136 : inline sal_Bool Point::IsLeft( const Point& rPoint ) const
137 : {
138 : return (nA < rPoint.nA);
139 : }
140 :
141 : inline sal_Bool Point::IsRight( const Point& rPoint ) const
142 : {
143 : return (nA > rPoint.nA);
144 : }
145 :
146 0 : inline Point& Point::operator += ( const Point& rPoint )
147 : {
148 0 : nA += rPoint.nA;
149 0 : nB += rPoint.nB;
150 0 : return *this;
151 : }
152 :
153 : inline Point& Point::operator -= ( const Point& rPoint )
154 : {
155 : nA -= rPoint.nA;
156 : nB -= rPoint.nB;
157 : return *this;
158 : }
159 :
160 : inline Point& Point::operator *= ( const long nVal )
161 : {
162 : nA *= nVal;
163 : nB *= nVal;
164 : return *this;
165 : }
166 :
167 : inline Point& Point::operator /= ( const long nVal )
168 : {
169 : nA /= nVal;
170 : nB /= nVal;
171 : return *this;
172 : }
173 :
174 : inline Point operator+( const Point &rVal1, const Point &rVal2 )
175 : {
176 : return Point( rVal1.nA+rVal2.nA, rVal1.nB+rVal2.nB );
177 : }
178 :
179 : inline Point operator-( const Point &rVal1, const Point &rVal2 )
180 : {
181 : return Point( rVal1.nA-rVal2.nA, rVal1.nB-rVal2.nB );
182 : }
183 :
184 : inline Point operator*( const Point &rVal1, const long nVal2 )
185 : {
186 : return Point( rVal1.nA*nVal2, rVal1.nB*nVal2 );
187 : }
188 :
189 : inline Point operator/( const Point &rVal1, const long nVal2 )
190 : {
191 : return Point( rVal1.nA/nVal2, rVal1.nB/nVal2 );
192 : }
193 :
194 : template< typename charT, typename traits >
195 : inline std::basic_ostream<charT, traits> & operator <<(
196 : std::basic_ostream<charT, traits> & stream, const Point& point )
197 : {
198 : return stream << point.X() << ',' << point.Y();
199 : }
200 :
201 : // Size
202 :
203 : class SAL_WARN_UNUSED Size : public Pair
204 : {
205 : public:
206 : Size();
207 : Size( long nWidth, long nHeight );
208 :
209 15779 : long Width() const { return nA; }
210 15693 : long Height() const { return nB; }
211 :
212 0 : long& Width() { return nA; }
213 0 : long& Height() { return nB; }
214 :
215 : long getWidth() const { return Width(); }
216 : long getHeight() const { return Height(); }
217 : void setWidth(long nWidth) { Width() = nWidth; }
218 : void setHeight(long nHeight) { Height() = nHeight; }
219 : };
220 :
221 : inline Size::Size()
222 : {
223 : }
224 :
225 0 : inline Size::Size( long nWidth, long nHeight ) :
226 0 : Pair( nWidth, nHeight )
227 : {
228 0 : }
229 :
230 : template< typename charT, typename traits >
231 : inline std::basic_ostream<charT, traits> & operator <<(
232 : std::basic_ostream<charT, traits> & stream, const Size& size )
233 : {
234 : return stream << size.Width() << 'x' << size.Height();
235 : }
236 :
237 : // Range
238 :
239 : #define RANGE_MAX LONG_MAX
240 :
241 : class SAL_WARN_UNUSED Range : public Pair
242 : {
243 : public:
244 : Range();
245 : Range( long nMin, long nMax );
246 :
247 29 : long Min() const { return nA; }
248 29 : long Max() const { return nB; }
249 23 : long Len() const { return nB - nA + 1; }
250 :
251 13 : long& Min() { return nA; }
252 37 : long& Max() { return nB; }
253 :
254 : sal_Bool IsInside( long nIs ) const;
255 :
256 : void Justify();
257 : };
258 :
259 : inline Range::Range()
260 : {
261 : }
262 :
263 2 : inline Range::Range( long nMin, long nMax ) : Pair( nMin, nMax )
264 : {
265 2 : }
266 :
267 22 : inline sal_Bool Range::IsInside( long nIs ) const
268 : {
269 22 : return ((nA <= nIs) && (nIs <= nB ));
270 : }
271 :
272 : inline void Range::Justify()
273 : {
274 : if ( nA > nB )
275 : {
276 : long nHelp = nA;
277 : nA = nB;
278 : nB = nHelp;
279 : }
280 : }
281 :
282 : template< typename charT, typename traits >
283 : inline std::basic_ostream<charT, traits> & operator <<(
284 : std::basic_ostream<charT, traits> & stream, const Range& range )
285 : {
286 : return stream << range.Min() << '-' << range.Max();
287 : }
288 :
289 : // Selection
290 :
291 : #define SELECTION_MIN LONG_MIN
292 : #define SELECTION_MAX LONG_MAX
293 :
294 : class SAL_WARN_UNUSED Selection : public Pair
295 : {
296 : public:
297 : Selection();
298 : Selection( long nPos );
299 : Selection( long nMin, long nMax );
300 :
301 : long Min() const { return nA; }
302 : long Max() const { return nB; }
303 : long Len() const { return nB - nA; }
304 :
305 : long& Min() { return nA; }
306 : long& Max() { return nB; }
307 :
308 : sal_Bool IsInside( long nIs ) const;
309 :
310 : void Justify();
311 :
312 : sal_Bool operator !() const { return !Len(); }
313 :
314 : long getMin() const { return Min(); }
315 : long getMax() const { return Max(); }
316 : void setMin(long nMin) { Min() = nMin; }
317 : void setMax(long nMax) { Max() = nMax; }
318 : };
319 :
320 : inline Selection::Selection()
321 : {
322 : }
323 :
324 : inline Selection::Selection( long nPos ) : Pair( nPos, nPos )
325 : {
326 : }
327 :
328 : inline Selection::Selection( long nMin, long nMax ) :
329 : Pair( nMin, nMax )
330 : {
331 : }
332 :
333 : inline sal_Bool Selection::IsInside( long nIs ) const
334 : {
335 : return ((nA <= nIs) && (nIs < nB ));
336 : }
337 :
338 : inline void Selection::Justify()
339 : {
340 : if ( nA > nB )
341 : {
342 : long nHelp = nA;
343 : nA = nB;
344 : nB = nHelp;
345 : }
346 : }
347 :
348 : template< typename charT, typename traits >
349 : inline std::basic_ostream<charT, traits> & operator <<(
350 : std::basic_ostream<charT, traits> & stream, const Selection& selection )
351 : {
352 : return stream << selection.Min() << '-' << selection.Max();
353 : }
354 : // Rectangle
355 :
356 : #define RECT_EMPTY ((short)-32767)
357 :
358 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Rectangle
359 : {
360 : public:
361 : Rectangle();
362 : Rectangle( const Point& rLT, const Point& rRB );
363 : Rectangle( long nLeft, long nTop,
364 : long nRight, long nBottom );
365 : Rectangle( const Point& rLT, const Size& rSize );
366 :
367 0 : long Left() const { return nLeft; }
368 0 : long Right() const { return nRight; }
369 0 : long Top() const { return nTop; }
370 : long Bottom() const { return nBottom; }
371 :
372 0 : long& Left() { return nLeft; }
373 0 : long& Right() { return nRight; }
374 0 : long& Top() { return nTop; }
375 0 : long& Bottom() { return nBottom; }
376 :
377 : inline Point TopLeft() const;
378 : inline Point TopRight() const;
379 : inline Point TopCenter() const;
380 : inline Point BottomLeft() const;
381 : inline Point BottomRight() const;
382 : inline Point BottomCenter() const;
383 : inline Point LeftCenter() const;
384 : inline Point RightCenter() const;
385 : inline Point Center() const;
386 :
387 : inline void Move( long nHorzMove, long nVertMove );
388 : inline void Transpose();
389 : inline void SetPos( const Point& rPoint );
390 : void SetSize( const Size& rSize );
391 : inline Size GetSize() const;
392 :
393 : inline long GetWidth() const;
394 : inline long GetHeight() const;
395 :
396 : Rectangle& Union( const Rectangle& rRect );
397 : Rectangle& Intersection( const Rectangle& rRect );
398 : inline Rectangle GetUnion( const Rectangle& rRect ) const;
399 : inline Rectangle GetIntersection( const Rectangle& rRect ) const;
400 :
401 : void Justify();
402 :
403 : sal_Bool IsInside( const Point& rPOINT ) const;
404 : sal_Bool IsInside( const Rectangle& rRect ) const;
405 : sal_Bool IsOver( const Rectangle& rRect ) const;
406 :
407 : void SetEmpty() { nRight = nBottom = RECT_EMPTY; }
408 : inline sal_Bool IsEmpty() const;
409 :
410 : inline sal_Bool operator == ( const Rectangle& rRect ) const;
411 : inline sal_Bool operator != ( const Rectangle& rRect ) const;
412 :
413 : inline Rectangle& operator += ( const Point& rPt );
414 : inline Rectangle& operator -= ( const Point& rPt );
415 :
416 : friend inline Rectangle operator + ( const Rectangle& rRect, const Point& rPt );
417 : friend inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt );
418 :
419 : TOOLS_DLLPUBLIC friend SvStream& operator>>( SvStream& rIStream, Rectangle& rRect );
420 : TOOLS_DLLPUBLIC friend SvStream& operator<<( SvStream& rOStream, const Rectangle& rRect );
421 :
422 : // ONE
423 : long getX() const { return nLeft; }
424 : long getY() const { return nTop; }
425 : long getWidth() const { return nRight - nLeft; }
426 : long getHeight() const { return nBottom - nTop; }
427 : void setX( long n ) { nRight += n-nLeft; nLeft = n; }
428 : void setY( long n ) { nBottom += n-nTop; nTop = n; }
429 : void setWidth( long n ) { nRight = nLeft + n; }
430 : void setHeight( long n ) { nBottom = nTop + n; }
431 :
432 : private:
433 : long nLeft;
434 : long nTop;
435 : long nRight;
436 : long nBottom;
437 : };
438 :
439 1003 : inline Rectangle::Rectangle()
440 : {
441 1003 : nLeft = nTop = 0;
442 1003 : nRight = nBottom = RECT_EMPTY;
443 1003 : }
444 :
445 : inline Rectangle::Rectangle( const Point& rLT, const Point& rRB )
446 : {
447 : nLeft = rLT.X();
448 : nTop = rLT.Y();
449 : nRight = rRB.X();
450 : nBottom = rRB.Y();
451 : }
452 :
453 33 : inline Rectangle::Rectangle( long _nLeft, long _nTop,
454 : long _nRight, long _nBottom )
455 : {
456 33 : nLeft = _nLeft;
457 33 : nTop = _nTop;
458 33 : nRight = _nRight;
459 33 : nBottom = _nBottom;
460 33 : }
461 :
462 : inline Rectangle::Rectangle( const Point& rLT, const Size& rSize )
463 : {
464 : nLeft = rLT.X();
465 : nTop = rLT.Y();
466 : nRight = rSize.Width() ? nLeft+rSize.Width()-1 : RECT_EMPTY;
467 : nBottom = rSize.Height() ? nTop+rSize.Height()-1 : RECT_EMPTY;
468 : }
469 :
470 56599 : inline sal_Bool Rectangle::IsEmpty() const
471 : {
472 56599 : return ((nRight == RECT_EMPTY) || (nBottom == RECT_EMPTY));
473 : }
474 :
475 1024 : inline Point Rectangle::TopLeft() const
476 : {
477 1024 : return Point( nLeft, nTop );
478 : }
479 :
480 20 : inline Point Rectangle::TopRight() const
481 : {
482 20 : return Point( (nRight == RECT_EMPTY) ? nLeft : nRight, nTop );
483 : }
484 :
485 20 : inline Point Rectangle::BottomLeft() const
486 : {
487 20 : return Point( nLeft, (nBottom == RECT_EMPTY) ? nTop : nBottom );
488 : }
489 :
490 956 : inline Point Rectangle::BottomRight() const
491 : {
492 : return Point( (nRight == RECT_EMPTY) ? nLeft : nRight,
493 956 : (nBottom == RECT_EMPTY) ? nTop : nBottom );
494 : }
495 :
496 : inline Point Rectangle::TopCenter() const
497 : {
498 : if ( IsEmpty() )
499 : return Point( nLeft, nTop );
500 : else
501 : return Point( Min( nLeft, nRight ) + Abs( (nRight - nLeft)/2 ),
502 : Min( nTop, nBottom) );
503 : }
504 :
505 : inline Point Rectangle::BottomCenter() const
506 : {
507 : if ( IsEmpty() )
508 : return Point( nLeft, nTop );
509 : else
510 : return Point( Min( nLeft, nRight ) + Abs( (nRight - nLeft)/2 ),
511 : Max( nTop, nBottom) );
512 : }
513 :
514 : inline Point Rectangle::LeftCenter() const
515 : {
516 : if ( IsEmpty() )
517 : return Point( nLeft, nTop );
518 : else
519 : return Point( Min( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
520 : }
521 :
522 : inline Point Rectangle::RightCenter() const
523 : {
524 : if ( IsEmpty() )
525 : return Point( nLeft, nTop );
526 : else
527 : return Point( Max( nLeft, nRight ), nTop + (nBottom - nTop)/2 );
528 : }
529 :
530 0 : inline Point Rectangle::Center() const
531 : {
532 0 : if ( IsEmpty() )
533 0 : return Point( nLeft, nTop );
534 : else
535 0 : return Point( nLeft+(nRight-nLeft)/2 , nTop+(nBottom-nTop)/2 );
536 : }
537 :
538 : inline void Rectangle::Move( long nHorzMove, long nVertMove )
539 : {
540 : nLeft += nHorzMove;
541 : nTop += nVertMove;
542 : if ( nRight != RECT_EMPTY )
543 : nRight += nHorzMove;
544 : if ( nBottom != RECT_EMPTY )
545 : nBottom += nVertMove;
546 : }
547 :
548 : void Rectangle::Transpose()
549 : {
550 : if ( !IsEmpty() )
551 : {
552 : long swap( nLeft );
553 : nLeft = nTop;
554 : nTop = swap;
555 :
556 : swap = nRight;
557 : nRight = nBottom;
558 : nBottom = swap;
559 : }
560 : }
561 :
562 : inline void Rectangle::SetPos( const Point& rPoint )
563 : {
564 : if ( nRight != RECT_EMPTY )
565 : nRight += rPoint.X() - nLeft;
566 : if ( nBottom != RECT_EMPTY )
567 : nBottom += rPoint.Y() - nTop;
568 : nLeft = rPoint.X();
569 : nTop = rPoint.Y();
570 : }
571 :
572 35 : inline long Rectangle::GetWidth() const
573 : {
574 : long n;
575 35 : if ( nRight == RECT_EMPTY )
576 35 : n = 0;
577 : else
578 : {
579 0 : n = nRight - nLeft;
580 0 : if( n < 0 )
581 0 : n--;
582 : else
583 0 : n++;
584 : }
585 :
586 35 : return n;
587 : }
588 :
589 35 : inline long Rectangle::GetHeight() const
590 : {
591 : long n;
592 35 : if ( nBottom == RECT_EMPTY )
593 35 : n = 0;
594 : else
595 : {
596 0 : n = nBottom - nTop;
597 0 : if ( n < 0 )
598 0 : n--;
599 : else
600 0 : n++;
601 : }
602 :
603 35 : return n;
604 : }
605 :
606 0 : inline Size Rectangle::GetSize() const
607 : {
608 0 : return Size( GetWidth(), GetHeight() );
609 : }
610 :
611 : inline Rectangle Rectangle::GetUnion( const Rectangle& rRect ) const
612 : {
613 : Rectangle aTmpRect( *this );
614 : return aTmpRect.Union( rRect );
615 : }
616 :
617 1061 : inline Rectangle Rectangle::GetIntersection( const Rectangle& rRect ) const
618 : {
619 1061 : Rectangle aTmpRect( *this );
620 1061 : return aTmpRect.Intersection( rRect );
621 : }
622 :
623 : inline sal_Bool Rectangle::operator == ( const Rectangle& rRect ) const
624 : {
625 : return ((nLeft == rRect.nLeft ) &&
626 : (nTop == rRect.nTop ) &&
627 : (nRight == rRect.nRight ) &&
628 : (nBottom == rRect.nBottom ));
629 : }
630 :
631 0 : inline sal_Bool Rectangle::operator != ( const Rectangle& rRect ) const
632 : {
633 : return ((nLeft != rRect.nLeft ) ||
634 : (nTop != rRect.nTop ) ||
635 : (nRight != rRect.nRight ) ||
636 0 : (nBottom != rRect.nBottom ));
637 : }
638 :
639 : inline Rectangle& Rectangle::operator +=( const Point& rPt )
640 : {
641 : nLeft += rPt.X();
642 : nTop += rPt.Y();
643 : if ( nRight != RECT_EMPTY )
644 : nRight += rPt.X();
645 : if ( nBottom != RECT_EMPTY )
646 : nBottom += rPt.Y();
647 : return *this;
648 : }
649 :
650 : inline Rectangle& Rectangle::operator -= ( const Point& rPt )
651 : {
652 : nLeft -= rPt.X();
653 : nTop -= rPt.Y();
654 : if ( nRight != RECT_EMPTY )
655 : nRight -= rPt.X();
656 : if ( nBottom != RECT_EMPTY )
657 : nBottom -= rPt.Y();
658 : return *this;
659 : }
660 :
661 : inline Rectangle operator + ( const Rectangle& rRect, const Point& rPt )
662 : {
663 : Rectangle aRect( rRect.nLeft + rPt.X(), rRect.nTop + rPt.Y(),
664 : (rRect.nRight == RECT_EMPTY) ? RECT_EMPTY : rRect.nRight + rPt.X(),
665 : (rRect.nBottom == RECT_EMPTY) ? RECT_EMPTY : rRect.nBottom + rPt.Y() );
666 : return aRect;
667 : }
668 :
669 : inline Rectangle operator - ( const Rectangle& rRect, const Point& rPt )
670 : {
671 : Rectangle aRect( rRect.nLeft - rPt.X(),
672 : rRect.nTop - rPt.Y(),
673 : (rRect.nRight == RECT_EMPTY) ? RECT_EMPTY : rRect.nRight - rPt.X(),
674 : (rRect.nBottom == RECT_EMPTY) ? RECT_EMPTY : rRect.nBottom - rPt.Y() );
675 : return aRect;
676 : }
677 :
678 : template< typename charT, typename traits >
679 : inline std::basic_ostream<charT, traits> & operator <<(
680 : std::basic_ostream<charT, traits> & stream, const Rectangle& rectangle )
681 : {
682 : return stream << rectangle.getX() << ',' << rectangle.getY() << ' '
683 : << rectangle.getWidth() << 'x' << rectangle.getHeight();
684 : }
685 :
686 : #endif
687 :
688 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|