📄 document.cpp
字号:
/*
* DOCUMENT.CPP
* Cosmo Chapter 23
*
* Implementation of the CCosmoDoc derivation of CDocument as
* well as an implementation of CPolylineAdviseSink.
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "cosmo.h"
/*
* CCosmoDoc::CCosmoDoc
* CCosmoDoc::~CCosmoDoc
*
* Constructor Parameters:
* hInst HINSTANCE of the application.
* pFR PCFrame of the frame object.
* pAdv PCDocumentAdviseSink to notify on events
*/
CCosmoDoc::CCosmoDoc(HINSTANCE hInst, PCFrame pFR
, PCDocumentAdviseSink pAdv)
: CDocument(hInst, pFR, pAdv)
{
m_pPL=NULL;
m_pPLAdv=NULL;
m_uPrevSize=SIZE_RESTORED;
m_pDropTarget=NULL;
m_fDragSource=FALSE;
m_cfEmbedSource=RegisterClipboardFormat(CFSTR_EMBEDSOURCE);
m_cfObjectDescriptor=RegisterClipboardFormat
(CFSTR_OBJECTDESCRIPTOR);
m_pFigure=NULL;
m_pMoniker=NULL;
m_dwRegROT=0L;
m_cfLinkSource=RegisterClipboardFormat(CFSTR_LINKSOURCE);
m_cfLinkSrcDescriptor=RegisterClipboardFormat
(CFSTR_LINKSRCDESCRIPTOR);
return;
}
CCosmoDoc::~CCosmoDoc(void)
{
//Make sure the object is saved
m_pFigure->SendAdvise(OBJECTCODE_SAVEOBJECT);
m_pFigure->SendAdvise(OBJECTCODE_HIDEWINDOW);
m_pFigure->SendAdvise(OBJECTCODE_CLOSED);
//If the document is reg'd as running, revoke and free the moniker
INOLE_RevokeAsRunning(&m_dwRegROT);
if (NULL!=m_pMoniker)
m_pMoniker->Release();
if (NULL!=m_pFigure)
{
m_pFigure->AddRef();
CoDisconnectObject(m_pFigure, 0L);
m_pFigure->Release(); //Starts shutdown if necessary.
delete m_pFigure;
}
//Clean up the allocations we did in Init
if (NULL!=m_pPL)
delete m_pPL;
if (NULL!=m_pPLAdv)
delete m_pPLAdv;
return;
}
/*
* CCosmoDoc::Init
*
* Purpose:
* Initializes an already created document window. The client
* actually creates the window for us, then passes that here for
* further initialization.
*
* Parameters:
* pDI PDOCUMENTINIT containing initialization
* parameters.
*
* Return Value:
* BOOL TRUE if the function succeeded, FALSE otherwise.
*/
BOOL CCosmoDoc::Init(PDOCUMENTINIT pDI)
{
RECT rc;
//Change the stringtable range to our customization.
pDI->idsMin=IDS_DOCUMENTMIN;
pDI->idsMax=IDS_DOCUMENTMAX;
//Do default initialization
if (!CDocument::Init(pDI))
return FALSE;
//Add the Polyline stuff we need.
m_pPLAdv=new CPolylineAdviseSink(this);
m_pPL =new CPolyline(m_hInst);
//Attempt to create our contained Polyline.
GetClientRect(m_hWnd, &rc);
InflateRect(&rc, -8, -8);
if (!m_pPL->Init(m_hWnd, &rc, WS_CHILD | WS_VISIBLE
, ID_POLYLINE, m_pPLAdv))
return FALSE;
m_pDropTarget=new CDropTarget(this);
if (NULL!=m_pDropTarget)
{
m_pDropTarget->AddRef();
CoLockObjectExternal(m_pDropTarget, TRUE, FALSE);
RegisterDragDrop(m_hWnd, m_pDropTarget);
}
m_pFigure=new CFigure(ObjectDestroyed, this);
if (NULL==m_pFigure)
return FALSE;
//We created an object, so count it.
g_cObj++;
if (!m_pFigure->Init())
return FALSE;
return TRUE;
}
/*
* CCosmoDoc::FMessageHook
*
* Purpose:
* Processes WM_SIZE for the document so we can resize
* the Polyline.
*
* Parameters:
* <WndProc Parameters>
* pLRes LRESULT * in which to store the return
* value for the message.
*
* Return Value:
* BOOL TRUE to prevent further processing,
* FALSE otherwise.
*/
BOOL CCosmoDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam, LRESULT *pLRes)
{
UINT dx, dy;
RECT rc;
*pLRes=0;
if (WM_SIZE==iMsg)
{
//Don't effect the Polyline size to or from minimized state.
if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
{
//When we change size, resize any Polyline we hold.
dx=LOWORD(lParam);
dy=HIWORD(lParam);
/*
* If we are getting WM_SIZE in response to a Polyline
* notification, then don't resize the Polyline window
* again.
*/
if (!m_fNoSize && NULL!=m_pPL)
{
//Resize the polyline to fit the new client
SetRect(&rc, 8, 8, dx-8, dy-8);
m_pPL->RectSet(&rc, FALSE);
/*
* We consider sizing something that makes the file
* dirty, but not until we've finished the create
* process, which is why we set fNoDirty to FALSE
* in WM_CREATE since we get a WM_SIZE on the first
* creation.
*/
if (!m_fNoDirty)
FDirtySet(TRUE);
SetRect(&rc, 0, 0, dx, dy);
if (NULL!=m_pAdv)
m_pAdv->OnSizeChange(this, &rc);
m_fNoDirty=FALSE;
}
}
m_uPrevSize=wParam;
}
if (WM_LBUTTONDOWN==iMsg)
{
LPDROPSOURCE pIDropSource;
LPDATAOBJECT pIDataObject;
HRESULT hr;
SCODE sc;
DWORD dwEffect;
/*
* The document has an 8 pixel border around the polyline
* window where we'll see mouse clicks. A left mouse button
* click here means the start of a drag-drop operation.
*
* Since this is a modal operation, this IDropSource
* is entirely local.
*/
pIDropSource=new CDropSource(this);
if (NULL==pIDropSource)
return FALSE;
pIDropSource->AddRef();
m_fDragSource=TRUE;
//Go get the data and start the ball rolling.
pIDataObject=TransferObjectCreate(FALSE);
if (NULL!=pIDataObject)
{
hr=DoDragDrop(pIDataObject, pIDropSource
, DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK
, &dwEffect);
pIDataObject->Release();
sc=GetScode(hr);
}
else
sc=E_FAIL;
/*
* When we return from DoDragDrop, either cancel or drop.
* First toss the IDropSource we have here, then bail out
* on cancel, and possibly clear our data on a move drop.
*/
pIDropSource->Release();
/*
* If dropped on the same document (determined using
* this flag, then dwEffect will be DROPEFFECT_NONE (see
* IDropTarget::Drop in DROPTGT.CPP). In any case,
* reset this since the operation is done.
*/
m_fDragSource=FALSE;
if (DRAGDROP_S_DROP==sc && DROPEFFECT_MOVE==dwEffect)
{
m_pPL->New();
FDirtySet(TRUE);
}
//On a canceled drop or a copy we don't do anything else
return TRUE;
}
if (WM_DESTROY==iMsg)
{
/*
* We have to revoke the drop target here because the window
* will be destroyed and the property forcefully removed
* before we could do this in the destructor.
*/
if (NULL!=m_pDropTarget)
{
RevokeDragDrop(m_hWnd);
CoLockObjectExternal(m_pDropTarget, FALSE, TRUE);
ReleaseInterface(m_pDropTarget);
}
return FALSE;
}
/*
* We return FALSE even on WM_SIZE so we can let the default
* procedure handle maximized MDI child windows appropriately.
*/
return FALSE;
}
/*
* CCosmoDoc::Clear
*
* Purpose:
* Sets all contents in the document back to defaults with
* no filename.
*
* Paramters:
* None
*
* Return Value:
* None
*/
void CCosmoDoc::Clear(void)
{
//Completely reset the polyline
m_pPL->New();
CDocument::Clear();
m_lVer=0;
return;
}
/*
* CCosmoDoc::FDirtySet
*
* Purpose:
* Sets or clears the document 'dirty' flag returning the previous
* state of that same flag. We override this in Cosmo server to
* send the OnDataChange notification as necessary.
*
* Parameters:
* fDirty BOOL indicating the new contents of the dirty
* flag.
*
* Return Value:
* BOOL Previous value of the dirty flag.
*/
BOOL CCosmoDoc::FDirtySet(BOOL fDirty)
{
BOOL fRet;
fRet=CDocument::FDirtySet(fDirty);
m_pFigure->SendAdvise(OBJECTCODE_DATACHANGED);
return fRet;
}
/*
* CCosmoDoc::FDirtyGet
*
* Purpose:
* Override of the normal FDirtyGet such that we never return dirty
* for an embedded object we're serving since updates constantly
* happen and since the object will be saved on closure. This then
* prevents any message boxes asking the user to save.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if the document is dirty, FALSE otherwise.
*/
BOOL CCosmoDoc::FDirtyGet(void)
{
if (m_pFigure->FIsEmbedded())
return FALSE;
return m_fDirty;
}
/*
* CCosmoDoc::Load
*
* Purpose:
* Loads a given document without any user interface overwriting
* the previous contents of the Polyline window. We do this by
* opening the file and telling the Polyline to load itself from
* that file.
*
* Parameters:
* fChangeFile BOOL indicating if we're to update the window
* title and the filename from using this file.
* pszFile LPTSTR to the filename to load, NULL if the file
* is new and untitled.
*
* Return Value:
* UINT An error value from DOCERR_*
*/
UINT CCosmoDoc::Load(BOOL fChangeFile, LPTSTR pszFile)
{
HRESULT hr;
LPSTORAGE pIStorage;
if (NULL==pszFile)
{
//For a new untitled document, just rename ourselves.
Rename(NULL);
m_lVer=VERSIONCURRENT;
return DOCERR_NONE;
}
/*
* If not a Compound File, open the file using STGM_CONVERT in
* transacted mode to see old files as a storage with one stream
* called "CONTENTS" (which is conveniently the name we use
* in the new files). We must use STGM_TRANSACTED here or else
* the old file will be immediately converted on disk: we only
* want a converted image in memory from which to read. In
* addition, note that we need STGM_READWRITE as well since
* conversion is inherently a write operation.
*/
pIStorage=NULL;
if (NOERROR!=StgIsStorageFile(pszFile))
{
hr=StgCreateDocfile(pszFile,STGM_TRANSACTED | STGM_READWRITE
| 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -