home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qrect.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  7KB  |  254 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qrect.h   3.0.0   edited Jun 22 12:24 $
  3. **
  4. ** Definition of QRect class
  5. **
  6. ** Created : 931028
  7. **
  8. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the kernel module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
  22. ** licenses may use this file in accordance with the Qt Commercial License
  23. ** Agreement provided with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #ifndef QRECT_H
  39. #define QRECT_H
  40.  
  41. #ifndef QT_H
  42. #include "qsize.h"
  43. #endif // QT_H
  44.  
  45. #if defined(topLeft)
  46. #error "Macro definition of topLeft conflicts with QRect"
  47. // don't just silently undo people's defines: #undef topLeft
  48. #endif
  49.  
  50. class Q_EXPORT QRect                    // rectangle class
  51. {
  52. public:
  53.     QRect()    { x1 = y1 = 0; x2 = y2 = -1; }
  54.     QRect( const QPoint &topleft, const QPoint &bottomright );
  55.     QRect( const QPoint &topleft, const QSize &size );
  56.     QRect( int left, int top, int width, int height );
  57.  
  58.     bool   isNull()    const;
  59.     bool   isEmpty()    const;
  60.     bool   isValid()    const;
  61.     QRect  normalize()    const;
  62.  
  63.     int       left()    const;
  64.     int       top()    const;
  65.     int       right()    const;
  66.     int       bottom()    const;
  67.  
  68.     QCOORD &rLeft();
  69.     QCOORD &rTop();
  70.     QCOORD &rRight();
  71.     QCOORD &rBottom();
  72.     
  73.     int       x()        const;
  74.     int       y()        const;
  75.     void   setLeft( int pos );
  76.     void   setTop( int pos );
  77.     void   setRight( int pos );
  78.     void   setBottom( int pos );
  79.     void   setX( int x );
  80.     void   setY( int y );
  81.  
  82.     QPoint topLeft()     const;
  83.     QPoint bottomRight() const;
  84.     QPoint topRight()     const;
  85.     QPoint bottomLeft()     const;
  86.     QPoint center()     const;
  87.  
  88.     void   rect( int *x, int *y, int *w, int *h ) const;
  89.     void   coords( int *x1, int *y1, int *x2, int *y2 ) const;
  90.  
  91.     void   moveTopLeft( const QPoint &p );
  92.     void   moveBottomRight( const QPoint &p );
  93.     void   moveTopRight( const QPoint &p );
  94.     void   moveBottomLeft( const QPoint &p );
  95.     void   moveCenter( const QPoint &p );
  96.     void   moveBy( int dx, int dy );
  97.  
  98.     void   setRect( int x, int y, int w, int h );
  99.     void   setCoords( int x1, int y1, int x2, int y2 );
  100.     void   addCoords( int x1, int y1, int x2, int y2 );
  101.     
  102.     QSize  size()    const;
  103.     int       width()    const;
  104.     int       height()    const;
  105.     void   setWidth( int w );
  106.     void   setHeight( int h );
  107.     void   setSize( const QSize &s );
  108.  
  109.     QRect  operator|(const QRect &r) const;
  110.     QRect  operator&(const QRect &r) const;
  111.     QRect&  operator|=(const QRect &r);
  112.     QRect&  operator&=(const QRect &r);
  113.  
  114.     bool   contains( const QPoint &p, bool proper=FALSE ) const;
  115.     bool   contains( int x, int y, bool proper=FALSE ) const;
  116.     bool   contains( const QRect &r, bool proper=FALSE ) const;
  117.     QRect  unite( const QRect &r ) const;
  118.     QRect  intersect( const QRect &r ) const;
  119.     bool   intersects( const QRect &r ) const;
  120.  
  121.     friend Q_EXPORT bool operator==( const QRect &, const QRect & );
  122.     friend Q_EXPORT bool operator!=( const QRect &, const QRect & );
  123.  
  124. private:
  125. #if defined(Q_OS_MAC)
  126.     QCOORD y1;
  127.     QCOORD x1;
  128.     QCOORD y2;
  129.     QCOORD x2;
  130. #else
  131.     QCOORD x1;
  132.     QCOORD y1;
  133.     QCOORD x2;
  134.     QCOORD y2;
  135. #endif
  136. };
  137.  
  138. Q_EXPORT bool operator==( const QRect &, const QRect & );
  139. Q_EXPORT bool operator!=( const QRect &, const QRect & );
  140.  
  141.  
  142. /*****************************************************************************
  143.   QRect stream functions
  144.  *****************************************************************************/
  145. #ifndef QT_NO_DATASTREAM
  146. Q_EXPORT QDataStream &operator<<( QDataStream &, const QRect & );
  147. Q_EXPORT QDataStream &operator>>( QDataStream &, QRect & );
  148. #endif
  149.  
  150. /*****************************************************************************
  151.   QRect inline member functions
  152.  *****************************************************************************/
  153.  
  154. inline QRect::QRect( int left, int top, int width, int height )
  155. {
  156.     x1 = (QCOORD)left;
  157.     y1 = (QCOORD)top;
  158.     x2 = (QCOORD)(left+width-1);
  159.     y2 = (QCOORD)(top+height-1);
  160. }
  161.  
  162. inline bool QRect::isNull() const
  163. { return x2 == x1-1 && y2 == y1-1; }
  164.  
  165. inline bool QRect::isEmpty() const
  166. { return x1 > x2 || y1 > y2; }
  167.  
  168. inline bool QRect::isValid() const
  169. { return x1 <= x2 && y1 <= y2; }
  170.  
  171. inline int QRect::left() const
  172. { return x1; }
  173.  
  174. inline int QRect::top() const
  175. { return y1; }
  176.  
  177. inline int QRect::right() const
  178. { return x2; }
  179.  
  180. inline int QRect::bottom() const
  181. { return y2; }
  182.  
  183. inline QCOORD &QRect::rLeft()
  184. { return x1; }
  185.  
  186. inline QCOORD & QRect::rTop()
  187. { return y1; }
  188.  
  189. inline QCOORD & QRect::rRight()
  190. { return x2; }
  191.  
  192. inline QCOORD & QRect::rBottom()
  193. { return y2; }
  194.  
  195. inline int QRect::x() const
  196. { return x1; }
  197.  
  198. inline int QRect::y() const
  199. { return y1; }
  200.  
  201. inline void QRect::setLeft( int pos )
  202. { x1 = (QCOORD)pos; }
  203.  
  204. inline void QRect::setTop( int pos )
  205. { y1 = (QCOORD)pos; }
  206.  
  207. inline void QRect::setRight( int pos )
  208. { x2 = (QCOORD)pos; }
  209.  
  210. inline void QRect::setBottom( int pos )
  211. { y2 = (QCOORD)pos; }
  212.  
  213. inline void QRect::setX( int x )
  214. { x1 = (QCOORD)x; }
  215.  
  216. inline void QRect::setY( int y )
  217. { y1 = (QCOORD)y; }
  218.  
  219. inline QPoint QRect::topLeft() const
  220. { return QPoint(x1, y1); }
  221.  
  222. inline QPoint QRect::bottomRight() const
  223. { return QPoint(x2, y2); }
  224.  
  225. inline QPoint QRect::topRight() const
  226. { return QPoint(x2, y1); }
  227.  
  228. inline QPoint QRect::bottomLeft() const
  229. { return QPoint(x1, y2); }
  230.  
  231. inline QPoint QRect::center() const
  232. { return QPoint((x1+x2)/2, (y1+y2)/2); }
  233.  
  234. inline int QRect::width() const
  235. { return  x2 - x1 + 1; }
  236.  
  237. inline int QRect::height() const
  238. { return  y2 - y1 + 1; }
  239.  
  240. inline QSize QRect::size() const
  241. { return QSize(x2-x1+1, y2-y1+1); }
  242.  
  243. inline bool QRect::contains( int x, int y, bool proper ) const
  244. {
  245.     if ( proper )
  246.         return x > x1 && x < x2 &&
  247.                y > y1 && y < y2;
  248.     else
  249.         return x >= x1 && x <= x2 &&
  250.                y >= y1 && y <= y2;
  251. }
  252.  
  253. #endif // QRECT_H
  254.