Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / geometry / point.h
blobbedfb5c979c64ddae89cb41e0c75897188a00336
1 // Copyright (c) 2012 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.
5 #ifndef UI_GFX_GEOMETRY_POINT_H_
6 #define UI_GFX_GEOMETRY_POINT_H_
8 #include <iosfwd>
9 #include <string>
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/gfx_export.h"
15 #if defined(OS_WIN)
16 typedef unsigned long DWORD;
17 typedef struct tagPOINT POINT;
18 #elif defined(OS_IOS)
19 #include <CoreGraphics/CoreGraphics.h>
20 #elif defined(OS_MACOSX)
21 #include <ApplicationServices/ApplicationServices.h>
22 #endif
24 namespace gfx {
26 // A point has an x and y coordinate.
27 class GFX_EXPORT Point {
28 public:
29 Point() : x_(0), y_(0) {}
30 Point(int x, int y) : x_(x), y_(y) {}
31 #if defined(OS_WIN)
32 // |point| is a DWORD value that contains a coordinate. The x-coordinate is
33 // the low-order short and the y-coordinate is the high-order short. This
34 // value is commonly acquired from GetMessagePos/GetCursorPos.
35 explicit Point(DWORD point);
36 explicit Point(const POINT& point);
37 Point& operator=(const POINT& point);
38 #elif defined(OS_MACOSX)
39 explicit Point(const CGPoint& point) : x_(point.x), y_(point.y) {}
40 #endif
42 ~Point() {}
44 #if defined(OS_WIN)
45 POINT ToPOINT() const;
46 #elif defined(OS_MACOSX)
47 CGPoint ToCGPoint() const { return CGPointMake(x(), y()); }
48 #endif
50 int x() const { return x_; }
51 int y() const { return y_; }
52 void set_x(int x) { x_ = x; }
53 void set_y(int y) { y_ = y; }
55 void SetPoint(int x, int y) {
56 x_ = x;
57 y_ = y;
60 void Offset(int delta_x, int delta_y) {
61 x_ += delta_x;
62 y_ += delta_y;
65 void operator+=(const Vector2d& vector) {
66 x_ += vector.x();
67 y_ += vector.y();
70 void operator-=(const Vector2d& vector) {
71 x_ -= vector.x();
72 y_ -= vector.y();
75 void SetToMin(const Point& other);
76 void SetToMax(const Point& other);
78 bool IsOrigin() const { return x_ == 0 && y_ == 0; }
80 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); }
82 // A point is less than another point if its y-value is closer
83 // to the origin. If the y-values are the same, then point with
84 // the x-value closer to the origin is considered less than the
85 // other.
86 // This comparison is required to use Point in sets, or sorted
87 // vectors.
88 bool operator<(const Point& rhs) const {
89 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_);
92 operator PointF() const {
93 return PointF(static_cast<float>(x()), static_cast<float>(y()));
96 // Returns a string representation of point.
97 std::string ToString() const;
99 private:
100 int x_;
101 int y_;
104 inline bool operator==(const Point& lhs, const Point& rhs) {
105 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
108 inline bool operator!=(const Point& lhs, const Point& rhs) {
109 return !(lhs == rhs);
112 inline Point operator+(const Point& lhs, const Vector2d& rhs) {
113 Point result(lhs);
114 result += rhs;
115 return result;
118 inline Point operator-(const Point& lhs, const Vector2d& rhs) {
119 Point result(lhs);
120 result -= rhs;
121 return result;
124 inline Vector2d operator-(const Point& lhs, const Point& rhs) {
125 return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y());
128 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
129 return Point(offset_from_origin.x(), offset_from_origin.y());
132 // This is declared here for use in gtest-based unit tests but is defined in
133 // the gfx_test_support target. Depend on that to use this in your unit test.
134 // This should not be used in production code - call ToString() instead.
135 void PrintTo(const Point& point, ::std::ostream* os);
137 } // namespace gfx
139 #endif // UI_GFX_GEOMETRY_POINT_H_