Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ui/gfx/geometry/rect.h

Issue 506803004: gfx: Don't template gfx::Rect and gfx::RectF (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: inlines: rm-rectbase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/geometry/BUILD.gn ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
11 11
12 #ifndef UI_GFX_GEOMETRY_RECT_H_ 12 #ifndef UI_GFX_GEOMETRY_RECT_H_
13 #define UI_GFX_GEOMETRY_RECT_H_ 13 #define UI_GFX_GEOMETRY_RECT_H_
14 14
15 #include <cmath> 15 #include <cmath>
16 #include <iosfwd> 16 #include <iosfwd>
17 #include <string> 17 #include <string>
18 18
19 #include "ui/gfx/geometry/point.h" 19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/rect_base.h"
21 #include "ui/gfx/geometry/rect_f.h" 20 #include "ui/gfx/geometry/rect_f.h"
22 #include "ui/gfx/geometry/size.h" 21 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/geometry/vector2d.h" 22 #include "ui/gfx/geometry/vector2d.h"
24 23
25 #if defined(OS_WIN) 24 #if defined(OS_WIN)
26 typedef struct tagRECT RECT; 25 typedef struct tagRECT RECT;
27 #elif defined(OS_IOS) 26 #elif defined(OS_IOS)
28 #include <CoreGraphics/CoreGraphics.h> 27 #include <CoreGraphics/CoreGraphics.h>
29 #elif defined(OS_MACOSX) 28 #elif defined(OS_MACOSX)
30 #include <ApplicationServices/ApplicationServices.h> 29 #include <ApplicationServices/ApplicationServices.h>
31 #endif 30 #endif
32 31
33 namespace gfx { 32 namespace gfx {
34 33
35 class Insets; 34 class Insets;
36 35
37 class GFX_EXPORT Rect 36 class GFX_EXPORT Rect {
38 : public RectBase<Rect, Point, Size, Insets, Vector2d, int> {
39 public: 37 public:
40 Rect() : RectBase<Rect, Point, Size, Insets, Vector2d, int>(Point()) {} 38 Rect() {}
41 39 Rect(int width, int height) : size_(width, height) {}
42 Rect(int width, int height)
43 : RectBase<Rect, Point, Size, Insets, Vector2d, int>
44 (Size(width, height)) {}
45
46 Rect(int x, int y, int width, int height) 40 Rect(int x, int y, int width, int height)
47 : RectBase<Rect, Point, Size, Insets, Vector2d, int> 41 : origin_(x, y), size_(width, height) {}
48 (Point(x, y), Size(width, height)) {} 42 explicit Rect(const Size& size) : size_(size) {}
43 Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
49 44
50 #if defined(OS_WIN) 45 #if defined(OS_WIN)
51 explicit Rect(const RECT& r); 46 explicit Rect(const RECT& r);
52 #elif defined(OS_MACOSX) 47 #elif defined(OS_MACOSX)
53 explicit Rect(const CGRect& r); 48 explicit Rect(const CGRect& r);
54 #endif 49 #endif
55 50
56 explicit Rect(const gfx::Size& size)
57 : RectBase<Rect, Point, Size, Insets, Vector2d, int>(size) {}
58
59 Rect(const gfx::Point& origin, const gfx::Size& size)
60 : RectBase<Rect, Point, Size, Insets, Vector2d, int>(origin, size) {}
61
62 ~Rect() {} 51 ~Rect() {}
63 52
64 #if defined(OS_WIN) 53 #if defined(OS_WIN)
65 // Construct an equivalent Win32 RECT object. 54 // Construct an equivalent Win32 RECT object.
66 RECT ToRECT() const; 55 RECT ToRECT() const;
67 #elif defined(OS_MACOSX) 56 #elif defined(OS_MACOSX)
68 // Construct an equivalent CoreGraphics object. 57 // Construct an equivalent CoreGraphics object.
69 CGRect ToCGRect() const; 58 CGRect ToCGRect() const;
70 #endif 59 #endif
71 60
72 operator RectF() const { 61 operator RectF() const {
73 return RectF(origin().x(), origin().y(), size().width(), size().height()); 62 return RectF(origin().x(), origin().y(), size().width(), size().height());
74 } 63 }
75 64
65 int x() const { return origin_.x(); }
66 void set_x(int x) { origin_.set_x(x); }
67
68 int y() const { return origin_.y(); }
69 void set_y(int y) { origin_.set_y(y); }
70
71 int width() const { return size_.width(); }
72 void set_width(int width) { size_.set_width(width); }
73
74 int height() const { return size_.height(); }
75 void set_height(int height) { size_.set_height(height); }
76
77 const Point& origin() const { return origin_; }
78 void set_origin(const Point& origin) { origin_ = origin; }
79
80 const Size& size() const { return size_; }
81 void set_size(const Size& size) { size_ = size; }
82
83 int right() const { return x() + width(); }
84 int bottom() const { return y() + height(); }
85
86 Point top_right() const { return Point(right(), y()); }
87 Point bottom_left() const { return Point(x(), bottom()); }
88 Point bottom_right() const { return Point(right(), bottom()); }
89
90 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
91
92 void SetRect(int x, int y, int width, int height) {
93 origin_.SetPoint(x, y);
94 size_.SetSize(width, height);
95 }
96
97 // Shrink the rectangle by a horizontal and vertical distance on all sides.
98 void Inset(int horizontal, int vertical) {
99 Inset(horizontal, vertical, horizontal, vertical);
100 }
101
102 // Shrink the rectangle by the given insets.
103 void Inset(const Insets& insets);
104
105 // Shrink the rectangle by the specified amount on each side.
106 void Inset(int left, int top, int right, int bottom);
107
108 // Move the rectangle by a horizontal and vertical distance.
109 void Offset(int horizontal, int vertical);
110 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
111 void operator+=(const Vector2d& offset);
112 void operator-=(const Vector2d& offset);
113
114 Insets InsetsFrom(const Rect& inner) const;
115
116 // Returns true if the area of the rectangle is zero.
117 bool IsEmpty() const { return size_.IsEmpty(); }
118
119 // A rect is less than another rect if its origin is less than
120 // the other rect's origin. If the origins are equal, then the
121 // shortest rect is less than the other. If the origin and the
122 // height are equal, then the narrowest rect is less than.
123 // This comparison is required to use Rects in sets, or sorted
124 // vectors.
125 bool operator<(const Rect& other) const;
126
127 // Returns true if the point identified by point_x and point_y falls inside
128 // this rectangle. The point (x, y) is inside the rectangle, but the
129 // point (x + width, y + height) is not.
130 bool Contains(int point_x, int point_y) const;
131
132 // Returns true if the specified point is contained by this rectangle.
133 bool Contains(const Point& point) const {
134 return Contains(point.x(), point.y());
135 }
136
137 // Returns true if this rectangle contains the specified rectangle.
138 bool Contains(const Rect& rect) const;
139
140 // Returns true if this rectangle intersects the specified rectangle.
141 // An empty rectangle doesn't intersect any rectangle.
142 bool Intersects(const Rect& rect) const;
143
144 // Computes the intersection of this rectangle with the given rectangle.
145 void Intersect(const Rect& rect);
146
147 // Computes the union of this rectangle with the given rectangle. The union
148 // is the smallest rectangle containing both rectangles.
149 void Union(const Rect& rect);
150
151 // Computes the rectangle resulting from subtracting |rect| from |*this|,
152 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
153 void Subtract(const Rect& rect);
154
155 // Fits as much of the receiving rectangle into the supplied rectangle as
156 // possible, becoming the result. For example, if the receiver had
157 // a x-location of 2 and a width of 4, and the supplied rectangle had
158 // an x-location of 0 with a width of 5, the returned rectangle would have
159 // an x-location of 1 with a width of 4.
160 void AdjustToFit(const Rect& rect);
161
162 // Returns the center of this rectangle.
163 Point CenterPoint() const;
164
165 // Becomes a rectangle that has the same center point but with a size capped
166 // at given |size|.
167 void ClampToCenteredSize(const Size& size);
168
169 // Splits |this| in two halves, |left_half| and |right_half|.
170 void SplitVertically(Rect* left_half, Rect* right_half) const;
171
172 // Returns true if this rectangle shares an entire edge (i.e., same width or
173 // same height) with the given rectangle, and the rectangles do not overlap.
174 bool SharesEdgeWith(const Rect& rect) const;
175
176 // Returns the manhattan distance from the rect to the point. If the point is
177 // inside the rect, returns 0.
178 int ManhattanDistanceToPoint(const Point& point) const;
179
180 // Returns the manhattan distance between the contents of this rect and the
181 // contents of the given rect. That is, if the intersection of the two rects
182 // is non-empty then the function returns 0. If the rects share a side, it
183 // returns the smallest non-zero value appropriate for int.
184 int ManhattanInternalDistance(const Rect& rect) const;
185
76 std::string ToString() const; 186 std::string ToString() const;
187
188 private:
189 gfx::Point origin_;
190 gfx::Size size_;
77 }; 191 };
78 192
79 inline bool operator==(const Rect& lhs, const Rect& rhs) { 193 inline bool operator==(const Rect& lhs, const Rect& rhs) {
80 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); 194 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
81 } 195 }
82 196
83 inline bool operator!=(const Rect& lhs, const Rect& rhs) { 197 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
84 return !(lhs == rhs); 198 return !(lhs == rhs);
85 } 199 }
86 200
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 int y = std::ceil(rect.y() * y_scale); 238 int y = std::ceil(rect.y() * y_scale);
125 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale); 239 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale);
126 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale); 240 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale);
127 return Rect(x, y, r - x, b - y); 241 return Rect(x, y, r - x, b - y);
128 } 242 }
129 243
130 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { 244 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
131 return ScaleToEnclosedRect(rect, scale, scale); 245 return ScaleToEnclosedRect(rect, scale, scale);
132 } 246 }
133 247
134 #if !defined(COMPILER_MSVC) && !defined(__native_client__)
135 extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
136 #endif
137
138 // This is declared here for use in gtest-based unit tests but is defined in 248 // This is declared here for use in gtest-based unit tests but is defined in
139 // the gfx_test_support target. Depend on that to use this in your unit test. 249 // the gfx_test_support target. Depend on that to use this in your unit test.
140 // This should not be used in production code - call ToString() instead. 250 // This should not be used in production code - call ToString() instead.
141 void PrintTo(const Rect& rect, ::std::ostream* os); 251 void PrintTo(const Rect& rect, ::std::ostream* os);
142 252
143 } // namespace gfx 253 } // namespace gfx
144 254
145 #endif // UI_GFX_GEOMETRY_RECT_H_ 255 #endif // UI_GFX_GEOMETRY_RECT_H_
OLDNEW
« no previous file with comments | « ui/gfx/geometry/BUILD.gn ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698