#include <stdio.h>
#include <string.h>
#include <common/stdlib.hpp>
#include <common/string.hpp>
#include <common/owner.hpp>

OwnerDraw::OwnerDraw(OwnerDraw::Usage sourceResource)
: mhLibrary(0), mhInstance(0)
{
	String dllPathFileName;

	mhInstance=instanceData();
	if(OwnerDraw::UseLibrary==sourceResource)
	{
		::GetWindowsDirectory(dllPathFileName,String::MaxString-1);
#if defined(__FLAT__)
		dllPathFileName+="\\RESLIB32.DLL";
#else
	  dllPathFileName+="\\RESLIB16.DLL";
#endif
		mhLibrary=::LoadLibrary(dllPathFileName);
		if((UINT)mhLibrary<32)mhLibrary=0;
	}
	return;
}

OwnerDraw::~OwnerDraw()
{
	if(0!=mhLibrary)::FreeLibrary(mhLibrary);
}

int OwnerDraw::associate(int ctlID,String &focusUp,String &noFocusUp,String &focusDown,FocusItem focusItem)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedButton(ctlID)))mLinkedButtons.insert(&LinkedButton(ctlID,focusUp,noFocusUp,focusDown,mhLibrary?mhLibrary:mhInstance,(LinkedButton::FocusItem)focusItem));
	else
	{
		itemIndex--;
	  mLinkedButtons[itemIndex].referenceCount(mLinkedButtons[itemIndex].referenceCount()+1);
	}
	return TRUE;
}

int OwnerDraw::associate(int ctlID,String &focusUp,String &noFocusUp,String &focusDown,String &noFocusDisabled,FocusItem focusItem)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedButton(ctlID)))mLinkedButtons.insert(&LinkedButton(ctlID,focusUp,noFocusUp,focusDown,noFocusDisabled,mhLibrary?mhLibrary:mhInstance,(LinkedButton::FocusItem)focusItem));
	else
	{
		itemIndex--;
	  mLinkedButtons[itemIndex].referenceCount(mLinkedButtons[itemIndex].referenceCount()+1);
	}
	return TRUE;
}

int OwnerDraw::handleOwnerButton(int ctlID,LPARAM lParam)
{
	int itemIndex;
	LPDRAWITEMSTRUCT lpControlData((LPDRAWITEMSTRUCT)lParam);

	if(!(itemIndex=locateLinkedButton(ctlID)))return FALSE;
	return mLinkedButtons[--itemIndex].drawButton(lpControlData);
}

void OwnerDraw::freeButton(int ctlID)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedButton(ctlID)))return;
	--itemIndex;
	if(mLinkedButtons[itemIndex].referenceCount()-1<=0)mLinkedButtons.remove(itemIndex);
	else mLinkedButtons[itemIndex].referenceCount(mLinkedButtons[itemIndex].referenceCount()-1);
}

int OwnerDraw::locateLinkedButton(int ctlID)
{
	size_t size((int)mLinkedButtons.size());

	for(int i=0;i<size;i++)if(mLinkedButtons[i]==ctlID)return i+1;
	return 0;
}

int OwnerDraw::associate(int ctlID,String &bitmapName)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedBitmap(ctlID)))mLinkedBitmaps.insert(&LinkedBitmap(ctlID,bitmapName,mhLibrary?mhLibrary:mhInstance));
	else
	{
		itemIndex--;
		mLinkedBitmaps[itemIndex].referenceCount(mLinkedBitmaps[itemIndex].referenceCount()+1);
	}
	return TRUE;
}

void OwnerDraw::drawBitmap(int ctlID,HWND hWnd,RECT locationRect)
{
	int itemIndex;
	HDC hDC;

	::SetRect((RECT FAR *)&locationRect,0,0,0,0);
	if(!(itemIndex=locateLinkedBitmap(ctlID)))return;
	--itemIndex;
	hDC=::GetDC(hWnd);
	mLinkedBitmaps[itemIndex].drawBitmap(hDC,locationRect);
}

void OwnerDraw::drawBitmap(int ctlID)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedBitmap(ctlID)))return;
	--itemIndex;
	mLinkedBitmaps[itemIndex].drawBitmap();
}

void OwnerDraw::freeBitmap(int ctlID)
{
	int itemIndex;

	if(!(itemIndex=locateLinkedBitmap(ctlID)))return;
	--itemIndex;
	if(mLinkedBitmaps[itemIndex].referenceCount()-1<=0)mLinkedBitmaps.remove(itemIndex);
	else mLinkedBitmaps[itemIndex].referenceCount(mLinkedBitmaps[itemIndex].referenceCount()-1);
}

int OwnerDraw::locateLinkedBitmap(int ctlID)
{
	size_t size((int)mLinkedBitmaps.size());

	for(int i=0;i<size;i++)if(mLinkedBitmaps[i]==ctlID)return i+1;
	return 0;
}

