Click here to Skip to main content
15,887,391 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 90.9K   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
//Yanxm 2008��1��16�� 18:01:25
// -----------------------------------------------------------------------*/
#pragma once
#include <utility>
#include "base.h"
#include "StyleDesc.h"
#include "StyleStore.h"
using mycell::style::StyleDesc;
using namespace std;
namespace mycell{
	//class ColStylesMgr;
	class CellStore
	{
		StyleID_t		nStyleID;	//��Ԫ��ķ��ID
		USHORT	cellType;		//cellType���ͱ���ECellType�Լ��û��Զ�������
		CComVariant cellVal;		//��Ԫ���ֵ
	public:
		//CellStore()
		//{
		//}
		CellStore(const CellStore* prhs=NULL)
		{
			if(prhs){
				*this=*prhs;
			}else{
				clear();
			}
		}
		CellStore& operator=(const CellStore& rhs)
		{
			nStyleID=rhs.nStyleID;
			cellVal=rhs.cellVal;
			cellType=rhs.cellType;
			return *this;
		}
		StyleID_t GetStyleID()const
		{
			return nStyleID;
		}
		ECellType GetCellType()const
		{
			return (ECellType)cellType;
		}
		const CComVariant& GetData()const
		{
			return cellVal;
		}
		CComVariant& GetData()
		{
			return cellVal;
		}
		StyleID_t SetStyle(StyleStore& ss,StyleDesc style)
		{
			ss.DecrementRefCount(nStyleID);
			return nStyleID=ss.add_style(style);
		}
		void ClearStyle(StyleStore& ss)
		{
			ss.DecrementRefCount(nStyleID);
			nStyleID=0;
		}
		void SetCellType(ECellType ct)
		{
			cellType=ct;
		}
		void SetData(const VARIANT& val)
		{
			cellVal=val;
		}
		void Attach(CellStore& rhs)
		{
			CComVariant val;
			rhs.cellVal.Detach(&val);
			*this=rhs;
			cellVal.Attach(&val);
		}
		/*
		BOOL IsNull()const
		{
			return !nStyleID && (VT_NULL==cellVal.vt||VT_EMPTY==cellVal.vt);
		}
		*/
		BOOL IsEmpty()const
		{
			return !nStyleID && !cellType && VT_EMPTY==cellVal.vt;
		}
		void clear()
		{
			nStyleID=0;
			cellType=0;
			cellVal.Clear();
		}
	};
	struct StylePack
	{
		StyleID_t nStyleID;
		ECellStorePosition eStorePos;
		//bool fValid;
		//StylePack():fValid(false)
		//{}
	};
	struct CellTypePack
	{
		BYTE cellType;//ECellType
		ECellStorePosition eStorePos;
		//bool fValid;
		//CellTypePack():fValid(false)
		//{}
	};
	struct CellValPack
	{
		const CComVariant* pVal;
		ECellStorePosition eStorePos;
	};
	class Worksheet;

	enum ECellStoreMask
	{
		ECSM_CELLDATA_VALID=0,
		ECSM_STYLE_VALID=1<<0,
		ECSM_CELLTYPE_VALID=1<<1
	};

	class CMergeCell;
	class CellStorePack
	{
		StylePack		stylePack;		
		CellTypePack	cellTypePack;
		CellValPack	cellValPack;
		const CMergeCell* pMerge;
	public:
		//const CCell cell;
		//const pair<const CellStore*,ECellStorePosition> csPack;
	public:
		CellStorePack():pMerge(NULL)
		{
			stylePack.nStyleID=0;
			cellTypePack.cellType=0;
		}
		//mMaskΪECellStoreMask��λ���
		CellStorePack(const Worksheet* pSheet,const CMergeCell* pMergeCell,CCell cell,const CellStore& cs,ECellStorePosition esp,UINT nMask=0);
		CellStorePack(const CellStorePack& rhs):stylePack(rhs.stylePack)
			,cellTypePack(rhs.cellTypePack),cellValPack(rhs.cellValPack),pMerge(rhs.pMerge)
		{
		}
		//const CellStore& CellStore()const
		//{
		//	return *csPack.first;
		//}
		//ECellStorePosition StorePos()const
		//{
		//	return csPack.second;
		//}

		const CMergeCell* GetMerge()const
		{
			return pMerge;
		}

		CellValPack GetCellValPack()const
		{
			return cellValPack;
		}
		//ȡ�����տ��õķ��
		StylePack GetStylePack()const
		{
			return stylePack;
		}
		StyleID_t GetStyleID()const
		{
			return stylePack.nStyleID;
		}

		template<class TWorksheet>
		const StyleDesc& GetStyle(const TWorksheet* pSheet)const
		{
			return pSheet->Style_GetStyleByID(stylePack.nStyleID);
		}
		CellTypePack GetCellTypePack()const
		{
			return cellTypePack;
		}
		ECellType GetCellType()const
		{
			return (ECellType)cellTypePack.cellType;
		}
	//private:
	//	void CalcStyle(const Worksheet* pSheet,CCell cell);
	//	void CalcCellType(const Worksheet* pSheet,CCell cell);
	};
}//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