| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/rect_base.h" | 5 #include "ui/gfx/rect_base.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 | 9 |
| 10 // This file provides the implementation for RectBaese template and | 10 // This file provides the implementation for RectBaese template and |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 namespace gfx { | 29 namespace gfx { |
| 30 | 30 |
| 31 template<typename Class, | 31 template<typename Class, |
| 32 typename PointClass, | 32 typename PointClass, |
| 33 typename SizeClass, | 33 typename SizeClass, |
| 34 typename InsetsClass, | 34 typename InsetsClass, |
| 35 typename VectorClass, |
| 35 typename Type> | 36 typename Type> |
| 36 RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( | 37 RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 37 const PointClass& origin, const SizeClass& size) | 38 RectBase(const PointClass& origin, const SizeClass& size) |
| 38 : origin_(origin), size_(size) { | 39 : origin_(origin), size_(size) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 template<typename Class, | 42 template<typename Class, |
| 42 typename PointClass, | 43 typename PointClass, |
| 43 typename SizeClass, | 44 typename SizeClass, |
| 44 typename InsetsClass, | 45 typename InsetsClass, |
| 46 typename VectorClass, |
| 45 typename Type> | 47 typename Type> |
| 46 RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( | 48 RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 47 const SizeClass& size) | 49 RectBase(const SizeClass& size) |
| 48 : size_(size) { | 50 : size_(size) { |
| 49 } | 51 } |
| 50 | 52 |
| 51 template<typename Class, | 53 template<typename Class, |
| 52 typename PointClass, | 54 typename PointClass, |
| 53 typename SizeClass, | 55 typename SizeClass, |
| 54 typename InsetsClass, | 56 typename InsetsClass, |
| 57 typename VectorClass, |
| 55 typename Type> | 58 typename Type> |
| 56 RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::RectBase( | 59 RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 57 const PointClass& origin) | 60 RectBase(const PointClass& origin) |
| 58 : origin_(origin) { | 61 : origin_(origin) { |
| 59 } | 62 } |
| 60 | 63 |
| 61 template<typename Class, | 64 template<typename Class, |
| 62 typename PointClass, | 65 typename PointClass, |
| 63 typename SizeClass, | 66 typename SizeClass, |
| 64 typename InsetsClass, | 67 typename InsetsClass, |
| 68 typename VectorClass, |
| 65 typename Type> | 69 typename Type> |
| 66 RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::~RectBase() {} | 70 RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 71 ~RectBase() {} |
| 67 | 72 |
| 68 template<typename Class, | 73 template<typename Class, |
| 69 typename PointClass, | 74 typename PointClass, |
| 70 typename SizeClass, | 75 typename SizeClass, |
| 71 typename InsetsClass, | 76 typename InsetsClass, |
| 77 typename VectorClass, |
| 72 typename Type> | 78 typename Type> |
| 73 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SetRect( | 79 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 74 Type x, Type y, Type width, Type height) { | 80 SetRect(Type x, Type y, Type width, Type height) { |
| 75 origin_.SetPoint(x, y); | 81 origin_.SetPoint(x, y); |
| 76 set_width(width); | 82 set_width(width); |
| 77 set_height(height); | 83 set_height(height); |
| 78 } | 84 } |
| 79 | 85 |
| 80 template<typename Class, | 86 template<typename Class, |
| 81 typename PointClass, | 87 typename PointClass, |
| 82 typename SizeClass, | 88 typename SizeClass, |
| 83 typename InsetsClass, | 89 typename InsetsClass, |
| 90 typename VectorClass, |
| 84 typename Type> | 91 typename Type> |
| 85 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Inset( | 92 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 86 const InsetsClass& insets) { | 93 Inset(const InsetsClass& insets) { |
| 87 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); | 94 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); |
| 88 } | 95 } |
| 89 | 96 |
| 90 template<typename Class, | 97 template<typename Class, |
| 91 typename PointClass, | 98 typename PointClass, |
| 92 typename SizeClass, | 99 typename SizeClass, |
| 93 typename InsetsClass, | 100 typename InsetsClass, |
| 101 typename VectorClass, |
| 94 typename Type> | 102 typename Type> |
| 95 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Inset( | 103 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 96 Type left, Type top, Type right, Type bottom) { | 104 Inset(Type left, Type top, Type right, Type bottom) { |
| 97 Offset(left, top); | 105 Offset(left, top); |
| 98 set_width(std::max(width() - left - right, static_cast<Type>(0))); | 106 set_width(std::max(width() - left - right, static_cast<Type>(0))); |
| 99 set_height(std::max(height() - top - bottom, static_cast<Type>(0))); | 107 set_height(std::max(height() - top - bottom, static_cast<Type>(0))); |
| 100 } | 108 } |
| 101 | 109 |
| 102 template<typename Class, | 110 template<typename Class, |
| 103 typename PointClass, | 111 typename PointClass, |
| 104 typename SizeClass, | 112 typename SizeClass, |
| 105 typename InsetsClass, | 113 typename InsetsClass, |
| 114 typename VectorClass, |
| 106 typename Type> | 115 typename Type> |
| 107 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Offset( | 116 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 108 Type horizontal, Type vertical) { | 117 Offset(Type horizontal, Type vertical) { |
| 109 origin_.Offset(horizontal, vertical); | 118 origin_.Offset(horizontal, vertical); |
| 110 } | 119 } |
| 111 | 120 |
| 112 template<typename Class, | 121 template<typename Class, |
| 113 typename PointClass, | 122 typename PointClass, |
| 114 typename SizeClass, | 123 typename SizeClass, |
| 115 typename InsetsClass, | 124 typename InsetsClass, |
| 125 typename VectorClass, |
| 116 typename Type> | 126 typename Type> |
| 117 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::operator<( | 127 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 118 const Class& other) const { | 128 operator<(const Class& other) const { |
| 119 if (origin_ == other.origin_) { | 129 if (origin_ == other.origin_) { |
| 120 if (width() == other.width()) { | 130 if (width() == other.width()) { |
| 121 return height() < other.height(); | 131 return height() < other.height(); |
| 122 } else { | 132 } else { |
| 123 return width() < other.width(); | 133 return width() < other.width(); |
| 124 } | 134 } |
| 125 } else { | 135 } else { |
| 126 return origin_ < other.origin_; | 136 return origin_ < other.origin_; |
| 127 } | 137 } |
| 128 } | 138 } |
| 129 | 139 |
| 130 template<typename Class, | 140 template<typename Class, |
| 131 typename PointClass, | 141 typename PointClass, |
| 132 typename SizeClass, | 142 typename SizeClass, |
| 133 typename InsetsClass, | 143 typename InsetsClass, |
| 144 typename VectorClass, |
| 134 typename Type> | 145 typename Type> |
| 135 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Contains( | 146 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 136 Type point_x, Type point_y) const { | 147 Contains(Type point_x, Type point_y) const { |
| 137 return (point_x >= x()) && (point_x < right()) && | 148 return (point_x >= x()) && (point_x < right()) && |
| 138 (point_y >= y()) && (point_y < bottom()); | 149 (point_y >= y()) && (point_y < bottom()); |
| 139 } | 150 } |
| 140 | 151 |
| 141 template<typename Class, | 152 template<typename Class, |
| 142 typename PointClass, | 153 typename PointClass, |
| 143 typename SizeClass, | 154 typename SizeClass, |
| 144 typename InsetsClass, | 155 typename InsetsClass, |
| 156 typename VectorClass, |
| 145 typename Type> | 157 typename Type> |
| 146 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Contains( | 158 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 147 const Class& rect) const { | 159 Contains(const Class& rect) const { |
| 148 return (rect.x() >= x() && rect.right() <= right() && | 160 return (rect.x() >= x() && rect.right() <= right() && |
| 149 rect.y() >= y() && rect.bottom() <= bottom()); | 161 rect.y() >= y() && rect.bottom() <= bottom()); |
| 150 } | 162 } |
| 151 | 163 |
| 152 template<typename Class, | 164 template<typename Class, |
| 153 typename PointClass, | 165 typename PointClass, |
| 154 typename SizeClass, | 166 typename SizeClass, |
| 155 typename InsetsClass, | 167 typename InsetsClass, |
| 168 typename VectorClass, |
| 156 typename Type> | 169 typename Type> |
| 157 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersects( | 170 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 158 const Class& rect) const { | 171 Intersects(const Class& rect) const { |
| 159 return !(rect.x() >= right() || rect.right() <= x() || | 172 return !(rect.x() >= right() || rect.right() <= x() || |
| 160 rect.y() >= bottom() || rect.bottom() <= y()); | 173 rect.y() >= bottom() || rect.bottom() <= y()); |
| 161 } | 174 } |
| 162 | 175 |
| 163 template<typename Class, | 176 template<typename Class, |
| 164 typename PointClass, | 177 typename PointClass, |
| 165 typename SizeClass, | 178 typename SizeClass, |
| 166 typename InsetsClass, | 179 typename InsetsClass, |
| 180 typename VectorClass, |
| 167 typename Type> | 181 typename Type> |
| 168 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( | 182 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 169 const Class& rect) { | 183 Intersect(const Class& rect) { |
| 170 if (IsEmpty() || rect.IsEmpty()) { | 184 if (IsEmpty() || rect.IsEmpty()) { |
| 171 SetRect(0, 0, 0, 0); | 185 SetRect(0, 0, 0, 0); |
| 172 return; | 186 return; |
| 173 } | 187 } |
| 174 | 188 |
| 175 Type rx = std::max(x(), rect.x()); | 189 Type rx = std::max(x(), rect.x()); |
| 176 Type ry = std::max(y(), rect.y()); | 190 Type ry = std::max(y(), rect.y()); |
| 177 Type rr = std::min(right(), rect.right()); | 191 Type rr = std::min(right(), rect.right()); |
| 178 Type rb = std::min(bottom(), rect.bottom()); | 192 Type rb = std::min(bottom(), rect.bottom()); |
| 179 | 193 |
| 180 if (rx >= rr || ry >= rb) | 194 if (rx >= rr || ry >= rb) |
| 181 rx = ry = rr = rb = 0; // non-intersecting | 195 rx = ry = rr = rb = 0; // non-intersecting |
| 182 | 196 |
| 183 SetRect(rx, ry, rr - rx, rb - ry); | 197 SetRect(rx, ry, rr - rx, rb - ry); |
| 184 } | 198 } |
| 185 | 199 |
| 186 template<typename Class, | 200 template<typename Class, |
| 187 typename PointClass, | 201 typename PointClass, |
| 188 typename SizeClass, | 202 typename SizeClass, |
| 189 typename InsetsClass, | 203 typename InsetsClass, |
| 204 typename VectorClass, |
| 190 typename Type> | 205 typename Type> |
| 191 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union( | 206 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 192 const Class& rect) { | 207 Union(const Class& rect) { |
| 193 if (IsEmpty()) { | 208 if (IsEmpty()) { |
| 194 *this = rect; | 209 *this = rect; |
| 195 return; | 210 return; |
| 196 } | 211 } |
| 197 if (rect.IsEmpty()) | 212 if (rect.IsEmpty()) |
| 198 return; | 213 return; |
| 199 | 214 |
| 200 Type rx = std::min(x(), rect.x()); | 215 Type rx = std::min(x(), rect.x()); |
| 201 Type ry = std::min(y(), rect.y()); | 216 Type ry = std::min(y(), rect.y()); |
| 202 Type rr = std::max(right(), rect.right()); | 217 Type rr = std::max(right(), rect.right()); |
| 203 Type rb = std::max(bottom(), rect.bottom()); | 218 Type rb = std::max(bottom(), rect.bottom()); |
| 204 | 219 |
| 205 SetRect(rx, ry, rr - rx, rb - ry); | 220 SetRect(rx, ry, rr - rx, rb - ry); |
| 206 } | 221 } |
| 207 | 222 |
| 208 template<typename Class, | 223 template<typename Class, |
| 209 typename PointClass, | 224 typename PointClass, |
| 210 typename SizeClass, | 225 typename SizeClass, |
| 211 typename InsetsClass, | 226 typename InsetsClass, |
| 227 typename VectorClass, |
| 212 typename Type> | 228 typename Type> |
| 213 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( | 229 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 214 const Class& rect) { | 230 Subtract(const Class& rect) { |
| 215 if (!Intersects(rect)) | 231 if (!Intersects(rect)) |
| 216 return; | 232 return; |
| 217 if (rect.Contains(*static_cast<const Class*>(this))) { | 233 if (rect.Contains(*static_cast<const Class*>(this))) { |
| 218 SetRect(0, 0, 0, 0); | 234 SetRect(0, 0, 0, 0); |
| 219 return; | 235 return; |
| 220 } | 236 } |
| 221 | 237 |
| 222 Type rx = x(); | 238 Type rx = x(); |
| 223 Type ry = y(); | 239 Type ry = y(); |
| 224 Type rr = right(); | 240 Type rr = right(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 239 rb = rect.y(); | 255 rb = rect.y(); |
| 240 } | 256 } |
| 241 } | 257 } |
| 242 SetRect(rx, ry, rr - rx, rb - ry); | 258 SetRect(rx, ry, rr - rx, rb - ry); |
| 243 } | 259 } |
| 244 | 260 |
| 245 template<typename Class, | 261 template<typename Class, |
| 246 typename PointClass, | 262 typename PointClass, |
| 247 typename SizeClass, | 263 typename SizeClass, |
| 248 typename InsetsClass, | 264 typename InsetsClass, |
| 265 typename VectorClass, |
| 249 typename Type> | 266 typename Type> |
| 250 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( | 267 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 251 const Class& rect) { | 268 AdjustToFit(const Class& rect) { |
| 252 Type new_x = x(); | 269 Type new_x = x(); |
| 253 Type new_y = y(); | 270 Type new_y = y(); |
| 254 Type new_width = width(); | 271 Type new_width = width(); |
| 255 Type new_height = height(); | 272 Type new_height = height(); |
| 256 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); | 273 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); |
| 257 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); | 274 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); |
| 258 SetRect(new_x, new_y, new_width, new_height); | 275 SetRect(new_x, new_y, new_width, new_height); |
| 259 } | 276 } |
| 260 | 277 |
| 261 template<typename Class, | 278 template<typename Class, |
| 262 typename PointClass, | 279 typename PointClass, |
| 263 typename SizeClass, | 280 typename SizeClass, |
| 264 typename InsetsClass, | 281 typename InsetsClass, |
| 282 typename VectorClass, |
| 265 typename Type> | 283 typename Type> |
| 266 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: | 284 PointClass RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, |
| 267 CenterPoint() const { | 285 Type>::CenterPoint() const { |
| 268 return PointClass(x() + width() / 2, y() + height() / 2); | 286 return PointClass(x() + width() / 2, y() + height() / 2); |
| 269 } | 287 } |
| 270 | 288 |
| 271 template<typename Class, | 289 template<typename Class, |
| 272 typename PointClass, | 290 typename PointClass, |
| 273 typename SizeClass, | 291 typename SizeClass, |
| 274 typename InsetsClass, | 292 typename InsetsClass, |
| 293 typename VectorClass, |
| 275 typename Type> | 294 typename Type> |
| 276 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: | 295 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 277 ClampToCenteredSize(const SizeClass& size) { | 296 ClampToCenteredSize(const SizeClass& size) { |
| 278 Type new_width = std::min(width(), size.width()); | 297 Type new_width = std::min(width(), size.width()); |
| 279 Type new_height = std::min(height(), size.height()); | 298 Type new_height = std::min(height(), size.height()); |
| 280 Type new_x = x() + (width() - new_width) / 2; | 299 Type new_x = x() + (width() - new_width) / 2; |
| 281 Type new_y = y() + (height() - new_height) / 2; | 300 Type new_y = y() + (height() - new_height) / 2; |
| 282 SetRect(new_x, new_y, new_width, new_height); | 301 SetRect(new_x, new_y, new_width, new_height); |
| 283 } | 302 } |
| 284 | 303 |
| 285 template<typename Class, | 304 template<typename Class, |
| 286 typename PointClass, | 305 typename PointClass, |
| 287 typename SizeClass, | 306 typename SizeClass, |
| 288 typename InsetsClass, | 307 typename InsetsClass, |
| 308 typename VectorClass, |
| 289 typename Type> | 309 typename Type> |
| 290 void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SplitVertically( | 310 void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 291 Class* left_half, Class* right_half) const { | 311 SplitVertically(Class* left_half, Class* right_half) const { |
| 292 DCHECK(left_half); | 312 DCHECK(left_half); |
| 293 DCHECK(right_half); | 313 DCHECK(right_half); |
| 294 | 314 |
| 295 left_half->SetRect(this->x(), this->y(), this->width() / 2, this->height()); | 315 left_half->SetRect(this->x(), this->y(), this->width() / 2, this->height()); |
| 296 right_half->SetRect(left_half->right(), | 316 right_half->SetRect(left_half->right(), |
| 297 this->y(), | 317 this->y(), |
| 298 this->width() - left_half->width(), | 318 this->width() - left_half->width(), |
| 299 this->height()); | 319 this->height()); |
| 300 } | 320 } |
| 301 | 321 |
| 302 template<typename Class, | 322 template<typename Class, |
| 303 typename PointClass, | 323 typename PointClass, |
| 304 typename SizeClass, | 324 typename SizeClass, |
| 305 typename InsetsClass, | 325 typename InsetsClass, |
| 326 typename VectorClass, |
| 306 typename Type> | 327 typename Type> |
| 307 bool RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::SharesEdgeWith( | 328 bool RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: |
| 308 const Class& rect) const { | 329 SharesEdgeWith(const Class& rect) const { |
| 309 return (y() == rect.y() && height() == rect.height() && | 330 return (y() == rect.y() && height() == rect.height() && |
| 310 (x() == rect.right() || right() == rect.x())) || | 331 (x() == rect.right() || right() == rect.x())) || |
| 311 (x() == rect.x() && width() == rect.width() && | 332 (x() == rect.x() && width() == rect.width() && |
| 312 (y() == rect.bottom() || bottom() == rect.y())); | 333 (y() == rect.bottom() || bottom() == rect.y())); |
| 313 } | 334 } |
| 314 | 335 |
| 315 } // namespace gfx | 336 } // namespace gfx |
| OLD | NEW |