
// MarkdownEditorDoc.cpp : CMarkdownEditorDoc 类的实现
//

#include "stdafx.h"
#include "./Util.h"
#include <memory>
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MarkdownEditor.h"
#endif

#include "MarkdownEditorDoc.h"

#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMarkdownEditorDoc

IMPLEMENT_DYNCREATE(CMarkdownEditorDoc, CDocument)

BEGIN_MESSAGE_MAP(CMarkdownEditorDoc, CDocument)

END_MESSAGE_MAP()


// CMarkdownEditorDoc 构造/析构

CMarkdownEditorDoc::CMarkdownEditorDoc()
{
	// TODO: 在此添加一次性构造代码
	resetData();
}

CMarkdownEditorDoc::~CMarkdownEditorDoc()
{
}

BOOL CMarkdownEditorDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	resetData();
	this->UpdateAllViews(NULL, LPARAM_Update);
	// TODO: 在此添加重新初始化代码
	// (SDI 文档将重用该文档)
	return TRUE;
}




// CMarkdownEditorDoc 序列化

void CMarkdownEditorDoc::Serialize(CArchive& ar)
{
	string strFile = ar.GetFile()->GetFilePath();
	_strPath = Util::GetFilePath(strFile,true);

	if (ar.IsStoring())
	{
		ar.WriteString(Util::ANSIToUTF8(_strText.c_str()).c_str());
		// TODO: 在此添加存储代码
	}
	else
	{
		
		_strText = Util::ReadStringFile(*ar.GetFile()).c_str();
		Util::ReplaceAllStr(_strText,"\r\n", "\n");
		Util::ReplaceAllStr(_strText,"\n", "\r\n");
		this->UpdateAllViews(NULL,LPARAM_Update);
		// TODO: 在此添加加载代码
	}
}

#ifdef SHARED_HANDLERS

// 缩略图的支持
void CMarkdownEditorDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// 修改此代码以绘制文档数据
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// 搜索处理程序的支持
void CMarkdownEditorDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// 从文档数据设置搜索内容。
	// 内容部分应由“;”分隔

	// 例如:  strSearchContent = _T("point;rectangle;circle;ole object;")；
	SetSearchContent(strSearchContent);
}

void CMarkdownEditorDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CMarkdownEditorDoc 诊断

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

void CMarkdownEditorDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG
const CString PREFIX_MODIFIED = "* ";
void setModified(CMarkdownEditorDoc*pDoc, bool modified) {
	pDoc->SetModifiedFlag(modified);
	//string path = pDoc->GetTitle();
	CString title = pDoc->GetTitle();
	if (modified) {
		CString strTitle;
		AfxGetMainWnd()->GetWindowText(strTitle);
		if (modified && strTitle.Find(PREFIX_MODIFIED) !=0)
			AfxGetMainWnd()->SetWindowText("* " + strTitle);
	}
}
// CMarkdownEditorDoc 命令
//更新文本内容
void CMarkdownEditorDoc::UpdateText(const string& text, CView* pSender, bool bMoveToEnd){
	_strText = text;
	int lParam = LPARAM_Update;
	if(bMoveToEnd)
		lParam |= LPARAM_MoveEnd;
	this->UpdateAllViews(pSender, lParam);
	setModified(this, true);
	//this->SetModifiedFlag();
}

void CMarkdownEditorDoc::resetData(void)
{
	_strText = "";
	_strPath = "";
}
