Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / gfx / geometry / rect.cc
blobf418e175b89c6fa79a10a4d4ba2d6fb88af23aa5
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/geometry/rect.h"
7 #include <algorithm>
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
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"
18 namespace gfx {
20 template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
22 typedef class RectBase<Rect, Point, Size, Insets, Vector2d, int> RectBaseT;
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));
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);
36 #endif
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;
47 #elif defined(OS_MACOSX)
48 CGRect Rect::ToCGRect() const {
49 return CGRectMake(x(), y(), width(), height());
51 #endif
53 std::string Rect::ToString() const {
54 return base::StringPrintf("%s %s",
55 origin().ToString().c_str(),
56 size().ToString().c_str());
59 Rect operator+(const Rect& lhs, const Vector2d& rhs) {
60 Rect result(lhs);
61 result += rhs;
62 return result;
65 Rect operator-(const Rect& lhs, const Vector2d& rhs) {
66 Rect result(lhs);
67 result -= rhs;
68 return result;
71 Rect IntersectRects(const Rect& a, const Rect& b) {
72 Rect result = a;
73 result.Intersect(b);
74 return result;
77 Rect UnionRects(const Rect& a, const Rect& b) {
78 Rect result = a;
79 result.Union(b);
80 return result;
83 Rect SubtractRects(const Rect& a, const Rect& b) {
84 Rect result = a;
85 result.Subtract(b);
86 return result;
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);
97 } // namespace gfx