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

  1. // TextDeVw.cpp : implementation of the CTextDemoView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TextDemo.h"
  6.  
  7. #include "TextDDoc.h"
  8. #include "TextDeVw.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. // CTextDemoView
  18.  
  19. IMPLEMENT_DYNCREATE(CTextDemoView, CScrollView)
  20.  
  21. BEGIN_MESSAGE_MAP(CTextDemoView, CScrollView)
  22.    //{{AFX_MSG_MAP(CTextDemoView)
  23.    ON_WM_KEYDOWN()
  24.    //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CTextDemoView construction/destruction
  29.  
  30. CTextDemoView::CTextDemoView()
  31. {
  32.    // TODO: add construction code here
  33.  
  34. }
  35.  
  36. CTextDemoView::~CTextDemoView()
  37. {
  38. }
  39.  
  40. BOOL CTextDemoView::PreCreateWindow(CREATESTRUCT& cs)
  41. {
  42.    // TODO: Modify the Window class or styles here by modifying
  43.    //  the CREATESTRUCT cs
  44.  
  45.    return CScrollView::PreCreateWindow(cs);
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CTextDemoView drawing
  50.  
  51. void CTextDemoView::OnDraw(CDC* pDC)
  52. {
  53.    CTextDemoDoc* pDoc = GetDocument();
  54.    ASSERT_VALID(pDoc);
  55.  
  56.    // TODO: add draw code for native data here
  57.  
  58.    // return if font has not yet been created:
  59.    if (pDoc->m_Font.m_hObject == NULL)
  60.       return;                         
  61.    
  62.    RECT ClipRect;
  63.    int LineHeight;
  64.    TEXTMETRIC TM;
  65.    int Y = MARGIN;
  66.                                               
  67.    // select font into device context object:                                           
  68.    pDC->SelectObject (&pDoc->m_Font);  
  69.  
  70.    // obtain text metrics:
  71.    pDC->GetTextMetrics (&TM);
  72.    LineHeight = TM.tmHeight + TM.tmExternalLeading;
  73.    
  74.    // set text attributes:
  75.    pDC->SetTextColor (pDoc->m_Color);
  76.    pDC->SetBkMode (TRANSPARENT);
  77.       
  78.    // obtain coordinates of invalidated area:
  79.    pDC->GetClipBox (&ClipRect);
  80.                                           
  81.    // display title line:                                          
  82.    pDC->TextOut (MARGIN, Y, "FONT PROPERTIES");
  83.    
  84.    // display text lines:
  85.    for (int Line = 0; Line < NUMLINES; ++Line)
  86.       {
  87.       Y += LineHeight;
  88.       if (Y + LineHeight >= ClipRect.top && Y <= ClipRect.bottom)
  89.          pDC->TextOut (MARGIN, Y, pDoc->m_LineTable [Line]);
  90.       }   
  91. }
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CTextDemoView diagnostics
  95.  
  96. #ifdef _DEBUG
  97. void CTextDemoView::AssertValid() const
  98. {
  99.    CScrollView::AssertValid();
  100. }
  101.  
  102. void CTextDemoView::Dump(CDumpContext& dc) const
  103. {
  104.    CScrollView::Dump(dc);
  105. }
  106.  
  107. CTextDemoDoc* CTextDemoView::GetDocument() // non-debug version is inline
  108. {
  109.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDemoDoc)));
  110.    return (CTextDemoDoc*)m_pDocument;
  111. }
  112. #endif //_DEBUG
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CTextDemoView message handlers
  116.  
  117. void CTextDemoView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  118. {
  119.    // TODO: Add your specialized code here and/or call the base class
  120.    
  121.    CTextDemoDoc* PDoc = GetDocument();
  122.  
  123.    if (PDoc->m_Font.m_hObject == NULL)  // font not yet created
  124.       SetScrollSizes (MM_TEXT, CSize (0,0));
  125.    else                                 // font created
  126.       {
  127.       CClientDC ClientDC (this);
  128.       int LineWidth = 0;
  129.       SIZE Size;   
  130.       TEXTMETRIC TM;
  131.       
  132.       ClientDC.SelectObject (&PDoc->m_Font);
  133.       ClientDC.GetTextMetrics (&TM);
  134.       
  135.       for (int Line = 0; Line < NUMLINES; ++Line)
  136.          {
  137.          Size = ClientDC.GetTextExtent 
  138.             (PDoc->m_LineTable [Line], 
  139.              PDoc->m_LineTable [Line].GetLength ());
  140.          if (Size.cx > LineWidth)
  141.             LineWidth = Size.cx;          
  142.          }   
  143.          
  144.       Size.cx = LineWidth + MARGIN;
  145.       Size.cy = (TM.tmHeight + TM.tmExternalLeading) * (NUMLINES + 1) + MARGIN;
  146.       
  147.       SetScrollSizes (MM_TEXT, Size);
  148.       ScrollToPosition (CPoint (0, 0));
  149.       }  
  150.    
  151.    CScrollView::OnUpdate (pSender, lHint, pHint);   
  152. }
  153.  
  154. void CTextDemoView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  155. {
  156.    // TODO: Add your message handler code here and/or call default
  157.    
  158.    CSize DocSize = GetTotalSize ();
  159.    RECT ClientRect;
  160.    GetClientRect (&ClientRect);
  161.  
  162.    switch (nChar)
  163.       {  
  164.       case VK_LEFT:     // left arrow
  165.          if (ClientRect.right < DocSize.cx)
  166.             SendMessage (WM_HSCROLL, SB_LINEUP);
  167.          break;
  168.          
  169.       case VK_RIGHT:    // right arrow
  170.          if (ClientRect.right < DocSize.cx)
  171.             SendMessage (WM_HSCROLL, SB_LINEDOWN);
  172.          break;
  173.                      
  174.       case VK_UP:       // up arrow
  175.          if (ClientRect.bottom < DocSize.cy)
  176.             SendMessage (WM_VSCROLL, SB_LINEUP);           
  177.          break;
  178.          
  179.       case VK_DOWN:     // down arrow
  180.          if (ClientRect.bottom < DocSize.cy)
  181.             SendMessage (WM_VSCROLL, SB_LINEDOWN);           
  182.          break;
  183.  
  184.       case VK_HOME:     // Home key
  185.          if (::GetKeyState (VK_CONTROL) & 0x8000)  // Ctrl pressed
  186.             {
  187.             if (ClientRect.bottom < DocSize.cy)
  188.                SendMessage (WM_VSCROLL, SB_TOP);
  189.             }
  190.          else                                      // Home key alone
  191.             {                                    
  192.             if (ClientRect.right < DocSize.cx)
  193.                SendMessage (WM_HSCROLL, SB_TOP);
  194.             }    
  195.          break;   
  196.             
  197.       case VK_END:      // End key
  198.          if (::GetKeyState (VK_CONTROL) & 0x8000)  // Ctrl pressed
  199.             {
  200.             if (ClientRect.bottom < DocSize.cy)
  201.                SendMessage (WM_VSCROLL, SB_BOTTOM);
  202.             }
  203.          else                                      // End key alone
  204.             {                                    
  205.             if (ClientRect.right < DocSize.cx)
  206.                SendMessage (WM_HSCROLL, SB_BOTTOM);
  207.             }    
  208.          break;   
  209.  
  210.       case VK_PRIOR:    // PgUp key                      
  211.          if (ClientRect.bottom < DocSize.cy)
  212.             SendMessage (WM_VSCROLL, SB_PAGEUP);           
  213.          break;
  214.          
  215.       case VK_NEXT:     // PgDn key                      
  216.          if (ClientRect.bottom < DocSize.cy)
  217.             SendMessage (WM_VSCROLL, SB_PAGEDOWN);           
  218.          break;
  219.       }
  220.  
  221.    CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
  222. }
  223.