www.pudn.com > diblook > DibDoc.cpp


// DibDoc.cpp : implementation of the CDibDoc class 
// 
 
#include "stdafx.h" 
#include "DibLookEx.h" 
 
#include "Dib.h" 
#include "DibDoc.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDibDoc 
 
IMPLEMENT_DYNCREATE(CDibDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CDibDoc, CDocument) 
	//{{AFX_MSG_MAP(CDibDoc) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDibDoc construction/destruction 
 
CDibDoc::CDibDoc() 
{ 
	// TODO: add one-time construction code here 
 
} 
 
CDibDoc::~CDibDoc() 
{ 
} 
 
BOOL CDibDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
	 
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDibDoc diagnostics 
 
#ifdef _DEBUG 
void CDibDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CDibDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CDibDoc commands 
 
BOOL CDibDoc::OnOpenDocument(LPCTSTR lpszPathName)  
{ 
	if (!CDocument::OnOpenDocument(lpszPathName)) 
		return FALSE; 
	 
	CFile file; 
	CFileException fe; 
	if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe)) 
	{ 
		ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); 
		return FALSE; 
	} 
 
	DeleteContents(); 
	BeginWaitCursor(); 
 
	// replace calls to Serialize with ReadDIBFile function 
	TRY 
	{ 
		m_DIB.Read(file); 
	} 
	CATCH (CFileException, eLoad) 
	{ 
		file.Abort(); // will not throw an exception 
		EndWaitCursor(); 
		ReportSaveLoadException(lpszPathName, eLoad, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); 
		return FALSE; 
	} 
	END_CATCH 
 
	EndWaitCursor(); 
 
	if (!m_DIB.IsValid()) 
	{ 
		// may not be DIB format 
		CString strMsg; 
		strMsg.LoadString(IDS_CANNOT_LOAD_DIB); 
		MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK); 
		return FALSE; 
	} 
	SetPathName(lpszPathName); 
	SetModifiedFlag(FALSE);     // start off with unmodified 
	return TRUE; 
} 
 
BOOL CDibDoc::OnSaveDocument(LPCTSTR lpszPathName)  
{ 
	CFile file; 
	CFileException fe; 
 
	if (!file.Open(lpszPathName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive, &fe)) 
	{ 
		ReportSaveLoadException(lpszPathName, &fe, TRUE, AFX_IDP_INVALID_FILENAME); 
		return FALSE; 
	} 
 
	// replace calls to Serialize with SaveDIB function 
	BOOL bSuccess = FALSE; 
	TRY 
	{ 
		BeginWaitCursor(); 
		bSuccess = m_DIB.Save(file); 
		file.Close(); 
	} 
	CATCH (CException, eSave) 
	{ 
		file.Abort(); // will not throw an exception 
		EndWaitCursor(); 
		ReportSaveLoadException(lpszPathName, eSave, TRUE, AFX_IDP_FAILED_TO_SAVE_DOC); 
		return FALSE; 
	} 
	END_CATCH 
 
	EndWaitCursor(); 
	SetModifiedFlag(FALSE);     // back to unmodified 
 
	if (!bSuccess) 
	{ 
		// may be other-style DIB (load supported but not save) 
		//  or other problem in Save 
		CString strMsg; 
		strMsg.LoadString(IDS_CANNOT_SAVE_DIB); 
		MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK); 
	} 
 
	return TRUE; 
} 
 
DWORD CDibDoc::ReadDIBFromHandle(HGLOBAL hGlobal) 
{ 
	DWORD dwResult = m_DIB.ReadFromHandle(hGlobal);  
	SetModifiedFlag(TRUE); 
	UpdateAllViews(NULL); 
	return dwResult; 
}