📄 document.cpp
字号:
| STGM_CONVERT | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
if (FAILED(hr))
{
//If denied write access, try to load the old way
if (STG_E_ACCESSDENIED==GetScode(hr))
m_lVer=m_pPL->ReadFromFile(pszFile);
else
return DOCERR_COULDNOTOPEN;
}
}
else
{
hr=StgOpenStorage(pszFile, NULL, STGM_DIRECT | STGM_READ
| STGM_SHARE_EXCLUSIVE, NULL, 0, &pIStorage);
if (FAILED(hr))
return DOCERR_COULDNOTOPEN;
}
if (NULL!=pIStorage)
{
m_lVer=m_pPL->ReadFromStorage(pIStorage);
pIStorage->Release();
}
if (POLYLINE_E_READFAILURE==m_lVer)
return DOCERR_READFAILURE;
if (POLYLINE_E_UNSUPPORTEDVERSION==m_lVer)
return DOCERR_UNSUPPORTEDVERSION;
if (fChangeFile)
Rename(pszFile);
//Importing a file makes things dirty
FDirtySet(!fChangeFile);
return DOCERR_NONE;
}
/*
* CCosmoDoc::Save
*
* Purpose:
* Writes the file to a known filename, requiring that the user has
* previously used FileOpen or FileSaveAs to provide a filename.
*
* Parameters:
* uType UINT indicating the type of file the user
* requested to save in the File Save As dialog.
* pszFile LPTSTR under which to save. If NULL, use the
* current name.
*
* Return Value:
* UINT An error value from DOCERR_*
*/
UINT CCosmoDoc::Save(UINT uType, LPTSTR pszFile)
{
LONG lVer, lRet;
UINT uTemp;
BOOL fRename=TRUE;
HRESULT hr;
LPSTORAGE pIStorage;
//CHAPTER18MOD
BOOL fEmbedding;
fEmbedding=m_pFigure->FIsEmbedded();
//End CHAPTER18MOD
if (NULL==pszFile)
{
fRename=FALSE;
pszFile=m_szFile;
}
/*
* Type 1 is the current version, type 2 is version 1.0 of the
* Polyline so we use this to send the right version to
* CPolyline::WriteToFile/WriteToStorage.
*/
switch (uType)
{
case 0: //From Save, use loaded version.
lVer=m_lVer;
break;
case 1:
lVer=VERSIONCURRENT;
break;
case 2:
lVer=MAKELONG(0, 1); //1.0
break;
default:
return DOCERR_UNSUPPORTEDVERSION;
}
/*
* If the version the user wants to save is different from the
* version that we loaded and m_lVer is not zero (new doc),
* then inform the user of the version change and verify.
*/
//CHAPTER18MOD
//For embedding, this is Save Copy As, so don't ask about versions.
if (0!=m_lVer && m_lVer!=lVer && !fEmbedding)
//End CHAPTER18MOD
{
TCHAR szMsg[128];
wsprintf(szMsg, PSZ(IDS_VERSIONCHANGE)
, (UINT)HIWORD(m_lVer), (UINT)LOWORD(m_lVer)
, (UINT)HIWORD(lVer), (UINT)LOWORD(lVer));
uTemp=MessageBox(m_hWnd, szMsg, PSZ(IDS_DOCUMENTCAPTION)
, MB_YESNOCANCEL);
if (IDCANCEL==uTemp)
return DOCERR_CANCELLED;
//If the user won't upgrade, revert to loaded version.
if (IDNO==uTemp)
lVer=m_lVer;
}
/*
* For 1.0 files, still use the old code. For new files, use
* storages instead
*/
if (lVer==MAKELONG(0, 1))
lRet=m_pPL->WriteToFile(pszFile, lVer);
else
{
hr=StgCreateDocfile(pszFile, STGM_DIRECT | STGM_READWRITE
| STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
if (FAILED(hr))
return DOCERR_COULDNOTOPEN;
//Mark this as one of our class
WriteClassStg(pIStorage, CLSID_CosmoFigure);
//Write user-readable class information
WriteFmtUserTypeStg(pIStorage, m_cf
, PSZ(IDS_CLIPBOARDFORMAT));
lRet=m_pPL->WriteToStorage(pIStorage, lVer);
pIStorage->Release();
}
if (POLYLINE_E_NONE!=lRet)
return DOCERR_WRITEFAILURE;
//CHAPTER18MOD
//Saving makes us clean, but this doesn't apply to embedding.
if (!fEmbedding)
FDirtySet(FALSE);
//End CHAPTER18MOD
//Update the known version of this document.
m_lVer=lVer;
//CHAPTER18MOD
/*
* If we're embedding, this is Save Copy As, so no rename.
* Note that we also don't care about having been set to clean
* since we're always 'clean' as an embedded object from
* the user's perspective.
*/
if (fRename && !fEmbedding)
Rename(pszFile);
//End CHAPTER18MOD
return DOCERR_NONE;
}
/*
* CCosmoDoc::Undo
*
* Purpose:
* Reverses a previous action.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void CCosmoDoc::Undo(void)
{
m_pPL->Undo();
return;
}
/*
* CCosmoDoc::Clip
*
* Purpose:
* Places a private format, a metafile, and a bitmap of the display
* on the clipboard, optionally implementing Cut by deleting the
* data in the current window after rendering.
*
* Parameters:
* hWndFrame HWND of the main window.
* fCut BOOL indicating cut (TRUE) or copy (FALSE).
*
* Return Value:
* BOOL TRUE if successful, FALSE otherwise.
*/
BOOL CCosmoDoc::Clip(HWND hWndFrame, BOOL fCut)
{
BOOL fRet=TRUE;
LPDATAOBJECT pIDataObject;
pIDataObject=TransferObjectCreate(fCut);
if (NULL==pIDataObject)
return FALSE;
fRet=SUCCEEDED(OleSetClipboard(pIDataObject));
pIDataObject->Release();
//Delete our current data if "cut" succeeded.
if (fRet && fCut)
{
m_pPL->New();
FDirtySet(TRUE);
}
return fRet;
}
/*
* CCosmoDoc::RenderFormat
*
* Purpose:
* Renders a specific clipboard format into global memory.
*
* Parameters:
* cf UINT format to render.
*
* Return Value:
* HGLOBAL Global memory handle containing the data.
*/
HGLOBAL CCosmoDoc::RenderFormat(UINT cf)
{
HGLOBAL hMem;
if (cf==m_cf)
{
m_pPL->DataGetMem(VERSIONCURRENT, &hMem);
return hMem;
}
switch (cf)
{
case CF_METAFILEPICT:
return m_pPL->RenderMetafilePict();
case CF_BITMAP:
return (HGLOBAL)m_pPL->RenderBitmap();
}
return NULL;
}
//CHAPTER18MOD
/*
* CCosmoDoc::RenderMedium
*
* Purpose:
* Like RenderFormat, this function creates a specific data format
* based on the cf parameter. Unlike RenderFormat, we store the
* result in a STGMEDIUM in case it has a medium other than
* TYMED_HGLOBAL. For conveniece we'll centralize all compound
* document formats here, hGlobal or not.
*
* Parameters:
* cf UINT clipboard format of interest.
* pSTM LSTGMEDIUM to fill. We only fill the union
* and tymed.
*
* Return Value:
* BOOL TRUE if we could render the format,
* FALSE otherwise.
*/
BOOL CCosmoDoc::RenderMedium(UINT cf, LPSTGMEDIUM pSTM)
{
if (NULL==pSTM)
return FALSE;
if (cf==m_cfEmbedSource)
{
/*
* Embed Source data is an IStorage containing the native
* data (same as Embedded Object). Since our data is small,
* it makes the most sense to create an IStorage in memory
* and put transfer that instead of a disk-based IStorage.
*/
pSTM->pstg=INOLE_CreateStorageOnHGlobal(STGM_DIRECT
| STGM_READWRITE | STGM_SHARE_EXCLUSIVE);
if (NULL==pSTM->pstg)
return FALSE;
//Now save the data to the storage.
WriteClassStg(pSTM->pstg, CLSID_CosmoFigure);
WriteFmtUserTypeStg(pSTM->pstg, m_cf
, PSZ(IDS_CLIPBOARDFORMAT));
if (POLYLINE_E_NONE!=m_pPL->WriteToStorage(pSTM->pstg
, VERSIONCURRENT))
{
/*
* When someone releases the IStorage, STORAGE.DLL will
* release the ILockBytes which, having fDeleteOnRelease
* TRUE (second parameter) will release the memory.
* That's why we don't have STGM_DELETEONRELEASE on the
* IStorage.
*/
pSTM->pstg->Release();
return FALSE;
}
pSTM->tymed=TYMED_ISTORAGE;
return TRUE;
}
if (cf==m_cfObjectDescriptor)
{
SIZEL szl, szlT;
POINTL ptl;
RECT rc;
m_pPL->SizeGet(&rc);
SETSIZEL(szlT, rc.right, rc.bottom);
XformSizeInPixelsToHimetric(NULL, &szlT, &szl);
SETPOINTL(ptl, 0, 0);
pSTM->hGlobal=INOLE_AllocObjectDescriptor
(CLSID_CosmoFigure, DVASPECT_CONTENT, szl, ptl
, OLEMISC_RECOMPOSEONRESIZE, PSZ(IDS_OBJECTDESCRIPTION)
, NULL);
pSTM->tymed=TYMED_HGLOBAL;
return (NULL!=pSTM->hGlobal);
}
return FALSE;
}
//End CHAPTER18MOD
/*
* CCosmoDoc::FQueryPaste
*
* Purpose:
* Determines if we can paste data from the clipboard.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if data is available, FALSE otherwise.
*/
BOOL CCosmoDoc::FQueryPaste(void)
{
LPDATAOBJECT pIDataObject;
BOOL fRet;
if (FAILED(OleGetClipboard(&pIDataObject)))
return FALSE;
fRet=FQueryPasteFromData(pIDataObject);
pIDataObject->Release();
return fRet;
}
/*
* CCosmoDoc::FQueryPasteFromData
* (Protected)
*
* Purpose:
* Determines if we can paste data from a data object.
*
* Parameters:
* pIDataObject LPDATAOBJECT from which we might want to paste.
*
* Return Value:
* BOOL TRUE if data is available, FALSE otherwise.
*/
BOOL CCosmoDoc::FQueryPasteFromData(LPDATAOBJECT pIDataObject)
{
FORMATETC fe;
SETDefFormatEtc(fe, m_cf, TYMED_HGLOBAL);
return (NOERROR==pIDataObject->QueryGetData(&fe));
}
/*
* CCosmoDoc::Paste
*
* Purpose:
* Retrieves the private data format from the clipboard and sets it
* to the current figure in the editor window.
*
* Note that if this function is called, then the clipboard format
* is available because the Paste menu item is only enabled if the
* format is present.
*
* Parameters:
* hWndFrame HWND of the main window.
*
* Return Value:
* BOOL TRUE if successful, FALSE otherwise.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -