- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gfx / rect_base_impl.h
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.
4
5 #include "ui/gfx/rect_base.h"
6
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9
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(GFX_IMPLEMENTATION)
13 #error "This file is intended for UI implementation only"
14 #endif
15
16 namespace {
17
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;
25 }
26
27 } // namespace
28
29 namespace gfx {
30
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);
42 }
43
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());
53 }
54
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)));
66 }
67
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);
77 }
78
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;
88 }
89
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;
99 }
100
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();
114     }
115   } else {
116     return origin_ < other.origin_;
117   }
118 }
119
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());
130 }
131
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());
142 }
143
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 !(IsEmpty() || rect.IsEmpty() ||
153            rect.x() >= right() || rect.right() <= x() ||
154            rect.y() >= bottom() || rect.bottom() <= y());
155 }
156
157 template<typename Class,
158          typename PointClass,
159          typename SizeClass,
160          typename InsetsClass,
161          typename VectorClass,
162          typename Type>
163 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
164     Intersect(const Class& rect) {
165   if (IsEmpty() || rect.IsEmpty()) {
166     SetRect(0, 0, 0, 0);
167     return;
168   }
169
170   Type rx = std::max(x(), rect.x());
171   Type ry = std::max(y(), rect.y());
172   Type rr = std::min(right(), rect.right());
173   Type rb = std::min(bottom(), rect.bottom());
174
175   if (rx >= rr || ry >= rb)
176     rx = ry = rr = rb = 0;  // non-intersecting
177
178   SetRect(rx, ry, rr - rx, rb - ry);
179 }
180
181 template<typename Class,
182          typename PointClass,
183          typename SizeClass,
184          typename InsetsClass,
185          typename VectorClass,
186          typename Type>
187 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
188     Union(const Class& rect) {
189   if (IsEmpty()) {
190     *this = rect;
191     return;
192   }
193   if (rect.IsEmpty())
194     return;
195
196   Type rx = std::min(x(), rect.x());
197   Type ry = std::min(y(), rect.y());
198   Type rr = std::max(right(), rect.right());
199   Type rb = std::max(bottom(), rect.bottom());
200
201   SetRect(rx, ry, rr - rx, rb - ry);
202 }
203
204 template<typename Class,
205          typename PointClass,
206          typename SizeClass,
207          typename InsetsClass,
208          typename VectorClass,
209          typename Type>
210 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
211     Subtract(const Class& rect) {
212   if (!Intersects(rect))
213     return;
214   if (rect.Contains(*static_cast<const Class*>(this))) {
215     SetRect(0, 0, 0, 0);
216     return;
217   }
218
219   Type rx = x();
220   Type ry = y();
221   Type rr = right();
222   Type rb = bottom();
223
224   if (rect.y() <= y() && rect.bottom() >= bottom()) {
225     // complete intersection in the y-direction
226     if (rect.x() <= x()) {
227       rx = rect.right();
228     } else if (rect.right() >= right()) {
229       rr = rect.x();
230     }
231   } else if (rect.x() <= x() && rect.right() >= right()) {
232     // complete intersection in the x-direction
233     if (rect.y() <= y()) {
234       ry = rect.bottom();
235     } else if (rect.bottom() >= bottom()) {
236       rb = rect.y();
237     }
238   }
239   SetRect(rx, ry, rr - rx, rb - ry);
240 }
241
242 template<typename Class,
243          typename PointClass,
244          typename SizeClass,
245          typename InsetsClass,
246          typename VectorClass,
247          typename Type>
248 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
249     AdjustToFit(const Class& rect) {
250   Type new_x = x();
251   Type new_y = y();
252   Type new_width = width();
253   Type new_height = height();
254   AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
255   AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
256   SetRect(new_x, new_y, new_width, new_height);
257 }
258
259 template<typename Class,
260          typename PointClass,
261          typename SizeClass,
262          typename InsetsClass,
263          typename VectorClass,
264          typename Type>
265 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass,
266     Type>::CenterPoint() const {
267   return PointClass(x() + width() / 2, y() + height() / 2);
268 }
269
270 template<typename Class,
271          typename PointClass,
272          typename SizeClass,
273          typename InsetsClass,
274          typename VectorClass,
275          typename Type>
276 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
277     ClampToCenteredSize(const SizeClass& size) {
278   Type new_width = std::min(width(), size.width());
279   Type new_height = std::min(height(), size.height());
280   Type new_x = x() + (width() - new_width) / 2;
281   Type new_y = y() + (height() - new_height) / 2;
282   SetRect(new_x, new_y, new_width, new_height);
283 }
284
285 template<typename Class,
286          typename PointClass,
287          typename SizeClass,
288          typename InsetsClass,
289          typename VectorClass,
290          typename Type>
291 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
292     SplitVertically(Class* left_half, Class* right_half) const {
293   DCHECK(left_half);
294   DCHECK(right_half);
295
296   left_half->SetRect(x(), y(), width() / 2, height());
297   right_half->SetRect(left_half->right(),
298                       y(),
299                       width() - left_half->width(),
300                       height());
301 }
302
303 template<typename Class,
304          typename PointClass,
305          typename SizeClass,
306          typename InsetsClass,
307          typename VectorClass,
308          typename Type>
309 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
310     SharesEdgeWith(const Class& rect) const {
311   return (y() == rect.y() && height() == rect.height() &&
312              (x() == rect.right() || right() == rect.x())) ||
313          (x() == rect.x() && width() == rect.width() &&
314              (y() == rect.bottom() || bottom() == rect.y()));
315 }
316
317 template<typename Class,
318          typename PointClass,
319          typename SizeClass,
320          typename InsetsClass,
321          typename VectorClass,
322          typename Type>
323 Type RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>::
324     ManhattanDistanceToPoint(const PointClass& point) const {
325   Type x_distance = std::max<Type>(0, std::max(
326       x() - point.x(), point.x() - right()));
327   Type y_distance = std::max<Type>(0, std::max(
328       y() - point.y(), point.y() - bottom()));
329
330   return x_distance + y_distance;
331 }
332
333 }  // namespace gfx