soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
souimisc.h
1// Windows Template Library - WTL version 8.1
2// Copyright (C) Microsoft Corporation. All rights reserved.
3//
4// This file is a part of the Windows Template Library.
5// The use and distribution terms for this software are covered by the
6// Common Public License 1.0 (http://opensource.org/licenses/cpl1.0.php)
7// which can be found in the file CPL.TXT at the root of this distribution.
8// By using this software in any fashion, you are agreeing to be bound by
9// the terms of this license. You must not remove this notice, or
10// any other, from this software.
11
12#ifndef __SOUIMISC_H__
13#define __SOUIMISC_H__
14
15
16
17
18///////////////////////////////////////////////////////////////////////////////
19// Classes in this file:
20//
21// CSize
22// CPoint
23// CRect
24
25
26namespace SOUI
27{
28
29// forward declarations
30class CSize;
31class CPoint;
32class CRect;
33
34///////////////////////////////////////////////////////////////////////////////
35// CSize - Wrapper for Windows SIZE structure.
36
37class CSize : public SIZE
38{
39public:
40// Constructors
41 CSize()
42 {
43 cx = 0;
44 cy = 0;
45 }
46
47 CSize(int initCX, int initCY)
48 {
49 cx = initCX;
50 cy = initCY;
51 }
52
53 CSize(SIZE initSize)
54 {
55 *(SIZE*)this = initSize;
56 }
57
58 CSize(POINT initPt)
59 {
60 *(POINT*)this = initPt;
61 }
62
63 CSize(DWORD dwSize)
64 {
65 cx = (short)LOWORD(dwSize);
66 cy = (short)HIWORD(dwSize);
67 }
68
69// Operations
70 BOOL operator ==(SIZE size) const
71 {
72 return (cx == size.cx && cy == size.cy);
73 }
74
75 BOOL operator !=(SIZE size) const
76 {
77 return (cx != size.cx || cy != size.cy);
78 }
79
80 void operator +=(SIZE size)
81 {
82 cx += size.cx;
83 cy += size.cy;
84 }
85
86 void operator -=(SIZE size)
87 {
88 cx -= size.cx;
89 cy -= size.cy;
90 }
91
92 void SetSize(int CX, int CY)
93 {
94 cx = CX;
95 cy = CY;
96 }
97
98// Operators returning CSize values
99 CSize operator +(SIZE size) const
100 {
101 return CSize(cx + size.cx, cy + size.cy);
102 }
103
104 CSize operator -(SIZE size) const
105 {
106 return CSize(cx - size.cx, cy - size.cy);
107 }
108
109 CSize operator -() const
110 {
111 return CSize(-cx, -cy);
112 }
113
114// Operators returning CPoint values
115 CPoint operator +(POINT point) const;
116 CPoint operator -(POINT point) const;
117
118// Operators returning CRect values
119 CRect operator +(const RECT* lpRect) const;
120 CRect operator -(const RECT* lpRect) const;
121};
122
123
124///////////////////////////////////////////////////////////////////////////////
125// CPoint - Wrapper for Windows POINT structure.
126
127class CPoint : public POINT
128{
129public:
130// Constructors
131 CPoint()
132 {
133 x = 0;
134 y = 0;
135 }
136
137 CPoint(int initX, int initY)
138 {
139 x = initX;
140 y = initY;
141 }
142
143 CPoint(POINT initPt)
144 {
145 *(POINT*)this = initPt;
146 }
147
148 CPoint(SIZE initSize)
149 {
150 *(SIZE*)this = initSize;
151 }
152
153 CPoint(DWORD dwPoint)
154 {
155 x = (short)LOWORD(dwPoint);
156 y = (short)HIWORD(dwPoint);
157 }
158
159// Operations
160 void Offset(int xOffset, int yOffset)
161 {
162 x += xOffset;
163 y += yOffset;
164 }
165
166 void Offset(POINT point)
167 {
168 x += point.x;
169 y += point.y;
170 }
171
172 void Offset(SIZE size)
173 {
174 x += size.cx;
175 y += size.cy;
176 }
177
178 BOOL operator ==(POINT point) const
179 {
180 return (x == point.x && y == point.y);
181 }
182
183 BOOL operator !=(POINT point) const
184 {
185 return (x != point.x || y != point.y);
186 }
187
188 void operator +=(SIZE size)
189 {
190 x += size.cx;
191 y += size.cy;
192 }
193
194 void operator -=(SIZE size)
195 {
196 x -= size.cx;
197 y -= size.cy;
198 }
199
200 void operator +=(POINT point)
201 {
202 x += point.x;
203 y += point.y;
204 }
205
206 void operator -=(POINT point)
207 {
208 x -= point.x;
209 y -= point.y;
210 }
211
212 void SetPoint(int X, int Y)
213 {
214 x = X;
215 y = Y;
216 }
217
218// Operators returning CPoint values
219 CPoint operator +(SIZE size) const
220 {
221 return CPoint(x + size.cx, y + size.cy);
222 }
223
224 CPoint operator -(SIZE size) const
225 {
226 return CPoint(x - size.cx, y - size.cy);
227 }
228
229 CPoint operator -() const
230 {
231 return CPoint(-x, -y);
232 }
233
234 CPoint operator +(POINT point) const
235 {
236 return CPoint(x + point.x, y + point.y);
237 }
238
239// Operators returning CSize values
240 CSize operator -(POINT point) const
241 {
242 return CSize(x - point.x, y - point.y);
243 }
244
245// Operators returning CRect values
246 CRect operator +(const RECT* lpRect) const;
247 CRect operator -(const RECT* lpRect) const;
248};
249
250
251///////////////////////////////////////////////////////////////////////////////
252// CRect - Wrapper for Windows RECT structure.
253
254class CRect : public RECT
255{
256public:
257// Constructors
258 CRect()
259 {
260 left = 0;
261 top = 0;
262 right = 0;
263 bottom = 0;
264 }
265
266 CRect(int l, int t, int r, int b)
267 {
268 left = l;
269 top = t;
270 right = r;
271 bottom = b;
272 }
273
274 CRect(const RECT& srcRect)
275 {
276 CopyRect(&srcRect);
277 }
278
279 CRect(LPCRECT lpSrcRect)
280 {
281 CopyRect(lpSrcRect);
282 }
283
284 CRect(POINT point, SIZE size)
285 {
286 right = (left = point.x) + size.cx;
287 bottom = (top = point.y) + size.cy;
288 }
289
290 CRect(POINT topLeft, POINT bottomRight)
291 {
292 left = topLeft.x;
293 top = topLeft.y;
294 right = bottomRight.x;
295 bottom = bottomRight.y;
296 }
297
298// Attributes (in addition to RECT members)
299 int Width() const
300 {
301 return right - left;
302 }
303
304 int Height() const
305 {
306 return bottom - top;
307 }
308
309 CSize Size() const
310 {
311 return CSize(right - left, bottom - top);
312 }
313
314 CPoint& TopLeft()
315 {
316 return *((CPoint*)this);
317 }
318
319 CPoint& BottomRight()
320 {
321 return *((CPoint*)this + 1);
322 }
323
324 const CPoint& TopLeft() const
325 {
326 return *((CPoint*)this);
327 }
328
329 const CPoint& BottomRight() const
330 {
331 return *((CPoint*)this + 1);
332 }
333
334 CPoint CenterPoint() const
335 {
336 return CPoint((left + right) / 2, (top + bottom) / 2);
337 }
338
339 // convert between CRect and LPRECT/LPCRECT (no need for &)
340 operator LPRECT()
341 {
342 return this;
343 }
344
345 operator LPCRECT() const
346 {
347 return this;
348 }
349
350
351 BOOL IsRectEmpty() const
352 {
353 return ::IsRectEmpty(this);
354 }
355
356 BOOL IsRectNull() const
357 {
358 return (left == 0 && right == 0 && top == 0 && bottom == 0);
359 }
360
361 BOOL PtInRect(POINT point) const
362 {
363 return point.x>=left && point.x<right
364 && point.y>=top && point.y<bottom;
365 }
366
367// Operations
368 void SetRect(int x1, int y1, int x2, int y2)
369 {
370 left = x1, right=x2;
371 top = y1, bottom = y2;
372 }
373
374 void SetRect(POINT topLeft, POINT bottomRight)
375 {
376 SetRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
377 }
378
379 void SetRectEmpty()
380 {
381 right = left;
382 bottom = top;
383 }
384
385 void CopyRect(LPCRECT lpSrcRect)
386 {
387 left = lpSrcRect->left;
388 top = lpSrcRect->top;
389 right = lpSrcRect->right;
390 bottom = lpSrcRect->bottom;
391 }
392
393 BOOL EqualRect(LPCRECT lpRect) const
394 {
395 return left == lpRect->left && right == lpRect->right
396 && top == lpRect->top && bottom == lpRect->bottom;
397 }
398
399 void InflateRect(int x, int y)
400 {
401 left -= x, right+=x;
402 top -=y ,bottom += y;
403 }
404
405 void InflateRect(SIZE size)
406 {
407 InflateRect(size.cx, size.cy);
408 }
409
410 void InflateRect(LPCRECT lpRect)
411 {
412 left -= lpRect->left;
413 top -= lpRect->top;
414 right += lpRect->right;
415 bottom += lpRect->bottom;
416 }
417
418 void InflateRect(int l, int t, int r, int b)
419 {
420 left -= l;
421 top -= t;
422 right += r;
423 bottom += b;
424 }
425
426 void DeflateRect(int x, int y)
427 {
428 InflateRect(-x, -y);
429 }
430
431 void DeflateRect(SIZE size)
432 {
433 InflateRect(-size.cx, -size.cy);
434 }
435
436 void DeflateRect(LPCRECT lpRect)
437 {
438 left += lpRect->left;
439 top += lpRect->top;
440 right -= lpRect->right;
441 bottom -= lpRect->bottom;
442 }
443
444 void DeflateRect(int l, int t, int r, int b)
445 {
446 left += l;
447 top += t;
448 right -= r;
449 bottom -= b;
450 }
451
452 void OffsetRect(int x, int y)
453 {
454 left +=x;
455 right +=x;
456 top += y;
457 bottom += y;
458 }
459 void OffsetRect(SIZE size)
460 {
461 OffsetRect(size.cx, size.cy);
462 }
463
464 void OffsetRect(POINT point)
465 {
466 OffsetRect(point.x, point.y);
467 }
468
469 void NormalizeRect()
470 {
471 int nTemp;
472 if (left > right)
473 {
474 nTemp = left;
475 left = right;
476 right = nTemp;
477 }
478 if (top > bottom)
479 {
480 nTemp = top;
481 top = bottom;
482 bottom = nTemp;
483 }
484 }
485
486 // absolute position of rectangle
487 void MoveToY(int y)
488 {
489 bottom = Height() + y;
490 top = y;
491 }
492
493 void MoveToX(int x)
494 {
495 right = Width() + x;
496 left = x;
497 }
498
499 void MoveToXY(int x, int y)
500 {
501 MoveToX(x);
502 MoveToY(y);
503 }
504
505 void MoveToXY(POINT pt)
506 {
507 MoveToX(pt.x);
508 MoveToY(pt.y);
509 }
510
511
512 // operations that fill '*this' with result
513 BOOL IntersectRect(LPCRECT src1, LPCRECT src2)
514 {
515 if (!src1 || !src2) return FALSE;
516 CRect rc1(src1),rc2(src2);
517 if (rc1.IsRectEmpty() || rc2.IsRectEmpty() ||
518 (src1->left >= src2->right) || (src2->left >= src1->right) ||
519 (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
520 {
521 SetRectEmpty();
522 return FALSE;
523 }
524 left = smax( src1->left, src2->left );
525 right = smin( src1->right, src2->right );
526 top = smax( src1->top, src2->top );
527 bottom = smin( src1->bottom, src2->bottom );
528 return TRUE;
529 }
530
531 BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2)
532 {
533 CRect src1(lpRect1),src2(lpRect2);
534 if (src1.IsRectEmpty())
535 {
536 if (src2.IsRectEmpty())
537 {
538 SetRectEmpty();
539 return FALSE;
540 }
541 else *this = src2;
542 }
543 else
544 {
545 if (src2.IsRectEmpty()) *this = src1;
546 else
547 {
548 left = smin( src1.left, src2.left );
549 right = smax( src1.right, src2.right );
550 top = smin( src1.top, src2.top );
551 bottom = smax( src1.bottom, src2.bottom );
552 }
553 }
554 return TRUE;
555 }
556
557 BOOL SubtractRect(const RECT *src1, const RECT *src2 )
558 {
559 CRect tmp;
560 if (::IsRectEmpty( src1 ))
561 {
562 SetRectEmpty();
563 return FALSE;
564 }
565 if (tmp.IntersectRect(src1, src2 ))
566 {
567 if (tmp.EqualRect(src1 ))
568 {
569 SetRectEmpty( );
570 return FALSE;
571 }
572 *this = *src1;
573 if ((tmp.top == top) && (tmp.bottom == bottom))
574 {
575 if (tmp.left == left) left = tmp.right;
576 else if (tmp.right == right) right = tmp.left;
577 }
578 else if ((tmp.left == left) && (tmp.right == right))
579 {
580 if (tmp.top == top) top = tmp.bottom;
581 else if (tmp.bottom == bottom) bottom = tmp.top;
582 }
583 }
584 else
585 {
586 *this = *src1;
587 }
588 return TRUE;
589 }
590
591// Additional Operations
592 void operator =(const RECT& srcRect)
593 {
594 CopyRect(&srcRect);
595 }
596
597 BOOL operator ==(const RECT& rect) const
598 {
599 return EqualRect(&rect);
600 }
601
602 BOOL operator !=(const RECT& rect) const
603 {
604 return EqualRect(&rect)==0;
605 }
606
607 void operator +=(POINT point)
608 {
609 OffsetRect(point.x, point.y);
610 }
611
612 void operator +=(SIZE size)
613 {
614 OffsetRect(size.cx, size.cy);
615 }
616
617 void operator +=(LPCRECT lpRect)
618 {
619 InflateRect(lpRect);
620 }
621
622 void operator -=(POINT point)
623 {
624 OffsetRect(-point.x, -point.y);
625 }
626
627 void operator -=(SIZE size)
628 {
629 OffsetRect(-size.cx, -size.cy);
630 }
631
632 void operator -=(LPCRECT lpRect)
633 {
634 DeflateRect(lpRect);
635 }
636
637 void operator &=(const RECT& rect)
638 {
639 IntersectRect(this, &rect);
640 }
641
642 void operator |=(const RECT& rect)
643 {
644 UnionRect(this, &rect);
645 }
646
647// Operators returning CRect values
648 CRect operator +(POINT pt) const
649 {
650 CRect rect(*this);
651 rect.OffsetRect(pt.x, pt.y);
652 return rect;
653 }
654
655 CRect operator -(POINT pt) const
656 {
657 CRect rect(*this);
658 rect.OffsetRect(-pt.x, -pt.y);
659 return rect;
660 }
661
662 CRect operator +(LPCRECT lpRect) const
663 {
664 CRect rect(this);
665 rect.InflateRect(lpRect);
666 return rect;
667 }
668
669 CRect operator +(SIZE size) const
670 {
671 CRect rect(*this);
672 rect.OffsetRect(size.cx, size.cy);
673 return rect;
674 }
675
676 CRect operator -(SIZE size) const
677 {
678 CRect rect(*this);
679 rect.OffsetRect(-size.cx, -size.cy);
680 return rect;
681 }
682
683 CRect operator -(LPCRECT lpRect) const
684 {
685 CRect rect(this);
686 rect.DeflateRect(lpRect);
687 return rect;
688 }
689
690 CRect operator &(const RECT& rect2) const
691 {
692 CRect rect;
693 rect.IntersectRect(this, &rect2);
694 return rect;
695 }
696
697 CRect operator |(const RECT& rect2) const
698 {
699 CRect rect;
700 rect.UnionRect(this, &rect2);
701 return rect;
702 }
703
704 CRect MulDiv(int nMultiplier, int nDivisor) const
705 {
706 return CRect(
707 ::MulDiv(left, nMultiplier, nDivisor),
708 ::MulDiv(top, nMultiplier, nDivisor),
709 ::MulDiv(right, nMultiplier, nDivisor),
710 ::MulDiv(bottom, nMultiplier, nDivisor));
711 }
712};
713
714
715// CSize implementation
716
717inline CPoint CSize::operator +(POINT point) const
718{
719 return CPoint(cx + point.x, cy + point.y);
720}
721
722inline CPoint CSize::operator -(POINT point) const
723{
724 return CPoint(cx - point.x, cy - point.y);
725}
726
727inline CRect CSize::operator +(const RECT* lpRect) const
728{
729 return CRect(lpRect) + *this;
730}
731
732inline CRect CSize::operator -(const RECT* lpRect) const
733{
734 return CRect(lpRect) - *this;
735}
736
737
738// CPoint implementation
739
740inline CRect CPoint::operator +(const RECT* lpRect) const
741{
742 return CRect(lpRect) + *this;
743}
744
745inline CRect CPoint::operator -(const RECT* lpRect) const
746{
747 return CRect(lpRect) - *this;
748}
749
750
751template <class Num>
752inline CSize operator *(SIZE s, Num n)
753{
754 return CSize((int)(s.cx * n), (int)(s.cy * n));
755};
756
757template <class Num>
758inline void operator *=(SIZE & s, Num n)
759{
760 s = s * n;
761};
762
763template <class Num>
764inline CSize operator /(SIZE s, Num n)
765{
766 return CSize((int)(s.cx / n), (int)(s.cy / n));
767};
768
769template <class Num>
770inline void operator /=(SIZE & s, Num n)
771{
772 s = s / n;
773};
774
775
776}; // namespace SOUI
777
778#endif // __SOUIMISC_H__