#include "xmlcheckbutton.h"

// ----------------------------------------------------------------------------
CXmlCheckButton::CXmlCheckButton(CXmlUi* pXmlUi, CXmlLayout* pParent)
	: CXmlButton(pXmlUi, pParent)
{
	m_type				= eXmlUiType_CheckButton;

	m_checkedTexture	= NULL;
	m_disabledCheckedTexture = NULL;
	m_checked			= false;
	m_autoChecked		= true;
}

// ----------------------------------------------------------------------------
CXmlCheckButton::~CXmlCheckButton()
{
	XML_SAFE_DLETE(m_checkedTexture);
	XML_SAFE_DLETE(m_disabledCheckedTexture);
}

// ----------------------------------------------------------------------------
void CXmlCheckButton::LoadNodeAttribute(const DOMNamedNodeMap* nodeMap)
{
	CXmlButton::LoadNodeAttribute(nodeMap);

	const XMLCh* nodeValue;
	if ( (nodeValue = GetNodeAttribute(nodeMap, "checked")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_checked = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "autoChecked")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_autoChecked = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
}

// ----------------------------------------------------------------------------
void CXmlCheckButton::LoadNodeElement(const DOMNode* node, const char* nodeName)
{
	CXmlButton::LoadNodeElement(node, nodeName);

	// Normal Texture
	if ( XMLString::compareString(nodeName, "CheckedTexture") == 0 )
	{
		XML_SAFE_DLETE(m_checkedTexture);
		m_checkedTexture = new CXmlTexture(m_pXmlUi, this);
		m_checkedTexture->LoadFromDOMNode(node);
	}
	// Pushed Texture
	else if ( XMLString::compareString(nodeName, "DisabledCheckedTexture") == 0 )
	{
		XML_SAFE_DLETE(m_disabledCheckedTexture);
		m_disabledCheckedTexture = new CXmlTexture(m_pXmlUi, this);
		m_disabledCheckedTexture->LoadFromDOMNode(node);
	}
}

// ----------------------------------------------------------------------------
void CXmlCheckButton::Inherit(CXmlLayout* pXmlLayout)
{
	CXmlButton::Inherit(pXmlLayout);
	CXmlCheckButton* pXmlCheckButton = dynamic_cast<CXmlCheckButton*>(pXmlLayout);
	if (pXmlCheckButton)
	{
		m_checked			= pXmlCheckButton->m_checked;
		m_autoChecked		= pXmlCheckButton->m_autoChecked;

		if (pXmlCheckButton->m_checkedTexture)
		{
			XML_SAFE_DLETE(m_checkedTexture);
			m_checkedTexture = new CXmlTexture(m_pXmlUi, this);
			m_checkedTexture->Inherit(pXmlCheckButton->m_checkedTexture);
		}
		if (pXmlCheckButton->m_disabledCheckedTexture)
		{
			XML_SAFE_DLETE(m_disabledCheckedTexture);
			m_disabledCheckedTexture = new CXmlTexture(m_pXmlUi, this);
			m_disabledCheckedTexture->Inherit(pXmlCheckButton->m_disabledCheckedTexture);
		}
	}
}