Esempio n. 1
0
BOOL CDibDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	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_hDIB = ::ReadDIBFile(file);
	}
	CATCH (CFileException, eLoad)
	{
		file.Abort(); // will not throw an exception
		EndWaitCursor();
		ReportSaveLoadException(lpszPathName, eLoad,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		m_hDIB = NULL;
		return FALSE;
	}
//----------------------------------------------------------------------------
BOOL CDockingPaletteDoc::OnOpenDocument(LPCTSTR lpszPathName,LPCTSTR lpszPaneName){
//----------------------------------------------------------------------------
	PROC_TRACE;


   if (IsModified())
		TRACE("Warning: OnOpenDocument replaces an unsaved document.\n");

	CFileException fe;
	CFile* pFile = GetFile(lpszPathName,
		CFile::modeRead|CFile::shareDenyWrite, &fe);
	if (pFile == NULL)
	{
		ReportSaveLoadException(lpszPathName, &fe,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		return FALSE;
	}

	DeleteContents();
	SetModifiedFlag();  

	CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
	loadArchive.m_pDocument = this;
	loadArchive.m_bForceFlat = FALSE;
	TRY
	{
		CWaitCursor wait;
		if (pFile->GetLength() != 0)
			Serialize(loadArchive);     // load me
		loadArchive.Close();
		ReleaseFile(pFile, FALSE);
	}
	CATCH_ALL(e)
	{
		ReleaseFile(pFile, TRUE);
		DeleteContents();   // remove failed contents

		TRY
		{
			ReportSaveLoadException(lpszPathName, e,
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		}
		END_TRY
		return FALSE;
	}
	END_CATCH_ALL

	SetModifiedFlag(FALSE);     // start off with unmodified
	m_documentName = lpszPaneName;
	return TRUE;
}
Esempio n. 3
0
BOOL COleLinkingDoc::RegisterIfServerAttached(LPCTSTR lpszPathName, BOOL bMessage)
{
	ASSERT_VALID(this);
	ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName));

	CDocTemplate* pTemplate = GetDocTemplate();
	ASSERT_VALID(pTemplate);

	COleObjectFactory* pFactory =
		(COleObjectFactory*)pTemplate->m_pAttachedFactory;
	if (pFactory != NULL)
	{
		// always attach the document to the server at this time
		ASSERT_KINDOF(COleObjectFactory, pFactory);
		m_pFactory = pFactory;

		// register with OLE Server
		if (!Register(pFactory, lpszPathName))
		{
			if (bMessage)
			{
				// only report error when message box allowed
				ReportSaveLoadException(lpszPathName, NULL, FALSE,
					AFX_IDP_FAILED_TO_NOTIFY);
			}
			return FALSE;
		}
	}
	return TRUE;
}
Esempio n. 4
0
BOOL CSoliDireDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	CFileException fe;
	CFile* pFile = NULL;
	
	DWORD nFlags = CFile::modeReadWrite | CFile::shareExclusive;
	if (!is_packed())
		nFlags |= CFile::modeCreate;

	pFile = GetFile(lpszPathName, nFlags, &fe);

	if (pFile == NULL)
	{
		ReportSaveLoadException(lpszPathName, &fe,
			TRUE, AFX_IDP_INVALID_FILENAME);
		return FALSE;
	}

	CArchive saveArchive(pFile, CArchive::store | CArchive::bNoFlushOnDelete);
	saveArchive.m_pDocument = this;
	saveArchive.m_bForceFlat = FALSE;

	CWaitCursor wait;
	Serialize(saveArchive);     // save me
	saveArchive.Close();
	ReleaseFile(pFile, FALSE);

	SetModifiedFlag(FALSE);     // back to unmodified

	return TRUE;        // success
}
Esempio n. 5
0
BOOL CDocument::OnOpenDocument( LPCTSTR lpszPathName )
/****************************************************/
{
    CFileException ex;
    CFile *pFile = GetFile( lpszPathName, CFile::modeRead, &ex );
    if( pFile == NULL ) {
        ReportSaveLoadException( lpszPathName, &ex, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC );
        return( FALSE );
    }
    try {
        DeleteContents();
        CArchive ar( pFile, CArchive::load );
        Serialize( ar );
        ar.Close();
        ReleaseFile( pFile, FALSE );
    } catch( CException *pEx ) {
        ReportSaveLoadException( lpszPathName, pEx, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC );
        ReleaseFile( pFile, TRUE );
        return( FALSE );
    }
    SetModifiedFlag( FALSE );
    return( TRUE );
}
Esempio n. 6
0
BOOL CDocument::OnSaveDocument( LPCTSTR lpszPathName )
/****************************************************/
{
    CFileException ex;
    CFile *pFile = GetFile( lpszPathName, CFile::modeCreate | CFile::modeWrite, &ex );
    if( pFile == NULL ) {
        ReportSaveLoadException( lpszPathName, &ex, TRUE, AFX_IDP_FAILED_TO_SAVE_DOC );
        return( FALSE );
    }
    try {
        CArchive ar( pFile, CArchive::store );
        Serialize( ar );
        ar.Close();
        SetPathName( lpszPathName );
        SetTitle( pFile->GetFileTitle() );
        ReleaseFile( pFile, FALSE );
    } catch( CException *pEx ) {
        ReportSaveLoadException( lpszPathName, pEx, TRUE, AFX_IDP_FAILED_TO_SAVE_DOC );
        ReleaseFile( pFile, TRUE );
        return( FALSE );
    }
    SetModifiedFlag( FALSE );
    return( TRUE );
}
Esempio n. 7
0
BOOL CWordPadDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (m_lpRootStg != NULL) // we are embedded
	{
		// we really want to use the converter on this storage
		m_nNewDocType = RD_EMBEDDED;
	}
	else
	{
		if (theApp.cmdInfo.m_bForceTextMode)
			m_nNewDocType = RD_TEXT;
		else
		{
			CFileException fe;
			m_nNewDocType = GetDocTypeFromName(lpszPathName, fe);
			if (m_nNewDocType == -1)
			{
				ReportSaveLoadException(lpszPathName, &fe, FALSE,
					AFX_IDP_FAILED_TO_OPEN_DOC);
				return FALSE;
			}
			if (m_nNewDocType == RD_TEXT && theApp.m_bForceOEM)
				m_nNewDocType = RD_OEMTEXT;
		}
		ScanForConverters();
		if (!doctypes[m_nNewDocType].bRead)
		{
			CString str;
			CString strName = doctypes[m_nNewDocType].GetString(DOCTYPE_DOCTYPE);
			AfxFormatString1(str, IDS_CANT_LOAD, strName);
			AfxMessageBox(str, MB_OK|MB_ICONINFORMATION);
			return FALSE;
		}
	}

