1 /* 2 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef IntSize_h 27 #define IntSize_h 28 29 #include <wtf/Platform.h> 30 31 #if PLATFORM(CG) 32 typedef struct CGSize CGSize; 33 #endif 34 35 #if PLATFORM(MAC) 36 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES 37 typedef struct CGSize NSSize; 38 #else 39 typedef struct _NSSize NSSize; 40 #endif 41 #endif 42 43 #if PLATFORM(WIN) 44 typedef struct tagSIZE SIZE; 45 #elif PLATFORM(QT) 46 #include <qglobal.h> 47 QT_BEGIN_NAMESPACE 48 class QSize; 49 QT_END_NAMESPACE 50 #elif PLATFORM(HAIKU) 51 class BSize; 52 #endif 53 54 #if PLATFORM(WX) 55 class wxSize; 56 #endif 57 58 #if PLATFORM(BREWMP) 59 typedef struct AEESize AEESize; 60 #endif 61 62 namespace WebCore { 63 64 class IntSize { 65 public: IntSize()66 IntSize() : m_width(0), m_height(0) { } IntSize(int width,int height)67 IntSize(int width, int height) : m_width(width), m_height(height) { } 68 width()69 int width() const { return m_width; } height()70 int height() const { return m_height; } 71 setWidth(int width)72 void setWidth(int width) { m_width = width; } setHeight(int height)73 void setHeight(int height) { m_height = height; } 74 isEmpty()75 bool isEmpty() const { return m_width <= 0 || m_height <= 0; } isZero()76 bool isZero() const { return !m_width && !m_height; } 77 expand(int width,int height)78 void expand(int width, int height) 79 { 80 m_width += width; 81 m_height += height; 82 } 83 scale(float scale)84 void scale(float scale) 85 { 86 m_width = static_cast<int>(static_cast<float>(m_width) * scale); 87 m_height = static_cast<int>(static_cast<float>(m_height) * scale); 88 } 89 expandedTo(const IntSize & other)90 IntSize expandedTo(const IntSize& other) const 91 { 92 return IntSize(m_width > other.m_width ? m_width : other.m_width, 93 m_height > other.m_height ? m_height : other.m_height); 94 } 95 shrunkTo(const IntSize & other)96 IntSize shrunkTo(const IntSize& other) const 97 { 98 return IntSize(m_width < other.m_width ? m_width : other.m_width, 99 m_height < other.m_height ? m_height : other.m_height); 100 } 101 clampNegativeToZero()102 void clampNegativeToZero() 103 { 104 *this = expandedTo(IntSize()); 105 } 106 107 #if PLATFORM(CG) 108 explicit IntSize(const CGSize&); // don't do this implicitly since it's lossy 109 operator CGSize() const; 110 #endif 111 112 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES) 113 explicit IntSize(const NSSize &); // don't do this implicitly since it's lossy 114 operator NSSize() const; 115 #endif 116 117 #if PLATFORM(WIN) 118 IntSize(const SIZE&); 119 operator SIZE() const; 120 #endif 121 122 #if PLATFORM(QT) 123 IntSize(const QSize&); 124 operator QSize() const; 125 #endif 126 127 #if PLATFORM(HAIKU) 128 explicit IntSize(const BSize&); 129 operator BSize() const; 130 #endif 131 132 #if PLATFORM(WX) 133 IntSize(const wxSize&); 134 operator wxSize() const; 135 #endif 136 137 #if PLATFORM(BREWMP) 138 IntSize(const AEESize&); 139 operator AEESize() const; 140 #endif 141 142 private: 143 int m_width, m_height; 144 }; 145 146 inline IntSize& operator+=(IntSize& a, const IntSize& b) 147 { 148 a.setWidth(a.width() + b.width()); 149 a.setHeight(a.height() + b.height()); 150 return a; 151 } 152 153 inline IntSize& operator-=(IntSize& a, const IntSize& b) 154 { 155 a.setWidth(a.width() - b.width()); 156 a.setHeight(a.height() - b.height()); 157 return a; 158 } 159 160 inline IntSize operator+(const IntSize& a, const IntSize& b) 161 { 162 return IntSize(a.width() + b.width(), a.height() + b.height()); 163 } 164 165 inline IntSize operator-(const IntSize& a, const IntSize& b) 166 { 167 return IntSize(a.width() - b.width(), a.height() - b.height()); 168 } 169 170 inline IntSize operator-(const IntSize& size) 171 { 172 return IntSize(-size.width(), -size.height()); 173 } 174 175 inline bool operator==(const IntSize& a, const IntSize& b) 176 { 177 return a.width() == b.width() && a.height() == b.height(); 178 } 179 180 inline bool operator!=(const IntSize& a, const IntSize& b) 181 { 182 return a.width() != b.width() || a.height() != b.height(); 183 } 184 185 } // namespace WebCore 186 187 #endif // IntSize_h 188