Home | History | Annotate | Download | only in geometry
      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.
      4 
      5 #ifndef UI_GFX_GEOMETRY_POINT_H_
      6 #define UI_GFX_GEOMETRY_POINT_H_
      7 
      8 #include <iosfwd>
      9 #include <string>
     10 
     11 #include "ui/gfx/geometry/point_base.h"
     12 #include "ui/gfx/geometry/point_f.h"
     13 #include "ui/gfx/geometry/vector2d.h"
     14 #include "ui/gfx/gfx_export.h"
     15 
     16 #if defined(OS_WIN)
     17 typedef unsigned long DWORD;
     18 typedef struct tagPOINT POINT;
     19 #elif defined(OS_IOS)
     20 #include <CoreGraphics/CoreGraphics.h>
     21 #elif defined(OS_MACOSX)
     22 #include <ApplicationServices/ApplicationServices.h>
     23 #endif
     24 
     25 namespace gfx {
     26 
     27 // A point has an x and y coordinate.
     28 class GFX_EXPORT Point : public PointBase<Point, int, Vector2d> {
     29  public:
     30   Point() : PointBase<Point, int, Vector2d>(0, 0) {}
     31   Point(int x, int y) : PointBase<Point, int, Vector2d>(x, y) {}
     32 #if defined(OS_WIN)
     33   // |point| is a DWORD value that contains a coordinate.  The x-coordinate is
     34   // the low-order short and the y-coordinate is the high-order short.  This
     35   // value is commonly acquired from GetMessagePos/GetCursorPos.
     36   explicit Point(DWORD point);
     37   explicit Point(const POINT& point);
     38   Point& operator=(const POINT& point);
     39 #elif defined(OS_MACOSX)
     40   explicit Point(const CGPoint& point);
     41 #endif
     42 
     43   ~Point() {}
     44 
     45 #if defined(OS_WIN)
     46   POINT ToPOINT() const;
     47 #elif defined(OS_MACOSX)
     48   CGPoint ToCGPoint() const;
     49 #endif
     50 
     51   operator PointF() const {
     52     return PointF(x(), y());
     53   }
     54 
     55   // Returns a string representation of point.
     56   std::string ToString() const;
     57 };
     58 
     59 inline bool operator==(const Point& lhs, const Point& rhs) {
     60   return lhs.x() == rhs.x() && lhs.y() == rhs.y();
     61 }
     62 
     63 inline bool operator!=(const Point& lhs, const Point& rhs) {
     64   return !(lhs == rhs);
     65 }
     66 
     67 inline Point operator+(const Point& lhs, const Vector2d& rhs) {
     68   Point result(lhs);
     69   result += rhs;
     70   return result;
     71 }
     72 
     73 inline Point operator-(const Point& lhs, const Vector2d& rhs) {
     74   Point result(lhs);
     75   result -= rhs;
     76   return result;
     77 }
     78 
     79 inline Vector2d operator-(const Point& lhs, const Point& rhs) {
     80   return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y());
     81 }
     82 
     83 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
     84   return Point(offset_from_origin.x(), offset_from_origin.y());
     85 }
     86 
     87 #if !defined(COMPILER_MSVC) && !defined(__native_client__)
     88 extern template class PointBase<Point, int, Vector2d>;
     89 #endif
     90 
     91 // This is declared here for use in gtest-based unit tests but is defined in
     92 // the gfx_test_support target. Depend on that to use this in your unit test.
     93 // This should not be used in production code - call ToString() instead.
     94 void PrintTo(const Point& point, ::std::ostream* os);
     95 
     96 }  // namespace gfx
     97 
     98 #endif  // UI_GFX_GEOMETRY_POINT_H_
     99