home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / echo / echoview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-30  |  3.7 KB  |  165 lines

  1. // EchoView.cpp : implementation of the CEchoView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Echo.h"
  6.  
  7. #include "EchoDoc.h"
  8. #include "EchoView.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. // CEchoView
  18.  
  19. IMPLEMENT_DYNCREATE(CEchoView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CEchoView, CView)
  22.    //{{AFX_MSG_MAP(CEchoView)
  23.    ON_WM_CHAR()
  24.    ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  25.    ON_WM_CREATE()
  26.    ON_WM_KILLFOCUS()
  27.    ON_WM_SETFOCUS()
  28.    //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CEchoView construction/destruction
  33.  
  34. CEchoView::CEchoView()
  35. {
  36.    // TODO: add construction code here
  37.    m_CaretPos.x = m_CaretPos.y = 0;
  38. }
  39.  
  40. CEchoView::~CEchoView()
  41. {
  42. }
  43.  
  44. BOOL CEchoView::PreCreateWindow(CREATESTRUCT& cs)
  45. {
  46.    // TODO: Modify the Window class or styles here by modifying
  47.    //  the CREATESTRUCT cs
  48.  
  49.    return CView::PreCreateWindow(cs);
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CEchoView drawing
  54.  
  55. void CEchoView::OnDraw(CDC* pDC)
  56. {
  57.    CEchoDoc* pDoc = GetDocument();
  58.    ASSERT_VALID(pDoc);
  59.  
  60.    // TODO: add draw code for native data here
  61.  
  62.    pDC->SetTextColor (::GetSysColor (COLOR_WINDOWTEXT));
  63.    pDC->SetBkMode (TRANSPARENT);
  64.    pDC->TextOut (0, 0, pDoc->m_TextLine);
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CEchoView diagnostics
  69.  
  70. #ifdef _DEBUG
  71. void CEchoView::AssertValid() const
  72. {
  73.    CView::AssertValid();
  74. }
  75.  
  76. void CEchoView::Dump(CDumpContext& dc) const
  77. {
  78.    CView::Dump(dc);
  79. }
  80.  
  81. CEchoDoc* CEchoView::GetDocument() // non-debug version is inline
  82. {
  83.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEchoDoc)));
  84.    return (CEchoDoc*)m_pDocument;
  85. }
  86. #endif //_DEBUG
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CEchoView message handlers
  90.  
  91. void CEchoView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  92. {
  93.    // TODO: Add your message handler code here and/or call default
  94.  
  95.    if (nChar < 32)
  96.       {
  97.       ::MessageBeep ((UINT)-1);
  98.       return;
  99.       }
  100.  
  101.    CEchoDoc* PDoc = GetDocument();
  102.    PDoc->m_TextLine += nChar;
  103.  
  104.    CClientDC ClientDC (this);
  105.    ClientDC.SetTextColor (::GetSysColor (COLOR_WINDOWTEXT));
  106.    ClientDC.SetBkMode (TRANSPARENT);
  107.    HideCaret ();   
  108.    ClientDC.TextOut (0, 0, PDoc->m_TextLine);
  109.    CSize Size = ClientDC.GetTextExtent
  110.       (PDoc->m_TextLine,
  111.       PDoc->m_TextLine.GetLength ());
  112.    m_CaretPos.x = Size.cx;
  113.    SetCaretPos (m_CaretPos);
  114.    ShowCaret ();
  115.  
  116.    CView::OnChar(nChar, nRepCnt, nFlags);
  117. }
  118.  
  119. void CEchoView::OnEditClear() 
  120. {
  121.    // TODO: Add your command handler code here
  122.  
  123.    CEchoDoc* PDoc = GetDocument();
  124.    PDoc->m_TextLine.Empty ();
  125.    PDoc->UpdateAllViews (NULL);
  126.    m_CaretPos.x = 0;
  127.    SetCaretPos (m_CaretPos);
  128. }
  129.  
  130. int CEchoView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  131. {
  132.    if (CView::OnCreate(lpCreateStruct) == -1)
  133.       return -1;
  134.    
  135.    // TODO: Add your specialized creation code here
  136.  
  137.    CClientDC ClientDC (this);
  138.    TEXTMETRIC TM;
  139.    
  140.    m_XCaret = ::GetSystemMetrics (SM_CXBORDER) * 2;
  141.    ClientDC.GetTextMetrics (&TM);
  142.    m_YCaret = TM.tmHeight + TM.tmExternalLeading;
  143.    
  144.    return 0;
  145. }
  146.  
  147. void CEchoView::OnKillFocus(CWnd* pNewWnd) 
  148. {
  149.    CView::OnKillFocus(pNewWnd);
  150.    
  151.    // TODO: Add your message handler code here
  152.    ::DestroyCaret ();   
  153. }
  154.  
  155. void CEchoView::OnSetFocus(CWnd* pOldWnd) 
  156. {
  157.    CView::OnSetFocus(pOldWnd);
  158.    
  159.    // TODO: Add your message handler code here
  160.  
  161.    CreateSolidCaret (m_XCaret, m_YCaret);
  162.    SetCaretPos (m_CaretPos);
  163.    ShowCaret ();
  164. }
  165.