home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SCRIB7.PAK / SCRIBVW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.3 KB  |  382 lines

  1. // ScribVw.cpp : implementation of the CScribbleView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "ScribVw.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleView
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleView, CScrollView)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleView, CScrollView)
  31.     //{{AFX_MSG_MAP(CScribbleView)
  32.     ON_WM_LBUTTONDOWN()
  33.     ON_WM_LBUTTONUP()
  34.     ON_WM_MOUSEMOVE()
  35.     ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)
  36.     ON_WM_SIZE()
  37.     //}}AFX_MSG_MAP
  38.     // Standard printing commands
  39.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  40.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  41.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  42. END_MESSAGE_MAP()
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CScribbleView construction/destruction
  46.  
  47. CScribbleView::CScribbleView()
  48. {
  49.     SetScrollSizes(MM_TEXT, CSize(0, 0));
  50. }
  51.  
  52. CScribbleView::~CScribbleView()
  53. {
  54. }
  55.  
  56. BOOL CScribbleView::PreCreateWindow(CREATESTRUCT& cs)
  57. {
  58.     // TODO: Modify the Window class or styles here by modifying
  59.     //  the CREATESTRUCT cs
  60.  
  61.     return CView::PreCreateWindow(cs);
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CScribbleView drawing
  66.  
  67. void CScribbleView::OnDraw(CDC* pDC)
  68. {
  69.     CScribbleDoc* pDoc = GetDocument();
  70.     ASSERT_VALID(pDoc);
  71.  
  72.     // Get the invalidated rectangle of the view, or in the case
  73.     // of printing, the clipping region of the printer dc.
  74.     CRect rectClip;
  75.     CRect rectStroke;
  76.     pDC->GetClipBox(&rectClip);
  77.     pDC->LPtoDP(&rectClip);
  78.     rectClip.InflateRect(1, 1); // avoid rounding to nothing
  79.  
  80.     // Note: CScrollView::OnPaint() will have already adjusted the
  81.     // viewport origin before calling OnDraw(), to reflect the
  82.     // currently scrolled position.
  83.  
  84.     // The view delegates the drawing of individual strokes to
  85.     // CStroke::DrawStroke().
  86.     CTypedPtrList<CObList,CStroke*>& strokeList = pDoc->m_strokeList;
  87.     POSITION pos = strokeList.GetHeadPosition();
  88.     while (pos != NULL)
  89.     {
  90.         CStroke* pStroke = strokeList.GetNext(pos);
  91.         rectStroke = pStroke->GetBoundingRect();
  92.         pDC->LPtoDP(&rectStroke);
  93.         rectStroke.InflateRect(1, 1); // avoid rounding to nothing
  94.         if (!rectStroke.IntersectRect(&rectStroke, &rectClip))
  95.             continue;
  96.         pStroke->DrawStroke(pDC);
  97.     }
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CScribbleView printing
  102.  
  103. BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
  104. {
  105.     pInfo->SetMaxPage(2);   // the document is two pages long:
  106.                             // the first page is the title page
  107.                             // the second is the drawing
  108.     BOOL bRet = DoPreparePrinting(pInfo);    // default preparation
  109.     pInfo->m_nNumPreviewPages = 2;  // Preview 2 pages at a time
  110.     // Set this value after calling DoPreparePrinting to override
  111.     // value read from .INI file
  112.     return bRet;
  113. }
  114.  
  115. void CScribbleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  116. {
  117.     // TODO: add extra initialization before printing
  118. }
  119.  
  120. void CScribbleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  121. {
  122.     // TODO: add cleanup after printing
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126. // CScribbleView diagnostics
  127.  
  128. #ifdef _DEBUG
  129. void CScribbleView::AssertValid() const
  130. {
  131.     CScrollView::AssertValid();
  132. }
  133.  
  134. void CScribbleView::Dump(CDumpContext& dc) const
  135. {
  136.     CScrollView::Dump(dc);
  137. }
  138.  
  139. CScribbleDoc* CScribbleView::GetDocument() // non-debug version is inline
  140. {
  141.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScribbleDoc)));
  142.     return (CScribbleDoc*)m_pDocument;
  143. }
  144. #endif //_DEBUG
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147. // CScribbleView message handlers
  148.  
  149. void CScribbleView::OnLButtonDown(UINT, CPoint point) 
  150. {
  151.     // Pressing the mouse button in the view window starts a new stroke
  152.  
  153.     // CScrollView changes the viewport origin and mapping mode.
  154.     // It's necessary to convert the point from device coordinates
  155.     // to logical coordinates, such as are stored in the document.
  156.     CClientDC dc(this);
  157.     OnPrepareDC(&dc);
  158.     dc.DPtoLP(&point);
  159.  
  160.     m_pStrokeCur = GetDocument()->NewStroke();
  161.     // Add first point to the new stroke
  162.     m_pStrokeCur->m_pointArray.Add(point);
  163.  
  164.     SetCapture();       // Capture the mouse until button up.
  165.     m_ptPrev = point;   // Serves as the MoveTo() anchor point for the
  166.                         // LineTo() the next point, as the user drags the
  167.                         // mouse.
  168.  
  169.     return;
  170. }
  171.  
  172. void CScribbleView::OnLButtonUp(UINT, CPoint point) 
  173. {
  174.     // Mouse button up is interesting in the Scribble application
  175.     // only if the user is currently drawing a new stroke by dragging
  176.     // the captured mouse.
  177.  
  178.     if (GetCapture() != this)
  179.         return; // If this window (view) didn't capture the mouse,
  180.                 // then the user isn't drawing in this window.
  181.  
  182.     CScribbleDoc* pDoc = GetDocument();
  183.  
  184.     CClientDC dc(this);
  185.  
  186.     // CScrollView changes the viewport origin and mapping mode.
  187.     // It's necessary to convert the point from device coordinates
  188.     // to logical coordinates, such as are stored in the document.
  189.     OnPrepareDC(&dc);  // set up mapping mode and viewport origin
  190.     dc.DPtoLP(&point);
  191.  
  192.     CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());
  193.     dc.MoveTo(m_ptPrev);
  194.     dc.LineTo(point);
  195.     dc.SelectObject(pOldPen);
  196.     m_pStrokeCur->m_pointArray.Add(point);
  197.  
  198.     // Tell the stroke item that we're done adding points to it.
  199.     // This is so it can finish computing its bounding rectangle.
  200.     m_pStrokeCur->FinishStroke();
  201.  
  202.     // Tell the other views that this stroke has been added
  203.     // so that they can invalidate this stroke's area in their
  204.     // client area.
  205.     pDoc->UpdateAllViews(this, 0L, m_pStrokeCur);
  206.  
  207.     ReleaseCapture();   // Release the mouse capture established at
  208.                         // the beginning of the mouse drag.
  209.     pDoc->NotifyChanged();
  210.     return;
  211. }
  212.  
  213. void CScribbleView::OnMouseMove(UINT, CPoint point) 
  214. {
  215.     // Mouse movement is interesting in the Scribble application
  216.     // only if the user is currently drawing a new stroke by dragging
  217.     // the captured mouse.
  218.  
  219.     if (GetCapture() != this)
  220.         return; // If this window (view) didn't capture the mouse,
  221.                 // then the user isn't drawing in this window.
  222.  
  223.     CClientDC dc(this);
  224.     // CScrollView changes the viewport origin and mapping mode.
  225.     // It's necessary to convert the point from device coordinates
  226.     // to logical coordinates, such as are stored in the document.
  227.     OnPrepareDC(&dc);
  228.     dc.DPtoLP(&point);
  229.  
  230.     m_pStrokeCur->m_pointArray.Add(point);
  231.  
  232.     // Draw a line from the previous detected point in the mouse
  233.     // drag to the current point.
  234.     CPen* pOldPen = dc.SelectObject(GetDocument()->GetCurrentPen());
  235.     dc.MoveTo(m_ptPrev);
  236.     dc.LineTo(point);
  237.     dc.SelectObject(pOldPen);
  238.     m_ptPrev = point;
  239.     return;
  240. }
  241.  
  242. void CScribbleView::OnUpdate(CView* /* pSender */, LPARAM /* lHint */, 
  243.     CObject* pHint) 
  244. {
  245.     // The document has informed this view that some data has changed.
  246.  
  247.     if (pHint != NULL)
  248.     {
  249.         if (pHint->IsKindOf(RUNTIME_CLASS(CStroke)))
  250.         {
  251.             // The hint is that a stroke as been added (or changed).
  252.             // So, invalidate its rectangle.
  253.             CStroke* pStroke = (CStroke*)pHint;
  254.             CClientDC dc(this);
  255.             OnPrepareDC(&dc);
  256.             CRect rectInvalid = pStroke->GetBoundingRect();
  257.             dc.LPtoDP(&rectInvalid);
  258.             InvalidateRect(&rectInvalid);
  259.             return;
  260.         }
  261.     }
  262.     // We can't interpret the hint, so assume that anything might
  263.     // have been updated.
  264.     Invalidate(TRUE);
  265.     return;
  266. }
  267.  
  268. void CScribbleView::OnInitialUpdate() 
  269. {
  270.         ResyncScrollSizes();
  271.     CScrollView::OnInitialUpdate();
  272. }
  273.  
  274. void CScribbleView::ResyncScrollSizes()
  275. {
  276.     CClientDC dc(NULL);
  277.     OnPrepareDC(&dc);
  278.     CSize sizeDoc = GetDocument()->GetDocSize();
  279.     dc.LPtoDP(&sizeDoc);
  280.     SetScrollSizes(MM_TEXT, sizeDoc);
  281. }
  282.  
  283. void CScribbleView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  284. {
  285.     if (pInfo->m_nCurPage == 1)  // page no. 1 is the title page
  286.     {
  287.         PrintTitlePage(pDC, pInfo);
  288.         return; // nothing else to print on page 1 but the page title
  289.     }
  290.     CString strHeader = GetDocument()->GetTitle();
  291.  
  292.     PrintPageHeader(pDC, pInfo, strHeader);
  293.     // PrintPageHeader() subtracts out from the pInfo->m_rectDraw the
  294.     // amount of the page used for the header.
  295.  
  296.     pDC->SetWindowOrg(pInfo->m_rectDraw.left,-pInfo->m_rectDraw.top);
  297.  
  298.     // Now print the rest of the page
  299.     OnDraw(pDC);
  300. }
  301.  
  302. void CScribbleView::PrintTitlePage(CDC* pDC, CPrintInfo* pInfo)
  303. {
  304.     // Prepare a font size for displaying the file name
  305.     LOGFONT logFont;
  306.     memset(&logFont, 0, sizeof(LOGFONT));
  307.     logFont.lfHeight = 75;  //  3/4th inch high in MM_LOENGLISH
  308.                             // (1/100th inch)
  309.     CFont font;
  310.     CFont* pOldFont = NULL;
  311.     if (font.CreateFontIndirect(&logFont))
  312.         pOldFont = pDC->SelectObject(&font);
  313.  
  314.     // Get the file name, to be displayed on title page
  315.     CString strPageTitle = GetDocument()->GetTitle();
  316.  
  317.     // Display the file name 1 inch below top of the page,
  318.     // centered horizontally
  319.     pDC->SetTextAlign(TA_CENTER);
  320.     pDC->TextOut(pInfo->m_rectDraw.right/2, -100, strPageTitle);
  321.  
  322.     if (pOldFont != NULL)
  323.         pDC->SelectObject(pOldFont);
  324. }
  325.  
  326. void CScribbleView::PrintPageHeader(CDC* pDC, CPrintInfo* pInfo,
  327.     CString& strHeader)
  328. {
  329.     // Print a page header consisting of the name of
  330.     // the document and a horizontal line
  331.     pDC->SetTextAlign(TA_LEFT);
  332.     pDC->TextOut(0,-25, strHeader);  // 1/4 inch down
  333.  
  334.     // Draw a line across the page, below the header
  335.     TEXTMETRIC textMetric;
  336.     pDC->GetTextMetrics(&textMetric);
  337.     int y = -35 - textMetric.tmHeight;          // line 1/10th inch below text
  338.     pDC->MoveTo(0, y);                          // from left margin
  339.     pDC->LineTo(pInfo->m_rectDraw.right, y);    // to right margin
  340.  
  341.     // Subtract out from the drawing rectange the space used by the header.
  342.     y -= 25;    // space 1/4 inch below (top of) line
  343.     pInfo->m_rectDraw.top += y;
  344. }
  345.  
  346. // The following command handler provides the standard keyboard
  347. //  user interface to cancel an in-place editing session.  Here,
  348. //  the server (not the container) causes the deactivation.
  349. void CScribbleView::OnCancelEditSrvr() 
  350. {
  351.     GetDocument()->OnDeactivateUI(FALSE);
  352. }
  353.  
  354. void CScribbleView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
  355. {
  356.     CScribbleDoc* pDoc = GetDocument();
  357.     CScrollView::OnPrepareDC(pDC, pInfo);
  358.  
  359.     pDC->SetMapMode(MM_ANISOTROPIC);
  360.     CSize sizeDoc = pDoc->GetDocSize();
  361.     sizeDoc.cy = -sizeDoc.cy;
  362.     pDC->SetWindowExt(sizeDoc);
  363.  
  364.     CSize sizeNum, sizeDenom;
  365.     pDoc->GetZoomFactor(&sizeNum, &sizeDenom);
  366.  
  367.     int xLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSX);
  368.     int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);
  369.  
  370.     long xExt = (long)sizeDoc.cx * xLogPixPerInch * sizeNum.cx;
  371.     xExt /= 100 * (long)sizeDenom.cx;
  372.     long yExt = (long)sizeDoc.cy * yLogPixPerInch * sizeNum.cy;
  373.     yExt /= 100 * (long)sizeDenom.cy;
  374.     pDC->SetViewportExt((int)xExt, (int)-yExt);
  375. }
  376.  
  377. void CScribbleView::OnSize(UINT nType, int cx, int cy) 
  378. {
  379.         ResyncScrollSizes();        // ensure that scroll info is up-to-date
  380.     CScrollView::OnSize(nType, cx, cy);
  381. }
  382.