#include "xmlframe.h"
#include "xmlmodel.h"
#include "xmlbutton.h"
#include "xmlcheckbutton.h"
#include "xmlradiobutton.h"
#include "xmleditbox.h"
#include "xmlslider.h"
#include "xmlstatusbar.h"
#include "xmlmessageframe.h"
#include "xmllistbox.h"
#include "xmlui.h"

// ----------------------------------------------------------------------------
CXmlBackdrop::CXmlBackdrop()
{	
	m_tileFile.clear();
	m_edgeFile.clear();
	m_isTile			= true;
}

// ----------------------------------------------------------------------------
bool CXmlBackdrop::LoadFromDOMNode(const DOMNode* node)
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Attribute
	DOMNamedNodeMap* nodeMap = node->getAttributes();
	if ( nodeMap )
	{
		const XMLCh* nodeValue;
		if ( (nodeValue = GetNodeAttribute(nodeMap, "bgFile")) != NULL )
		{
			char* tmpStr = XMLString::transcode(nodeValue);
			m_tileFile = tmpStr;
			XMLString::release(&tmpStr);
		}
		if ( (nodeValue = GetNodeAttribute(nodeMap, "edgeFile")) != NULL )
		{
			char* tmpStr = XMLString::transcode(nodeValue);
			m_edgeFile = tmpStr;
			XMLString::release(&tmpStr);
		}
		if ( (nodeValue = GetNodeAttribute(nodeMap, "tile")) != NULL )
		{
			char* tmpStr = XMLString::transcode(nodeValue);
			m_isTile = XmlValueToBoolean(tmpStr);
			XMLString::release(&tmpStr);
		}
	}

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			// Background Insets
			if ( XMLString::compareString(nodeName, "BackgroundInsets") == 0 )
			{
				m_bgInsets.LoadFromDOMNode(node);
			}
			// Tile Size
			else if ( XMLString::compareString(nodeName, "TileSize") == 0 )
			{
				m_tileSize.LoadFromDOMNode(node);
			}
			// Edge Size
			else if ( XMLString::compareString(nodeName, "EdgeSize") == 0 )
			{
				m_edgeSize.LoadFromDOMNode(node);
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}

	return true;
}

// ----------------------------------------------------------------------------
CXmlResizeBounds::CXmlResizeBounds()
{	
	m_maxBounds = NULL;
	m_minBounds = NULL;
}

// ----------------------------------------------------------------------------
CXmlResizeBounds::~CXmlResizeBounds()
{
	XML_SAFE_DLETE(m_maxBounds);
	XML_SAFE_DLETE(m_minBounds);
}

// ----------------------------------------------------------------------------
bool CXmlResizeBounds::LoadFromDOMNode( const DOMNode* node )
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			// Min Resize Bounds
			if ( XMLString::compareString(nodeName, "minResize") == 0 )
			{				
				XML_SAFE_DLETE(m_minBounds);
				m_minBounds = new CXmlDimension;
				m_minBounds->LoadFromDOMNode(node);
			}
			// Max Resize Bounds
			else if ( XMLString::compareString(nodeName, "maxResize") == 0 )
			{
				XML_SAFE_DLETE(m_maxBounds);
				m_maxBounds = new CXmlDimension;
				m_maxBounds->LoadFromDOMNode(node);
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}

	return true;
}

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

	m_layers.clear();
	m_frames.clear();
	m_scripts.clear();	

	m_pBackdrop			= NULL;
	m_pTitleRegion		= NULL;
	m_alpha				= 1.0f;
	m_id				= 0;
	m_frameStrata		= eStrata_Background;
	m_frameLevel		= 0;
	m_topLevel			= false;
	m_isEnableMouse		= false;
	m_isEnableKeyboard	= false;
	m_isMovable			= false;
	m_isResizable		= false;
}

// ----------------------------------------------------------------------------
CXmlFrame::~CXmlFrame()
{
	XML_SAFE_DLETE( m_pBackdrop );
	XML_SAFE_DLETE( m_pTitleRegion );

	// scripts
	m_scripts.clear();

	// frames
	for ( vector<CXmlFrame*>::iterator iter = m_frames.begin(); iter != m_frames.end(); iter++ )
	{
		delete *iter;
	}
	m_frames.clear();

	// layers
	for ( multimap<int, CXmlLayout*>::iterator iter = m_layers.begin(); iter != m_layers.end(); iter++ )
	{
		delete iter->second;
	}
	m_layers.clear();	
}

