home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / drawcli / drawvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  31.7 KB  |  1,303 lines

  1. // drawvw.cpp : implementation of the CDrawView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include <afxpriv.h>
  16.  
  17. #include "drawcli.h"
  18.  
  19. #include "drawdoc.h"
  20. #include "drawobj.h"
  21. #include "cntritem.h"
  22. #include "drawvw.h"
  23.  
  24. #include "drawobj.h"
  25. #include "drawtool.h"
  26. #include "mainfrm.h"
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. // private clipboard format (list of Draw objects)
  34. CLIPFORMAT CDrawView::m_cfDraw = (CLIPFORMAT)
  35.     ::RegisterClipboardFormat(_T("MFC Draw Sample"));
  36. CLIPFORMAT CDrawView::m_cfObjectDescriptor = NULL;
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CDrawView
  40.  
  41. IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
  42.  
  43. BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
  44.     //{{AFX_MSG_MAP(CDrawView)
  45.     ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  46.     ON_COMMAND(ID_CANCEL_EDIT, OnCancelEdit)
  47.     ON_WM_LBUTTONDOWN()
  48.     ON_WM_LBUTTONUP()
  49.     ON_WM_MOUSEMOVE()
  50.     ON_WM_LBUTTONDBLCLK()
  51.     ON_COMMAND(ID_DRAW_SELECT, OnDrawSelect)
  52.     ON_COMMAND(ID_DRAW_ROUNDRECT, OnDrawRoundRect)
  53.     ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
  54.     ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  55.     ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
  56.     ON_UPDATE_COMMAND_UI(ID_DRAW_ELLIPSE, OnUpdateDrawEllipse)
  57.     ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
  58.     ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
  59.     ON_UPDATE_COMMAND_UI(ID_DRAW_ROUNDRECT, OnUpdateDrawRoundRect)
  60.     ON_UPDATE_COMMAND_UI(ID_DRAW_SELECT, OnUpdateDrawSelect)
  61.     ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEBACK, OnUpdateSingleSelect)
  62.     ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  63.     ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  64.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateAnySelect)
  65.     ON_COMMAND(ID_DRAW_POLYGON, OnDrawPolygon)
  66.     ON_UPDATE_COMMAND_UI(ID_DRAW_POLYGON, OnUpdateDrawPolygon)
  67.     ON_WM_SIZE()
  68.     ON_COMMAND(ID_VIEW_GRID, OnViewGrid)
  69.     ON_UPDATE_COMMAND_UI(ID_VIEW_GRID, OnUpdateViewGrid)
  70.     ON_WM_ERASEBKGND()
  71.     ON_COMMAND(ID_OBJECT_FILLCOLOR, OnObjectFillColor)
  72.     ON_COMMAND(ID_OBJECT_LINECOLOR, OnObjectLineColor)
  73.     ON_COMMAND(ID_OBJECT_MOVEBACK, OnObjectMoveBack)
  74.     ON_COMMAND(ID_OBJECT_MOVEFORWARD, OnObjectMoveForward)
  75.     ON_COMMAND(ID_OBJECT_MOVETOBACK, OnObjectMoveToBack)
  76.     ON_COMMAND(ID_OBJECT_MOVETOFRONT, OnObjectMoveToFront)
  77.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  78.     ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  79.     ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  80.     ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
  81.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  82.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  83.     ON_WM_SETFOCUS()
  84.     ON_COMMAND(ID_VIEW_SHOWOBJECTS, OnViewShowObjects)
  85.     ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWOBJECTS, OnUpdateViewShowObjects)
  86.     ON_COMMAND(ID_EDIT_PROPERTIES, OnEditProperties)
  87.     ON_UPDATE_COMMAND_UI(ID_EDIT_PROPERTIES, OnUpdateEditProperties)
  88.     ON_WM_DESTROY()
  89.     ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  90.     ON_WM_CREATE()
  91.     ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEFORWARD, OnUpdateSingleSelect)
  92.     ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOBACK, OnUpdateSingleSelect)
  93.     ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOFRONT, OnUpdateSingleSelect)
  94.     ON_WM_CONTEXTMENU()
  95.     //}}AFX_MSG_MAP
  96.     // Standard printing commands
  97.     ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  98.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  99. END_MESSAGE_MAP()
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CDrawView construction/destruction
  103.  
  104. CDrawView::CDrawView()
  105. {
  106.     m_bGrid = TRUE;
  107.     m_gridColor = RGB(0, 0, 128);
  108.     m_bActive = FALSE;
  109. // new
  110.     if( m_cfObjectDescriptor == NULL )
  111.         m_cfObjectDescriptor = (CLIPFORMAT)::RegisterClipboardFormat(_T("Object Descriptor") );
  112.     m_prevDropEffect = DROPEFFECT_NONE;
  113. // end new
  114. }
  115.  
  116. CDrawView::~CDrawView()
  117. {
  118. }
  119.  
  120. BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
  121. {
  122.     ASSERT(cs.style & WS_CHILD);
  123.     if (cs.lpszClass == NULL)
  124.         cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
  125.     return TRUE;
  126. }
  127.  
  128. void CDrawView::OnActivateView(BOOL bActivate, CView* pActiveView,
  129.     CView* pDeactiveView)
  130. {
  131.     CView::OnActivateView(bActivate, pActiveView, pDeactiveView);
  132.  
  133.     // invalidate selections when active status changes
  134.     if (m_bActive != bActivate)
  135.     {
  136.         if (bActivate)  // if becoming active update as if active
  137.             m_bActive = bActivate;
  138.         if (!m_selection.IsEmpty())
  139.             OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  140.         m_bActive = bActivate;
  141.     }
  142. }
  143.  
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CDrawView drawing
  146.  
  147. void CDrawView::InvalObj(CDrawObj* pObj)
  148. {
  149.     CRect rect = pObj->m_position;
  150.     DocToClient(rect);
  151.     if (m_bActive && IsSelected(pObj))
  152.     {
  153.         rect.left -= 4;
  154.         rect.top -= 5;
  155.         rect.right += 5;
  156.         rect.bottom += 4;
  157.     }
  158.     rect.InflateRect(1, 1); // handles CDrawOleObj objects
  159.  
  160.     InvalidateRect(rect, FALSE);
  161. }
  162.  
  163. void CDrawView::OnUpdate(CView* , LPARAM lHint, CObject* pHint)
  164. {
  165.     switch (lHint)
  166.     {
  167.     case HINT_UPDATE_WINDOW:    // redraw entire window
  168.         Invalidate(FALSE);
  169.         break;
  170.  
  171.     case HINT_UPDATE_DRAWOBJ:   // a single object has changed
  172.         InvalObj((CDrawObj*)pHint);
  173.         break;
  174.  
  175.     case HINT_UPDATE_SELECTION: // an entire selection has changed
  176.         {
  177.             CDrawObjList* pList = pHint != NULL ?
  178.                 (CDrawObjList*)pHint : &m_selection;
  179.             POSITION pos = pList->GetHeadPosition();
  180.             while (pos != NULL)
  181.                 InvalObj(pList->GetNext(pos));
  182.         }
  183.         break;
  184.  
  185.     case HINT_DELETE_SELECTION: // an entire selection has been removed
  186.         if (pHint != &m_selection)
  187.         {
  188.             CDrawObjList* pList = (CDrawObjList*)pHint;
  189.             POSITION pos = pList->GetHeadPosition();
  190.             while (pos != NULL)
  191.             {
  192.                 CDrawObj* pObj = pList->GetNext(pos);
  193.                 InvalObj(pObj);
  194.                 Remove(pObj);   // remove it from this view's selection
  195.             }
  196.         }
  197.         break;
  198.  
  199.     case HINT_UPDATE_OLE_ITEMS:
  200.         {
  201.             CDrawDoc* pDoc = GetDocument();
  202.             POSITION pos = pDoc->GetObjects()->GetHeadPosition();
  203.             while (pos != NULL)
  204.             {
  205.                 CDrawObj* pObj = pDoc->GetObjects()->GetNext(pos);
  206.                 if (pObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  207.                     InvalObj(pObj);
  208.             }
  209.         }
  210.         break;
  211.  
  212.     default:
  213.         ASSERT(FALSE);
  214.         break;
  215.     }
  216. }
  217.  
  218. void CDrawView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
  219. {
  220.     CScrollView::OnPrepareDC(pDC, pInfo);
  221.  
  222.     // mapping mode is MM_ANISOTROPIC
  223.     // these extents setup a mode similar to MM_LOENGLISH
  224.     // MM_LOENGLISH is in .01 physical inches
  225.     // these extents provide .01 logical inches
  226.  
  227.     pDC->SetMapMode(MM_ANISOTROPIC);
  228.     pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
  229.         pDC->GetDeviceCaps(LOGPIXELSY));
  230.     pDC->SetWindowExt(100, -100);
  231.  
  232.     // set the origin of the coordinate system to the center of the page
  233.     CPoint ptOrg;
  234.     ptOrg.x = GetDocument()->GetSize().cx / 2;
  235.     ptOrg.y = GetDocument()->GetSize().cy / 2;
  236.  
  237.     // ptOrg is in logical coordinates
  238.     pDC->OffsetWindowOrg(-ptOrg.x,ptOrg.y);
  239. }
  240.  
  241. BOOL CDrawView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
  242. {
  243.     // do the scroll
  244.     if (!CScrollView::OnScrollBy(sizeScroll, bDoScroll))
  245.         return FALSE;
  246.  
  247.     // update the position of any in-place active item
  248.     if (bDoScroll)
  249.     {
  250.         UpdateActiveItem();
  251.         UpdateWindow();
  252.     }
  253.     return TRUE;
  254. }
  255.  
  256. void CDrawView::OnDraw(CDC* pDC)
  257. {
  258.     CDrawDoc* pDoc = GetDocument();
  259.     ASSERT_VALID(pDoc);
  260.  
  261.     CDC dc;
  262.     CDC* pDrawDC = pDC;
  263.     CBitmap bitmap;
  264.     CBitmap* pOldBitmap;
  265.  
  266.     // only paint the rect that needs repainting
  267.     CRect client;
  268.     pDC->GetClipBox(client);
  269.     CRect rect = client;
  270.     DocToClient(rect);
  271.  
  272.     if (!pDC->IsPrinting())
  273.     {
  274.         // draw to offscreen bitmap for fast looking repaints
  275.         if (dc.CreateCompatibleDC(pDC))
  276.         {
  277.             if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
  278.             {
  279.                 OnPrepareDC(&dc, NULL);
  280.                 pDrawDC = &dc;
  281.  
  282.                 // offset origin more because bitmap is just piece of the whole drawing
  283.                 dc.OffsetViewportOrg(-rect.left, -rect.top);
  284.                 pOldBitmap = dc.SelectObject(&bitmap);
  285.                 dc.SetBrushOrg(rect.left % 8, rect.top % 8);
  286.  
  287.                 // might as well clip to the same rectangle
  288.                 dc.IntersectClipRect(client);
  289.             }
  290.         }
  291.     }
  292.  
  293.     // paint background
  294.     CBrush brush;
  295.     if (!brush.CreateSolidBrush(pDoc->GetPaperColor()))
  296.         return;
  297.  
  298.     brush.UnrealizeObject();
  299.     pDrawDC->FillRect(client, &brush);
  300.  
  301.     if (!pDC->IsPrinting() && m_bGrid)
  302.         DrawGrid(pDrawDC);
  303.  
  304.     pDoc->Draw(pDrawDC, this);
  305.  
  306.     if (pDrawDC != pDC)
  307.     {
  308.         pDC->SetViewportOrg(0, 0);
  309.         pDC->SetWindowOrg(0,0);
  310.         pDC->SetMapMode(MM_TEXT);
  311.         dc.SetViewportOrg(0, 0);
  312.         dc.SetWindowOrg(0,0);
  313.         dc.SetMapMode(MM_TEXT);
  314.         pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  315.             &dc, 0, 0, SRCCOPY);
  316.         dc.SelectObject(pOldBitmap);
  317.     }
  318. }
  319.  
  320. void CDrawView::Remove(CDrawObj* pObj)
  321. {
  322.     POSITION pos = m_selection.Find(pObj);
  323.     if (pos != NULL)
  324.         m_selection.RemoveAt(pos);
  325. }
  326.  
  327. void CDrawView::PasteNative(COleDataObject& dataObject)
  328. {
  329.     // get file refering to clipboard data
  330.     CFile* pFile = dataObject.GetFileData(m_cfDraw);
  331.     if (pFile == NULL)
  332.         return;
  333.  
  334.     // connect the file to the archive
  335.     CArchive ar(pFile, CArchive::load);
  336.     TRY
  337.     {
  338.         ar.m_pDocument = GetDocument(); // set back-pointer in archive
  339.  
  340.         // read the selection
  341.         m_selection.Serialize(ar);
  342.     }
  343.     CATCH_ALL(e)
  344.     {
  345.         ar.Close();
  346.         delete pFile;
  347.         THROW_LAST();
  348.     }
  349.     END_CATCH_ALL
  350.  
  351.     ar.Close();
  352.     delete pFile;
  353. }
  354.  
  355. void CDrawView::PasteEmbedded(COleDataObject& dataObject, CPoint point )
  356. {
  357.     BeginWaitCursor();
  358.  
  359.     // paste embedded
  360.     CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  361.     ASSERT_VALID(pObj);
  362.     CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  363.     ASSERT_VALID(pItem);
  364.     pObj->m_pClientItem = pItem;
  365.  
  366.     TRY
  367.     {
  368.         if (!pItem->CreateFromData(&dataObject) &&
  369.             !pItem->CreateStaticFromData(&dataObject))
  370.         {
  371.             AfxThrowMemoryException();      // any exception will do
  372.         }
  373.  
  374.         // add the object to the document
  375.         GetDocument()->Add(pObj);
  376.         m_selection.AddTail(pObj);
  377.         ClientToDoc( point );
  378.         pObj->MoveTo( CRect( point, pObj->m_extent ), this );
  379.  
  380.         // try to get initial presentation data
  381.         pItem->UpdateLink();
  382.         pItem->UpdateExtent();
  383.     }
  384.     CATCH_ALL(e)
  385.     {
  386.         // clean up item
  387.         pItem->Delete();
  388.         pObj->m_pClientItem = NULL;
  389.         GetDocument()->Remove(pObj);
  390.         pObj->Remove();
  391.  
  392.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  393.     }
  394.     END_CATCH_ALL
  395.  
  396.     EndWaitCursor();
  397. }
  398.  
  399. void CDrawView::DrawGrid(CDC* pDC)
  400. {
  401.     CDrawDoc* pDoc = GetDocument();
  402.  
  403.     COLORREF oldBkColor = pDC->SetBkColor(pDoc->GetPaperColor());
  404.  
  405.     CRect rect;
  406.     rect.left = -pDoc->GetSize().cx / 2;
  407.     rect.top = -pDoc->GetSize().cy / 2;
  408.     rect.right = rect.left + pDoc->GetSize().cx;
  409.     rect.bottom = rect.top + pDoc->GetSize().cy;
  410.  
  411.     // Center lines
  412.     CPen penDash;
  413.     penDash.CreatePen(PS_DASH, 1, m_gridColor);
  414.     CPen* pOldPen = pDC->SelectObject(&penDash);
  415.  
  416.     pDC->MoveTo(0, rect.top);
  417.     pDC->LineTo(0, rect.bottom);
  418.     pDC->MoveTo(rect.left, 0);
  419.     pDC->LineTo(rect.right, 0);
  420.  
  421.     // Major unit lines
  422.     CPen penDot;
  423.     penDot.CreatePen(PS_DOT, 1, m_gridColor);
  424.     pDC->SelectObject(&penDot);
  425.  
  426.     for (int x = rect.left / 100 * 100; x < rect.right; x += 100)
  427.     {
  428.         if (x != 0)
  429.         {
  430.             pDC->MoveTo(x, rect.top);
  431.             pDC->LineTo(x, rect.bottom);
  432.         }
  433.     }
  434.  
  435.     for (int y = rect.top / 100 * 100; y < rect.bottom; y += 100)
  436.     {
  437.         if (y != 0)
  438.         {
  439.             pDC->MoveTo(rect.left, y);
  440.             pDC->LineTo(rect.right, y);
  441.         }
  442.     }
  443.  
  444.     // Outlines
  445.     CPen penSolid;
  446.     penSolid.CreatePen(PS_SOLID, 1, m_gridColor);
  447.     pDC->SelectObject(&penSolid);
  448.     pDC->MoveTo(rect.left, rect.top);
  449.     pDC->LineTo(rect.right, rect.top);
  450.     pDC->LineTo(rect.right, rect.bottom);
  451.     pDC->LineTo(rect.left, rect.bottom);
  452.     pDC->LineTo(rect.left, rect.top);
  453.  
  454.     pDC->SelectObject(pOldPen);
  455.     pDC->SetBkColor(oldBkColor);
  456. }
  457.  
  458. void CDrawView::OnInitialUpdate()
  459. {
  460.     CSize size = GetDocument()->GetSize();
  461.     CClientDC dc(NULL);
  462.     size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
  463.     size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
  464.     SetScrollSizes(MM_TEXT, size);
  465. }
  466.  
  467. void CDrawView::SetPageSize(CSize size)
  468. {
  469.     CClientDC dc(NULL);
  470.     size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
  471.     size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
  472.     SetScrollSizes(MM_TEXT, size);
  473.     GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_WINDOW, NULL);
  474. }
  475.  
  476. /////////////////////////////////////////////////////////////////////////////
  477. // CDrawView printing
  478.  
  479. BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  480. {
  481.     // default preparation
  482.     return DoPreparePrinting(pInfo);
  483. }
  484.  
  485. void CDrawView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  486. {
  487.     CScrollView::OnBeginPrinting(pDC,pInfo);
  488.  
  489.     // check page size -- user could have gone into print setup
  490.     // from print dialog and changed paper or orientation
  491.     GetDocument()->ComputePageSize();
  492. }
  493.  
  494. void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  495. {
  496.     // TODO: add cleanup after printing
  497. }
  498.  
  499. /////////////////////////////////////////////////////////////////////////////
  500. // OLE Client support and commands
  501.  
  502. BOOL CDrawView::IsSelected(const CObject* pDocItem) const
  503. {
  504.     CDrawObj* pDrawObj = (CDrawObj*)pDocItem;
  505.     if (pDocItem->IsKindOf(RUNTIME_CLASS(CDrawItem)))
  506.         pDrawObj = ((CDrawItem*)pDocItem)->m_pDrawObj;
  507.     return m_selection.Find(pDrawObj) != NULL;
  508. }
  509.  
  510. void CDrawView::OnInsertObject()
  511. {
  512.     // Invoke the standard Insert Object dialog box to obtain information
  513.     //  for new CDrawItem object.
  514.     COleInsertDialog dlg;
  515.     if (dlg.DoModal() != IDOK)
  516.         return;
  517.  
  518.     BeginWaitCursor();
  519.  
  520.     // First create the C++ object
  521.     CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  522.     ASSERT_VALID(pObj);
  523.     CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  524.     ASSERT_VALID(pItem);
  525.     pObj->m_pClientItem = pItem;
  526.  
  527.     // Now create the OLE object/item
  528.     TRY
  529.     {
  530.         if (!dlg.CreateItem(pObj->m_pClientItem))
  531.             AfxThrowMemoryException();
  532.  
  533.         // add the object to the document
  534.         GetDocument()->Add(pObj);
  535.  
  536.         // try to get initial presentation data
  537.         pItem->UpdateLink();
  538.         pItem->UpdateExtent();
  539.  
  540.         // if insert new object -- initially show the object
  541.         if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  542.             pItem->DoVerb(OLEIVERB_SHOW, this);
  543.     }
  544.     CATCH_ALL(e)
  545.     {
  546.         // clean up item
  547.         pItem->Delete();
  548.         pObj->m_pClientItem = NULL;
  549.         GetDocument()->Remove(pObj);
  550.         pObj->Remove();
  551.  
  552.         AfxMessageBox(IDP_FAILED_TO_CREATE);
  553.     }
  554.     END_CATCH_ALL
  555.  
  556.     EndWaitCursor();
  557. }
  558.  
  559. // The following command handler provides the standard keyboard
  560. //  user interface to cancel an in-place editing session.
  561. void CDrawView::OnCancelEdit()
  562. {
  563.     // deactivate any in-place active item on this view!
  564.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  565.     if (pActiveItem != NULL)
  566.     {
  567.         // if we found one, deactivate it
  568.         pActiveItem->Close();
  569.     }
  570.     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  571.  
  572.     // escape also brings us back into select mode
  573.     ReleaseCapture();
  574.  
  575.     CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  576.     if (pTool != NULL)
  577.         pTool->OnCancel();
  578.  
  579.     CDrawTool::c_drawShape = selection;
  580. }
  581.  
  582. void CDrawView::OnSetFocus(CWnd* pOldWnd)
  583. {
  584.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  585.     if (pActiveItem != NULL &&
  586.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  587.     {
  588.         // need to set focus to this item if it is in the same view
  589.         CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  590.         if (pWnd != NULL)
  591.         {
  592.             pWnd->SetFocus();
  593.             return;
  594.         }
  595.     }
  596.  
  597.     CScrollView::OnSetFocus(pOldWnd);
  598. }
  599.  
  600. CRect CDrawView::GetInitialPosition()
  601. {
  602.     CRect rect(10, 10, 10, 10);
  603.     ClientToDoc(rect);
  604.     return rect;
  605. }
  606.  
  607. void CDrawView::ClientToDoc(CPoint& point)
  608. {
  609.     CClientDC dc(this);
  610.     OnPrepareDC(&dc, NULL);
  611.     dc.DPtoLP(&point);
  612. }
  613.  
  614. void CDrawView::ClientToDoc(CRect& rect)
  615. {
  616.     CClientDC dc(this);
  617.     OnPrepareDC(&dc, NULL);
  618.     dc.DPtoLP(rect);
  619.     ASSERT(rect.left <= rect.right);
  620.     ASSERT(rect.bottom <= rect.top);
  621. }
  622.  
  623. void CDrawView::DocToClient(CPoint& point)
  624. {
  625.     CClientDC dc(this);
  626.     OnPrepareDC(&dc, NULL);
  627.     dc.LPtoDP(&point);
  628. }
  629.  
  630. void CDrawView::DocToClient(CRect& rect)
  631. {
  632.     CClientDC dc(this);
  633.     OnPrepareDC(&dc, NULL);
  634.     dc.LPtoDP(rect);
  635.     rect.NormalizeRect();
  636. }
  637.  
  638. void CDrawView::Select(CDrawObj* pObj, BOOL bAdd)
  639. {
  640.     if (!bAdd)
  641.     {
  642.         OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  643.         m_selection.RemoveAll();
  644.     }
  645.  
  646.     if (pObj == NULL || IsSelected(pObj))
  647.         return;
  648.  
  649.     m_selection.AddTail(pObj);
  650.     InvalObj(pObj);
  651. }
  652.  
  653. // rect is in device coordinates
  654. void CDrawView::SelectWithinRect(CRect rect, BOOL bAdd)
  655. {
  656.     if (!bAdd)
  657.         Select(NULL);
  658.  
  659.     ClientToDoc(rect);
  660.  
  661.     CDrawObjList* pObList = GetDocument()->GetObjects();
  662.     POSITION posObj = pObList->GetHeadPosition();
  663.     while (posObj != NULL)
  664.     {
  665.         CDrawObj* pObj = pObList->GetNext(posObj);
  666.         if (pObj->Intersects(rect))
  667.             Select(pObj, TRUE);
  668.     }
  669. }
  670.  
  671. void CDrawView::Deselect(CDrawObj* pObj)
  672. {
  673.     POSITION pos = m_selection.Find(pObj);
  674.     if (pos != NULL)
  675.     {
  676.         InvalObj(pObj);
  677.         m_selection.RemoveAt(pos);
  678.     }
  679. }
  680.  
  681. void CDrawView::CloneSelection()
  682. {
  683.     POSITION pos = m_selection.GetHeadPosition();
  684.     while (pos != NULL)
  685.     {
  686.         CDrawObj* pObj = m_selection.GetNext(pos);
  687.         pObj->Clone(pObj->m_pDocument);
  688.             // copies object and adds it to the document
  689.     }
  690. }
  691.  
  692. void CDrawView::UpdateActiveItem()
  693. {
  694.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  695.     if (pActiveItem != NULL &&
  696.         pActiveItem->GetItemState() == COleClientItem::activeUIState)
  697.     {
  698.         // this will update the item rectangles by calling
  699.         //  OnGetPosRect & OnGetClipRect.
  700.         pActiveItem->SetItemRects();
  701.     }
  702. }
  703.  
  704. /////////////////////////////////////////////////////////////////////////////
  705. // CDrawView message handlers
  706.  
  707. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  708. {
  709.     if (!m_bActive)
  710.         return;
  711.     CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  712.     if (pTool != NULL)
  713.         pTool->OnLButtonDown(this, nFlags, point);
  714. }
  715.  
  716. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  717. {
  718.     if (!m_bActive)
  719.         return;
  720.     CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  721.     if (pTool != NULL)
  722.         pTool->OnLButtonUp(this, nFlags, point);
  723. }
  724.  
  725. void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
  726. {
  727.     if (!m_bActive)
  728.         return;
  729.     CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  730.     if (pTool != NULL)
  731.         pTool->OnMouseMove(this, nFlags, point);
  732. }
  733.  
  734. void CDrawView::OnLButtonDblClk(UINT nFlags, CPoint point)
  735. {
  736.     if (!m_bActive)
  737.         return;
  738.     CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  739.     if (pTool != NULL)
  740.         pTool->OnLButtonDblClk(this, nFlags, point);
  741. }
  742.  
  743. void CDrawView::OnDestroy()
  744. {
  745.     CScrollView::OnDestroy();
  746.  
  747.     // deactivate the inplace active item on this view
  748.     COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  749.     if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
  750.     {
  751.         pActiveItem->Deactivate();
  752.         ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  753.     }
  754. }
  755.  
  756. void CDrawView::OnDrawSelect()
  757. {
  758.     CDrawTool::c_drawShape = selection;
  759. }
  760.  
  761. void CDrawView::OnDrawRoundRect()
  762. {
  763.     CDrawTool::c_drawShape = roundRect;
  764. }
  765.  
  766. void CDrawView::OnDrawRect()
  767. {
  768.     CDrawTool::c_drawShape = rect;
  769. }
  770.  
  771. void CDrawView::OnDrawLine()
  772. {
  773.     CDrawTool::c_drawShape = line;
  774. }
  775.  
  776. void CDrawView::OnDrawEllipse()
  777. {
  778.     CDrawTool::c_drawShape = ellipse;
  779. }
  780.  
  781. void CDrawView::OnDrawPolygon()
  782. {
  783.     CDrawTool::c_drawShape = poly;
  784. }
  785.  
  786. void CDrawView::OnUpdateDrawEllipse(CCmdUI* pCmdUI)
  787. {
  788.     pCmdUI->SetRadio(CDrawTool::c_drawShape == ellipse);
  789. }
  790.  
  791. void CDrawView::OnUpdateDrawLine(CCmdUI* pCmdUI)
  792. {
  793.     pCmdUI->SetRadio(CDrawTool::c_drawShape == line);
  794. }
  795.  
  796. void CDrawView::OnUpdateDrawRect(CCmdUI* pCmdUI)
  797. {
  798.     pCmdUI->SetRadio(CDrawTool::c_drawShape == rect);
  799. }
  800.  
  801. void CDrawView::OnUpdateDrawRoundRect(CCmdUI* pCmdUI)
  802. {
  803.     pCmdUI->SetRadio(CDrawTool::c_drawShape == roundRect);
  804. }
  805.  
  806. void CDrawView::OnUpdateDrawSelect(CCmdUI* pCmdUI)
  807. {
  808.     pCmdUI->SetRadio(CDrawTool::c_drawShape == selection);
  809. }
  810.  
  811. void CDrawView::OnUpdateSingleSelect(CCmdUI* pCmdUI)
  812. {
  813.     pCmdUI->Enable(m_selection.GetCount() == 1);
  814. }
  815.  
  816. void CDrawView::OnEditSelectAll()
  817. {
  818.     CDrawObjList* pObList = GetDocument()->GetObjects();
  819.     POSITION pos = pObList->GetHeadPosition();
  820.     while (pos != NULL)
  821.         Select(pObList->GetNext(pos), TRUE);
  822. }
  823.  
  824. void CDrawView::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
  825. {
  826.     pCmdUI->Enable(GetDocument()->GetObjects()->GetCount() != 0);
  827. }
  828.  
  829. void CDrawView::OnEditClear()
  830. {
  831.     // update all the views before the selection goes away
  832.     GetDocument()->UpdateAllViews(NULL, HINT_DELETE_SELECTION, &m_selection);
  833.     OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  834.  
  835.     // now remove the selection from the document
  836.     POSITION pos = m_selection.GetHeadPosition();
  837.     while (pos != NULL)
  838.     {
  839.         CDrawObj* pObj = m_selection.GetNext(pos);
  840.         GetDocument()->Remove(pObj);
  841.         pObj->Remove();
  842.     }
  843.     m_selection.RemoveAll();
  844. }
  845.  
  846. void CDrawView::OnUpdateAnySelect(CCmdUI* pCmdUI)
  847. {
  848.     pCmdUI->Enable(!m_selection.IsEmpty());
  849. }
  850.  
  851. void CDrawView::OnUpdateDrawPolygon(CCmdUI* pCmdUI)
  852. {
  853.     pCmdUI->SetRadio(CDrawTool::c_drawShape == poly);
  854. }
  855.  
  856. void CDrawView::OnSize(UINT nType, int cx, int cy)
  857. {
  858.     CScrollView::OnSize(nType, cx, cy);
  859.     UpdateActiveItem();
  860. }
  861.  
  862. void CDrawView::OnViewGrid()
  863. {
  864.     m_bGrid = !m_bGrid;
  865.     Invalidate(FALSE);
  866. }
  867.  
  868. void CDrawView::OnUpdateViewGrid(CCmdUI* pCmdUI)
  869. {
  870.     pCmdUI->SetCheck(m_bGrid);
  871.  
  872. }
  873.  
  874. BOOL CDrawView::OnEraseBkgnd(CDC*)
  875. {
  876.     return TRUE;
  877. }
  878.  
  879. void CDrawView::OnObjectFillColor()
  880. {
  881.     CColorDialog dlg;
  882.     if (dlg.DoModal() != IDOK)
  883.         return;
  884.  
  885.     COLORREF color = dlg.GetColor();
  886.  
  887.     POSITION pos = m_selection.GetHeadPosition();
  888.     while (pos != NULL)
  889.     {
  890.         CDrawObj* pObj = m_selection.GetNext(pos);
  891.         pObj->SetFillColor(color);
  892.     }
  893. }
  894.  
  895. void CDrawView::OnObjectLineColor()
  896. {
  897.     CColorDialog dlg;
  898.     if (dlg.DoModal() != IDOK)
  899.         return;
  900.  
  901.     COLORREF color = dlg.GetColor();
  902.  
  903.     POSITION pos = m_selection.GetHeadPosition();
  904.     while (pos != NULL)
  905.     {
  906.         CDrawObj* pObj = m_selection.GetNext(pos);
  907.         pObj->SetLineColor(color);
  908.     }
  909. }
  910.  
  911. void CDrawView::OnObjectMoveBack()
  912. {
  913.     CDrawDoc* pDoc = GetDocument();
  914.     CDrawObj* pObj = m_selection.GetHead();
  915.     CDrawObjList* pObjects = pDoc->GetObjects();
  916.     POSITION pos = pObjects->Find(pObj);
  917.     ASSERT(pos != NULL);
  918.     if (pos != pObjects->GetHeadPosition())
  919.     {
  920.         POSITION posPrev = pos;
  921.         pObjects->GetPrev(posPrev);
  922.         pObjects->RemoveAt(pos);
  923.         pObjects->InsertBefore(posPrev, pObj);
  924.         InvalObj(pObj);
  925.     }
  926. }
  927.  
  928. void CDrawView::OnObjectMoveForward()
  929. {
  930.     CDrawDoc* pDoc = GetDocument();
  931.     CDrawObj* pObj = m_selection.GetHead();
  932.     CDrawObjList* pObjects = pDoc->GetObjects();
  933.     POSITION pos = pObjects->Find(pObj);
  934.     ASSERT(pos != NULL);
  935.     if (pos != pObjects->GetTailPosition())
  936.     {
  937.         POSITION posNext = pos;
  938.         pObjects->GetNext(posNext);
  939.         pObjects->RemoveAt(pos);
  940.         pObjects->InsertAfter(posNext, pObj);
  941.         InvalObj(pObj);
  942.     }
  943. }
  944.  
  945. void CDrawView::OnObjectMoveToBack()
  946. {
  947.     CDrawDoc* pDoc = GetDocument();
  948.     CDrawObj* pObj = m_selection.GetHead();
  949.     CDrawObjList* pObjects = pDoc->GetObjects();
  950.     POSITION pos = pObjects->Find(pObj);
  951.     ASSERT(pos != NULL);
  952.     pObjects->RemoveAt(pos);
  953.     pObjects->AddHead(pObj);
  954.     InvalObj(pObj);
  955. }
  956.  
  957. void CDrawView::OnObjectMoveToFront()
  958. {
  959.     CDrawDoc* pDoc = GetDocument();
  960.     CDrawObj* pObj = m_selection.GetHead();
  961.     CDrawObjList* pObjects = pDoc->GetObjects();
  962.     POSITION pos = pObjects->Find(pObj);
  963.     ASSERT(pos != NULL);
  964.     pObjects->RemoveAt(pos);
  965.     pObjects->AddTail(pObj);
  966.     InvalObj(pObj);
  967. }
  968.  
  969. void CDrawView::OnEditCopy()
  970. {
  971.     ASSERT_VALID(this);
  972.     ASSERT(m_cfDraw != NULL);
  973.  
  974.     // Create a shared file and associate a CArchive with it
  975.     CSharedFile file;
  976.     CArchive ar(&file, CArchive::store);
  977.  
  978.     // Serialize selected objects to the archive
  979.     m_selection.Serialize(ar);
  980.     ar.Close();
  981.  
  982.     COleDataSource* pDataSource = NULL;
  983.     TRY
  984.     {
  985.         pDataSource = new COleDataSource;
  986.         // put on local format instead of or in addation to
  987.         pDataSource->CacheGlobalData(m_cfDraw, file.Detach());
  988.  
  989.         // if only one item and it is a COleClientItem then also
  990.         // paste in that format
  991.         CDrawObj* pDrawObj = m_selection.GetHead();
  992.         if (m_selection.GetCount() == 1 &&
  993.             pDrawObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  994.         {
  995.             CDrawOleObj* pDrawOle = (CDrawOleObj*)pDrawObj;
  996.             pDrawOle->m_pClientItem->GetClipboardData(pDataSource, FALSE);
  997.         }
  998.         pDataSource->SetClipboard();
  999.     }
  1000.     CATCH_ALL(e)
  1001.     {
  1002.         delete pDataSource;
  1003.         THROW_LAST();
  1004.     }
  1005.     END_CATCH_ALL
  1006. }
  1007.  
  1008. void CDrawView::OnUpdateEditCopy(CCmdUI* pCmdUI)
  1009. {
  1010.     pCmdUI->Enable(!m_selection.IsEmpty());
  1011. }
  1012.  
  1013. void CDrawView::OnEditCut()
  1014. {
  1015.     OnEditCopy();
  1016.     OnEditClear();
  1017. }
  1018.  
  1019. void CDrawView::OnUpdateEditCut(CCmdUI* pCmdUI)
  1020. {
  1021.     pCmdUI->Enable(!m_selection.IsEmpty());
  1022. }
  1023.  
  1024. void CDrawView::OnEditPaste()
  1025. {
  1026.     COleDataObject dataObject;
  1027.     dataObject.AttachClipboard();
  1028.  
  1029.     // invalidate current selection since it will be deselected
  1030.     OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  1031.     m_selection.RemoveAll();
  1032.     if (dataObject.IsDataAvailable(m_cfDraw))
  1033.     {
  1034.         PasteNative(dataObject);
  1035.  
  1036.         // now add all items in m_selection to document
  1037.         POSITION pos = m_selection.GetHeadPosition();
  1038.         while (pos != NULL)
  1039.             GetDocument()->Add(m_selection.GetNext(pos));
  1040.     }
  1041.     else
  1042.         PasteEmbedded(dataObject, GetInitialPosition().TopLeft() );
  1043.  
  1044.     GetDocument()->SetModifiedFlag();
  1045.  
  1046.     // invalidate new pasted stuff
  1047.     GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_SELECTION, &m_selection);
  1048. }
  1049.  
  1050. void CDrawView::OnUpdateEditPaste(CCmdUI* pCmdUI)
  1051. {
  1052.     // determine if private or standard OLE formats are on the clipboard
  1053.     COleDataObject dataObject;
  1054.     BOOL bEnable = dataObject.AttachClipboard() &&
  1055.         (dataObject.IsDataAvailable(m_cfDraw) ||
  1056.          COleClientItem::CanCreateFromData(&dataObject));
  1057.  
  1058.     // enable command based on availability
  1059.     pCmdUI->Enable(bEnable);
  1060. }
  1061.  
  1062. void CDrawView::OnFilePrint()
  1063. {
  1064.     CScrollView::OnFilePrint();
  1065.     GetDocument()->ComputePageSize();
  1066. }
  1067.  
  1068. void CDrawView::OnViewShowObjects()
  1069. {
  1070.     CDrawOleObj::c_bShowItems = !CDrawOleObj::c_bShowItems;
  1071.     GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_OLE_ITEMS, NULL);
  1072. }
  1073.  
  1074. void CDrawView::OnUpdateViewShowObjects(CCmdUI* pCmdUI)
  1075. {
  1076.     pCmdUI->SetCheck(CDrawOleObj::c_bShowItems);
  1077. }
  1078.  
  1079. void CDrawView::OnEditProperties()
  1080. {
  1081.     if (m_selection.GetCount() == 1 && CDrawTool::c_drawShape == selection)
  1082.     {
  1083.         CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  1084.         ASSERT(pTool != NULL);
  1085.         pTool->OnEditProperties(this);
  1086.     }
  1087. }
  1088.  
  1089. void CDrawView::OnUpdateEditProperties(CCmdUI* pCmdUI)
  1090. {
  1091.     pCmdUI->Enable(m_selection.GetCount() == 1 &&
  1092.                    CDrawTool::c_drawShape == selection);
  1093. }
  1094.  
  1095. /////////////////////////////////////////////////////////////////////////////
  1096. // CDrawView diagnostics
  1097.  
  1098. #ifdef _DEBUG
  1099. void CDrawView::AssertValid() const
  1100. {
  1101.     CScrollView::AssertValid();
  1102. }
  1103.  
  1104. void CDrawView::Dump(CDumpContext& dc) const
  1105. {
  1106.     CScrollView::Dump(dc);
  1107. }
  1108. #endif //_DEBUG
  1109.  
  1110. /////////////////////////////////////////////////////////////////////////////
  1111. // new
  1112. // support for drag/drop
  1113.  
  1114. int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  1115. {
  1116.     if (CScrollView::OnCreate(lpCreateStruct) == -1)
  1117.         return -1;
  1118.  
  1119.     // register drop target
  1120.     if( m_dropTarget.Register( this ) )
  1121.         return 0;
  1122.     else
  1123.         return -1;
  1124. }
  1125.  
  1126. BOOL CDrawView::GetObjectInfo(COleDataObject* pDataObject,
  1127.     CSize* pSize, CSize* pOffset)
  1128. {
  1129.     ASSERT(pSize != NULL);
  1130.  
  1131.     // get object descriptor data
  1132.     HGLOBAL hObjDesc = pDataObject->GetGlobalData(m_cfObjectDescriptor);
  1133.     if (hObjDesc == NULL)
  1134.     {
  1135.         if (pOffset != NULL)
  1136.             *pOffset = CSize(0, 0); // fill in defaults instead
  1137.         *pSize = CSize(0, 0);
  1138.         return FALSE;
  1139.     }
  1140.     ASSERT(hObjDesc != NULL);
  1141.  
  1142.     // otherwise, got CF_OBJECTDESCRIPTOR ok.  Lock it down and extract size.
  1143.     LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObjDesc);
  1144.     ASSERT(pObjDesc != NULL);
  1145.     pSize->cx = (int)pObjDesc->sizel.cx;
  1146.     pSize->cy = (int)pObjDesc->sizel.cy;
  1147.     if (pOffset != NULL)
  1148.     {
  1149.         pOffset->cx = (int)pObjDesc->pointl.x;
  1150.         pOffset->cy = (int)pObjDesc->pointl.y;
  1151.     }
  1152.     GlobalUnlock(hObjDesc);
  1153.     GlobalFree(hObjDesc);
  1154.  
  1155.     // successfully retrieved pSize & pOffset info
  1156.     return TRUE;
  1157. }
  1158.  
  1159. DROPEFFECT CDrawView::OnDragEnter(COleDataObject* pDataObject,
  1160.     DWORD grfKeyState, CPoint point)
  1161. {
  1162.     ASSERT(m_prevDropEffect == DROPEFFECT_NONE);
  1163.     m_bDragDataAcceptable = FALSE;
  1164.     if (!COleClientItem::CanCreateFromData(pDataObject))
  1165.         return DROPEFFECT_NONE;
  1166.  
  1167.     m_bDragDataAcceptable = TRUE;
  1168.     GetObjectInfo(pDataObject, &m_dragSize, &m_dragOffset);
  1169.     CClientDC dc(NULL);
  1170.     dc.HIMETRICtoDP(&m_dragSize);
  1171.     dc.HIMETRICtoDP(&m_dragOffset);
  1172.  
  1173.     return OnDragOver(pDataObject, grfKeyState, point);
  1174. }
  1175.  
  1176. DROPEFFECT CDrawView::OnDragOver(COleDataObject*,
  1177.     DWORD grfKeyState, CPoint point)
  1178. {
  1179.     if(m_bDragDataAcceptable == FALSE)
  1180.         return DROPEFFECT_NONE;
  1181.  
  1182.     point -= m_dragOffset;  // adjust target rect by original cursor offset
  1183.  
  1184.     // check for point outside logical area -- i.e. in hatched region
  1185.     // GetTotalSize() returns the size passed to SetScrollSizes
  1186.     CRect rectScroll(CPoint(0, 0), GetTotalSize());
  1187.  
  1188.     CRect rectItem(point,m_dragSize);
  1189.     rectItem.OffsetRect(GetDeviceScrollPosition());
  1190.  
  1191.     DROPEFFECT de = DROPEFFECT_NONE;
  1192.     CRect rectTemp;
  1193.     if (rectTemp.IntersectRect(rectScroll, rectItem))
  1194.     {
  1195.         // check for force link
  1196.         if ((grfKeyState & (MK_CONTROL|MK_SHIFT)) == (MK_CONTROL|MK_SHIFT))
  1197.             de = DROPEFFECT_NONE; // DRAWCLI isn't a linking container
  1198.         // check for force copy
  1199.         else if ((grfKeyState & MK_CONTROL) == MK_CONTROL)
  1200.             de = DROPEFFECT_COPY;
  1201.         // check for force move
  1202.         else if ((grfKeyState & MK_ALT) == MK_ALT)
  1203.             de = DROPEFFECT_MOVE;
  1204.         // default -- recommended action is move
  1205.         else
  1206.             de = DROPEFFECT_MOVE;
  1207.     }
  1208.  
  1209.     if (point == m_dragPoint)
  1210.         return de;
  1211.  
  1212.     // otherwise, cursor has moved -- need to update the drag feedback
  1213.     CClientDC dc(this);
  1214.     if (m_prevDropEffect != DROPEFFECT_NONE)
  1215.     {
  1216.         // erase previous focus rect
  1217.         dc.DrawFocusRect(CRect(m_dragPoint, m_dragSize));
  1218.     }
  1219.     m_prevDropEffect = de;
  1220.     if (m_prevDropEffect != DROPEFFECT_NONE)
  1221.     {
  1222.         m_dragPoint = point;
  1223.         dc.DrawFocusRect(CRect(point, m_dragSize));
  1224.     }
  1225.     return de;
  1226. }
  1227.  
  1228. BOOL CDrawView::OnDrop(COleDataObject* pDataObject,
  1229.     DROPEFFECT /*dropEffect*/, CPoint point)
  1230. {
  1231.     ASSERT_VALID(this);
  1232.  
  1233.     // clean up focus rect
  1234.     OnDragLeave();
  1235.  
  1236.     // offset point as appropriate for dragging
  1237.     GetObjectInfo(pDataObject, &m_dragSize, &m_dragOffset);
  1238.     CClientDC dc(NULL);
  1239.     dc.HIMETRICtoDP(&m_dragSize);
  1240.     dc.HIMETRICtoDP(&m_dragOffset);
  1241.     point -= m_dragOffset;
  1242.  
  1243.     // invalidate current selection since it will be deselected
  1244.     OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  1245.     m_selection.RemoveAll();
  1246.     if (m_bDragDataAcceptable)
  1247.         PasteEmbedded(*pDataObject, point);
  1248.  
  1249.     // update the document and views
  1250.     GetDocument()->SetModifiedFlag();
  1251.     GetDocument()->UpdateAllViews(NULL, 0, NULL);      // including this view
  1252.  
  1253.     return TRUE;
  1254. }
  1255.  
  1256. void CDrawView::OnDragLeave()
  1257. {
  1258.     CClientDC dc(this);
  1259.     if (m_prevDropEffect != DROPEFFECT_NONE)
  1260.     {
  1261.         dc.DrawFocusRect(CRect(m_dragPoint,m_dragSize)); // erase previous focus rect
  1262.         m_prevDropEffect = DROPEFFECT_NONE;
  1263.     }
  1264. }
  1265.  
  1266.  
  1267. void CDrawView::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
  1268. {
  1269.     // make sure window is active
  1270.     GetParentFrame()->ActivateFrame();
  1271.  
  1272.     CPoint local = point;
  1273.     ScreenToClient(&local);
  1274.     ClientToDoc(local);
  1275.  
  1276.     CDrawObj* pObj;
  1277.     pObj = GetDocument()->ObjectAt(local);
  1278.     if(pObj != NULL)
  1279.     {
  1280.         if(!IsSelected(pObj))
  1281.             Select( pObj, FALSE );          // reselect item if appropriate
  1282.         UpdateWindow();
  1283.  
  1284.         CMenu menu;
  1285.         if (menu.LoadMenu(ID_POPUP_MENU))
  1286.         {
  1287.             CMenu* pPopup = menu.GetSubMenu(0);
  1288.             ASSERT(pPopup != NULL);
  1289.  
  1290.             pPopup->TrackPopupMenu(TPM_RIGHTBUTTON | TPM_LEFTALIGN,
  1291.                                    point.x, point.y,
  1292.                                    AfxGetMainWnd()); // route commands through main window
  1293.         }
  1294.     }
  1295. }
  1296.  
  1297. void CDrawView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  1298. {
  1299.     if (pInfo->m_bPreview == FALSE)
  1300.         ((CDrawDoc*)GetDocument())->m_pSummInfo->RecordPrintDate();
  1301.     OnDraw(pDC);
  1302. }
  1303.