Fl_Rectangle.H 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // "$Id: Rectangle.h 5454 2006-09-19 02:38:02Z spitzak $"
  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 to "[email protected]".
  21. #ifndef fltk_Rectangle_h
  22. #define fltk_Rectangle_h
  23. #include "Enumerations.H"
  24. // rectangle macros that help keeping rectangle predicates as strict as possible
  25. // even when not using rectangles in some situations (as when only using w h scalars)
  26. // so that there is only one strict defintion for common predicates,
  27. // if one change the following, it will be repercuted in all the core lib
  28. #define FLTK_RECT_EMPTY(w,h) (w <= 0 || h <= 0)
  29. // we should always use the same evaluation for center_x, center_y in all corelib code:
  30. //#define FLTK_CENTER_X(coord, length) (coord + (length>>1))
  31. //#define FLTK_CENTER_Y(coord, length) (coord + (length>>1))
  32. //namespace fltk {
  33. typedef int coord_t;
  34. class FL_EXPORT Fl_Rectangle {
  35. protected:
  36. coord_t x_, y_, w_, h_;
  37. public:
  38. /*! Left edge */
  39. coord_t x() const {return x_;}
  40. /*! Top edge */
  41. coord_t y() const {return y_;}
  42. /*! Distance between left and right edges */
  43. coord_t w() const {return w_;}
  44. /*! Distance between top and bottom edges */
  45. coord_t h() const {return h_;}
  46. /*! Return x()+w(), the right edge of the rectangle. */
  47. coord_t r() const {return x_+w_;}
  48. /*! Return y()+h(), the bottom edge of the rectangle. */
  49. coord_t b() const {return y_+h_;}
  50. /*! Move the rectangle so the left edge is at \a v. */
  51. void x(coord_t v) {x_ = v;}
  52. /*! Move the rectangle so the top edge is at \a v. */
  53. void y(coord_t v) {y_ = v;}
  54. /*! Change w() by moving the right edge. x() does not change. */
  55. void w(coord_t v) {w_ = v;}
  56. /*! Change h() by moving the bottom edge. y() does not change. */
  57. void h(coord_t v) {h_ = v;}
  58. /*! Change x() without changing r(), by changing the width. */
  59. void set_x(coord_t v) {w_ -= v-x_; x_ = v;}
  60. /*! Change y() without changing b(), by changing the height. */
  61. void set_y(coord_t v) {h_ -= v-y_; y_ = v;}
  62. /*! Change r() without changing x(), by changine the width. */
  63. void set_r(coord_t v) {w_ = v-x_;}
  64. /*! Change b() without changing y(), by changine the height. */
  65. void set_b(coord_t v) {h_ = v-y_;}
  66. /*! Set x(), y(), w(), and h() all at once. */
  67. void set(coord_t x, coord_t y, coord_t w, coord_t h) {x_=x; y_=y; w_=w; h_=h;}
  68. /*! Sets x, y, w, h so that's it's centered or aligned (if flags!=0) inside the source r */
  69. void set (const Fl_Rectangle& r, coord_t w, coord_t h, coord_t flags = 0);
  70. /*! Add \a d to x() without changing r() (it reduces w() by \a d). */
  71. void move_x(coord_t d) {x_ += d; w_ -= d;}
  72. /*! Add \a d to y() without changing b() (it reduces h() by \a d). */
  73. void move_y(coord_t d) {y_ += d; h_ -= d;}
  74. /*! Add \a d to r() and w(). */
  75. void move_r(coord_t d) {w_ += d;}
  76. /*! Add \a d to b() and h(). */
  77. void move_b(coord_t d) {h_ += d;}
  78. /*! Move all edges in by \a d. See also Symbol::inset() */
  79. void inset(coord_t d) {x_ += d; y_ += d; w_ -= 2*d; h_ -= 2*d;}
  80. /*! Move entire rectangle by given distance in x and y. */
  81. void move(coord_t dx, coord_t dy) {x_ += dx; y_ += dy;}
  82. /*! True if w() or h() are less or equal to zero. */
  83. bool empty() const {return FLTK_RECT_EMPTY(w_, h_);}
  84. /*! Same as !empty(), true if w() and h() are both greater than zero. */
  85. bool not_empty() const {return !FLTK_RECT_EMPTY(w_, h_);}
  86. /*! Integer center position. Rounded to the left if w() is odd. */
  87. coord_t center_x() const {return x_+(w_/2);}
  88. /*! Integer center position. Rounded to lower y if h() is odd. */
  89. coord_t center_y() const {return y_+(h_/2);}
  90. /*! Where to put baseline to center current font nicely */
  91. coord_t baseline_y() const;
  92. Fl_Rectangle() {x_ = y_ = w_ = h_ = 0;}
  93. /*! Constructor that sets x(), y(), w(), and h(). */
  94. Fl_Rectangle(coord_t x, coord_t y, coord_t w, coord_t h) : x_(x), y_(y), w_(w), h_(h) {}
  95. /*! Constructor that sets x() and y() to zero, and sets w() and h(). */
  96. Fl_Rectangle(coord_t w, coord_t h) : x_(0), y_(0), w_(w), h_(h) {}
  97. /*! Copy constructor. */
  98. Fl_Rectangle(const Fl_Rectangle& r) : x_(r.x_),y_(r.y_),w_(r.w_),h_(r.h_) {}
  99. /*! Constructor that calls set(). */
  100. Fl_Rectangle(const Fl_Rectangle& r, coord_t w, coord_t h, coord_t flags = 0) {set(r,w,h,flags);}
  101. /*! True if rectangle contains the pixel who's upper-left corner is at x,y */
  102. bool contains(coord_t x, coord_t y) const {return x>=x_ && y>=y_ && x<x_+w_ && y<y_+h_;}
  103. void merge(const Fl_Rectangle& r);
  104. void intersect(const Fl_Rectangle& r);
  105. };
  106. //}
  107. #endif