// IADWCBt.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "IADWCBt.hpp"

#include "IAD_Res.h"

#define IAD_C_BUTTON_FIRST_ID							13000
#define IAD_C_BUTTON_SPACE_AROUND_TEXT		10

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// IAD_Color_Button
IMPLEMENT_DYNAMIC(IAD_Color_Button, CButton)

BEGIN_MESSAGE_MAP(IAD_Color_Button, CButton)
	//{{AFX_MSG_MAP(IAD_Color_Button)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
IAD_Color_Button::IAD_Color_Button(COLORREF *_pclColRef,
																	 CString _csText,
																	 long _lButtonNumber)
{
	m_pri_pclColRef = _pclColRef;
	m_pri_csText = _csText;
	m_pri_lButtonNumber = _lButtonNumber;

	m_pri_bIsDragging = FALSE;

	m_pri_hcDragCursor = AfxGetApp()->LoadCursor(IDC_CURSOR_DRAG_COLOR);
	m_pri_hcDragInvalidCursor = AfxGetApp()->LoadCursor(IDC_CURSOR_DRAG_INVALID_COLOR);

	//Computes color for Text
	BYTE ucRed = GetRValue(*m_pri_pclColRef);
	BYTE ucGreen = GetGValue(*m_pri_pclColRef);
	BYTE ucBlue = GetBValue(*m_pri_pclColRef);
	ucRed = (ucRed + 128) % 255;
	ucGreen = (ucGreen + 128) % 255;
	ucBlue = (ucBlue + 128) % 255;
	m_pri_clColRefForText = RGB(ucRed, ucGreen, ucBlue);
}

//***************************************************************************
IAD_Color_Button::~IAD_Color_Button()
{

}

/////////////////////////////////////////////////////////////////////////////
// IAD_Color_Button message handlers

//***************************************************************************
CSize IAD_Color_Button::m_pub_fn_csCreate(CWnd *_pclParentWnd,
																					CRect _crCreationRect)
{
	BOOL bReturn = CButton::Create(	"",
																	WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON /*| BS_OWNERDRAW*/,
																	_crCreationRect,
																	_pclParentWnd,
																	IAD_C_BUTTON_FIRST_ID + m_pri_lButtonNumber);
	
	if ( bReturn )
	{
		CRect crDestRect = _crCreationRect;
		CClientDC dc(this);
		m_pri_pclFont = _pclParentWnd->GetFont();
		dc.SelectObject(m_pri_pclFont);
		CSize csTextSize = dc.GetTextExtent(m_pri_csText);
		crDestRect.right = crDestRect.left + csTextSize.cx + IAD_C_BUTTON_SPACE_AROUND_TEXT;
		crDestRect.bottom = crDestRect.top + csTextSize.cy + IAD_C_BUTTON_SPACE_AROUND_TEXT;

		MoveWindow(crDestRect);
	
		return CSize(crDestRect.Width(), crDestRect.Height());
	}

	return CSize(0,0);
}

//***************************************************************************
void IAD_Color_Button::OnPaint() 
{	
	CButton::OnPaint();
	
	CClientDC dc(this); // device context for painting
	//CPaintDC dc(this); // device context for painting

	CClientDC cdc(this);
	CRect crClientRect;
	GetClientRect(crClientRect);
	crClientRect.InflateRect(-4, -4);
	cdc.FillSolidRect(crClientRect, *m_pri_pclColRef);
	
	cdc.SetTextColor(m_pri_clColRefForText);
	cdc.SetBkMode(TRANSPARENT);
	cdc.SelectObject(m_pri_pclFont);

	cdc.DrawText(m_pri_csText, crClientRect, DT_CENTER);
	
	// Do not call CButton::OnPaint() for painting messages
}

//***************************************************************************
void IAD_Color_Button::OnClicked() 
{
	CColorDialog ColorDial(*m_pri_pclColRef, CC_ANYCOLOR | CC_FULLOPEN | CC_RGBINIT /*| CC_SHOWHELP*/, this);
	
	if ( ColorDial.DoModal() == IDOK )
		*m_pri_pclColRef = ColorDial.GetColor();

	InvalidateRect(NULL);
}

//***************************************************************************
void IAD_Color_Button::OnLButtonDown(UINT nFlags, CPoint point) 
{

	m_pri_bIsDragging = TRUE;
	SetCapture();
	m_pri_hcPreviousCursor = ::SetCursor(m_pri_hcDragCursor);

	CButton::OnLButtonDown(nFlags, point);
	InvalidateRect(NULL);
}

//***************************************************************************
void IAD_Color_Button::OnMouseMove(UINT nFlags, CPoint point) 
{

	if ( m_pri_bIsDragging )
	{
		CPoint cpRightPoint = point ;
		ClientToScreen(&cpRightPoint);
		CWnd *pclTargettedWindow = WindowFromPoint(cpRightPoint);

		if ( pclTargettedWindow != NULL )
		{
			if ( pclTargettedWindow->IsKindOf( RUNTIME_CLASS(IAD_Color_Button)) )
				::SetCursor(m_pri_hcDragCursor);
			else
				::SetCursor(m_pri_hcDragInvalidCursor);
		}
		else
			::SetCursor(m_pri_hcDragInvalidCursor);
	}

	CButton::OnMouseMove(nFlags, point);
	if ( m_pri_bIsDragging )
		InvalidateRect(NULL);
}

//***************************************************************************
void IAD_Color_Button::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if ( m_pri_bIsDragging )
	{
		//Gets window under cursor
		CPoint cpRightPoint = point ;
		ClientToScreen(&cpRightPoint);
		CWnd *pclTargettedWindow = WindowFromPoint(cpRightPoint);

		if ( pclTargettedWindow != NULL )
			if ( pclTargettedWindow->IsKindOf( RUNTIME_CLASS(IAD_Color_Button)) )
			{
				IAD_Color_Button *pclTargettedButton = (IAD_Color_Button *)pclTargettedWindow;

				pclTargettedButton->m_pub_fn_vSetColor(m_pub_fn_colrefGetColor());
				pclTargettedButton->InvalidateRect(NULL);
			}
		
		::SetCursor(m_pri_hcPreviousCursor);
		m_pri_hcPreviousCursor = NULL;
		m_pri_bIsDragging = FALSE;

		ReleaseCapture();
	}

	CButton::OnLButtonUp(nFlags, point);
	InvalidateRect(NULL);
}

//***************************************************************************
void IAD_Color_Button::m_pub_fn_vSetColor(COLORREF _Colref)
{
	*m_pri_pclColRef = _Colref;	
}

//***************************************************************************
COLORREF IAD_Color_Button::m_pub_fn_colrefGetColor()
{
	return *m_pri_pclColRef;
}

//***************************************************************************
void IAD_Color_Button::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	
}

