Click here to Skip to main content
15,886,518 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
// -----------------------------------------------------------------------*/
#pragma once
#include "ResourcelessDlg.h"
#include "PropertyPageBase.h"
#include "ColorButton.h"
#include "FontPreviewCombo.h"
namespace mycell{
	using namespace style;
	class FontPropertyPage : public xm::ui::CResourcelessDlg<FontPropertyPage>
		,public PropertyPageBase<FontPropertyPage>
	{
		typedef xm::ui::CResourcelessDlg<FontPropertyPage> baseWnd;
		enum RIDS
		{
			__IDC_BTNCOLOR=1000,
			__IDC_FONTDEMO=1001,
			__IDC_COMBO_FONTNAME=1002,
			__IDC_COMBO_FONTSIZE=1003,
			__IDC_CHECK_BOLD=1004,
			__IDC_CHECK_ITALIC=1005,
			__IDC_CHECK_UNDERLINE=1006,
			__IDC_CHECK_STRIKEOUT=1007,
		};
		CFontPreviewComboBox fontName_;
		//CComPtr<IFont> pFont_;
	public:
		FontPropertyPage():nDirtyMask_(0)
		{
			m_size.cx=400;
			m_size.cy=300;
			m_bstrTitle=L"����";
			CFontHandle font((HFONT)GetStockObject(DEFAULT_GUI_FONT));
			LOGFONT lf;
			font.GetLogFont(&lf);
			style_.Font_GetLogFont()=lf;
			/*
			FONTDESC _fontDesc=
			{
				sizeof(FONTDESC),OLESTR("����"),
				FONTSIZE(9),FW_NORMAL,
				GB2312_CHARSET,FALSE,FALSE,FALSE
			};
			HRESULT hr=OleCreateFontIndirect(&_fontDesc,IID_IFont,(void**)&pFont_);
			_ASSERT(SUCCEEDED(hr));
			*/
		}
		~FontPropertyPage()
		{
		}
		//BEGIN_COM_MAP(FontPropertyPage)
		//	COM_INTERFACE_ENTRY(IPropertyPage)
		//END_COM_MAP()
		BEGIN_MSG_MAP(FontPropertyPage)
			MESSAGE_HANDLER(WM_DRAWITEM ,OnDrawItem)
			MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)
			MESSAGE_HANDLER(WM_COMMAND,OnCommand)
			NOTIFY_CODE_HANDLER(CPN_SELENDOK,OnBtnColor)
			CHAIN_MSG_MAP(baseWnd)
			REFLECT_NOTIFICATIONS()
		END_MSG_MAP()
		// Dialog function
		void CreateDlg()
		{
			CreateDlgTemplate("�Ի���", DS_SETFONT|WS_CHILD, 0, 0, 0, 186, 100, 9, "����", "", "");
			// Start generated dialog
			//CreateDlgTemplate("", 0, 0, 0, 0, 199, 178, | DS_FIXEDSYS | WS_CHILD | WS_SYSMENU
			//FONT 8, "MS Shell Dlg", "", "");
			AddButton("��ɫ", 0|BS_DEFPUSHBUTTON, 0, 122, 80, 57, 14, __IDC_BTNCOLOR);
			AddButton("MyCell Font", BS_OWNERDRAW|WS_TABSTOP, 0, 7, 121,  180, 50, __IDC_FONTDEMO);
			AddStatic("����(&F):", 0, 0, 11, 9, 45, 8, IDC_STATIC);
			AddCombo("", CBS_DROPDOWNLIST|/*CBS_SORT|*/WS_VSCROLL|WS_TABSTOP, 0, 7, 25, 116, 146, __IDC_COMBO_FONTNAME);
			AddStatic("��С(&S):", 0, 0, 139, 9, 45, 8, IDC_STATIC);
			AddCombo("", CBS_DROPDOWNLIST|/*CBS_SORT|*/WS_VSCROLL|WS_TABSTOP, 0, 135, 25, 53, 146, __IDC_COMBO_FONTSIZE);
			AddButton("��", 0 | BS_GROUPBOX, 0, 7, 49, 180, 56, IDC_STATIC);
			AddDlgItem("����", BS_AUTOCHECKBOX|WS_TABSTOP, 0,  20, 62, 32, 10, __IDC_CHECK_BOLD, "Button", 0, NULL);
			AddDlgItem("�", BS_AUTOCHECKBOX|WS_TABSTOP, 0,  20, 83, 32, 10, __IDC_CHECK_ITALIC, "Button", 0, NULL);
			AddDlgItem("�»���", BS_AUTOCHECKBOX|WS_TABSTOP, 0, 73, 62, 40, 10, __IDC_CHECK_UNDERLINE, "Button", 0, NULL);
			AddDlgItem("ɾ����", BS_AUTOCHECKBOX|WS_TABSTOP, 0, 73, 83, 40, 10, __IDC_CHECK_STRIKEOUT, "Button", 0, NULL);
			AddStatic("ʾ���ı�:", 0, 0, 7, 110, 36, 8, IDC_STATIC);
			// End generated dialog
		}
	public:
		STDMETHOD(Apply)(void)
		{
			//ATLTRACE(atlTraceControls,2,_T("PropertyPageBase::Apply\n"));
			Ranges rngs;
			pSheet_->get_Selection(rngs);
			vector<CCellRange>& vec=rngs.vec_;
			const LOGFONT& lf=style_.Font_GetLogFont();
			const ColorTable::TIndex clrIdx=style_.Font_GetColorIndex();
			for(vector<CCellRange>::const_iterator it=vec.begin();it!=vec.end();++it)
			{
				/*
				for(int r=max(0,it->TopRow());r<=it->BottomRow();++r){
					for(int c=max(0,it->LeftCol());c<=it->RightCol();++c){
						const CCell cell(r,c);
						//pSheet_->SetBottomBorder(r,c,scratch_.bdrInsideHorizontal_);
						style::StyleDesc style=pSheet_->Style_GetCellStyle(cell);
						//style.Font_GetLogFont()=style_.Font_GetLogFont();
						Style_CopyFontProperties(style,style_);
						pSheet_->Style_SetCellStyle(cell,style);
						//pSheet_->SetCellFont(r,c,fontDesc_);
						//pSheet_->InvalidateCell(r,c);
					}
				}
				*/
				pSheet_->Range_SetFont(*it,lf,clrIdx,nDirtyMask_);
				pSheet_->InvalidateCellRange(&*it);
			}
			//pSheet_->RedrawWindow();
			return S_OK;
		}
	private:
		//Scratch scratch_;
		//BorderSelectorWindow bsw_;
		//typedef xm::ui::CColorPopup ColorPopup_t;
		//typedef xm::ui::CColorButton<ColorPopup_t> ColorButton_t;
		//typedef CColorButton ColorButton_t;
		ColorButton_t colorBtn_;
		CComboBox	cbFontSize_;
		///FontDesc fontDesc_;
		style::StyleDesc style_;
		UINT nDirtyMask_;//EFontSetMask
	private:
		LRESULT OnInitDialog(UINT,WPARAM,LPARAM,BOOL&)
		{
			fontName_.SubclassWindow(GetDlgItem(__IDC_COMBO_FONTNAME));
			fontName_.SetCurSel(fontName_.FindString(-1,_T("����")));
			cbFontSize_=GetDlgItem(__IDC_COMBO_FONTSIZE);

			cbFontSize_.AddString("8"); cbFontSize_.AddString("9");cbFontSize_.AddString("10");
			cbFontSize_.AddString("11");cbFontSize_.AddString("12");cbFontSize_.AddString("14");
			cbFontSize_.AddString("16");cbFontSize_.AddString("18");cbFontSize_.AddString("20");
			cbFontSize_.AddString("22");cbFontSize_.AddString("24");cbFontSize_.AddString("26");
			cbFontSize_.AddString("28");cbFontSize_.AddString("36");cbFontSize_.AddString("72");
			cbFontSize_.SetCurSel(2);


			colorBtn_.SubclassWindow(GetDlgItem(__IDC_BTNCOLOR));
			ColorButton_t::ColorPopup_t& cp=colorBtn_.GetColorPopup();
			cp.SetDefaultText(_T("�Զ�"));
			//cp.SetCustomText(_T(""));
			//pair<ColorEntry*,int> p=mycell::ColorTable::GetColorTable();
			//cp.SetColourTable((ColorPopup_t::ColourTableEntry*)p.first,p.second);

			//SetDirty(TRUE);

			Ranges rngs;
			vector<CCellRange>& vec=rngs.vec_;
			pSheet_->get_Selection(rngs);
			if(!vec.empty()){
				const StyleDesc style=pSheet_->Style_GetCellStyle(vec[0].TopLeft());//TopRow(),vec[0].LeftCol());
				//fontDesc_=style.fontDesc;
				//style::StyleDesc::Font_t* pFont=style.GetFontPtr();
				const LOGFONT& logFont=style.Font_GetLogFont();
				//style_.Font_GetLogFont()=logFont;
				Style_CopyFontProperties(style_,style,EFSM_FONT_ALL);

				fontName_.SetCurSel(fontName_.FindString(-1,logFont.lfFaceName));
				CString str;
				str.Format(_T("%d"),logFont.lfHeight/*pFont->GetHeight()*/);
				int ifind=cbFontSize_.FindString(-1,str);
				if(ifind>=0)
					cbFontSize_.SetCurSel(ifind);
				CButton btn;
				if(style.Font_IsBold()){
					btn=GetDlgItem(__IDC_CHECK_BOLD);	
					btn.SetCheck(BST_CHECKED);
				}
				if(/*pFont->IsItalic()*/logFont.lfItalic){
					btn=GetDlgItem(__IDC_CHECK_ITALIC);	
					btn.SetCheck(BST_CHECKED);
				}
				if(/*pFont->IsUnderline()*/logFont.lfUnderline){
					btn=GetDlgItem(__IDC_CHECK_UNDERLINE);	
					btn.SetCheck(BST_CHECKED);
				}
				if(/*pFont->IsStrikethrough()*/logFont.lfStrikeOut){
					btn=GetDlgItem(__IDC_CHECK_STRIKEOUT);	
					btn.SetCheck(BST_CHECKED);
				}
				colorBtn_.SetColor(style.Font_GetColor()/*pFont->GetColor()*/);

				//if(vec[0].BottomRow() == vec[0].TopRow()){
				//	scratch_.hasInsideHorizontal_=FALSE;
				//	CWindow wnd(GetDlgItem(__IDC_BUTTON5));
				//	wnd.EnableWindow(FALSE);
				//}
				//if(vec[0].RightCol() == vec[0].LeftCol()){
				//	scratch_.hasInsideVertical_=FALSE;
				//	CWindow wnd(GetDlgItem(__IDC_BUTTON9));
				//	wnd.EnableWindow(FALSE);
				//}
				//if(!scratch_.hasInsideHorizontal_ && !scratch_.hasInsideVertical_){
				//	CWindow wnd(GetDlgItem(__IDC_BUTTON3));
				//	wnd.EnableWindow(FALSE);
				//}
			}
			AppliyFont();
			//RECT rc={169, 30, 78, 90};
			//bsw_.Create(m_hWnd,&rc,NULL,WS_VISIBLE|WS_CHILD);
			return 0;
		}
		LRESULT OnCommand(UINT,WPARAM wParam,LPARAM,BOOL&)
		{
			WORD		const wID=LOWORD(wParam);
			switch(wID)
			{
			case __IDC_BTNCOLOR:
				nDirtyMask_|=EFSM_FONT_COLOR;
				break;
			case __IDC_COMBO_FONTSIZE:
				nDirtyMask_|=EFSM_FONT_SIZE;
				break;
			case __IDC_CHECK_BOLD:
				nDirtyMask_|=EFSM_FONT_BOLD;
				break;
			case __IDC_CHECK_ITALIC:
				nDirtyMask_|=EFSM_FONT_ITALIC;
				break;
			case __IDC_CHECK_UNDERLINE:
				nDirtyMask_|=EFSM_FONT_UNDERLINE;
				break;
			case __IDC_CHECK_STRIKEOUT:
				nDirtyMask_|=EFSM_FONT_STRIKEOUT;
				break;
			case __IDC_COMBO_FONTNAME:
				if(HIWORD(wParam)==CBN_SELCHANGE){
					nDirtyMask_|=EFSM_FONT_NAME;
					break;
				}
			default:
				return 0;
			}
			CWindow wnd(GetDlgItem(__IDC_FONTDEMO));
			RECT rc;
			wnd.GetClientRect(&rc);
			wnd.InvalidateRect(&rc,FALSE);
			AppliyFont();
			SetDirty(TRUE);
			return 0;
		}
		LRESULT OnBtnColor(int, LPNMHDR,BOOL& bHandled)
		{
			CWindow wnd(GetDlgItem(__IDC_FONTDEMO));
			RECT rc;
			wnd.GetClientRect(&rc);
			wnd.InvalidateRect(&rc,FALSE);
			style_.Font_SetColorIndex(ColorTable::Instance().find_color(colorBtn_.GetColor()));
			nDirtyMask_|=EFSM_FONT_COLOR;
			AppliyFont();
			SetDirty(TRUE);
			return 0;
		}
		void AppliyFont()
		{
			LPCTSTR lpsz=fontName_.GetSelFontName();
			//FontDesc fd;
			//StyleDesc::Font_t* pFont=style_.GetFontPtr();
			LOGFONT& lf=style_.Font_GetLogFont();
			//pFont->SetName(lpsz);
			memcpy(lf.lfFaceName,lpsz,lstrlen(lpsz)+1);

			//CY cySize;
			//cySize.Hi=0;
			//cySize.Lo=GetDlgItemInt(__IDC_COMBO_FONTSIZE)*10000;
			lf.lfHeight=GetDlgItemInt(__IDC_COMBO_FONTSIZE);
			CButton btn=GetDlgItem(__IDC_CHECK_BOLD);
			style_.Font_SetBold(BST_CHECKED==btn.GetCheck());
			btn=GetDlgItem(__IDC_CHECK_ITALIC);
			lf.lfItalic=BST_CHECKED==btn.GetCheck();
			btn=GetDlgItem(__IDC_CHECK_UNDERLINE);
			lf.lfUnderline=BST_CHECKED==btn.GetCheck();
			btn=GetDlgItem(__IDC_CHECK_STRIKEOUT);
			lf.lfStrikeOut=BST_CHECKED==btn.GetCheck();
			//pFont->AssignToIFont(pFont_);
		}
		LRESULT OnDrawItem(UINT,WPARAM wID,LPARAM lParam,BOOL& bHandled)
		{
			LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) lParam;
			if(__IDC_FONTDEMO==wID){
				CDCHandle dc(lpdis->hDC);
				dc.SetTextColor(colorBtn_.GetColor());

				//FontDesc fd;
				//fd.name=lpsz;
				//fd.cySize=GetDlgItemInt(__IDC_COMBO_FONTSIZE);
				//CButton btn=GetDlgItem(__IDC_CHECK_BOLD);
				//fd.SetBold(BST_CHECKED==btn.GetCheck());
				//btn=GetDlgItem(__IDC_CHECK_ITALIC);
				//fd.SetItalic(BST_CHECKED==btn.GetCheck());
				//btn=GetDlgItem(__IDC_CHECK_UNDERLINE);
				//fd.SetUnderline(BST_CHECKED==btn.GetCheck());
				//btn=GetDlgItem(__IDC_CHECK_STRIKEOUT);
				//fd.SetStrikethrough(BST_CHECKED==btn.GetCheck());
				CFontHandle hFont=style_.Font_GetHFont(lpdis->hDC);
				//pFont_->Font_GetHFont(&hFont);
				HFONT hOldFont=dc.SelectFont(hFont);

				dc.DrawEdge(&lpdis->rcItem,BDR_SUNKENINNER,BF_LEFT|BF_TOP);
				dc.DrawEdge(&lpdis->rcItem,BDR_SUNKENOUTER,BF_RIGHT|BF_BOTTOM);
				CRect rc=lpdis->rcItem;
				rc.DeflateRect(2,2);
				CBrush br(CreateSolidBrush(RGB(0xff,0xff,0xff)));
				dc.FillRect(&rc,br);
				POINT ptCenter={rc.left+(rc.Width()>>1),rc.top+(rc.Height()>>1)};
				dc.SetBkMode(TRANSPARENT);
				LPCTSTR lpsz=fontName_.GetSelFontName();
				int const nCount=lstrlen(lpsz);
				dc.DrawText(lpsz,nCount,&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
				dc.SelectFont(hOldFont);
			}else{
				bHandled=FALSE;
			}
			//DrawPushButton(lpdis);
			//Draw_focus_box<> draw_focus_box;
			return 0;
		}
	};

}//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