// XmlGuiGridInfo.cpp: implementation of the CXmlGuiGridInfo class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlGuiGridInfo.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXmlGuiGridInfo::CXmlGuiGridInfo():
	colCountEnabled_(false),
	buttonBarEnabled_(false),
	zeroAlignedCount_(false)
{

}

CXmlGuiGridInfo::~CXmlGuiGridInfo()
{
	Clear();
}

bool CXmlGuiGridInfo::BuildFromXml(IXMLDOMNode* node)
{
	Clear();

	IXMLDOMNode* tmp;
	cmString dummy;

	tmp = GetChildNode(node, CXmlBaseElement::GetXmlNodeName(), dummy);
	assert(tmp);
	if (tmp) {
		CXmlBaseElement::BuildFromXml(tmp);
		tmp->Release();
	}

	ExtractMemberNode(node, _T("ColumnCountEnabled"), colCountEnabled_);
	ExtractMemberNode(node, _T("ButtonBarEnabled"), buttonBarEnabled_);
	ExtractMemberNode(node, _T("ZeroAlignedCount"), zeroAlignedCount_);
		
	cmString value;
	IXMLDOMNode* columnNode;
	IXMLDOMNodeList* columnList;
	CComBSTR columnNodeName;
	CComVariant columnValue;
	long j;
	long len;
	HRESULT hRes;

	tmp = GetChildNode(node, _T("ColumnList"), value);
	
	hRes = tmp->get_childNodes(&columnList);
	tmp->Release();

	hRes = columnList->get_length(&len);
	for (j = 0; j < len; j++) {
		hRes = columnList->get_item(j, &columnNode);
		hRes = columnNode->get_nodeName(&columnNodeName);
		
		if (wcscmp(columnNodeName, _T("Column")) == 0) {
			hRes= columnNode->get_nodeTypedValue(&columnValue);
			columns_.push_back(columnValue.bstrVal);
		}

		columnNode->Release();
	}
	
	columnList->Release();

	return true;
}

bool CXmlGuiGridInfo::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, 
										  IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	IXMLDOMElement* columnListElement;
	IXMLDOMNode* newOutNode;
	bool ret;
	HRESULT hr;

	hr = xmlDoc->createElement(CComBSTR((const TCHAR*)GetXmlNodeName()), &newElement);

	ret = CXmlBaseElement::AppendToDOMDocument(xmlDoc, newElement);
	if (!ret)
		return false;

	AppendMemberNode(_T("ColumnCountEnabled"), colCountEnabled_, newElement, xmlDoc);
	AppendMemberNode(_T("ButtonBarEnabled"), buttonBarEnabled_, newElement, xmlDoc);
	AppendMemberNode(_T("ZeroAlignedCount"), zeroAlignedCount_, newElement, xmlDoc);
	
	hr  = xmlDoc->createElement(CComBSTR(_T("ColumnList")), &columnListElement);

	for (int i = 0; i < columns_.size(); i++)
		AppendMemberNode(_T("Column"), columns_[i], columnListElement, xmlDoc);
	
	hr = newElement->appendChild(columnListElement, &newOutNode);
	newOutNode->Release();

	// append node to parent element
	//
	if (parentElement)
		hr = parentElement->appendChild(newElement, &newOutNode);
	else
		hr = xmlDoc->appendChild(newElement, &newOutNode);
	newOutNode->Release();

	return ret;
}

void CXmlGuiGridInfo::Clear()
{
	columns_.clear();

	colCountEnabled_ = false;
	buttonBarEnabled_ = false;
}

void CXmlGuiGridInfo::SetDefaults()
{
	Clear();

	colCountEnabled_ = false;
	buttonBarEnabled_ = false;
}

GridColumns* CXmlGuiGridInfo::GetColumns()
{
	return &columns_;
}

bool CXmlGuiGridInfo::GetColumnCountEnabled() const
{
	return colCountEnabled_;
}

void CXmlGuiGridInfo::SetColumnCountEnabled(bool enabled)
{
	colCountEnabled_ = enabled;
}

bool CXmlGuiGridInfo::GetButtonBarEnabled() const
{
	return buttonBarEnabled_;
}

void CXmlGuiGridInfo::SetButtonBarEnabled(bool enabled)
{
	buttonBarEnabled_ = enabled;
}

bool CXmlGuiGridInfo::GetZeroAlignedCount() const
{
	return zeroAlignedCount_;
}

void CXmlGuiGridInfo::SetZeroAlignedCount(bool zeroAlignedCount)
{
	zeroAlignedCount_ = zeroAlignedCount;
}