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/Vector.h>
31
32#if USE(CG)
33typedef struct CGRect CGRect;
34#endif
35
36#if PLATFORM(MAC)
37#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
38typedef struct CGRect NSRect;
39#else
40typedef struct _NSRect NSRect;
41#endif
42#endif
43
44#if PLATFORM(WIN)
45typedef struct tagRECT RECT;
46#elif PLATFORM(QT)
47QT_BEGIN_NAMESPACE
48class QRect;
49QT_END_NAMESPACE
50#elif PLATFORM(GTK)
51#ifdef GTK_API_VERSION_2
52typedef struct _GdkRectangle GdkRectangle;
53#endif
54#elif PLATFORM(EFL)
55typedef struct _Eina_Rectangle Eina_Rectangle;
56#elif PLATFORM(BLACKBERRY)
57namespace BlackBerry {
58namespace Platform {
59class IntRect;
60}
61}
62#endif
63
64#if USE(CAIRO)
65typedef struct _cairo_rectangle_int cairo_rectangle_int_t;
66#endif
67
68namespace WebCore {
69
70class FloatRect;
71class LayoutRect;
72
73class IntRect {
74    WTF_MAKE_FAST_ALLOCATED;
75public:
76    IntRect() { }
77    IntRect(const IntPoint& location, const IntSize& size)
78        : m_location(location), m_size(size) { }
79    IntRect(int x, int y, int width, int height)
80        : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
81
82    explicit IntRect(const FloatRect&); // don't do this implicitly since it's lossy
83    explicit IntRect(const LayoutRect&); // don't do this implicitly since it's lossy
84
85    IntPoint location() const { return m_location; }
86    IntSize size() const { return m_size; }
87
88    void setLocation(const IntPoint& location) { m_location = location; }
89    void setSize(const IntSize& size) { m_size = size; }
90
91    int x() const { return m_location.x(); }
92    int y() const { return m_location.y(); }
93    int maxX() const { return x() + width(); }
94    int maxY() const { return y() + height(); }
95    int width() const { return m_size.width(); }
96    int height() const { return m_size.height(); }
97
98    void setX(int x) { m_location.setX(x); }
99    void setY(int y) { m_location.setY(y); }
100    void setWidth(int width) { m_size.setWidth(width); }
101    void setHeight(int height) { m_size.setHeight(height); }
102
103    bool isEmpty() const { return m_size.isEmpty(); }
104
105    // NOTE: The result is rounded to integer values, and thus may be not the exact
106    // center point.
107    IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
108
109    void move(const IntSize& size) { m_location += size; }
110    void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
111    void move(int dx, int dy) { m_location.move(dx, dy); }
112
113    void expand(const IntSize& size) { m_size += size; }
114    void expand(int dw, int dh) { m_size.expand(dw, dh); }
115    void contract(const IntSize& size) { m_size -= size; }
116    void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
117
118    void shiftXEdgeTo(int edge)
119    {
120        int delta = edge - x();
121        setX(edge);
122        setWidth(std::max(0, width() - delta));
123    }
124    void shiftMaxXEdgeTo(int edge)
125    {
126        int delta = edge - maxX();
127        setWidth(std::max(0, width() + delta));
128    }
129    void shiftYEdgeTo(int edge)
130    {
131        int delta = edge - y();
132        setY(edge);
133        setHeight(std::max(0, height() - delta));
134    }
135    void shiftMaxYEdgeTo(int edge)
136    {
137        int delta = edge - maxY();
138        setHeight(std::max(0, height() + delta));
139    }
140
141    IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
142    IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
143    IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
144    IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
145
146    bool intersects(const IntRect&) const;
147    bool contains(const IntRect&) const;
148
149    // This checks to see if the rect contains x,y in the traditional sense.
150    // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
151    bool contains(int px, int py) const
152        { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
153    bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
154
155    void intersect(const IntRect&);
156    void unite(const IntRect&);
157    void uniteIfNonZero(const IntRect&);
158
159    void inflateX(int dx)
160    {
161        m_location.setX(m_location.x() - dx);
162        m_size.setWidth(m_size.width() + dx + dx);
163    }
164    void inflateY(int dy)
165    {
166        m_location.setY(m_location.y() - dy);
167        m_size.setHeight(m_size.height() + dy + dy);
168    }
169    void inflate(int d) { inflateX(d); inflateY(d); }
170    void scale(float s);
171
172    IntSize differenceToPoint(const IntPoint&) const;
173    int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
174
175    IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
176
177#if PLATFORM(WIN)
178    IntRect(const RECT&);
179    operator RECT() const;
180#elif PLATFORM(QT)
181    IntRect(const QRect&);
182    operator QRect() const;
183#elif PLATFORM(GTK)
184#ifdef GTK_API_VERSION_2
185    IntRect(const GdkRectangle&);
186    operator GdkRectangle() const;
187#endif
188#elif PLATFORM(EFL)
189    explicit IntRect(const Eina_Rectangle&);
190    operator Eina_Rectangle() const;
191#endif
192
193#if USE(CAIRO)
194    IntRect(const cairo_rectangle_int_t&);
195    operator cairo_rectangle_int_t() const;
196#endif
197
198#if USE(CG)
199    operator CGRect() const;
200#endif
201
202#if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES))
203    operator NSRect() const;
204#endif
205
206#if PLATFORM(BLACKBERRY)
207    IntRect(const BlackBerry::Platform::IntRect&);
208    operator BlackBerry::Platform::IntRect() const;
209#endif
210
211private:
212    IntPoint m_location;
213    IntSize m_size;
214};
215
216inline IntRect intersection(const IntRect& a, const IntRect& b)
217{
218    IntRect c = a;
219    c.intersect(b);
220    return c;
221}
222
223inline IntRect unionRect(const IntRect& a, const IntRect& b)
224{
225    IntRect c = a;
226    c.unite(b);
227    return c;
228}
229
230IntRect unionRect(const Vector<IntRect>&);
231
232inline bool operator==(const IntRect& a, const IntRect& b)
233{
234    return a.location() == b.location() && a.size() == b.size();
235}
236
237inline bool operator!=(const IntRect& a, const IntRect& b)
238{
239    return a.location() != b.location() || a.size() != b.size();
240}
241
242#if USE(CG)
243IntRect enclosingIntRect(const CGRect&);
244#endif
245
246#if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES))
247IntRect enclosingIntRect(const NSRect&);
248#endif
249
250} // namespace WebCore
251
252#endif // IntRect_h
253