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