Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 typedef struct tagRECT RECT; | 26 typedef struct tagRECT RECT; |
| 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 #define GetClampedValue(origin, size) \ | |
|
danakj
2016/08/25 17:59:21
can this be a constexpr function instead, in an in
sunxd
2016/08/25 19:25:09
Acknowledged.
| |
| 36 origin > 0 && std::numeric_limits<int>::max() - origin < size \ | |
| 37 ? std::numeric_limits<int>::max() - origin \ | |
| 38 : size | |
| 39 | |
| 35 class GFX_EXPORT Rect { | 40 class GFX_EXPORT Rect { |
| 36 public: | 41 public: |
| 37 constexpr Rect() = default; | 42 constexpr Rect() = default; |
| 38 constexpr Rect(int width, int height) : size_(width, height) {} | 43 constexpr Rect(int width, int height) : size_(width, height) {} |
| 39 constexpr Rect(int x, int y, int width, int height) | 44 constexpr Rect(int x, int y, int width, int height) |
| 40 : origin_(x, y), size_(width, height) {} | 45 : origin_(x, y), |
| 46 size_(GetClampedValue(x, width), GetClampedValue(y, height)) {} | |
| 41 constexpr explicit Rect(const Size& size) : size_(size) {} | 47 constexpr explicit Rect(const Size& size) : size_(size) {} |
| 42 constexpr Rect(const Point& origin, const Size& size) | 48 constexpr Rect(const Point& origin, const Size& size) |
| 43 : origin_(origin), size_(size) {} | 49 : origin_(origin), |
| 50 size_(GetClampedValue(origin.x(), size.width()), | |
| 51 GetClampedValue(origin.y(), size.height())) {} | |
| 44 | 52 |
| 45 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 46 explicit Rect(const RECT& r); | 54 explicit Rect(const RECT& r); |
| 47 #elif defined(OS_MACOSX) | 55 #elif defined(OS_MACOSX) |
| 48 explicit Rect(const CGRect& r); | 56 explicit Rect(const CGRect& r); |
| 49 #endif | 57 #endif |
| 50 | 58 |
| 51 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
| 52 // Construct an equivalent Win32 RECT object. | 60 // Construct an equivalent Win32 RECT object. |
| 53 RECT ToRECT() const; | 61 RECT ToRECT() const; |
| 54 #elif defined(OS_MACOSX) | 62 #elif defined(OS_MACOSX) |
| 55 // Construct an equivalent CoreGraphics object. | 63 // Construct an equivalent CoreGraphics object. |
| 56 CGRect ToCGRect() const; | 64 CGRect ToCGRect() const; |
| 57 #endif | 65 #endif |
| 58 | 66 |
| 59 constexpr int x() const { return origin_.x(); } | 67 constexpr int x() const { return origin_.x(); } |
| 60 void set_x(int x) { origin_.set_x(x); } | 68 void set_x(int x) { origin_.set_x(x); } |
| 61 | 69 |
| 62 constexpr int y() const { return origin_.y(); } | 70 constexpr int y() const { return origin_.y(); } |
| 63 void set_y(int y) { origin_.set_y(y); } | 71 void set_y(int y) { origin_.set_y(y); } |
| 64 | 72 |
| 65 constexpr int width() const { return size_.width(); } | 73 constexpr int width() const { return size_.width(); } |
| 66 void set_width(int width) { size_.set_width(width); } | 74 void set_width(int width) { size_.set_width(width); } |
|
danakj
2016/08/25 17:59:21
How about the non-const methods in this class that
sunxd
2016/08/25 19:25:09
I'm afraid that clamping values in these functions
| |
| 67 | 75 |
| 68 constexpr int height() const { return size_.height(); } | 76 constexpr int height() const { return size_.height(); } |
| 69 void set_height(int height) { size_.set_height(height); } | 77 void set_height(int height) { size_.set_height(height); } |
| 70 | 78 |
| 71 constexpr const Point& origin() const { return origin_; } | 79 constexpr const Point& origin() const { return origin_; } |
| 72 void set_origin(const Point& origin) { origin_ = origin; } | 80 void set_origin(const Point& origin) { origin_ = origin; } |
| 73 | 81 |
| 74 constexpr const Size& size() const { return size_; } | 82 constexpr const Size& size() const { return size_; } |
| 75 void set_size(const Size& size) { size_ = size; } | 83 void set_size(const Size& size) { size_ = size; } |
| 76 | 84 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 } | 278 } |
| 271 | 279 |
| 272 // This is declared here for use in gtest-based unit tests but is defined in | 280 // This is declared here for use in gtest-based unit tests but is defined in |
| 273 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 281 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
| 274 // test. This should not be used in production code - call ToString() instead. | 282 // test. This should not be used in production code - call ToString() instead. |
| 275 void PrintTo(const Rect& rect, ::std::ostream* os); | 283 void PrintTo(const Rect& rect, ::std::ostream* os); |
| 276 | 284 |
| 277 } // namespace gfx | 285 } // namespace gfx |
| 278 | 286 |
| 279 #endif // UI_GFX_GEOMETRY_RECT_H_ | 287 #endif // UI_GFX_GEOMETRY_RECT_H_ |
| OLD | NEW |