// Exp10View.cpp : implementation of the CExp10View class
//

#include "stdafx.h"
#include "Exp10.h"
// -- Exp10 (start)
#include "Connection.h"
// -- Exp10 (end)
#include "Exp10Doc.h"
#include "Exp10View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExp10View

IMPLEMENT_DYNCREATE(CExp10View, CView)

BEGIN_MESSAGE_MAP(CExp10View, CView)
	//{{AFX_MSG_MAP(CExp10View)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CExp10View construction/destruction

CExp10View::CExp10View()
{
	// TODO: add construction code here

}

CExp10View::~CExp10View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExp10View drawing

void CExp10View::OnDraw(CDC* pDC)
{
	CExp10Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	// -- Exp10 (start)
	pDoc->DrawConnections(pDC);
	// -- Exp10 (end)
}

/////////////////////////////////////////////////////////////////////////////
// CExp10View printing

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

void CExp10View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CExp10View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CExp10View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CExp10View message handlers

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

	// Capture the mouse so no other apps. can get it.
	SetCapture();

	// Save the pt that the cursor is at, upon left-btn-down, i.e. init member var with incoming CPoint var.
	m_ptPrevPos = point;	// Init the prev. pt, to be used in OnMouseMove(), to the current point
	m_ptOrigin = point;		// Init the origin of the ensuing line, to be drawn in OnMouseMove(), to be the starting pt.

	// -- Exp10 (end)

	CView::OnLButtonDown(nFlags, point);
}

void CExp10View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	// -- Exp10 (start)

	// Check to see if the left mouse btn. is down
	if((nFlags & MK_LBUTTON) == MK_LBUTTON)
	{
		// Check to see if mouse is captured
		if(GetCapture() == this)	// If what has been captured is the obj. upon which the present fn is called.
		{
			// Get the device context
			CClientDC dc(this);

			// Reverse the pixel color from the original pt to the prev pt.
			// SetROP2() sets the current foreground mix mode.
			dc.SetROP2(R2_NOT);		// R2_NOT => pixel is the inverse of the screen color

			// Declare a CConnection obj in order to call DrawConnection()
			CConnection con_obj1(m_ptOrigin, m_ptPrevPos);
			con_obj1.DrawConnection(&dc);
			//dc.MoveTo(m_ptOrigin);
			//dc.LineTo(m_ptPrevPos);	

			// Declare a CConnection obj in order to call DrawConnection()
			// Draw the current stretch of line (but don't save it until OnLButtonUp().
			CConnection con_obj2(m_ptOrigin, point);
			con_obj2.DrawConnection(&dc);
			//dc.MoveTo(m_ptOrigin);
			//dc.LineTo(point);

			// Save the current pt (point) as the prev. pt (m_ptPrevPos) of the CView class.
			m_ptPrevPos = point;
		}
	}

	// -- Exp10 (end)

	CView::OnMouseMove(nFlags, point);
}

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

	// -- Exp10 (start)

	// Check to see mouse has been captured
	if(GetCapture() == this) // if what has been captured is the obj. that the "this" ptr. is pointing to.
	{
		// Add the connection to the connection_list
		GetDocument()->AddConnection(m_ptOrigin, point);

		// Make the document redraw itself.
		Invalidate(TRUE);

		// Release the capture so other apps. can have access to the mouse.
		ReleaseCapture();
	}

	// -- Exp10 (end)

	CView::OnLButtonUp(nFlags, point);
}
