2 * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include <wtf/Vector.h>
32 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
33 typedef struct CGRect CGRect;
36 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
38 typedef struct CGRect NSRect;
40 typedef struct _NSRect NSRect;
45 typedef struct tagRECT RECT;
51 #ifdef GTK_API_VERSION_2
52 typedef struct _GdkRectangle GdkRectangle;
54 typedef struct _cairo_rectangle_int cairo_rectangle_int_t;
55 typedef cairo_rectangle_int_t GdkRectangle;
60 typedef struct _Eina_Rectangle Eina_Rectangle;
79 IntRect(const IntPoint& location, const IntSize& size)
80 : m_location(location), m_size(size) { }
81 IntRect(int x, int y, int width, int height)
82 : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
84 explicit IntRect(const FloatRect& rect); // don't do this implicitly since it's lossy
86 IntPoint location() const { return m_location; }
87 IntSize size() const { return m_size; }
89 void setLocation(const IntPoint& location) { m_location = location; }
90 void setSize(const IntSize& size) { m_size = size; }
92 int x() const { return m_location.x(); }
93 int y() const { return m_location.y(); }
94 int maxX() const { return x() + width(); }
95 int maxY() const { return y() + height(); }
96 int width() const { return m_size.width(); }
97 int height() const { return m_size.height(); }
99 void setX(int x) { m_location.setX(x); }
100 void setY(int y) { m_location.setY(y); }
101 void setWidth(int width) { m_size.setWidth(width); }
102 void setHeight(int height) { m_size.setHeight(height); }
104 bool isEmpty() const { return m_size.isEmpty(); }
106 // NOTE: The result is rounded to integer values, and thus may be not the exact
108 IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
110 void move(const IntSize& size) { m_location += size; }
111 void move(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
112 void move(int dx, int dy) { m_location.move(dx, dy); }
114 void expand(const IntSize& size) { m_location += size; }
115 void expand(int dw, int dh) { m_size.expand(dw, dh); }
117 void shiftXEdgeTo(int edge)
119 int delta = edge - x();
121 setWidth(std::max(0, width() - delta));
123 void shiftMaxXEdgeTo(int edge)
125 int delta = edge - maxX();
126 setWidth(std::max(0, width() + delta));
128 void shiftYEdgeTo(int edge)
130 int delta = edge - y();
132 setHeight(std::max(0, height() - delta));
134 void shiftMaxYEdgeTo(int edge)
136 int delta = edge - maxY();
137 setHeight(std::max(0, height() + delta));
140 IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
141 IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
142 IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
143 IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
145 bool intersects(const IntRect&) const;
146 bool contains(const IntRect&) const;
148 // This checks to see if the rect contains x,y in the traditional sense.
149 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
150 bool contains(int px, int py) const
151 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
152 bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
154 void intersect(const IntRect&);
155 void unite(const IntRect&);
156 void uniteIfNonZero(const IntRect&);
158 void inflateX(int dx)
160 m_location.setX(m_location.x() - dx);
161 m_size.setWidth(m_size.width() + dx + dx);
163 void inflateY(int dy)
165 m_location.setY(m_location.y() - dy);
166 m_size.setHeight(m_size.height() + dy + dy);
168 void inflate(int d) { inflateX(d); inflateY(d); }
171 IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
174 IntRect(const wxRect&);
175 operator wxRect() const;
179 IntRect(const RECT&);
180 operator RECT() const;
182 IntRect(const QRect&);
183 operator QRect() const;
185 IntRect(const GdkRectangle&);
186 operator GdkRectangle() const;
187 #elif PLATFORM(HAIKU)
188 explicit IntRect(const BRect&);
189 operator BRect() const;
191 explicit IntRect(const Eina_Rectangle&);
192 operator Eina_Rectangle() const;
195 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
196 operator CGRect() const;
200 IntRect(const SkIRect&);
201 operator SkRect() const;
202 operator SkIRect() const;
205 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
206 || (PLATFORM(CHROMIUM) && OS(DARWIN))
207 operator NSRect() const;
215 inline IntRect intersection(const IntRect& a, const IntRect& b)
222 inline IntRect unionRect(const IntRect& a, const IntRect& b)
229 IntRect unionRect(const Vector<IntRect>&);
231 inline bool operator==(const IntRect& a, const IntRect& b)
233 return a.location() == b.location() && a.size() == b.size();
236 inline bool operator!=(const IntRect& a, const IntRect& b)
238 return a.location() != b.location() || a.size() != b.size();
241 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
242 IntRect enclosingIntRect(const CGRect&);
245 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
246 || (PLATFORM(CHROMIUM) && OS(DARWIN))
247 IntRect enclosingIntRect(const NSRect&);
250 } // namespace WebCore