#include "stdafx.h"
#include "MyButton.h"
#include "ButtonGroup.h"
#include <atlimage.h>
IMPLEMENT_DYNAMIC(CMyButton, CButton)

CMyButton::CMyButton():
btnState(0)
,colorState(0)
,lastColorState(0)
,chooseState(0)
,strInfo("")
{

}

CMyButton::~CMyButton()
{
}

BEGIN_MESSAGE_MAP(CMyButton, CButton)
	ON_CONTROL_REFLECT_EX(BN_CLICKED,OnClicked)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_PAINT()
	ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
END_MESSAGE_MAP()

void CMyButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	CButton::OnMouseMove(nFlags, point);

	CRect rc;
	GetClientRect(&rc);
	if(rc.PtInRect(point))	
	{
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(tme);
		tme.hwndTrack = m_hWnd;
		tme.dwFlags = TME_LEAVE | TME_HOVER;
		tme.dwHoverTime = 1;
		_TrackMouseEvent(&tme);

		if (btnState == 0 && chooseState == 0)
		{
			colorState = 0;//���û�а�����û��ѡ��ʱbtn���ı���ɫ
		}
		else if (btnState == 1)
		{
			colorState = 1;
		}
 	}
 	else
 	{
		colorState = 0;
	}
	if (lastColorState != colorState)
	{
		Invalidate( false );
		lastColorState = colorState;
	}
}

LRESULT CMyButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{	
	if (type == 0)
	{
		colorState = 0;
	}
	else if (type == 1)
	{
	}

	if (lastColorState != colorState)
	{
		Invalidate( false );
		lastColorState = colorState;
	}
	return 0;
}

void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	btnState = 1;
	colorState = 1;
	CButton::OnLButtonDown(nFlags, point);
}

void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	btnState = 0;
	if (type == 0)
	{
		colorState = 0;
	}
	else if (type == 1)
	{
		CRect rc;
		GetClientRect(&rc);
		if(rc.PtInRect(point))
		{
			colorState = 1;
		}
		else
		{
			colorState = 0;
		}
	}

	CButton::OnLButtonUp(nFlags, point);
}

BOOL CMyButton::OnClicked()
{
	pParent->handleClick(index);
	return FALSE;
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	drawButton();
}

