Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgrid.h"
Toshihiro Shimizu 890ddd
#include "tw/colors.h"
Toshihiro Shimizu 890ddd
#include "tw/event.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TwConsts;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TGridColumn::Imp
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	vector<tgridcell> m_cells;</tgridcell>
Toshihiro Shimizu 890ddd
	string m_name;
Toshihiro Shimizu 890ddd
	TGridColumn::Alignment m_alignment;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Imp(const string &name, Alignment alignment)
Toshihiro Shimizu 890ddd
		: m_name(name), m_alignment(alignment) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~Imp() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Imp &operator=(const Imp &src)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_cells = src.m_cells;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	//not implemented
Toshihiro Shimizu 890ddd
	Imp(const Imp &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGridColumn::TGridColumn(const string &name, Alignment alignment)
Toshihiro Shimizu 890ddd
	: m_imp(new Imp(name, alignment))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGridColumn::~TGridColumn()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_imp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGridColumn *TGridColumn::createEmpty()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new TGridColumn();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGridColumn::getRowCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_imp->m_cells.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TGridCell &TGridColumn::getCell(int row) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(row >= 0 && row < (int)m_imp->m_cells.size());
Toshihiro Shimizu 890ddd
	return m_imp->m_cells[row];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGridColumn::setCell(int row, const TGridCell &cell)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_imp);
Toshihiro Shimizu 890ddd
	assert(row >= 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//checkColumn();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int firstRow = 0;
Toshihiro Shimizu 890ddd
	if (m_imp->m_cells.empty()) //se la colonna e' vuota
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//    if(!cell.isEmpty())
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			m_imp->m_cells.push_back(cell);
Toshihiro Shimizu 890ddd
			firstRow = row;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int oldCellCount = m_imp->m_cells.size();
Toshihiro Shimizu 890ddd
	assert(oldCellCount > 0);
Toshihiro Shimizu 890ddd
	int lastRow = firstRow + oldCellCount - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (row < firstRow) //prima
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//    if(cell.isEmpty()) return; //non faccio nulla
Toshihiro Shimizu 890ddd
		int delta = firstRow - row;
Toshihiro Shimizu 890ddd
		assert(delta > 0);
Toshihiro Shimizu 890ddd
		m_imp->m_cells.insert(m_imp->m_cells.begin(), delta - 1, TGridCell()); //celle vuote
Toshihiro Shimizu 890ddd
		m_imp->m_cells.insert(m_imp->m_cells.begin(), cell);				   //devo settare la prima comp. del vettore
Toshihiro Shimizu 890ddd
		firstRow = row;														   //row 'e la nuova firstrow
Toshihiro Shimizu 890ddd
																			   //    updateIcon();
Toshihiro Shimizu 890ddd
																			   //    checkColumn();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} else if (row > lastRow) //dopo
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//    if(cell.isEmpty()) return; //non faccio nulla
Toshihiro Shimizu 890ddd
		int count = row - lastRow - 1;
Toshihiro Shimizu 890ddd
		//se necessario, inserisco celle vuote
Toshihiro Shimizu 890ddd
		for (int i = 0; i < count; ++i)
Toshihiro Shimizu 890ddd
			m_imp->m_cells.push_back(TGridCell());
Toshihiro Shimizu 890ddd
		m_imp->m_cells.push_back(cell);
Toshihiro Shimizu 890ddd
		//    checkColumn();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//"[r0,r1]"
Toshihiro Shimizu 890ddd
	int index = row - firstRow;
Toshihiro Shimizu 890ddd
	assert(0 <= index && index < (int)m_imp->m_cells.size());
Toshihiro Shimizu 890ddd
	m_imp->m_cells[index] = cell;
Toshihiro Shimizu 890ddd
	//  if(index == 0) updateIcon();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  if(cell.isEmpty())
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    if(row==lastRow)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      // verifico la presenza di celle bianche alla fine     
Toshihiro Shimizu 890ddd
      while(!m_imp->m_cells.empty() && m_imp->m_cells.back().isEmpty()) 
Toshihiro Shimizu 890ddd
        m_imp->m_cells.pop_back();     
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
    else
Toshihiro Shimizu 890ddd
    if(row==firstRow)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      // verifico la presenza di celle bianche all'inizio
Toshihiro Shimizu 890ddd
      while(!m_imp->m_cells.empty() && m_imp->m_cells.front().isEmpty()) 
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
       m_imp->m_cells.erase(m_imp->m_cells.begin());  
Toshihiro Shimizu 890ddd
       firstRow++;
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
  
Toshihiro Shimizu 890ddd
    if(m_imp->m_cells.empty())
Toshihiro Shimizu 890ddd
      firstRow = 0;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  checkColumn();
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGridColumn::getCells(int row, int rowCount, TGridCell cells[]);
Toshihiro Shimizu 890ddd
void TGridColumn::setCells(int row, int rowCount, const TGridCell cells[]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGridColumn::insertEmptyCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_imp);
Toshihiro Shimizu 890ddd
	if (m_imp->m_cells.empty())
Toshihiro Shimizu 890ddd
		return; //se la colonna e' vuota non devo inserire celle
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int firstRow = 0;
Toshihiro Shimizu 890ddd
	if (row >= firstRow + (int)m_imp->m_cells.size())
Toshihiro Shimizu 890ddd
		return;			 //dopo:non inserisco nulla
Toshihiro Shimizu 890ddd
	if (row <= firstRow) //prima
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		firstRow += rowCount;
Toshihiro Shimizu 890ddd
	} else //in mezzo
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int delta = row - firstRow;
Toshihiro Shimizu 890ddd
		std::vector<tgridcell>::iterator it = m_imp->m_cells.begin();</tgridcell>
