Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



Listing 5.2 EditorView.h and EditorView.cpp

// EditorView.h : interface of the CEditorView class
//
/////////////////////////////////////////////////////////////////////////////

#if 
!defined(AFX_EDITORVIEW_H__3FD89C6F_A149_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_EDITORVIEW_H__3FD89C6F_A149_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CEditorView : public CView
{
protected: // create from serialization only
    CEditorView();
    DECLARE_DYNCREATE(CEditorView)

// Attributes
public:
    CEditorDoc* GetDocument();

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides

    //{{AFX_VIRTUAL(CEditorView)
    public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
    protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CEditorView();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:
    int x, y;
    bool m_SelectFlag;
    bool m_CaretFlag;
    CPoint caret;

// Generated message map functions
protected:
    //{{AFX_MSG(CEditorView)
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnEditCopy();
    afx_msg void OnUpdateEditPaste(CCmdUI* pCmdUI);
    afx_msg void OnEditPaste();
    afx_msg void OnEditCut();
    afx_msg void OnEditSelectall();
    afx_msg void OnUpdateEditCut(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditCopy(CCmdUI* pCmdUI);
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnSetFocus(CWnd* pOldWnd);

    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in EditorView.cpp
inline CEditorDoc* CEditorView::GetDocument()
   { return (CEditorDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_EDITORVIEW_H__3FD89C6F_A149_11D1_887F_D42B07C10710__INCLUDED_)

// EditorView.cpp : implementation of the CEditorView class
//

#include “stdafx.h”
#include “Editor.h”

#include “EditorDoc.h”
#include “EditorView.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditorView

IMPLEMENT_DYNCREATE(CEditorView, CView)

BEGIN_MESSAGE_MAP(CEditorView, CView)
    //{{AFX_MSG_MAP(CEditorView)
    ON_WM_CHAR()
    ON_WM_LBUTTONDOWN()
    ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
    ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
    ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
    ON_COMMAND(ID_EDIT_CUT, OnEditCut)
    ON_COMMAND(ID_EDIT_SELECTALL, OnEditSelectall)
    ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
    ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
    ON_WM_KILLFOCUS()
    ON_WM_SETFOCUS()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditorView construction/destruction

CEditorView::CEditorView()
{
    // TODO: add construction code here
    m_SelectFlag = false;
    caret.x = caret.y = 0;
    m_CaretFlag = false;

}

CEditorView::~CEditorView()
{

}

BOOL CEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS, AfxGetApp()-
>LoadStandardCursor(IDC_IBEAM), (HBRUSH)(COLOR_WINDOW + 1), AfxGetApp()-
>LoadIcon(IDR_MAINFRAME));
    return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEditorView drawing

void CEditorView::OnDraw(CDC* pDC)
{
    if(!m_CaretFlag){
     TEXTMETRIC textmetric;

     pDC->GetTextMetrics(&textmetric);

     CreateSolidCaret(textmetric.tmAveCharWidth/4, textmetric.tmHeight);
     caret.x = caret.y = 0;

     SetCaretPos(caret);

     ShowCaret();

     m_CaretFlag = true;
    }

    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    CString out_string(pDoc->m_data.GetText());

    if(m_SelectFlag){

     pDC->SetBkColor(RGB(0, 0, 0));
     pDC->SetTextColor(RGB(255, 255, 255,));
    }
    else{
     pDC->SetBkColor(RGB(255, 255, 255));
     pDC->SetTextColor(RGB(0, 0, 0,));
    }

    pDC->TextOut(pDoc->m_data.GetX(), pDoc->m_data.GetY(), out_string);

    HideCaret();

    caret.x = pDoc->m_data.GetX() + (pDC->GetTextExtent(pDoc->m_data.GetText())).cx;
    caret.y = pDoc->m_data.GetY();

    SetCaretPos(caret);

    ShowCaret();

    // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CEditorView printing

BOOL CEditorView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // default preparation
    return DoPreparePrinting(pInfo);
}

void CEditorView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
    pInfo->SetMinPage(1);
    pInfo->SetMaxPage(2);

    // TODO: add extra initialization before printing
}
void CEditorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEditorView diagnostics

#ifdef _DEBUG
void CEditorView::AssertValid() const
{
    CView::AssertValid();
}

void CEditorView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CEditorDoc* CEditorView::GetDocument() // non-debug version is inline
{
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEditorDoc)));
    return (CEditorDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEditorView message handlers

void CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default
    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    if(nChar == ‘\b’)
     pDoc->m_data.SetText(pDoc->m_data.GetText().Left(pDoc->m_data.GetText().GetLength() - 1));
    else

     pDoc->m_data.SetText(pDoc->m_data.GetText() += nChar);

    Invalidate();

    pDoc->SetModifiedFlag(true);
    pDoc->UpdateAllViews(this);

    CView::OnChar(nChar, nRepCnt, nFlags);
}

