Repository navigation
Expand file tree
/
Copy pathMultiUITracker.cpp
More file actions
1327 lines (1128 loc) · 31.3 KB
/
Copy pathMultiUITracker.cpp
File metadata and controls
1327 lines (1128 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StdAfx.h"
#include "MultiUITracker.h"
/////////////////////////////////////////////////////////////////////////////
// CUITracker
// CUITracker global state
// this array describes all 8 handles (clock-wise)
const HandleInfo g_HandleInfo[] =
{
// corner handles (top-left, top-right, bottom-right, bottom-left
{ offsetof(RECT, left), offsetof(RECT, top), 0, 0, 0, 0, 1, 3 },
{ offsetof(RECT, right), offsetof(RECT, top), 0, 0, -1, 0, 0, 2 },
{ offsetof(RECT, right), offsetof(RECT, bottom), 0, 0, -1, -1, 3, 1 },
{ offsetof(RECT, left), offsetof(RECT, bottom), 0, 0, 0, -1, 2, 0 },
// side handles (top, right, bottom, left)
{ offsetof(RECT, left), offsetof(RECT, top), 1, 0, 0, 0, 4, 6 },
{ offsetof(RECT, right), offsetof(RECT, top), 0, 1, -1, 0, 7, 5 },
{ offsetof(RECT, left), offsetof(RECT, bottom), 1, 0, 0, -1, 6, 4 },
{ offsetof(RECT, left), offsetof(RECT, top), 0, 1, 0, 0, 5, 7 }
};
// this array is indexed by the offset of the RECT member / sizeof(int)
const RectInfo g_RectInfo[] =
{
{ offsetof(RECT, right), +1 },
{ offsetof(RECT, bottom), +1 },
{ offsetof(RECT, left), -1 },
{ offsetof(RECT, top), -1 },
};
/////////////////////////////////////////////////////////////////////////////
// CUITracker intitialization
HCURSOR CUITracker::m_hCursors[10];
HBRUSH CUITracker::m_hHatchBrush=NULL;
CUITracker::CUITracker()
{
Init();
}
CUITracker::CUITracker(LPCRECT lpSrcRect, UINT nStyle)
{
ASSERT(AfxIsValidAddress(lpSrcRect, sizeof(RECT), FALSE));
Init();
m_rect.CopyRect(lpSrcRect);
m_nStyle = nStyle;
}
void CUITracker::Init()
{
m_clrDottedLine=RGB(0,0,0);
m_hDottedLinePen=CreatePen(PS_DOT,0,m_clrDottedLine);
m_clrHandleBorder=RGB(0,0,0);
m_hHandlePen=CreatePen(PS_SOLID,1,m_clrHandleBorder);
m_clrHandleBackground=RGB(255,255,255);
m_hHandleBrush=CreateSolidBrush(m_clrHandleBackground);
m_hMoveHandleBitmap=(HBITMAP)::LoadImage(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP_MOVEHANDLE),
IMAGE_BITMAP,0,0,0);
BITMAP bm;
GetObject(m_hMoveHandleBitmap,sizeof(BITMAP),(LPBYTE)&bm);
m_nMask=0xFF;
m_nControlType=typeControl;
m_nMoveHandleSize=bm.bmWidth;
Construct();
}
void CUITracker::Construct()
{
// do one-time initialization if necessary
static BOOL bInitialized;
if (!bInitialized)
{
// sanity checks for assumptions we make in the code
ASSERT(sizeof(((RECT*)NULL)->left) == sizeof(int));
ASSERT(offsetof(RECT, top) > offsetof(RECT, left));
ASSERT(offsetof(RECT, right) > offsetof(RECT, top));
ASSERT(offsetof(RECT, bottom) > offsetof(RECT, right));
if (m_hHatchBrush == NULL)
{
// create the hatch pattern + bitmap
WORD hatchPattern[8];
WORD wPattern = 0x1111;
for (int i = 0; i < 4; i++)
{
hatchPattern[i] = wPattern;
hatchPattern[i+4] = wPattern;
wPattern <<= 1;
}
HBITMAP hatchBitmap = CreateBitmap(8, 8, 1, 1, hatchPattern);
if (hatchBitmap == NULL)
{
AfxThrowResourceException();
}
// create black hatched brush
m_hHatchBrush = CreatePatternBrush(hatchBitmap);
DeleteObject(hatchBitmap);
if (m_hHatchBrush == NULL)
{
AfxThrowResourceException();
}
}
// Note: all track cursors must live in same module
HINSTANCE hInst = AfxFindResourceHandle(
ATL_MAKEINTRESOURCE(AFX_IDC_TRACK4WAY), ATL_RT_GROUP_CURSOR);
// initialize the cursor array
m_hCursors[0] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_TRACKNWSE));
m_hCursors[1] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_TRACKNESW));
m_hCursors[2] = m_hCursors[0];
m_hCursors[3] = m_hCursors[1];
m_hCursors[4] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_TRACKNS));
m_hCursors[5] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_TRACKWE));
m_hCursors[6] = m_hCursors[4];
m_hCursors[7] = m_hCursors[5];
m_hCursors[8] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_TRACK4WAY));
m_hCursors[9] = ::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_MOVE4WAY));
// get default handle size from Windows profile setting
static const TCHAR szWindows[] = _T("windows");
static const TCHAR szInplaceBorderWidth[] =
_T("oleinplaceborderwidth");
bInitialized = TRUE;
}
m_nStyle = 0;
m_nHandleSize = 4;
m_sizeMin.cy = m_sizeMin.cx = m_nHandleSize*2;
m_rectLast.SetRectEmpty();
m_sizeLast.cx = m_sizeLast.cy = 0;
m_bErase = FALSE;
m_bFinalErase = FALSE;
}
CUITracker::~CUITracker()
{
}
/////////////////////////////////////////////////////////////////////////////
// CUITracker operations
void CUITracker::Draw(CDC* pDC) const
{
// set initial DC state
VERIFY(pDC->SaveDC() != 0);
// get normalized rectangle
CRect rect = m_rect;
rect.NormalizeRect();
CPen* pOldPen = NULL;
CBrush* pOldBrush = NULL;
CGdiObject* pTemp;
int nOldROP;
pDC->SetBkMode(TRANSPARENT);
// draw lines
if ((m_nStyle & (dottedLine|solidLine)) != 0)
{
if (m_nStyle & dottedLine)
pOldPen = pDC->SelectObject(CPen::FromHandle(m_hDottedLinePen));
else
pOldPen = (CPen*)pDC->SelectStockObject(BLACK_PEN);
pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
nOldROP = pDC->SetROP2(R2_COPYPEN);
int offset=GetHandleSize(rect)/2;
rect.InflateRect(offset, offset); // borders are one pixel outside
pDC->Rectangle(rect.left, rect.top, rect.right, rect.bottom);
pDC->SetROP2(nOldROP);
}
// if hatchBrush is going to be used, need to unrealize it
if ((m_nStyle & (hatchInside|hatchedBorder)) != 0)
UnrealizeObject(m_hHatchBrush);
// hatch inside
if ((m_nStyle & hatchInside) != 0)
{
pTemp = pDC->SelectStockObject(NULL_PEN);
if (pOldPen == NULL)
pOldPen = (CPen*)pTemp;
pTemp = pDC->SelectObject(CBrush::FromHandle(m_hHatchBrush));
if (pOldBrush == NULL)
pOldBrush = (CBrush*)pTemp;
pDC->SetBkMode(TRANSPARENT);
nOldROP = pDC->SetROP2(R2_MASKNOTPEN);
pDC->Rectangle(rect.left+1, rect.top+1, rect.right, rect.bottom);
pDC->SetROP2(nOldROP);
}
// draw hatched border
if ((m_nStyle & hatchedBorder) != 0)
{
pTemp = pDC->SelectObject(CBrush::FromHandle(m_hHatchBrush));
if (pOldBrush == NULL)
pOldBrush = (CBrush*)pTemp;
pDC->SetBkMode(OPAQUE);
CRect rectTrue;
GetTrueRect(&rectTrue);
pDC->PatBlt(rectTrue.left, rectTrue.top, rectTrue.Width(),
rect.top-rectTrue.top, 0x000F0001 /* Pn */);
pDC->PatBlt(rectTrue.left, rect.bottom,
rectTrue.Width(), rectTrue.bottom-rect.bottom, 0x000F0001 /* Pn */);
pDC->PatBlt(rectTrue.left, rect.top, rect.left-rectTrue.left,
rect.Height(), 0x000F0001 /* Pn */);
pDC->PatBlt(rect.right, rect.top, rectTrue.right-rect.right,
rect.Height(), 0x000F0001 /* Pn */);
}
// draw resize handles
pDC->SelectObject(m_hHandlePen);
pDC->SelectObject(m_hHandleBrush);
if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
{
UINT mask = GetHandleMask();
for (int i = 0; i < 8; ++i)
{
if (mask & (1<<i))
{
GetHandleRect((TrackerHit)i, &rect);
pDC->Rectangle(&rect);
}
}
}
//draw move handle
if(m_nControlType==typeContainer)
{
GetMoveHandleRect(&rect);
CDC hCloneDC;
hCloneDC.CreateCompatibleDC(pDC);
HBITMAP hOldBitmap=(HBITMAP)hCloneDC.SelectObject(m_hMoveHandleBitmap);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &hCloneDC, 0, 0, SRCCOPY);
hCloneDC.SelectObject(hOldBitmap);
::DeleteDC(hCloneDC);
}
// cleanup pDC state
if (pOldPen != NULL)
pDC->SelectObject(pOldPen);
if (pOldBrush != NULL)
pDC->SelectObject(pOldBrush);
VERIFY(pDC->RestoreDC(-1));
}
BOOL CUITracker::SetCursor(CPoint point, UINT nHitTest) const
{
// trackers should only be in client area
if (nHitTest != HTCLIENT)
return FALSE;
// do hittest and normalize hit
int nHandle = HitTestHandles(point);
if (nHandle < 0)
return FALSE;
// need to normalize the hittest such that we get proper cursors
nHandle = NormalizeHit(nHandle);
// handle special case of hitting area between handles
// (logically the same -- handled as a move -- but different cursor)
if (nHandle == hitMiddle && !m_rect.PtInRect(point))
{
// only for trackers with hatchedBorder (ie. in-place resizing)
if (m_nStyle & hatchedBorder)
nHandle = (TrackerHit)10;
}
ENSURE(nHandle < _countof(m_hCursors));
::SetCursor(m_hCursors[nHandle]);
return TRUE;
}
int CUITracker::HitTest(CPoint point) const
{
TrackerHit hitResult = hitNothing;
CRect rectTrue;
GetTrueRect(&rectTrue);
ASSERT(rectTrue.left <= rectTrue.right);
ASSERT(rectTrue.top <= rectTrue.bottom);
if (rectTrue.PtInRect(point))
{
if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
hitResult = (TrackerHit)HitTestHandles(point);
else
hitResult = hitMiddle;
}
return hitResult;
}
int CUITracker::NormalizeHit(int nHandle) const
{
ENSURE(nHandle <= 8 && nHandle >= -1);
if (nHandle == hitMiddle || nHandle == hitNothing)
return nHandle;
ENSURE(0 <= nHandle && nHandle < _countof(g_HandleInfo));
const HandleInfo* pHandleInfo = &g_HandleInfo[nHandle];
if (m_rect.Width() < 0)
{
nHandle = (TrackerHit)pHandleInfo->nInvertX;
ENSURE(0 <= nHandle && nHandle < _countof(g_HandleInfo));
pHandleInfo = &g_HandleInfo[nHandle];
}
if (m_rect.Height() < 0)
nHandle = (TrackerHit)pHandleInfo->nInvertY;
return nHandle;
}
BOOL CUITracker::Track(CWnd* pWnd, CPoint point, BOOL bAllowInvert,
CDC* pDCClipTo)
{
// perform hit testing on the handles
int nHandle = HitTestHandles(point);
if (nHandle < 0)
{
// didn't hit a handle, so just return FALSE
return FALSE;
}
// otherwise, call helper function to do the tracking
m_bAllowInvert = bAllowInvert;
return TrackHandle(nHandle, pWnd, pDCClipTo);
}
BOOL CUITracker::TrackRubberBand(CWnd* pWnd, CPoint point, BOOL bAllowInvert)
{
// simply call helper function to track from bottom right handle
m_bAllowInvert = bAllowInvert;
m_rect.SetRect(point.x, point.y, point.x, point.y);
return TrackHandle(hitBottomRight, pWnd, NULL);
}
void CUITracker::DrawTrackerRect(LPCRECT lpRect, CDC* pDC)
{
// first, normalize the rectangle for drawing
CRect rect = *lpRect;
rect.NormalizeRect();
CSize size(0, 0);
if (!m_bFinalErase)
{
// otherwise, size depends on the style
if (m_nStyle & hatchedBorder)
{
size.cx = size.cy = max(1, GetHandleSize(rect)-1);
rect.InflateRect(size);
}
else
{
size.cx = AFX_CX_BORDER;
size.cy = AFX_CY_BORDER;
}
if(m_nStyle & (dottedLine|solidLine))
{
int offset=GetHandleSize(rect)/2;
rect.InflateRect(offset,offset);
}
}
pDC->LPtoDP(&rect);
// and draw it
if (m_bFinalErase || !m_bErase)
pDC->DrawDragRect(rect, size, m_rectLast, m_sizeLast);
// remember last rectangles
m_rectLast = rect;
m_sizeLast = size;
}
void CUITracker::AdjustRect(int nHandle, LPRECT)
{
if (nHandle == hitMiddle)
return;
// convert the handle into locations within m_rect
int *px, *py;
GetModifyPointers(nHandle, &px, &py, NULL, NULL);
// enforce minimum width
int nNewWidth = m_rect.Width();
int nAbsWidth = m_bAllowInvert ? abs(nNewWidth) : nNewWidth;
if (px != NULL && nAbsWidth < m_sizeMin.cx)
{
nNewWidth = nAbsWidth != 0 ? nNewWidth / nAbsWidth : 1;
ptrdiff_t iRectInfo = (int*)px - (int*)&m_rect;
ENSURE(0 <= iRectInfo && iRectInfo < _countof(g_RectInfo));
const RectInfo* pRectInfo = &g_RectInfo[iRectInfo];
*px = *(int*)((BYTE*)&m_rect + pRectInfo->nOffsetAcross) +
nNewWidth * m_sizeMin.cx * -pRectInfo->nSignAcross;
}
// enforce minimum height
int nNewHeight = m_rect.Height();
int nAbsHeight = m_bAllowInvert ? abs(nNewHeight) : nNewHeight;
if (py != NULL && nAbsHeight < m_sizeMin.cy)
{
nNewHeight = nAbsHeight != 0 ? nNewHeight / nAbsHeight : 1;
ptrdiff_t iRectInfo = (int*)py - (int*)&m_rect;
ENSURE(0 <= iRectInfo && iRectInfo < _countof(g_RectInfo));
const RectInfo* pRectInfo = &g_RectInfo[iRectInfo];
*py = *(int*)((BYTE*)&m_rect + pRectInfo->nOffsetAcross) +
nNewHeight * m_sizeMin.cy * -pRectInfo->nSignAcross;
}
}
void CUITracker::GetTrueRect(LPRECT lpTrueRect) const
{
ASSERT(AfxIsValidAddress(lpTrueRect, sizeof(RECT)));
CRect rect = m_rect;
rect.NormalizeRect();
int nInflateBy = 0;
if ((m_nStyle & (resizeOutside|hatchedBorder)) != 0)
nInflateBy += GetHandleSize() - 1;
if ((m_nStyle & (solidLine|dottedLine)) != 0)
++nInflateBy;
rect.InflateRect(nInflateBy, nInflateBy);
*lpTrueRect = rect;
}
void CUITracker::OnChangedRect(const CRect& /*rectOld*/)
{
// no default implementation, useful for derived classes
}
/////////////////////////////////////////////////////////////////////////////
// CUITracker implementation helpers
void CUITracker::GetHandleRect(int nHandle, CRect* pHandleRect) const
{
ASSERT(nHandle < 8);
// get normalized rectangle of the tracker
CRect rectT = m_rect;
rectT.NormalizeRect();
if ((m_nStyle & (solidLine|dottedLine)) != 0)
rectT.InflateRect(+1, +1);
// since the rectangle itself was normalized, we also have to invert the
// resize handles.
nHandle = NormalizeHit(nHandle);
// handle case of resize handles outside the tracker
int size = GetHandleSize();
if (m_nStyle & resizeOutside)
rectT.InflateRect(size-1, size-1);
// calculate position of the resize handle
int nWidth = rectT.Width();
int nHeight = rectT.Height();
CRect rect;
const HandleInfo* pHandleInfo = &g_HandleInfo[nHandle];
rect.left = *(int*)((BYTE*)&rectT + pHandleInfo->nOffsetX);
rect.top = *(int*)((BYTE*)&rectT + pHandleInfo->nOffsetY);
rect.left += size * pHandleInfo->nHandleX;
rect.top += size * pHandleInfo->nHandleY;
rect.left += pHandleInfo->nCenterX * (nWidth - size) / 2;
rect.top += pHandleInfo->nCenterY * (nHeight - size) / 2;
rect.right = rect.left + size;
rect.bottom = rect.top + size;
*pHandleRect = rect;
}
void CUITracker::GetMoveHandleRect(CRect* pHandleRect) const
{
CRect rcTopLeft;
CRect rcTop;
GetHandleRect(hitTopLeft,&rcTopLeft);
GetHandleRect(hitTop,&rcTop);
CRect rect;
int size=m_nMoveHandleSize/2;
int offset=rcTopLeft.right+10+size;
rect.left=offset>rcTop.left?rcTopLeft.right:offset;
rect.left-=size;
rect.right=rect.left+m_nMoveHandleSize;
rect.top=rcTopLeft.bottom-size;
rect.bottom=rect.top+m_nMoveHandleSize;
*pHandleRect=rect;
}
int CUITracker::GetHandleSize(LPCRECT lpRect) const
{
if (lpRect == NULL)
lpRect = &m_rect;
int size = m_nHandleSize;
if (!(m_nStyle & resizeOutside))
{
// make sure size is small enough for the size of the rect
int sizeMax = min(abs(lpRect->right - lpRect->left),
abs(lpRect->bottom - lpRect->top));
if (size * 2 > sizeMax)
size = sizeMax / 2;
}
return size;
}
void CUITracker::SetControlType(int nType)
{
m_nControlType=nType;
if(m_nControlType==typeForm)
m_nMask=0x64;
else
m_nMask=0xFF;
}
int CUITracker::HitTestHandles(CPoint point) const
{
CRect rect;
UINT mask = GetHandleMask();
// see if hit anywhere inside the tracker
GetTrueRect(&rect);
if (!rect.PtInRect(point))
return hitNothing; // totally missed
//see if we hit movehandle
if(m_nControlType==typeContainer)
{
GetMoveHandleRect(&rect);
if(rect.PtInRect(point))
return hitMiddle;
}
// see if we hit a handle
for (int i = 0; i < 8; ++i)
{
if (mask & (1<<i))
{
GetHandleRect((TrackerHit)i, &rect);
if (rect.PtInRect(point))
return (TrackerHit)i;
}
}
// last of all, check for non-hit outside of object, between resize handles
if ((m_nStyle & hatchedBorder) == 0)
{
CRect rect = m_rect;
rect.NormalizeRect();
if ((m_nStyle & dottedLine|solidLine) != 0)
rect.InflateRect(+1, +1);
if (!rect.PtInRect(point))
return hitNothing; // must have been between resize handles
}
return m_nControlType==typeControl?hitMiddle:hitNothing; // no handle hit, but hit object (or object border)
}
BOOL CUITracker::TrackHandle(int nHandle, CWnd* pWnd, CDC* pDCClipTo)
{
ASSERT(nHandle >= 0);
ASSERT(nHandle <= 8); // handle 8 is inside the rect
// don't handle if capture already set
if (::GetCapture() != NULL)
return FALSE;
AfxLockTempMaps(); // protect maps while looping
ASSERT(!m_bFinalErase);
// save original width & height in pixels
int nWidth = m_rect.Width();
int nHeight = m_rect.Height();
// set capture to the window which received this message
pWnd->SetCapture();
ASSERT(pWnd == CWnd::GetCapture());
pWnd->UpdateWindow();
CRect rectSave = m_rect;
// convert cursor position to client co-ordinates
CPoint point;
GetCursorPos(&point);
pWnd->ScreenToClient(&point);
// find out what x/y coords we are supposed to modify
int *px, *py;
int xDiff, yDiff;
GetModifyPointers(nHandle, &px, &py, &xDiff, &yDiff);
xDiff = point.x - xDiff;
yDiff = point.y - yDiff;
// get DC for drawing
CDC* pDrawDC;
if (pDCClipTo != NULL)
{
pDrawDC = pDCClipTo;
}
else
{
// otherwise, just use normal DC
pDrawDC = pWnd->GetDC();
}
ENSURE_VALID(pDrawDC);
CRect rectOld;
BOOL bMoved = FALSE;
// get messages until capture lost or cancelled/accepted
for (;;)
{
MSG msg;
VERIFY(::GetMessage(&msg, NULL, 0, 0));
if (CWnd::GetCapture() != pWnd)
break;
switch (msg.message)
{
// handle movement/accept messages
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
rectOld = m_rect;
// handle resize cases (and part of move)
if (px != NULL)
*px = GET_X_LPARAM(msg.lParam) - xDiff;
if (py != NULL)
*py = GET_Y_LPARAM(msg.lParam) - yDiff;
// handle move case
if (nHandle == hitMiddle)
{
m_rect.right = m_rect.left + nWidth;
m_rect.bottom = m_rect.top + nHeight;
}
// allow caller to adjust the rectangle if necessary
AdjustRect(nHandle, &m_rect);
// only redraw and callback if the rect actually changed!
m_bFinalErase = (msg.message == WM_LBUTTONUP);
if (!rectOld.EqualRect(&m_rect) || m_bFinalErase)
{
if (bMoved)
{
m_bErase = TRUE;
DrawTrackerRect(&rectOld, pDrawDC);
}
OnChangedRect(rectOld);
if (msg.message != WM_LBUTTONUP)
bMoved = TRUE;
}
if (m_bFinalErase)
goto ExitLoop;
if (!rectOld.EqualRect(&m_rect))
{
m_bErase = FALSE;
DrawTrackerRect(&m_rect, pDrawDC);
}
break;
// handle cancel messages
case WM_KEYDOWN:
if (msg.wParam != VK_ESCAPE)
break;
case WM_RBUTTONDOWN:
if (bMoved)
{
m_bErase = m_bFinalErase = TRUE;
DrawTrackerRect(&m_rect, pDrawDC);
}
m_rect = rectSave;
goto ExitLoop;
// just dispatch rest of the messages
default:
DispatchMessage(&msg);
break;
}
}
ExitLoop:
if(pDCClipTo==NULL)
pWnd->ReleaseDC(pDrawDC);
ReleaseCapture();
AfxUnlockTempMaps(FALSE);
// restore rect in case bMoved is still FALSE
if (!bMoved)
m_rect = rectSave;
m_bFinalErase = FALSE;
m_bErase = FALSE;
// return TRUE only if rect has changed
return !rectSave.EqualRect(&m_rect);
}
void CUITracker::GetModifyPointers(
int nHandle, int** ppx, int** ppy, int* px, int* py)
{
ENSURE(nHandle >= 0);
ENSURE(nHandle <= 8);
if (nHandle == hitMiddle)
nHandle = hitTopLeft; // same as hitting top-left
*ppx = NULL;
*ppy = NULL;
// fill in the part of the rect that this handle modifies
// (Note: handles that map to themselves along a given axis when that
// axis is inverted don't modify the value on that axis)
const HandleInfo* pHandleInfo = &g_HandleInfo[nHandle];
if (pHandleInfo->nInvertX != nHandle)
{
*ppx = (int*)((BYTE*)&m_rect + pHandleInfo->nOffsetX);
if (px != NULL)
*px = **ppx;
}
else
{
// middle handle on X axis
if (px != NULL)
*px = m_rect.left + abs(m_rect.Width()) / 2;
}
if (pHandleInfo->nInvertY != nHandle)
{
*ppy = (int*)((BYTE*)&m_rect + pHandleInfo->nOffsetY);
if (py != NULL)
*py = **ppy;
}
else
{
// middle handle on Y axis
if (py != NULL)
*py = m_rect.top + abs(m_rect.Height()) / 2;
}
}
UINT CUITracker::GetHandleMask() const
{
UINT mask = m_nMask; // always have 4 corner handles
int size = m_nHandleSize*3;
if (abs(m_rect.Width()) - size < 4)
mask &= (~0x50);
if (abs(m_rect.Height()) - size < 4)
mask &= (~0xA0);
return mask;
}
/////////////////////////////////////////////////////////////////////////////
// CMultiUITracker
CTrackerElement::CTrackerElement(CControlUI* pControl,int nType,INotifyUI* pOwner)
:m_pControl(pControl),m_nType(nType),m_pOwner(pOwner)
{
}
CTrackerElement::~CTrackerElement(void)
{
}
const RECT& CTrackerElement::GetPos() const
{
ASSERT(m_pControl);
return m_pControl->GetPos();
}
void CTrackerElement::SetPos(RECT rect,BOOL bMove/*=FALSE*/)
{
ASSERT(m_pControl);
CControlUI* pParent=m_pControl->GetParent();
CRect rcParent(0,0,0,0);
if(pParent)
rcParent=pParent->GetPos();
m_pControl->SetFixedXY(CSize(rect.left-rcParent.left,rect.top-rcParent.top));
if(!bMove)
{
m_pControl->SetFixedHeight(rect.bottom-rect.top);
m_pControl->SetFixedWidth(rect.right-rect.left);
TNotifyUI Msg;
Msg.pSender=m_pControl;
Msg.sType=_T("setpos");
Msg.wParam=FALSE;//Not Move
Msg.lParam=NULL;
m_pOwner->Notify(Msg);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMultiUITracker
CMultiUITracker::CMultiUITracker(void):m_pFocused(NULL)
{
m_szForm.cx=0;
m_szForm.cy=0;
HINSTANCE hInst = AfxFindResourceHandle(
ATL_MAKEINTRESOURCE(AFX_IDC_TRACK4WAY), ATL_RT_GROUP_CURSOR);
m_hNoDropCursor=::LoadCursor(hInst, ATL_MAKEINTRESOURCE(AFX_IDC_NODROPCRSR));
}
CMultiUITracker::~CMultiUITracker(void)
{
RemoveAll();
ClearUIRect();
}
void CMultiUITracker::Draw(CDC* pDC,LPSIZE pOffset/*=NULL*/)
{
HPEN hOldPen=m_hHandlePen;
HPEN hWhitePen=CreatePen(PS_SOLID,1,RGB(255,255,255));
HBRUSH hOldBrush=m_hHandleBrush;
HBRUSH hBlackBrush=CreateSolidBrush(RGB(0,0,0));
for (int i=0;i<m_arrTracker.GetSize();i++)
{
CTrackerElement* pArrTracker = m_arrTracker.GetAt(i);
m_rect=pArrTracker->GetPos();
if(pOffset)
m_rect.OffsetRect(*pOffset);
SetControlType(pArrTracker->m_nType);
if(pArrTracker==m_pFocused)
{
m_hHandlePen=hOldPen;
m_hHandleBrush=hOldBrush;
}
else
{
m_hHandlePen=hWhitePen;
m_hHandleBrush=hBlackBrush;
}
CUITracker::Draw(pDC);
}
m_hHandlePen=hOldPen;
m_hHandleBrush=hOldBrush;
::DeleteObject(hWhitePen);
::DeleteObject(hBlackBrush);
}
int CMultiUITracker::HitTest(CPoint point)
{
int nHit=hitNothing;
for (int i=0;i<m_arrTracker.GetSize();i++)
{
CTrackerElement* pArrTracker = m_arrTracker.GetAt(i);
m_rect=pArrTracker->GetPos();
SetControlType(pArrTracker->m_nType);
nHit=CUITracker::HitTest(point);
if (nHit!=hitNothing)
{
return nHit;
}
}
return hitNothing;
}
int CMultiUITracker::HitTestHandles(CPoint point)
{
int nHit=hitNothing;
for (int i=0;i<m_arrTracker.GetSize();i++)
{
CTrackerElement* pArrTracker = m_arrTracker.GetAt(i);
m_rect=pArrTracker->GetPos();
SetControlType(pArrTracker->m_nType);
nHit=CUITracker::HitTestHandles(point);
if (nHit!=hitNothing)
{
return nHit;
}
}
return hitNothing;
}
BOOL CMultiUITracker::Track( CWnd* pWnd, CPoint point,BOOL bAllowInvert/*=FALSE*/,CDC* pDCClipTo/*=NULL*/)
{
if (IsEmpty())
return FALSE;
BOOL bRet=FALSE;
int nHandle=HitTestHandles(point);
switch(nHandle)
{
case hitNothing:
break;
case hitMiddle:
bRet = MultiTrackHandle(pWnd, pDCClipTo);
break;
default:
bRet = OneTrackHandle(nHandle, pWnd, bAllowInvert, pDCClipTo);
break;
}
return bRet;
}
BOOL CMultiUITracker::SetCursor(CPoint point, UINT nHitTest)
{
CRect rectSave = m_rect;
for (int i=0;i<m_arrTracker.GetSize();i++)
{
CTrackerElement* pArrTracker = m_arrTracker.GetAt(i);
m_rect=pArrTracker->GetPos();
SetControlType(pArrTracker->m_nType);
if (CUITracker::SetCursor(point, nHitTest))
{
m_rect = rectSave;
return TRUE;
}
}
m_rect = rectSave;
return FALSE;
}
BOOL CMultiUITracker::MultiTrackHandle(CWnd* pWnd,CDC* pDCClipTo)
{
if (::GetCapture() != NULL)
return FALSE;
AfxLockTempMaps(); // protect maps while looping
ASSERT(!m_bFinalErase);
// set capture to the window which received this message
pWnd->SetCapture();
ASSERT(pWnd == CWnd::GetCapture());
pWnd->UpdateWindow();
// convert cursor position to client co-ordinates
CPoint oldPoint;
GetCursorPos(&oldPoint);
pWnd->ScreenToClient(&oldPoint);
// We work on the rects copies
CopyUIRect();
// get DC for drawing
CDC* pDrawDC;
if (pDCClipTo != NULL)
{
pDrawDC = pDCClipTo;
}
else
{
// otherwise, just use normal DC
pDrawDC = pWnd->GetDC();
}
ASSERT_VALID(pDrawDC);
CRect rectOld;
CPoint point;
HCURSOR hOldCursor=::GetCursor();
BOOL bBeyond=FALSE;
BOOL bMoved = FALSE;
// get messages until capture lost or cancelled/accepted
for (;;)
{
MSG msg;
VERIFY(::GetMessage(&msg, NULL, 0, 0));
if (CWnd::GetCapture() != pWnd)
break;
//dispatch the message to window
DispatchMessage(&msg);
switch (msg.message)
{
// handle movement/accept messages
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
// only redraw and callback if the rect actually changed!
m_bFinalErase = (msg.message == WM_LBUTTONUP);
point=msg.lParam;
pDrawDC->DPtoLP(&point);