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
31 #if PLATFORM(QT)
32 #include <QDataStream>
33 #endif
34
35 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
36 typedef struct CGPoint CGPoint;
37 #endif
38
39
40 #if PLATFORM(MAC)
41 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
42 typedef struct CGPoint NSPoint;
43 #else
44 typedef struct _NSPoint NSPoint;
45 #endif
46 #endif
47
48 #if PLATFORM(WIN)
49 typedef struct tagPOINT POINT;
50 typedef struct tagPOINTS POINTS;
51 #elif PLATFORM(QT)
52 QT_BEGIN_NAMESPACE
53 class QPoint;
54 QT_END_NAMESPACE
55 #elif PLATFORM(GTK)
56 typedef struct _GdkPoint GdkPoint;
57 #elif PLATFORM(HAIKU)
58 class BPoint;
59 #elif PLATFORM(EFL)
60 typedef struct _Evas_Point Evas_Point;
61 #endif
62
63 #if PLATFORM(WX)
64 class wxPoint;
65 #endif
66
67 #if PLATFORM(BREWMP)
68 typedef struct _point AEEPoint;
69 #endif
70
71 #if USE(SKIA)
72 struct SkPoint;
73 struct SkIPoint;
74 #endif
75
76 namespace WebCore {
77
78 class IntPoint {
79 public:
IntPoint()80 IntPoint() : m_x(0), m_y(0) { }
IntPoint(int x,int y)81 IntPoint(int x, int y) : m_x(x), m_y(y) { }
IntPoint(const IntSize & size)82 explicit IntPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { }
83
zero()84 static IntPoint zero() { return IntPoint(); }
85
x()86 int x() const { return m_x; }
y()87 int y() const { return m_y; }
88
setX(int x)89 void setX(int x) { m_x = x; }
setY(int y)90 void setY(int y) { m_y = y; }
91
move(const IntSize & s)92 void move(const IntSize& s) { move(s.width(), s.height()); }
move(int dx,int dy)93 void move(int dx, int dy) { m_x += dx; m_y += dy; }
94
expandedTo(const IntPoint & other)95 IntPoint expandedTo(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
shrunkTo(const IntPoint & other)101 IntPoint shrunkTo(const IntPoint& other) const
102 {
103 return IntPoint(m_x < other.m_x ? m_x : other.m_x,
104 m_y < other.m_y ? m_y : other.m_y);
105 }
106
clampNegativeToZero()107 void clampNegativeToZero()
108 {
109 *this = expandedTo(zero());
110 }
111
transposedPoint()112 IntPoint transposedPoint() const
113 {
114 return IntPoint(m_y, m_x);
115 }
116
117 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
118 explicit IntPoint(const CGPoint&); // don't do this implicitly since it's lossy
119 operator CGPoint() const;
120 #endif
121
122 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
123 explicit IntPoint(const NSPoint&); // don't do this implicitly since it's lossy
124 operator NSPoint() const;
125 #endif
126
127 #if PLATFORM(WIN)
128 IntPoint(const POINT&);
129 operator POINT() const;
130 IntPoint(const POINTS&);
131 operator POINTS() const;
132 #elif PLATFORM(QT)
133 IntPoint(const QPoint&);
134 operator QPoint() const;
135 #elif PLATFORM(GTK)
136 IntPoint(const GdkPoint&);
137 operator GdkPoint() const;
138 #elif PLATFORM(HAIKU)
139 explicit IntPoint(const BPoint&);
140 operator BPoint() const;
141 #elif PLATFORM(EFL)
142 explicit IntPoint(const Evas_Point&);
143 operator Evas_Point() const;
144 #endif
145
146 #if PLATFORM(WX)
147 IntPoint(const wxPoint&);
148 operator wxPoint() const;
149 #endif
150
151 #if PLATFORM(BREWMP)
152 IntPoint(const AEEPoint&);
153 operator AEEPoint() const;
154 #endif
155
156 #if USE(SKIA)
157 IntPoint(const SkIPoint&);
158 operator SkIPoint() const;
159 operator SkPoint() const;
160 #endif
161
162 private:
163 int m_x, m_y;
164 };
165
166 inline IntPoint& operator+=(IntPoint& a, const IntSize& b)
167 {
168 a.move(b.width(), b.height());
169 return a;
170 }
171
172 inline IntPoint& operator-=(IntPoint& a, const IntSize& b)
173 {
174 a.move(-b.width(), -b.height());
175 return a;
176 }
177
178 inline IntPoint operator+(const IntPoint& a, const IntSize& b)
179 {
180 return IntPoint(a.x() + b.width(), a.y() + b.height());
181 }
182
183 inline IntSize operator-(const IntPoint& a, const IntPoint& b)
184 {
185 return IntSize(a.x() - b.x(), a.y() - b.y());
186 }
187
188 inline IntPoint operator-(const IntPoint& a, const IntSize& b)
189 {
190 return IntPoint(a.x() - b.width(), a.y() - b.height());
191 }
192
193 inline bool operator==(const IntPoint& a, const IntPoint& b)
194 {
195 return a.x() == b.x() && a.y() == b.y();
196 }
197
198 inline bool operator!=(const IntPoint& a, const IntPoint& b)
199 {
200 return a.x() != b.x() || a.y() != b.y();
201 }
202
toPoint(const IntSize & size)203 inline IntPoint toPoint(const IntSize& size)
204 {
205 return IntPoint(size.width(), size.height());
206 }
207
208 #if PLATFORM(QT)
209 inline QDataStream& operator<<(QDataStream& stream, const IntPoint& point)
210 {
211 stream << point.x() << point.y();
212 return stream;
213 }
214
215 inline QDataStream& operator>>(QDataStream& stream, IntPoint& point)
216 {
217 int x, y;
218 stream >> x >> y;
219 point.setX(x);
220 point.setY(y);
221 return stream;
222 }
223 #endif
224
225 } // namespace WebCore
226
227 #endif // IntPoint_h
228