Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / gfx / rect_base_impl.h
blob4184c27c859f2c8920f2b092cf59b627ae915b92
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"
14 #endif
16 namespace {
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)
22 *origin = dst_origin;
23 else
24 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
27 } // namespace
29 namespace gfx {
31 template<typename Class,
32 typename PointClass,
33 typename SizeClass,
34 typename InsetsClass,
35 typename VectorClass,
36 typename Type>
37 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
38 SetRect(Type x, Type y, Type width, Type height) {
39 origin_.SetPoint(x, y);
40 set_width(width);
41 set_height(height);
44 template<typename Class,
45 typename PointClass,
46 typename SizeClass,
47 typename InsetsClass,
48 typename VectorClass,
49 typename Type>
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,
56 typename PointClass,
57 typename SizeClass,
58 typename InsetsClass,
59 typename VectorClass,
60 typename Type>
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,
69 typename PointClass,
70 typename SizeClass,
71 typename InsetsClass,
72 typename VectorClass,
73 typename Type>
74 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
75 Offset(Type horizontal, Type vertical) {
76 origin_ += VectorClass(horizontal, vertical);
79 template<typename Class,
80 typename PointClass,
81 typename SizeClass,
82 typename InsetsClass,
83 typename VectorClass,
84 typename Type>
85 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
86 operator+=(const VectorClass& offset) {
87 origin_ += offset;
90 template<typename Class,
91 typename PointClass,
92 typename SizeClass,
93 typename InsetsClass,
94 typename VectorClass,
95 typename Type>
96 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
97 operator-=(const VectorClass& offset) {
98 origin_ -= offset;
101 template<typename Class,
102 typename PointClass,
103 typename SizeClass,
104 typename InsetsClass,
105 typename VectorClass,
106 typename Type>
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();
112 } else {
113 return width() < other.width();
115 } else {
116 return origin_ < other.origin_;
120 template<typename Class,
121 typename PointClass,
122 typename SizeClass,
123 typename InsetsClass,
124 typename VectorClass,
125 typename Type>
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,
133 typename PointClass,
134 typename SizeClass,
135 typename InsetsClass,
136 typename VectorClass,
137 typename Type>
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,
145 typename PointClass,
146 typename SizeClass,
147 typename InsetsClass,
148 typename VectorClass,
149 typename Type>
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,
157 typename PointClass,
158 typename SizeClass,
159 typename InsetsClass,
160 typename VectorClass,
161 typename Type>
162 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
163 Intersect(const Class& rect) {
164 if (IsEmpty() || rect.IsEmpty()) {
165 SetRect(0, 0, 0, 0);
166 return;
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,
181 typename PointClass,
182 typename SizeClass,
183 typename InsetsClass,
184 typename VectorClass,
185 typename Type>
186 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
187 Union(const Class& rect) {
188 if (IsEmpty()) {
189 *this = rect;
190 return;
192 if (rect.IsEmpty())
193 return;
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,
204 typename PointClass,
205 typename SizeClass,
206 typename InsetsClass,
207 typename VectorClass,
208 typename Type>
209 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
210 Subtract(const Class& rect) {
211 if (!Intersects(rect))
212 return;
213 if (rect.Contains(*static_cast<const Class*>(this))) {
214 SetRect(0, 0, 0, 0);
215 return;
218 Type rx = x();
219 Type ry = y();
220 Type rr = right();
221 Type rb = bottom();
223 if (rect.y() <= y() && rect.bottom() >= bottom()) {
224 // complete intersection in the y-direction
225 if (rect.x() <= x()) {
226 rx = rect.right();
227 } else {
228 rr = rect.x();
230 } else if (rect.x() <= x() && rect.right() >= right()) {
231 // complete intersection in the x-direction
232 if (rect.y() <= y()) {
233 ry = rect.bottom();
234 } else {
235 rb = rect.y();
238 SetRect(rx, ry, rr - rx, rb - ry);
241 template<typename Class,
242 typename PointClass,
243 typename SizeClass,
244 typename InsetsClass,
245 typename VectorClass,
246 typename Type>
247 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
248 AdjustToFit(const Class& rect) {
249 Type new_x = x();
250 Type new_y = y();
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,
259 typename PointClass,
260 typename SizeClass,
261 typename InsetsClass,
262 typename VectorClass,
263 typename Type>
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,
270 typename PointClass,
271 typename SizeClass,
272 typename InsetsClass,
273 typename VectorClass,
274 typename Type>
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,
285 typename PointClass,
286 typename SizeClass,
287 typename InsetsClass,
288 typename VectorClass,
289 typename Type>
290 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
291 SplitVertically(Class* left_half, Class* right_half) const {
292 DCHECK(left_half);
293 DCHECK(right_half);
295 left_half->SetRect(x(), y(), width() / 2, height());
296 right_half->SetRect(left_half->right(),
297 y(),
298 width() - left_half->width(),
299 height());
302 template<typename Class,
303 typename PointClass,
304 typename SizeClass,
305 typename InsetsClass,
306 typename VectorClass,
307 typename Type>
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()));
316 } // namespace gfx