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
3 // found in the LICENSE file.
5 #include "ui/gfx/rect_base.h"
7 #include "base/logging.h"
8 #include "base/stringprintf.h"
10 // This file provides the implementation for RectBaese template and
11 // used to instantiate the base class for Rect and RectF classes.
12 #if !defined(UI_IMPLEMENTATION)
13 #error "This file is intended for UI implementation only"
18 template<typename Type
>
19 void AdjustAlongAxis(Type dst_origin
, Type dst_size
, Type
* origin
, Type
* size
) {
20 *size
= std::min(dst_size
, *size
);
21 if (*origin
< dst_origin
)
24 *origin
= std::min(dst_origin
+ dst_size
, *origin
+ *size
) - *size
;
31 template<typename Class
,
37 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
38 SetRect(Type x
, Type y
, Type width
, Type height
) {
39 origin_
.SetPoint(x
, y
);
44 template<typename Class
,
50 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
51 Inset(const InsetsClass
& insets
) {
52 Inset(insets
.left(), insets
.top(), insets
.right(), insets
.bottom());
55 template<typename Class
,
61 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
62 Inset(Type left
, Type top
, Type right
, Type bottom
) {
63 origin_
+= VectorClass(left
, top
);
64 set_width(std::max(width() - left
- right
, static_cast<Type
>(0)));
65 set_height(std::max(height() - top
- bottom
, static_cast<Type
>(0)));
68 template<typename Class
,
74 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
75 Offset(Type horizontal
, Type vertical
) {
76 origin_
+= VectorClass(horizontal
, vertical
);
79 template<typename Class
,
85 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
86 operator+=(const VectorClass
& offset
) {
90 template<typename Class
,
96 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
97 operator-=(const VectorClass
& offset
) {
101 template<typename Class
,
104 typename InsetsClass
,
105 typename VectorClass
,
107 bool RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
108 operator<(const Class
& other
) const {
109 if (origin_
== other
.origin_
) {
110 if (width() == other
.width()) {
111 return height() < other
.height();
113 return width() < other
.width();
116 return origin_
< other
.origin_
;
120 template<typename Class
,
123 typename InsetsClass
,
124 typename VectorClass
,
126 bool RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
127 Contains(Type point_x
, Type point_y
) const {
128 return (point_x
>= x()) && (point_x
< right()) &&
129 (point_y
>= y()) && (point_y
< bottom());
132 template<typename Class
,
135 typename InsetsClass
,
136 typename VectorClass
,
138 bool RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
139 Contains(const Class
& rect
) const {
140 return (rect
.x() >= x() && rect
.right() <= right() &&
141 rect
.y() >= y() && rect
.bottom() <= bottom());
144 template<typename Class
,
147 typename InsetsClass
,
148 typename VectorClass
,
150 bool RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
151 Intersects(const Class
& rect
) const {
152 return !(rect
.x() >= right() || rect
.right() <= x() ||
153 rect
.y() >= bottom() || rect
.bottom() <= y());
156 template<typename Class
,
159 typename InsetsClass
,
160 typename VectorClass
,
162 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
163 Intersect(const Class
& rect
) {
164 if (IsEmpty() || rect
.IsEmpty()) {
169 Type rx
= std::max(x(), rect
.x());
170 Type ry
= std::max(y(), rect
.y());
171 Type rr
= std::min(right(), rect
.right());
172 Type rb
= std::min(bottom(), rect
.bottom());
174 if (rx
>= rr
|| ry
>= rb
)
175 rx
= ry
= rr
= rb
= 0; // non-intersecting
177 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
180 template<typename Class
,
183 typename InsetsClass
,
184 typename VectorClass
,
186 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
187 Union(const Class
& rect
) {
195 Type rx
= std::min(x(), rect
.x());
196 Type ry
= std::min(y(), rect
.y());
197 Type rr
= std::max(right(), rect
.right());
198 Type rb
= std::max(bottom(), rect
.bottom());
200 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
203 template<typename Class
,
206 typename InsetsClass
,
207 typename VectorClass
,
209 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
210 Subtract(const Class
& rect
) {
211 if (!Intersects(rect
))
213 if (rect
.Contains(*static_cast<const Class
*>(this))) {
223 if (rect
.y() <= y() && rect
.bottom() >= bottom()) {
224 // complete intersection in the y-direction
225 if (rect
.x() <= x()) {
230 } else if (rect
.x() <= x() && rect
.right() >= right()) {
231 // complete intersection in the x-direction
232 if (rect
.y() <= y()) {
238 SetRect(rx
, ry
, rr
- rx
, rb
- ry
);
241 template<typename Class
,
244 typename InsetsClass
,
245 typename VectorClass
,
247 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
248 AdjustToFit(const Class
& rect
) {
251 Type new_width
= width();
252 Type new_height
= height();
253 AdjustAlongAxis(rect
.x(), rect
.width(), &new_x
, &new_width
);
254 AdjustAlongAxis(rect
.y(), rect
.height(), &new_y
, &new_height
);
255 SetRect(new_x
, new_y
, new_width
, new_height
);
258 template<typename Class
,
261 typename InsetsClass
,
262 typename VectorClass
,
264 PointClass RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
,
265 Type
>::CenterPoint() const {
266 return PointClass(x() + width() / 2, y() + height() / 2);
269 template<typename Class
,
272 typename InsetsClass
,
273 typename VectorClass
,
275 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
276 ClampToCenteredSize(const SizeClass
& size
) {
277 Type new_width
= std::min(width(), size
.width());
278 Type new_height
= std::min(height(), size
.height());
279 Type new_x
= x() + (width() - new_width
) / 2;
280 Type new_y
= y() + (height() - new_height
) / 2;
281 SetRect(new_x
, new_y
, new_width
, new_height
);
284 template<typename Class
,
287 typename InsetsClass
,
288 typename VectorClass
,
290 void RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
291 SplitVertically(Class
* left_half
, Class
* right_half
) const {
295 left_half
->SetRect(x(), y(), width() / 2, height());
296 right_half
->SetRect(left_half
->right(),
298 width() - left_half
->width(),
302 template<typename Class
,
305 typename InsetsClass
,
306 typename VectorClass
,
308 bool RectBase
<Class
, PointClass
, SizeClass
, InsetsClass
, VectorClass
, Type
>::
309 SharesEdgeWith(const Class
& rect
) const {
310 return (y() == rect
.y() && height() == rect
.height() &&
311 (x() == rect
.right() || right() == rect
.x())) ||
312 (x() == rect
.x() && width() == rect
.width() &&
313 (y() == rect
.bottom() || bottom() == rect
.y()));