home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / minidrw4 / minidrvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  6.0 KB  |  231 lines

  1. // MiniDrVw.cpp : implementation of the CMiniDrawView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MiniDraw.h"
  6.  
  7. #include "MiniDDoc.h"
  8. #include "MiniDrVw.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMiniDrawView
  18.  
  19. IMPLEMENT_DYNCREATE(CMiniDrawView, CScrollView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMiniDrawView, CScrollView)
  22.    //{{AFX_MSG_MAP(CMiniDrawView)
  23.    ON_WM_LBUTTONDOWN()
  24.    ON_WM_MOUSEMOVE()
  25.    ON_WM_LBUTTONUP()
  26.    //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CMiniDrawView construction/destruction
  31.  
  32. CMiniDrawView::CMiniDrawView()
  33. {
  34.    // TODO: add construction code here
  35.  
  36.    m_Dragging = 0;
  37.    m_HArrow = AfxGetApp ()->LoadStandardCursor (IDC_ARROW);
  38.    m_HCross = AfxGetApp ()->LoadStandardCursor (IDC_CROSS);
  39. }
  40.  
  41. CMiniDrawView::~CMiniDrawView()
  42. {
  43. }
  44.  
  45. BOOL CMiniDrawView::PreCreateWindow(CREATESTRUCT& cs)
  46. {
  47.    // TODO: Modify the Window class or styles here by modifying
  48.    //  the CREATESTRUCT cs
  49.  
  50.    m_ClassName = AfxRegisterWndClass
  51.       (CS_HREDRAW | CS_VREDRAW,                // class styles
  52.       0,                                       // no cursor
  53.       (HBRUSH)::GetStockObject (WHITE_BRUSH),  // assign white background brush
  54.       0);                                      // no icon
  55.    cs.lpszClass = m_ClassName;      
  56.  
  57.    return CScrollView::PreCreateWindow(cs);
  58. }
  59.    
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CMiniDrawView drawing
  62.  
  63. void CMiniDrawView::OnDraw(CDC* pDC)
  64. {
  65.    CMiniDrawDoc* pDoc = GetDocument();
  66.    ASSERT_VALID(pDoc);
  67.  
  68.    // TODO: add draw code for native data here
  69.  
  70.    CSize ScrollSize = GetTotalSize ();
  71.    pDC->MoveTo (ScrollSize.cx, 0);
  72.    pDC->LineTo (ScrollSize.cx, ScrollSize.cy);
  73.    pDC->LineTo (0, ScrollSize.cy);
  74.  
  75.    CRect ClipRect;
  76.    CRect DimRect;
  77.    CRect IntRect;
  78.    CLine *PLine;
  79.    pDC->GetClipBox (&ClipRect);
  80.  
  81.    int Index = pDoc->GetNumLines ();
  82.    while (Index--)
  83.       {
  84.       PLine = pDoc->GetLine (Index);
  85.       DimRect = PLine->GetDimRect ();
  86.       if (IntRect.IntersectRect (DimRect, ClipRect))
  87.          PLine->Draw (pDC);
  88.       }   
  89. }
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CMiniDrawView diagnostics
  93.  
  94. #ifdef _DEBUG
  95. void CMiniDrawView::AssertValid() const
  96. {
  97.    CScrollView::AssertValid();
  98. }
  99.  
  100. void CMiniDrawView::Dump(CDumpContext& dc) const
  101. {
  102.    CScrollView::Dump(dc);
  103. }
  104.  
  105. CMiniDrawDoc* CMiniDrawView::GetDocument() // non-debug version is inline
  106. {
  107.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMiniDrawDoc)));
  108.    return (CMiniDrawDoc*)m_pDocument;
  109. }
  110. #endif //_DEBUG
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CMiniDrawView message handlers
  114.  
  115. void CMiniDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  116. {
  117.    // TODO: Add your message handler code here and/or call default
  118.  
  119.    CClientDC ClientDC (this);
  120.    OnPrepareDC (&ClientDC);
  121.    ClientDC.DPtoLP (&point);
  122.  
  123.    // test whether cursor is within drawing area of view window:
  124.    CSize ScrollSize = GetTotalSize ();
  125.    CRect ScrollRect (0, 0, ScrollSize.cx, ScrollSize.cy);
  126.    if (!ScrollRect.PtInRect (point))
  127.       return;
  128.  
  129.    // save cursor position, capture mouse, & set dragging flag:
  130.    m_PointOrigin = point;
  131.    m_PointOld = point;
  132.    SetCapture ();
  133.    m_Dragging = 1;
  134.  
  135.    // clip mouse cursor:
  136.    ClientDC.LPtoDP (&ScrollRect);
  137.    CRect ViewRect;
  138.    GetClientRect (&ViewRect);
  139.    CRect IntRect;
  140.    IntRect.IntersectRect (&ScrollRect, &ViewRect);
  141.    ClientToScreen (&IntRect);
  142.    ::ClipCursor (&IntRect);
  143.    
  144.    CScrollView::OnLButtonDown(nFlags, point);
  145. }
  146.  
  147. void CMiniDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  148. {
  149.    // TODO: Add your message handler code here and/or call default
  150.  
  151.    CClientDC ClientDC (this);
  152.    OnPrepareDC (&ClientDC);
  153.    ClientDC.DPtoLP (&point);
  154.  
  155.    CSize ScrollSize = GetTotalSize ();
  156.    CRect ScrollRect (0, 0, ScrollSize.cx, ScrollSize.cy);
  157.    if (ScrollRect.PtInRect (point))
  158.       ::SetCursor (m_HCross);
  159.    else
  160.       ::SetCursor (m_HArrow);      
  161.  
  162.    if (m_Dragging)
  163.       {
  164.       ClientDC.SetROP2 (R2_NOT);
  165.       ClientDC.MoveTo (m_PointOrigin);
  166.       ClientDC.LineTo (m_PointOld);
  167.       ClientDC.MoveTo (m_PointOrigin);
  168.       ClientDC.LineTo (point);
  169.       m_PointOld = point;
  170.       } 
  171.  
  172.    CScrollView::OnMouseMove(nFlags, point);
  173. }
  174.  
  175. void CMiniDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  176. {
  177.    // TODO: Add your message handler code here and/or call default
  178.    
  179.    if (m_Dragging)
  180.       {
  181.       m_Dragging = 0;
  182.       ::ReleaseCapture ();
  183.       ::ClipCursor (NULL);
  184.  
  185.       CClientDC ClientDC (this);
  186.       OnPrepareDC (&ClientDC);
  187.       ClientDC.DPtoLP (&point);
  188.       
  189.       ClientDC.SetROP2 (R2_NOT);
  190.       ClientDC.MoveTo (m_PointOrigin);
  191.       ClientDC.LineTo (m_PointOld);
  192.       ClientDC.SetROP2 (R2_COPYPEN);
  193.       ClientDC.MoveTo (m_PointOrigin);
  194.       ClientDC.LineTo (point);      
  195.  
  196.       CMiniDrawDoc* PDoc = GetDocument();
  197.       CLine *PCLine;
  198.       PCLine = PDoc->AddLine (m_PointOrigin.x, m_PointOrigin.y, 
  199.          point.x, point.y);
  200.       PDoc->UpdateAllViews (this, 0, PCLine);
  201.       }   
  202.  
  203.    CScrollView::OnLButtonUp(nFlags, point);
  204. }
  205.  
  206. void CMiniDrawView::OnInitialUpdate() 
  207. {
  208.    CScrollView::OnInitialUpdate();
  209.    
  210.    // TODO: Add your specialized code here and/or call the base class
  211.    
  212.    SIZE Size = {640, 480};
  213.    SetScrollSizes (MM_TEXT, Size); 
  214. }
  215.  
  216. void CMiniDrawView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  217. {
  218.    // TODO: Add your specialized code here and/or call the base class
  219.    
  220.    if (pHint != 0)
  221.       {
  222.       CRect InvalidRect = ((CLine *)pHint)->GetDimRect ();
  223.       CClientDC ClientDC (this);
  224.       OnPrepareDC (&ClientDC);
  225.       ClientDC.LPtoDP (&InvalidRect);
  226.       InvalidateRect (&InvalidRect);            
  227.       }
  228.    else
  229.       CScrollView::OnUpdate (pSender, lHint, pHint);      
  230. }
  231.