📄 kyofficedoc.cpp
字号:
// KyOfficeDoc.cpp : implementation of the CKyOfficeDoc class
//
#include "stdafx.h"
#include "KyOffice.h"
#include "KyOfficeDoc.h"
#include "KyOfficeView.h"
#include "CntrItem.h"
#include "comutil.h"
#include "KyOfficeCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DELETE_EXCEPTION(e) do { e->Delete(); } while (0)
/////////////////////////////////////////////////////////////////////////////
// CKyOfficeDoc
CKyDocInfoArray CKyOfficeDoc::m_arrayDocInfo;
IMPLEMENT_DYNCREATE(CKyOfficeDoc, COleDocument)
BEGIN_MESSAGE_MAP(CKyOfficeDoc, COleDocument)
//{{AFX_MSG_MAP(CKyOfficeDoc)
//}}AFX_MSG_MAP
// Enable default OLE container implementation
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
ON_COMMAND(ID_OLE_EDIT_CONVERT, COleDocument::OnEditConvert)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, COleDocument::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKyOfficeDoc construction/destruction
CKyOfficeDoc::CKyOfficeDoc()
{
// Use OLE compound files
EnableCompoundFile();
// TODO: add one-time construction code here
m_pOfficeCtrl = NULL;
}
CKyOfficeDoc::~CKyOfficeDoc()
{
}
BOOL CKyOfficeDoc::OnNewDocument()
{
if (!COleDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CKyOfficeDoc serialization
void CKyOfficeDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
// Calling the base class COleDocument enables serialization
// of the container document's COleClientItem objects.
COleDocument::Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CKyOfficeDoc diagnostics
#ifdef _DEBUG
void CKyOfficeDoc::AssertValid() const
{
COleDocument::AssertValid();
}
void CKyOfficeDoc::Dump(CDumpContext& dc) const
{
COleDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CKyOfficeDoc commands
BOOL CKyOfficeDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
//if (!COleDocument::OnOpenDocument(lpszPathName))
// return FALSE;
// TODO: Add your specialized creation code here
CKyOfficeView * pActiveView = (CKyOfficeView*)m_viewList.GetHead();
ASSERT(pActiveView);
return pActiveView->DoOpenDocument(lpszPathName);
}
BOOL CKyOfficeDoc::SaveModified()
{
// TODO: Add your specialized code here and/or call the base class
if (::InSendMessage())
{
POSITION pos = GetStartPosition();
COleClientItem* pItem;
while ((pItem = GetNextClientItem(pos)) != NULL)
{
ASSERT(pItem->m_lpObject != NULL);
SCODE sc = pItem->m_lpObject->IsUpToDate();
if (sc != OLE_E_NOTRUNNING && FAILED(sc))
{
// inside inter-app SendMessage limits the user's choices
CString name = m_strPathName;
if (name.IsEmpty())
VERIFY(name.LoadString(AFX_IDS_UNTITLED));
CString prompt;
AfxFormatString1(prompt, AFX_IDP_ASK_TO_DISCARD, name);
return AfxMessageBox(prompt, MB_OKCANCEL|MB_DEFBUTTON2,
AFX_IDP_ASK_TO_DISCARD) == IDOK;
}
}
}
// sometimes items change without a notification, so we have to
// update the document's modified flag before calling
// CDocument::SaveModified.
UpdateModifiedFlag();
////////////////////////////////////
//return CDocument::SaveModified();
////////////////////////////////////
if (!IsModified())
return TRUE; // ok to continue
// get name/title of document
CString name;
if (m_strPathName.IsEmpty())
{
// get name based on caption
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;
//if (afxData.bMarked4)
{
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
}
CKyOfficeCntrItem * CKyOfficeDoc::GetDocItem()
{
POSITION pos=GetStartPosition();
if(pos)
{
CDocItem* pItem = GetNextItem(pos);
if(pItem->IsKindOf(RUNTIME_CLASS(CKyOfficeCntrItem)))
return (CKyOfficeCntrItem*)pItem;
}
return NULL;
}
CKyOfficeView * CKyOfficeDoc::GetActiveView()
{
POSITION pos = m_viewList.GetHeadPosition();
if(pos)
{
CKyOfficeView * pActiveView = (CKyOfficeView*)m_viewList.GetNext(pos);
return pActiveView;
}
return NULL;
}
BOOL CKyOfficeDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName));
ASSERT(m_lpRootStg != NULL);
USES_CONVERSION;
BSTR bstrPathName = A2W(lpszPathName);
IStorage * pStorage = NULL;
BOOL bSuccess = FALSE;
if(SUCCEEDED(StgCreateDocfile(bstrPathName, STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE, 0, &pStorage)))
{
LPPERSISTSTORAGE lpPersistStorage = NULL;
if(SUCCEEDED(GetDocItem()->m_lpObject->QueryInterface(IID_IPersistStorage, (void**)&lpPersistStorage)))
{
if(SUCCEEDED(OleSave(lpPersistStorage, pStorage, FALSE)))
{
VERIFY(lpPersistStorage->SaveCompleted(NULL) == S_OK);
bSuccess = TRUE;
}
lpPersistStorage->Release();
}
pStorage->Release();
}
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -