1 /*
2 * Copyright (C) 2003, 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 IntRect_h
27 #define IntRect_h
28
29 #include "IntPoint.h"
30 #include <wtf/Platform.h>
31
32 #if PLATFORM(CG)
33 typedef struct CGRect CGRect;
34 #endif
35
36 #if PLATFORM(MAC)
37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
38 typedef struct CGRect NSRect;
39 #else
40 typedef struct _NSRect NSRect;
41 #endif
42 #endif
43
44 #if PLATFORM(WIN)
45 typedef struct tagRECT RECT;
46 #elif PLATFORM(QT)
47 QT_BEGIN_NAMESPACE
48 class QRect;
49 QT_END_NAMESPACE
50 #elif PLATFORM(GTK)
51 typedef struct _GdkRectangle GdkRectangle;
52 #endif
53 #if PLATFORM(SYMBIAN)
54 class TRect;
55 #endif
56
57 #if PLATFORM(WX)
58 class wxRect;
59 #endif
60
61 #if PLATFORM(SKIA)
62 struct SkRect;
63 struct SkIRect;
64 #endif
65
66 namespace WebCore {
67
68 class FloatRect;
69
70 class IntRect {
71 public:
IntRect()72 IntRect() { }
IntRect(const IntPoint & location,const IntSize & size)73 IntRect(const IntPoint& location, const IntSize& size)
74 : m_location(location), m_size(size) { }
IntRect(int x,int y,int width,int height)75 IntRect(int x, int y, int width, int height)
76 : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
77
78 explicit IntRect(const FloatRect& rect); // don't do this implicitly since it's lossy
79
location()80 IntPoint location() const { return m_location; }
size()81 IntSize size() const { return m_size; }
82
setLocation(const IntPoint & location)83 void setLocation(const IntPoint& location) { m_location = location; }
setSize(const IntSize & size)84 void setSize(const IntSize& size) { m_size = size; }
85
x()86 int x() const { return m_location.x(); }
y()87 int y() const { return m_location.y(); }
width()88 int width() const { return m_size.width(); }
height()89 int height() const { return m_size.height(); }
90
setX(int x)91 void setX(int x) { m_location.setX(x); }
setY(int y)92 void setY(int y) { m_location.setY(y); }
setWidth(int width)93 void setWidth(int width) { m_size.setWidth(width); }
setHeight(int height)94 void setHeight(int height) { m_size.setHeight(height); }
95
96 // Be careful with these functions. The point is considered to be to the right and below. These are not
97 // substitutes for right() and bottom().
topLeft()98 IntPoint topLeft() const { return m_location; }
topRight()99 IntPoint topRight() const { return IntPoint(right() - 1, y()); }
bottomLeft()100 IntPoint bottomLeft() const { return IntPoint(x(), bottom() - 1); }
bottomRight()101 IntPoint bottomRight() const { return IntPoint(right() - 1, bottom() - 1); }
102
isEmpty()103 bool isEmpty() const { return m_size.isEmpty(); }
104
right()105 int right() const { return x() + width(); }
bottom()106 int bottom() const { return y() + height(); }
107
move(const IntSize & s)108 void move(const IntSize& s) { m_location += s; }
move(int dx,int dy)109 void move(int dx, int dy) { m_location.move(dx, dy); }
110
111 bool intersects(const IntRect&) const;
112 bool contains(const IntRect&) const;
113
114 // This checks to see if the rect contains x,y in the traditional sense.
115 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
contains(int px,int py)116 bool contains(int px, int py) const
117 { return px >= x() && px < right() && py >= y() && py < bottom(); }
contains(const IntPoint & point)118 bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
119
120 void intersect(const IntRect&);
121 void unite(const IntRect&);
122
inflateX(int dx)123 void inflateX(int dx)
124 {
125 m_location.setX(m_location.x() - dx);
126 m_size.setWidth(m_size.width() + dx + dx);
127 }
inflateY(int dy)128 void inflateY(int dy)
129 {
130 m_location.setY(m_location.y() - dy);
131 m_size.setHeight(m_size.height() + dy + dy);
132 }
inflate(int d)133 void inflate(int d) { inflateX(d); inflateY(d); }
134 void scale(float s);
135
136 #if PLATFORM(WX)
137 IntRect(const wxRect&);
138 operator wxRect() const;
139 #endif
140
141 #if PLATFORM(WIN)
142 IntRect(const RECT&);
143 operator RECT() const;
144 #elif PLATFORM(QT)
145 IntRect(const QRect&);
146 operator QRect() const;
147 #elif PLATFORM(GTK)
148 IntRect(const GdkRectangle&);
149 operator GdkRectangle() const;
150 #endif
151 #if PLATFORM(SYMBIAN)
152 IntRect(const TRect&);
153 operator TRect() const;
154 TRect Rect() const;
155 #endif
156
157 #if PLATFORM(CG)
158 operator CGRect() const;
159 #endif
160
161 #if PLATFORM(SKIA)
162 IntRect(const SkIRect&);
163 operator SkRect() const;
164 operator SkIRect() const;
165 #endif
166
167 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
168 operator NSRect() const;
169 #endif
170
171 private:
172 IntPoint m_location;
173 IntSize m_size;
174 };
175
intersection(const IntRect & a,const IntRect & b)176 inline IntRect intersection(const IntRect& a, const IntRect& b)
177 {
178 IntRect c = a;
179 c.intersect(b);
180 return c;
181 }
182
unionRect(const IntRect & a,const IntRect & b)183 inline IntRect unionRect(const IntRect& a, const IntRect& b)
184 {
185 IntRect c = a;
186 c.unite(b);
187 return c;
188 }
189
190 inline bool operator==(const IntRect& a, const IntRect& b)
191 {
192 return a.location() == b.location() && a.size() == b.size();
193 }
194
195 inline bool operator!=(const IntRect& a, const IntRect& b)
196 {
197 return a.location() != b.location() || a.size() != b.size();
198 }
199
200 #if PLATFORM(CG)
201 IntRect enclosingIntRect(const CGRect&);
202 #endif
203
204 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
205 IntRect enclosingIntRect(const NSRect&);
206 #endif
207
208 } // namespace WebCore
209
210 #endif // IntRect_h
211