license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 "base/gfx/size.h" |
| 16 | #include "base/gfx/point.h" |
| 17 | |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 18 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | typedef struct tagRECT RECT; |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 20 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | |
| 22 | namespace gfx { |
| 23 | |
| 24 | class Rect { |
| 25 | public: |
| 26 | Rect(); |
| 27 | Rect(int width, int height); |
| 28 | Rect(int x, int y, int width, int height); |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 29 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 30 | explicit Rect(const RECT& r); |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 31 | #elif defined(OS_MACOSX) |
| 32 | explicit Rect(const CGRect& r); |
| 33 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | Rect(const gfx::Point& origin, const gfx::Size& size); |
| 35 | |
| 36 | ~Rect() {} |
| 37 | |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 38 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 39 | Rect& operator=(const RECT& r); |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 40 | #elif defined(OS_MACOSX) |
| 41 | Rect& operator=(const CGRect& r); |
| 42 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | |
| 44 | int x() const { return origin_.x(); } |
| 45 | void set_x(int x) { origin_.set_x(x); } |
| 46 | |
| 47 | int y() const { return origin_.y(); } |
| 48 | void set_y(int y) { origin_.set_y(y); } |
| 49 | |
| 50 | int width() const { return size_.width(); } |
| 51 | void set_width(int width); |
| 52 | |
| 53 | int height() const { return size_.height(); } |
| 54 | void set_height(int height); |
| 55 | |
| 56 | const gfx::Point& origin() const { return origin_; } |
| 57 | void set_origin(const gfx::Point& origin) { origin_ = origin; } |
| 58 | |
| 59 | const gfx::Size& size() const { return size_; } |
| 60 | |
| 61 | int right() const { return x() + width(); } |
| 62 | int bottom() const { return y() + height(); } |
| 63 | |
| 64 | void SetRect(int x, int y, int width, int height); |
| 65 | |
| 66 | // Shrink the rectangle by a horizontal and vertical distance on all sides. |
| 67 | void Inset(int horizontal, int vertical); |
| 68 | |
| 69 | // Move the rectangle by a horizontal and vertical distance. |
| 70 | void Offset(int horizontal, int vertical); |
| 71 | |
| 72 | // Returns true if the area of the rectangle is zero. |
| 73 | bool IsEmpty() const; |
| 74 | |
| 75 | bool operator==(const Rect& other) const; |
| 76 | |
| 77 | bool operator!=(const Rect& other) const { |
| 78 | return !(*this == other); |
| 79 | } |
| 80 | |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 81 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 82 | // Construct an equivalent Win32 RECT object. |
| 83 | RECT ToRECT() const; |
avi@google.com | 8227724 | 2008-08-07 05:38:29 +0900 | [diff] [blame] | 84 | #elif defined(OS_MACOSX) |
| 85 | // Construct an equivalent CoreGraphics object. |
| 86 | CGRect ToCGRect() const; |
| 87 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | |
| 89 | // Returns true if the point identified by point_x and point_y falls inside |
| 90 | // this rectangle. The point (x, y) is inside the rectangle, but the |
| 91 | // point (x + width, y + height) is not. |
| 92 | bool Contains(int point_x, int point_y) const; |
| 93 | |
| 94 | // Returns true if this rectangle contains the specified rectangle. |
| 95 | bool Contains(const Rect& rect) const; |
| 96 | |
| 97 | // Returns true if this rectangle intersects the specified rectangle. |
| 98 | bool Intersects(const Rect& rect) const; |
| 99 | |
| 100 | // Computes the intersection of this rectangle with the given rectangle. |
| 101 | Rect Intersect(const Rect& rect) const; |
| 102 | |
| 103 | // Computes the union of this rectangle with the given rectangle. The union |
| 104 | // is the smallest rectangle containing both rectangles. |
| 105 | Rect Union(const Rect& rect) const; |
| 106 | |
| 107 | // Computes the rectangle resulting from subtracting |rect| from |this|. If |
| 108 | // |rect| does not intersect completely in either the x- or y-direction, then |
| 109 | // |*this| is returned. If |rect| contains |this|, then an empty Rect is |
| 110 | // returned. |
| 111 | Rect Subtract(const Rect& rect) const; |
| 112 | |
| 113 | // Returns true if this rectangle equals that of the supplied rectangle. |
| 114 | bool Equals(const Rect& rect) const { |
| 115 | return *this == rect; |
| 116 | } |
| 117 | |
| 118 | // Fits as much of the receiving rectangle into the supplied rectangle as |
| 119 | // possible, returning the result. For example, if the receiver had |
| 120 | // a x-location of 2 and a width of 4, and the supplied rectangle had |
| 121 | // an x-location of 0 with a width of 5, the returned rectangle would have |
| 122 | // an x-location of 1 with a width of 4. |
| 123 | Rect AdjustToFit(const Rect& rect) const; |
| 124 | |
| 125 | // Returns the center of this rectangle. |
| 126 | Point CenterPoint() const; |
| 127 | |
| 128 | private: |
| 129 | gfx::Point origin_; |
| 130 | gfx::Size size_; |
| 131 | }; |
| 132 | |
| 133 | } // namespace gfx |
| 134 | |
| 135 | #ifdef UNIT_TEST |
| 136 | |
| 137 | inline std::ostream& operator<<(std::ostream& out, const gfx::Rect& r) { |
| 138 | return out << r.origin() << " " << r.size(); |
| 139 | } |
| 140 | |
| 141 | #endif // #ifdef UNIT_TEST |
| 142 | |
| 143 | #endif // BASE_GFX_RECT_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 144 | |