summaryrefslogtreecommitdiffstats
path: root/ui/gfx
diff options
context:
space:
mode:
authordpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-05 22:49:09 +0000
committerdpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-05 22:49:09 +0000
commitfa742ee5562cf36b55dbd0f1f8ec5b9e131ca539 (patch)
tree7e5e9eea2197f2119f1d80b0c0a4531ad97570e9 /ui/gfx
parentb95700e633f8e4aa280fdaffe7e761c62d377221 (diff)
downloadchromium_src-fa742ee5562cf36b55dbd0f1f8ec5b9e131ca539.zip
chromium_src-fa742ee5562cf36b55dbd0f1f8ec5b9e131ca539.tar.gz
chromium_src-fa742ee5562cf36b55dbd0f1f8ec5b9e131ca539.tar.bz2
Adding gfx::Rect::SplitVertically() method.
1) Code was replicated in dragged_tab_controller_gtk.cc and dragged_tab_controller.cc, this allows the same code to be reused. Also removing the corresponding TODO. 2) The old SplitRectangleInTwo() function was violating the styleguide by having not const references as parameters. BUG=NONE TEST=NONE Review URL: http://codereview.chromium.org/8137018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104199 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r--ui/gfx/rect.cc12
-rw-r--r--ui/gfx/rect.h3
-rw-r--r--ui/gfx/rect_unittest.cc24
3 files changed, 39 insertions, 0 deletions
diff --git a/ui/gfx/rect.cc b/ui/gfx/rect.cc
index bc0a8a9..81d4e09 100644
--- a/ui/gfx/rect.cc
+++ b/ui/gfx/rect.cc
@@ -15,6 +15,7 @@
#include <cairo.h>
#endif
+#include "base/logging.h"
#include "base/stringprintf.h"
#include "ui/gfx/insets.h"
@@ -266,6 +267,17 @@ Rect Rect::Center(const gfx::Size& size) const {
return Rect(new_x, new_y, new_width, new_height);
}
+void Rect::SplitVertically(gfx::Rect* left_half, gfx::Rect* right_half) const {
+ DCHECK(left_half);
+ DCHECK(right_half);
+
+ left_half->SetRect(this->x(), this->y(), this->width() / 2, this->height());
+ right_half->SetRect(left_half->right(),
+ this->y(),
+ this->width() - left_half->width(),
+ this->height());
+}
+
bool Rect::SharesEdgeWith(const gfx::Rect& rect) const {
return (y() == rect.y() && height() == rect.height() &&
(x() == rect.right() || right() == rect.x())) ||
diff --git a/ui/gfx/rect.h b/ui/gfx/rect.h
index d77d2f6..d19074c 100644
--- a/ui/gfx/rect.h
+++ b/ui/gfx/rect.h
@@ -181,6 +181,9 @@ class UI_EXPORT Rect {
// at given |size|.
Rect Center(const gfx::Size& size) const;
+ // Splits |this| in two halves, |left_half| and |right_half|.
+ void SplitVertically(gfx::Rect* left_half, gfx::Rect* right_half) const;
+
// Returns true if this rectangle shares an entire edge (i.e., same width or
// same height) with the given rectangle, and the rectangles do not overlap.
bool SharesEdgeWith(const gfx::Rect& rect) const;
diff --git a/ui/gfx/rect_unittest.cc b/ui/gfx/rect_unittest.cc
index 4ad3f74..4eefdd6 100644
--- a/ui/gfx/rect_unittest.cc
+++ b/ui/gfx/rect_unittest.cc
@@ -280,6 +280,30 @@ TEST(RectTest, IsEmpty) {
EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).size().IsEmpty());
}
+TEST(RectTest, SplitVertically) {
+ gfx::Rect left_half, right_half;
+
+ // Splitting when origin is (0, 0).
+ gfx::Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half);
+ EXPECT_TRUE(left_half.Equals(gfx::Rect(0, 0, 10, 20)));
+ EXPECT_TRUE(right_half.Equals(gfx::Rect(10, 0, 10, 20)));
+
+ // Splitting when origin is arbitrary.
+ gfx::Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half);
+ EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 10, 10)));
+ EXPECT_TRUE(right_half.Equals(gfx::Rect(20, 10, 10, 10)));
+
+ // Splitting a rectangle of zero width.
+ gfx::Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half);
+ EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 0, 10)));
+ EXPECT_TRUE(right_half.Equals(gfx::Rect(10, 10, 0, 10)));
+
+ // Splitting a rectangle of odd width.
+ gfx::Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half);
+ EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 2, 10)));
+ EXPECT_TRUE(right_half.Equals(gfx::Rect(12, 10, 3, 10)));
+}
+
TEST(RectTest, SharesEdgeWith) {
gfx::Rect r(2, 3, 4, 5);