📄 document.cpp
字号:
if (FAILED(hr))
return DOCERR_READFAILURE;
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 in order to have
* 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)
{
BOOL fRename=TRUE;
HRESULT hr;
LPSTORAGE pIStorage;
BOOL fSameAsLoad;
//If Save or Save As under the same name, do Save.
if (NULL==pszFile || 0==lstrcmpi(pszFile, m_szFile))
{
fRename=FALSE;
pszFile=m_szFile;
/*
* If we're saving to an existing storage, just pass
* the IStorage we have from Load along with TRUE
* in fSameAsLoad.
*/
fSameAsLoad=TRUE;
}
else
{
/*
* In Component Cosmo, we only deal with one version of
* data; all the code in Chapter 1 Cosmo that dealt with
* 1.0 and 2.0 files has been removed.
*/
hr=StgCreateDocfile(pszFile, STGM_DIRECT | STGM_READWRITE
| STGM_CREATE | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
if (FAILED(hr))
return DOCERR_COULDNOTOPEN;
//Tell the object to save into this new storage
fSameAsLoad=FALSE;
//Update our variable
m_pIStorage->Release();
m_pIStorage=pIStorage;
}
if (PERSIST_STORAGE==m_pp.psModel)
{
hr=m_pp.pIP.pIPersistStorage->Save(m_pIStorage, fSameAsLoad);
if (SUCCEEDED(hr))
{
hr=m_pp.pIP.pIPersistStorage->SaveCompleted(fSameAsLoad
? NULL : m_pIStorage);
}
}
else
{
LPSTREAM pIStream;
hr=m_pIStorage->CreateStream(SZSTREAM, STGM_DIRECT
| STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE
, 0, 0, &pIStream);
if (SUCCEEDED(hr))
{
//This also works for PERSIST_STREAMINIT
hr=m_pp.pIP.pIPersistStream->Save(pIStream, TRUE);
pIStream->Release();
}
}
if (FAILED(hr))
return DOCERR_WRITEFAILURE;
//Saving makes us clean
FDirtySet(FALSE);
if (fRename)
Rename(pszFile);
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)
{
//CHAPTER13MOD
BOOL fRet=TRUE;
LPDATAOBJECT pIDataObject;
pIDataObject=TransferObjectCreate();
if (NULL==pIDataObject)
return FALSE;
//CHAPTER13MOD
fRet=SUCCEEDED(OleSetClipboard(pIDataObject));
if (NULL!=m_pIDataClip)
m_pIDataClip->Release();
m_pIDataClip=pIDataObject;
//Delete our current data if a "cut" succeeded.
if (fRet && fCut)
{
m_pPL->New();
FDirtySet(TRUE);
}
return fRet;
}
/*
* 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;
//CHAPTER13MOD
fRet=FQueryPasteFromData(pIDataObject);
//End CHAPTER13MOD
pIDataObject->Release();
return fRet;
}
//CHAPTER13MOD
/*
* 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));
}
//End CHAPTER13MOD
/*
* 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.
*/
BOOL CCosmoDoc::Paste(HWND hWndFrame)
{
LPDATAOBJECT pIDataObject;
BOOL fRet;
if (FAILED(OleGetClipboard(&pIDataObject)))
return FALSE;
//CHAPTER13MOD
fRet=PasteFromData(pIDataObject);
//End CHAPTER13MOD
pIDataObject->Release();
return TRUE;
}
//CHAPTER13MOD
/*
* CCosmoDoc::PasteFromData
* (Protected)
*
* Purpose:
* Retrieves the private data format from a data object and sets
* it to the current figure in the editor window.
*
* Parameters:
* pIDataObject LPDATAOBJECT from which to paste.
*
* Return Value:
* BOOL TRUE if successful, FALSE otherwise.
*/
BOOL CCosmoDoc::PasteFromData(LPDATAOBJECT pIDataObject)
{
FORMATETC fe;
STGMEDIUM stm;
BOOL fRet;
SETDefFormatEtc(fe, m_cf, TYMED_HGLOBAL);
fRet=SUCCEEDED(pIDataObject->GetData(&fe, &stm));
if (!fRet || NULL==stm.hGlobal)
return FALSE;
if (fRet && NULL!=stm.hGlobal)
{
m_pPL->QueryInterface(IID_IDataObject
, (PPVOID)&pIDataObject);
pIDataObject->SetData(&fe, &stm, TRUE);
pIDataObject->Release();
FDirtySet(TRUE);
}
return fRet;
}
/*
* CCosmoDoc::TransferObjectCreate
* (Protected)
*
* Purpose:
* Creates a DataTransferObject and stuffs the current Polyline
* data into it, used for both clipboard and drag-drop operations.
*
* Parameters:
* None
*
* Return Value:
* LPDATAOBJECT Pointer to the object created, NULL on failure
*/
LPDATAOBJECT CCosmoDoc::TransferObjectCreate(void)
{
UINT i;
HRESULT hr;
STGMEDIUM stm;
FORMATETC fe;
LPDATAOBJECT pIDataSrc;
LPDATAOBJECT pIDataObject=NULL;
const UINT cFormats=3;
static UINT rgcf[3]={0, CF_METAFILEPICT, CF_BITMAP};
static DWORD rgtm[3]={TYMED_HGLOBAL, TYMED_MFPICT
, TYMED_GDI};
hr=CoCreateInstance(CLSID_DataTransferObject, NULL
, CLSCTX_INPROC_SERVER, IID_IDataObject
, (PPVOID)&pIDataObject);
if (FAILED(hr))
return NULL;
rgcf[0]=m_cf;
hr=m_pPL->QueryInterface(IID_IDataObject, (PPVOID)&pIDataSrc);
if (FAILED(hr))
{
pIDataObject->Release();
return NULL;
}
for (i=0; i < cFormats; i++)
{
//Copy private data first.
SETDefFormatEtc(fe, rgcf[i], rgtm[i]);
if (SUCCEEDED(pIDataSrc->GetData(&fe, &stm)))
pIDataObject->SetData(&fe, &stm, TRUE);
}
pIDataSrc->Release();
return pIDataObject; //Caller now responsible
}
/*
* CCosmoDoc::DropSelectTargetWindow
* (Protected)
*
* Purpose:
* Creates a thin inverted frame around a window that we use to
* show the window as a drop target. This is a toggle function:
* it uses XOR to create the effect so it must be called twice
* to leave the window as it was.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void CCosmoDoc::DropSelectTargetWindow(void)
{
HDC hDC;
RECT rc;
UINT dd=3;
HWND hWnd;
m_pPL->Window(&hWnd);
hDC=GetWindowDC(hWnd);
GetClientRect(hWnd, &rc);
//Frame this window with inverted pixels
//Top
PatBlt(hDC, rc.left, rc.top, rc.right-rc.left, dd, DSTINVERT);
//Bottom
PatBlt(hDC, rc.left, rc.bottom-dd, rc.right-rc.left, dd
, DSTINVERT);
//Left excluding regions already affected by top and bottom
PatBlt(hDC, rc.left, rc.top+dd, dd, rc.bottom-rc.top-(2*dd)
, DSTINVERT);
//Right excluding regions already affected by top and bottom
PatBlt(hDC, rc.right-dd, rc.top+dd, dd, rc.bottom-rc.top-(2*dd)
, DSTINVERT);
ReleaseDC(hWnd, hDC);
return;
}
//End CHAPTER13MOD
/*
* CCosmoDoc::ColorSet
*
* Purpose:
* Changes a color used in our contained Polyline.
*
* Parameters:
* iColor UINT index of the color to change.
* cr COLORREF new color.
*
* Return Value:
* COLORREF Previous color for the given index.
*/
COLORREF CCosmoDoc::ColorSet(UINT iColor, COLORREF cr)
{
COLORREF crRet;
m_pPL->ColorSet(iColor, cr, &crRet);
return crRet;
}
/*
* CCosmoDoc::ColorGet
*
* Purpose:
* Retrieves a color currently in use in the Polyline.
*
* Parameters:
* iColor UINT index of the color to retrieve.
*
* Return Value:
* COLORREF Current color for the given index.
*/
COLORREF CCosmoDoc::ColorGet(UINT iColor)
{
COLORREF crRet;
m_pPL->ColorGet(iColor, &crRet);
return crRet;
}
/*
* CCosmoDoc::LineStyleSet
*
* Purpose:
* Changes the line style currently used in the Polyline
*
* Parameters:
* iStyle UINT index of the new line style to use.
*
* Return Value:
* UINT Previous line style.
*/
UINT CCosmoDoc::LineStyleSet(UINT iStyle)
{
UINT i;
m_pPL->LineStyleSet(iStyle, &i);
return i;
}
/*
* CCosmoDoc::LineStyleGet
*
* Purpose:
* Retrieves the line style currently used in the Polyline
*
* Parameters:
* None
*
* Return Value:
* UINT Current line style.
*/
UINT CCosmoDoc::LineStyleGet(void)
{
UINT i=0;
//m_pPL might not be valid yet.
if (NULL!=m_pPL)
m_pPL->LineStyleGet(&i);
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -