1 /* 2 * ReactOS ATL 3 * 4 * Copyright 2016 Mark Jansen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #pragma once 22 23 24 class CSize; 25 class CRect; 26 27 28 class CPoint : public tagPOINT 29 { 30 public: 31 CPoint()32 CPoint() noexcept 33 { 34 x = y = 0; 35 } 36 CPoint(int initX,int initY)37 CPoint(int initX, int initY) noexcept 38 { 39 x = initX; 40 y = initY; 41 } 42 CPoint(POINT initPt)43 CPoint(POINT initPt) noexcept 44 { 45 *((POINT*)this) = initPt; 46 } 47 CPoint(SIZE initSize)48 CPoint(SIZE initSize) noexcept 49 { 50 *((SIZE*)this) = initSize; 51 } 52 CPoint(LPARAM dwPoint)53 CPoint(LPARAM dwPoint) noexcept 54 { 55 x = LOWORD(dwPoint); 56 y = HIWORD(dwPoint); 57 } 58 Offset(int xOffset,int yOffset)59 void Offset(int xOffset, int yOffset) noexcept 60 { 61 x += xOffset; 62 y += yOffset; 63 } 64 Offset(POINT point)65 void Offset(POINT point) noexcept 66 { 67 Offset(point.x, point.y); 68 } 69 Offset(SIZE size)70 void Offset(SIZE size) noexcept 71 { 72 Offset(size.cx, size.cy); 73 } 74 75 BOOL operator==(POINT point) const noexcept 76 { 77 return (x == point.x && y == point.y); 78 } 79 80 BOOL operator!=(POINT point) const noexcept 81 { 82 return !(*this == point); 83 } 84 85 void operator+=(SIZE size) noexcept 86 { 87 Offset(size); 88 } 89 90 void operator+=(POINT point) noexcept 91 { 92 Offset(point); 93 } 94 95 void operator-=(SIZE size) noexcept 96 { 97 Offset(-size.cx, -size.cy); 98 } 99 100 void operator-=(POINT point) noexcept 101 { 102 Offset(-point.x, -point.y); 103 } 104 105 CPoint operator+(SIZE size) const noexcept 106 { 107 return CPoint(x + size.cx, y + size.cy); 108 } 109 110 CPoint operator+(POINT point) const noexcept 111 { 112 return CPoint(x + point.x, y + point.y); 113 } 114 115 CRect operator+(const RECT* lpRect) const noexcept; 116 117 CSize operator-(POINT point) const noexcept; 118 119 CPoint operator-(SIZE size) const noexcept 120 { 121 return CPoint(x - size.cx, y - size.cy); 122 } 123 124 CRect operator-(const RECT* lpRect) const noexcept; 125 126 CPoint operator-() const noexcept 127 { 128 return CPoint(-x, -y); 129 } 130 }; 131 132 class CSize : public tagSIZE 133 { 134 public: CSize()135 CSize() noexcept 136 { 137 cx = cy = 0; 138 } 139 CSize(int initCX,int initCY)140 CSize(int initCX, int initCY) noexcept 141 { 142 cx = initCX; 143 cy = initCY; 144 } 145 CSize(SIZE initSize)146 CSize(SIZE initSize) noexcept 147 { 148 *((SIZE*)this) = initSize; 149 } 150 CSize(POINT initPt)151 CSize(POINT initPt) noexcept 152 { 153 *((POINT*)this) = initPt; 154 } 155 CSize(DWORD dwSize)156 CSize(DWORD dwSize) noexcept 157 { 158 cx = LOWORD(dwSize); 159 cy = HIWORD(dwSize); 160 } 161 162 BOOL operator==(SIZE size) const noexcept 163 { 164 return (size.cx == cx && size.cy == cy); 165 } 166 167 BOOL operator!=(SIZE size) const noexcept 168 { 169 return !(*this == size); 170 } 171 172 void operator+=(SIZE size) noexcept 173 { 174 cx += size.cx; 175 cy += size.cy; 176 } 177 178 void operator-=(SIZE size) noexcept 179 { 180 cx -= size.cx; 181 cy -= size.cy; 182 } 183 184 CSize operator+(SIZE size) const noexcept 185 { 186 return CSize(cx + size.cx, cy + size.cy); 187 } 188 189 CPoint operator+(POINT point) const noexcept 190 { 191 return CPoint(cx + point.x, cy + point.y); 192 } 193 194 CRect operator+(const RECT* lpRect) const noexcept; 195 196 CSize operator-(SIZE size) const noexcept 197 { 198 return CSize(cx - size.cx, cy - size.cy); 199 } 200 201 CPoint operator-(POINT point) const noexcept 202 { 203 return CPoint(cx - point.x, cy - point.y); 204 } 205 206 CRect operator-(const RECT* lpRect) const noexcept; 207 208 CSize operator-() const noexcept 209 { 210 return CSize(-cx, -cy); 211 } 212 }; 213 214 215 inline CSize CPoint::operator-(POINT point) const noexcept 216 { 217 return CSize(x - point.x, y - point.y); 218 } 219 220 221 class CRect : public tagRECT 222 { 223 public: CRect()224 CRect() noexcept 225 { 226 left = top = right = bottom = 0; 227 } 228 CRect(int l,int t,int r,int b)229 CRect(int l, int t, int r, int b) noexcept 230 { 231 left = l; 232 top = t; 233 right = r; 234 bottom = b; 235 } 236 CRect(const RECT & srcRect)237 CRect(const RECT& srcRect) noexcept 238 { 239 left = srcRect.left; 240 top = srcRect.top; 241 right = srcRect.right; 242 bottom = srcRect.bottom; 243 } 244 CRect(LPCRECT lpSrcRect)245 CRect(LPCRECT lpSrcRect) noexcept 246 { 247 left = lpSrcRect->left; 248 top = lpSrcRect->top; 249 right = lpSrcRect->right; 250 bottom = lpSrcRect->bottom; 251 } 252 CRect(POINT point,SIZE size)253 CRect(POINT point, SIZE size) noexcept 254 { 255 left = point.x; 256 top = point.y; 257 right = point.x + size.cx; 258 bottom = point.y + size.cy; 259 } 260 CRect(POINT topLeft,POINT bottomRight)261 CRect(POINT topLeft, POINT bottomRight) noexcept 262 { 263 left = topLeft.x; 264 top = topLeft.y; 265 right = bottomRight.x; 266 bottom = bottomRight.y; 267 } 268 BottomRight()269 CPoint& BottomRight() noexcept 270 { 271 return ((CPoint*)this)[1]; 272 } 273 BottomRight()274 const CPoint& BottomRight() const noexcept 275 { 276 return ((const CPoint*)this)[1]; 277 } 278 CenterPoint()279 CPoint CenterPoint() const noexcept 280 { 281 return CPoint(left + (Width() >> 1), top + (Height() >> 1)); 282 } 283 CopyRect(LPCRECT lpSrcRect)284 void CopyRect(LPCRECT lpSrcRect) noexcept 285 { 286 ::CopyRect(this, lpSrcRect); 287 } 288 DeflateRect(int x,int y)289 void DeflateRect(int x, int y) noexcept 290 { 291 ::InflateRect(this, -x, -y); 292 } 293 DeflateRect(SIZE size)294 void DeflateRect(SIZE size) noexcept 295 { 296 ::InflateRect(this, -size.cx, -size.cy); 297 } 298 DeflateRect(LPCRECT lpRect)299 void DeflateRect(LPCRECT lpRect) noexcept 300 { 301 DeflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom); 302 } 303 DeflateRect(int l,int t,int r,int b)304 void DeflateRect(int l, int t, int r, int b) noexcept 305 { 306 left += l; 307 top += t; 308 right -= r; 309 bottom -= b; 310 } 311 EqualRect(LPCRECT lpRect)312 BOOL EqualRect(LPCRECT lpRect) const noexcept 313 { 314 return ::EqualRect(this, lpRect); 315 } 316 317 Height()318 int Height() const noexcept 319 { 320 return bottom - top; 321 } 322 InflateRect(int x,int y)323 void InflateRect(int x, int y) noexcept 324 { 325 ::InflateRect(this, x, y); 326 } 327 InflateRect(SIZE size)328 void InflateRect(SIZE size) noexcept 329 { 330 ::InflateRect(this, size.cx, size.cy); 331 } 332 InflateRect(LPCRECT lpRect)333 void InflateRect(LPCRECT lpRect) noexcept 334 { 335 InflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom); 336 } 337 InflateRect(int l,int t,int r,int b)338 void InflateRect(int l, int t, int r, int b) noexcept 339 { 340 left -= l; 341 top -= t; 342 right += r; 343 bottom += b; 344 } 345 IntersectRect(LPCRECT lpRect1,LPCRECT lpRect2)346 BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2) noexcept 347 { 348 return ::IntersectRect(this, lpRect1, lpRect2); 349 } 350 IsRectEmpty()351 BOOL IsRectEmpty() const noexcept 352 { 353 return ::IsRectEmpty(this); 354 } 355 IsRectNull()356 BOOL IsRectNull() const noexcept 357 { 358 return (left == 0 && right == 0 && 359 top == 0 && bottom == 0); 360 } 361 MoveToX(int x)362 void MoveToX(int x) noexcept 363 { 364 int dx = x - left; 365 left = x; 366 right += dx; 367 } 368 MoveToY(int y)369 void MoveToY(int y) noexcept 370 { 371 int dy = y - top; 372 top = y; 373 bottom += dy; 374 } 375 MoveToXY(int x,int y)376 void MoveToXY(int x, int y) noexcept 377 { 378 MoveToX(x); 379 MoveToY(y); 380 } 381 MoveToXY(POINT point)382 void MoveToXY(POINT point) noexcept 383 { 384 MoveToXY(point.x, point.y); 385 } 386 NormalizeRect()387 void NormalizeRect() noexcept 388 { 389 if (left > right) 390 { 391 LONG tmp = left; 392 left = right; 393 right = tmp; 394 } 395 if (top > bottom) 396 { 397 LONG tmp = top; 398 top = bottom; 399 bottom = tmp; 400 } 401 } 402 OffsetRect(int x,int y)403 void OffsetRect(int x, int y) noexcept 404 { 405 ::OffsetRect(this, x, y); 406 } 407 OffsetRect(POINT point)408 void OffsetRect(POINT point) noexcept 409 { 410 ::OffsetRect(this, point.x, point.y); 411 } 412 OffsetRect(SIZE size)413 void OffsetRect(SIZE size) noexcept 414 { 415 ::OffsetRect(this, size.cx, size.cy); 416 } 417 PtInRect(POINT point)418 BOOL PtInRect(POINT point) const noexcept 419 { 420 return ::PtInRect(this, point); 421 } 422 SetRect(int x1,int y1,int x2,int y2)423 void SetRect(int x1, int y1, int x2, int y2) noexcept 424 { 425 left = x1; 426 top = y1; 427 right = x2; 428 bottom = y2; 429 } 430 SetRectEmpty()431 void SetRectEmpty() noexcept 432 { 433 ZeroMemory(this, sizeof(*this)); 434 } 435 Size()436 CSize Size() const noexcept 437 { 438 return CSize(Width(), Height()); 439 } 440 SubtractRect(LPCRECT lpRectSrc1,LPCRECT lpRectSrc2)441 BOOL SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2) noexcept 442 { 443 return ::SubtractRect(this, lpRectSrc1, lpRectSrc2); 444 } 445 TopLeft()446 CPoint& TopLeft() noexcept 447 { 448 return ((CPoint*)this)[0]; 449 } 450 TopLeft()451 const CPoint& TopLeft() const noexcept 452 { 453 return ((const CPoint*)this)[0]; 454 } 455 UnionRect(LPCRECT lpRect1,LPCRECT lpRect2)456 BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2) noexcept 457 { 458 return ::UnionRect(this, lpRect1, lpRect2); 459 } 460 Width()461 int Width() const noexcept 462 { 463 return right - left; 464 } 465 466 467 BOOL operator==(const RECT& rect) const noexcept 468 { 469 return (left == rect.left && 470 top == rect.top && 471 right == rect.right && 472 bottom == rect.bottom); 473 } 474 475 BOOL operator!=(const RECT& rect) const noexcept 476 { 477 return !(*this == rect); 478 } 479 480 void operator=(const RECT& srcRect) noexcept 481 { 482 left = srcRect.left; 483 top = srcRect.top; 484 right = srcRect.right; 485 bottom = srcRect.bottom; 486 } 487 488 void operator+=(POINT point) noexcept 489 { 490 OffsetRect(point); 491 } 492 493 void operator+=(SIZE size) noexcept 494 { 495 OffsetRect(size); 496 } 497 498 void operator+=(LPCRECT lpRect) noexcept 499 { 500 InflateRect(lpRect); 501 } 502 503 void operator-=(POINT point) noexcept 504 { 505 OffsetRect(-point.x, -point.y); 506 } 507 508 void operator-=(SIZE size) noexcept 509 { 510 OffsetRect(-size.cx, -size.cy); 511 } 512 513 void operator-=(LPCRECT lpRect) noexcept 514 { 515 DeflateRect(lpRect); 516 } 517 518 519 CRect operator+(POINT point) const noexcept 520 { 521 CRect r(this); 522 r.OffsetRect(point); 523 return r; 524 } 525 526 CRect operator+(LPCRECT lpRect) const noexcept 527 { 528 CRect r(this); 529 r.InflateRect(lpRect); 530 return r; 531 } 532 533 CRect operator+(SIZE size) const noexcept 534 { 535 CRect r(this); 536 r.OffsetRect(size); 537 return r; 538 } 539 540 CRect operator-(POINT point) const noexcept 541 { 542 CRect r(this); 543 r.OffsetRect(-point.x, -point.y); 544 return r; 545 } 546 547 CRect operator-(SIZE size) const noexcept 548 { 549 CRect r(this); 550 r.OffsetRect(-size.cx, -size.cy); 551 return r; 552 } 553 554 CRect operator-(LPCRECT lpRect) const noexcept 555 { 556 CRect r(this); 557 r.DeflateRect(lpRect); 558 return r; 559 } 560 561 void operator&=(const RECT& rect) noexcept 562 { 563 IntersectRect(this, &rect); 564 } 565 566 CRect operator&(const RECT& rect2) const noexcept 567 { 568 CRect r; 569 r.IntersectRect(this, &rect2); 570 return r; 571 } 572 573 void operator|=(const RECT& rect) noexcept 574 { 575 UnionRect(this, &rect); 576 } 577 578 CRect operator|(const RECT& rect2) const noexcept 579 { 580 CRect r; 581 r.UnionRect(this, &rect2); 582 return r; 583 } 584 LPRECT()585 operator LPRECT() noexcept 586 { 587 return this; 588 } 589 LPCRECT()590 operator LPCRECT() const noexcept 591 { 592 return this; 593 } 594 }; 595 596 inline CRect CPoint::operator+(const RECT* lpRect) const noexcept 597 { 598 CRect r(lpRect); 599 r += *this; 600 return r; 601 } 602 603 inline CRect CPoint::operator-(const RECT* lpRect) const noexcept 604 { 605 CRect r(lpRect); 606 r -= *this; 607 return r; 608 } 609 610 inline CRect CSize::operator+(const RECT* lpRect) const noexcept 611 { 612 CRect r(lpRect); 613 r += *this; 614 return r; 615 } 616 617 inline CRect CSize::operator-(const RECT* lpRect) const noexcept 618 { 619 CRect r(lpRect); 620 r -= *this; 621 return r; 622 } 623 624