/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          ccolumn.cpp  -  description
                             -------------------
    begin                : Thu Apr 27 2000
    copyright            : (C) 2000-2005 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;

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;
}

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::loadFromRegistry(CRegistry& registry,const char *topic) {
   registry.topic(topic);
   unsigned counter = registry["Number of columns"];
   for (unsigned i = 0; i < counter; i++) {
      std::string columnNumber = "Column "+int2string(i);
      try {
         CRegistryItem& columnData = registry[columnNumber.c_str()];
         unsigned columnWidth = columnData;
         string&  columnName = columnData["caption"];
         if (!columnName.empty()) {
            if (columnWidth > 400) columnWidth = 400;
            int columnIndex = indexOf(columnName.c_str());
            if (columnIndex >= 0)
               (*this)[columnIndex].width(columnWidth);
         }
      }
      catch (...) {
      }
   }
}

void CColumnList::saveToRegistry(CRegistry& registry,const char *topic) {
   registry.topic(topic);
   unsigned counter = size();
   registry["Number of columns"] = counter;
   for (unsigned i = 0; i < counter; i++) {
      std::string columnNumber = "Column "+int2string(i);
      try {
         CColumn& column = (*this)[i];
         CRegistryItem& columnData = registry[columnNumber.c_str()];
         columnData = column.width();
         columnData["caption"] = column.name();
      }
      catch (...) {
      }
   }
}
