diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 09:07:01 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 09:07:01 +0000 |
commit | e5c45f6c5f6778f1da0053ccbb8d95a15bdbcc14 (patch) | |
tree | d319bff466a413f5e090a542e058ce7ef7ed2b85 /ui | |
parent | 394633964593be10c6bb220c73083e1b8308bf23 (diff) | |
download | chromium_src-e5c45f6c5f6778f1da0053ccbb8d95a15bdbcc14.zip chromium_src-e5c45f6c5f6778f1da0053ccbb8d95a15bdbcc14.tar.gz chromium_src-e5c45f6c5f6778f1da0053ccbb8d95a15bdbcc14.tar.bz2 |
Use template for Point/Size/Rect and remove duplicates
BUG=114664
TEST=none
Review URL: https://chromiumcodereview.appspot.com/10014027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131737 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/point.cc | 32 | ||||
-rw-r--r-- | ui/gfx/point.h | 64 | ||||
-rw-r--r-- | ui/gfx/point_base.h | 83 | ||||
-rw-r--r-- | ui/gfx/point_f.cc | 15 | ||||
-rw-r--r-- | ui/gfx/point_f.h | 66 | ||||
-rw-r--r-- | ui/gfx/rect.cc | 192 | ||||
-rw-r--r-- | ui/gfx/rect.h | 121 | ||||
-rw-r--r-- | ui/gfx/rect_base.h | 152 | ||||
-rw-r--r-- | ui/gfx/rect_base_impl.h | 318 | ||||
-rw-r--r-- | ui/gfx/rect_f.cc | 181 | ||||
-rw-r--r-- | ui/gfx/rect_f.h | 124 | ||||
-rw-r--r-- | ui/gfx/size.cc | 34 | ||||
-rw-r--r-- | ui/gfx/size.h | 40 | ||||
-rw-r--r-- | ui/gfx/size_base.h | 62 | ||||
-rw-r--r-- | ui/gfx/size_base_impl.h | 45 | ||||
-rw-r--r-- | ui/gfx/size_f.cc | 29 | ||||
-rw-r--r-- | ui/gfx/size_f.h | 46 | ||||
-rw-r--r-- | ui/ui.gyp | 9 |
18 files changed, 779 insertions, 834 deletions
diff --git a/ui/gfx/point.cc b/ui/gfx/point.cc index ba9ced7..24e1ca2 100644 --- a/ui/gfx/point.cc +++ b/ui/gfx/point.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,45 +12,49 @@ namespace gfx { -Point::Point() : x_(0), y_(0) { +template<> +PointBase<Point,int>::PointBase(int x, int y) : x_(x), y_(y) { } -Point::Point(int x, int y) : x_(x), y_(y) { +Point::Point() : PointBase<Point, int>(0, 0) { +} + +Point::Point(int x, int y) : PointBase<Point, int>(x, y) { } #if defined(OS_WIN) -Point::Point(DWORD point) { +Point::Point(DWORD point) : PointBase<Point, int>(0, 0){ POINTS points = MAKEPOINTS(point); - x_ = points.x; - y_ = points.y; + set_x(points.x); + set_y(points.y); } -Point::Point(const POINT& point) : x_(point.x), y_(point.y) { +Point::Point(const POINT& point) : PointBase<Point, int>(point.x, point.y) { } Point& Point::operator=(const POINT& point) { - x_ = point.x; - y_ = point.y; + set_x(point.x); + set_y(point.y); return *this; } POINT Point::ToPOINT() const { POINT p; - p.x = x_; - p.y = y_; + p.x = x(); + p.y = y(); return p; } #elif defined(OS_MACOSX) -Point::Point(const CGPoint& point) : x_(point.x), y_(point.y) { +Point::Point(const CGPoint& point) : PointBase<Point, int>(point.x, point.y) { } CGPoint Point::ToCGPoint() const { - return CGPointMake(x_, y_); + return CGPointMake(x(), y()); } #endif std::string Point::ToString() const { - return base::StringPrintf("%d,%d", x_, y_); + return base::StringPrintf("%d,%d", x(), y()); } } // namespace gfx diff --git a/ui/gfx/point.h b/ui/gfx/point.h index fe39e3b..27bf098 100644 --- a/ui/gfx/point.h +++ b/ui/gfx/point.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -6,10 +6,8 @@ #define UI_GFX_POINT_H_ #pragma once -#include <string> - -#include "build/build_config.h" #include "ui/base/ui_export.h" +#include "ui/gfx/point_base.h" #if defined(OS_WIN) typedef unsigned long DWORD; @@ -21,7 +19,7 @@ typedef struct tagPOINT POINT; namespace gfx { // A point has an x and y coordinate. -class UI_EXPORT Point { +class UI_EXPORT Point : public PointBase<Point, int> { public: Point(); Point(int x, int y); @@ -36,57 +34,7 @@ class UI_EXPORT Point { explicit Point(const CGPoint& point); #endif - ~Point() {} - - int x() const { return x_; } - int y() const { return y_; } - - void SetPoint(int x, int y) { - x_ = x; - y_ = y; - } - - void set_x(int x) { x_ = x; } - void set_y(int y) { y_ = y; } - - void Offset(int delta_x, int delta_y) { - x_ += delta_x; - y_ += delta_y; - } - - Point Add(const Point& other) const{ - Point copy = *this; - copy.Offset(other.x_, other.y_); - return copy; - } - - Point Subtract(const Point& other) const { - Point copy = *this; - copy.Offset(-other.x_, -other.y_); - return copy; - } - - Point Middle(const Point& other) const { - return Point((x_ + other.x_) / 2, (y_ + other.y_) / 2); - } - - bool operator==(const Point& rhs) const { - return x_ == rhs.x_ && y_ == rhs.y_; - } - - bool operator!=(const Point& rhs) const { - return !(*this == rhs); - } - - // A point is less than another point if its y-value is closer - // to the origin. If the y-values are the same, then point with - // the x-value closer to the origin is considered less than the - // other. - // This comparison is required to use Points in sets, or sorted - // vectors. - bool operator<(const Point& rhs) const { - return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); - } + virtual ~Point() {} #if defined(OS_WIN) POINT ToPOINT() const; @@ -96,10 +44,6 @@ class UI_EXPORT Point { // Returns a string representation of point. std::string ToString() const; - - private: - int x_; - int y_; }; } // namespace gfx diff --git a/ui/gfx/point_base.h b/ui/gfx/point_base.h new file mode 100644 index 0000000..82936ee --- /dev/null +++ b/ui/gfx/point_base.h @@ -0,0 +1,83 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_GFX_POINT_BASE_H_ +#define UI_GFX_POINT_BASE_H_ +#pragma once + +#include <string> + +#include "build/build_config.h" +#include "ui/base/ui_export.h" + +namespace gfx { + +// A point has an x and y coordinate. +template<typename Class, typename Type> +class UI_EXPORT PointBase { + public: + Type x() const { return x_; } + Type y() const { return y_; } + + void SetPoint(Type x, Type y) { + x_ = x; + y_ = y; + } + + void set_x(Type x) { x_ = x; } + void set_y(Type y) { y_ = y; } + + void Offset(Type delta_x, Type delta_y) { + x_ += delta_x; + y_ += delta_y; + } + + Class Add(const Class& other) const{ + const Class* orig = static_cast<const Class*>(this); + Class copy = *orig; + copy.Offset(other.x_, other.y_); + return copy; + } + + Class Subtract(const Class& other) const { + const Class* orig = static_cast<const Class*>(this); + Class copy = *orig; + copy.Offset(-other.x_, -other.y_); + return copy; + } + + Class Middle(const Class& other) const { + return Class((x_ + other.x_) / 2, (y_ + other.y_) / 2); + } + + bool operator==(const Class& rhs) const { + return x_ == rhs.x_ && y_ == rhs.y_; + } + + bool operator!=(const Class& rhs) const { + return !(*this == rhs); + } + + // A point is less than another point if its y-value is closer + // to the origin. If the y-values are the same, then point with + // the x-value closer to the origin is considered less than the + // other. + // This comparison is required to use Point in sets, or sorted + // vectors. + bool operator<(const Class& rhs) const { + return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); + } + + protected: + PointBase(Type x, Type y); + virtual ~PointBase() {} + + private: + Type x_; + Type y_; +}; + +} // namespace gfx + +#endif // UI_GFX_POINT_BASE_H_ diff --git a/ui/gfx/point_f.cc b/ui/gfx/point_f.cc index 061e019..d7ba8c9 100644 --- a/ui/gfx/point_f.cc +++ b/ui/gfx/point_f.cc @@ -4,23 +4,30 @@ #include "ui/gfx/point_f.h" +#include <cmath> + #include "base/stringprintf.h" #include "ui/gfx/point.h" namespace gfx { -PointF::PointF() : x_(0), y_(0) { +template<> +PointBase<Point, float>::PointBase(float x, float y) : x_(x), y_(y) { +} + +PointF::PointF() : PointBase<PointF, float>(0, 0) { } -PointF::PointF(float x, float y) : x_(x), y_(y) { +PointF::PointF(float x, float y) : PointBase<PointF, float>(x, y) { } Point PointF::ToPoint() const { - return Point(static_cast<int>(x_), static_cast<int>(y_)); + return Point(static_cast<int>(std::floor(x())), + static_cast<int>(std::floor(y()))); } std::string PointF::ToString() const { - return base::StringPrintf("%f,%f", x_, y_); + return base::StringPrintf("%f,%f", x(), y()); } } // namespace gfx diff --git a/ui/gfx/point_f.h b/ui/gfx/point_f.h index 877195b..5d29c48 100644 --- a/ui/gfx/point_f.h +++ b/ui/gfx/point_f.h @@ -8,8 +8,8 @@ #include <string> -#include "build/build_config.h" #include "ui/base/ui_export.h" +#include "ui/gfx/point_base.h" #if !defined(ENABLE_DIP) #error "This class should be used only when DIP feature is enabled" @@ -18,74 +18,18 @@ namespace gfx { class Point; -// A floating versino of gfx::Point. This is copied, instead of using -// template, to avoid conflict with m19 branch. -// TODO(oshima): Merge this to ui/gfx/point.h using template. -class UI_EXPORT PointF { +// A floating version of gfx::Point. +class UI_EXPORT PointF : public PointBase<PointF, float> { public: PointF(); PointF(float x, float y); - PointF(Point& point); - ~PointF() {} - - float x() const { return x_; } - float y() const { return y_; } - - void SetPoint(float x, float y) { - x_ = x; - y_ = y; - } - - void set_x(float x) { x_ = x; } - void set_y(float y) { y_ = y; } - - void Offset(float delta_x, float delta_y) { - x_ += delta_x; - y_ += delta_y; - } - - PointF Add(const PointF& other) const{ - PointF copy = *this; - copy.Offset(other.x_, other.y_); - return copy; - } - - PointF Subtract(const PointF& other) const { - PointF copy = *this; - copy.Offset(-other.x_, -other.y_); - return copy; - } - - PointF Middle(const PointF& other) const { - return PointF((x_ + other.x_) / 2, (y_ + other.y_) / 2); - } - - bool operator==(const PointF& rhs) const { - return x_ == rhs.x_ && y_ == rhs.y_; - } - - bool operator!=(const PointF& rhs) const { - return !(*this == rhs); - } - - // A point is less than another point if its y-value is closer - // to the origin. If the y-values are the same, then point with - // the x-value closer to the origin is considered less than the - // other. - // This comparison is required to use Points in sets, or sorted - // vectors. - bool operator<(const PointF& rhs) const { - return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); - } + explicit PointF(Point& point); + virtual ~PointF() {} Point ToPoint() const; // Returns a string representation of point. std::string ToString() const; - - private: - float x_; - float y_; }; } // namespace gfx diff --git a/ui/gfx/rect.cc b/ui/gfx/rect.cc index 7a3f347..487b648 100644 --- a/ui/gfx/rect.cc +++ b/ui/gfx/rect.cc @@ -15,119 +15,79 @@ #include "base/logging.h" #include "base/stringprintf.h" #include "ui/gfx/insets.h" +#include "ui/gfx/rect_base_impl.h" -namespace { - -void AdjustAlongAxis(int dst_origin, int dst_size, int* origin, int* size) { - *size = std::min(dst_size, *size); - if (*origin < dst_origin) - *origin = dst_origin; - else - *origin = std::min(dst_origin + dst_size, *origin + *size) - *size; -} +namespace gfx { -} // namespace +template class RectBase<Rect, Point, Size, Insets, int>; -namespace gfx { +typedef class RectBase<Rect, Point, Size, Insets, int> RectBaseT; -Rect::Rect() { +Rect::Rect() : RectBaseT(gfx::Point()) { } Rect::Rect(int width, int height) - : size_(width, height) { + : RectBaseT(gfx::Size(width, height)) { } Rect::Rect(int x, int y, int width, int height) - : origin_(x, y), size_(width, height) { + : RectBaseT(gfx::Point(x, y), gfx::Size(width, height)) { } Rect::Rect(const gfx::Size& size) - : size_(size) { + : RectBaseT(size) { } Rect::Rect(const gfx::Point& origin, const gfx::Size& size) - : origin_(origin), size_(size) { + : RectBaseT(origin, size) { } Rect::~Rect() {} #if defined(OS_WIN) Rect::Rect(const RECT& r) - : origin_(r.left, r.top) { + : RectBaseT(gfx::Point(r.left, r.top)) { set_width(std::abs(r.right - r.left)); set_height(std::abs(r.bottom - r.top)); } Rect& Rect::operator=(const RECT& r) { - origin_.SetPoint(r.left, r.top); + set_x(r.left); + set_y(r.top); set_width(std::abs(r.right - r.left)); set_height(std::abs(r.bottom - r.top)); return *this; } #elif defined(OS_MACOSX) Rect::Rect(const CGRect& r) - : origin_(r.origin.x, r.origin.y) { + : RectBaseT(gfx::Point(r.origin.x, r.origin.y)) { set_width(r.size.width); set_height(r.size.height); } Rect& Rect::operator=(const CGRect& r) { - origin_.SetPoint(r.origin.x, r.origin.y); + set_x(r.origin.x); + set_y(r.origin.y); set_width(r.size.width); set_height(r.size.height); return *this; } #elif defined(TOOLKIT_GTK) Rect::Rect(const GdkRectangle& r) - : origin_(r.x, r.y) { + : RectBaseT(gfx::Point(r.x, r.y)) { set_width(r.width); set_height(r.height); } Rect& Rect::operator=(const GdkRectangle& r) { - origin_.SetPoint(r.x, r.y); + set_x(r.x); + set_y(r.y); set_width(r.width); set_height(r.height); return *this; } #endif -void Rect::SetRect(int x, int y, int width, int height) { - origin_.SetPoint(x, y); - set_width(width); - set_height(height); -} - -void Rect::Inset(const gfx::Insets& insets) { - Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); -} - -void Rect::Inset(int left, int top, int right, int bottom) { - Offset(left, top); - set_width(std::max(width() - left - right, 0)); - set_height(std::max(height() - top - bottom, 0)); -} - -void Rect::Offset(int horizontal, int vertical) { - origin_.Offset(horizontal, vertical); -} - -bool Rect::operator==(const Rect& other) const { - return origin_ == other.origin_ && size_ == other.size_; -} - -bool Rect::operator<(const Rect& other) const { - if (origin_ == other.origin_) { - if (width() == other.width()) { - return height() < other.height(); - } else { - return width() < other.width(); - } - } else { - return origin_ < other.origin_; - } -} - #if defined(OS_WIN) RECT Rect::ToRECT() const { RECT r; @@ -148,122 +108,10 @@ GdkRectangle Rect::ToGdkRectangle() const { } #endif -bool Rect::Contains(int point_x, int point_y) const { - return (point_x >= x()) && (point_x < right()) && - (point_y >= y()) && (point_y < bottom()); -} - -bool Rect::Contains(const Rect& rect) const { - return (rect.x() >= x() && rect.right() <= right() && - rect.y() >= y() && rect.bottom() <= bottom()); -} - -bool Rect::Intersects(const Rect& rect) const { - return !(rect.x() >= right() || rect.right() <= x() || - rect.y() >= bottom() || rect.bottom() <= y()); -} - -Rect Rect::Intersect(const Rect& rect) const { - int rx = std::max(x(), rect.x()); - int ry = std::max(y(), rect.y()); - int rr = std::min(right(), rect.right()); - int rb = std::min(bottom(), rect.bottom()); - - if (rx >= rr || ry >= rb) - rx = ry = rr = rb = 0; // non-intersecting - - return Rect(rx, ry, rr - rx, rb - ry); -} - -Rect Rect::Union(const Rect& rect) const { - // special case empty rects... - if (IsEmpty()) - return rect; - if (rect.IsEmpty()) - return *this; - - int rx = std::min(x(), rect.x()); - int ry = std::min(y(), rect.y()); - int rr = std::max(right(), rect.right()); - int rb = std::max(bottom(), rect.bottom()); - - return Rect(rx, ry, rr - rx, rb - ry); -} - -Rect Rect::Subtract(const Rect& rect) const { - // boundary cases: - if (!Intersects(rect)) - return *this; - if (rect.Contains(*this)) - return Rect(); - - int rx = x(); - int ry = y(); - int rr = right(); - int rb = bottom(); - - if (rect.y() <= y() && rect.bottom() >= bottom()) { - // complete intersection in the y-direction - if (rect.x() <= x()) { - rx = rect.right(); - } else { - rr = rect.x(); - } - } else if (rect.x() <= x() && rect.right() >= right()) { - // complete intersection in the x-direction - if (rect.y() <= y()) { - ry = rect.bottom(); - } else { - rb = rect.y(); - } - } - return Rect(rx, ry, rr - rx, rb - ry); -} - -Rect Rect::AdjustToFit(const Rect& rect) const { - int new_x = x(); - int new_y = y(); - int new_width = width(); - int new_height = height(); - AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); - AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); - return Rect(new_x, new_y, new_width, new_height); -} - -Point Rect::CenterPoint() const { - return Point(x() + (width() - 1) / 2, y() + (height() - 1) / 2); -} - -Rect Rect::Center(const gfx::Size& size) const { - int new_width = std::min(width(), size.width()); - int new_height = std::min(height(), size.height()); - int new_x = x() + (width() - new_width) / 2; - int new_y = y() + (height() - new_height) / 2; - 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())) || - (x() == rect.x() && width() == rect.width() && - (y() == rect.bottom() || bottom() == rect.y())); -} - std::string Rect::ToString() const { return base::StringPrintf("%s %s", - origin_.ToString().c_str(), - size_.ToString().c_str()); + origin().ToString().c_str(), + size().ToString().c_str()); } } // namespace gfx diff --git a/ui/gfx/rect.h b/ui/gfx/rect.h index 4343825..804754b 100644 --- a/ui/gfx/rect.h +++ b/ui/gfx/rect.h @@ -16,6 +16,7 @@ #include <string> #include "ui/gfx/point.h" +#include "ui/gfx/rect_base.h" #include "ui/gfx/size.h" #if defined(OS_WIN) @@ -28,7 +29,7 @@ namespace gfx { class Insets; -class UI_EXPORT Rect { +class UI_EXPORT Rect : public RectBase<Rect, Point, Size, Insets, int> { public: Rect(); Rect(int width, int height); @@ -43,7 +44,7 @@ class UI_EXPORT Rect { explicit Rect(const gfx::Size& size); Rect(const gfx::Point& origin, const gfx::Size& size); - ~Rect(); + virtual ~Rect(); #if defined(OS_WIN) Rect& operator=(const RECT& r); @@ -53,63 +54,6 @@ class UI_EXPORT Rect { Rect& operator=(const GdkRectangle& r); #endif - int x() const { return origin_.x(); } - void set_x(int x) { origin_.set_x(x); } - - int y() const { return origin_.y(); } - void set_y(int y) { origin_.set_y(y); } - - int width() const { return size_.width(); } - void set_width(int width) { size_.set_width(width); } - - int height() const { return size_.height(); } - void set_height(int height) { size_.set_height(height); } - - const gfx::Point& origin() const { return origin_; } - void set_origin(const gfx::Point& origin) { origin_ = origin; } - - const gfx::Size& size() const { return size_; } - void set_size(const gfx::Size& size) { size_ = size; } - - int right() const { return x() + width(); } - int bottom() const { return y() + height(); } - - void SetRect(int x, int y, int width, int height); - - // Shrink the rectangle by a horizontal and vertical distance on all sides. - void Inset(int horizontal, int vertical) { - Inset(horizontal, vertical, horizontal, vertical); - } - - // Shrink the rectangle by the given insets. - void Inset(const gfx::Insets& insets); - - // Shrink the rectangle by the specified amount on each side. - void Inset(int left, int top, int right, int bottom); - - // Move the rectangle by a horizontal and vertical distance. - void Offset(int horizontal, int vertical); - void Offset(const gfx::Point& point) { - Offset(point.x(), point.y()); - } - - // Returns true if the area of the rectangle is zero. - bool IsEmpty() const { return size_.IsEmpty(); } - - bool operator==(const Rect& other) const; - - bool operator!=(const Rect& other) const { - return !(*this == other); - } - - // A rect is less than another rect if its origin is less than - // the other rect's origin. If the origins are equal, then the - // shortest rect is less than the other. If the origin and the - // height are equal, then the narrowest rect is less than. - // This comparison is required to use Rects in sets, or sorted - // vectors. - bool operator<(const Rect& other) const; - #if defined(OS_WIN) // Construct an equivalent Win32 RECT object. RECT ToRECT() const; @@ -120,66 +64,7 @@ class UI_EXPORT Rect { CGRect ToCGRect() const; #endif - // Returns true if the point identified by point_x and point_y falls inside - // this rectangle. The point (x, y) is inside the rectangle, but the - // point (x + width, y + height) is not. - bool Contains(int point_x, int point_y) const; - - // Returns true if the specified point is contained by this rectangle. - bool Contains(const gfx::Point& point) const { - return Contains(point.x(), point.y()); - } - - // Returns true if this rectangle contains the specified rectangle. - bool Contains(const Rect& rect) const; - - // Returns true if this rectangle intersects the specified rectangle. - bool Intersects(const Rect& rect) const; - - // Computes the intersection of this rectangle with the given rectangle. - Rect Intersect(const Rect& rect) const; - - // Computes the union of this rectangle with the given rectangle. The union - // is the smallest rectangle containing both rectangles. - Rect Union(const Rect& rect) const; - - // Computes the rectangle resulting from subtracting |rect| from |this|. If - // |rect| does not intersect completely in either the x- or y-direction, then - // |*this| is returned. If |rect| contains |this|, then an empty Rect is - // returned. - Rect Subtract(const Rect& rect) const; - - // Returns true if this rectangle equals that of the supplied rectangle. - bool Equals(const Rect& rect) const { - return *this == rect; - } - - // Fits as much of the receiving rectangle into the supplied rectangle as - // possible, returning the result. For example, if the receiver had - // a x-location of 2 and a width of 4, and the supplied rectangle had - // an x-location of 0 with a width of 5, the returned rectangle would have - // an x-location of 1 with a width of 4. - Rect AdjustToFit(const Rect& rect) const; - - // Returns the center of this rectangle. - Point CenterPoint() const; - - // Return a rectangle that has the same center point but with a size capped - // 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; - std::string ToString() const; - - private: - gfx::Point origin_; - gfx::Size size_; }; } // namespace gfx diff --git a/ui/gfx/rect_base.h b/ui/gfx/rect_base.h new file mode 100644 index 0000000..3ccd28d --- /dev/null +++ b/ui/gfx/rect_base.h @@ -0,0 +1,152 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// A template for a simple rectangle class. The containment semantics +// are array-like; that is, the coordinate (x, y) is considered to be +// contained by the rectangle, but the coordinate (x + width, y) is not. +// The class will happily let you create malformed rectangles (that is, +// rectangles with negative width and/or height), but there will be assertions +// in the operations (such as Contains()) to complain in this case. + +#ifndef UI_GFX_RECT_BASE_H_ +#define UI_GFX_RECT_BASE_H_ +#pragma once + +#include <string> + +namespace gfx { + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +class UI_EXPORT RectBase { + public: + Type x() const { return origin_.x(); } + void set_x(Type x) { origin_.set_x(x); } + + Type y() const { return origin_.y(); } + void set_y(Type y) { origin_.set_y(y); } + + Type width() const { return size_.width(); } + void set_width(Type width) { size_.set_width(width); } + + Type height() const { return size_.height(); } + void set_height(Type height) { size_.set_height(height); } + + const PointClass& origin() const { return origin_; } + void set_origin(const PointClass& origin) { origin_ = origin; } + + const SizeClass& size() const { return size_; } + void set_size(const SizeClass& size) { size_ = size; } + + Type right() const { return x() + width(); } + Type bottom() const { return y() + height(); } + + void SetRect(Type x, Type y, Type width, Type height); + + // Shrink the rectangle by a horizontal and vertical distance on all sides. + void Inset(Type horizontal, Type vertical) { + Inset(horizontal, vertical, horizontal, vertical); + } + + // Shrink the rectangle by the given insets. + void Inset(const InsetsClass& insets); + + // Shrink the rectangle by the specified amount on each side. + void Inset(Type left, Type top, Type right, Type bottom); + + // Move the rectangle by a horizontal and vertical distance. + void Offset(Type horizontal, Type vertical); + void Offset(const PointClass& point) { + Offset(point.x(), point.y()); + } + + // Returns true if the area of the rectangle is zero. + bool IsEmpty() const { return size_.IsEmpty(); } + + bool operator==(const Class& other) const; + + bool operator!=(const Class& other) const { + return !(*this == other); + } + + // A rect is less than another rect if its origin is less than + // the other rect's origin. If the origins are equal, then the + // shortest rect is less than the other. If the origin and the + // height are equal, then the narrowest rect is less than. + // This comparison is required to use Rects in sets, or sorted + // vectors. + bool operator<(const Class& other) const; + + // Returns true if the point identified by point_x and point_y falls inside + // this rectangle. The point (x, y) is inside the rectangle, but the + // point (x + width, y + height) is not. + bool Contains(Type point_x, Type point_y) const; + + // Returns true if the specified point is contained by this rectangle. + bool Contains(const PointClass& point) const { + return Contains(point.x(), point.y()); + } + + // Returns true if this rectangle contains the specified rectangle. + bool Contains(const Class& rect) const; + + // Returns true if this rectangle intersects the specified rectangle. + bool Intersects(const Class& rect) const; + + // Computes the intersection of this rectangle with the given rectangle. + Class Intersect(const Class& rect) const; + + // Computes the union of this rectangle with the given rectangle. The union + // is the smallest rectangle containing both rectangles. + Class Union(const Class& rect) const; + + // Computes the rectangle resulting from subtracting |rect| from |this|. If + // |rect| does not intersect completely in either the x- or y-direction, then + // |*this| is returned. If |rect| contains |this|, then an empty Rect is + // returned. + Class Subtract(const Class& rect) const; + + // Returns true if this rectangle equals that of the supplied rectangle. + bool Equals(const Class& rect) const { + return *this == rect; + } + + // Fits as much of the receiving rectangle into the supplied rectangle as + // possible, returning the result. For example, if the receiver had + // a x-location of 2 and a width of 4, and the supplied rectangle had + // an x-location of 0 with a width of 5, the returned rectangle would have + // an x-location of 1 with a width of 4. + Class AdjustToFit(const Class& rect) const; + + // Returns the center of this rectangle. + PointClass CenterPoint() const; + + // Return a rectangle that has the same center point but with a size capped + // at given |size|. + Class Center(const SizeClass& size) const; + + // Splits |this| in two halves, |left_half| and |right_half|. + void SplitVertically(Class* left_half, Class* 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 Class& rect) const; + + protected: + RectBase(const PointClass& origin, const SizeClass& size); + explicit RectBase(const SizeClass& size); + explicit RectBase(const PointClass& origin); + virtual ~RectBase(); + + private: + PointClass origin_; + SizeClass size_; +}; + +} // namespace gfx + +#endif // UI_GFX_RECT_BASE_H_ diff --git a/ui/gfx/rect_base_impl.h b/ui/gfx/rect_base_impl.h new file mode 100644 index 0000000..84caa76 --- /dev/null +++ b/ui/gfx/rect_base_impl.h @@ -0,0 +1,318 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/rect_base.h" + +#include "base/logging.h" +#include "base/stringprintf.h" + +// This file provides the implementation for RectBaese template and +// used to instantiate the base class for Rect and RectF classes. +#if !defined(UI_IMPLEMENTATION) +#error "This file is intended for UI implementation only" +#endif + +namespace { + +template<typename Type> +void AdjustAlongAxis(Type dst_origin, Type dst_size, Type* origin, Type* size) { + *size = std::min(dst_size, *size); + if (*origin < dst_origin) + *origin = dst_origin; + else + *origin = std::min(dst_origin + dst_size, *origin + *size) - *size; +} + +} // namespace + +namespace gfx { + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( + const PointClass& origin, const SizeClass& size) + : origin_(origin), size_(size) { +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( + const SizeClass& size) + : size_(size) { +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( + const PointClass& origin) + : origin_(origin) { +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::~RectBase() {} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SetRect( + Type x, Type y, Type width, Type height) { + origin_.SetPoint(x, y); + set_width(width); + set_height(height); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Inset( + const InsetsClass& insets) { + Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Inset( + Type left, Type top, Type right, Type bottom) { + Offset(left, top); + set_width(std::max(width() - left - right, static_cast<Type>(0))); + set_height(std::max(height() - top - bottom, static_cast<Type>(0))); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Offset( + Type horizontal, Type vertical) { + origin_.Offset(horizontal, vertical); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::operator==( + const Class& other) const { + return origin_ == other.origin_ && size_ == other.size_; +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::operator<( + const Class& other) const { + if (origin_ == other.origin_) { + if (width() == other.width()) { + return height() < other.height(); + } else { + return width() < other.width(); + } + } else { + return origin_ < other.origin_; + } +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Contains( + Type point_x, Type point_y) const { + return (point_x >= x()) && (point_x < right()) && + (point_y >= y()) && (point_y < bottom()); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Contains( + const Class& rect) const { + return (rect.x() >= x() && rect.right() <= right() && + rect.y() >= y() && rect.bottom() <= bottom()); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersects( + const Class& rect) const { + return !(rect.x() >= right() || rect.right() <= x() || + rect.y() >= bottom() || rect.bottom() <= y()); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( + const Class& rect) const { + Type rx = std::max(x(), rect.x()); + Type ry = std::max(y(), rect.y()); + Type rr = std::min(right(), rect.right()); + Type rb = std::min(bottom(), rect.bottom()); + + if (rx >= rr || ry >= rb) + rx = ry = rr = rb = 0; // non-intersecting + + return Class(rx, ry, rr - rx, rb - ry); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union( + const Class& rect) const { + // special case empty rects... + if (IsEmpty()) + return rect; + if (rect.IsEmpty()) + return *static_cast<const Class*>(this); + + Type rx = std::min(x(), rect.x()); + Type ry = std::min(y(), rect.y()); + Type rr = std::max(right(), rect.right()); + Type rb = std::max(bottom(), rect.bottom()); + + return Class(rx, ry, rr - rx, rb - ry); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( + const Class& rect) const { + // boundary cases: + if (!Intersects(rect)) + return *static_cast<const Class*>(this); + if (rect.Contains(*static_cast<const Class*>(this))) + return Class(); + + Type rx = x(); + Type ry = y(); + Type rr = right(); + Type rb = bottom(); + + if (rect.y() <= y() && rect.bottom() >= bottom()) { + // complete intersection in the y-direction + if (rect.x() <= x()) { + rx = rect.right(); + } else { + rr = rect.x(); + } + } else if (rect.x() <= x() && rect.right() >= right()) { + // complete intersection in the x-direction + if (rect.y() <= y()) { + ry = rect.bottom(); + } else { + rb = rect.y(); + } + } + return Class(rx, ry, rr - rx, rb - ry); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( + const Class& rect) const { + Type new_x = x(); + Type new_y = y(); + Type new_width = width(); + Type new_height = height(); + AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); + AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); + return Class(new_x, new_y, new_width, new_height); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: + CenterPoint() const { + return PointClass(x() + (width() - 1) / 2, y() + (height() - 1) / 2); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Center( + const SizeClass& size) const { + Type new_width = std::min(width(), size.width()); + Type new_height = std::min(height(), size.height()); + Type new_x = x() + (width() - new_width) / 2; + Type new_y = y() + (height() - new_height) / 2; + return Class(new_x, new_y, new_width, new_height); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically( + Class* left_half, Class* 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()); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename Type> +bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith( + const Class& rect) const { + return (y() == rect.y() && height() == rect.height() && + (x() == rect.right() || right() == rect.x())) || + (x() == rect.x() && width() == rect.width() && + (y() == rect.bottom() || bottom() == rect.y())); +} + +} // namespace gfx diff --git a/ui/gfx/rect_f.cc b/ui/gfx/rect_f.cc index d6bbe28..6677c29 100644 --- a/ui/gfx/rect_f.cc +++ b/ui/gfx/rect_f.cc @@ -7,198 +7,43 @@ #include "base/logging.h" #include "base/stringprintf.h" #include "ui/gfx/insets_f.h" +#include "ui/gfx/rect_base_impl.h" -namespace { - -void AdjustAlongAxis(float dst_origin, - float dst_size, - float* origin, - float* size) { - *size = std::min(dst_size, *size); - if (*origin < dst_origin) - *origin = dst_origin; - else - *origin = std::min(dst_origin + dst_size, *origin + *size) - *size; -} +namespace gfx { -} // namespace +template class RectBase<RectF, PointF, SizeF, InsetsF, float>; -namespace gfx { +typedef class RectBase<RectF, PointF, SizeF, InsetsF, float> RectBaseT; -RectF::RectF() { +RectF::RectF() : RectBaseT(gfx::SizeF()) { } RectF::RectF(float width, float height) - : size_(width, height) { + : RectBaseT(gfx::SizeF(width, height)) { } RectF::RectF(float x, float y, float width, float height) - : origin_(x, y), size_(width, height) { + : RectBaseT(gfx::PointF(x, y), gfx::SizeF(width, height)) { } RectF::RectF(const gfx::SizeF& size) - : size_(size) { + : RectBaseT(size) { } RectF::RectF(const gfx::PointF& origin, const gfx::SizeF& size) - : origin_(origin), size_(size) { + : RectBaseT(origin, size) { } RectF::~RectF() {} -void RectF::SetRect(float x, float y, float width, float height) { - origin_.SetPoint(x, y); - set_width(width); - set_height(height); -} - -void RectF::Inset(const gfx::InsetsF& insets) { - Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); -} - -void RectF::Inset(float left, float top, float right, float bottom) { - Offset(left, top); - set_width(std::max(width() - left - right, 0.0f)); - set_height(std::max(height() - top - bottom, 0.0f)); -} - -void RectF::Offset(float horizontal, float vertical) { - origin_.Offset(horizontal, vertical); -} - -bool RectF::operator==(const RectF& other) const { - return origin_ == other.origin_ && size_ == other.size_; -} - -bool RectF::operator<(const RectF& other) const { - if (origin_ == other.origin_) { - if (width() == other.width()) { - return height() < other.height(); - } else { - return width() < other.width(); - } - } else { - return origin_ < other.origin_; - } -} - -bool RectF::Contains(float point_x, float point_y) const { - return (point_x >= x()) && (point_x < right()) && - (point_y >= y()) && (point_y < bottom()); -} - -bool RectF::Contains(const RectF& rect) const { - return (rect.x() >= x() && rect.right() <= right() && - rect.y() >= y() && rect.bottom() <= bottom()); -} - -bool RectF::Intersects(const RectF& rect) const { - return !(rect.x() >= right() || rect.right() <= x() || - rect.y() >= bottom() || rect.bottom() <= y()); -} - -RectF RectF::Intersect(const RectF& rect) const { - float rx = std::max(x(), rect.x()); - float ry = std::max(y(), rect.y()); - float rr = std::min(right(), rect.right()); - float rb = std::min(bottom(), rect.bottom()); - - if (rx >= rr || ry >= rb) - rx = ry = rr = rb = 0; // non-intersecting - - return RectF(rx, ry, rr - rx, rb - ry); -} - -RectF RectF::Union(const RectF& rect) const { - // special case empty rects... - if (IsEmpty()) - return rect; - if (rect.IsEmpty()) - return *this; - - float rx = std::min(x(), rect.x()); - float ry = std::min(y(), rect.y()); - float rr = std::max(right(), rect.right()); - float rb = std::max(bottom(), rect.bottom()); - - return RectF(rx, ry, rr - rx, rb - ry); -} - -RectF RectF::Subtract(const RectF& rect) const { - // boundary cases: - if (!Intersects(rect)) - return *this; - if (rect.Contains(*this)) - return RectF(); - - float rx = x(); - float ry = y(); - float rr = right(); - float rb = bottom(); - - if (rect.y() <= y() && rect.bottom() >= bottom()) { - // complete intersection in the y-direction - if (rect.x() <= x()) { - rx = rect.right(); - } else { - rr = rect.x(); - } - } else if (rect.x() <= x() && rect.right() >= right()) { - // complete intersection in the x-direction - if (rect.y() <= y()) { - ry = rect.bottom(); - } else { - rb = rect.y(); - } - } - return RectF(rx, ry, rr - rx, rb - ry); -} - -RectF RectF::AdjustToFit(const RectF& rect) const { - float new_x = x(); - float new_y = y(); - float new_width = width(); - float new_height = height(); - AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); - AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); - return RectF(new_x, new_y, new_width, new_height); -} - -PointF RectF::CenterPoint() const { - return PointF(x() + (width() - 1) / 2, y() + (height() - 1) / 2); -} - -RectF RectF::Center(const gfx::SizeF& size) const { - float new_width = std::min(width(), size.width()); - float new_height = std::min(height(), size.height()); - float new_x = x() + (width() - new_width) / 2; - float new_y = y() + (height() - new_height) / 2; - return RectF(new_x, new_y, new_width, new_height); -} - -void RectF::SplitVertically(gfx::RectF* left_half, - gfx::RectF* 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 RectF::SharesEdgeWith(const gfx::RectF& rect) const { - return (y() == rect.y() && height() == rect.height() && - (x() == rect.right() || right() == rect.x())) || - (x() == rect.x() && width() == rect.width() && - (y() == rect.bottom() || bottom() == rect.y())); +Rect RectF::ToRect() const { + return Rect(origin().ToPoint(), size().ToSize()); } std::string RectF::ToString() const { return base::StringPrintf("%s %s", - origin_.ToString().c_str(), - size_.ToString().c_str()); + origin().ToString().c_str(), + size().ToString().c_str()); } } // namespace gfx diff --git a/ui/gfx/rect_f.h b/ui/gfx/rect_f.h index fcf6c09..bb6e814 100644 --- a/ui/gfx/rect_f.h +++ b/ui/gfx/rect_f.h @@ -9,6 +9,8 @@ #include <string> #include "ui/gfx/point_f.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/rect_base.h" #include "ui/gfx/size_f.h" #if !defined(ENABLE_DIP) @@ -19,10 +21,8 @@ namespace gfx { class InsetsF; -// A floating versino of gfx::Rect. This is copied, instead of using -// template, to avoid conflict with m19 branch. -// TODO(oshima): Merge this to ui/gfx/rect.h using template. -class UI_EXPORT RectF { +// A floating version of gfx::Rect. +class UI_EXPORT RectF : public RectBase<RectF, PointF, SizeF, InsetsF, float> { public: RectF(); RectF(float width, float height); @@ -30,123 +30,11 @@ class UI_EXPORT RectF { explicit RectF(const gfx::SizeF& size); RectF(const gfx::PointF& origin, const gfx::SizeF& size); - ~RectF(); + virtual ~RectF(); - float x() const { return origin_.x(); } - void set_x(float x) { origin_.set_x(x); } - - float y() const { return origin_.y(); } - void set_y(float y) { origin_.set_y(y); } - - float width() const { return size_.width(); } - void set_width(float width) { size_.set_width(width); } - - float height() const { return size_.height(); } - void set_height(float height) { size_.set_height(height); } - - const gfx::PointF& origin() const { return origin_; } - void set_origin(const gfx::PointF& origin) { origin_ = origin; } - - const gfx::SizeF& size() const { return size_; } - void set_size(const gfx::SizeF& size) { size_ = size; } - - float right() const { return x() + width(); } - float bottom() const { return y() + height(); } - - void SetRect(float x, float y, float width, float height); - - void Inset(float horizontal, float vertical) { - Inset(horizontal, vertical, horizontal, vertical); - } - // Shrink the rectangle by the given insets. - void Inset(const gfx::InsetsF& insets); - - // Shrink the rectangle by the specified amount on each side. - void Inset(float left, float top, float right, float bottom); - - // Move the rectangle by a horizontal and vertical distance. - void Offset(float horizontal, float vertical); - void Offset(const gfx::PointF& point) { - Offset(point.x(), point.y()); - } - - // Returns true if the area of the rectangle is zero. - bool IsEmpty() const { return size_.IsEmpty(); } - - bool operator==(const RectF& other) const; - - bool operator!=(const RectF& other) const { - return !(*this == other); - } - - // A rect is less than another rect if its origin is less than - // the other rect's origin. If the origins are equal, then the - // shortest rect is less than the other. If the origin and the - // height are equal, then the narrowest rect is less than. - // This comparison is required to use Rects in sets, or sorted - // vectors. - bool operator<(const RectF& other) const; - - // Returns true if the point identified by point_x and point_y falls inside - // this rectangle. The point (x, y) is inside the rectangle, but the - // point (x + width, y + height) is not. - bool Contains(float point_x, float point_y) const; - - // Returns true if the specified point is contained by this rectangle. - bool Contains(const gfx::PointF& point) const { - return Contains(point.x(), point.y()); - } - - // Returns true if this rectangle contains the specified rectangle. - bool Contains(const RectF& rect) const; - - // Returns true if this rectangle intersects the specified rectangle. - bool Intersects(const RectF& rect) const; - - // Computes the intersection of this rectangle with the given rectangle. - RectF Intersect(const RectF& rect) const; - - // Computes the union of this rectangle with the given rectangle. The union - // is the smallest rectangle containing both rectangles. - RectF Union(const RectF& rect) const; - - // Computes the rectangle resulting from subtracting |rect| from |this|. If - // |rect| does not intersect completely in either the x- or y-direction, then - // |*this| is returned. If |rect| contains |this|, then an empty Rect is - // returned. - RectF Subtract(const RectF& rect) const; - - // Returns true if this rectangle equals that of the supplied rectangle. - bool Equals(const RectF& rect) const { - return *this == rect; - } - - // Fits as much of the receiving rectangle into the supplied rectangle as - // possible, returning the result. For example, if the receiver had - // a x-location of 2 and a width of 4, and the supplied rectangle had - // an x-location of 0 with a width of 5, the returned rectangle would have - // an x-location of 1 with a width of 4. - RectF AdjustToFit(const RectF& rect) const; - - // Returns the center of this rectangle. - PointF CenterPoint() const; - - // Return a rectangle that has the same center point but with a size capped - // at given |size|. - RectF Center(const gfx::SizeF& size) const; - - // Splits |this| in two halves, |left_half| and |right_half|. - void SplitVertically(gfx::RectF* left_half, gfx::RectF* 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::RectF& rect) const; + Rect ToRect() const; std::string ToString() const; - - private: - gfx::PointF origin_; - gfx::SizeF size_; }; } // namespace gfx diff --git a/ui/gfx/size.cc b/ui/gfx/size.cc index 0173e1b..303559d 100644 --- a/ui/gfx/size.cc +++ b/ui/gfx/size.cc @@ -12,18 +12,22 @@ #include "base/logging.h" #include "base/stringprintf.h" +#include "ui/gfx/size_base.h" +#include "ui/gfx/size_base_impl.h" namespace gfx { -Size::Size() : width_(0), height_(0) {} +template class SizeBase<Size, int>; -Size::Size(int width, int height) { +Size::Size() : SizeBase<Size, int>(0, 0) {} + +Size::Size(int width, int height) : SizeBase<Size, int>(0, 0) { set_width(width); set_height(height); } #if defined(OS_MACOSX) -Size::Size(const CGSize& s) { +Size::Size(const CGSize& s) : SizeBase<Size, int>(0, 0) { set_width(s.width); set_height(s.height); } @@ -40,34 +44,18 @@ Size::~Size() {} #if defined(OS_WIN) SIZE Size::ToSIZE() const { SIZE s; - s.cx = width_; - s.cy = height_; + s.cx = width(); + s.cy = height(); return s; } #elif defined(OS_MACOSX) CGSize Size::ToCGSize() const { - return CGSizeMake(width_, height_); + return CGSizeMake(width(), height()); } #endif -void Size::set_width(int width) { - if (width < 0) { - NOTREACHED() << "negative width:" << width; - width = 0; - } - width_ = width; -} - -void Size::set_height(int height) { - if (height < 0) { - NOTREACHED() << "negative height:" << height; - height = 0; - } - height_ = height; -} - std::string Size::ToString() const { - return base::StringPrintf("%dx%d", width_, height_); + return base::StringPrintf("%dx%d", width(), height()); } } // namespace gfx diff --git a/ui/gfx/size.h b/ui/gfx/size.h index 5e288fe..670e67e 100644 --- a/ui/gfx/size.h +++ b/ui/gfx/size.h @@ -10,6 +10,7 @@ #include "build/build_config.h" #include "ui/base/ui_export.h" +#include "ui/gfx/size_base.h" #if defined(OS_WIN) typedef struct tagSIZE SIZE; @@ -20,7 +21,7 @@ typedef struct tagSIZE SIZE; namespace gfx { // A size has width and height values. -class UI_EXPORT Size { +class UI_EXPORT Size : public SizeBase<Size, int> { public: Size(); Size(int width, int height); @@ -28,43 +29,12 @@ class UI_EXPORT Size { explicit Size(const CGSize& s); #endif - ~Size(); + virtual ~Size(); #if defined(OS_MACOSX) Size& operator=(const CGSize& s); #endif - int width() const { return width_; } - int height() const { return height_; } - - int GetArea() const { return width_ * height_; } - - void SetSize(int width, int height) { - set_width(width); - set_height(height); - } - - void Enlarge(int width, int height) { - set_width(width_ + width); - set_height(height_ + height); - } - - void set_width(int width); - void set_height(int height); - - bool operator==(const Size& s) const { - return width_ == s.width_ && height_ == s.height_; - } - - bool operator!=(const Size& s) const { - return !(*this == s); - } - - bool IsEmpty() const { - // Size doesn't allow negative dimensions, so testing for 0 is enough. - return (width_ == 0) || (height_ == 0); - } - #if defined(OS_WIN) SIZE ToSIZE() const; #elif defined(OS_MACOSX) @@ -72,10 +42,6 @@ class UI_EXPORT Size { #endif std::string ToString() const; - - private: - int width_; - int height_; }; } // namespace gfx diff --git a/ui/gfx/size_base.h b/ui/gfx/size_base.h new file mode 100644 index 0000000..1256f7f --- /dev/null +++ b/ui/gfx/size_base.h @@ -0,0 +1,62 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_GFX_SIZE_BASE_H_ +#define UI_GFX_SIZE_BASE_H_ +#pragma once + +#include <string> + +#include "build/build_config.h" +#include "ui/base/ui_export.h" + +namespace gfx { + +// A size has width and height values. +template<typename Class, typename Type> +class UI_EXPORT SizeBase { + public: + Type width() const { return width_; } + Type height() const { return height_; } + + Type GetArea() const { return width_ * height_; } + + void SetSize(Type width, Type height) { + set_width(width); + set_height(height); + } + + void Enlarge(Type width, Type height) { + set_width(width_ + width); + set_height(height_ + height); + } + + void set_width(Type width); + void set_height(Type height); + + bool operator==(const Class& s) const { + return width_ == s.width_ && height_ == s.height_; + } + + bool operator!=(const Class& s) const { + return !(*this == s); + } + + bool IsEmpty() const { + // Size doesn't allow negative dimensions, so testing for 0 is enough. + return (width_ == 0) || (height_ == 0); + } + + protected: + SizeBase(Type width, Type height); + virtual ~SizeBase(); + + private: + Type width_; + Type height_; +}; + +} // namespace gfx + +#endif // UI_GFX_SIZE_BASE_H_ diff --git a/ui/gfx/size_base_impl.h b/ui/gfx/size_base_impl.h new file mode 100644 index 0000000..4eca741 --- /dev/null +++ b/ui/gfx/size_base_impl.h @@ -0,0 +1,45 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/size_base.h" + +#include "base/logging.h" +#include "base/stringprintf.h" + +// This file provides the implementation for SizeBaese template and +// used to instantiate the base class for Size and SizeF classes. +#if !defined(UI_IMPLEMENTATION) +#error "This file is intended for UI implementation only" +#endif + +namespace gfx { + +template<typename Class, typename Type> +SizeBase<Class, Type>::SizeBase(Type width, Type height) { + set_width(width); + set_height(height); +} + +template<typename Class, typename Type> +SizeBase<Class, Type>::~SizeBase() {} + +template<typename Class, typename Type> +void SizeBase<Class, Type>::set_width(Type width) { + if (width < 0) { + NOTREACHED() << "negative width:" << width; + width = 0; + } + width_ = width; +} + +template<typename Class, typename Type> +void SizeBase<Class, Type>::set_height(Type height) { + if (height < 0) { + NOTREACHED() << "negative height:" << height; + height = 0; + } + height_ = height; +} + +} // namespace gfx diff --git a/ui/gfx/size_f.cc b/ui/gfx/size_f.cc index aeffdd3..53645eb 100644 --- a/ui/gfx/size_f.cc +++ b/ui/gfx/size_f.cc @@ -4,38 +4,33 @@ #include "ui/gfx/size_f.h" +#include <cmath> + #include "base/logging.h" #include "base/stringprintf.h" +#include "ui/gfx/size.h" +#include "ui/gfx/size_base_impl.h" namespace gfx { -SizeF::SizeF() : width_(0), height_(0) {} +template class SizeBase<SizeF, float>; + +SizeF::SizeF() : SizeBase<SizeF, float>(0, 0) {} -SizeF::SizeF(float width, float height) { +SizeF::SizeF(float width, float height) : SizeBase<SizeF, float>(0, 0) { set_width(width); set_height(height); } SizeF::~SizeF() {} -void SizeF::set_width(float width) { - if (width < 0) { - NOTREACHED() << "negative width:" << width; - width = 0; - } - width_ = width; -} - -void SizeF::set_height(float height) { - if (height < 0) { - NOTREACHED() << "negative height:" << height; - height = 0; - } - height_ = height; +Size SizeF::ToSize() const { + return Size(static_cast<int>(std::floor(width())), + static_cast<int>(std::floor(height()))); } std::string SizeF::ToString() const { - return base::StringPrintf("%fx%f", width_, height_); + return base::StringPrintf("%fx%f", width(), height()); } } // namespace gfx diff --git a/ui/gfx/size_f.h b/ui/gfx/size_f.h index 23b6f3b..733e368 100644 --- a/ui/gfx/size_f.h +++ b/ui/gfx/size_f.h @@ -8,8 +8,9 @@ #include <string> -#include "build/build_config.h" #include "ui/base/ui_export.h" +#include "ui/gfx/size.h" +#include "ui/gfx/size_base.h" #if !defined(ENABLE_DIP) #error "This class should be used only when DIP feature is enabled" @@ -17,51 +18,16 @@ namespace gfx { -// A floating versino of gfx::Size. This is copied, instead of using -// template, to avoid conflict with m19 branch. -// TODO(oshima): Merge this to ui/gfx/size.h using template. -class UI_EXPORT SizeF { +// A floating version of gfx::Size. +class UI_EXPORT SizeF : public SizeBase<SizeF, float> { public: SizeF(); SizeF(float width, float height); - ~SizeF(); + virtual ~SizeF(); - float width() const { return width_; } - float height() const { return height_; } - - float GetArea() const { return width_ * height_; } - - void SetSize(float width, float height) { - set_width(width); - set_height(height); - } - - void Enlarge(float width, float height) { - set_width(width_ + width); - set_height(height_ + height); - } - - void set_width(float width); - void set_height(float height); - - bool operator==(const SizeF& s) const { - return width_ == s.width_ && height_ == s.height_; - } - - bool operator!=(const SizeF& s) const { - return !(*this == s); - } - - bool IsEmpty() const { - // Size doesn't allow negative dimensions, so testing for 0 is enough. - return (width_ == 0) || (height_ == 0); - } + Size ToSize() const; std::string ToString() const; - - private: - float width_; - float height_; }; } // namespace gfx @@ -263,7 +263,6 @@ 'base/win/singleton_hwnd.h', 'base/win/window_impl.cc', 'base/win/window_impl.h', - 'base/work_area_watcher_observer.h', 'base/x/active_window_watcher_x.cc', 'base/x/active_window_watcher_x.h', 'base/x/active_window_watcher_x_observer.h', @@ -272,6 +271,7 @@ 'base/x/root_window_property_watcher_x.h', 'base/x/work_area_watcher_x.cc', 'base/x/work_area_watcher_x.h', + 'base/x/work_area_watcher_x_observer.h', 'base/x/x11_util.cc', 'base/x/x11_util.h', 'base/x/x11_util_internal.h', @@ -347,8 +347,11 @@ 'gfx/platform_font_win.cc', 'gfx/point.cc', 'gfx/point.h', + 'gfx/point_base.h', 'gfx/rect.cc', 'gfx/rect.h', + 'gfx/rect_base.h', + 'gfx/rect_base_impl.h', 'gfx/render_text.cc', 'gfx/render_text.h', 'gfx/render_text_linux.cc', @@ -372,6 +375,8 @@ 'gfx/shadow_value.h', 'gfx/size.cc', 'gfx/size.h', + 'gfx/size_base.h', + 'gfx/size_base_impl.h', 'gfx/skbitmap_operations.cc', 'gfx/skbitmap_operations.h', 'gfx/skia_util.cc', @@ -419,7 +424,6 @@ ['exclude', 'base/view_prop.h'], ['exclude', 'base/win/mouse_wheel_util.cc'], ['exclude', 'base/win/mouse_wheel_util.h'], - ['exclude', 'base/work_area_watcher_observer.h'], ['exclude', 'base/x/active_window_watcher_x.cc'], ['exclude', 'base/x/active_window_watcher_x.h'], ['exclude', 'base/x/active_window_watcher_x_observer.h'], @@ -427,6 +431,7 @@ ['exclude', 'base/x/root_window_property_watcher_x.h'], ['exclude', 'base/x/work_area_watcher_x.cc'], ['exclude', 'base/x/work_area_watcher_x.h'], + ['exclude', 'base/x/work_area_watcher_x_observer.h'], ['exclude', 'ui_controls_win.cc'], ], }, { # use_aura!=1 |