📄 document.cpp
字号:
/*
* 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;
BOOL fEmbedding;
fEmbedding=m_pFigure->FIsEmbedded();
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.
*/
//For embedding, this is Save Copy As, so don't ask about versions.
if (0!=m_lVer && m_lVer!=lVer && !fEmbedding)
{
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;
//Saving makes us clean, but this doesn't apply to embedding.
if (!fEmbedding)
FDirtySet(FALSE);
//Update the known version of this document.
m_lVer=lVer;
/*
* 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);
return DOCERR_NONE;
}
/*
* CCosmoDoc::Rename
*
* Purpose:
* Overrides the normal rename to manage a moniker for the open
* file. We maintain no moniker for untitled documents, and
* therefore do not allow linking to such documents.
*
* Parameters:
* pszFile LPTSTR to the new filename.
*
* Return Value:
* None
*/
void CCosmoDoc::Rename(LPTSTR pszFile)
{
LPMONIKER pmk;
//We don't need to change the base class, just augment...
CDocument::Rename(pszFile);
//Unregister the old moniker (m_dwRegROT set to zero).
INOLE_RevokeAsRunning(&m_dwRegROT);
ReleaseInterface(m_pMoniker);
if (NULL!=pszFile)
{
CreateFileMoniker(pszFile, &pmk);
if (NULL!=pmk)
{
m_pMoniker=pmk; //pmk AddRef'd in CreateFileMoniker
INOLE_RegisterAsRunning(m_pFigure, m_pMoniker
, 0, &m_dwRegROT);
m_pFigure->SendAdvise(OBJECTCODE_RENAMED);
}
}
return;
}
/*
* CCosmoDoc::Undo
*
* Purpose:
* Reverses a previous action.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void CCosmoDoc::Undo(void)
{
//CHAPTER23MOD
//Give the in-place object a chance for DeactivateAndUndo.
if (m_pFigure->Undo())
return;
//End CHAPTER23MOD
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;
}
/*
* 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;
}
/*
* CFSTR_OBJECTDESCRIPTOR and CFSTR_LINKSRCDESCRIPTOR are the
* same formats, but only copy link source if we have a moniker.
*/
if (cf==m_cfLinkSrcDescriptor && NULL==m_pMoniker)
return FALSE;
if (cf==m_cfObjectDescriptor || cf==m_cfLinkSrcDescriptor)
{
SIZEL szl, szlT;
POINTL ptl;
RECT rc;
LPTSTR psz=NULL;
m_pPL->SizeGet(&rc);
SETSIZEL(szlT, rc.right, rc.bottom);
XformSizeInPixelsToHimetric(NULL, &szlT, &szl);
SETPOINTL(ptl, 0, 0);
//Include the moniker display name now, if we have one.
if (m_pMoniker)
{
LPBC pbc;
CreateBindCtx(0, &pbc);
#ifdef WIN32ANSI
LPOLESTR pszW;
m_pMoniker->GetDisplayName(pbc, NULL, &pszW);
psz=(LPTSTR)CoTaskMemAlloc(512);
WideCharToMultiByte(CP_ACP, 0, pszW, -1, psz, 512
, NULL, NULL);
CoTaskMemFree((void *)pszW);
#else
m_pMoniker->GetDisplayName(pbc, NULL, &psz);
#endif
pbc->Release();
}
pSTM->hGlobal=INOLE_AllocObjectDescriptor
(CLSID_CosmoFigure, DVASPECT_CONTENT, szl, ptl
, OLEMISC_RECOMPOSEONRESIZE, PSZ(IDS_OBJECTDESCRIPTION)
, psz);
CoTaskMemFree((void *)psz);
pSTM->tymed=TYMED_HGLOBAL;
return (NULL!=pSTM->hGlobal);
}
if (cf==m_cfLinkSource)
{
if (NULL!=m_pMoniker)
{
FORMATETC fe;
HRESULT hr;
pSTM->tymed=TYMED_NULL;
SETDefFormatEtc(fe, cf, TYMED_ISTREAM);
hr=INOLE_GetLinkSourceData(m_pMoniker
, (LPCLSID)&CLSID_CosmoFigure, &fe, pSTM);
return SUCCEEDED(hr);
}
}
return FALSE;
}
/*
* 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -