1 // Copyright (c) 2011 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_POINT_H_ 6 #define UI_GFX_POINT_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "build/build_config.h" 12 #include "ui/base/ui_export.h" 13 14 #if defined(OS_WIN) 15 typedef unsigned long DWORD; 16 typedef struct tagPOINT POINT; 17 #elif defined(OS_MACOSX) 18 #include <ApplicationServices/ApplicationServices.h> 19 #endif 20 21 namespace gfx { 22 23 // A point has an x and y coordinate. 24 class UI_EXPORT Point { 25 public: 26 Point(); 27 Point(int x, int y); 28 #if defined(OS_WIN) 29 // |point| is a DWORD value that contains a coordinate. The x-coordinate is 30 // the low-order short and the y-coordinate is the high-order short. This 31 // value is commonly acquired from GetMessagePos/GetCursorPos. 32 explicit Point(DWORD point); 33 explicit Point(const POINT& point); 34 Point& operator=(const POINT& point); 35 #elif defined(OS_MACOSX) 36 explicit Point(const CGPoint& point); 37 #endif 38 ~Point()39 ~Point() {} 40 x()41 int x() const { return x_; } y()42 int y() const { return y_; } 43 SetPoint(int x,int y)44 void SetPoint(int x, int y) { 45 x_ = x; 46 y_ = y; 47 } 48 set_x(int x)49 void set_x(int x) { x_ = x; } set_y(int y)50 void set_y(int y) { y_ = y; } 51 Offset(int delta_x,int delta_y)52 void Offset(int delta_x, int delta_y) { 53 x_ += delta_x; 54 y_ += delta_y; 55 } 56 Add(const Point & other)57 Point Add(const Point& other) const{ 58 Point copy = *this; 59 copy.Offset(other.x_, other.y_); 60 return copy; 61 } 62 Subtract(const Point & other)63 Point Subtract(const Point& other) const { 64 Point copy = *this; 65 copy.Offset(-other.x_, -other.y_); 66 return copy; 67 } 68 Middle(const Point & other)69 Point Middle(const Point& other) const { 70 return Point((x_ + other.x_) / 2, (y_ + other.y_) / 2); 71 } 72 73 bool operator==(const Point& rhs) const { 74 return x_ == rhs.x_ && y_ == rhs.y_; 75 } 76 77 bool operator!=(const Point& rhs) const { 78 return !(*this == rhs); 79 } 80 81 // A point is less than another point if its y-value is closer 82 // to the origin. If the y-values are the same, then point with 83 // the x-value closer to the origin is considered less than the 84 // other. 85 // This comparison is required to use Points in sets, or sorted 86 // vectors. 87 bool operator<(const Point& rhs) const { 88 return (y_ == rhs.y_) ? (x_ < rhs.x_) : (y_ < rhs.y_); 89 } 90 91 #if defined(OS_WIN) 92 POINT ToPOINT() const; 93 #elif defined(OS_MACOSX) 94 CGPoint ToCGPoint() const; 95 #endif 96 97 // Returns a string representation of point. 98 std::string ToString() const; 99 100 private: 101 int x_; 102 int y_; 103 }; 104 105 } // namespace gfx 106 107 #endif // UI_GFX_POINT_H_ 108