#include "xmllistbox.h"

// ----------------------------------------------------------------------------
CXmlListBox::CXmlListBox(CXmlUi* pXmlUi, CXmlLayout* pParent)
	: CXmlFrame(pXmlUi, pParent)
{
	m_type				= eXmlUiType_ListBox;

	m_fontString		= NULL;
	m_fadeDuration		= 3.0f;
	m_insertMode		= eInsertMode_Bottom;
}

// ----------------------------------------------------------------------------
CXmlListBox::~CXmlListBox()
{
	XML_SAFE_DLETE(m_fontString);
}

// ----------------------------------------------------------------------------
int CXmlListBox::XmlValueToInsertMode(const char* valueStr)
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "TOP") == 0 )
			return eInsertMode_Top;
		else if ( XMLString::compareString(valueStr, "BOTTOM") == 0 )
			return eInsertMode_Bottom;
	}
	return eInsertMode_Bottom;
}

// ----------------------------------------------------------------------------
void CXmlListBox::LoadNodeAttribute(const DOMNamedNodeMap* nodeMap)
{
	CXmlFrame::LoadNodeAttribute(nodeMap);

	const XMLCh* nodeValue;
	if ( (nodeValue = GetNodeAttribute(nodeMap, "fadeDuration")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_fadeDuration = (float)atof(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "insertMode")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_insertMode = XmlValueToInsertMode(tmpStr);
		XMLString::release(&tmpStr);
	}
}

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

	// FontString
	if ( XMLString::compareString(nodeName, "FontString") == 0 )
	{
		XML_SAFE_DLETE(m_fontString);
		m_fontString = new CXmlFontString(m_pXmlUi, this);
		m_fontString->LoadFromDOMNode(node);
	}
	// Text Insets
	else if ( XMLString::compareString(nodeName, "TextInsets") == 0 )
	{
		m_textInsets.LoadFromDOMNode(node);
	}
}

// ----------------------------------------------------------------------------
void CXmlListBox::Inherit(CXmlLayout* pXmlLayout)
{
	CXmlFrame::Inherit(pXmlLayout);
	CXmlListBox* pXmlListBox = dynamic_cast<CXmlListBox*>(pXmlLayout);
	if (pXmlListBox)
	{
		if (pXmlListBox->m_fontString)
		{
			XML_SAFE_DLETE(m_fontString);
			m_fontString = new CXmlFontString(m_pXmlUi, this);
			m_fontString->Inherit(pXmlListBox->m_fontString);
		}		
		m_textInsets		= pXmlListBox->m_textInsets;
		m_fadeDuration		= pXmlListBox->m_fadeDuration;
		m_insertMode		= pXmlListBox->m_insertMode;
	}
}