Click here to Skip to main content
15,897,371 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 91.1K   4.5K   81  
A fast and lightweight cell control for displaying tabular data. The cell is a custom control derived from ATL::CWindow.
// MyCell - version 1.0
// Written by Yanxueming <yanxm2003@hotmail.com>
// Copyright (C) 2006-2006
// All rights reserved.
//
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
#pragma once
#include "StyleDesc.h"
namespace mycell{
	template<class T>
	class CellTypeMgr
	{
	public:
		CellTypeMgr()
		{}
		ECellType get_CellType(int row,int col,StyleDesc const& styleDesc)const
		{
			T const* pT=static_cast<T const*>(this);
			//StyleDesc styleDesc;
			//if(pT->GetCellStyle(row,col,styleDesc)){
			//	if(cellTypeNone!=styleDesc.cellType)
			//		return styleDesc.cellType;
			//}
			//map<int,map<int,ECellType> >::const_iterator i1=cellType_.find(row);
			//if(i1!=cellType_.end()){
			//	map<int,ECellType>::const_iterator i2=i1->second.find(col);
			//	if(i2!=i1->second.end())
			//		return i2->second;
			//}
			if(cellTypeNone==styleDesc.cellType){
				ColHeader const& ch=pT->get_ColHeader();
				return ch.get_ColType(col);
			}else
				return styleDesc.cellType;
		}
		void put_CellType(int row,int col,ECellType cellType)
		{
			T* pT=static_cast<T*>(this);
			_ASSERT(cellType<ECT_MAX);
			ColHeader& ch=pT->get_ColHeader();
			if(ch.get_ColType(col)!=cellType){
				StyleDesc styleDesc;
				pT->GetCellStyle(row,col,styleDesc);
				styleDesc.cellType=cellType;
				pT->SetCellStyle(row,col,styleDesc);
			}
		}
	//protected:
	//	void OnRowDelete(int beg,int end);
	//	void OnColDelete(int beg,int end);
	//	void OnColInsertFront(int col,int nCount);
	//	void OnRowInsertFront(int row,int nCount);
	//private:
	//	map<int,map<int,ECellType> > cellType_;
	};
	/*
	//---------------------------impl---------------------------
	template<class T>
	void CellTypeMgr<T>::OnRowInsertFront(int row,int nCount)
	{
		typedef map<int,ECellType> second_t;
		vector<pair<int,second_t> > vec;     
		for(map<int,second_t>::iterator it=cellType_.begin();it!=cellType_.end();)
		{
			if(it->first>=row){
				vec.push_back(make_pair(it->first+nCount,it->second));
				it=cellType_.erase(it);
				continue;
			}
			++it;
		}			    
		for(vector<pair<int,second_t> >::const_iterator _i=vec.begin();_i!=vec.end();++_i)
		{
			cellType_.insert(*_i);
		}
	}
	template<class T>
	void CellTypeMgr<T>::OnColInsertFront(int col,int nCount)
	{
		typedef map<int,ECellType> second_t;
		vector<pair<int,ECellType> > vec;     
		for(map<int,map<int,ECellType> >::iterator it=cellType_.begin();it!=cellType_.end();++it)
		{
			for(second_t::iterator i=it->second.begin();i!=it->second.end();)
			{
				int const _col=i->first;
				if(_col>=col){
					vec.push_back(make_pair(_col+nCount,i->second));
					i=it->second.erase(i);
					continue;
				}				  
				++i;
			}
			for(vector<pair<int,ECellType> >::const_iterator _i=vec.begin();_i!=vec.end();++_i)
			{
				it->second.insert(*_i);
			}
			vec.clear();
		}				   
	}
	template<class T>
	void CellTypeMgr<T>::OnRowDelete(int beg,int end)
	{
		_ASSERT(end>=beg);
		int const cnt=end-beg+1;
		vector<pair<int,map<int,ECellType> > > vec;
		for(map<int,map<int,ECellType> >::iterator it=cellType_.begin();it!=cellType_.end();)
		{
			if(it->first>=beg && it->first<=end){
				cellType_.erase(it);
				it=cellType_.begin();
				continue;
			}else if(it->first>end){
				vec.push_back(make_pair(it->first-cnt,it->second));
				cellType_.erase(it);
				it=cellType_.begin();
				continue;
			}
			++it;
		}				   
		for(vector<pair<int,map<int,ECellType> > >::const_iterator it=vec.begin();it!=vec.end();++it)
		{
			cellType_.insert(*it);
		}
	}
	template<class T>
	void CellTypeMgr<T>::OnColDelete(int beg,int end)
	{
		_ASSERT(end>=beg);      
		int const cnt=end-beg+1;
		vector<pair<int,ECellType> > vec;
		for(map<int,map<int,ECellType> >::iterator it=cellType_.begin();it!=cellType_.end();++it)
		{
			for(map<int,ECellType>::iterator i=it->second.begin();i!=it->second.end();)
			{
				if(i->first>=beg && i->first<=end){
					i=it->second.erase(i);
					continue;
				}else if(i->first>end){
					vec.push_back(make_pair(i->first-cnt,i->second));
					i=it->second.erase(i);
					continue;
				}
				++i;
			}
			for(vector<pair<int,ECellType> >::const_iterator _i=vec.begin();_i!=vec.end();++_i)
			{
				it->second.insert(*_i);
			}
			vec.clear();
		}				   
	}
	*/
}//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