// HeaderToolbar.cpp : implementation file
//

#include "stdafx.h"
#include "HeaderToolbar.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHeaderToolbar

CHeaderToolbar::CHeaderToolbar()
{
	//Defaults:
	//Background drawings...
	Start=RGB(128,128,128);
	Background.CreateSolidBrush(Start);
	End=RGB(0,0,0);
	Gradient=FALSE;
	Height=0;

	//Text drawings
	Text="";
	TextColor=RGB(255,255,255);
	ShadowColor=RGB(0,0,0);
	Font="Verdana";
	Size=140;
	Marg=5;
	Placement=DT_LEFT;
}

CHeaderToolbar::~CHeaderToolbar()
{
}

BEGIN_MESSAGE_MAP(CHeaderToolbar, CToolBar)
	//{{AFX_MSG_MAP(CHeaderToolbar)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	ON_WM_ERASEBKGND()
    ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHeaderToolbar message handlers

//*************************************************************************************

BOOL CHeaderToolbar::OnEraseBkgnd(CDC* pDC) 
{
	CFlickerFree dc(pDC);

	if(Gradient==TRUE)
	{
		int r1 = GetRValue(Start);
		int g1 = GetGValue(Start);
		int b1 = GetBValue(Start);
		int r2 = GetRValue(End);
		int g2 = GetGValue(End);
		int b2 = GetBValue(End);
		
		for ( int x = 0; x<DrawRect.Width(); x++) //Draw transparent stuffie.. Thnx to CGSToolbar
		{ 
			dc.FillSolidRect(x, 0, x+1, DrawRect.Height(), RGB(r1 * (DrawRect.Width()-x)/DrawRect.Width() + r2 * x/DrawRect.Width(), 
				g1 * (DrawRect.Width()-x)/DrawRect.Width() + g2 * x/DrawRect.Width(), b1 * (DrawRect.Width()-x)/DrawRect.Width() + b2 * x/DrawRect.Width())); 
		}
	}
	else //If not gradient, use normal fill...
		::FillRect(dc.GetSafeHdc(),DrawRect, Background);

	if(Text.GetLength()!=0) //Text is needed for this kind of work...
	{
		int Move=2;

		CFont DrawFont;
		DrawFont.CreatePointFont(Size, Font, dc);
		dc.SelectObject(&DrawFont);

		//Every text is transparent!
		dc.SetBkMode(TRANSPARENT);

		//HEre we create a shadow rect, so the text looks like it is shadowed...
		CRect TempRect=DrawRect;
		TempRect.top+=Move;
		TempRect.bottom+=Move;
		TempRect.left+=Move+Marg;
		TempRect.right+=Move-Marg;

		dc.SetTextColor(ShadowColor);
		dc.DrawText(Text,TempRect,DT_SINGLELINE|DT_VCENTER|Placement);

		//Now we draw the frontside of the text with the marg...
		TempRect=DrawRect;
		TempRect.left+=Marg;
		TempRect.right-=Marg;

		dc.SetTextColor(TextColor);
		dc.DrawText(Text,TempRect,DT_SINGLELINE|DT_VCENTER|Placement);
	}

	return FALSE;
}

//*************************************************************************************

void CHeaderToolbar::SetDrawRect(CRect Rect)
{
	DrawRect=Rect;
	Height=Rect.Height();

	this->RedrawWindow();
}

//*************************************************************************************

void CHeaderToolbar::OnSize(UINT nType, int cx, int cy) 
{
    CToolBar::OnSize(nType, cx, cy);
	//Get parent so width is correct...
	CWnd *Parent=this->GetParent();

	Parent->GetClientRect(DrawRect);

	//The height must be stored, since there are 0 buttons, the cy = 0
	cx=DrawRect.Width();
	cy=Height+2; //+2 is the frame border... if it is not included the drawing get
                     //transparent. REALLY ugly.

	//Set the new correct rect
	DrawRect.SetRect(0,0,cx,cy); //Set the new rect.
}

//*************************************************************************************

void CHeaderToolbar::SetGradient(BOOL Activate)
{
	Gradient=Activate;
	this->RedrawWindow();
}

void CHeaderToolbar::SetGradientStartColor(COLORREF GradientStart)
{
	Start=GradientStart;

	if(Background.m_hObject!=NULL)
		Background.DeleteObject();

	Background.CreateSolidBrush(Start);

	this->RedrawWindow();
}

void CHeaderToolbar::SetGradientEndColor(COLORREF GradientEnd)
{
	End=GradientEnd;
	this->RedrawWindow();
}

//*************************************************************************************

void CHeaderToolbar::SetWindowText(CString InputText)
{
	Text=InputText;

	this->RedrawWindow();
}

void CHeaderToolbar::GetWindowText(CString &OutputText)
{
	OutputText=Text;

	this->RedrawWindow();
}

//*************************************************************************************

void CHeaderToolbar::SetFont(CString FontName)
{
	if(FontName.GetLength()==0) return;

	Font=FontName;

	this->RedrawWindow();
}

void CHeaderToolbar::SetFontSize(int NewSize)
{
	Size=NewSize;

	this->RedrawWindow();
}

void CHeaderToolbar::SetTextCol(COLORREF Col)
{
	TextColor=Col;

	this->RedrawWindow();
}

void CHeaderToolbar::SetTextShadowCol(COLORREF Col)
{
	ShadowColor=Col;

	this->RedrawWindow();
}

void CHeaderToolbar::SetPlaceMent(int Place)
{
	Placement=Place;

	this->RedrawWindow();
}

//*************************************************************************************

void CHeaderToolbar::RedrawWindow()
{
	//Internal redraw is not possible, the whole frame must be updated...
	if(AfxGetMainWnd()==NULL) return;
	
	(AfxGetMainWnd( ))->RedrawWindow();
}

//*************************************************************************************
