/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: gdiDC
#Project: xdcGdi
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#include "gdiDC.h"
#include "gdiFont.h"
#include "gdiPen.h"
#include "gdiBrush.h"
#include "gdiItf.h"

HGDIOBJ gdiDClow::select(HGDIOBJ hObj)
{
	return ::SelectObject(m_hDC,hObj);
}

gdiSizeLow gdiDClow::GetTextSizeLow(LPCWSTR pStr,int len) const
{
	gdiSizeLow sz;
	if (!::GetTextExtentPoint32W(m_hDC,pStr,len,&sz))
		gdiEsys::Throw(xbHR_LAST,"gdiDClow::GetTextExtent failure");
	return sz;
}

void gdiDClow::Select(const gdiFont &font)
{
	HGDIOBJ hOld = select(font.GetHandle());
	if (!m_bSelectFont)
	{
		m_bSelectFont = true;
		m_hSelectFont = hOld;
	}
}

void gdiDClow::Select(const gdiFontObject &font)
{
	HGDIOBJ hOld = select(font.GetHandle());
	if (!m_bSelectFont)
	{
		m_bSelectFont = true;
		m_hSelectFont = hOld;
	}
}

void gdiDClow::Select(const gdiPen &pen)
{
	HGDIOBJ hOld = select(pen.GetHandle());
	if (!m_bSelectPen)
	{
		m_bSelectPen = true;
		m_hSelectPen = hOld;
	}
}

void gdiDClow::Select(IgdiBitmap *pBmp)
{
	unselectBmp();

	m_prSelectedBmp = pBmp;

	HBITMAP hBmp = pBmp->getHandle(&m_lckSelectedBmp);
	if (!hBmp) 
		xbThrow gdiEsys("gdiDClow::Select(bitmap) : failed, bitmap is empty");

	HGDIOBJ hOld = select(hBmp);
	m_bSelectBmp = true;
	m_hSelectBmp = hOld;
	m_bHasAlpha = pBmp->HasAlpha();
}

void gdiDClow::unselectBmp()
{
	if (m_bSelectBmp)
	{
		m_bSelectBmp = false;
		select(m_hSelectBmp);
	}
	m_lckSelectedBmp.Unlock();
	m_prSelectedBmp.Empty();
	m_bHasAlpha = false;
}

void gdiDClow::SelectClear()
{
	if (m_bSelectFont)
	{
		m_bSelectFont = false;
		select(m_hSelectFont);
	}
	if (m_bSelectPen)
	{
		m_bSelectPen = false;
		select(m_hSelectPen);
	}
	if (m_bSelectBmp)
		unselectBmp();
}

xbStringW gdiDClow::GetTextFace() const
{
	wchar buf[256];
	int lg = ::GetTextFaceW(m_hDC,sizeof(buf)-1,buf);
	if (lg <= 0) 
		gdiEsys::Throw(xbHR_LAST,"gdiDClow::GetTextFace failure");

	return buf;
}

long gdiDClow::GetPixelPerInchX() const
{
	return ::GetDeviceCaps(m_hDC, LOGPIXELSX);
}

long gdiDClow::GetPixelPerInchY() const
{
	return ::GetDeviceCaps(m_hDC, LOGPIXELSY);
}

void gdiDClow::SetTextColor(const gdiColor &clr)
{
	::SetTextColor(m_hDC,clr.GetRGB());
}

gdiBkMode gdiDClow::GetBkMode() const
{
	return (gdiBkMode)::GetBkMode(m_hDC);
}

void gdiDClow::SetBkMode(gdiBkMode bkmode)
{
	::SetBkMode(m_hDC,(int)bkmode);
}

void gdiDClow::TextOut(long x,long y,gdiTextMode textMode,const gdiRectLow *pRect,LPCWSTR pStr,int len,const int *lpDX)
{
	::ExtTextOutW(m_hDC,x,y,(UINT)textMode,pRect,pStr,len,lpDX);
}

void gdiDClow::GetTextExtentLow(LPCWSTR pStr,int len,long maxWidth,int *plenFit,gdiSizeLow *pSize,int *lPDX) const
{
	if (!::GetTextExtentExPointW(m_hDC,pStr,len,maxWidth,plenFit,lPDX,pSize)) 
		gdiEsys::Throw(xbHR_LAST,"gdiDClow::GetTextExtentLow() failure");
}

void gdiDClow::MoveToLow(long x,long y)
{
	::MoveToEx(m_hDC,x,y,NULL);
}


void gdiDClow::LineToLow(long x,long y)
{
	::LineTo(m_hDC,x,y);
}

