Click here to Skip to main content
15,896,374 members
Articles / Desktop Programming / WTL

A fast and lightweight cell control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (31 votes)
11 Mar 2008CPOL1 min read 91K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
/* -------------------------------------------------------------------------
//MyCell Library - MyCell version 1.0
//
// This file is a part of the MyCell Library.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.txt at this distribution. By using
// this software in any fashion, you are agreeing to be bound by the terms
// of this license. You must not remove this notice, or any other, from
// this software.
//
// Creator: yanxueming
// Email: xueming.yan@gmail.com
// -----------------------------------------------------------------------*/
#pragma once
#include "_IPropertyPage.h"
#include "base.h"
namespace mycell{
	template <class T/*,class TPropertyPageSite*/>
	class PropertyPageBase : public _IPropertyPage
	{

	public:
		void SetDirty(BOOL bDirty)
		{
			T* pT = static_cast<T*>(this);
			if (pT->m_bDirty != bDirty)
			{
				pT->m_bDirty = bDirty;
				//pT->pSheet_->OnStatusChange(bDirty ? PROPPAGESTATUS_DIRTY : PROPPAGESTATUS_CLEAN);
			}
		}

		PropertyPageBase()
		{
			T* pT = static_cast<T*>(this);
			pT->pSheet_ = NULL;
			pT->m_size.cx = 0;
			pT->m_size.cy = 0;
			//pT->m_dwTitleID = 0;
			pT->m_dwHelpFileID = 0;
			pT->m_dwDocStringID = 0;
			pT->m_dwHelpContext = 0;
			//pT->m_ppUnk = NULL;
			//pT->m_nObjects = 0;
			pT->m_bDirty = FALSE;
			pT->m_hWnd = NULL;
		}

		~PropertyPageBase()
		{
			/*
			T* pT = static_cast<T*>(this);
			if (pT->pSheet_ != NULL)
				pT->pSheet_->Release();

			for (UINT i = 0; i < m_nObjects; i++)
				pT->m_ppUnk[i]->Release();

			delete[] pT->m_ppUnk;
			*/
		}

		// IPropertyPage
		//
		STDMETHOD(SetPageSite)(Worksheet *pPageSite)
		{
			T* pT = static_cast<T*>(this);
			pT->pSheet_=pPageSite;
			//ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::SetPageSite\n"));

			//if (!pPageSite && pT->pSheet_)
			//{
			//	pT->pSheet_->Release();
			//	pT->pSheet_ = NULL;
			//	return S_OK;
			//}

			//if (!pPageSite && !pT->pSheet_)
			//	return S_OK;

			//if (pPageSite && pT->pSheet_)
			//{
			//	ATLTRACE(atlTraceControls,2,_T("Error : setting page site again with non NULL value\n"));
			//	return E_UNEXPECTED;
			//}

			//pT->pSheet_ = pPageSite;
			//pT->pSheet_->AddRef();
			return S_OK;
		}
		STDMETHOD(Activate)(HWND hWndParent, LPCRECT pRect, BOOL /* bModal */)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Activate\n"));

			if (pRect == NULL)
			{
				ATLTRACE(atlTraceControls,2,_T("Error : Passed a NULL rect\n"));
				return E_POINTER;
			}

			pT->m_hWnd = pT->Create(hWndParent);
			Move(pRect);

			m_size.cx = pRect->right - pRect->left;
			m_size.cy = pRect->bottom - pRect->top;

			return S_OK;

		}
		STDMETHOD(Deactivate)( void)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Deactivate\n"));

			if (pT->m_hWnd)
			{
				ATLTRACE(atlTraceControls,2,_T("Destroying Dialog\n"));
				if (::IsWindow(pT->m_hWnd))
					pT->DestroyWindow();
				pT->m_hWnd = NULL;
			}

