xref: /trunk/main/embedserv/source/embed/tracker.cxx (revision f78e906f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #if defined(_MSC_VER) && (_MSC_VER > 1310)
24 #pragma warning(disable : 4917 4555)
25 #endif
26 
27 #include "stdafx.h"
28 #include <stddef.h>
29 #include "syswinwrapper.hxx"
30 
31 
32 HCURSOR _afxCursors[10] = { 0, };
33 HBRUSH _afxHalftoneBrush = 0;
34 
35 
36 // the struct below is used to determine the qualities of a particular handle
37 struct AFX_HANDLEINFO
38 {
39     size_t nOffsetX;    // offset within RECT for X coordinate
40     size_t nOffsetY;    // offset within RECT for Y coordinate
41     int nCenterX;       // adjust X by Width()/2 * this number
42     int nCenterY;       // adjust Y by Height()/2 * this number
43     int nHandleX;       // adjust X by handle size * this number
44     int nHandleY;       // adjust Y by handle size * this number
45     int nInvertX;       // handle converts to this when X inverted
46     int nInvertY;       // handle converts to this when Y inverted
47 };
48 
49 // this array describes all 8 handles (clock-wise)
50 const AFX_HANDLEINFO _afxHandleInfo[] =
51 {
52     // corner handles (top-left, top-right, bottom-right, bottom-left
53     { offsetof(RECT, left), offsetof(RECT, top),        0, 0,  0,  0, 1, 3 },
54     { offsetof(RECT, right), offsetof(RECT, top),       0, 0, -1,  0, 0, 2 },
55     { offsetof(RECT, right), offsetof(RECT, bottom),    0, 0, -1, -1, 3, 1 },
56     { offsetof(RECT, left), offsetof(RECT, bottom),     0, 0,  0, -1, 2, 0 },
57 
58     // side handles (top, right, bottom, left)
59     { offsetof(RECT, left), offsetof(RECT, top),        1, 0,  0,  0, 4, 6 },
60     { offsetof(RECT, right), offsetof(RECT, top),       0, 1, -1,  0, 7, 5 },
61     { offsetof(RECT, left), offsetof(RECT, bottom),     1, 0,  0, -1, 6, 4 },
62     { offsetof(RECT, left), offsetof(RECT, top),        0, 1,  0,  0, 5, 7 }
63 };
64 
65 // the struct below gives us information on the layout of a RECT struct and
66 //  the relationship between its members
67 struct AFX_RECTINFO
68 {
69     size_t nOffsetAcross;   // offset of opposite point (ie. left->right)
70     int nSignAcross;        // sign relative to that point (ie. add/subtract)
71 };
72 
73 // this array is indexed by the offset of the RECT member / sizeof(int)
74 const AFX_RECTINFO _afxRectInfo[] =
75 {
76     { offsetof(RECT, right), +1 },
77     { offsetof(RECT, bottom), +1 },
78     { offsetof(RECT, left), -1 },
79     { offsetof(RECT, top), -1 },
80 };
81 
82 
HalftoneBrush()83 HBRUSH HalftoneBrush()
84 {
85     if (_afxHalftoneBrush == NULL)
86     {
87         WORD grayPattern[8];
88         for (int i = 0; i < 8; i++)
89             grayPattern[i] = (WORD)(0x5555 << (i & 1));
90         HBITMAP grayBitmap = CreateBitmap(8, 8, 1, 1, &grayPattern);
91         if (grayBitmap != NULL)
92         {
93             _afxHalftoneBrush = CreatePatternBrush(grayBitmap);
94             DeleteObject(grayBitmap);
95         }
96     }
97     return _afxHalftoneBrush;
98 }
99 
100 
101 
DrawDragRect(HDC hDC,LPRECT lpRect,SIZE size,LPRECT lpRectLast,SIZE sizeLast,HBRUSH hBrush=NULL,HBRUSH hBrushLast=NULL)102 void DrawDragRect(
103     HDC hDC,LPRECT lpRect,SIZE size,
104     LPRECT lpRectLast,SIZE sizeLast,
105     HBRUSH hBrush = NULL,HBRUSH hBrushLast = NULL)
106 {
107     // first, determine the update region and select it
108     HRGN rgnNew;
109     HRGN rgnOutside,rgnInside;
110     rgnOutside = CreateRectRgnIndirect(lpRect);
111     RECT rect = *lpRect;
112     InflateRect(&rect,-size.cx, -size.cy);
113     IntersectRect(&rect,&rect,lpRect);
114     rgnInside = CreateRectRgnIndirect(&rect);
115     rgnNew = CreateRectRgn(0, 0, 0, 0);
116     CombineRgn(rgnNew,rgnOutside,rgnInside,RGN_XOR);
117 
118     HBRUSH hBrushOld = NULL;
119     if (hBrush == NULL)
120         hBrush = HalftoneBrush();
121     if (hBrushLast == NULL)
122         hBrushLast = hBrush;
123 
124     HRGN rgnLast(NULL);
125     HRGN rgnUpdate(NULL);
126     if (lpRectLast != NULL)
127     {
128         // find difference between new region and old region
129         rgnLast = CreateRectRgn(0, 0, 0, 0);
130         SetRectRgn(
131             rgnOutside,
132             lpRectLast->left,
133             lpRectLast->top,
134             lpRectLast->right,
135             lpRectLast->bottom);
136         rect = *lpRectLast;
137         InflateRect(&rect,-sizeLast.cx, -sizeLast.cy);
138         IntersectRect(&rect,&rect, lpRectLast);
139         SetRectRgn(rgnInside,rect.left,rect.top,rect.right,rect.bottom);
140         CombineRgn(rgnLast,rgnOutside,rgnInside, RGN_XOR);
141 
142 // 		// only diff them if brushes are the same
143         if (hBrush == hBrushLast)
144         {
145             rgnUpdate = CreateRectRgn(0, 0, 0, 0);
146             CombineRgn(rgnUpdate,rgnLast,rgnNew, RGN_XOR);
147         }
148     }
149     if (hBrush != hBrushLast && lpRectLast != NULL)
150     {
151         // brushes are different -- erase old region first
152         SelectClipRgn(hDC,rgnLast);
153         GetClipBox(hDC,&rect);
154         hBrushOld = (HBRUSH)SelectObject(hDC,(HGDIOBJ)hBrushLast);
155         PatBlt(hDC,rect.left,rect.top,(rect.right-rect.left),(rect.bottom-rect.top),PATINVERT);
156 
157         SelectObject(hDC,(HGDIOBJ)hBrushOld);
158         hBrushOld = NULL;
159     }
160 
161     // draw into the update/new region
162     SelectClipRgn(hDC,rgnUpdate);
163 
164     GetClipBox(hDC,&rect);
165     hBrushOld = (HBRUSH) SelectObject(hDC,(HGDIOBJ) hBrush);
166     PatBlt(hDC,rect.left, rect.top,(rect.right-rect.left),(rect.bottom-rect.top), PATINVERT);
167 
168     // cleanup DC
169     if (hBrushOld != NULL)
170         SelectObject(hDC,(HGDIOBJ)hBrushOld);
171     SelectClipRgn(hDC,NULL);
172 }
173 
174 
TransformRect(LPRECT rect,HWND pWnd,HWND pWndClipTo)175 void winwrap::TransformRect(LPRECT rect,HWND pWnd,HWND pWndClipTo)
176 {
177     POINT pt;
178     pt.x = rect->left;pt.y = rect->top;
179     ClientToScreen(pWnd,&pt);
180     ScreenToClient(pWndClipTo,&pt);
181     rect->left = pt.x; rect->top = pt.y;
182 
183     pt.x = rect->right;pt.y = rect->bottom;
184     ClientToScreen(pWnd,&pt);
185     ScreenToClient(pWndClipTo,&pt);
186     rect->right = pt.x; rect->bottom = pt.y;
187 }
188 
189 
NormalizeRect(LPRECT rp)190 void NormalizeRect(LPRECT rp)
191 {
192     if(rp->left > rp->right) {
193         UINT tmp = rp->left;
194         rp->left = rp->right;
195         rp->right = tmp;
196     }
197 
198     if(rp->top > rp->bottom) {
199         UINT tmp = rp->top;
200         rp->top = rp->bottom;
201         rp->bottom = tmp;
202     }
203 }
204 
205 
206 using namespace winwrap;
207 
208 
Tracker()209 Tracker::Tracker()
210 {
211 }
212 
213 
Tracker(LPCRECT lpSrcRect,UINT nStyle)214 Tracker::Tracker(LPCRECT lpSrcRect, UINT nStyle)
215 {
216     Construct();
217     CopyRect(&m_rect,lpSrcRect);
218     m_nStyle = nStyle;
219 }
220 
221 HBRUSH _afxHatchBrush = 0;
222 HPEN _afxBlackDottedPen = 0;
223 int _afxHandleSize = 0;
224 
225 
Construct()226 void Tracker::Construct()
227 {
228     static BOOL bInitialized = false;
229     if (!bInitialized)
230     {
231         if (_afxHatchBrush == NULL)
232         {
233             // create the hatch pattern + bitmap
234             WORD hatchPattern[8];
235             WORD wPattern = 0x1111;
236             for (int i = 0; i < 4; i++)
237             {
238                 hatchPattern[i] = wPattern;
239                 hatchPattern[i+4] = wPattern;
240                 wPattern <<= 1;
241             }
242             HBITMAP hatchBitmap = CreateBitmap(8, 8, 1, 1,&hatchPattern);
243 
244             // create black hatched brush
245             _afxHatchBrush = CreatePatternBrush(hatchBitmap);
246             DeleteObject(hatchBitmap);
247         }
248 
249         if (_afxBlackDottedPen == NULL)
250         {
251             // create black dotted pen
252             _afxBlackDottedPen = CreatePen(PS_DOT, 0, RGB(0, 0, 0));
253         }
254 
255         // get default handle size from Windows profile setting
256         static const TCHAR szWindows[] = TEXT("windows");
257         static const TCHAR szInplaceBorderWidth[] =
258             TEXT("oleinplaceborderwidth");
259         _afxHandleSize = GetProfileInt(szWindows, szInplaceBorderWidth, 4);
260         bInitialized = TRUE;
261 
262         _afxCursors[0] = _afxCursors[2] = LoadCursor(0,IDC_SIZENWSE);
263         _afxCursors[4] = _afxCursors[6] = LoadCursor(0,IDC_SIZENS);
264         _afxCursors[1] = _afxCursors[3] = LoadCursor(0,IDC_SIZENESW);
265         _afxCursors[5] = _afxCursors[7] = LoadCursor(0,IDC_SIZEWE);
266         _afxCursors[8] = LoadCursor(0,IDC_SIZEALL);
267     }
268 
269     m_nStyle = 0;
270     m_nHandleSize = _afxHandleSize;
271     m_sizeMin.cy = m_sizeMin.cx = m_nHandleSize*2;
272 
273     SetRectEmpty(&m_rectLast);
274     m_sizeLast.cx = m_sizeLast.cy = 0;
275     m_bErase = FALSE;
276     m_bFinalErase =  FALSE;
277 }
278 
~Tracker()279 Tracker::~Tracker()
280 {
281 }
282 
283 
HitTest(POINT point) const284 int Tracker::HitTest(POINT point) const
285 {
286     TrackerHit hitResult = hitNothing;
287 
288     RECT rectTrue;
289     GetTrueRect(&rectTrue);
290     NormalizeRect(&rectTrue);
291     if (PtInRect(&rectTrue,point))
292     {
293         if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
294             hitResult = (TrackerHit)HitTestHandles(point);
295         else
296             hitResult = hitMiddle;
297     }
298     return hitResult;
299 }
300 
301 
SetCursor(HWND pWnd,UINT nHitTest) const302 BOOL Tracker::SetCursor(HWND pWnd, UINT nHitTest) const
303 {
304     // trackers should only be in client area
305     if (nHitTest != HTCLIENT)
306         return FALSE;
307 
308     // convert cursor position to client co-ordinates
309     POINT point;
310     GetCursorPos(&point);
311     ScreenToClient(pWnd,&point);
312 
313     // do hittest and normalize hit
314     int nHandle = HitTestHandles(point);
315     if (nHandle < 0)
316         return FALSE;
317 
318     // need to normalize the hittest such that we get proper cursors
319     nHandle = NormalizeHit(nHandle);
320 
321     // handle special case of hitting area between handles
322     //  (logically the same -- handled as a move -- but different cursor)
323     if (nHandle == hitMiddle && !PtInRect(&m_rect,point))
324     {
325         // only for trackers with hatchedBorder (ie. in-place resizing)
326         if (m_nStyle & hatchedBorder)
327             nHandle = (TrackerHit)9;
328     }
329 
330     ::SetCursor(_afxCursors[nHandle]);
331     return TRUE;
332 }
333 
334 
335 
Track(HWND hWnd,POINT point,BOOL bAllowInvert,HWND hWndClipTo)336 BOOL Tracker::Track(HWND hWnd,POINT point,BOOL bAllowInvert,
337                     HWND hWndClipTo)
338 {
339     // perform hit testing on the handles
340     int nHandle = HitTestHandles(point);
341     if (nHandle < 0)
342     {
343         // didn't hit a handle, so just return FALSE
344         return FALSE;
345     }
346 
347     // otherwise, call helper function to do the tracking
348     m_bAllowInvert = bAllowInvert;
349     SetCursor(hWnd,nHandle);
350     return TrackHandle(nHandle, hWnd, point, hWndClipTo);
351 }
352 
353 
TrackHandle(int nHandle,HWND hWnd,POINT point,HWND hWndClipTo)354 BOOL Tracker::TrackHandle(int nHandle,HWND hWnd,POINT point,HWND hWndClipTo)
355 {
356     // don't handle if capture already set
357     if (GetCapture() != NULL)
358         return FALSE;
359 
360     // save original width & height in pixels
361     int nWidth = m_rect.right - m_rect.left;
362     int nHeight = m_rect.bottom - m_rect.top;
363 
364     // set capture to the window which received this message
365     SetCapture(hWnd);
366     UpdateWindow(hWnd);
367     if (hWndClipTo != NULL)
368         UpdateWindow(hWndClipTo);
369     RECT rectSave = m_rect;
370 
371 	// find out what x/y coords we are supposed to modify
372     int *px, *py;
373     int xDiff, yDiff;
374     GetModifyPointers(nHandle, &px, &py, &xDiff, &yDiff);
375     xDiff = point.x - xDiff;
376     yDiff = point.y - yDiff;
377 
378     // get DC for drawing
379     HDC hDrawDC;
380     if (hWndClipTo != NULL)
381     {
382         // clip to arbitrary window by using adjusted Window DC
383         hDrawDC = GetDCEx(hWndClipTo,NULL, DCX_CACHE);
384     }
385     else
386     {
387         // otherwise, just use normal DC
388         hDrawDC = GetDC(hWnd);
389     }
390 
391     RECT rectOld;
392     BOOL bMoved = FALSE;
393 
394     // get messages until capture lost or cancelled/accepted
395     for (;;)
396     {
397         MSG msg;
398         GetMessage(&msg, NULL, 0, 0);
399 
400         if (GetCapture() != hWnd)
401             break;
402 
403         switch (msg.message)
404         {
405             // handle movement/accept messages
406         case WM_LBUTTONUP:
407         case WM_MOUSEMOVE:
408             rectOld = m_rect;
409             // handle resize cases (and part of move)
410             if (px != NULL)
411                 *px = (int)(short)LOWORD(msg.lParam) - xDiff;
412             if (py != NULL)
413                 *py = (int)(short)HIWORD(msg.lParam) - yDiff;
414 
415             // handle move case
416             if (nHandle == hitMiddle)
417             {
418                 m_rect.right = m_rect.left + nWidth;
419                 m_rect.bottom = m_rect.top + nHeight;
420             }
421             // allow caller to adjust the rectangle if necessary
422             AdjustRect(nHandle,&m_rect);
423 
424             // only redraw and callback if the rect actually changed!
425             m_bFinalErase = (msg.message == WM_LBUTTONUP);
426             if (!EqualRect(&rectOld,&m_rect) || m_bFinalErase)
427             {
428                 if (bMoved)
429                 {
430                     m_bErase = TRUE;
431                     DrawTrackerRect(&rectOld,hWndClipTo,hDrawDC,hWnd);
432                 }
433                 OnChangedRect(rectOld);
434                 if (msg.message != WM_LBUTTONUP)
435                     bMoved = TRUE;
436             }
437             if (m_bFinalErase)
438                 goto ExitLoop;
439 
440             if (!EqualRect(&rectOld,&m_rect))
441             {
442                 m_bErase = FALSE;
443                 DrawTrackerRect(&m_rect,hWndClipTo,hDrawDC,hWnd);
444             }
445             break;
446 
447             // handle cancel messages
448         case WM_KEYDOWN:
449             if (msg.wParam != VK_ESCAPE)
450                 break;
451         case WM_RBUTTONDOWN:
452             if (bMoved)
453             {
454                 m_bErase = m_bFinalErase = TRUE;
455                 DrawTrackerRect(&m_rect, hWndClipTo, hDrawDC, hWnd);
456             }
457             m_rect = rectSave;
458             goto ExitLoop;
459 
460             // just dispatch rest of the messages
461         default:
462             DispatchMessage(&msg);
463             break;
464         }
465     }
466 
467   ExitLoop:
468     if (hWndClipTo != NULL)
469         ReleaseDC(hWndClipTo,hDrawDC);
470     else
471         ReleaseDC(hWnd,hDrawDC);
472     ReleaseCapture();
473 
474     // restore rect in case bMoved is still FALSE
475     if (!bMoved)
476         m_rect = rectSave;
477     m_bFinalErase = FALSE;
478     m_bErase = FALSE;
479 
480     // return TRUE only if rect has changed
481     return !EqualRect(&rectSave,&m_rect);
482 }
483 
484 
OnChangedRect(const RECT &)485 void Tracker::OnChangedRect(const RECT& /*rectOld*/)
486 {
487 }
488 
489 
AdjustRect(int nHandle,LPRECT)490 void Tracker::AdjustRect(int nHandle, LPRECT)
491 {
492     if(nHandle == hitMiddle)
493         return;
494 
495     // convert the handle into locations within m_rect
496     int *px, *py;
497     GetModifyPointers(nHandle, &px, &py, NULL, NULL);
498 
499     // enforce minimum width
500     int nNewWidth = m_rect.right - m_rect.left;
501     int nAbsWidth = m_bAllowInvert ? abs(nNewWidth) : nNewWidth;
502     if (px != NULL && nAbsWidth < m_sizeMin.cx)
503     {
504         nNewWidth = nAbsWidth != 0 ? nNewWidth / nAbsWidth : 1;
505         const AFX_RECTINFO* pRectInfo =
506             &_afxRectInfo[(int*)px - (int*)&m_rect];
507         *px = *(int*)((BYTE*)&m_rect + pRectInfo->nOffsetAcross) +
508             nNewWidth * m_sizeMin.cx * -pRectInfo->nSignAcross;
509     }
510 
511     // enforce minimum height
512     int nNewHeight = m_rect.bottom - m_rect.top;
513     int nAbsHeight = m_bAllowInvert ? abs(nNewHeight) : nNewHeight;
514     if (py != NULL && nAbsHeight < m_sizeMin.cy)
515     {
516         nNewHeight = nAbsHeight != 0 ? nNewHeight / nAbsHeight : 1;
517         const AFX_RECTINFO* pRectInfo =
518             &_afxRectInfo[(int*)py - (int*)&m_rect];
519         *py = *(int*)((BYTE*)&m_rect + pRectInfo->nOffsetAcross) +
520             nNewHeight * m_sizeMin.cy * -pRectInfo->nSignAcross;
521     }
522 }
523 
524 
DrawTrackerRect(LPRECT lpRect,HWND pWndClipTo,HDC pDC,HWND pWnd)525 void Tracker::DrawTrackerRect(
526     LPRECT lpRect,HWND pWndClipTo,HDC pDC,HWND pWnd)
527 {
528     // first, normalize the rectangle for drawing
529     RECT rect = *lpRect;
530     NormalizeRect(&rect);
531 
532     // convert to client coordinates
533     if (pWndClipTo != NULL)
534         TransformRect(&rect,pWnd,pWndClipTo);
535 
536     SIZE size;
537     size.cx = 0; size.cy = 0;
538     if (!m_bFinalErase)
539     {
540         // otherwise, size depends on the style
541         if (m_nStyle & hatchedBorder)
542         {
543             size.cx = size.cy = max(1,GetHandleSize(&rect)-1);
544             InflateRect(&rect,size.cx,size.cy);
545         }
546         else
547         {
548             size.cx = 1; // CX_BORDER;
549             size.cy = 1; // CY_BORDER;
550         }
551     }
552 
553     // and draw it
554     if (m_bFinalErase || !m_bErase)
555         DrawDragRect(pDC,&rect,size,&m_rectLast,m_sizeLast);
556 
557     // remember last rectangles
558     m_rectLast = rect;
559     m_sizeLast = size;
560 }
561 
562 
Draw(HDC hDC) const563 void Tracker::Draw(HDC hDC) const
564 {
565     // set initial DC state
566     SetMapMode(hDC,MM_TEXT);
567     SetViewportOrgEx(hDC,0, 0,NULL);
568     SetWindowOrgEx(hDC,0, 0,NULL);
569 
570     // get normalized rectangle
571     RECT rect = m_rect;
572     NormalizeRect(&rect);
573 
574     HPEN pOldPen = NULL;
575     HBRUSH pOldBrush = NULL;
576     HGDIOBJ pTemp;
577     int nOldROP;
578 
579     // draw lines
580     if ((m_nStyle & (dottedLine|solidLine)) != 0)
581     {
582         if (m_nStyle & dottedLine)
583             pOldPen = (HPEN)SelectObject(hDC,_afxBlackDottedPen);
584         else
585             pOldPen = (HPEN)SelectObject(hDC,(HGDIOBJ)BLACK_PEN);
586         pOldBrush = (HBRUSH)SelectObject(hDC,(HGDIOBJ)NULL_BRUSH);
587         nOldROP = SetROP2(hDC,R2_COPYPEN);
588         InflateRect(&rect,+1, +1);   // borders are one pixel outside
589         Rectangle(hDC,rect.left, rect.top, rect.right, rect.bottom);
590         SetROP2(hDC,nOldROP);
591     }
592 
593     // if hatchBrush is going to be used, need to unrealize it
594     if ((m_nStyle & (hatchInside|hatchedBorder)) != 0)
595         UnrealizeObject((HGDIOBJ)_afxHatchBrush);
596 
597     // hatch inside
598     if ((m_nStyle & hatchInside) != 0)
599     {
600         pTemp = SelectObject(hDC,(HGDIOBJ)NULL_PEN);
601         if (pOldPen == NULL)
602             pOldPen = (HPEN)pTemp;
603         pTemp = SelectObject(hDC,(HGDIOBJ)_afxHatchBrush);
604         if (pOldBrush == NULL)
605             pOldBrush = (HBRUSH)pTemp;
606         SetBkMode(hDC,TRANSPARENT);
607         nOldROP = SetROP2(hDC,R2_MASKNOTPEN);
608         Rectangle(hDC,rect.left+1, rect.top+1, rect.right, rect.bottom);
609         SetROP2(hDC,nOldROP);
610     }
611 
612     // draw hatched border
613     if ((m_nStyle & hatchedBorder) != 0)
614     {
615         pTemp = SelectObject(hDC,(HGDIOBJ)_afxHatchBrush);
616         if (pOldBrush == NULL)
617             pOldBrush = (HBRUSH)pTemp;
618         SetBkMode(hDC,OPAQUE);
619         RECT rectTrue;
620         GetTrueRect(&rectTrue);
621         PatBlt(hDC,rectTrue.left, rectTrue.top, rectTrue.right-rectTrue.left,
622                rect.top-rectTrue.top, 0x000F0001 /* Pn */);
623         PatBlt(hDC,rectTrue.left, rect.bottom,
624                rectTrue.right-rectTrue.left,
625                rectTrue.bottom-rect.bottom, 0x000F0001 /* Pn */);
626         PatBlt(hDC,rectTrue.left, rect.top, rect.left-rectTrue.left,
627                rect.bottom-rect.top, 0x000F0001 /* Pn */);
628         PatBlt(hDC,rect.right, rect.top, rectTrue.right-rect.right,
629                rect.bottom-rect.top, 0x000F0001 /* Pn */);
630     }
631 
632     // draw resize handles
633     if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
634     {
635         UINT mask = GetHandleMask();
636         HBRUSH hbrush = CreateSolidBrush(RGB(0,0,0));
637         for (int i = 0; i < 8; ++i)
638         {
639             if (mask & (1<<i))
640             {
641                 GetHandleRect((TrackerHit)i, &rect);
642                 // FillSolidRect(hDC,rect, RGB(0, 0, 0));
643                 FillRect(hDC,&rect,hbrush);
644             }
645         }
646         DeleteObject(hbrush);
647     }
648 
649     // cleanup pDC state
650     if (pOldPen != NULL)
651         SelectObject(hDC,pOldPen);
652     if (pOldBrush != NULL)
653         SelectObject(hDC,pOldBrush);
654     RestoreDC(hDC,-1);
655 }
656 
657 
GetHandleRect(int nHandle,RECT * pHandleRect) const658 void Tracker::GetHandleRect(int nHandle,RECT* pHandleRect) const
659 {
660     // get normalized rectangle of the tracker
661     RECT rectT = m_rect;
662     NormalizeRect(&rectT);
663     if ((m_nStyle & (solidLine|dottedLine)) != 0)
664         InflateRect(&rectT,+1, +1);
665 
666     // since the rectangle itself was normalized, we also have to invert the
667     //  resize handles.
668     nHandle = NormalizeHit(nHandle);
669 
670     // handle case of resize handles outside the tracker
671     int size = GetHandleSize();
672     if (m_nStyle & resizeOutside)
673         InflateRect(&rectT,size-1, size-1);
674 
675     // calculate position of the resize handle
676     int nWidth = rectT.right - rectT.left;
677     int nHeight = rectT.bottom - rectT.top;
678     RECT rect;
679     const AFX_HANDLEINFO* pHandleInfo = &_afxHandleInfo[nHandle];
680     rect.left = *(int*)((BYTE*)&rectT + pHandleInfo->nOffsetX);
681     rect.top = *(int*)((BYTE*)&rectT + pHandleInfo->nOffsetY);
682     rect.left += size * pHandleInfo->nHandleX;
683     rect.top += size * pHandleInfo->nHandleY;
684     rect.left += pHandleInfo->nCenterX * (nWidth - size) / 2;
685     rect.top += pHandleInfo->nCenterY * (nHeight - size) / 2;
686     rect.right = rect.left + size;
687     rect.bottom = rect.top + size;
688 
689     *pHandleRect = rect;
690 }
691 
692 
GetHandleSize(LPRECT lpRect) const693 int Tracker::GetHandleSize(LPRECT lpRect) const
694 {
695     if (lpRect == NULL)
696         lpRect = (LPRECT)&m_rect;
697 
698     int size = m_nHandleSize;
699     if (!(m_nStyle & resizeOutside))
700     {
701         // make sure size is small enough for the size of the rect
702         int sizeMax = min(abs(lpRect->right - lpRect->left),
703                           abs(lpRect->bottom - lpRect->top));
704         if (size * 2 > sizeMax)
705             size = sizeMax / 2;
706     }
707     return size;
708 }
709 
710 
GetHandleMask() const711 UINT Tracker::GetHandleMask() const
712 {
713     UINT mask = 0x0F;   // always have 4 corner handles
714     int size = m_nHandleSize*3;
715     if (abs(m_rect.right - m_rect.left) - size > 4)
716         mask |= 0x50;
717     if (abs(m_rect.bottom - m_rect.top) - size > 4)
718         mask |= 0xA0;
719     return mask;
720 }
721 
722 
GetTrueRect(LPRECT lpTrueRect) const723 void Tracker::GetTrueRect(LPRECT lpTrueRect) const
724 {
725     RECT rect = m_rect;
726     NormalizeRect(&rect);
727     int nInflateBy = 0;
728     if ((m_nStyle & (resizeOutside|hatchedBorder)) != 0)
729         nInflateBy += GetHandleSize() - 1;
730     if ((m_nStyle & (solidLine|dottedLine)) != 0)
731         ++nInflateBy;
732     InflateRect(&rect,nInflateBy, nInflateBy);
733     *lpTrueRect = rect;
734 }
735 
736 
NormalizeHit(int nHandle) const737 int Tracker::NormalizeHit(int nHandle) const
738 {
739     if (nHandle == hitMiddle || nHandle == hitNothing)
740         return nHandle;
741     const AFX_HANDLEINFO* pHandleInfo = &_afxHandleInfo[nHandle];
742     if (m_rect.right - m_rect.left < 0)
743     {
744         nHandle = (TrackerHit)pHandleInfo->nInvertX;
745         pHandleInfo = &_afxHandleInfo[nHandle];
746     }
747     if (m_rect.bottom - m_rect.top < 0)
748         nHandle = (TrackerHit)pHandleInfo->nInvertY;
749     return nHandle;
750 }
751 
752 
HitTestHandles(POINT point) const753 int Tracker::HitTestHandles(POINT point) const
754 {
755     RECT rect;
756     UINT mask = GetHandleMask();
757 
758     // see if hit anywhere inside the tracker
759     GetTrueRect(&rect);
760     if (!PtInRect(&rect,point))
761         return hitNothing;  // totally missed
762 
763     // see if we hit a handle
764     for (int i = 0; i < 8; ++i)
765     {
766         if (mask & (1<<i))
767         {
768             GetHandleRect((TrackerHit)i, &rect);
769             if (PtInRect(&rect,point))
770                 return (TrackerHit)i;
771         }
772     }
773 
774     // last of all, check for non-hit outside of object, between resize handles
775     if ((m_nStyle & hatchedBorder) == 0)
776     {
777         RECT rect = m_rect;
778         NormalizeRect(&rect);
779         if ((m_nStyle & dottedLine|solidLine) != 0)
780             InflateRect(&rect,+1, +1);
781         if (!PtInRect(&rect,point))
782             return hitNothing;  // must have been between resize handles
783     }
784     return hitMiddle;   // no handle hit, but hit object (or object border)
785 }
786 
787 
788 
GetModifyPointers(int nHandle,int ** ppx,int ** ppy,int * px,int * py)789 void Tracker::GetModifyPointers(
790     int nHandle, int** ppx, int** ppy, int* px, int* py)
791 {
792     if (nHandle == hitMiddle)
793         nHandle = hitTopLeft;   // same as hitting top-left
794 
795     *ppx = NULL;
796     *ppy = NULL;
797 
798     // fill in the part of the rect that this handle modifies
799     //  (Note: handles that map to themselves along a given axis when that
800     //   axis is inverted don't modify the value on that axis)
801 
802     const AFX_HANDLEINFO* pHandleInfo = &_afxHandleInfo[nHandle];
803     if (pHandleInfo->nInvertX != nHandle)
804     {
805         *ppx = (int*)((BYTE*)&m_rect + pHandleInfo->nOffsetX);
806         if (px != NULL)
807             *px = **ppx;
808     }
809     else
810     {
811         // middle handle on X axis
812         if (px != NULL)
813             *px = m_rect.left + (m_rect.left-m_rect.right) / 2;
814     }
815     if (pHandleInfo->nInvertY != nHandle)
816     {
817         *ppy = (int*)((BYTE*)&m_rect + pHandleInfo->nOffsetY);
818         if (py != NULL)
819             *py = **ppy;
820     }
821     else
822     {
823         // middle handle on Y axis
824         if (py != NULL)
825             *py = m_rect.top + (m_rect.top-m_rect.bottom) / 2;
826     }
827 }
828 
829 // Fix strange warnings about some
830 // ATL::CAxHostWindow::QueryInterface|AddRef|Releae functions.
831 // warning C4505: 'xxx' : unreferenced local function has been removed
832 #if defined(_MSC_VER)
833 #pragma warning(disable: 4505)
834 #endif
835