mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 1 | // 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" |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 8 | #include "ui/gfx/geometry/point_conversions.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 9 | #include "ui/gfx/geometry/rect.h" |
danakj | 335602b | 2015-08-31 23:35:03 | [diff] [blame] | 10 | #include "ui/gfx/geometry/rect_f.h" |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 11 | #include "ui/gfx/selection_bound.h" |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 12 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 13 | namespace gfx { |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 14 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 15 | SelectionBound::SelectionBound() : type_(EMPTY), visible_(false) {} |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 16 | |
spang | ce336f6 | 2015-02-17 22:53:31 | [diff] [blame] | 17 | SelectionBound::SelectionBound(const SelectionBound& other) = default; |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 18 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 19 | SelectionBound::~SelectionBound() {} |
mohsen | f837da7c | 2014-12-09 19:01:34 | [diff] [blame] | 20 | |
| 21 | void SelectionBound::SetEdgeTop(const gfx::PointF& value) { |
| 22 | edge_top_ = value; |
| 23 | edge_top_rounded_ = gfx::ToRoundedPoint(value); |
| 24 | } |
| 25 | |
| 26 | void SelectionBound::SetEdgeBottom(const gfx::PointF& value) { |
| 27 | edge_bottom_ = value; |
| 28 | edge_bottom_rounded_ = gfx::ToRoundedPoint(value); |
| 29 | } |
| 30 | |
| 31 | void SelectionBound::SetEdge(const gfx::PointF& top, |
| 32 | const gfx::PointF& bottom) { |
| 33 | SetEdgeTop(top); |
| 34 | SetEdgeBottom(bottom); |
| 35 | } |
| 36 | |
| 37 | int SelectionBound::GetHeight() const { |
| 38 | return edge_bottom_rounded_.y() - edge_top_rounded_.y(); |
| 39 | } |
| 40 | |
| 41 | bool 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 | |
| 47 | bool operator!=(const SelectionBound& lhs, const SelectionBound& rhs) { |
| 48 | return !(lhs == rhs); |
| 49 | } |
| 50 | |
| 51 | gfx::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 | |
jdduke | 3628fb38 | 2015-04-15 00:08:41 | [diff] [blame] | 67 | gfx::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 | |
fsamuel | 181cbc0 | 2016-06-03 02:31:14 | [diff] [blame] | 83 | } // namespace gfx |