Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / geometry / rect.cc
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/geometry/rect.h"
6
7 #include <algorithm>
8
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
12
13 #include "base/logging.h"
14 #include "base/strings/stringprintf.h"
15 #include "ui/gfx/geometry/insets.h"
16 #include "ui/gfx/geometry/rect_base_impl.h"
17
18 namespace gfx {
19
20 template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
21
22 typedef class RectBase<Rect, Point, Size, Insets, Vector2d, int> RectBaseT;
23
24 #if defined(OS_WIN)
25 Rect::Rect(const RECT& r)
26     : RectBaseT(gfx::Point(r.left, r.top)) {
27   set_width(std::abs(r.right - r.left));
28   set_height(std::abs(r.bottom - r.top));
29 }
30 #elif defined(OS_MACOSX)
31 Rect::Rect(const CGRect& r)
32     : RectBaseT(gfx::Point(r.origin.x, r.origin.y)) {
33   set_width(r.size.width);
34   set_height(r.size.height);
35 }
36 #endif
37
38 #if defined(OS_WIN)
39 RECT Rect::ToRECT() const {
40   RECT r;
41   r.left = x();
42   r.right = right();
43   r.top = y();
44   r.bottom = bottom();
45   return r;
46 }
47 #elif defined(OS_MACOSX)
48 CGRect Rect::ToCGRect() const {
49   return CGRectMake(x(), y(), width(), height());
50 }
51 #endif
52
53 std::string Rect::ToString() const {
54   return base::StringPrintf("%s %s",
55                             origin().ToString().c_str(),
56                             size().ToString().c_str());
57 }
58
59 Rect operator+(const Rect& lhs, const Vector2d& rhs) {
60   Rect result(lhs);
61   result += rhs;
62   return result;
63 }
64
65 Rect operator-(const Rect& lhs, const Vector2d& rhs) {
66   Rect result(lhs);
67   result -= rhs;
68   return result;
69 }
70
71 Rect IntersectRects(const Rect& a, const Rect& b) {
72   Rect result = a;
73   result.Intersect(b);
74   return result;
75 }
76
77 Rect UnionRects(const Rect& a, const Rect& b) {
78   Rect result = a;
79   result.Union(b);
80   return result;
81 }
82
83 Rect SubtractRects(const Rect& a, const Rect& b) {
84   Rect result = a;
85   result.Subtract(b);
86   return result;
87 }
88
89 Rect BoundingRect(const Point& p1, const Point& p2) {
90   int rx = std::min(p1.x(), p2.x());
91   int ry = std::min(p1.y(), p2.y());
92   int rr = std::max(p1.x(), p2.x());
93   int rb = std::max(p1.y(), p2.y());
94   return Rect(rx, ry, rr - rx, rb - ry);
95 }
96
97 }  // namespace gfx