summaryrefslogtreecommitdiffstats
path: root/base/gfx
diff options
context:
space:
mode:
authoravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-05 18:04:07 +0000
committeravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-05 18:04:07 +0000
commit66d3a8e27c79e8d8cf6f3db37f2c20226b71c7d8 (patch)
treedd1744434e0bf229fdf63fd46c220d742cf19a60 /base/gfx
parent4d562a7e2538a1396a4dbeae66aea33f4f4afcd3 (diff)
downloadchromium_src-66d3a8e27c79e8d8cf6f3db37f2c20226b71c7d8.zip
chromium_src-66d3a8e27c79e8d8cf6f3db37f2c20226b71c7d8.tar.gz
chromium_src-66d3a8e27c79e8d8cf6f3db37f2c20226b71c7d8.tar.bz2
Fix basic geometric types.
Review URL: http://chrome-reviews.prom.corp.google.com/1106 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@381 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/gfx')
-rw-r--r--base/gfx/point.cc14
-rw-r--r--base/gfx/point.h12
-rw-r--r--base/gfx/rect.cc30
-rw-r--r--base/gfx/rect.h15
-rw-r--r--base/gfx/rect_unittest.cc14
-rw-r--r--base/gfx/size.h8
-rw-r--r--base/gfx/size_mac.cc22
7 files changed, 108 insertions, 7 deletions
diff --git a/base/gfx/point.cc b/base/gfx/point.cc
index 7c435a0..c8bf413 100644
--- a/base/gfx/point.cc
+++ b/base/gfx/point.cc
@@ -29,7 +29,9 @@
#include "base/gfx/point.h"
+#if defined(OS_WIN)
#include <windows.h>
+#endif
namespace gfx {
@@ -39,6 +41,7 @@ Point::Point() : x_(0), y_(0) {
Point::Point(int x, int y) : x_(x), y_(y) {
}
+#if defined(OS_WIN)
Point::Point(const POINT& point) : x_(point.x), y_(point.y) {
}
@@ -48,5 +51,16 @@ POINT Point::ToPOINT() const {
p.y = y_;
return p;
}
+#elif defined(OS_MACOSX)
+Point::Point(const CGPoint& point) : x_(point.x), y_(point.y) {
+}
+
+CGPoint Point::ToCGPoint() const {
+ CGPoint p;
+ p.x = x_;
+ p.y = y_;
+ return p;
+}
+#endif
} // namespace gfx
diff --git a/base/gfx/point.h b/base/gfx/point.h
index 3e09a81..1a41031 100644
--- a/base/gfx/point.h
+++ b/base/gfx/point.h
@@ -34,7 +34,11 @@
#include <iostream>
#endif
+#if defined(OS_WIN)
typedef struct tagPOINT POINT;
+#elif defined(OS_MACOSX)
+#import <ApplicationServices/ApplicationServices.h>
+#endif
namespace gfx {
@@ -45,7 +49,11 @@ class Point {
public:
Point();
Point(int x, int y);
+#if defined(OS_WIN)
explicit Point(const POINT& point);
+#elif defined(OS_MACOSX)
+ explicit Point(const CGPoint& point);
+#endif
~Point() {}
@@ -68,7 +76,11 @@ class Point {
return !(*this == rhs);
}
+#if defined(OS_WIN)
POINT ToPOINT() const;
+#elif defined(OS_MACOSX)
+ CGPoint ToCGPoint() const;
+#endif
private:
int x_;
diff --git a/base/gfx/rect.cc b/base/gfx/rect.cc
index dda7e67..f9a88d4 100644
--- a/base/gfx/rect.cc
+++ b/base/gfx/rect.cc
@@ -29,7 +29,11 @@
#include "base/gfx/rect.h"
+#if defined(OS_WIN)
#include <windows.h>
+#elif defined(OS_MACOSX)
+#import <CoreGraphics/CGGeometry.h>
+#endif
#include "base/logging.h"
@@ -63,6 +67,7 @@ Rect::Rect(int x, int y, int width, int height)
set_height(height);
}
+#if defined(OS_WIN)
Rect::Rect(const RECT& r)
: origin_(r.left, r.top) {
set_width(r.right - r.left);
@@ -75,6 +80,20 @@ Rect& Rect::operator=(const RECT& r) {
set_height(r.bottom - r.top);
return *this;
}
+#elif defined(OS_MACOSX)
+Rect::Rect(const CGRect& r)
+: origin_(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_width(r.size.width);
+ set_height(r.size.height);
+ return *this;
+}
+#endif
void Rect::set_width(int width) {
if (width < 0) {
@@ -119,6 +138,7 @@ bool Rect::operator==(const Rect& other) const {
return origin_ == other.origin_ && size_ == other.size_;
}
+#if defined(OS_WIN)
RECT Rect::ToRECT() const {
RECT r;
r.left = x();
@@ -127,6 +147,16 @@ RECT Rect::ToRECT() const {
r.bottom = bottom();
return r;
}
+#elif defined(OS_MACOSX)
+CGRect Rect::ToCGRect() const {
+ CGRect r;
+ r.origin.x = x();
+ r.origin.y = y();
+ r.size.width = width();
+ r.size.height = height();
+ return r;
+}
+#endif
bool Rect::Contains(int point_x, int point_y) const {
return (point_x >= x()) && (point_x < right()) &&
diff --git a/base/gfx/rect.h b/base/gfx/rect.h
index 486f799..d19bf7e 100644
--- a/base/gfx/rect.h
+++ b/base/gfx/rect.h
@@ -40,7 +40,9 @@
#include "base/gfx/size.h"
#include "base/gfx/point.h"
+#if defined(OS_WIN)
typedef struct tagRECT RECT;
+#endif
namespace gfx {
@@ -49,12 +51,20 @@ class Rect {
Rect();
Rect(int width, int height);
Rect(int x, int y, int width, int height);
+#if defined(OS_WIN)
explicit Rect(const RECT& r);
+#elif defined(OS_MACOSX)
+ explicit Rect(const CGRect& r);
+#endif
Rect(const gfx::Point& origin, const gfx::Size& size);
~Rect() {}
+#if defined(OS_WIN)
Rect& operator=(const RECT& r);
+#elif defined(OS_MACOSX)
+ Rect& operator=(const CGRect& r);
+#endif
int x() const { return origin_.x(); }
void set_x(int x) { origin_.set_x(x); }
@@ -93,8 +103,13 @@ class Rect {
return !(*this == other);
}
+#if defined(OS_WIN)
// Construct an equivalent Win32 RECT object.
RECT ToRECT() const;
+#elif defined(OS_MACOSX)
+ // Construct an equivalent CoreGraphics object.
+ 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
diff --git a/base/gfx/rect_unittest.cc b/base/gfx/rect_unittest.cc
index c218cc9..2bfcac2 100644
--- a/base/gfx/rect_unittest.cc
+++ b/base/gfx/rect_unittest.cc
@@ -55,7 +55,7 @@ TEST(RectTest, Contains) {
{0, 0, -10, -10, 0, 0, false},
#endif // NDEBUG
};
- for (int i = 0; i < arraysize(contains_cases); ++i) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(contains_cases); ++i) {
const ContainsCase& value = contains_cases[i];
gfx::Rect rect(value.rect_x, value.rect_y,
value.rect_width, value.rect_height);
@@ -84,7 +84,7 @@ TEST(RectTest, Intersects) {
{ 10, 10, 10, 10, 20, 15, 10, 10, false },
{ 10, 10, 10, 10, 21, 15, 10, 10, false }
};
- for (size_t i = 0; i < arraysize(tests); ++i) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
EXPECT_EQ(tests[i].intersects, r1.Intersects(r2));
@@ -125,7 +125,7 @@ TEST(RectTest, Intersect) {
0, 0, 2, 2,
0, 0, 0, 0 }
};
- for (size_t i = 0; i < arraysize(tests); ++i) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
@@ -138,7 +138,7 @@ TEST(RectTest, Intersect) {
}
TEST(RectTest, Union) {
- static const struct {
+ static const struct Test {
int x1; // rect 1
int y1;
int w1;
@@ -174,7 +174,7 @@ TEST(RectTest, Union) {
2, 2, 2, 2,
2, 2, 2, 2 }
};
- for (size_t i = 0; i < arraysize(tests); ++i) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
@@ -196,7 +196,7 @@ TEST(RectTest, Equals) {
}
TEST(RectTest, AdjustToFit) {
- static const struct {
+ static const struct Test {
int x1; // source
int y1;
int w1;
@@ -226,7 +226,7 @@ TEST(RectTest, AdjustToFit) {
0, 0, 3, 3,
2, 2, 1, 1 }
};
- for (size_t i = 0; i < arraysize(tests); ++i) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
diff --git a/base/gfx/size.h b/base/gfx/size.h
index cba2e51..b8a8e81 100644
--- a/base/gfx/size.h
+++ b/base/gfx/size.h
@@ -34,7 +34,11 @@
#include <iostream>
#endif
+#if defined(OS_WIN)
typedef struct tagSIZE SIZE;
+#elif defined(OS_MACOSX)
+#import <ApplicationServices/ApplicationServices.h>
+#endif
namespace gfx {
@@ -71,7 +75,11 @@ class Size {
return !width_ && !height_;
}
+#if defined(OS_WIN)
SIZE ToSIZE() const;
+#elif defined(OS_MACOSX)
+ CGSize ToCGSize() const;
+#endif
private:
int width_;
diff --git a/base/gfx/size_mac.cc b/base/gfx/size_mac.cc
new file mode 100644
index 0000000..1b0abb5
--- /dev/null
+++ b/base/gfx/size_mac.cc
@@ -0,0 +1,22 @@
+// Copyright 2008 Google Inc. All Rights Reserved.
+
+#include "base/gfx/size.h"
+
+#import <CoreGraphics/CoreGraphics.h>
+
+#include <ostream>
+
+namespace gfx {
+
+CGSize Size::ToCGSize() const {
+ CGSize s;
+ s.width = width_;
+ s.height = height_;
+ return s;
+}
+
+std::ostream& operator<<(std::ostream& out, const gfx::Size& s) {
+ return out << s.width() << "x" << s.height();
+}
+
+} // namespace gfx