void CMyButton::drawButton()
{
	CClientDC dc(this);

	dc.SetBkMode( TRANSPARENT );

	CRect rc;
	GetClientRect(rc);


	CFont BigFont, NozFont, ItemFont;
	CFont *pFont = NULL;

	int nBigFontH = 30;
	int nNozFontH = 19;
	int nItemFoldH = 16;

	if( 0 != NozFont.CreateFont( nNozFontH, 0,0,0, FW_NORMAL ,0,0,0,DEFAULT_CHARSET,
		OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,
		VARIABLE_PITCH|FF_SWISS, _T("΢���ź�") ) )
	{
		pFont = dc.SelectObject(&NozFont);
	}


	CPen bgPen;
	bgPen.CreatePen( PS_SOLID,1,RGB( 255,255,255 ) );

	CPen* pOldPen = dc.SelectObject( &bgPen );


	CBrush brh1;

	if (colorState == 0)
	{
		brh1.CreateSolidBrush(RGB(255,255,255));
	}
	else if (colorState == 1)
	{
		brh1.CreateSolidBrush(RGB(117,197,240));
	}



	CBrush *pOldBrush = dc.SelectObject( &brh1 );


	CBrush brh2;

	 CFont font;
	 font.CreateFont(-MulDiv(int(18),72,72),
		 0,0,0,FW_MEDIUM,0,0,0,GB2312_CHARSET,							
		 OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,PROOF_QUALITY,
		 FIXED_PITCH|FF_SWISS, "΢���ź�" ); 

	 dc.SelectObject(&font);

	 COLORREF color;

	TRIVERTEX        vert[2] ;
	GRADIENT_RECT    gRect;
	vert [0] .x      = rc.left;
	vert [0] .y      = rc.top;
	vert [1] .x      = rc.right;
	vert [1] .y      = rc.bottom;

	if (colorState == 1 || strInfo.Find(_T("��ѡ")) != -1)
	{
		//�� (194,239,150) (118,195,44)
// 		vert [0] .Red    = 0xc200;
// 		vert [0] .Green  = 0xef00;
// 		vert [0] .Blue   = 0x9600;
// 		vert [0] .Alpha  = 0xff00;
// 
// 		vert [1] .Red    = 0x7600;
// 		vert [1] .Green  = 0xc300;
// 		vert [1] .Blue   = 0x2c00;
// 		vert [1] .Alpha  = 0xff00;

		//��2 (15,220,247) (0,87,127)
		vert [0] .Red    = 0x0f00;
		vert [0] .Green  = 0xdc00;
		vert [0] .Blue   = 0xf700;
		vert [0] .Alpha  = 0xff00;

		vert [1] .Red    = 0x0000;
		vert [1] .Green  = 0x5700;
		vert [1] .Blue   = 0x7f00;
		vert [1] .Alpha  = 0xff00;

		brh2.CreateSolidBrush(RGB(0,87,127));
	
		color = RGB(18,71,91);
	}
	else if (colorState == 0)
	{
		if (index == 0)
		{
			//�� (243,196,107) (252,152,0)
			vert [0] .Red    = 0xf300;
			vert [0] .Green  = 0xc400;
			vert [0] .Blue   = 0x6b00;
			vert [0] .Alpha  = 0xff00;

			vert [1] .Red    = 0xfc00;
			vert [1] .Green  = 0x9800;
			vert [1] .Blue   = 0x0000;
			vert [1] .Alpha  = 0xff00;

			brh2.CreateSolidBrush(RGB(252,152,0));
			color = RGB(88,36,27);
		}
		else if (index >= 1 && index <= 2)
		{
			//�� (150,196,235) (39,148,228)
			vert [0] .Red    = 0x9600;
			vert [0] .Green  = 0xc400;
			vert [0] .Blue   = 0xeb00;
			vert [0] .Alpha  = 0xff00;

			vert [1] .Red    = 0x2700;
			vert [1] .Green  = 0x9400;
			vert [1] .Blue   = 0xe400;
			vert [1] .Alpha  = 0xff00;

			brh2.CreateSolidBrush(RGB(51,51,153));
			color = RGB(51,51,153);
		}
		else if (index >= 3 && index <= 5)
		{
			//�� (239,240,113) (231,159,67)
			vert [0] .Red    = 0xef00;
			vert [0] .Green  = 0xf000;
			vert [0] .Blue   = 0x7100;
			vert [0] .Alpha  = 0xff00;

			vert [1] .Red    = 0xe700;
			vert [1] .Green  = 0xc300;
			vert [1] .Blue   = 0x4300;
			vert [1] .Alpha  = 0xff00;

			brh2.CreateSolidBrush(RGB(231,159,67));
			color = RGB(51,47,3);
		}
		else if (index >= 6 && index <= 9)
		{
			//�� (178,202,224) (137,160,175)
			vert [0] .Red    = 0xb200;
			vert [0] .Green  = 0xca00;
			vert [0] .Blue   = 0xe000;
			vert [0] .Alpha  = 0xff00;

			vert [1] .Red    = 0x8900;
			vert [1] .Green  = 0xa000;
			vert [1] .Blue   = 0xaf00;
			vert [1] .Alpha  = 0xff00;

			brh2.CreateSolidBrush(RGB(137,160,175));
			color = RGB(38,45,49);
		}
		else
		{
			//�� 229 169
			vert [0] .Red    = 0xe500;
			vert [0] .Green  = 0xe500;
			vert [0] .Blue   = 0xe500;
			vert [0] .Alpha  = 0xff00;

			vert [1] .Red    = 0xa900;
			vert [1] .Green  = 0xa900;
			vert [1] .Blue   = 0xa900;
			vert [1] .Alpha  = 0xff00;

			brh2.CreateSolidBrush(RGB(169,169,169));
			color = RGB(50,50,50);
		}
	}


	gRect.UpperLeft  = 0;
	gRect.LowerRight = 1;

	GradientFill(dc.m_hDC,vert,2,&gRect,1,GRADIENT_FILL_RECT_V);



	pOldBrush = dc.SelectObject( &brh2 );
	CRgn rgn;
	rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,15,15);
	dc.FrameRgn(&rgn,&brh2,1,1);

	CRgn rgn3;
	rgn3.CreateRoundRectRgn(rc.left-5,rc.top-5,rc.right+5,rc.bottom+5,15,15);
	CBrush brh3(RGB( 51,55,68));
	dc.FrameRgn(&rgn3,&brh3,5,5);


	CRect rcBlank(13,75,162,102);

	CBrush brh4(RGB( 255,255,255));
	pOldBrush = dc.SelectObject( &brh4 );
	if (type == 0)
	{
		dc.RoundRect(rcBlank,CPoint(8,8));
	}

	CBrush brh(RGB( 57,171,229));
	pOldBrush = dc.SelectObject( &brh );
	dc.SetTextColor(color);

	CRect rcStr(rc);
	if (type == 0)
	{
		rcStr.bottom = 74;
		if (strInfo != "")
		{
			CRect rcText(rcBlank);
			rcText.OffsetRect(0,2);
			dc.DrawText(strInfo,rcText,DT_CENTER|DT_VCENTER);
		}
	}
	drawlines(&dc,name,rcStr,14,3,22);


	dc.SelectObject( pOldBrush );
	dc.SelectObject( pOldPen );
	
	bgPen.DeleteObject();
	brh1.DeleteObject();
	brh2.DeleteObject();
	brh.DeleteObject();
	NozFont.DeleteObject();
}

