// MFCDoc.cpp : CMFCDoc 클래스의 구현
//

#include "stdafx.h"
#include "paint.h"
#include "paintDoc.h"
#include "Data.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCDoc

IMPLEMENT_DYNCREATE(CpaintDoc, CDocument)

BEGIN_MESSAGE_MAP(CpaintDoc, CDocument)
END_MESSAGE_MAP()


// CMFCDoc 생성/소멸

CpaintDoc::CpaintDoc()
{
	CData* mainList = new CData();
	m_CurDraw = NULL;

}

CpaintDoc::~CpaintDoc()
{

	CData* obj;
	POSITION pos = m_DrawObjs.GetHeadPosition();
	while (pos != NULL) 
	{
		obj = (CData*)m_DrawObjs.GetNext(pos);
	}
	m_DrawObjs.RemoveAll();
	

}

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

	// TODO: 여기에 재초기화 코드를 추가합니다.
	// SDI 문서는 이 문서를 다시 사용합니다.
	CData* obj;
	POSITION pos = m_DrawObjs.GetHeadPosition();
	while (pos != NULL) 
	{
		obj = (CData*)m_DrawObjs.GetNext(pos);
		delete obj;
	}
	m_DrawObjs.RemoveAll();

	return TRUE;
}

void CpaintDoc::CreateObj(int nChoice)
{
	switch(nChoice)
	{
	case ELLIPSE:
		{	
			m_CurDraw = new CDrawElli();
			m_DrawObjs.AddTail(m_CurDraw);
			return;
			delete m_CurDraw;
		}break;
	case RECTANGLE:
		{
			m_CurDraw = new CDrawRect();
			m_DrawObjs.AddTail(m_CurDraw);
			return;
			delete m_CurDraw;
		}break;
	case LINE:

		{
			m_CurDraw = new CDrawLine();
			m_DrawObjs.AddTail(m_CurDraw);
			return;
			delete m_CurDraw;
		}break;

	case POLYLINE:
		{
			m_CurDraw = new CDrawPoly();
			m_DrawObjs.AddTail(m_CurDraw);
			return;
			delete m_CurDraw;
		}break;

	}
	
}

//========================================================================================//
// CMFCDoc serialization
void CpaintDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring()) 
	{	
		POSITION pos = m_DrawObjs.GetHeadPosition();
		while (pos != NULL) 
		{
			((CData*)m_DrawObjs.GetNext(pos))->Serialize(ar);
		}	
	} 
	else 
	{
		int nSelect;
		CData *obj;

		do 
		{
			ar >> nSelect;

			switch (nSelect) 
			{
			
			case LINE:
				obj = new CDrawLine(); 
				break;
			case RECTANGLE:
				obj = new CDrawRect(); 
				break;
			case ELLIPSE:
				obj = new CDrawElli(); 
				break;
			case POLYLINE:
				obj = new CDrawPoly(); 
				break;

			}

			obj->Deserialize(ar);
			m_DrawObjs.AddTail(obj);		
		} 
		while (!ar.IsBufferEmpty());

		CView* pViewTmp;
		pViewTmp = ((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
		if(pViewTmp != NULL)
			pViewTmp->Invalidate();
		delete obj;
	}
}

//========================================================================================//
// CMFCDoc 진단

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

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

//========================================================================================//
