#include <common/odbutton.hpp>
#include <common/drawitem.hpp>
#include <common/purehdc.hpp>
#include <common/measure.hpp>
#include <common/dib.hpp>
#include <common/font.hpp>
			
OwnerDrawButton::OwnerDrawButton(GUIWindow &parentWnd,HWND hControlWnd,UINT controlID,const String &strBmFocusUp,const String &strBmFocusDown,const String &strBmNoFocusUp,const String &strBmDisabled,BOOL hasButtonLines,Font textFont) 
: mParentWnd(parentWnd), Control(hControlWnd,controlID), mTextColor(0,0,0),
  mBmFocusUp(strBmFocusUp), mBmFocusDown(strBmFocusDown), mBmNoFocusUp(strBmNoFocusUp), 
	mBmDisabled(strBmDisabled), mhBkGndBrush((HBRUSH)::GetStockObject(NULL_BRUSH)),
	mTextFont(textFont), mHasButtonLines(hasButtonLines)
{
  PureDevice pureDevice(parentWnd);
  mDIBitmap=new DIBitmap(pureDevice,mBmFocusUp.width(),mBmFocusUp.height(),mBmFocusUp);
	mDIBitmap.disposition(PointerDisposition::Delete);
	mDrawItemHandler.setCallback(this,&OwnerDrawButton::drawItemHandler);
	mControlColorHandler.setCallback(this,&OwnerDrawButton::controlColorHandler);
	mParentWnd.insertHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
	mParentWnd.insertHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
}

OwnerDrawButton::OwnerDrawButton(GUIWindow &parentWnd,const Point &initPoint,UINT controlID,const String &strBmFocusUp,const String &strBmFocusDown,const String &strBmNoFocusUp,const String &strBmDisabled,BOOL hasButtonLines,const String &strLabel,Font textFont,DWORD style)
: mParentWnd(parentWnd), mBmFocusUp(strBmFocusUp), mTextColor(0,0,0),
  mBmFocusDown(strBmFocusDown), mBmNoFocusUp(strBmNoFocusUp), 
	mBmDisabled(strBmDisabled), mhBkGndBrush((HBRUSH)::GetStockObject(NULL_BRUSH)), 
	mInitPoint(initPoint), mTextFont(textFont), mHasButtonLines(hasButtonLines)
{
  PureDevice pureDevice(parentWnd);
  mDIBitmap=new DIBitmap(pureDevice,mBmFocusUp.width(),mBmFocusUp.height(),mBmFocusUp);
	mDIBitmap.disposition(PointerDisposition::Delete);
	mDrawItemHandler.setCallback(this,&OwnerDrawButton::drawItemHandler);
	mControlColorHandler.setCallback(this,&OwnerDrawButton::controlColorHandler);
	mParentWnd.insertHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
	mParentWnd.insertHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
  createControl("BUTTON",strLabel,style,Rect(initPoint.x(),initPoint.y(),mBmFocusUp.width(),mBmFocusUp.height()),parentWnd,controlID);
	show(SW_SHOW);
	update();
}

OwnerDrawButton::OwnerDrawButton(GUIWindow &parentWnd,const Rect &initRect,UINT controlID,const String &strBmFocusUp,const String &strBmFocusDown,const String &strBmNoFocusUp,const String &strBmDisabled,BOOL hasButtonLines,const String &strLabel,Font textFont,DWORD style)
: mParentWnd(parentWnd), mBmFocusUp(strBmFocusUp), mTextColor(0,0,0),
  mBmFocusDown(strBmFocusDown), mBmNoFocusUp(strBmNoFocusUp), 
	mBmDisabled(strBmDisabled), mhBkGndBrush((HBRUSH)::GetStockObject(NULL_BRUSH)), 
	mInitPoint(initRect.left(),initRect.top()), mTextFont(textFont), mHasButtonLines(hasButtonLines)
{
  PureDevice pureDevice(parentWnd);
  mDIBitmap=new DIBitmap(pureDevice,initRect.right(),initRect.bottom(),mBmFocusUp);
	mDIBitmap.disposition(PointerDisposition::Delete);
	mDrawItemHandler.setCallback(this,&OwnerDrawButton::drawItemHandler);
	mControlColorHandler.setCallback(this,&OwnerDrawButton::controlColorHandler);
	mParentWnd.insertHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
	mParentWnd.insertHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
  createControl("BUTTON",strLabel,style,Rect(initRect.left(),initRect.top(),initRect.right(),initRect.bottom()),parentWnd,controlID);
	show(SW_SHOW);
	update();
}

OwnerDrawButton::~OwnerDrawButton()
{
	mParentWnd.removeHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
	mParentWnd.removeHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
	if(mhBkGndBrush)::DeleteObject(mhBkGndBrush);
}

