1/*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef IntPoint_h
27#define IntPoint_h
28
29#include "IntSize.h"
30#include <wtf/MathExtras.h>
31
32#if USE(CG)
33typedef struct CGPoint CGPoint;
34#endif
35
36
37#if OS(DARWIN) && !PLATFORM(QT)
38#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39typedef struct CGPoint NSPoint;
40#else
41typedef struct _NSPoint NSPoint;
42#endif
43#endif
44
45#if PLATFORM(WIN)
46typedef struct tagPOINT POINT;
47typedef struct tagPOINTS POINTS;
48#elif PLATFORM(QT)
49QT_BEGIN_NAMESPACE
50class QPoint;
51QT_END_NAMESPACE
52#elif PLATFORM(GTK)
53typedef struct _GdkPoint GdkPoint;
54#elif PLATFORM(BLACKBERRY)
55namespace BlackBerry {
56namespace Platform {
57class IntPoint;
58}
59}
60#elif PLATFORM(EFL)
61typedef struct _Evas_Point Evas_Point;
62#endif
63
64namespace WebCore {
65
66class IntPoint {
67public:
68    IntPoint() : m_x(0), m_y(0) { }
69    IntPoint(int x, int y) : m_x(x), m_y(y) { }
70    explicit IntPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { }
71
72    static IntPoint zero() { return IntPoint(); }
73
74    int x() const { return m_x; }
75    int y() const { return m_y; }
76
77    void setX(int x) { m_x = x; }
78    void setY(int y) { m_y = y; }
79
80    void move(const IntSize& s) { move(s.width(), s.height()); }
81    void moveBy(const IntPoint& offset) { move(offset.x(), offset.y()); }
82    void move(int dx, int dy) { m_x += dx; m_y += dy; }
83    void scale(float sx, float sy)
84    {
85        m_x = lroundf(static_cast<float>(m_x * sx));
86        m_y = lroundf(static_cast<float>(m_y * sy));
87    }
88
89    IntPoint expandedTo(const IntPoint& other) const
90    {
91        return IntPoint(m_x > other.m_x ? m_x : other.m_x,
92            m_y > other.m_y ? m_y : other.m_y);
93    }
94
95    IntPoint shrunkTo(const IntPoint& other) const
96    {
97        return IntPoint(m_x < other.m_x ? m_x : other.m_x,
98            m_y < other.m_y ? m_y : other.m_y);
99    }
100
101    int distanceSquaredToPoint(const IntPoint&) const;
102
103    void clampNegativeToZero()
104    {
105        *this = expandedTo(zero());
106    }
107
108    IntPoint transposedPoint() const
109    {
110        return IntPoint(m_y, m_x);
111    }
112
113#if USE(CG)
114    explicit IntPoint(const CGPoint&); // don't do this implicitly since it's lossy
115    operator CGPoint() const;
116#endif
117
118#if OS(DARWIN) && !PLATFORM(QT) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
119    explicit IntPoint(const NSPoint&); // don't do this implicitly since it's lossy
120    operator NSPoint() const;
121#endif
122
123#if PLATFORM(WIN)
124    IntPoint(const POINT&);
125    operator POINT() const;
126    IntPoint(const POINTS&);
127    operator POINTS() const;
128#elif PLATFORM(QT)
129    IntPoint(const QPoint&);
130    operator QPoint() const;
131#elif PLATFORM(GTK)
132    IntPoint(const GdkPoint&);
133    operator GdkPoint() const;
134#elif PLATFORM(BLACKBERRY)
135    IntPoint(const BlackBerry::Platform::IntPoint&);
136    operator BlackBerry::Platform::IntPoint() const;
137#elif PLATFORM(EFL)
138    explicit IntPoint(const Evas_Point&);
139    operator Evas_Point() const;
140#endif
141
142private:
143    int m_x, m_y;
144};
145
146inline IntPoint& operator+=(IntPoint& a, const IntSize& b)
147{
148    a.move(b.width(), b.height());
149    return a;
150}
151
152inline IntPoint& operator-=(IntPoint& a, const IntSize& b)
153{
154    a.move(-b.width(), -b.height());
155    return a;
156}
157
158inline IntPoint operator+(const IntPoint& a, const IntSize& b)
159{
160    return IntPoint(a.x() + b.width(), a.y() + b.height());
161}
162
163inline IntPoint operator+(const IntPoint& a, const IntPoint& b)
164{
165    return IntPoint(a.x() + b.x(), a.y() + b.y());
166}
167
168inline IntSize operator-(const IntPoint& a, const IntPoint& b)
169{
170    return IntSize(a.x() - b.x(), a.y() - b.y());
171}
172
173inline IntPoint operator-(const IntPoint& a, const IntSize& b)
174{
175    return IntPoint(a.x() - b.width(), a.y() - b.height());
176}
177
178inline IntPoint operator-(const IntPoint& point)
179{
180    return IntPoint(-point.x(), -point.y());
181}
182
183inline bool operator==(const IntPoint& a, const IntPoint& b)
184{
185    return a.x() == b.x() && a.y() == b.y();
186}
187
188inline bool operator!=(const IntPoint& a, const IntPoint& b)
189{
190    return a.x() != b.x() || a.y() != b.y();
191}
192
193inline IntSize toIntSize(const IntPoint& a)
194{
195    return IntSize(a.x(), a.y());
196}
197
198inline int IntPoint::distanceSquaredToPoint(const IntPoint& point) const
199{
200    return ((*this) - point).diagonalLengthSquared();
201}
202
203} // namespace WebCore
204
205#endif // IntPoint_h
206