			return S_OK;

		}
		STDMETHOD(GetPageInfo)(PROPPAGEINFO *pPageInfo)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::GetPageInfo\n"));

			if (pPageInfo == NULL)
			{
				ATLTRACE(atlTraceControls,2,_T("Error : PROPPAGEINFO passed == NULL\n"));
				return E_POINTER;
			}

			//HRSRC hRsrc = FindResource(_AtlBaseModule.GetResourceInstance(),
			//	MAKEINTRESOURCE(static_cast<T*>(this)->IDD), RT_DIALOG);
			//if (hRsrc == NULL)
			//{
			//	ATLTRACE(atlTraceControls,2,_T("Could not find resource template\n"));
			//	return E_UNEXPECTED;
			//}

			//HGLOBAL hGlob = LoadResource(_AtlBaseModule.GetResourceInstance(), hRsrc);
			//DLGTEMPLATE* pDlgTempl = (DLGTEMPLATE*)LockResource(hGlob);
			//if (pDlgTempl == NULL)
			//{
			//	ATLTRACE(atlTraceControls,2,_T("Could not load resource template\n"));
			//	return E_UNEXPECTED;
			//}
			//AtlGetDialogSize(pDlgTempl, &m_size, true);

			pPageInfo->cb = sizeof(PROPPAGEINFO);
			pPageInfo->pszTitle = m_bstrTitle;//LoadStringHelper(pT->m_dwTitleID);
			pPageInfo->size = m_size;
			//pPageInfo->pszHelpFile = LoadStringHelper(pT->m_dwHelpFileID);
			//pPageInfo->pszDocString = LoadStringHelper(pT->m_dwDocStringID);
			//pPageInfo->dwHelpContext = pT->m_dwHelpContext;

			return S_OK;
		}

			/*
		STDMETHOD(SetObjects)(ULONG nObjects, IUnknown **ppUnk)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::SetObjects\n"));

			if (ppUnk == NULL)
				return E_POINTER;

			if (pT->m_ppUnk != NULL && pT->m_nObjects > 0)
			{
				for (UINT iObj = 0; iObj < pT->m_nObjects; iObj++)
					pT->m_ppUnk[iObj]->Release();

				delete [] pT->m_ppUnk;
			}

			pT->m_ppUnk = NULL;
			ATLTRY(pT->m_ppUnk = new IUnknown*[nObjects]);

			if (pT->m_ppUnk == NULL)
				return E_OUTOFMEMORY;

			for (UINT i = 0; i < nObjects; i++)
			{
				ppUnk[i]->AddRef();
				pT->m_ppUnk[i] = ppUnk[i];
			}

			pT->m_nObjects = nObjects;
			return S_OK;
		}
			*/
		STDMETHOD(Show)(UINT nCmdShow)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Show\n"));

			if (pT->m_hWnd == NULL)
				return E_UNEXPECTED;

			ShowWindow(pT->m_hWnd, nCmdShow);
			return S_OK;
		}
		STDMETHOD(Move)(LPCRECT pRect)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Move\n"));

			if (pT->m_hWnd == NULL)
				return E_UNEXPECTED;

			if (pRect == NULL)
				return E_POINTER;

			MoveWindow(pT->m_hWnd, pRect->left, pRect->top, pRect->right - pRect->left,
				pRect->bottom - pRect->top, TRUE);

			return S_OK;

		}
		STDMETHOD(IsPageDirty)(void)
		{
			T* pT = static_cast<T*>(this);
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::IsPageDirty\n"));
			return pT->m_bDirty ? S_OK : S_FALSE;
		}
		STDMETHOD(Apply)(void)
		{
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Apply\n"));
			return S_OK;
		}
		STDMETHOD(Help)(LPCOLESTR pszHelpDir)
		{
			/*
			T* pT = static_cast<T*>(this);
			USES_CONVERSION;

			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Help\n"));
			CComBSTR szFullFileName(pszHelpDir);
			CComHeapPtr< OLECHAR > pszFileName(LoadStringHelper(pT->m_dwHelpFileID));
			if (pszFileName == NULL)
				return E_OUTOFMEMORY;
			szFullFileName.Append(OLESTR("\\"));
			szFullFileName.Append(pszFileName);
			WinHelp(pT->m_hWnd, OLE2CT(szFullFileName), HELP_CONTEXTPOPUP, NULL);
			*/
			return S_OK;
		}
		STDMETHOD(TranslateAccelerator)(MSG *pMsg)
		{
			ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::TranslateAccelerator\n"));
			T* pT = static_cast<T*>(this);
			if ((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
				(pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
				return S_FALSE;

			return (IsDialogMessage(pT->m_hWnd, pMsg)) ? S_OK : S_FALSE;
		}

		Worksheet* pSheet_;
		//IPropertyPageSite* pSheet_;
		//IUnknown** m_ppUnk;
		//ULONG m_nObjects;
		SIZE m_size;
		//UINT m_dwTitleID;
		CComBSTR m_bstrTitle;
		UINT m_dwHelpFileID;
		UINT m_dwDocStringID;
		DWORD m_dwHelpContext;
		BOOL m_bDirty;

		//methods
	public:

		BEGIN_MSG_MAP(PropertyPageBase<T>)
			MESSAGE_HANDLER(WM_STYLECHANGING, OnStyleChange)
		END_MSG_MAP()

		LRESULT OnStyleChange(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
		{
			if (wParam == GWL_EXSTYLE)
			{
				LPSTYLESTRUCT lpss = (LPSTYLESTRUCT) lParam;
				lpss->styleNew |= WS_EX_CONTROLPARENT;
				return 0;
			}
			return 1;
		}

		LPOLESTR LoadStringHelper(UINT idRes)
		{
			const ATLSTRINGRESOURCEIMAGE* pString = AtlGetStringResourceImage( 
				_AtlBaseModule.GetResourceInstance(), idRes);
			if (pString == NULL)
			{
				ATLTRACE(atlTraceControls,2,_T("Error : Failed to load string from res\n"));
				return NULL;
			}

			CComHeapPtr< OLECHAR > psz;

			psz.Allocate( pString->nLength+1 );
			if (psz != NULL)
			{
				memcpy(psz, pString->achString, pString->nLength*sizeof(OLECHAR));
				psz[pString->nLength] = L'\0';
			}

			return psz.Detach();
		}
	};
}//namespace mycell

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
My name is Yanxueming,i live in Chengdu China.Graduated from UESTC in 1999.

Comments and Discussions