
#include "stdafx.h"
#include "FCool.h"
#include "DrawUtils.h"

BEGIN_MESSAGE_MAP(CCoolButton,CButton)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
	ON_WM_ERASEBKGND()
	ON_WM_SETCURSOR()
END_MESSAGE_MAP()


void CCoolButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (!GetSafeHwnd())
		return;

	SetStatus(DOWN);
	//SetCapture();
	
	CButton::OnLButtonDown(nFlags, point);
}


void CCoolButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	if (!GetSafeHwnd())
		return;

	CRect ButtonRect;

	SetStatus(NORMAL);
	//ReleaseCapture();
	Invalidate();
	CButton::OnLButtonUp(nFlags, point);

	GetClientRect(&ButtonRect);
	
	//if (m_PrevStatus == DOWN && ButtonRect.PtInRect(point))
	//	GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),0),(LPARAM) m_hWnd);
}

void CCoolButton::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if (!GetSafeHwnd())
		return;

	GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(GetDlgCtrlID(),0),(LPARAM) m_hWnd);
}

void CCoolButton::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!GetSafeHwnd())
		return;

	CRect ButtonRect;
	TRACKMOUSEEVENT tme;
	
	GetClientRect(&ButtonRect);

	//sprintf(logs,"Button Rect(%d,%d,%d,%d), Mouse Point= (%d,%d)",ButtonRect.left,ButtonRect.right,
	//		ButtonRect.top, ButtonRect.bottom, point.x, point.y);


//	GetParent()->SetWindowText(logs);

	if (m_Status == DOWN) 
	{
		if (ButtonRect.PtInRect(point))
		{
			//SetStatus(DOWN);
		}
		else
		{
			SetStatus(HOVER);
			Invalidate(FALSE);
		}
	}
	else if (m_PrevStatus == DOWN && m_Status == HOVER)
	{
		SetStatus(DOWN);
	}
	else
	{
		//if (IsInRect(ButtonRect,point))
		if (ButtonRect.PtInRect(point))
		{
			//GetParent()->SetWindowText("END HOVER");
			SetStatus(HOVER);
			Invalidate(FALSE);
		}
		else
		{
			//GetParent()->SetWindowText("END NORMAL");
			SetStatus(NORMAL);
			//Invalidate();
		}

		
	}

	


	CButton::OnMouseMove(nFlags, point);


	tme.cbSize = sizeof(TRACKMOUSEEVENT);
	tme.dwFlags = TME_LEAVE;
	tme.hwndTrack = m_hWnd;

	_TrackMouseEvent(&tme);
	

}

void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

	if (!GetSafeHwnd())
		return;

	//CButton::DrawItem(lpDrawItemStruct);
	CDC *pthisDC;
	int bmpId;
		
	pthisDC = CDC::FromHandle(lpDrawItemStruct->hDC);

	switch (m_Status)
	{

		case NORMAL:
			bmpId = normalBmpId_;
			break;
		
		case HOVER:
			bmpId = hoverBmpId_;
		
			break;
		case DOWN:
			bmpId = downBmpId_;
			break;
	}

	DrawUtils::DrawBitmap(pthisDC, bmpId, CPoint(0,0), 0);
}




CCoolButton::CCoolButton(int nIDNormal,int nIDHover, int nIDDown):m_Status(NORMAL)
{
	SetBitmaps(nIDNormal, nIDHover, nIDDown);
	
}

LRESULT CCoolButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
	if (!GetSafeHwnd())
		return 0;

	SetStatus(NORMAL);
	RedrawWindow();
	
	return 0;
}

CCoolButton::CCoolButton()
{

}


void CCoolButton::SetStatus(enum CCoolButton::ButtonStatus bs)
{
	// store status
	//
	m_PrevStatus = m_Status;
	m_Status = bs;

}

BOOL CCoolButton::IsInRect(CRect &rect, CPoint &point)
{
	return (point.x >= 0 && point.x <= (rect.right - rect.left) && point.y >= 0 && point.y <= (rect.bottom - rect.top))? TRUE : FALSE;
}

void CCoolButton::SetBitmaps(int nIDNormal, int nIDHover, int nIDDown)
{
	normalBmpId_ = nIDNormal;
	hoverBmpId_ = nIDHover;
	downBmpId_ = nIDDown;
}


BOOL CCoolButton::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;	
}

BOOL CCoolButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message )
{
	// update cursor
	//
	HCURSOR hCursor;
	
	switch (m_Status) {
	
		case NORMAL:
			hCursor = ::LoadCursor(NULL, IDC_ARROW);
			break;

		case HOVER:
			hCursor = ::LoadCursor(NULL, IDC_ARROW);
			break;

		case DOWN:
			hCursor = ::LoadCursor(NULL, IDC_ARROW);
			break;
	
	}

	::SetCursor(hCursor);

	return TRUE;
}

void CCoolButton::AutoSize()
{
	CSize bmpSize;

	bmpSize = DrawUtils::GetBitmapSize(normalBmpId_);

	SetWindowPos(&CWnd::wndTop, 0, 0, bmpSize.cx, bmpSize.cy, SWP_SHOWWINDOW | SWP_NOMOVE);
}