// Exp10Doc.cpp : implementation of the CExp10Doc class
//

#include "stdafx.h"
#include "Exp10.h"
// -- Exp10 (start)
#include <list>
#include "Connection.h"
using namespace std;
// -- Exp10 (end)
#include "Exp10Doc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExp10Doc

IMPLEMENT_DYNCREATE(CExp10Doc, CDocument)

BEGIN_MESSAGE_MAP(CExp10Doc, CDocument)
	//{{AFX_MSG_MAP(CExp10Doc)
		// 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
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExp10Doc construction/destruction

CExp10Doc::CExp10Doc()
{
	// TODO: add one-time construction code here

}

CExp10Doc::~CExp10Doc()
{
	// DiagramEng (start)
	list<CConnection*>::iterator it_con;	// local iterator
	
	// Iterate through the list
	for(it_con = connection_list.begin(); it_con != connection_list.end(); it_con++)
	{
		delete (*it_con);	// delete what it_con is pointing to: i.e. deref the it_con ptr and delete the ptr-to-CConnection
	}
	connection_list.clear();

	// DiagramEng (end)
}

BOOL CExp10Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CExp10Doc serialization

void CExp10Doc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CExp10Doc diagnostics

#ifdef _DEBUG
void CExp10Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CExp10Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExp10Doc commands

void CExp10Doc::AddConnection(CPoint ptFrom, CPoint ptTo)
{
	// Init.
	CString sMsg;		// main msg string
	UINT nType = MB_OK;	// style of msg. box
	UINT nIDhelp = 0;	// help context ID for the msg: 0 indicates the app's default help context will be used

	// Print msg.
	sMsg.Format("\n CExp10Doc::AddConnection(), from (%d,%d) to (%d,%d)\n", ptFrom.x, ptFrom.y, ptTo.x, ptTo.y);
	//AfxMessageBox(sMsg, nType, nIDhelp);

	// Create a new CConnection obj.
	CConnection *pCon = new CConnection(ptFrom, ptTo);

	// Add the new connection to the connection_list
	connection_list.push_back(pCon);

	// Mark the document as being modified
	SetModifiedFlag();
}

int CExp10Doc::GetConnectionCount()
{
	// Return the no. of connections in the list.
	return connection_list.size();
}


list<CConnection*>& CExp10Doc::GetConnectionList()
{
	return connection_list;
}

void CExp10Doc::DrawConnections(CDC *pDC)
{
	// -- Exp10 (start)
	// Init.
	CString sMsg;		// main msg string
	UINT nType = MB_OK;	// style of msg. box
	UINT nIDhelp = 0;	// help context ID for the msg: 0 indicates the app's default help context will be used
	list<CConnection*>::iterator it_con;	// local iterator
	list<CConnection*> con_list;			// local connection list

	// -- Print msg.
	sMsg.Format("\n CExp10Doc::DrawConnections()\n");
	//AfxMessageBox(sMsg, nType, nIDhelp);

	// Get the connection list
	con_list = GetConnectionList();

	// Iterate through the list
	for(it_con = con_list.begin(); it_con != con_list.end(); it_con++)
	{
		(*it_con)->DrawConnection(pDC);		// DrawConnection() called on the ptr-to-CConnection
	}

	// -- Exp10 (end)
}