// ----------------------------------------------------------------------------
int	CXmlFrame::XmlValueToLayerLevel(const char* valueStr)
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "BACKGROUND") == 0 )
			return eLayer_Background;
		else if ( XMLString::compareString(valueStr, "BORDER") == 0 )
			return eLayer_Border;
		else if ( XMLString::compareString(valueStr, "ARTWORK") == 0 )
			return eLayer_Artwork;
		else if ( XMLString::compareString(valueStr, "OVERLAY") == 0 )
			return eLayer_Overlay;
		else if ( XMLString::compareString(valueStr, "HIGHLIGHT") == 0 )
			return eLayer_Highlight;
	}
	return eLayer_Background;
}

// ----------------------------------------------------------------------------
int	CXmlFrame::XmlValueToFrameStrata(const char* valueStr)
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "BACKGROUND") == 0 )
			return eStrata_Background;
		else if ( XMLString::compareString(valueStr, "LOW") == 0 )
			return eStrata_Low;
		else if ( XMLString::compareString(valueStr, "MEDIUM") == 0 )
			return eStrata_Medium;
		else if ( XMLString::compareString(valueStr, "HIGH") == 0 )
			return eStrata_High;
		else if ( XMLString::compareString(valueStr, "DIALOG") == 0 )
			return eStrata_Dialog;
		else if ( XMLString::compareString(valueStr, "TOOLTIP") == 0 )
			return eStrata_Tooltip;
	}
	return eStrata_Background;
}


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

	const XMLCh* nodeValue;
	if ( (nodeValue = GetNodeAttribute(nodeMap, "alpha")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_alpha = (float)atof(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "parent")) != NULL )
	{
		if (m_pXmlUi)
		{
			char* tmpStr = XMLString::transcode(nodeValue);
			m_parent = m_pXmlUi->FindByName(tmpStr);
			XMLString::release(&tmpStr);
		}
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "movable")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_isMovable = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "resizable")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_isResizable = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "frameStrata")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_frameStrata = XmlValueToFrameStrata(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "frameLevel")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_frameLevel = atoi(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "toplevel")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_topLevel = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "id")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_id = atoi(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "enableMouse")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_isEnableMouse = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "enableKeyboard")) != NULL )
	{
		char* tmpStr = XMLString::transcode(nodeValue);
		m_isEnableKeyboard = XmlValueToBoolean(tmpStr);
		XMLString::release(&tmpStr);
	}
}

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

	// Title Region
	if ( XMLString::compareString(nodeName, "TitleRegion") == 0 )
	{
		XML_SAFE_DLETE( m_pTitleRegion );
		m_pTitleRegion = new CXmlLayout;
		m_pTitleRegion->LoadFromDOMNode(node);
	}
	// Resize Bounds
	else if ( XMLString::compareString(nodeName, "ResizeBounds") == 0 )
	{
		m_resizeBounds.LoadFromDOMNode(node);
	}
	// Backdrop
	else if ( XMLString::compareString(nodeName, "Backdrop") == 0 )
	{
		XML_SAFE_DLETE(m_pBackdrop);
		m_pBackdrop = new CXmlBackdrop;
		m_pBackdrop->LoadFromDOMNode(node);
	}
	// HitRectInsets
	else if ( XMLString::compareString(nodeName, "HitRectInsets") == 0 )
	{
		m_hitRectInsets.LoadFromDOMNode(node);
	}
	// Layers
	else if ( XMLString::compareString(nodeName, "Layers") == 0 )
	{
		LoadXmlLayers(node);
	}
	// Frames
	else if ( XMLString::compareString(nodeName, "Frames") == 0 )
	{
		LoadXmlFrames(node);
	}
	// Scripts
	else if ( XMLString::compareString(nodeName, "Scripts") == 0 )
	{
		LoadXmlScripts(node);
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::Inherit(CXmlLayout* pXmlLayout)
{
	CXmlLayout::Inherit(pXmlLayout);
	CXmlFrame* pXmlFrame = dynamic_cast<CXmlFrame*>(pXmlLayout);
	if (pXmlFrame)
	{
		InheritFrames(pXmlFrame);
		InheritLayers(pXmlFrame);

		m_scripts			= pXmlFrame->m_scripts;
		m_resizeBounds		= pXmlFrame->m_resizeBounds;
		m_hitRectInsets		= pXmlFrame->m_hitRectInsets;
		m_alpha				= pXmlFrame->m_alpha;
		m_id				= pXmlFrame->m_id;
		m_frameStrata		= pXmlFrame->m_frameStrata;
		m_frameLevel		= pXmlFrame->m_frameLevel;
		m_topLevel			= pXmlFrame->m_topLevel;
		m_isEnableMouse		= pXmlFrame->m_isEnableMouse;
		m_isEnableKeyboard	= pXmlFrame->m_isEnableKeyboard;
		m_isMovable			= pXmlFrame->m_isMovable;
		m_isResizable		= pXmlFrame->m_isResizable;

		if (pXmlFrame->m_pBackdrop)
		{
			XML_SAFE_DLETE(m_pBackdrop);
			m_pBackdrop = new CXmlBackdrop;
			*m_pBackdrop = *pXmlFrame->m_pBackdrop;
		}

		if (pXmlFrame->m_pTitleRegion)
		{
			XML_SAFE_DLETE(m_pTitleRegion);
			m_pTitleRegion = new CXmlLayout(m_pXmlUi);
			*m_pTitleRegion = *pXmlFrame->m_pTitleRegion;
		}
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::LoadXmlLayers(const DOMNode* node)
{
	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			// Layer
			if ( XMLString::compareString(nodeName, "Layer") == 0 )
			{
				LoadXmlLayer(node);
			}			
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::LoadXmlLayer(const DOMNode* node)
{
	int level = eLayer_Background;

	// Attribute
	DOMNamedNodeMap* nodeMap = node->getAttributes();
	if ( nodeMap )
	{
		const XMLCh* nodeValue;
		if ( (nodeValue = GetNodeAttribute(nodeMap, "level")) != NULL )
		{
			char* tmpStr = XMLString::transcode(nodeValue);
			level = XmlValueToLayerLevel(tmpStr);
			XMLString::release(&tmpStr);
		}
	}

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());
			CXmlLayout* pXmlLayout = NULL;

			// Layer
			if ( XMLString::compareString(nodeName, "Texture") == 0 )
			{
				pXmlLayout = new CXmlTexture(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "FontString") == 0 )
			{
				pXmlLayout = new CXmlFontString(m_pXmlUi, this);
			}

			if (pXmlLayout)
			{
				if ( pXmlLayout->LoadFromDOMNode(node) )
				{
					// 子元件虛擬狀態需和父元件一致
					pXmlLayout->m_parent	= this;
					pXmlLayout->m_isVirtual = m_isVirtual;
					m_layers.insert( make_pair(level, pXmlLayout) );
				}
				else
					delete pXmlLayout;
			}			

			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::LoadXmlFrames(const DOMNode* node)
{
	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			CXmlFrame* pXmlFrame;
			char* nodeName = XMLString::transcode(node->getNodeName());

			pXmlFrame = NULL;
			if ( XMLString::compareString(nodeName, "Frame") == 0 )
			{
				pXmlFrame = new CXmlFrame(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "Model") == 0 )
			{
				pXmlFrame = new CXmlModel(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "Button") == 0 )
			{
				pXmlFrame = new CXmlButton(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "CheckButton") == 0 )
			{
				pXmlFrame = new CXmlCheckButton(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "RadioButton") == 0 )
			{
				pXmlFrame = new CXmlRadioButton(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "EditBox") == 0 )
			{
				pXmlFrame = new CXmlEditBox(m_pXmlUi, this);
			}			
			else if ( XMLString::compareString(nodeName, "Slider") == 0 )
			{
				pXmlFrame = new CXmlSlider(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "StatusBar") == 0 )
			{
				pXmlFrame = new CXmlStatusBar(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "MessageFrame") == 0 )
			{
				pXmlFrame = new CXmlMessageFrame(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "ListBox") == 0 )
			{
				pXmlFrame = new CXmlListBox(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "ScrollFrame") == 0 )
			{
				pXmlFrame = new CXmlScrollFrame(m_pXmlUi, this);
			}
			else if ( XMLString::compareString(nodeName, "GameTooltip") == 0 )
			{
				pXmlFrame = new CXmlTooltip(m_pXmlUi, this);
			}
			XMLString::release(&nodeName);

			if ( pXmlFrame )
			{
				if ( pXmlFrame->LoadFromDOMNode(node) )
				{
					// 子元件虛擬狀態需和父元件一致
					pXmlFrame->m_parent		= this;
					pXmlFrame->m_isVirtual	= m_isVirtual;
					m_frames.push_back(pXmlFrame);
				}
				else
					XML_SAFE_DLETE(pXmlFrame);
			}
		}
		node = node->getNextSibling();
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::LoadXmlScripts(const DOMNode* node)
{
	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{			
			char* nodeName = XMLString::transcode(node->getNodeName());

			// OnLoad
			if ( XMLString::compareString(nodeName, "OnLoad") == 0 )
			{
				InsertScript(eOn_Load, node->getTextContent());
			}
			// OnResize
			else if ( XMLString::compareString(nodeName, "OnResize") == 0 )
			{
				InsertScript(eOn_Resize, node->getTextContent());
			}
			// OnUpdate
			else if ( XMLString::compareString(nodeName, "OnUpdate") == 0 )
			{
				InsertScript(eOn_Update, node->getTextContent());
			}			
			// OnShow
			else if ( XMLString::compareString(nodeName, "OnShow") == 0 )
			{
				InsertScript(eOn_Show, node->getTextContent());
			}
			// OnHide
			else if ( XMLString::compareString(nodeName, "OnHide") == 0 )
			{
				InsertScript(eOn_Hide, node->getTextContent());
			}
			// OnTabPressed
			else if ( XMLString::compareString(nodeName, "OnTabPressed") == 0 )
			{
				InsertScript(eOn_TabPressed, node->getTextContent());
			}
			// OnEnterPressed
			else if ( XMLString::compareString(nodeName, "OnEnterPressed") == 0 )
			{
				InsertScript(eOn_EnterPressed, node->getTextContent());
			}
			// OnEscapePressed
			else if ( XMLString::compareString(nodeName, "OnEscapePressed") == 0 )
			{
				InsertScript(eOn_EscapePressed, node->getTextContent());
			}
			// OnSpacePressed
			else if ( XMLString::compareString(nodeName, "OnSpacePressed") == 0 )
			{
				InsertScript(eOn_SpacePressed, node->getTextContent());
			}
			// OnArrowUpPressed
			else if ( XMLString::compareString(nodeName, "OnArrowUpPressed") == 0 )
			{
				InsertScript(eOn_ArrowUpPressed, node->getTextContent());
			}
			// OnArrowDownPressed
			else if ( XMLString::compareString(nodeName, "OnArrowDownPressed") == 0 )
			{
				InsertScript(eOn_ArrowDownPressed, node->getTextContent());
			}
			// OnKeyDown
			else if ( XMLString::compareString(nodeName, "OnKeyDown") == 0 )
			{
				InsertScript(eOn_KeyDown, node->getTextContent());
			}
			// OnKeyUp
			else if ( XMLString::compareString(nodeName, "OnKeyUp") == 0 )
			{
				InsertScript(eOn_KeyUp, node->getTextContent());
			}
			// OnChar
			else if ( XMLString::compareString(nodeName, "OnChar") == 0 )
			{
				InsertScript(eOn_Char, node->getTextContent());
			}
			// OnMouseDown
			else if ( XMLString::compareString(nodeName, "OnMouseDown") == 0 )
			{
				InsertScript(eOn_MouseDown, node->getTextContent());
			}
			// OnMouseUp
			else if ( XMLString::compareString(nodeName, "OnMouseUp") == 0 )
			{
				InsertScript(eOn_MouseUp, node->getTextContent());
			}
			// OnClick
			else if ( XMLString::compareString(nodeName, "OnClick") == 0 )
			{
				InsertScript(eOn_Click, node->getTextContent());
			}
			// OnDoubleClick
			else if ( XMLString::compareString(nodeName, "OnDoubleClick") == 0 )
			{
				InsertScript(eOn_DoublebClick, node->getTextContent());
			}
			// OnMouseWheel
			else if ( XMLString::compareString(nodeName, "OnMouseWheel") == 0 )
			{
				InsertScript(eOn_MouseWheel, node->getTextContent());
			}
			// OnMouseEnter
			else if ( XMLString::compareString(nodeName, "OnEnter") == 0 )
			{
				InsertScript(eOn_MouseEnter, node->getTextContent());
			}
			// OnMouseLeave
			else if ( XMLString::compareString(nodeName, "OnLeave") == 0 )
			{
				InsertScript(eOn_MouseLeave, node->getTextContent());
			}
			// OnDragStart
			else if ( XMLString::compareString(nodeName, "OnDragStart") == 0 )
			{
				InsertScript(eOn_DragStart, node->getTextContent());
			}
			// OnDragStop
			else if ( XMLString::compareString(nodeName, "OnDragStop") == 0 )
			{
				InsertScript(eOn_DragStop, node->getTextContent());
			}
			// OnReceiveDrag
			else if ( XMLString::compareString(nodeName, "OnReceiveDrag") == 0 )
			{
				InsertScript(eOn_ReceiveDrag, node->getTextContent());
			}
			// OnValueChanged
			else if ( XMLString::compareString(nodeName, "OnValueChanged") == 0 )
			{
				InsertScript(eOn_ValueChanged, node->getTextContent());
			}
			// OnVerticalScroll			
			else if ( XMLString::compareString(nodeName, "OnVerticalScroll") == 0 )
			{
				InsertScript(eOn_VerticalScroll, node->getTextContent());
			}
			// OnHorizontalScroll
			else if ( XMLString::compareString(nodeName, "OnHorizontalScroll") == 0 )
			{
				InsertScript(eOn_HorizontalScroll, node->getTextContent());
			}
			// OnScrollRangeChanged
			else if ( XMLString::compareString(nodeName, "OnScrollRangeChanged") == 0 )
			{
				InsertScript(eOn_ScrollRangeChanged, node->getTextContent());
			}
			// OnTextChanged
			else if ( XMLString::compareString(nodeName, "OnTextChanged") == 0 )
			{
				InsertScript(eOn_TextChanged, node->getTextContent());
			}
			// OnTextChanged
			else if ( XMLString::compareString(nodeName, "OnEvent") == 0 )
			{
				InsertScript(eOn_Event, node->getTextContent());
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}
}

// ----------------------------------------------------------------------------
bool CXmlFrame::InsertScript(int key, const XMLCh* content)
{	
	bool hr = false;
	if ( content )
	{
		char* tmpStr = XMLString::transcode(content);

		// 去除前後空白
		int len = strlen(tmpStr);
		int st = 0, ed = len-1;
		for (int i = 0; i < len; i++ )
		{
			st = i;
			if (tmpStr[i] > ' ')
				break;
		}
		for (int i = len-1; i >= st; i--)
		{
			ed = i;
			if (tmpStr[i] > ' ')
				break;
		}

		if (ed >= st)
		{
			int size = ed - st + 1;
			char* script = new char[size + 1];
			memcpy(script, tmpStr + st, size);
			script[size] = 0;

			pair<map<int, string>::iterator, bool> hr;			
			hr = m_scripts.insert( make_pair(key, script) );
			if ( !hr.second )
			{
				m_scripts.erase( hr.first );
				hr = m_scripts.insert( make_pair(key, script) );
			}

			delete [] script;
		}

		XMLString::release(&tmpStr);
	}
	return false;
}

// ----------------------------------------------------------------------------
void CXmlFrame::InheritFrames(CXmlFrame* pXmlFrame)
{
	vector<CXmlFrame*>::iterator iter = pXmlFrame->m_frames.begin();
	for (; iter != pXmlFrame->m_frames.end(); iter++)
	{
		CXmlFrame* pXmlDesc = NULL;
		switch ((*iter)->GetType())
		{
		case eXmlUiType_Frame:
			pXmlDesc = new CXmlFrame(m_pXmlUi, this);
			break;
		case eXmlUiType_Model:
			pXmlDesc = new CXmlModel(m_pXmlUi, this);
			break;
		case eXmlUiType_Button:
			pXmlDesc = new CXmlButton(m_pXmlUi, this);
			break;
		case eXmlUiType_CheckButton:
			pXmlDesc = new CXmlCheckButton(m_pXmlUi, this);
			break;
		case eXmlUiType_RadioButton:
			pXmlDesc = new CXmlRadioButton(m_pXmlUi, this);
			break;
		case eXmlUiType_StatusBar:
			pXmlDesc = new CXmlStatusBar(m_pXmlUi, this);
			break;
		case eXmlUiType_Slider:
			pXmlDesc = new CXmlSlider(m_pXmlUi, this);
			break;
		case eXmlUiType_EditBox:
			pXmlDesc = new CXmlEditBox(m_pXmlUi, this);
			break;
		case eXmlUiType_ListBox:
			pXmlDesc = new CXmlListBox(m_pXmlUi, this);
			break;
		case eXmlUiType_MessageFrame:
			pXmlDesc = new CXmlMessageFrame(m_pXmlUi, this);
			break;
		}

		if (pXmlDesc)
		{
			pXmlDesc->Inherit(*iter);
			m_frames.push_back(pXmlDesc);
		}
	}
}

// ----------------------------------------------------------------------------
void CXmlFrame::InheritLayers(CXmlFrame* pXmlFrame)
{
	multimap<int, CXmlLayout*>::iterator iter = pXmlFrame->m_layers.begin();
	for ( ; iter != pXmlFrame->m_layers.end(); iter++ )
	{
		CXmlLayout* pXmlLayout = NULL;
		switch(iter->second->GetType())
		{
		case eXmlUiType_FontString:
			pXmlLayout = new CXmlFontString(m_pXmlUi, this);
			break;
		case eXmlUiType_Texture:
			pXmlLayout = new CXmlTexture(m_pXmlUi, this);
			break;
		}

		if (pXmlLayout)
		{
			pXmlLayout->Inherit(iter->second);
			m_layers.insert( make_pair(iter->first, pXmlLayout) );
		}
	}
}