// ColorRangeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ColorRangeCtrl.h"


/////////////////////////////////////////////////////////////////////////////
// CColorRangeCtrl

CColorRangeCtrl::CColorRangeCtrl()
{

}

CColorRangeCtrl::~CColorRangeCtrl()
{
}


BEGIN_MESSAGE_MAP(CColorRangeCtrl, CSliderCtrl)
	//{{AFX_MSG_MAP(CColorRangeCtrl)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdraw)
	ON_WM_CTLCOLOR()
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorRangeCtrl message handlers


void CColorRangeCtrl::OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

	*pResult = 0;
}

HBRUSH CColorRangeCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CSliderCtrl::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CColorRangeCtrl::OnPaint() 
{
	//CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here

CRect rct,trct,crct;
GetChannelRect(&rct);
GetThumbRect(&trct);
GetClientRect(&crct);
rct.top=trct.top;
rct.bottom=trct.bottom;

	CDC *pDC=GetDC();
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(BgdBmp);
	pDC->BitBlt(rct.left,rct.top,rct.Width(),rct.Height(),&memDC,0,0,SRCCOPY);
	memDC.DeleteDC();

int lpos,dif;
dif=GetRangeMax()-GetRangeMin();
	if(dif==0) return;

	lpos=GetPos();
	lpos-=GetRangeMin();
	lpos*=rct.Width();
	lpos/=dif;

	pDC->SelectStockObject(NULL_BRUSH);
	pDC->SelectStockObject(BLACK_PEN);
	pDC->Rectangle(trct);

	pDC->SelectStockObject(WHITE_PEN);
	trct.left++;
	trct.top++;
	trct.bottom--;
	trct.right--;
	pDC->Rectangle(trct);

	ReleaseDC(pDC);	
	ValidateRect(NULL);

	// Do not call CSliderCtrl::OnPaint() for painting messages
}

void CColorRangeCtrl::SetColorRange(COLORREF bot,COLORREF top)
{
bcolor=bot;
tcolor=top;
CRect rct,crct;
GetChannelRect(&rct);
GetClientRect(&crct);
rct.top=crct.top;
rct.bottom=crct.bottom;


	BgdBmp.DeleteObject();

	CDC *pDC=GetDC();
	BgdBmp.CreateCompatibleBitmap(pDC,rct.Width(),rct.Height());
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(BgdBmp);

	BITMAPINFO binf;
	ZeroMemory(&binf,sizeof(binf));
	binf.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
	binf.bmiHeader.biWidth=rct.Width();
	binf.bmiHeader.biHeight=-rct.Height();
	binf.bmiHeader.biPlanes=1;
	binf.bmiHeader.biBitCount=32;
	binf.bmiHeader.biCompression=BI_RGB;
	int res;
	COLORREF *RangeBits=new COLORREF[rct.Width()*rct.Height()];
	int x,y;
	COLORREF col;
	for(x=0;x<rct.Width();x++)
	{
		
		col=RGB(GetBValue(bcolor)+x*(GetBValue(tcolor)-GetBValue(bcolor))/rct.Width(),
				GetGValue(bcolor)+x*(GetGValue(tcolor)-GetGValue(bcolor))/rct.Width(),
				GetRValue(bcolor)+x*(GetRValue(tcolor)-GetRValue(bcolor))/rct.Width());
		for(y=0;y<rct.Height();y++)
			RangeBits[y*rct.Width()+x]=col;
	}
	res=SetDIBitsToDevice(memDC,0,0,rct.Width(),rct.Height(),0,0,0,rct.Height(),RangeBits,&binf,DIB_RGB_COLORS);
	delete RangeBits;
	ASSERT(res!=0);

	memDC.DeleteDC();
	ReleaseDC(pDC);	
	ValidateRect(NULL);


UpdateWindow();

}

void CColorRangeCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	

	CSliderCtrl::OnMouseMove(nFlags, point);
}

void CColorRangeCtrl::OnSize(UINT nType, int cx, int cy) 
{
	CSliderCtrl::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	
}
