⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexeditordoc.cpp

📁 VC开发环境下
💻 CPP
字号:
// IndexEditorDoc.cpp : implementation of the CIndexEditorDoc class
//

#include "stdafx.h"
#include "IndexEditor.h"

#include "IndexEditorDoc.h"
#include ".\indexeditordoc.h"
#include "SrvrItem.h"

#include <afximpl.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define DELETE_EXCEPTION(e) do { e->Delete(); } while (0)
// CIndexEditorDoc

IMPLEMENT_DYNCREATE(CIndexEditorDoc, COleServerDoc)

BEGIN_MESSAGE_MAP(CIndexEditorDoc, COleServerDoc)
END_MESSAGE_MAP()


// CIndexEditorDoc construction/destruction

CIndexEditorDoc::CIndexEditorDoc()
{
	// TODO: add one-time construction code here
	EnableCompoundFile();
}

CIndexEditorDoc::~CIndexEditorDoc()
{
}

COleServerItem* CIndexEditorDoc::OnGetEmbeddedItem()
{
	// OnGetEmbeddedItem 由框架调用以获取与该文档关联的 
	//COleServerItem。只在必要时才调用它。

	CIndexsSrvrItem* pItem = new CIndexsSrvrItem(this);
	ASSERT_VALID(pItem);
	return pItem;
}

BOOL CIndexEditorDoc::OnNewDocument()
{
	if (!COleServerDoc::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_cpIndexSystem.CreateInstance(CLSID_GHIndexs);
	//
	//IGHClassifyPtr cpClassify1;
	//IGHClassifyPtr cpClassify2;
	//IGHIndexPtr	   cpIndex;
	//IGHIndexPtr    cpIndex1;
	//IGHIndexPtr    cpIndex2;
	//HRESULT hr;
	//hr=cpClassify1.CreateInstance(CLSID_GHClassify);
	//hr=cpClassify1->put_Name(CComBSTR(L"松散体"));
	//hr=cpClassify1->put_Description(CComBSTR(L"黄土;松散堆积体:崩积物"));
	//hr=cpClassify1->put_Value(4);

	//hr=cpClassify2.CreateInstance(CLSID_GHClassify);
	//hr=cpClassify2->put_Name(CComBSTR(L"软弱岩体"));
	//hr=cpClassify2->put_Description(CComBSTR(L"含煤岩系煤层"));
	//hr=cpClassify2->put_Value(3);

	//hr=cpIndex.CreateInstance(CLSID_GHIndex);
	//hr=cpIndex->Add(cpClassify1);
	//hr=cpIndex->Add(cpClassify2);
	//hr=cpIndex->put_Name(CComBSTR(L"工程岩组"));

	//CComBSTR bstrName;
	//hr=cpIndex->get_Name(&bstrName);

	//hr=cpIndex1.CreateInstance(CLSID_GHIndex);
	//hr=cpIndex1->put_Name(CComBSTR(_T("岸坡结构")));

	//cpIndex2.CreateInstance(CLSID_GHIndex);
	//cpIndex2->put_Name(CComBSTR(_T("构造情况")));

	////hr=cpIndexs.CreateInstance(CLSID_GHIndexs);
	//hr=m_cpIndexSystem->Add(cpIndex);
	//m_cpIndexSystem->Add(cpIndex1);
	//m_cpIndexSystem->Add(cpIndex2);

	//hr=m_cpIndexSystem->put_Name(CComBSTR(L"三峡地质灾害危险性区划指标体系"));

	
	return TRUE;
}




// CIndexEditorDoc serialization

void CIndexEditorDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar<<1;
		CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
		if(FAILED(pApp->m_cpIndexsWorkspace->Save(m_lpRootStg,m_cpIndexSystem)))
		{
			AfxMessageBox(_T("保存文件错误"));
			return ;//FALSE;
		}
		
		m_lpRootStg->Commit(STGC_ONLYIFCURRENT);
		SetModifiedFlag(FALSE);
		UpdateTitle();		
	}
	else
	{
		int iTmp;
		ar>>iTmp;

		CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
		if(FAILED(pApp->m_cpIndexsWorkspace->Open(m_lpRootStg,&m_cpIndexSystem)))
		{
			AfxMessageBox(_T("打开文件错误"));
			return ;//FALSE;
		}
	}
}


// CIndexEditorDoc diagnostics

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

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


// CIndexEditorDoc commands

BOOL CIndexEditorDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	// TODO: Add your specialized code here and/or call the base class
	
	if(!COleDocument::OnSaveDocument(lpszPathName))
		return FALSE;

	//CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
	//if(FAILED(pApp->m_cpIndexsWorkspace->Save(m_lpRootStg,m_cpIndexSystem)))
	//{
	//	AfxMessageBox(_T("保存文件错误"));
	//	return FALSE;
	//}
	//
	//m_lpRootStg->Commit(STGC_ONLYIFCURRENT);
	//SetModifiedFlag(FALSE);
	//UpdateTitle();
	return TRUE;

	/*CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
	if(FAILED(pApp->m_cpIndexsWorkspace->SaveAs(CComBSTR(lpszPathName),m_cpIndexSystem)))
		return FALSE;

	SetModifiedFlag(FALSE);
	UpdateTitle();
	return TRUE;*/
}

BOOL CIndexEditorDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if(!COleDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	//CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
	//if(FAILED(pApp->m_cpIndexsWorkspace->Open(m_lpRootStg,&m_cpIndexSystem)))
	//{
	//	AfxMessageBox(_T("打开文件错误"));
	//	return FALSE;
	//}

	/*CIndexEditorApp* pApp=(CIndexEditorApp*)AfxGetApp();
	if(FAILED(pApp->m_cpIndexsWorkspace->Open(CComBSTR(lpszPathName),&m_cpIndexSystem)))
	{
		AfxMessageBox(_T("打开文件错误"));
		return FALSE;
	}*/
	

	return TRUE;
}

