[ATL] Add CComBSTR.Attach
[reactos.git] / sdk / lib / atl / atltypes.h
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
32 CPoint() throw()
33 {
34 x = y = 0;
35 }
36
37 CPoint(int initX, int initY) throw()
38 {
39 x = initX;
40 y = initY;
41 }
42
43 CPoint(POINT initPt) throw()
44 {
45 *((POINT*)this) = initPt;
46 }
47
48 CPoint(SIZE initSize) throw()
49 {
50 *((SIZE*)this) = initSize;
51 }
52
53 CPoint(LPARAM dwPoint) throw()
54 {
55 x = LOWORD(dwPoint);
56 y = HIWORD(dwPoint);
57 }
58
59 void Offset(int xOffset, int yOffset) throw()
60 {
61 x += xOffset;
62 y += yOffset;
63 }
64
65 void Offset(POINT point) throw()
66 {
67 Offset(point.x, point.y);
68 }
69
70 void Offset(SIZE size) throw()
71 {
72 Offset(size.cx, size.cy);
73 }
74
75 BOOL operator==(POINT point) const throw()
76 {
77 return (x == point.x && y == point.y);
78 }
79
80 BOOL operator!=(POINT point) const throw()
81 {
82 return !(*this == point);
83 }
84
85 void operator+=(SIZE size) throw()
86 {
87 Offset(size);
88 }
89
90 void operator+=(POINT point) throw()
91 {
92 Offset(point);
93 }
94
95 void operator-=(SIZE size) throw()
96 {
97 Offset(-size.cx, -size.cy);
98 }
99
100 void operator-=(POINT point) throw()
101 {
102 Offset(-point.x, -point.y);
103 }
104
105 CPoint operator+(SIZE size) const throw()
106 {
107 return CPoint(x + size.cx, y + size.cy);
108 }
109
110 CPoint operator+(POINT point) const throw()
111 {
112 return CPoint(x + point.x, y + point.y);
113 }
114
115 CRect operator+(const RECT* lpRect) const throw();
116
117 CSize operator-(POINT point) const throw();
118
119 CPoint operator-(SIZE size) const throw()
120 {
121 return CPoint(x - size.cx, y - size.cy);
122 }
123
124 CRect operator-(const RECT* lpRect) const throw();
125
126 CPoint operator-() const throw()
127 {
128 return CPoint(-x, -y);
129 }
130 };
131
132 class CSize : public tagSIZE
133 {
134 public:
135 CSize() throw()
136 {
137 cx = cy = 0;
138 }
139
140 CSize(int initCX, int initCY) throw()
141 {
142 cx = initCX;
143 cy = initCY;
144 }
145
146 CSize(SIZE initSize) throw()
147 {
148 *((SIZE*)this) = initSize;
149 }
150
151 CSize(POINT initPt) throw()
152 {
153 *((POINT*)this) = initPt;
154 }
155
156 CSize(DWORD dwSize) throw()
157 {
158 cx = LOWORD(dwSize);
159 cy = HIWORD(dwSize);
160 }
161
162 BOOL operator==(SIZE size) const throw()
163 {
164 return (size.cx == cx && size.cy == cy);
165 }
166
167 BOOL operator!=(SIZE size) const throw()
168 {
169 return !(*this == size);
170 }
171
172 void operator+=(SIZE size) throw()
173 {
174 cx += size.cx;
175 cy += size.cy;
176 }
177
178 void operator-=(SIZE size) throw()
179 {
180 cx -= size.cx;
181 cy -= size.cy;
182 }
183
184 CSize operator+(SIZE size) const throw()
185 {
186 return CSize(cx + size.cx, cy + size.cy);
187 }
188
189 CPoint operator+(POINT point) const throw()
190 {
191 return CPoint(cx + point.x, cy + point.y);
192 }
193
194 CRect operator+(const RECT* lpRect) const throw();
195
196 CSize operator-(SIZE size) const throw()
197 {
198 return CSize(cx - size.cx, cy - size.cy);
199 }
200
201 CPoint operator-(POINT point) const throw()
202 {
203 return CPoint(cx - point.x, cy - point.y);
204 }
205
206 CRect operator-(const RECT* lpRect) const throw();
207
208 CSize operator-() const throw()
209 {
210 return CSize(-cx, -cy);
211 }
212 };
213
214
215 CSize CPoint::operator-(POINT point) const throw()
216 {
217 return CSize(x - point.x, y - point.y);
218 }
219
220
221 class CRect : public tagRECT
222 {
223 public:
224 CRect() throw()
225 {
226 left = top = right = bottom = 0;
227 }
228
229 CRect(int l, int t, int r, int b) throw()
230 {
231 left = l;
232 top = t;
233 right = r;
234 bottom = b;
235 }
236
237 CRect(const RECT& srcRect) throw()
238 {
239 left = srcRect.left;
240 top = srcRect.top;
241 right = srcRect.right;
242 bottom = srcRect.bottom;
243 }
244
245 CRect(LPCRECT lpSrcRect) throw()
246 {
247 left = lpSrcRect->left;
248 top = lpSrcRect->top;
249 right = lpSrcRect->right;
250 bottom = lpSrcRect->bottom;
251 }
252
253 CRect(POINT point, SIZE size) throw()
254 {
255 left = point.x;
256 top = point.y;
257 right = point.x + size.cx;
258 bottom = point.y + size.cy;
259 }
260
261 CRect(POINT topLeft, POINT bottomRight) throw()
262 {
263 left = topLeft.x;
264 top = topLeft.y;
265 right = bottomRight.x;
266 bottom = bottomRight.y;
267 }
268
269 CPoint& BottomRight() throw()
270 {
271 return ((CPoint*)this)[1];
272 }
273
274 const CPoint& BottomRight() const throw()
275 {
276 return ((const CPoint*)this)[1];
277 }
278
279 CPoint CenterPoint() const throw()
280 {
281 return CPoint(left + (Width() >> 1), top + (Height() >> 1));
282 }
283
284 void CopyRect(LPCRECT lpSrcRect) throw()
285 {
286 ::CopyRect(this, lpSrcRect);
287 }
288
289 void DeflateRect(int x, int y) throw()
290 {
291 ::InflateRect(this, -x, -y);
292 }
293
294 void DeflateRect(SIZE size) throw()
295 {
296 ::InflateRect(this, -size.cx, -size.cy);
297 }
298
299 void DeflateRect(LPCRECT lpRect) throw()
300 {
301 DeflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
302 }
303
304 void DeflateRect(int l, int t, int r, int b) throw()
305 {
306 left += l;
307 top += t;
308 right -= r;
309 bottom -= b;
310 }
311
312 BOOL EqualRect(LPCRECT lpRect) const throw()
313 {
314 return ::EqualRect(this, lpRect);
315 }
316
317
318 int Height() const throw()
319 {
320 return bottom - top;
321 }
322
323 void InflateRect(int x, int y) throw()
324 {
325 ::InflateRect(this, x, y);
326 }
327
328 void InflateRect(SIZE size) throw()
329 {
330 ::InflateRect(this, size.cx, size.cy);
331 }
332
333 void InflateRect(LPCRECT lpRect) throw()
334 {
335 InflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
336 }
337
338 void InflateRect(int l, int t, int r, int b) throw()
339 {
340 left -= l;
341 top -= t;
342 right += r;
343 bottom += b;
344 }
345
346 BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2) throw()
347 {
348 return ::IntersectRect(this, lpRect1, lpRect2);
349 }
350
351 BOOL IsRectEmpty() const throw()
352 {
353 return ::IsRectEmpty(this);
354 }
355
356 BOOL IsRectNull() const throw()
357 {
358 return (left == 0 && right == 0 &&
359 top == 0 && bottom == 0);
360 }
361
362 //void MoveToX(int x) throw()
363 //void MoveToXY(int x, int y) throw()
364 //void MoveToXY(POINT point) throw()
365 //void MoveToY(int y) throw()
366 //void NormalizeRect() throw()
367
368 void OffsetRect(int x, int y) throw()
369 {
370 ::OffsetRect(this, x, y);
371 }
372
373 void OffsetRect(POINT point) throw()
374 {
375 ::OffsetRect(this, point.x, point.y);
376 }
377
378 void OffsetRect(SIZE size) throw()
379 {
380 ::OffsetRect(this, size.cx, size.cy);
381 }
382
383 //BOOL PtInRect(POINT point) const throw()
384 //void SetRect(int x1, int y1, int x2, int y2) throw()
385 //void SetRectEmpty() throw()
386 //CSize Size() const throw()
387 //BOOL SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2) throw()
388
389 CPoint& TopLeft() throw()
390 {
391 return ((CPoint*)this)[0];
392 }
393
394 const CPoint& TopLeft() const throw()
395 {
396 return ((const CPoint*)this)[0];
397 }
398
399 BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2) throw()
400 {
401 return ::UnionRect(this, lpRect1, lpRect2);
402 }
403
404 int Width() const throw()
405 {
406 return right - left;
407 }
408
409
410 BOOL operator==(const RECT& rect) const throw()
411 {
412 return (left == rect.left &&
413 top == rect.top &&
414 right == rect.right &&
415 bottom == rect.bottom);
416 }
417
418 BOOL operator!=(const RECT& rect) const throw()
419 {
420 return !(*this == rect);
421 }
422
423 void operator=(const RECT& srcRect) throw()
424 {
425 left = srcRect.left;
426 top = srcRect.top;
427 right = srcRect.right;
428 bottom = srcRect.bottom;
429 }
430
431 void operator+=(POINT point) throw()
432 {
433 OffsetRect(point);
434 }
435
436 void operator+=(SIZE size) throw()
437 {
438 OffsetRect(size);
439 }
440
441 void operator+=(LPCRECT lpRect) throw()
442 {
443 InflateRect(lpRect);
444 }
445
446 void operator-=(POINT point) throw()
447 {
448 OffsetRect(-point.x, -point.y);
449 }
450
451 void operator-=(SIZE size) throw()
452 {
453 OffsetRect(-size.cx, -size.cy);
454 }
455
456 void operator-=(LPCRECT lpRect) throw()
457 {
458 DeflateRect(lpRect);
459 }
460
461
462 CRect operator+(POINT point) const throw()
463 {
464 CRect r(this);
465 r.OffsetRect(point);
466 return r;
467 }
468
469 CRect operator+(LPCRECT lpRect) const throw()
470 {
471 CRect r(this);
472 r.InflateRect(lpRect);
473 return r;
474 }
475
476 CRect operator+(SIZE size) const throw()
477 {
478 CRect r(this);
479 r.OffsetRect(size);
480 return r;
481 }
482
483 CRect operator-(POINT point) const throw()
484 {
485 CRect r(this);
486 r.OffsetRect(-point.x, -point.y);
487 return r;
488 }
489
490 CRect operator-(SIZE size) const throw()
491 {
492 CRect r(this);
493 r.OffsetRect(-size.cx, -size.cy);
494 return r;
495 }
496
497 CRect operator-(LPCRECT lpRect) const throw()
498 {
499 CRect r(this);
500 r.DeflateRect(lpRect);
501 return r;
502 }
503
504 void operator&=(const RECT& rect) throw()
505 {
506 IntersectRect(this, &rect);
507 }
508
509 CRect operator&(const RECT& rect2) const throw()
510 {
511 CRect r;
512 r.IntersectRect(this, &rect2);
513 return r;
514 }
515
516 void operator|=(const RECT& rect) throw()
517 {
518 UnionRect(this, &rect);
519 }
520
521 CRect operator|(const RECT& rect2) const throw()
522 {
523 CRect r;
524 r.UnionRect(this, &rect2);
525 return r;
526 }
527
528 operator LPRECT() throw()
529 {
530 return this;
531 }
532
533 operator LPCRECT() const throw()
534 {
535 return this;
536 }
537 };
538
539 CRect CPoint::operator+(const RECT* lpRect) const throw()
540 {
541 CRect r(lpRect);
542 r += *this;
543 return r;
544 }
545
546 CRect CPoint::operator-(const RECT* lpRect) const throw()
547 {
548 CRect r(lpRect);
549 r -= *this;
550 return r;
551 }
552
553 CRect CSize::operator+(const RECT* lpRect) const throw()
554 {
555 CRect r(lpRect);
556 r += *this;
557 return r;
558 }
559
560 CRect CSize::operator-(const RECT* lpRect) const throw()
561 {
562 CRect r(lpRect);
563 r -= *this;
564 return r;
565 }
566