// DrawDoc.cpp : CDrawDoc 类的实现
//

#include "stdafx.h"
#include "Draw.h"

#include "DrawDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDrawDoc

IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)

BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)
	ON_COMMAND(ID_FILE_SAVE, &CDrawDoc::OnFileSave)
//	ON_COMMAND(ID_FILE_OPEN, &CDrawDoc::OnFileOpen)
END_MESSAGE_MAP()


// CDrawDoc 构造/析构

CDrawDoc::CDrawDoc()
{
	// TODO: 在此添加一次性构造代码
	m_DrawType = DRAW_NULL;
	m_arPt.RemoveAll();
	m_pMap  = NULL;
	m_pMap  = new C2DMap;
}

CDrawDoc::~CDrawDoc()
{
	if (m_pMap!=NULL)
	{
		delete m_pMap;
		m_pMap = NULL;
	}
}

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

	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)
	if (m_pMap != NULL)
	{
		delete m_pMap;
		m_pMap = NULL;
	}

	m_pMap = new C2DMap;
	m_arPt.RemoveAll();
	m_arPt.FreeExtra();

	return TRUE;
}




// CDrawDoc 序列化

void CDrawDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
	}
	else
	{
		// TODO: 在此添加加载代码
	}
}


// CDrawDoc 诊断

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

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


// CDrawDoc 命令

void CDrawDoc::OnFileSave()
{
	// TODO: 在此添加命令处理程序代码
	CString FilePathName = GetPathName();
	if (FilePathName == _T(""))
	{
		CFileDialog fileDlg(FALSE,NULL,NULL);
		fileDlg.m_ofn.lpstrFilter = _T("Draw2D Files (*.2d)\0*.2d\0All Files (*.*)\0*.*\0");
        fileDlg.m_ofn.lpstrTitle = _T("保存文件");
		if (fileDlg.DoModal() == IDOK)
		{
			CString szFile = fileDlg.GetPathName();
			if (szFile.ReverseFind('.') == -1)
			{
				szFile += _T(".2d");
			}
			this->OnSaveDocument(szFile);
		}
	}
	else
	{
		this->OnSaveDocument(FilePathName);
	}
}

BOOL CDrawDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	// TODO: 在此添加专用代码和/或调用基类
    this->SetModifiedFlag(FALSE);
	return m_pMap->Save(lpszPathName);
	//return CDocument::OnSaveDocument(lpszPathName);
}

BOOL CDrawDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// TODO:  在此添加您专用的创建代码
	CFileStatus status;
	if( CFile::GetStatus(lpszPathName, status))   // static function
	{
#ifdef _DEBUG
		afxDump << "Full file name = " << status.m_szFullName << "\n";
#endif
		if(status.m_attribute & 0x01)
		{
			if(MessageBox(NULL, _T("该文件是只读属性, 确实要打开吗?"), _T("打开文件"), 
				MB_OKCANCEL | MB_ICONWARNING)!= IDOK)
			{
				return FALSE;
			}
		}
	}

	m_pMap->Open(lpszPathName);
	m_arPt.RemoveAll();
	m_arPt.FreeExtra();

	return TRUE;
}
