📄 page.cpp
字号:
/*
* PAGE.CPP
* Patron Chapter 12
*
* Implementation of parts of the CPage class; those member
* functions dealing with mouse events are in PAGEMOUS.CPP.
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "patron.h"
/*
* CPage::CPage
* CPage::~CPage
*
* Constructor Parameters:
* dwID DWORD identifier for this page.
* hWnd HWND of the pages window (for repaints, etc).
* pPG PCPages to the Pages window.
*/
//CHAPTER12MOD
CPage::CPage(DWORD dwID, HWND hWnd, PCPages pPG)
//End CHAPTER12MOD
{
m_dwID =dwID;
m_pIStorage=NULL;
//CHAPTER12MOD
m_cOpens=0;
m_hWnd=hWnd;
m_pPG=pPG;
m_dwIDNext =0;
m_cTenants =0;
m_hWndTenantList=NULL;
m_iTenantCur =NOVALUE; //Tenants are zero indexed.
m_pTenantCur =NULL;
m_uHTCode=HTNOWHERE;
m_uSizingFlags=0;
m_fTracking=FALSE;
m_hDC=NULL;
m_fSizePending=FALSE;
m_fTimer=FALSE;
//These will be read from WIN.INI in Chapter 13.
m_cxyDist=2;
m_cDelay=200;
//End CHAPTER12MOD
return;
}
CPage::~CPage(void)
{
//CHAPTER12MOD
if (m_fTimer)
KillTimer(m_hWnd, IDTIMER_DEBOUNCE);
//End CHAPTER12MOD
m_hWnd=NULL;
Close(FALSE);
return;
}
/*
* CPage::GetID
*
* Return Value:
* DWORD dwID field in this page.
*/
DWORD CPage::GetID(void)
{
return m_dwID;
}
/*
* CPage::Open
*
* Purpose:
* Retrieves the IStorage associated with this page. The IStorage
* is owned by the page and thus the page always holds a reference
* count. The caller should call Close or delete this page to
* match this open.
*
* This function may be called multiple times resulting in
* additional reference counts on the storage each of which must be
* matched with a call to Close. The last Close can be done
* through delete.
*
* Parameters:
* pIStorage LPSTORAGE in which this page lives.
*
* Return Value:
* BOOL TRUE if opening succeeds, FALSE otherwise.
*/
BOOL CPage::Open(LPSTORAGE pIStorage)
{
//CHAPTER12MOD
HRESULT hr=NOERROR;
LPSTREAM pIStream;
DWORD dwMode;
OLECHAR szTemp[32];
BOOL fNew;
BOOL fCreated=FALSE;
TENANTLIST tl;
PTENANTINFO pti;
ULONG cb;
LPMALLOC pIMalloc;
UINT i;
PCTenant pTenant;
fNew=(NULL==m_pIStorage);
if (!fNew)
{
m_cOpens++;
m_pIStorage->AddRef();
return TRUE;
}
if (NULL==pIStorage)
return FALSE;
/*
* Attempt to open the storage under this ID. If none,
* create one. In either case, the IStorage is either
* saved in pPage or released.
*/
GetStorageName(szTemp);
dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
hr=pIStorage->OpenStorage(szTemp, NULL, dwMode, NULL, 0
, &m_pIStorage);
if (FAILED(hr))
{
hr=pIStorage->CreateStorage(szTemp, dwMode, 0, 0
, &m_pIStorage);
fCreated=TRUE;
}
if (FAILED(hr))
return FALSE;
m_cOpens++;
if (NULL==m_hWndTenantList)
{
/*
* The first time we open this page, create the hidden
* listbox we'll use to track tenants. We give it the
* owner-draw style so we can just store pointers in it.
*/
m_hWndTenantList=CreateWindow(TEXT("listbox")
, TEXT("Tenant List"), WS_POPUP | LBS_OWNERDRAWFIXED
, 0, 0, 100, 100, HWND_DESKTOP, NULL
, m_pPG->m_hInst, NULL);
if (NULL==m_hWndTenantList)
return FALSE;
}
//If this is brand-new, we're done.
if (fCreated)
return TRUE;
/*
* Now open the stream we saved in Close and load all the
* tenants listed in there. If there are none, then we don't
* have to load squat.
*/
hr=m_pIStorage->OpenStream(SZSTREAMTENANTLIST, NULL, STGM_DIRECT
| STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
if (FAILED(hr))
return FALSE;
if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
{
pIStream->Read(&tl, sizeof(tl), NULL);
m_cTenants=tl.cTenants;
m_dwIDNext=tl.dwIDNext;
m_iTenantCur=0;
cb=tl.cTenants*sizeof(TENANTINFO);
if (0!=cb)
{
pti=(PTENANTINFO)pIMalloc->Alloc(cb);
if (NULL!=pti)
{
pIStream->Read(pti, cb, NULL);
for (i=0; i < m_cTenants; i++)
{
if (TenantAdd(NOVALUE, (pti+i)->dwID, &pTenant))
pTenant->Load(m_pIStorage, (pti+i));
}
pIMalloc->Free(pti);
}
}
pIMalloc->Release();
}
pIStream->Release();
//Get and select the first tenant
if (TenantGet(0, &m_pTenantCur, FALSE))
m_pTenantCur->Select(TRUE);
return TRUE;
//End CHAPTER12MOD
}
/*
* CPage::Close
*
* Purpose:
* Possibly commits the storage, then releases it reversing the
* reference count from Open.
*
* Parameters:
* fCommit BOOL indicating if we're to commit.
*
* Return Value:
* None
*/
void CPage::Close(BOOL fCommit)
{
if (NULL==m_pIStorage)
return;
if (fCommit)
Update();
//CHAPTER12MOD
m_pIStorage->Release();
//If this was the last close, make all tenants loaded->passive
if (0==--m_cOpens)
{
UINT i;
PCTenant pTenant;
m_pIStorage=NULL;
for (i=0; i < m_cTenants; i++)
{
if (TenantGet(i, &pTenant, FALSE))
{
if (NULL!=m_hWnd)
{
//Open may select again, so this repaints.
pTenant->Select(FALSE);
}
pTenant->Close(FALSE);
delete pTenant;
}
}
DestroyWindow(m_hWndTenantList);
m_hWndTenantList=NULL;
}
//End CHAPTER12MOD
return;
}
/*
* CPage::Update
*
* Purpose:
* Forces a common on the page if it's open.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if there are any open objects on this page,
* that is, we should remain open.
*/
BOOL CPage::Update(void)
{
//CHAPTER12MOD
BOOL fOpen=FALSE;
UINT i;
PCTenant pTenant;
HRESULT hr;
LPSTREAM pIStream;
TENANTLIST tl;
PTENANTINFO pti;
ULONG cb;
LPMALLOC pIMalloc;
//Walk the list of objects and update them all as well.
for (i=0; i < m_cTenants; i++)
{
if (TenantGet(i, &pTenant, FALSE))
fOpen |= pTenant->Update();
}
//Now write our own stream containing the tenant list.
hr=m_pIStorage->CreateStream(SZSTREAMTENANTLIST, STGM_CREATE
| STGM_WRITE| STGM_DIRECT | STGM_SHARE_EXCLUSIVE, 0, 0
, &pIStream);
if (FAILED(hr))
return fOpen;
if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
{
tl.cTenants=m_cTenants;
tl.dwIDNext=m_dwIDNext;
pIStream->Write(&tl, sizeof(TENANTLIST), &cb);
cb=m_cTenants*sizeof(TENANTINFO);
pti=(PTENANTINFO)pIMalloc->Alloc(cb);
if (NULL!=pti)
{
for (i=0; i < m_cTenants; i++)
{
TenantGet(i, &pTenant, FALSE);
pTenant->GetInfo((pti+i));
}
pIStream->Write(pti, cb, &cb);
pIMalloc->Free(pti);
}
pIMalloc->Release();
}
pIStream->Release();
//Now commit the whole mess and we're done
if (NULL!=m_pIStorage)
m_pIStorage->Commit(STGC_DEFAULT);
return fOpen;
//End CHAPTER12MOD
}
/*
* CPage::Destroy
*
* Purpose:
* Removes this page from the given storage. The caller should
* eventually delete this Page object to free the storage.
*
* Parameters:
* pIStorage LPSTORAGE contianing this page on which to call
* DestroyElement
*
* Return Value:
* None
*/
void CPage::Destroy(LPSTORAGE pIStorage)
{
if (NULL!=pIStorage)
{
OLECHAR szTemp[32];
Close(FALSE);
GetStorageName(szTemp);
pIStorage->DestroyElement(szTemp);
}
return;
}
/*
* CPage::GetStorageName
*
* Parameters:
* pszName LPOLESTR to a buffer in which to store the
* storage name for this page.
*
* Return Value:
* UINT Number of characters stored.
*/
UINT CPage::GetStorageName(LPOLESTR pszName)
{
#ifdef WIN32ANSI
char szTemp[32];
UINT cch;
cch=wsprintf(szTemp, "Page %lu", m_dwID);
MultiByteToWideChar(CP_ACP, 0, szTemp, -1, pszName, 32);
return cch;
#else
return wsprintf(pszName, TEXT("Page %lu"), m_dwID);
#endif
}
//CHAPTER12MOD
/*
* CPage::Draw
*
* Purpose:
* Draws the objects on this page to the given hDC.
*
* Parameters:
* hDC HDC on which to draw.
* xOff, yOff int offsets for the page.
* fNoColor BOOL indicating black & white screen rendering.
* fPrinter BOOL indicating hDC is on the printer.
*
* Return Value:
* None
*/
void CPage::Draw(HDC hDC, int xOff, int yOff, BOOL fNoColor
, BOOL fPrinter)
{
int i;
PCTenant pTenant;
HDC hIC=NULL;
PCOMBINEDEVICE pcd=NULL;
DVTARGETDEVICE *ptd=NULL;
/*
* If printing, tell the tenant to forget the borders. Otherwise
* we leave xOff and yOff the same to account for scrolling.
*/
if (fPrinter)
{
xOff=LOMETRIC_BORDER+m_pPG->m_xMarginLeft;
yOff=-LOMETRIC_BORDER-m_pPG->m_yMarginTop;
/*
* Get device information. If this fails, ptd is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -