]> git.saurik.com Git - iphone-api.git/blob - WebCore/FloatRect.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / FloatRect.h
1 /*
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef FloatRect_h
28 #define FloatRect_h
29
30 #include "FloatPoint.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(QT)
45 QT_BEGIN_NAMESPACE
46 class QRectF;
47 QT_END_NAMESPACE
48 #endif
49
50 #if PLATFORM(WX) && USE(WXGC)
51 class wxRect2DDouble;
52 #endif
53
54 #if PLATFORM(SKIA)
55 struct SkRect;
56 #endif
57
58 namespace WebCore {
59
60 class IntRect;
61
62 class FloatRect {
63 public:
64 FloatRect() { }
65 FloatRect(const FloatPoint& location, const FloatSize& size)
66 : m_location(location), m_size(size) { }
67 FloatRect(float x, float y, float width, float height)
68 : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
69 FloatRect(const IntRect&);
70
71 static FloatRect narrowPrecision(double x, double y, double width, double height);
72
73 FloatPoint location() const { return m_location; }
74 FloatSize size() const { return m_size; }
75
76 void setLocation(const FloatPoint& location) { m_location = location; }
77 void setSize(const FloatSize& size) { m_size = size; }
78
79 float x() const { return m_location.x(); }
80 float y() const { return m_location.y(); }
81 float width() const { return m_size.width(); }
82 float height() const { return m_size.height(); }
83
84 void setX(float x) { m_location.setX(x); }
85 void setY(float y) { m_location.setY(y); }
86 void setWidth(float width) { m_size.setWidth(width); }
87 void setHeight(float height) { m_size.setHeight(height); }
88
89 bool isEmpty() const { return m_size.isEmpty(); }
90
91 float right() const { return x() + width(); }
92 float bottom() const { return y() + height(); }
93
94 void move(const FloatSize& delta) { m_location += delta; }
95 void move(float dx, float dy) { m_location.move(dx, dy); }
96
97 bool intersects(const FloatRect&) const;
98 bool contains(const FloatRect&) const;
99
100 void intersect(const FloatRect&);
101 void unite(const FloatRect&);
102
103 // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
104 // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
105 bool contains(float px, float py) const
106 { return px >= x() && px <= right() && py >= y() && py <= bottom(); }
107 bool contains(const FloatPoint& point) const { return contains(point.x(), point.y()); }
108
109
110 void inflateX(float dx) {
111 m_location.setX(m_location.x() - dx);
112 m_size.setWidth(m_size.width() + dx + dx);
113 }
114 void inflateY(float dy) {
115 m_location.setY(m_location.y() - dy);
116 m_size.setHeight(m_size.height() + dy + dy);
117 }
118 void inflate(float d) { inflateX(d); inflateY(d); }
119 void scale(float s);
120
121 #if PLATFORM(CG)
122 FloatRect(const CGRect&);
123 operator CGRect() const;
124 #endif
125
126
127 #if PLATFORM(QT)
128 FloatRect(const QRectF&);
129 operator QRectF() const;
130 #endif
131 #if PLATFORM(SYMBIAN)
132 FloatRect(const TRect&);
133 operator TRect() const;
134 TRect rect() const;
135 #endif
136
137 #if PLATFORM(WX) && USE(WXGC)
138 FloatRect(const wxRect2DDouble&);
139 operator wxRect2DDouble() const;
140 #endif
141
142 #if PLATFORM(SKIA)
143 FloatRect(const SkRect&);
144 operator SkRect() const;
145 #endif
146
147 private:
148 FloatPoint m_location;
149 FloatSize m_size;
150 };
151
152 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
153 {
154 FloatRect c = a;
155 c.intersect(b);
156 return c;
157 }
158
159 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
160 {
161 FloatRect c = a;
162 c.unite(b);
163 return c;
164 }
165
166
167 inline bool operator==(const FloatRect& a, const FloatRect& b)
168 {
169 return a.location() == b.location() && a.size() == b.size();
170 }
171
172 inline bool operator!=(const FloatRect& a, const FloatRect& b)
173 {
174 return a.location() != b.location() || a.size() != b.size();
175 }
176
177 IntRect enclosingIntRect(const FloatRect&);
178
179 // Map rect r from srcRect to an equivalent rect in destRect.
180 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);
181
182 }
183
184 #endif