// GradientStatic.cpp : implementation file
//

#include "stdafx.h"
#include "GradientStatic.h"
#include "MemDC.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGradientStatic

CGradientStatic::CGradientStatic()
{
	m_iLeftSpacing = 10;
	clLeft = GetSysColor(COLOR_ACTIVECAPTION);
	clRight = GetSysColor(COLOR_BTNFACE);
	clText = GetSysColor(COLOR_CAPTIONTEXT);
	
	m_bCenter = FALSE;

	hinst_msimg32 = LoadLibrary( "msimg32.dll" );
	m_bCanDoGradientFill = FALSE;
	pFont = NULL;
	
	if(hinst_msimg32)
	{
		m_bCanDoGradientFill = TRUE;		
		dllfunc_GradientFill = ((LPFNDLLFUNC1) GetProcAddress( hinst_msimg32, "GradientFill" ));
	}


}

CGradientStatic::~CGradientStatic()
{
	FreeLibrary( hinst_msimg32 );
}


BEGIN_MESSAGE_MAP(CGradientStatic, CStatic)
	//{{AFX_MSG_MAP(CGradientStatic)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGradientStatic message handlers

void CGradientStatic::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	GetClientRect(&rect);

	CMemDC	mDC( &dc, rect );		

	if(m_bCanDoGradientFill)
	{
	
		TRIVERTEX rcVertex[2];
		rect.right--; // exclude this point, like FillRect does 
		rect.bottom--;
		rcVertex[0].x=rect.left;
		rcVertex[0].y=rect.top;
		rcVertex[0].Red=GetRValue(clLeft)<<8;	// color values from 0x0000 to 0xff00 !!!!
		rcVertex[0].Green=GetGValue(clLeft)<<8;
		rcVertex[0].Blue=GetBValue(clLeft)<<8;
		rcVertex[0].Alpha=0x0000;
		rcVertex[1].x=rect.right; 
		rcVertex[1].y=rect.bottom;
		rcVertex[1].Red=GetRValue(clRight)<<8;
		rcVertex[1].Green=GetGValue(clRight)<<8;
		rcVertex[1].Blue=GetBValue(clRight)<<8;
		rcVertex[1].Alpha=0;
		GRADIENT_RECT rect;
		rect.UpperLeft=0;
		rect.LowerRight=1;
		
		// fill the area 
		//dllfunc_GradientFill( mDC,rcVertex,2,&rect,1,GRADIENT_FILL_RECT_H);
		
		//if Gradient Fil works - let's use font color defined by user
		::SetTextColor(mDC,clText);
	}
	else
	{
		//there is no gradient, so let's use standart color
		::SetTextColor(mDC,GetSysColor(COLOR_BTNTEXT));
	}

	HFONT hfontOld;
	if(pFont)
		hfontOld = (HFONT)SelectObject(mDC.m_hDC, (HFONT)pFont->m_hObject);

	::SetBkMode(mDC, TRANSPARENT);
	GetClientRect(&rect);


	if(m_bCenter)
		::DrawText(mDC, m_sTEXT, -1, &rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
	else
	{
		rect.left+=m_iLeftSpacing;
		::DrawText(mDC, m_sTEXT, -1, &rect, DT_SINGLELINE|DT_VCENTER|DT_LEFT);
	}

	if(pFont)
		::SelectObject(mDC.m_hDC, hfontOld);
}

void CGradientStatic::SetWindowText(LPCSTR lpszString)
{
	m_sTEXT = lpszString;
	Invalidate(TRUE);
}

CString CGradientStatic::GetWindowText()
{
	return m_sTEXT;

}

void CGradientStatic::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CStatic::OnLButtonDown(nFlags, point);
}