CallbackData::ReturnType OwnerDrawButton::drawItemHandler(CallbackData &someCallbackData)
{
  DrawItem drawItem(*((LPDRAWITEMSTRUCT)someCallbackData.lParam()));
	if(drawItem.controlID()!=controlID())return (CallbackData::ReturnType)TRUE;
	return handleDraw(drawItem);
}

CallbackData::ReturnType OwnerDrawButton::controlColorHandler(CallbackData &someCallbackData)
{
	Control wndButton((HWND)someCallbackData.lParam(),::GetDlgCtrlID((HWND)someCallbackData.lParam()),FALSE);
	if(!(*this==wndButton))return (CallbackData::ReturnType)FALSE;
  PureDevice pureDevice((HDC)someCallbackData.wParam());
	return handleControlColor(pureDevice,wndButton);
}

void OwnerDrawButton::drawBitmap(const DrawItem &drawItem,ResBitmap &resBitmap)
{
  if(!resBitmap.isOkay())return;
	PureDevice displayDevice(drawItem.deviceContext());
  mDIBitmap->copyBits(resBitmap.ptrData(),resBitmap.imageExtent());  
	mDIBitmap->usePalette(displayDevice,TRUE);
  mDIBitmap->stretchBlt(displayDevice,Rect(0,0,width(),height()));
	mDIBitmap->usePalette(displayDevice,FALSE);
}

// *************** virtuals

WORD OwnerDrawButton::handleDraw(const DrawItem &drawItem)
{
  WORD retCode(TRUE);

  switch(drawItem.itemAction())
	{
	  case ODA_DRAWENTIRE :
		  drawEntire(drawItem);
		  break;
		case ODA_SELECT :
		  retCode=drawSelect(drawItem);
		  break;
		case ODA_FOCUS :
		  drawFocus(drawItem);
		  break;
	}
  return retCode;
}

WORD OwnerDrawButton::handleMeasureItem(MeasureItem &/*measureItem*/)
{
  return TRUE;
}

LPARAM OwnerDrawButton::handleControlColor(PureDevice &/*pureDevice*/,Control &/*wndButton*/)
{
  return (CallbackData::ReturnType)mhBkGndBrush;
}

void OwnerDrawButton::drawEntire(const DrawItem &drawItem)
{
  if(drawItem.itemState()&ODS_SELECTED)drawBitmap(drawItem,mBmFocusDown);
	else if(drawItem.itemState()&ODS_FOCUS)drawBitmap(drawItem,mBmFocusUp);
	else if(drawItem.itemState()&ODS_DISABLED)drawBitmap(drawItem,mBmDisabled);
	else drawBitmap(drawItem,mBmNoFocusUp);
  drawButtonText(drawItem);
	drawButtonLines(drawItem);
  return;
}

WORD OwnerDrawButton::drawSelect(const DrawItem &drawItem)
{
  WORD retCode(TRUE);
  if(drawItem.itemState()&ODS_SELECTED)drawBitmap(drawItem,mBmFocusDown);
	else {drawBitmap(drawItem,mBmFocusUp);retCode=FALSE;}
	drawButtonText(drawItem);
	drawButtonLines(drawItem);
	return retCode;
}

void OwnerDrawButton::drawFocus(const DrawItem &drawItem)
{
  if(drawItem.itemState()&ODS_FOCUS)drawBitmap(drawItem,mBmFocusUp);
	else drawBitmap(drawItem,mBmNoFocusUp);
  drawButtonText(drawItem);
	drawButtonLines(drawItem);
  return;
}

void OwnerDrawButton::drawButtonText(const DrawItem &drawItem)
{
  TEXTMETRIC textMetric;
	Rect drawRect;
  String strText;
  PureDevice controlDevice(drawItem.deviceContext());

	windowText(strText);
	if(strText.isNull())return;
	controlDevice.setBkMode(PureDevice::Transparent);
	controlDevice.select((GDIObj)mTextFont,TRUE);
	controlDevice.setTextColor(textColor());	
	::GetTextMetrics(drawItem.deviceContext(),&textMetric);
	drawRect=drawItem.rectItem();
	controlDevice.textOut(0,(drawRect.bottom()+drawRect.top()-textMetric.tmHeight)/2,strText);
	controlDevice.select((GDIObj)mTextFont,FALSE);
}

void OwnerDrawButton::drawButtonLines(const DrawItem &drawItem)
{
  if(!hasButtonLines())return;
  PureDevice controlDevice(drawItem.deviceContext());
	controlDevice.line(Point(0,0),Point(width(),0),RGBColor(255,255,255));
	controlDevice.line(Point(0,0),Point(0,height()),RGBColor(255,255,255));
	controlDevice.line(Point(0,height()-1),Point(width(),height()-1),RGBColor(0,0,0));
	controlDevice.line(Point(width(),height()-1),Point(width()-1,0),RGBColor(0,0,0));
}
