OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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. |
(...skipping 16 matching lines...) Expand all Loading... |
27 #elif defined(OS_MACOSX) | 27 #elif defined(OS_MACOSX) |
28 typedef struct CGRect CGRect; | 28 typedef struct CGRect CGRect; |
29 #endif | 29 #endif |
30 | 30 |
31 namespace gfx { | 31 namespace gfx { |
32 | 32 |
33 class Insets; | 33 class Insets; |
34 | 34 |
35 class GFX_EXPORT Rect { | 35 class GFX_EXPORT Rect { |
36 public: | 36 public: |
37 Rect() {} | 37 constexpr Rect() = default; |
38 Rect(int width, int height) : size_(width, height) {} | 38 constexpr Rect(int width, int height) : size_(width, height) {} |
39 Rect(int x, int y, int width, int height) | 39 constexpr Rect(int x, int y, int width, int height) |
40 : origin_(x, y), size_(width, height) {} | 40 : origin_(x, y), size_(width, height) {} |
41 explicit Rect(const Size& size) : size_(size) {} | 41 constexpr explicit Rect(const Size& size) : size_(size) {} |
42 Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {} | 42 constexpr Rect(const Point& origin, const Size& size) |
| 43 : origin_(origin), size_(size) {} |
43 | 44 |
44 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
45 explicit Rect(const RECT& r); | 46 explicit Rect(const RECT& r); |
46 #elif defined(OS_MACOSX) | 47 #elif defined(OS_MACOSX) |
47 explicit Rect(const CGRect& r); | 48 explicit Rect(const CGRect& r); |
48 #endif | 49 #endif |
49 | 50 |
50 ~Rect() {} | |
51 | |
52 #if defined(OS_WIN) | 51 #if defined(OS_WIN) |
53 // Construct an equivalent Win32 RECT object. | 52 // Construct an equivalent Win32 RECT object. |
54 RECT ToRECT() const; | 53 RECT ToRECT() const; |
55 #elif defined(OS_MACOSX) | 54 #elif defined(OS_MACOSX) |
56 // Construct an equivalent CoreGraphics object. | 55 // Construct an equivalent CoreGraphics object. |
57 CGRect ToCGRect() const; | 56 CGRect ToCGRect() const; |
58 #endif | 57 #endif |
59 | 58 |
60 int x() const { return origin_.x(); } | 59 constexpr int x() const { return origin_.x(); } |
61 void set_x(int x) { origin_.set_x(x); } | 60 void set_x(int x) { origin_.set_x(x); } |
62 | 61 |
63 int y() const { return origin_.y(); } | 62 constexpr int y() const { return origin_.y(); } |
64 void set_y(int y) { origin_.set_y(y); } | 63 void set_y(int y) { origin_.set_y(y); } |
65 | 64 |
66 int width() const { return size_.width(); } | 65 constexpr int width() const { return size_.width(); } |
67 void set_width(int width) { size_.set_width(width); } | 66 void set_width(int width) { size_.set_width(width); } |
68 | 67 |
69 int height() const { return size_.height(); } | 68 constexpr int height() const { return size_.height(); } |
70 void set_height(int height) { size_.set_height(height); } | 69 void set_height(int height) { size_.set_height(height); } |
71 | 70 |
72 const Point& origin() const { return origin_; } | 71 constexpr const Point& origin() const { return origin_; } |
73 void set_origin(const Point& origin) { origin_ = origin; } | 72 void set_origin(const Point& origin) { origin_ = origin; } |
74 | 73 |
75 const Size& size() const { return size_; } | 74 constexpr const Size& size() const { return size_; } |
76 void set_size(const Size& size) { size_ = size; } | 75 void set_size(const Size& size) { size_ = size; } |
77 | 76 |
78 int right() const { return x() + width(); } | 77 constexpr int right() const { return x() + width(); } |
79 int bottom() const { return y() + height(); } | 78 constexpr int bottom() const { return y() + height(); } |
80 | 79 |
81 Point top_right() const { return Point(right(), y()); } | 80 constexpr Point top_right() const { return Point(right(), y()); } |
82 Point bottom_left() const { return Point(x(), bottom()); } | 81 constexpr Point bottom_left() const { return Point(x(), bottom()); } |
83 Point bottom_right() const { return Point(right(), bottom()); } | 82 constexpr Point bottom_right() const { return Point(right(), bottom()); } |
84 | 83 |
85 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); } | 84 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); } |
86 | 85 |
87 void SetRect(int x, int y, int width, int height) { | 86 void SetRect(int x, int y, int width, int height) { |
88 origin_.SetPoint(x, y); | 87 origin_.SetPoint(x, y); |
89 size_.SetSize(width, height); | 88 size_.SetSize(width, height); |
90 } | 89 } |
91 | 90 |
92 // Shrink the rectangle by a horizontal and vertical distance on all sides. | 91 // Shrink the rectangle by a horizontal and vertical distance on all sides. |
93 void Inset(int horizontal, int vertical) { | 92 void Inset(int horizontal, int vertical) { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 } | 270 } |
272 | 271 |
273 // This is declared here for use in gtest-based unit tests but is defined in | 272 // This is declared here for use in gtest-based unit tests but is defined in |
274 // the gfx_test_support target. Depend on that to use this in your unit test. | 273 // the gfx_test_support target. Depend on that to use this in your unit test. |
275 // This should not be used in production code - call ToString() instead. | 274 // This should not be used in production code - call ToString() instead. |
276 void PrintTo(const Rect& rect, ::std::ostream* os); | 275 void PrintTo(const Rect& rect, ::std::ostream* os); |
277 | 276 |
278 } // namespace gfx | 277 } // namespace gfx |
279 | 278 |
280 #endif // UI_GFX_GEOMETRY_RECT_H_ | 279 #endif // UI_GFX_GEOMETRY_RECT_H_ |
OLD | NEW |