blob: 9be1c6062e8d11149a106a0278a4db58397ce94d [file] [log] [blame]
mohsenf837da7c2014-12-09 19:01:341// Copyright 2014 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 <algorithm>
6
7#include "base/macros.h"
mohsenf837da7c2014-12-09 19:01:348#include "ui/gfx/geometry/point_conversions.h"
tfarina3b0452d2014-12-31 15:20:099#include "ui/gfx/geometry/rect.h"
danakj335602b2015-08-31 23:35:0310#include "ui/gfx/geometry/rect_f.h"
fsamuel181cbc02016-06-03 02:31:1411#include "ui/gfx/selection_bound.h"
mohsenf837da7c2014-12-09 19:01:3412
fsamuel181cbc02016-06-03 02:31:1413namespace gfx {
mohsenf837da7c2014-12-09 19:01:3414
fsamuel181cbc02016-06-03 02:31:1415SelectionBound::SelectionBound() : type_(EMPTY), visible_(false) {}
mohsenf837da7c2014-12-09 19:01:3416
spangce336f62015-02-17 22:53:3117SelectionBound::SelectionBound(const SelectionBound& other) = default;
mohsenf837da7c2014-12-09 19:01:3418
fsamuel181cbc02016-06-03 02:31:1419SelectionBound::~SelectionBound() {}
mohsenf837da7c2014-12-09 19:01:3420
21void SelectionBound::SetEdgeTop(const gfx::PointF& value) {
22 edge_top_ = value;
23 edge_top_rounded_ = gfx::ToRoundedPoint(value);
24}
25
26void SelectionBound::SetEdgeBottom(const gfx::PointF& value) {
27 edge_bottom_ = value;
28 edge_bottom_rounded_ = gfx::ToRoundedPoint(value);
29}
30
31void SelectionBound::SetEdge(const gfx::PointF& top,
32 const gfx::PointF& bottom) {
33 SetEdgeTop(top);
34 SetEdgeBottom(bottom);
35}
36
37int SelectionBound::GetHeight() const {
38 return edge_bottom_rounded_.y() - edge_top_rounded_.y();
39}
40
41bool operator==(const SelectionBound& lhs, const SelectionBound& rhs) {
42 return lhs.type() == rhs.type() && lhs.visible() == rhs.visible() &&
43 lhs.edge_top() == rhs.edge_top() &&
44 lhs.edge_bottom() == rhs.edge_bottom();
45}
46
47bool operator!=(const SelectionBound& lhs, const SelectionBound& rhs) {
48 return !(lhs == rhs);
49}
50
51gfx::Rect RectBetweenSelectionBounds(const SelectionBound& b1,
52 const SelectionBound& b2) {
53 gfx::Point top_left(b1.edge_top_rounded());
54 top_left.SetToMin(b1.edge_bottom_rounded());
55 top_left.SetToMin(b2.edge_top_rounded());
56 top_left.SetToMin(b2.edge_bottom_rounded());
57
58 gfx::Point bottom_right(b1.edge_top_rounded());
59 bottom_right.SetToMax(b1.edge_bottom_rounded());
60 bottom_right.SetToMax(b2.edge_top_rounded());
61 bottom_right.SetToMax(b2.edge_bottom_rounded());
62
63 gfx::Vector2d diff = bottom_right - top_left;
64 return gfx::Rect(top_left, gfx::Size(diff.x(), diff.y()));
65}
66
jdduke3628fb382015-04-15 00:08:4167gfx::RectF RectFBetweenSelectionBounds(const SelectionBound& b1,
68 const SelectionBound& b2) {
69 gfx::PointF top_left(b1.edge_top());
70 top_left.SetToMin(b1.edge_bottom());
71 top_left.SetToMin(b2.edge_top());
72 top_left.SetToMin(b2.edge_bottom());
73
74 gfx::PointF bottom_right(b1.edge_top());
75 bottom_right.SetToMax(b1.edge_bottom());
76 bottom_right.SetToMax(b2.edge_top());
77 bottom_right.SetToMax(b2.edge_bottom());
78
79 gfx::Vector2dF diff = bottom_right - top_left;
80 return gfx::RectF(top_left, gfx::SizeF(diff.x(), diff.y()));
81}
82
fsamuel181cbc02016-06-03 02:31:1483} // namespace gfx