void CMyButton::drawlines(CDC* pDC, CString str, CRect rc, UINT charNumOfLine, UINT linenum, UINT rowHeight)
{
	vector<CString> vStrs;
	CString tmpStr = str;
	while (tmpStr!="" && vStrs.size() < linenum)
	{
		int pos = getDividePos(tmpStr,charNumOfLine);
		CString tmpStr1 = tmpStr.Left(pos);
		vStrs.push_back(tmpStr1);
		if (tmpStr1 == tmpStr)
		{
			tmpStr = "";
		}
		else
		{
			tmpStr = tmpStr.Mid(pos);
		}
	}
	
	UINT headAlign = (rc.bottom - rc.top - vStrs.size() * rowHeight) / 2;
	CRect rc1(rc);
	rc1.top += headAlign;
	rc1.left += 5;
	rc1.right -=5;
	for (size_t i = 0; i < vStrs.size(); i++)
	{
		rc1.bottom = rc1.top + rowHeight;
		pDC->DrawText(vStrs[i],rc1,DT_CENTER|DT_VCENTER);

		rc1.top += rowHeight;
	}
}

int CMyButton::getDividePos(CString str, int num)
{
	if( str.GetLength() > num )
	{
		int numOfChineseChar = 0;
		for ( int i = 0; i < num; i ++ )
		{
			if ( str[i] < 0 || str[i] > 255 )
			{
				numOfChineseChar ++;
			}
		}
		if ( numOfChineseChar % 2 == 0 )
		{
			if (str.GetAt(num-1) == 'm')
			{
				return num - 1;
			}
			else
			{
				return num;
			}

		}
		else
		{
			if (str.GetAt(num-1) == 'm')
			{
				return num - 2;
			}
			else
			{
				return num-1;
			}
		}
	}
	else
	{
		return str.GetLength();
	}

}

void CMyButton::changeColor(bool change)
{
	if (change)
	{
		chooseState = 1;
		colorState = 1;
	}
	else
	{
		chooseState = 0;
		colorState = 0;
	}
 	if (lastColorState != colorState)
 	{
 		Invalidate( false );
 		lastColorState = colorState;
 	}
}

void CMyButton::OnPaint()
{
	CButton::OnPaint();
}