You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
5.2KB

  1. // "$Id: Fl_Rectangle.h 8500 2011-03-03 09:20:46Z bgbnbigben $"
  2. //
  3. // Copyright 1998-2006 by Bill Spitzak and others.
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Library General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Library General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Library General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. // USA.
  19. //
  20. // Please report all bugs and problems on the following page:
  21. //
  22. // http://www.fltk.org/str.php
  23. #ifndef fltk_Fl_Rectangle_h
  24. #define fltk_Fl_Rectangle_h
  25. // rectangle macros that help keeping rectangle predicates as strict as possible
  26. // even when not using rectangles in some situations (as when only using w h scalars)
  27. // so that there is only one strict defintion for common predicates,
  28. // if one change the following, it will be repercuted in all the core lib
  29. #define FLTK_RECT_EMPTY(w,h) (w <= 0 || h <= 0)
  30. // we should always use the same evaluation for center_x, center_y in all corelib code:
  31. //#define FLTK_CENTER_X(coord, length) (coord + (length>>1))
  32. //#define FLTK_CENTER_Y(coord, length) (coord + (length>>1))
  33. class Fl_Rectangle {
  34. int x_, y_, w_, h_;
  35. public:
  36. /*! Left edge */
  37. int x() const {return x_;}
  38. /*! Top edge */
  39. int y() const {return y_;}
  40. /*! Distance between left and right edges */
  41. int w() const {return w_;}
  42. /*! Distance between top and bottom edges */
  43. int h() const {return h_;}
  44. /*! Return x()+w(), the right edge of the rectangle. */
  45. int r() const {return x_+w_;}
  46. /*! Return y()+h(), the bottom edge of the rectangle. */
  47. int b() const {return y_+h_;}
  48. /*! Move the rectangle so the left edge is at \a v. */
  49. void x(int v) {x_ = v;}
  50. /*! Move the rectangle so the top edge is at \a v. */
  51. void y(int v) {y_ = v;}
  52. /*! Change w() by moving the right edge. x() does not change. */
  53. void w(int v) {w_ = v;}
  54. /*! Change h() by moving the bottom edge. y() does not change. */
  55. void h(int v) {h_ = v;}
  56. /*! Change x() without changing r(), by changing the width. */
  57. void set_x(int v) {w_ -= v-x_; x_ = v;}
  58. /*! Change y() without changing b(), by changing the height. */
  59. void set_y(int v) {h_ -= v-y_; y_ = v;}
  60. /*! Change r() without changing x(), by changine the width. */
  61. void set_r(int v) {w_ = v-x_;}
  62. /*! Change b() without changing y(), by changine the height. */
  63. void set_b(int v) {h_ = v-y_;}
  64. /*! Set x(), y(), w(), and h() all at once. */
  65. void set(int x, int y, int w, int h) {x_=x; y_=y; w_=w; h_=h;}
  66. /*! Sets x, y, w, h so that's it's centered or aligned (if flags!=0) inside the source r */
  67. void set (const Fl_Rectangle& r, int w, int h, int flags = 0);
  68. /*! Add \a d to x() without changing r() (it reduces w() by \a d). */
  69. void move_x(int d) {x_ += d; w_ -= d;}
  70. /*! Add \a d to y() without changing b() (it reduces h() by \a d). */
  71. void move_y(int d) {y_ += d; h_ -= d;}
  72. /*! Add \a d to r() and w(). */
  73. void move_r(int d) {w_ += d;}
  74. /*! Add \a d to b() and h(). */
  75. void move_b(int d) {h_ += d;}
  76. /*! Move all edges in by \a d. See also Symbol::inset() */
  77. void inset(int d) {x_ += d; y_ += d; w_ -= 2*d; h_ -= 2*d;}
  78. /*! Move entire rectangle by given distance in x and y. */
  79. void move(int dx, int dy) {x_ += dx; y_ += dy;}
  80. /*! True if w() or h() are less or equal to zero. */
  81. bool empty() const {return FLTK_RECT_EMPTY(w_, h_);}
  82. /*! Same as !empty(), true if w() and h() are both greater than zero. */
  83. bool not_empty() const {return !FLTK_RECT_EMPTY(w_, h_);}
  84. /*! Integer center position. Rounded to the left if w() is odd. */
  85. int center_x() const {return x_+(w_>>1);}
  86. /*! Integer center position. Rounded to lower y if h() is odd. */
  87. int center_y() const {return y_+(h_>>1);}
  88. /*! Where to put baseline to center current font nicely */
  89. int baseline_y() const;
  90. Fl_Rectangle() {}
  91. /*! Constructor that sets x(), y(), w(), and h(). */
  92. Fl_Rectangle(int x, int y, int w, int h) : x_(x), y_(y), w_(w), h_(h) {}
  93. /*! Constructor that sets x() and y() to zero, and sets w() and h(). */
  94. Fl_Rectangle(int w, int h) : x_(0), y_(0), w_(w), h_(h) {}
  95. /*! Copy constructor. */
  96. Fl_Rectangle(const Fl_Rectangle& r) : x_(r.x_),y_(r.y_),w_(r.w_),h_(r.h_) {}
  97. /*! Constructor that calls set(). */
  98. Fl_Rectangle(const Fl_Rectangle& r, int w, int h, int flags = 0) {set(r,w,h,flags);}
  99. /*! True if rectangle contains the pixel who's upper-left corner is at x,y */
  100. bool contains(int x, int y) const {return x>=x_ && y>=y_ && x<x_+w_ && y<y_+h_;}
  101. /* returns true if the rectangles intersect at all */
  102. bool intersects ( const Fl_Rectangle &r ) const
  103. {
  104. Fl_Rectangle r2( r );
  105. r2.intersect( *this );
  106. return ! r2.empty();
  107. }
  108. void merge(const Fl_Rectangle& r);
  109. void intersect(const Fl_Rectangle& r);
  110. };
  111. #endif