• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
34 typedef struct CGRect CGRect;
35 #endif
36 
37 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
38 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39 typedef struct CGRect NSRect;
40 #else
41 typedef struct _NSRect NSRect;
42 #endif
43 #endif
44 
45 #if PLATFORM(WIN)
46 typedef struct tagRECT RECT;
47 #elif PLATFORM(QT)
48 QT_BEGIN_NAMESPACE
49 class QRect;
50 QT_END_NAMESPACE
51 #elif PLATFORM(GTK)
52 typedef struct _GdkRectangle GdkRectangle;
53 #elif PLATFORM(HAIKU)
54 class BRect;
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 #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 
170 private:
171     IntPoint m_location;
172     IntSize m_size;
173 };
174 
intersection(const IntRect & a,const IntRect & b)175 inline IntRect intersection(const IntRect& a, const IntRect& b)
176 {
177     IntRect c = a;
178     c.intersect(b);
179     return c;
180 }
181 
unionRect(const IntRect & a,const IntRect & b)182 inline IntRect unionRect(const IntRect& a, const IntRect& b)
183 {
184     IntRect c = a;
185     c.unite(b);
186     return c;
187 }
188 
189 IntRect unionRect(const Vector<IntRect>&);
190 
191 inline bool operator==(const IntRect& a, const IntRect& b)
192 {
193     return a.location() == b.location() && a.size() == b.size();
194 }
195 
196 inline 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)
202 IntRect enclosingIntRect(const CGRect&);
203 #endif
204 
205 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
206         || (PLATFORM(CHROMIUM) && OS(DARWIN))
207 IntRect enclosingIntRect(const NSRect&);
208 #endif
209 
210 } // namespace WebCore
211 
212 #endif // IntRect_h
213