1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Defines a simple integer rectangle class. The containment semantics 6 // are array-like; that is, the coordinate (x, y) is considered to be 7 // contained by the rectangle, but the coordinate (x + width, y) is not. 8 // The class will happily let you create malformed rectangles (that is, 9 // rectangles with negative width and/or height), but there will be assertions 10 // in the operations (such as contain()) to complain in this case. 11 12 #ifndef BASE_GFX_RECT_H__ 13 #define BASE_GFX_RECT_H__ 14 15 #include <iosfwd> 16 17 #include "base/gfx/point.h" 18 #include "base/gfx/size.h" 19 20 #if defined(OS_WIN) 21 typedef struct tagRECT RECT; 22 #elif defined(USE_X11) 23 typedef struct _GdkRectangle GdkRectangle; 24 #endif 25 26 namespace gfx { 27 28 class Rect { 29 public: 30 Rect(); 31 Rect(int width, int height); 32 Rect(int x, int y, int width, int height); 33 #if defined(OS_WIN) 34 explicit Rect(const RECT& r); 35 #elif defined(OS_MACOSX) 36 explicit Rect(const CGRect& r); 37 #elif defined(USE_X11) 38 explicit Rect(const GdkRectangle& r); 39 #endif 40 Rect(const gfx::Point& origin, const gfx::Size& size); 41 ~Rect()42 ~Rect() {} 43 44 #if defined(OS_WIN) 45 Rect& operator=(const RECT& r); 46 #elif defined(OS_MACOSX) 47 Rect& operator=(const CGRect& r); 48 #elif defined(USE_X11) 49 Rect& operator=(const GdkRectangle& r); 50 #endif 51 x()52 int x() const { return origin_.x(); } set_x(int x)53 void set_x(int x) { origin_.set_x(x); } 54 y()55 int y() const { return origin_.y(); } set_y(int y)56 void set_y(int y) { origin_.set_y(y); } 57 width()58 int width() const { return size_.width(); } 59 void set_width(int width); 60 height()61 int height() const { return size_.height(); } 62 void set_height(int height); 63 origin()64 const gfx::Point& origin() const { return origin_; } set_origin(const gfx::Point & origin)65 void set_origin(const gfx::Point& origin) { origin_ = origin; } 66 size()67 const gfx::Size& size() const { return size_; } set_size(const gfx::Size & size)68 void set_size(const gfx::Size& size) { size_ = size; } 69 right()70 int right() const { return x() + width(); } bottom()71 int bottom() const { return y() + height(); } 72 73 void SetRect(int x, int y, int width, int height); 74 75 // Shrink the rectangle by a horizontal and vertical distance on all sides. Inset(int horizontal,int vertical)76 void Inset(int horizontal, int vertical) { 77 Inset(horizontal, vertical, horizontal, vertical); 78 } 79 80 // Shrink the rectangle by the specified amount on each side. 81 void Inset(int left, int top, int right, int bottom); 82 83 // Move the rectangle by a horizontal and vertical distance. 84 void Offset(int horizontal, int vertical); Offset(const gfx::Point & point)85 void Offset(const gfx::Point& point) { 86 Offset(point.x(), point.y()); 87 } 88 89 // Returns true if the area of the rectangle is zero. IsEmpty()90 bool IsEmpty() const { return size_.IsEmpty(); } 91 92 bool operator==(const Rect& other) const; 93 94 bool operator!=(const Rect& other) const { 95 return !(*this == other); 96 } 97 98 #if defined(OS_WIN) 99 // Construct an equivalent Win32 RECT object. 100 RECT ToRECT() const; 101 #elif defined(USE_X11) 102 GdkRectangle ToGdkRectangle() const; 103 #elif defined(OS_MACOSX) 104 // Construct an equivalent CoreGraphics object. 105 CGRect ToCGRect() const; 106 #endif 107 108 // Returns true if the point identified by point_x and point_y falls inside 109 // this rectangle. The point (x, y) is inside the rectangle, but the 110 // point (x + width, y + height) is not. 111 bool Contains(int point_x, int point_y) const; 112 113 // Returns true if the specified point is contained by this rectangle. Contains(const gfx::Point & point)114 bool Contains(const gfx::Point& point) const { 115 return Contains(point.x(), point.y()); 116 } 117 118 // Returns true if this rectangle contains the specified rectangle. 119 bool Contains(const Rect& rect) const; 120 121 // Returns true if this rectangle intersects the specified rectangle. 122 bool Intersects(const Rect& rect) const; 123 124 // Computes the intersection of this rectangle with the given rectangle. 125 Rect Intersect(const Rect& rect) const; 126 127 // Computes the union of this rectangle with the given rectangle. The union 128 // is the smallest rectangle containing both rectangles. 129 Rect Union(const Rect& rect) const; 130 131 // Computes the rectangle resulting from subtracting |rect| from |this|. If 132 // |rect| does not intersect completely in either the x- or y-direction, then 133 // |*this| is returned. If |rect| contains |this|, then an empty Rect is 134 // returned. 135 Rect Subtract(const Rect& rect) const; 136 137 // Returns true if this rectangle equals that of the supplied rectangle. Equals(const Rect & rect)138 bool Equals(const Rect& rect) const { 139 return *this == rect; 140 } 141 142 // Fits as much of the receiving rectangle into the supplied rectangle as 143 // possible, returning the result. For example, if the receiver had 144 // a x-location of 2 and a width of 4, and the supplied rectangle had 145 // an x-location of 0 with a width of 5, the returned rectangle would have 146 // an x-location of 1 with a width of 4. 147 Rect AdjustToFit(const Rect& rect) const; 148 149 // Returns the center of this rectangle. 150 Point CenterPoint() const; 151 152 // Returns true if this rectangle shares an entire edge (i.e., same width or 153 // same height) with the given rectangle, and the rectangles do not overlap. 154 bool SharesEdgeWith(const gfx::Rect& rect) const; 155 156 private: 157 gfx::Point origin_; 158 gfx::Size size_; 159 }; 160 161 } // namespace gfx 162 163 std::ostream& operator<<(std::ostream& out, const gfx::Rect& r); 164 165 #endif // BASE_GFX_RECT_H__ 166