KHTML
FloatRect.h
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. 00003 * Copyright (C) 2005 Nokia. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 00015 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00017 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 00018 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00019 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00020 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00021 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00022 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00023 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00024 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 00027 #ifndef FloatRect_h 00028 #define FloatRect_h 00029 00030 #include "FloatPoint.h" 00031 #include "IntRect.h" 00032 00033 #if PLATFORM(CG) 00034 typedef struct CGRect CGRect; 00035 #endif 00036 00037 #if PLATFORM(MAC) 00038 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 00039 typedef struct CGRect NSRect; 00040 #else 00041 typedef struct _NSRect NSRect; 00042 #endif 00043 #endif 00044 00045 #if PLATFORM(QT) 00046 QT_BEGIN_NAMESPACE 00047 class QRectF; 00048 QT_END_NAMESPACE 00049 #endif 00050 00051 #if PLATFORM(WX) && USE(WXGC) 00052 class wxRect2DDouble; 00053 #endif 00054 00055 namespace WebCore { 00056 00057 class IntRect; 00058 00059 class FloatRect { 00060 public: 00061 FloatRect() { } 00062 FloatRect(const FloatPoint& location, const FloatSize& size) 00063 : m_location(location), m_size(size) { } 00064 FloatRect(float x, float y, float width, float height) 00065 : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { } 00066 FloatRect(const IntRect&); 00067 00068 static FloatRect narrowPrecision(double x, double y, double width, double height); 00069 00070 FloatPoint location() const { return m_location; } 00071 FloatSize size() const { return m_size; } 00072 00073 void setLocation(const FloatPoint& location) { m_location = location; } 00074 void setSize(const FloatSize& size) { m_size = size; } 00075 00076 float x() const { return m_location.x(); } 00077 float y() const { return m_location.y(); } 00078 float width() const { return m_size.width(); } 00079 float height() const { return m_size.height(); } 00080 00081 void setX(float x) { m_location.setX(x); } 00082 void setY(float y) { m_location.setY(y); } 00083 void setWidth(float width) { m_size.setWidth(width); } 00084 void setHeight(float height) { m_size.setHeight(height); } 00085 00086 bool isEmpty() const { return m_size.isEmpty(); } 00087 00088 float right() const { return x() + width(); } 00089 float bottom() const { return y() + height(); } 00090 00091 void move(const FloatSize& delta) { m_location += delta; } 00092 void move(float dx, float dy) { m_location.move(dx, dy); } 00093 00094 bool intersects(const FloatRect&) const; 00095 bool contains(const FloatRect&) const; 00096 00097 void intersect(const FloatRect&); 00098 void unite(const FloatRect&); 00099 00100 // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version 00101 // is really checking for containment of 1x1 rect, but that doesn't make sense with floats. 00102 bool contains(float px, float py) const 00103 { return px >= x() && px <= right() && py >= y() && py <= bottom(); } 00104 bool contains(const FloatPoint& point) const { return contains(point.x(), point.y()); } 00105 00106 00107 void inflateX(float dx) { 00108 m_location.setX(m_location.x() - dx); 00109 m_size.setWidth(m_size.width() + dx + dx); 00110 } 00111 void inflateY(float dy) { 00112 m_location.setY(m_location.y() - dy); 00113 m_size.setHeight(m_size.height() + dy + dy); 00114 } 00115 void inflate(float d) { inflateX(d); inflateY(d); } 00116 void scale(float s); 00117 00118 #if PLATFORM(CG) 00119 FloatRect(const CGRect&); 00120 operator CGRect() const; 00121 #endif 00122 00123 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 00124 FloatRect(const NSRect&); 00125 operator NSRect() const; 00126 #endif 00127 00128 #if PLATFORM(QT) 00129 FloatRect(const QRectF&); 00130 operator QRectF() const; 00131 #endif 00132 #if PLATFORM(SYMBIAN) 00133 FloatRect(const TRect&); 00134 operator TRect() const; 00135 TRect rect() const; 00136 #endif 00137 00138 #if PLATFORM(WX) && USE(WXGC) 00139 FloatRect(const wxRect2DDouble&); 00140 operator wxRect2DDouble() const; 00141 #endif 00142 00143 private: 00144 FloatPoint m_location; 00145 FloatSize m_size; 00146 }; 00147 00148 inline FloatRect intersection(const FloatRect& a, const FloatRect& b) 00149 { 00150 FloatRect c = a; 00151 c.intersect(b); 00152 return c; 00153 } 00154 00155 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b) 00156 { 00157 FloatRect c = a; 00158 c.unite(b); 00159 return c; 00160 } 00161 00162 inline bool operator==(const FloatRect& a, const FloatRect& b) 00163 { 00164 return a.location() == b.location() && a.size() == b.size(); 00165 } 00166 00167 inline bool operator!=(const FloatRect& a, const FloatRect& b) 00168 { 00169 return a.location() != b.location() || a.size() != b.size(); 00170 } 00171 00172 IntRect enclosingIntRect(const FloatRect&); 00173 00174 } 00175 00176 #endif
KDE 4.6 API Reference