]> git.saurik.com Git - iphone-api.git/blob - WebCore/IntRect.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / IntRect.h
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 #include <CoreGraphics/CGGeometry.h>
33
34 #if PLATFORM(CG)
35 typedef struct CGRect CGRect;
36 #endif
37
38
39 #ifndef NSRect
40 #define NSRect CGRect
41 #endif
42
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:
72 IntRect() { }
73 IntRect(const IntPoint& location, const IntSize& size)
74 : m_location(location), m_size(size) { }
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
80 IntPoint location() const { return m_location; }
81 IntSize size() const { return m_size; }
82
83 void setLocation(const IntPoint& location) { m_location = location; }
84 void setSize(const IntSize& size) { m_size = size; }
85
86 int x() const { return m_location.x(); }
87 int y() const { return m_location.y(); }
88 int width() const { return m_size.width(); }
89 int height() const { return m_size.height(); }
90
91 void setX(int x) { m_location.setX(x); }
92 void setY(int y) { m_location.setY(y); }
93 void setWidth(int width) { m_size.setWidth(width); }
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().
98 IntPoint topLeft() const { return m_location; }
99 IntPoint topRight() const { return IntPoint(right() - 1, y()); }
100 IntPoint bottomLeft() const { return IntPoint(x(), bottom() - 1); }
101 IntPoint bottomRight() const { return IntPoint(right() - 1, bottom() - 1); }
102
103 bool isEmpty() const { return m_size.isEmpty(); }
104
105 int right() const { return x() + width(); }
106 int bottom() const { return y() + height(); }
107
108 void move(const IntSize& s) { m_location += s; }
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).
116 bool contains(int px, int py) const
117 { return px >= x() && px < right() && py >= y() && py < bottom(); }
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
123 void inflateX(int dx)
124 {
125 m_location.setX(m_location.x() - dx);
126 m_size.setWidth(m_size.width() + dx + dx);
127 }
128 void inflateY(int dy)
129 {
130 m_location.setY(m_location.y() - dy);
131 m_size.setHeight(m_size.height() + dy + dy);
132 }
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
168 private:
169 IntPoint m_location;
170 IntSize m_size;
171 };
172
173 inline IntRect intersection(const IntRect& a, const IntRect& b)
174 {
175 IntRect c = a;
176 c.intersect(b);
177 return c;
178 }
179
180 inline IntRect unionRect(const IntRect& a, const IntRect& b)
181 {
182 IntRect c = a;
183 c.unite(b);
184 return c;
185 }
186
187 inline bool operator==(const IntRect& a, const IntRect& b)
188 {
189 return a.location() == b.location() && a.size() == b.size();
190 }
191
192 inline bool operator!=(const IntRect& a, const IntRect& b)
193 {
194 return a.location() != b.location() || a.size() != b.size();
195 }
196
197 #if PLATFORM(CG)
198 IntRect enclosingIntRect(const CGRect&);
199 #endif
200
201
202 } // namespace WebCore
203
204 #endif // IntRect_h