BOOL CIndexEditorDoc::FindIndexByName(const CString& strName,IGHIndex** pIndex)
{
	ASSERT(pIndex);

    if(pIndex==NULL)
		return FALSE;
	
	LONG lCount;
	m_cpIndexSystem->get_Count(&lCount);

	CComBSTR	 bstrName;
	IGHIndexPtr  cpIndex;
	for (LONG i=0;i<lCount;i++)
	{
		bstrName.Empty();
		cpIndex=0;

		m_cpIndexSystem->get_Item(i,&cpIndex);
		assert(cpIndex!=NULL);

		cpIndex->get_Name(&bstrName);
		if (strName==bstrName)
		{
			*pIndex=cpIndex.Detach();
			return TRUE;
		}
	}

	return FALSE;
}

BOOL CIndexEditorDoc::FindClassifyByName(const CString& strName,IGHIndex* pIndex,IGHClassify** pClassify)
{
	ASSERT(pIndex);
	ASSERT(pClassify);
	
	LONG lCount;
	pIndex->get_Count(&lCount);

	CComBSTR		bstrName;
	IGHClassifyPtr  cpClassify;
	for(LONG i=0;i<lCount;i++)
	{
		bstrName.Empty();
		cpClassify=0;

		pIndex->get_Item(i,&cpClassify);
		ASSERT(pClassify);

		cpClassify->get_Name(&bstrName);
		if (strName==bstrName) 
		{
			*pClassify=cpClassify.Detach();
			return TRUE;
		}
	}

	return FALSE;

}

void CIndexEditorDoc::UpdateTitle()
{
	if (IsModified())
	{
		if (m_strTitle.IsEmpty() ||  m_strTitle.GetAt(0)!='*') 
			m_strTitle.Insert(0,'*');
	}
	else
	{
		if ((!m_strTitle.IsEmpty())&& m_strTitle.GetAt(0)=='*') 
			m_strTitle.Delete(0);
	}

	UpdateFrameCounts();
}

BOOL CIndexEditorDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
// Save the document data to a file
// lpszPathName = path name where to save document file
// if lpszPathName is NULL then the user will be prompted (SaveAs)
// note: lpszPathName can be different than 'm_strPathName'
// if 'bReplace' is TRUE will change file name if successful (SaveAs)
// if 'bReplace' is FALSE will not change path name (SaveCopyAs)
{
	CString newName = lpszPathName;
	if (newName.IsEmpty())
	{
		CDocTemplate* pTemplate = GetDocTemplate();
		ASSERT(pTemplate != NULL);

		newName = m_strPathName;
		if (bReplace && newName.IsEmpty())
		{
			// get name/title from caption
			// if name has '*' , delete it
			if ((!m_strTitle.IsEmpty())&& m_strTitle.GetAt(0)=='*')
				newName=m_strTitle.Right(m_strTitle.GetLength()-1);
			else
                newName = m_strTitle;
			// check for dubious filename
			int iBad = newName.FindOneOf(_T(":/\\"));
			if (iBad != -1)
				newName.ReleaseBuffer(iBad);

			// append the default suffix if there is one
			CString strExt;
			if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) &&
				!strExt.IsEmpty())
			{
				ASSERT(strExt[0] == '.');
				int iStart = 0;
				newName += strExt.Tokenize(_T(";"), iStart);
			}
		}

		if (!AfxGetApp()->DoPromptFileName(newName,
			bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY,
			OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, pTemplate))
			return FALSE;       // don't even attempt to save
	}

	CWaitCursor wait;

	if (!OnSaveDocument(newName))
	{
		if (lpszPathName == NULL)
		{
			// be sure to delete the file
			TRY
			{
				CFile::Remove(newName);
			}
			CATCH_ALL(e)
			{
				TRACE(traceAppMsg, 0, "Warning: failed to delete file after failed SaveAs.\n");
				DELETE_EXCEPTION(e);
			}
			END_CATCH_ALL
		}
		return FALSE;
	}

	// reset the title and change the document name
	if (bReplace)
		SetPathName(newName);

	return TRUE;        // success
}

BOOL CIndexEditorDoc::SaveModified()
{
	if (!IsModified())
		return TRUE;        // ok to continue

	// get name/title of document
	CString name;
	if (m_strPathName.IsEmpty())
	{
		// get name based on caption
		// if caption has '*' delete it--matthew
		if ((!m_strTitle.IsEmpty())&& m_strTitle.GetAt(0)=='*')
			name=m_strTitle.Right(m_strTitle.GetLength()-1);
		else
			name = m_strTitle;
		if (name.IsEmpty())
			VERIFY(name.LoadString(AFX_IDS_UNTITLED));
	}
	else
	{
		// get name based on file title of path name
		name = m_strPathName;
		AfxGetFileTitle(m_strPathName, name.GetBuffer(_MAX_PATH), _MAX_PATH);
		name.ReleaseBuffer();
	}

	CString prompt;
	AfxFormatString1(prompt, AFX_IDP_ASK_TO_SAVE, name);
	switch (AfxMessageBox(prompt, MB_YESNOCANCEL, AFX_IDP_ASK_TO_SAVE))
	{
	case IDCANCEL:
		return FALSE;       // don't continue

	case IDYES:
		// If so, either Save or Update, as appropriate
		if (!DoFileSave())
			return FALSE;       // don't continue
		break;

	case IDNO:
		// If not saving changes, revert the document
		break;

	default:
		ASSERT(FALSE);
		break;
	}
	return TRUE;    // keep going
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -