/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelColumn.h  -  description
                             -------------------
    begin                : 19 March 2004
    copyright            : (C) 2004 by Alexey 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 <sptk3/excel/CExcelColumn.h>
#include <string.h>

using namespace std;

CExcelColumn::CExcelColumn(unsigned columnIndex) 
: CExcelRecord(COLINFO,12,new char[12],true)
{
   memset(m_data,0,12);
   CExcelRecord::setshort(m_data,0,columnIndex);  // first column in the range of columns
   CExcelRecord::setshort(m_data,2,columnIndex);  // last column in the range of columns
   CExcelRecord::setshort(m_data,4,8*256);        // column width in 1/256 of default font '0' character
}

unsigned CExcelColumn::index() const {
   return CExcelRecord::getshort(m_data,0);
}

unsigned CExcelColumn::width() const {
   return CExcelRecord::getshort(m_data,4) / 256;
}

void CExcelColumn::width(unsigned w) {
   CExcelRecord::setshort(m_data,4,w * 256);
}

void CExcelColumns::clear() {
   for (iterator itor = begin(); itor != end(); itor++)
      delete *itor;
   vector<CExcelColumn *>::clear();
}

void CExcelColumns::resize(unsigned newSize) {
   unsigned oldSize = size();
   unsigned i;
   for (i = newSize; i < oldSize; i++)
      delete (*this)[i];

   vector<CExcelColumn *>::resize(newSize);

   unsigned columnIndex = size();
   for (i = oldSize; i < newSize; i++) {
      (*this)[i] = new CExcelColumn(columnIndex);
      columnIndex++;
   }
}

void CExcelColumns::saveRecords(CExcelRecordsBuffer& records) {
   unsigned cnt = size();
   for (unsigned i = 0; i < cnt; i++) {
      const CExcelRecord *columnRecord = (*this)[i];
      records.append(columnRecord);
   }
}
