/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          ccolumn.cpp  -  description
                             -------------------
    begin                : Thu Apr 27 2000
    copyright            : (C) 2000-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at
   your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <stdlib.h>

#include <FL/Fl.H>
#include <sptk3/sptk.h>
#include <sptk3/CColumn.h>
#include <sptk3/CException.h>
#include <sptk3/string_ext.h>

using namespace std;
using namespace sptk;

CColumn::CColumn(const std::string cname,CVariantType type,short cwidth,bool cvisible) {
	m_name = cname;
	m_type = type;
	m_visible = cvisible;
	if (cwidth < 0) {
		m_autoWidth = true;
		m_width = 0;
	} else {
		m_autoWidth = false;
		m_width = cwidth;
	}
}

CColumn::CColumn(const CColumn& col) {
	m_name = col.m_name;
	m_type = col.m_type;
	m_width = col.m_width;
	m_visible = col.m_visible;
	m_autoWidth = col.m_autoWidth;
}

void CColumn::load(const CXmlNode& node) {
	m_name = node.getAttribute("caption");
	m_type = (CVariantType)(unsigned) node.getAttribute("type");
	m_width = node.getAttribute("width");
	m_visible = node.getAttribute("visible");
	m_autoWidth = node.getAttribute("auto_width");
}

void CColumn::save(CXmlNode& node) const {
    node.clear();
    node.name("column");
	node.attributes["caption"] = m_name;
	node.attributes["type"] = m_type;
	node.attributes["width"] = m_width;
	node.attributes["visible"] = m_visible;
	node.attributes["auto_width"] = m_autoWidth;
}

int CColumnList::indexOf(const char *colname) const {
	unsigned cnt = size();
	for (unsigned i = 0; i < cnt; i++) {
		const CColumn& column = operator[](i);
		if (column.name() == colname)
			return i;
	}
	return -1;
}

void CColumnList::load(const CXmlNode& node) {
    CXmlNode::const_iterator itor = node.begin();
    CXmlNode::const_iterator iend = node.end();
    resize(node.size());
	for (; itor != iend; itor++) {
		try {
            CXmlNode& columnNode = *(*itor);
            unsigned columnIndex = columnNode.getAttribute("index");
            if (columnIndex >= size()) continue;
            CColumn& column = (*this)[columnIndex];
            column.load(columnNode);
		}
		catch (...) {}
	}
}

void CColumnList::save(CXmlNode& node) const {
    node.clear();
    node.name("columns");
	unsigned counter = size();
	for (unsigned i = 0; i < counter; i++) {
		try {
			const CColumn& column = (*this)[i];
			CXmlNode& columnNode = *(new CXmlElement(node,"column"));
            column.save(columnNode);
            columnNode.attributes["index"] = i;
		}
		catch (...) {}
	}
}
