#ifndef _gdiBRUSH_
#define _gdiBRUSH_
/*<<<***************************************************************************
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: gdiBrush
#Project: xdcGdi
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _gdiBASE_
#	include "gdiBase.h"
#endif

enum gdiBrushStyleMode
{
	//TODO gdiBrushStyle_DibPattern = BS_DIBPATTERN,		// A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB. For more information, see discussion in lbHatch.
	//TODO gdiBrushStyle_DibPatternPt = BS_DIBPATTERNPT,	// A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERNPT, the lbHatch member contains a pointer to a packed DIB. For more information, see discussion in lbHatch.
	gdiBrushStyle_Hatched = BS_HATCHED,						// Hatched brush.
//	gdiBrushStyle_Hollow = BS_HOLLOW,						// Hollow brush.
	gdiBrushStyle_Null = BS_NULL,							// Same as BS_HOLLOW.
	//TODO gdiBrushStyle_Pattern = BS_PATTERN,				// Pattern brush defined by a memory bitmap.
	gdiBrushStyle_Solid = BS_SOLID							// Solid brush.
};

enum gdiBrushHatchMode
{
	gdiBrushHatch_Null = 0,									// Null value
	gdiBrushHatch_Bdiag = HS_BDIAGONAL,						// A 45-degree upward, left-to-right hatch
	gdiBrushHatch_Cross = HS_CROSS,							// Horizontal and vertical cross-hatch
	gdiBrushHatch_DiagCross = HS_DIAGCROSS,					// 45-degree crosshatch
	gdiBrushHatch_Fdiag = HS_FDIAGONAL,						// A 45-degree downward, left-to-right hatch
	gdiBrushHatch_Horizontal = HS_HORIZONTAL,				// Horizontal hatch
	gdiBrushHatch_Vertical = HS_VERTICAL					// Vertical hatch
};

class gdiEXPORT gdiBrush
{
public:
	gdiBrush(const gdiColor &clr,gdiBrushStyleMode style = gdiBrushStyle_Solid)
		:m_clr(clr),m_style(style),m_hatch(gdiBrushHatch_Null)
	{
		xbRdbgAssert(style != gdiBrushStyle_Hatched);
	}
	gdiBrush(const gdiColor &clr,gdiBrushHatchMode hatch)
		:m_clr(clr),m_style(gdiBrushStyle_Hatched),m_hatch(hatch)
	{
	}
	gdiBrush()
		:m_style(gdiBrushStyle_Null),m_hatch(gdiBrushHatch_Null)
	{
	}
	void Init(const gdiColor &clr,gdiBrushStyleMode style = gdiBrushStyle_Solid,gdiBrushHatchMode hatch = gdiBrushHatch_Null)
	{
		brushDestroy();
		m_clr = clr;
		m_style = style;
		m_hatch = hatch;
	}
	void Init(const gdiColor &clr,gdiBrushHatchMode hatch)
	{
		brushDestroy();
		m_clr = clr;
		m_style = gdiBrushStyle_Hatched;
		m_hatch = hatch;
	}
	HBRUSH GetHandle() const {if (m_hBr==NULL) brushCreate();return m_hBr;}

	~gdiBrush()
	{
		brushDestroy();
		
	}
protected:
	void brushCreate() const
	{
		if (m_hBr != NULL)
			return;
		LOGBRUSH logbr;
		logbr.lbStyle = (UINT)m_style;
		logbr.lbColor = m_clr.GetRGB();
		logbr.lbHatch = (UINT)m_hatch;
		HBRUSH hBr = ::CreateBrushIndirect(&logbr);
		if (hBr == NULL)
			gdiEsys::Throw(xbHR_LAST,"gdiBrush::brushCreate() failure");
		m_hBr = hBr;
	}
	void brushDestroy()
	{
		if (m_hBr == NULL)
			return;
		::DeleteObject(m_hBr);
		m_hBr = NULL;
	}
	gdiBrushStyleMode m_style;
	gdiBrushHatchMode m_hatch;
	gdiColor		m_clr;
	mutable HBRUSH	m_hBr;
private:
	xbDclNoNewForObject(gdiBrush);
	xbDclNoCopyForObject(gdiBrush);
};

#endif