Toshihiro Shimizu 890ddd
		std::advance(it, delta);
Toshihiro Shimizu 890ddd
		m_imp->m_cells.insert(it, rowCount, TGridCell());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGridColumn::removeCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (rowCount <= 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_imp->m_cells.empty())
Toshihiro Shimizu 890ddd
		return; //se la colonna e' vuota
Toshihiro Shimizu 890ddd
	assert(m_imp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int firstRow = 0; //m_imp->m_first;
Toshihiro Shimizu 890ddd
	int cellCount = m_imp->m_cells.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (row >= firstRow + cellCount)
Toshihiro Shimizu 890ddd
		return; //sono "sotto" l'ultima cella
Toshihiro Shimizu 890ddd
	if (row < firstRow) {
Toshihiro Shimizu 890ddd
		if (row + rowCount <= firstRow) //"sono sopra la prima cella"
Toshihiro Shimizu 890ddd
		{								// aggiorno solo m_first
Toshihiro Shimizu 890ddd
			firstRow -= rowCount;
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		rowCount += row - firstRow; //rowCount = row+rowCount-firstRow;
Toshihiro Shimizu 890ddd
		firstRow = row;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(row >= firstRow);
Toshihiro Shimizu 890ddd
	// le celle sotto firstRow+cellCount sono gia' vuote
Toshihiro Shimizu 890ddd
	if (rowCount > firstRow + cellCount - row)
Toshihiro Shimizu 890ddd
		rowCount = firstRow + cellCount - row;
Toshihiro Shimizu 890ddd
	if (rowCount <= 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (row == firstRow) {
Toshihiro Shimizu 890ddd
		//cancello all'inizio
Toshihiro Shimizu 890ddd
		assert(rowCount <= cellCount);
Toshihiro Shimizu 890ddd
		std::vector<tgridcell>::iterator it = m_imp->m_cells.begin();</tgridcell>
Toshihiro Shimizu 890ddd
		std::vector<tgridcell>::iterator it2 = m_imp->m_cells.begin();</tgridcell>
Toshihiro Shimizu 890ddd
		std::advance(it2, rowCount);
Toshihiro Shimizu 890ddd
		//m_imp->m_cells.erase(&m_imp->m_cells[0],&m_imp->m_cells[rowCount]);
Toshihiro Shimizu 890ddd
		m_imp->m_cells.erase(it, it2);
Toshihiro Shimizu 890ddd
		// verifico la presenza di celle bianche all'inizio
Toshihiro Shimizu 890ddd
		while (!m_imp->m_cells.empty() /*&& m_imp->m_cells.front().isEmpty()*/) {
Toshihiro Shimizu 890ddd
			m_imp->m_cells.erase(m_imp->m_cells.begin());
Toshihiro Shimizu 890ddd
			firstRow++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// cancello dopo l'inizio
Toshihiro Shimizu 890ddd
		int d = row - firstRow;
Toshihiro Shimizu 890ddd
		std::vector<tgridcell>::iterator it = m_imp->m_cells.begin();</tgridcell>
Toshihiro Shimizu 890ddd
		std::vector<tgridcell>::iterator it2 = m_imp->m_cells.begin();</tgridcell>
Toshihiro Shimizu 890ddd
		std::advance(it, d);
Toshihiro Shimizu 890ddd
		std::advance(it2, d + rowCount);
Toshihiro Shimizu 890ddd
		//m_imp->m_cells.erase(&m_imp->m_cells[d],&m_imp->m_cells[d+rowCount]);
Toshihiro Shimizu 890ddd
		m_imp->m_cells.erase(it, it2);
Toshihiro Shimizu 890ddd
		if (row + rowCount == firstRow + cellCount) {
Toshihiro Shimizu 890ddd
			// verifico la presenza di celle bianche alla fine
Toshihiro Shimizu 890ddd
			while (!m_imp->m_cells.empty() /*&& m_imp->m_cells.back().isEmpty()*/) {
Toshihiro Shimizu 890ddd
				m_imp->m_cells.pop_back();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_imp->m_cells.empty()) {
Toshihiro Shimizu 890ddd
		firstRow = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//updateIcon();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGridColumn::clearCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string TGridColumn::getName() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_imp->m_name;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGridColumn::Alignment TGridColumn::getAlignment() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_imp->m_alignment;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TGrid::Data
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Data(TWidget *w) : m_w(w), m_selAction(0), m_dblClickAction(0), m_scrollbar(new TScrollbar(w)), m_yoffset(0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~Data()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_selAction)
Toshihiro Shimizu 890ddd
			delete m_selAction;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_dblClickAction)
Toshihiro Shimizu 890ddd
			delete m_dblClickAction;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int posToRow(const TPoint &p);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void updateScrollBarStatus()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (!m_scrollbar)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		int ly = m_w->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		assert(m_columnSet.getColumnCount() > 0);
Toshihiro Shimizu 890ddd
		TGridColumnP col = m_columnSet.getColumn(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((col->getRowCount() * m_rowHeight) > ly)
Toshihiro Shimizu 890ddd
			m_scrollbar->show(); //m_scrollbar->setValue(m_yoffset,0, yrange-ly, ly);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			m_scrollbar->hide(); //m_scrollbar->setValue(0,0, 0, 0);
Toshihiro Shimizu 890ddd
		m_scrollbar->invalidate();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TWidget *m_w;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TColumnSetT<tgridcolumn> m_columnSet;</tgridcolumn>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<int> m_selectedRows;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TGenericGridAction *m_selAction;
Toshihiro Shimizu 890ddd
	TGenericGridAction *m_dblClickAction;
Toshihiro Shimizu 890ddd
	TScrollbar *m_scrollbar;
Toshihiro Shimizu 890ddd
	int m_yoffset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_colSeparatorDragged;
Toshihiro Shimizu 890ddd
	int m_prevColSeparatorX;
Toshihiro Shimizu 890ddd
	int m_colSeparatorX;
Toshihiro Shimizu 890ddd
	int m_colIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static int m_rowHeight;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::Data::m_rowHeight = 20;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::Data::posToRow(const TPoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int y = m_w->getSize().ly - p.y + m_yoffset;
Toshihiro Shimizu 890ddd
	int item = y / m_rowHeight;
Toshihiro Shimizu 890ddd
	return item;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGrid::TGrid(TWidget *parent, string name)
Toshihiro Shimizu 890ddd
	: TWidget(parent, name), m_data(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data = new Data(this);
Toshihiro Shimizu 890ddd
	m_data->m_scrollbar->setAction(
Toshihiro Shimizu 890ddd
		new TScrollbarAction<tgrid>(this, &TGrid::scrollTo));</tgrid>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//setBackgroundColor(White);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TGrid::~TGrid()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_data;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::addColumn(const string &name, int width, TGridColumn::Alignment align)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TGridColumnP column = new TGridColumn(name, align);
Toshihiro Shimizu 890ddd
	column->setWidth(width);
Toshihiro Shimizu 890ddd
	m_data->m_columnSet.insertColumn(m_data->m_columnSet.getColumnCount(), column);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::addRow()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int colCount = m_data->m_columnSet.getColumnCount();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < colCount; ++i) {
Toshihiro Shimizu 890ddd
		TGridColumnP col = m_data->m_columnSet.getColumn(i);
Toshihiro Shimizu 890ddd
		col->setCell(col->getRowCount(), "");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::removeRow(int row)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int colCount = m_data->m_columnSet.getColumnCount();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < colCount; ++i) {
Toshihiro Shimizu 890ddd
		TGridColumnP col = m_data->m_columnSet.getColumn(i);
Toshihiro Shimizu 890ddd
		col->removeCells(row);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::getColIndex(const string &colName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int colCount = m_data->m_columnSet.getColumnCount();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < colCount; ++i) {
Toshihiro Shimizu 890ddd
		TGridColumnP col = m_data->m_columnSet.getColumn(i);
Toshihiro Shimizu 890ddd
		if (colName == col->getName())
Toshihiro Shimizu 890ddd
			return i;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::setCell(int row, int col, const string &text)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(row >= 0 && col >= 0);
Toshihiro Shimizu 890ddd
	TGridColumnP column = m_data->m_columnSet.touchColumn(col);
Toshihiro Shimizu 890ddd
	column->setCell(row, TGridCell(text));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::getColCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_data->m_columnSet.getColumnCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::getRowCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_data->m_columnSet.getColumnCount() > 0);
Toshihiro Shimizu 890ddd
	TGridColumnP col = m_data->m_columnSet.getColumn(0);
Toshihiro Shimizu 890ddd
	return col->getRowCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::getSelectedRowCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_data->m_selectedRows.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TGrid::getSelectedRowIndex(int i) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(i >= 0 && i < (int)m_data->m_selectedRows.size());
Toshihiro Shimizu 890ddd
	return m_data->m_selectedRows[i];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::select(int row, bool on)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(row >= 0 && row < (int)getRowCount());
Toshihiro Shimizu 890ddd
	std::vector<int>::iterator it =</int>
Toshihiro Shimizu 890ddd
		std::find(m_data->m_selectedRows.begin(), m_data->m_selectedRows.end(), row);
Toshihiro Shimizu 890ddd
	if (on) {
Toshihiro Shimizu 890ddd
		if (it == m_data->m_selectedRows.end())
Toshihiro Shimizu 890ddd
			m_data->m_selectedRows.push_back(row);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (it != m_data->m_selectedRows.end())
Toshihiro Shimizu 890ddd
			m_data->m_selectedRows.erase(it);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_data->m_selAction)
Toshihiro Shimizu 890ddd
		m_data->m_selAction->sendCommand(row);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::unselectAll()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data->m_selectedRows.clear();
Toshihiro Shimizu 890ddd
	invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TGrid::isSelected(int row) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::vector<int>::iterator it =</int>
Toshihiro Shimizu 890ddd
		std::find(m_data->m_selectedRows.begin(), m_data->m_selectedRows.end(), row);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return it != m_data->m_selectedRows.end();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::scrollTo(int y)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_data->m_yoffset == y)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_data->m_yoffset = y;
Toshihiro Shimizu 890ddd
	invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::draw()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TDimension size = getSize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//TRect gridHeaderPlacement(TPoint(0,size.ly-1), TDimension(size.lx-1, -m_data->m_rowHeight));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect gridHeaderPlacement(0, size.ly - 1 - m_data->m_rowHeight, size.lx - 1, size.ly - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setColor(Gray180 /*White*/);
Toshihiro Shimizu 890ddd
	fillRect(gridHeaderPlacement);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int gridHeaderY0 = gridHeaderPlacement.getP00().y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < m_data->m_columnSet.getColumnCount(); ++i) {
Toshihiro Shimizu 890ddd
		TGridColumnP col = m_data->m_columnSet.getColumn(i);
Toshihiro Shimizu 890ddd
		int x0, x1;
Toshihiro Shimizu 890ddd
		col->getCoords(x0, x1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// draw column header
Toshihiro Shimizu 890ddd
		setColor(Black);
Toshihiro Shimizu 890ddd
		drawText(TRect(x0, gridHeaderY0, x1, gridHeaderY0 + m_data->m_rowHeight - 1), col->getName());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int rowY = gridHeaderY0; // - m_data->m_rowHeight;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int y = size.ly - 1 - m_data->m_rowHeight;
Toshihiro Shimizu 890ddd
		for (int j = m_data->m_yoffset / m_data->m_rowHeight; j < col->getRowCount() && y; ++j, y -= m_data->m_rowHeight) {
Toshihiro Shimizu 890ddd
			drawHLine(TPoint(0, rowY), size.lx);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TRect cellRect(x0, rowY - m_data->m_rowHeight - 1, x1, rowY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (isSelected(j)) {
Toshihiro Shimizu 890ddd
				setColor(Blue, 1);
Toshihiro Shimizu 890ddd
				fillRect(cellRect);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			setColor(Black);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TWidget::Alignment align = TWidget::CENTER;
Toshihiro Shimizu 890ddd
			switch (col->getAlignment()) {
Toshihiro Shimizu 890ddd
			case TGridColumn::CenterAlignment:
Toshihiro Shimizu 890ddd
				align = TWidget::CENTER;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case TGridColumn::RightAlignment:
Toshihiro Shimizu 890ddd
				align = TWidget::END;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			case TGridColumn::LeftAlignment:
Toshihiro Shimizu 890ddd
				align = TWidget::BEGIN;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			drawText(cellRect, col->getCell(j).m_text, align);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			rowY -= m_data->m_rowHeight;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		setColor(Gray210);
Toshihiro Shimizu 890ddd
		drawVLine(TPoint(x0, 0), size.ly);
Toshihiro Shimizu 890ddd
		drawVLine(TPoint(x1, 0), size.ly);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_data->m_colSeparatorDragged) {
Toshihiro Shimizu 890ddd
		drawVLine(TPoint(m_data->m_colSeparatorX, 0), size.ly);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::configureNotify(const TDimension &d)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data->m_scrollbar->setGeometry(d.lx - 20, 1, d.lx - 2, d.ly - 2);
Toshihiro Shimizu 890ddd
	m_data->updateScrollBarStatus();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::leftButtonDown(const TMouseEvent &e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data->m_colSeparatorDragged = false;
Toshihiro Shimizu 890ddd
	int rowCount = getRowCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i = m_data->posToRow(e.m_pos);
Toshihiro Shimizu 890ddd
	if (i == 0) {
Toshihiro Shimizu 890ddd
		// la riga 0 e' l'header della tabella
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int j = 0; j < m_data->m_columnSet.getColumnCount(); ++j) {
Toshihiro Shimizu 890ddd
			TGridColumnP col = m_data->m_columnSet.getColumn(j);
Toshihiro Shimizu 890ddd
			int x0, x1;
Toshihiro Shimizu 890ddd
			col->getCoords(x0, x1);
Toshihiro Shimizu 890ddd
			if (x0 - 3 < e.m_pos.x && e.m_pos.x < x0 + 3) {
Toshihiro Shimizu 890ddd
				m_data->m_colSeparatorDragged = true;
Toshihiro Shimizu 890ddd
				m_data->m_colSeparatorX = x0;
Toshihiro Shimizu 890ddd
				m_data->m_prevColSeparatorX = x0;
Toshihiro Shimizu 890ddd
				m_data->m_colIndex = j;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		i--;
Toshihiro Shimizu 890ddd
		if (i >= 0 && i < (int)rowCount) {
Toshihiro Shimizu 890ddd
			if (!e.isShiftPressed())
Toshihiro Shimizu 890ddd
				unselectAll();
Toshihiro Shimizu 890ddd
			select(i, !isSelected(i));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::leftButtonDrag(const TMouseEvent &e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_data->m_colSeparatorDragged) {
Toshihiro Shimizu 890ddd
		m_data->m_colSeparatorX += (e.m_pos.x - m_data->m_prevColSeparatorX);
Toshihiro Shimizu 890ddd
		m_data->m_prevColSeparatorX = e.m_pos.x;
Toshihiro Shimizu 890ddd
		invalidate();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGrid::leftButtonUp(const TMouseEvent &e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_data->m_colSeparatorDragged) {
Toshihiro Shimizu 890ddd
		TGridColumnP col0 = m_data->m_columnSet.getColumn(m_data->m_colIndex - 1);
Toshihiro Shimizu 890ddd
		TGridColumnP col1 = m_data->m_columnSet.getColumn(m_data->m_colIndex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int col0X0, col0X1;
Toshihiro Shimizu 890ddd
		col0->getCoords(col0X0, col0X1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int newWidth = m_data->m_colSeparatorX - col0X0;
Toshihiro Shimizu 890ddd
		int totWidth = col0->getWidth() + col1->getWidth();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		col0->setWidth(newWidth);
Toshihiro Shimizu 890ddd
		col1->setWidth(totWidth - newWidth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_data->m_colSeparatorDragged = false;
Toshihiro Shimizu 890ddd
		invalidate();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}