// XmlTreeIcons.cpp: implementation of the CXmlTreeIcons class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlTreeIcons.h"
#include "CPathSplit.h"

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

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

CXmlTreeIcons::CXmlTreeIcons()
{
}

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

void CXmlTreeIcons::Clear()
{
	iconExpandedSelected_ = _T("");
	iconExpandedNotSelected_ = _T("");
	iconCollapsedSelected_ = _T("");
	iconCollapsedNotSelected_ = _T("");
	expandedAtStartup_ = true;
}

void CXmlTreeIcons::SetDefaults()
{
	iconExpandedSelected_ = _T("");
	iconExpandedNotSelected_ = _T("");
	iconCollapsedSelected_ = _T("");
	iconCollapsedNotSelected_ = _T("");
	expandedAtStartup_ = true;
}

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

	CPathSplit p;	// older version used a full filename (with path) so we have
					// to extract only the filename (es icofile.ico)

	ExtractMemberNode(node, _T("IconExpandedSelected"), iconExpandedSelected_);
	if (iconExpandedSelected_.Find(_T(":\\")) != -1) {
		p.Split(iconExpandedSelected_);
		iconExpandedSelected_ = p.GetFileName() + p.GetExtension();
	}
	
	ExtractMemberNode(node, _T("IconExpandedNotSelected"), iconExpandedNotSelected_);
	if (iconExpandedNotSelected_.Find(_T(":\\")) != -1) {
		p.Split(iconExpandedNotSelected_);
		iconExpandedNotSelected_ = p.GetFileName() + p.GetExtension();
	}

	ExtractMemberNode(node, _T("IconCollapsedSelected"), iconCollapsedSelected_);
	if (iconCollapsedSelected_.Find(_T(":\\")) != -1) {
		p.Split(iconCollapsedSelected_);	
		iconCollapsedSelected_ = p.GetFileName() + p.GetExtension();
	}

	ExtractMemberNode(node, _T("IconCollapsedNotSelected"), iconCollapsedNotSelected_);
	if (iconCollapsedNotSelected_.Find(_T(":\\")) != -1) {
		p.Split(iconCollapsedNotSelected_);
		iconCollapsedNotSelected_ = p.GetFileName() + p.GetExtension();
	}

	ExtractMemberNode(node, _T("ExpandedAtStartup"), expandedAtStartup_);

	return true;
}

bool CXmlTreeIcons::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	HRESULT hr;

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

	AppendMemberNode(_T("IconExpandedSelected"), iconExpandedSelected_, newElement, xmlDoc);
	AppendMemberNode(_T("IconExpandedNotSelected"), iconExpandedNotSelected_, newElement, xmlDoc);
	AppendMemberNode(_T("IconCollapsedSelected"), iconCollapsedSelected_, newElement, xmlDoc);
	AppendMemberNode(_T("IconCollapsedNotSelected"), iconCollapsedNotSelected_, newElement, xmlDoc);
	AppendMemberNode(_T("ExpandedAtStartup"), expandedAtStartup_, newElement, xmlDoc);
		
	IXMLDOMNode* newOutNode;

	if (parentElement)
		hr = parentElement->appendChild(newElement, &newOutNode);
	else
		hr = xmlDoc->appendChild(newElement, &newOutNode);
	newOutNode->Release();	
   
	return true;
}

void CXmlTreeIcons::SetIconExpandedSelected(const cmString& iconFileName)
{
	iconExpandedSelected_ = iconFileName;
}

cmString CXmlTreeIcons::GetIconExpandedSelected() const
{
	return iconExpandedSelected_;
}

void CXmlTreeIcons::SetIconExpandedNotSelected(const cmString& iconFileName)
{
	iconExpandedNotSelected_ = iconFileName;
}

cmString CXmlTreeIcons::GetIconExpandedNotSelected() const
{
	return iconExpandedNotSelected_;
}

void CXmlTreeIcons::SetIconCollapsedSelected(const cmString& iconFileName)
{
	iconCollapsedSelected_ = iconFileName;
}

cmString CXmlTreeIcons::GetIconCollapsedSelected() const
{
	return iconCollapsedSelected_;
}

void CXmlTreeIcons::SetIconCollapsedNotSelected(const cmString& iconFileName)
{
	iconCollapsedNotSelected_ = iconFileName;
}

cmString CXmlTreeIcons::GetIconCollapsedNotSelected() const
{
	return iconCollapsedNotSelected_;
}

void CXmlTreeIcons::SetExpandedAtStartup(bool expanded)
{
	expandedAtStartup_ = expanded;
}

bool CXmlTreeIcons::GetExpandedAtStartup() const
{
	return expandedAtStartup_;
}