//  SetDocType(nNewDocType);
	if (!CRichEditDoc::OnOpenDocument(lpszPathName))
		return FALSE;
	return TRUE;
}
Esempio n. 8
0
/******************************************************************************************
BOOL CIntChessDoc::OnOpenDocument(LPCTSTR lpszPathName)
作者      : tangjs520       创建日期: 2004-9-29
函数名    : CIntChessDoc::OnOpenDocument
返回值    : BOOL
参数列表  : 
  参数1: LPCTSTR lpszPathName
描述      : 
调用关系  : 
被调用关系: 
备注      : 
修改记录  : 
******************************************************************************************/
BOOL CIntChessDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
    // TODO: Add your specialized creation code here
    if (IsModified())
    {
        TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n");
    }

    BOOL bRet = TRUE;

    CFileException fe;
    CFile* pFile = GetFile(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe);
    if (NULL == pFile)
    {
        ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
        return FALSE;
    }

    DeleteContents();
    SetModifiedFlag();  // dirty during de-serialize

    CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
    loadArchive.m_pDocument = this;
    loadArchive.m_bForceFlat = FALSE;
    try
    {
        CWaitCursor wait;
        if (pFile->GetLength() != 0)
        {
            bRet = CChessRule::Open(loadArchive);   // load me
        }
        loadArchive.Close();
        ReleaseFile(pFile, FALSE);
    }
    catch (CException* e)
    {
        //读取棋局文件发生异常,应将用户模式还原为默认模式,即手动模式
        //否则用户将无法正常关闭应用程序
        CChessRule::SetUserMode(MANUALMODE);
        SetModifiedFlag(TRUE);

        ASSERT(e->IsKindOf(RUNTIME_CLASS(CException)));
        UNUSED(e);

        ReleaseFile(pFile, TRUE);
        DeleteContents();   // remove failed contents

        try
        {
            ReportSaveLoadException(lpszPathName, e, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
        }
        catch (CException* e)
        {
            ASSERT(e->IsKindOf(RUNTIME_CLASS(CException)));
            e->Delete();
        }

        e->Delete();

        return FALSE;
    }

    SetModifiedFlag(!bRet);     // start off with unmodified

    return bRet;
}
Esempio n. 9
0
BOOL COleDocument::OnSaveDocument(LPCTSTR lpszPathName)
	// lpszPathName must be fully qualified
{
	ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName));

	// use default implementation if 'docfile' not enabled
	if (!m_bCompoundFile && m_lpRootStg == NULL)
	{
		ASSERT(lpszPathName != NULL);
		return CDocument::OnSaveDocument(lpszPathName);
	}

	LPSTORAGE lpOrigStg = NULL;
	if (lpszPathName != NULL)
		m_bSameAsLoad = AfxComparePath(m_strPathName, lpszPathName);

	BOOL bResult = FALSE;
	TRY
	{
		// open new root storage if necessary
		if (lpszPathName != NULL && !m_bSameAsLoad)
		{
			// temporarily detach current storage
			lpOrigStg = m_lpRootStg;
			m_lpRootStg = NULL;

			LPSTORAGE lpStorage;
			const CStringW strPathName(lpszPathName);
			SCODE sc = ::StgCreateDocfile(strPathName.GetString(),
				STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_CREATE,
				0, &lpStorage);
			if (sc != S_OK)
				AfxThrowOleException(sc);

			ASSERT(lpStorage != NULL);
			m_lpRootStg = lpStorage;
		}
		ASSERT(m_lpRootStg != NULL);

		// use helper to save to root storage
		SaveToStorage();

		if (lpszPathName != NULL)
		{
			// commit each of the items
			CommitItems(m_bRemember && !m_bSameAsLoad);

			// mark document as clean if remembering the storage
			if (m_bRemember)
				SetModifiedFlag(FALSE);

			// remember correct storage or release save copy as storage
			if (!m_bSameAsLoad)
			{
				if (m_bRemember)
				{
					// Save As case -- m_stgRoot is new storage, forget old storage
					lpOrigStg->Release();
				}
				else
				{
					// Save Copy As case -- m_stgRoot should hook up to m_stgOrig.
					m_lpRootStg->Release();
					m_lpRootStg = lpOrigStg;
				}
			}
		}

		bResult = TRUE;
	}
	CATCH_ALL(e)
	{
		if (lpOrigStg != NULL)
		{
			// save as failed: abort new storage, and re-attach original
			RELEASE(m_lpRootStg);
			m_lpRootStg = lpOrigStg;
		}

		if (lpszPathName == NULL)
		{
			THROW_LAST();
		}

		TRY
		{
			ReportSaveLoadException(lpszPathName, e,
				TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
		}
		END_TRY
		DELETE_EXCEPTION(e);
	}
	END_CATCH_ALL

	// cleanup
	m_bSameAsLoad = TRUE;
	m_bRemember = TRUE;

	return bResult;
}
Esempio n. 10
0
BOOL COleDocument::OnOpenDocument(LPCTSTR lpszPathName)
{
	ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName));

	// just use default implementation if 'docfile' not enabled
	if (!m_bCompoundFile && m_lpRootStg == NULL)
	{
		ASSERT(lpszPathName != NULL);
		return CDocument::OnOpenDocument(lpszPathName);
	}

	if (IsModified())
		TRACE(traceOle, 0, "Warning: OnOpenDocument replaces an unsaved document.\n");

	// abort changes to current docfile
	if (lpszPathName != NULL)
	{
		DeleteContents();
		RELEASE(m_lpRootStg);
	}
	SetModifiedFlag();  // dirty during de-serialize

	BOOL bResult = FALSE;
	TRY
	{
		if (m_lpRootStg == NULL)
		{
			const CStringW strPathName(lpszPathName);
			LPCOLESTR lpsz = lpszPathName ? strPathName.GetString() : NULL;

			// use STGM_CONVERT if necessary
			SCODE sc;
			LPSTORAGE lpStorage = NULL;
			if (StgIsStorageFile(lpsz) == S_FALSE)
			{
				// convert existing storage file
				sc = StgCreateDocfile(lpsz, STGM_READWRITE|
					STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_CONVERT,
					0, &lpStorage);
			}
			else
			{
				// open new storage file
				sc = StgOpenStorage(lpsz, NULL,
					STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_DENY_WRITE,
					0, 0, &lpStorage);
				if (FAILED(sc) || lpStorage == NULL)
					sc = StgOpenStorage(lpsz, NULL,
						STGM_READ|STGM_TRANSACTED, 0, 0, &lpStorage);
			}
			if (FAILED(sc))
				AfxThrowOleException(sc);

			ASSERT(lpStorage != NULL);
			m_lpRootStg = lpStorage;
		}

		// use helper to read document from storage
		LoadFromStorage();

		SetModifiedFlag(FALSE); // start off with unmodified
		bResult = TRUE;
	}
	CATCH_ALL(e)
	{
		DeleteContents();   // removed failed contents
		RELEASE(m_lpRootStg);

		// if not file-based load, return exceptions to the caller
		if (lpszPathName == NULL)
		{
			THROW_LAST();
		}

		TRY
		{
			ReportSaveLoadException(lpszPathName, e,
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		}
		END_TRY
		DELETE_EXCEPTION(e);
	}
	END_CATCH_ALL

	return bResult;
}