// mymfc20View.cpp : implementation of the CMymfc20View class
//

#include "stdafx.h"
#include "mymfc20.h"

#include "mymfc20Doc.h"
#include "mymfc20View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View

IMPLEMENT_DYNCREATE(CMymfc20View, CView)

BEGIN_MESSAGE_MAP(CMymfc20View, CView)
	//{{AFX_MSG_MAP(CMymfc20View)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View construction/destruction

CMymfc20View::CMymfc20View()
{
	// TODO: add construction code here

}

CMymfc20View::~CMymfc20View()
{
}

BOOL CMymfc20View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View drawing

void CMymfc20View::OnDraw(CDC* pDC)
{
	// TODO: add draw code for native data here
	int i, j;

    CMymfc20Doc* pDoc = GetDocument();
   
    j = pDoc->m_ellipseArray.GetUpperBound();

    for (i = 0; i < j; i++)
    {
        pDC->Ellipse(pDoc->m_ellipseArray[i]);
    }
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View printing

BOOL CMymfc20View::OnPreparePrinting(CPrintInfo* pInfo)
{
	CMymfc20Doc* pDoc = GetDocument();
    pInfo->SetMaxPage(pDoc->m_ellipseArray.GetUpperBound() /
                      CMymfc20Doc::nLinesPerPage + 1);
    return DoPreparePrinting(pInfo);
}

void CMymfc20View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMymfc20View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View diagnostics

#ifdef _DEBUG
void CMymfc20View::AssertValid() const
{
	CView::AssertValid();
}

void CMymfc20View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMymfc20Doc* CMymfc20View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMymfc20Doc)));
	return (CMymfc20Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMymfc20View message handlers

void CMymfc20View::PrintPageHeader(CDC *pDC)
{
	CString str;

    CPoint point(0, 0);
    pDC->TextOut(point.x, point.y, "Bubble Report");
    point += CSize(720, -720);
    str.Format("%6.6s %6.6s %6.6s %6.6s %6.6s",
               "Index", "Left", "Top", "Right", "Bottom");
    pDC->TextOut(point.x, point.y, str);
}

void CMymfc20View::PrintPageFooter(CDC *pDC)
{
	CString str;

    CPoint point(0, -14400); // Move 10 inches down
    CMymfc20Doc* pDoc = GetDocument();
    str.Format("Document %s", (LPCSTR) pDoc->GetTitle());
    pDC->TextOut(point.x, point.y, str);
    str.Format("Page %d", m_nPage);
    CSize size = pDC->GetTextExtent(str);
    point.x += 11520 - size.cx;
    pDC->TextOut(point.x, point.y, str); // right-justified
}

void CMymfc20View::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	// TODO: Add your specialized code here and/or call the base class
	pDC->SetMapMode(MM_LOENGLISH);	
}

void CMymfc20View::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
	 int        i, nStart, nEnd, nHeight;
    CString    str;
    CPoint     point(720, -1440);
    CFont      font;
    TEXTMETRIC tm;

    pDC->SetMapMode(MM_TWIPS);
    CMymfc20Doc* pDoc = GetDocument();
    // for PrintPageFooter's benefit
    m_nPage = pInfo->m_nCurPage;
    nStart = (m_nPage - 1) * CMymfc20Doc::nLinesPerPage;
    nEnd = nStart + CMymfc20Doc::nLinesPerPage;
    
    // 14-point fixed-pitch font
    font.CreateFont(-280, 0, 0, 0, 400, FALSE, FALSE,
                    0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                    DEFAULT_PITCH | FF_MODERN, "Courier New");
                    // Courier New is a TrueType font
    CFont* pOldFont = (CFont*) (pDC->SelectObject(&font));
    PrintPageHeader(pDC);
    pDC->GetTextMetrics(&tm);
    nHeight = tm.tmHeight + tm.tmExternalLeading;
    for (i = nStart; i < nEnd; i++) {
        if (i > pDoc->m_ellipseArray.GetUpperBound()) {
            break;
        }
        str.Format("%6d %6d %6d %6d %6d", i + 1,
                   pDoc->m_ellipseArray[i].left,
                   pDoc->m_ellipseArray[i].top,
                   pDoc->m_ellipseArray[i].right,
                   pDoc->m_ellipseArray[i].bottom);
        point.y -= nHeight;
        pDC->TextOut(point.x, point.y, str);
    }
    PrintPageFooter(pDC);
    pDC->SelectObject(pOldFont);
}

