#include <common/odlstchk.hpp>
#include <common/drawitem.hpp>
#include <common/measure.hpp>
#include <common/purebmp.hpp>

OwnerDrawListCheck::OwnerDrawListCheck(GUIWindow &parentWnd,HWND hControlWnd,UINT controlID,const PureBitmap &selected,const PureBitmap &unselected)
: OwnerDrawList(parentWnd,hControlWnd,controlID), mRGBGray(192,192,192), mGrayBrush(mRGBGray), 
  mDisabledBrush(RGBColor(128,128,128))
{
	mPureBitmaps.insert(&selected);
  mPureBitmaps.insert(&unselected);
}

OwnerDrawListCheck::OwnerDrawListCheck(GUIWindow &parentWnd,const Rect &initRect,int controlID,const PureBitmap &selected,const PureBitmap &unselected,DWORD style)
: OwnerDrawList(parentWnd,initRect,controlID,style), mRGBGray(192,192,192), mGrayBrush(mRGBGray),
  mDisabledBrush(RGBColor(128,128,128))
{
	mPureBitmaps.insert(&selected);
  mPureBitmaps.insert(&unselected);
}

OwnerDrawListCheck::~OwnerDrawListCheck()
{
}

OwnerDrawListCheck &OwnerDrawListCheck::operator=(const OwnerDrawListCheck &/*someOwnerDrawListCheck*/)
{  // private implementation
  return *this;
}

// **** virtuals

LPARAM OwnerDrawListCheck::handleControlColor(PureDevice &/*pureDevice*/,Control &/*wndListBox*/)
{
  return (LPARAM)(GDIObj)mGrayBrush;
}

WORD OwnerDrawListCheck::handleDraw(const DrawItem &drawItem)
{
	if(!isOkay()||-1==drawItem.itemID()||DrawItem::ListBox!=drawItem.controlType())return FALSE;
	PureDevice pureDevice(drawItem.deviceContext());
	pureDevice.setBkColor(mRGBGray);
  switch(drawItem.itemAction())
	{
		case DrawItem::Select :
		case DrawItem::DrawEntire :
		case DrawItem::Focus :
			drawEntire(drawItem);
			break;
	}
  return TRUE;
}

WORD OwnerDrawListCheck::handleMeasureItem(MeasureItem &measureItem)
{
  measureItem.itemHeight(20);
	return TRUE;
}

void OwnerDrawListCheck::drawEntire(const DrawItem &drawItem)
{
	PureDevice pureDevice(drawItem.deviceContext());
	PureDevice memDevice;
	String stringData;
	TEXTMETRIC textMetric;
	Rect drawRect;
	SIZE sizeExtent;

	if(!isOkay())return;
  stringData.reserve(256);
	drawRect=drawItem.rectItem();
	memDevice.compatibleDevice(pureDevice);
	PureBitmap &pureBitmap=(((UINT)drawItem.itemState())&ODS_SELECTED?mPureBitmaps[BmSel]:mPureBitmaps[BmUnSel]);
	sendMessage(drawItem.hwndItem(),LB_GETTEXT,drawItem.itemID(),(LPARAM)(LPSTR)stringData);
  if(stringData.isNull())::MessageBeep(0);
	::GetTextMetrics(drawItem.deviceContext(),&textMetric);
	::GetTextExtentPoint32(drawItem.deviceContext(),(LPSTR)stringData,stringData.length(),&sizeExtent);
	if(drawItem.itemState()&ODS_DISABLED)
	{
	  ::GrayString(drawItem.deviceContext(),(HBRUSH)(GDIObj)mDisabledBrush,(GRAYSTRINGPROC)0,(LPARAM)(LPSTR)stringData,stringData.length(),pureBitmap.width()+6,(drawRect.bottom()+drawRect.top()-textMetric.tmHeight)/2,0,0);
	  memDevice.select(pureBitmap,TRUE);
    Rect stretchRect(drawRect);
		stretchRect.right(pureBitmap.width());
		stretchRect.bottom(sizeExtent.cy);
		pureDevice.stretchBlt(stretchRect,memDevice,Rect(0,0,pureBitmap.width(),pureBitmap.height()),PureDevice::MergeCopy);
		memDevice.select(pureBitmap,FALSE);
	}
	else 
	{
	  ::TextOut(drawItem.deviceContext(),pureBitmap.width()+6,(drawRect.bottom()+drawRect.top()-textMetric.tmHeight)/2,(LPSTR)stringData,stringData.length());
	  memDevice.select(pureBitmap,TRUE);
    Rect stretchRect(drawRect);
		stretchRect.right(pureBitmap.width());
		stretchRect.bottom(sizeExtent.cy);
		pureDevice.stretchBlt(stretchRect,memDevice,Rect(0,0,pureBitmap.width(),pureBitmap.height()),PureDevice::MergeCopy);
	  memDevice.select(pureBitmap,FALSE);
	}
  if((((UINT)drawItem.itemState())&ODS_FOCUS))
	{
		Rect bmpRect(drawRect);
		bmpRect.right(bmpRect.left()+pureBitmap.width());
		bmpRect.bottom(bmpRect.top()+sizeExtent.cy);
		::DrawFocusRect(drawItem.deviceContext(),(RECT*)&bmpRect);
	}
}

