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