#include <common/btnlnk.hpp>
#include <common/purehdc.hpp>

LinkedButton::LinkedButton(void)
: mCtlID(0), mFocus(Focus), mlpBitmapDefault(0), mReferenceCount(0), mhInstance(0)
{
}

LinkedButton::LinkedButton(int ctlID,String &focusUp,String &noFocusUp,String &focusDown,String &noFocusDisabled,HINSTANCE hInstance,FocusItem focusItem)
: mCtlID(ctlID), mReferenceCount(1), mFocus(focusItem),
  mBitmapFocusUp(focusUp,hInstance), mBitmapNoFocusUp(noFocusUp,hInstance),
	mBitmapFocusDown(focusDown,hInstance), mBitmapDisabled(noFocusDisabled,hInstance), 
	mFocusUpString(focusUp), mNoFocusUpString(noFocusUp), mFocusDownString(focusDown),
	mNoFocusDisabledString(noFocusDisabled), mhInstance(hInstance)
{
  setBitmapDefault();
}

LinkedButton::LinkedButton(int ctlID,String &focusUp,String &noFocusUp,String &focusDown,HINSTANCE hInstance,FocusItem focusItem)
: mCtlID(ctlID), mReferenceCount(1), mFocus(focusItem), 
  mBitmapFocusUp(focusUp,hInstance), mBitmapNoFocusUp(noFocusUp,hInstance),
	mBitmapFocusDown(focusDown,hInstance), mBitmapDisabled(noFocusUp,hInstance),
	mFocusUpString(focusUp), mNoFocusUpString(noFocusUp), mFocusDownString(focusDown),
	mNoFocusDisabledString(noFocusUp), mhInstance(hInstance)
{
  setBitmapDefault();
}

LinkedButton::LinkedButton(const LinkedButton &someLinkedButton)
: mCtlID(someLinkedButton.mCtlID), mReferenceCount(someLinkedButton.mReferenceCount),
	mFocus(someLinkedButton.mFocus), mhInstance(someLinkedButton.instance()),
	mBitmapFocusUp(someLinkedButton.mFocusUpString,someLinkedButton.instance()),
	mBitmapNoFocusUp(someLinkedButton.mNoFocusUpString,someLinkedButton.instance()),
	mBitmapFocusDown(someLinkedButton.mFocusDownString,someLinkedButton.instance()),
	mBitmapDisabled(someLinkedButton.mNoFocusDisabledString,someLinkedButton.instance()),
	mFocusUpString(someLinkedButton.mFocusUpString),
	mNoFocusUpString(someLinkedButton.mNoFocusUpString),
	mFocusDownString(someLinkedButton.mFocusDownString),
	mNoFocusDisabledString(someLinkedButton.mNoFocusDisabledString)
{
  setBitmapDefault();
}

LinkedButton::~LinkedButton()
{
}

WORD LinkedButton::drawButton(LPDRAWITEMSTRUCT lpControlData)
{
	int retCode(TRUE);

	switch(lpControlData->itemAction)
	{
		case ODA_DRAWENTIRE :
			if(lpControlData->itemState&ODS_SELECTED)drawBitmap(lpControlData,mBitmapFocusDown);
				else if(lpControlData->itemState&ODS_FOCUS)drawBitmap(lpControlData,*mlpBitmapDefault);
				  else if(lpControlData->itemState&ODS_DISABLED)drawBitmap(lpControlData,mBitmapDisabled);
					  else drawBitmap(lpControlData,mBitmapNoFocusUp);
			break;
		case ODA_SELECT :
			if(lpControlData->itemState&ODS_SELECTED)drawBitmap(lpControlData,mBitmapFocusDown);
			else
			{
				drawBitmap(lpControlData,mBitmapFocusUp);
				retCode=FALSE;
			}
			break;
		case ODA_FOCUS :
			if(lpControlData->itemState&ODS_FOCUS)drawBitmap(lpControlData,mBitmapFocusUp);
			else drawBitmap(lpControlData,mBitmapNoFocusUp);
			break;
	}
	return retCode;
}

void LinkedButton::drawBitmap(const LPDRAWITEMSTRUCT lpControlData,PureBitmap &pureBitmap)
{
	RECT controlRect;

  if(!pureBitmap.isOkay())return;
	PureDevice displayDevice(lpControlData->hDC);
	PureDevice memDevice(displayDevice);
	::GetClientRect(lpControlData->hwndItem,&controlRect);
  memDevice.select((HBITMAP)pureBitmap);
	displayDevice.stretchBlt(controlRect,memDevice,Rect(0,0,pureBitmap.width(),pureBitmap.height()),PureDevice::SourceCopy);
	memDevice.select((HBITMAP)pureBitmap,FALSE);
}
