1/*
2 * Copyright (C) 2003, 2006, 2009 Apple 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * 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#include <wtf/Vector.h>
32
33#if PLATFORM(CG)
34typedef struct CGRect CGRect;
35#endif
36
37#if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
38#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39typedef struct CGRect NSRect;
40#else
41typedef struct _NSRect NSRect;
42#endif
43#endif
44
45#if PLATFORM(WIN)
46typedef struct tagRECT RECT;
47#elif PLATFORM(QT)
48QT_BEGIN_NAMESPACE
49class QRect;
50QT_END_NAMESPACE
51#elif PLATFORM(GTK)
52typedef struct _GdkRectangle GdkRectangle;
53#elif PLATFORM(HAIKU)
54class BRect;
55#endif
56
57#if PLATFORM(WX)
58class wxRect;
59#endif
60
61#if PLATFORM(SKIA)
62struct SkRect;
63struct SkIRect;
64#endif
65
66namespace WebCore {
67
68class FloatRect;
69
70class IntRect {
71public:
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#elif PLATFORM(HAIKU)
151    explicit IntRect(const BRect&);
152    operator BRect() const;
153#endif
154
155#if PLATFORM(CG)
156    operator CGRect() const;
157#endif
158
159#if PLATFORM(SKIA)
160    IntRect(const SkIRect&);
161    operator SkRect() const;
162    operator SkIRect() const;
163#endif
164
165#if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
166        || (PLATFORM(CHROMIUM) && OS(DARWIN))
167    operator NSRect() const;
168#endif
169
170private:
171    IntPoint m_location;
172    IntSize m_size;
173};
174
175inline IntRect intersection(const IntRect& a, const IntRect& b)
176{
177    IntRect c = a;
178    c.intersect(b);
179    return c;
180}
181
182inline IntRect unionRect(const IntRect& a, const IntRect& b)
183{
184    IntRect c = a;
185    c.unite(b);
186    return c;
187}
188
189IntRect unionRect(const Vector<IntRect>&);
190
191inline bool operator==(const IntRect& a, const IntRect& b)
192{
193    return a.location() == b.location() && a.size() == b.size();
194}
195
196inline bool operator!=(const IntRect& a, const IntRect& b)
197{
198    return a.location() != b.location() || a.size() != b.size();
199}
200
201#if PLATFORM(CG)
202IntRect enclosingIntRect(const CGRect&);
203#endif
204
205#if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
206        || (PLATFORM(CHROMIUM) && OS(DARWIN))
207IntRect enclosingIntRect(const NSRect&);
208#endif
209
210} // namespace WebCore
211
212#endif // IntRect_h
213