void CEditorView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default

    HideCaret();

    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->m_data.SetX(point.x);
    caret.x = point.x;
    pDoc->m_data.SetY(point.y);
    caret.y = point.y;

    ShowCaret();

    pDoc->m_data.SetText(””);

    Invalidate();

    pDoc->SetModifiedFlag(true);
    pDoc->UpdateAllViews(this);

    CView::OnLButtonDown(nFlags, point);
}
void CEditorView::OnEditCopy()
{
    CEditorDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    HGLOBAL MemoryHandle;

    MemoryHandle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, pDoc->m_data.GetText().GetLength());

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    ::lstrcpy(pMemory, (LPCTSTR)pDoc->m_data.GetText());

    ::GlobalUnlock(MemoryHandle);

    OpenClipboard();

    EmptyClipboard();

    SetClipboardData(CF_TEXT, MemoryHandle);

    CloseClipboard();

    // TODO: Add your command handler code here

}

void CEditorView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
    // TODO: Add your command update UI handler code here

}

void CEditorView::OnEditPaste()
{

    OpenClipboard();

    HANDLE MemoryHandle;

    MemoryHandle = ::GetClipboardData(CF_TEXT);

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    pDoc->m_data.SetText(pMemory);

    ::GlobalUnlock(MemoryHandle);

    ::CloseClipboard();

    Invalidate();

    // TODO: Add your command handler code here

}

void CEditorView::OnEditCut()
{
    CEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    HGLOBAL MemoryHandle;

    MemoryHandle = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, pDoc->m_data.GetText().GetLength());

    char* pMemory;

    pMemory = (char*)::GlobalLock(MemoryHandle);

    ::lstrcpy(pMemory, (LPCTSTR)pDoc->m_data.GetText());

    ::GlobalUnlock(MemoryHandle);

    OpenClipboard();

    EmptyClipboard();

    SetClipboardData(CF_TEXT, MemoryHandle);

    CloseClipboard();

    pDoc->m_data.SetText(””);

    Invalidate();

    // TODO: Add your command handler code here

}

void CEditorView::OnEditSelectall()
{

    m_SelectFlag = !m_SelectFlag;
    Invalidate();

    // TODO: Add your command handler code here

}

void CEditorView::OnUpdateEditCut(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(m_SelectFlag);

    // TODO: Add your command update UI handler code here

}

void CEditorView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{

    pCmdUI->Enable(m_SelectFlag);

    // TODO: Add your command update UI handler code here

}

void CEditorView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
    // TODO: Add your specialized code here and/or call the base class

    if(!pInfo)
     return;

    CFont* font;

    font = new CFont();

    font->CreatePointFont(2400, CString(”Roman”));

    if(pInfo->m_nCurPage == 2){
     pDC->SelectObject(font);
    }

    CView::OnPrepareDC(pDC, pInfo);
}

void CEditorView::OnKillFocus(CWnd* pNewWnd)
{
    HideCaret();

    CView::OnKillFocus(pNewWnd);

    // TODO: Add your message handler code here

}

void CEditorView::OnSetFocus(CWnd* pOldWnd)
{

    ShowCaret();

    CView::OnSetFocus(pOldWnd);

    // TODO: Add your message handler code here

}

void CEditorView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
    // TODO: Add your specialized code here and/or call the base class

    char header[20];

    wsprintf(header, “Page %ld”, pInfo->m_nCurPage);

    pDC->TextOut(0, 0, header, strlen(header));

    OnDraw(pDC);
    CView::OnPrint(pDC, pInfo);
}

What’s Ahead

In the next chapter, we start working with a more involved topic: advanced graphics, including metafiles and screen capture. We learn some powerful and popular techniques that will save us a lot of time, so we turn to that now.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.