void gdiDClow::VideoInvert(const gdiRectLow &rect)
{
	::InvertRect(m_hDC,&rect);
}

void gdiDClow::TextMetrics(TEXTMETRICW *pMetrics) const
{
	if (!::GetTextMetricsW(m_hDC,pMetrics))
		gdiEsys::Throw(xbHR_LAST,"gdiDClow::TextMetrics failed");
}

xbRefPtr<IgdiDC> gdiDClow::CreateCompatibleDC() const
{
	xbRefPtr<gdiCompatibleDC> prDC;
	gdiCompatibleDC::CreateObject(&prDC);
	HDC hdcNew;
	if (!(hdcNew = ::CreateCompatibleDC(m_hDC))
		&& !(hdcNew = ::CreateCompatibleDC(NULL)) )
		xbThrow gdiEsys(xbHR_LAST,"gdiDClow::CreateCompatibleDC() : dc creation failure");
	prDC->Init(hdcNew);
	return (gdiCompatibleDC*)prDC;
}

#define AC_SRC_ALPHA 0x01 
void gdiDClow::AlphaDraw(long xDst,long yDst,long widthDst,long heightDst,
		IgdiDC &dcSrc,long xSrc,long ySrc,long widthSrc,long heightSrc,
		int cAlpha)
{
	xbRefQIPtr<gdiDClow> prDcSrcLow(&dcSrc);
	BLENDFUNCTION blendFunction;
	memset(&blendFunction,0,sizeof(BLENDFUNCTION));
//		blendFunction.BlendFlags=0;
	blendFunction.BlendOp=AC_SRC_OVER;
	blendFunction.AlphaFormat=dcSrc.HasAlpha()?AC_SRC_ALPHA:0;/*(HasAlpha()?0:AC_SRC_NO_ALPHA)|(bDstHasAlpha?0:AC_DST_NO_ALPHA)*/;
	blendFunction.SourceConstantAlpha=cAlpha==-1?255:cAlpha;

	if (!::AlphaBlend(m_hDC,xDst,yDst,widthDst,heightDst,prDcSrcLow->GetHandle(),
		xSrc,ySrc,widthSrc,heightSrc,blendFunction))
		xbThrow gdiEsys(xbHR_LAST,"gdiDClow::AlphaDraw() : Draw operation failed");
}

void gdiDClow::StdDraw(long xDst,long yDst,long widthDst,long heightDst,
		IgdiDC &dcSrc,long xSrc,long ySrc,long widthSrc,long heightSrc)
{
	xbRefQIPtr<gdiDClow> prDcSrcLow(&dcSrc);

	int		stretch = ::SetStretchBltMode(m_hDC,STRETCH_DELETESCANS/* HALFTONE*/);
	int oldMode = ::SetBkMode(m_hDC /*??hdcSrc*/,OPAQUE);
	BOOL bRet;
	if (widthDst == widthSrc && heightDst == heightSrc)
		bRet = ::BitBlt(m_hDC,xDst,yDst,widthDst,heightDst,prDcSrcLow->GetHandle(),xSrc,ySrc,SRCCOPY);
	else
		bRet = ::StretchBlt(m_hDC,xDst,yDst,widthDst,heightDst,prDcSrcLow->GetHandle(),
			xSrc,ySrc,widthSrc,heightSrc,SRCCOPY);

	::SetStretchBltMode(m_hDC,stretch);
	::SetBkMode(m_hDC,oldMode);

	if (!bRet)
		xbThrow gdiEsys(xbHR_LAST,"gdiDClow::StdDraw() : Draw operation failed");
}

bool gdiDClow::DrawIcon(long xDst,long yDst,long widthDst,long heightDst,HICON hIcon,int nAni,gdiBrush *pBrush,xbOpMode opm)
{
	HBRUSH hBr = pBrush?pBrush->GetHandle():NULL;
	BOOL bRet = ::DrawIconEx(m_hDC,xDst,yDst,
		hIcon,widthDst,heightDst,nAni,hBr,DI_NORMAL);

	if (bRet)
		return true;
	if (opm == xbOp_Required)
		xbThrow gdiEsys(xbHR_LAST,"gdiDClow::DrawIcon() : Draw operation failed");
	return false;
}

void gdiDClow::FillRect(const gdiRectLow *pRect,gdiBrush *pBrush)
{
	if (!::FillRect(m_hDC,pRect,pBrush->GetHandle()))
		xbThrow gdiEsys(xbHR_LAST,"gdiDClow::FillRect() : operation failed");
}
