/*
 * Copyright (c) 2002 Robert Collins.
 * Copyright (c) 2005-2006 Tom N Harris.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by Robert Collins <robertc@hotmail.com>
 * Written by Tom N Harris <telliamed@whoopdedo.org>
 *
 */

#include "PickCategoryLine.h"
#include "PickView.h"

using namespace Setup;
using namespace std;

PickLine *
PickCategoryLine::find(const char* key)
{
	for (vector<PickLine*>::iterator i = m_contents.begin(); 
	     i != m_contents.end(); ++i)
	{
		     if ((*i)->key() == key)
			return *i;
	}
	return NULL;
}

void
PickCategoryLine::clear(void)
{
	for (vector<PickLine*>::iterator i = m_contents.begin(); 
	     i != m_contents.end(); ++i)
		delete *i;
	m_contents.clear();
}

void
PickCategoryLine::paint(HDC hdc, int x, int y, int row, int indent)
{
	int r = y + row * m_view.m_rowheight;
	int by = r + m_view.m_textmetrics.tmHeight - CHECK_SIZE;
	int accum_row = 0;

	if (! m_label.empty())
	{
		//XXX: How cheap. Use a bitmap instead.
		//String temp = (String(TEXT("+ ")) + m_label);
		DrawCheck(m_view, hdc, NULL, x + indent, by, getCheckState());
		::TextOut(hdc, x + indent + CHECK_SIZE + ICON_MARGIN + HMARGIN / 2, r,
				  m_label.c_str(), m_label.size());
		indent += CATEGORY_INDENT;
		++accum_row;
	}

	if (m_collapsed)
		return;

	for (vector<PickLine*>::iterator i = m_contents.begin(); 
	     i != m_contents.end(); ++i)
	{
		(*i)->paint(hdc, x, r, accum_row, indent);
		accum_row += (*i)->itemCount();
	}
}

int
PickCategoryLine::click(int myrow, int ClickedRow, int x, int indent)
{
	if (m_label.empty())
	{
		int accum_row = myrow;
		for (vector<PickLine*>::iterator i = m_contents.begin(); 
		     i != m_contents.end(); ++i)
		{
			if (accum_row + (*i)->itemCount() > ClickedRow)
				return (*i)->click(accum_row, ClickedRow, x, indent);
			accum_row += (*i)->itemCount();
		}
		return 0;
	}
	if (myrow == ClickedRow)
	{
// XXX: This is just plain wrong.
		if (x < indent + CHECK_SIZE + ICON_MARGIN)
		{
			setCheckState(NextCheckState(getCheckState()));
			return 1;
		}
		/* I don't think I want this right now.
		else
		{
			m_collapsed = ! m_collapsed;
			int accum_row = 0;
			for (vector<PickLine*>::iterator i = m_contents.begin(); 
			     i != m_contents.end(); ++i)
				accum_row += (*i)->itemCount();
			return m_collapsed ? accum_row : -accum_row;
		}
		*/
	}
	else
	{
		indent += CATEGORY_INDENT;
		int accum_row = myrow + 1;
		for (vector<PickLine*>::iterator i = m_contents.begin(); 
		     i != m_contents.end(); ++i)
		{
			if (accum_row + (*i)->itemCount() > ClickedRow)
				return (*i)->click(accum_row, ClickedRow, x, indent);
			accum_row += (*i)->itemCount();
		}
	}
	return 0;
}

int 
PickCategoryLine::itemCount() const
{
	int t = (! m_label.empty()) ? 1 : 0;
	for (vector<PickLine*>::const_iterator i = m_contents.begin(); 
	     i != m_contents.end(); ++i)
		t += (*i)->itemCount();
	return t;
};

PickLine::CheckState 
PickCategoryLine::getCheckState() const
{
	vector<PickLine*>::const_iterator i = m_contents.begin();
	if (i == m_contents.end())
		return CheckStateUnchecked;
	CheckState checked = (*i)->getCheckState();
	if (checked == CheckStateIndeterminate)
		return checked;
	++i;
	for (; i != m_contents.end(); ++i)
	{
		if (checked != (*i)->getCheckState())
			return CheckStateIndeterminate;
	}
	return checked;
}

void 
PickCategoryLine::setCheckState(PickLine::CheckState checked)
{
	for (vector<PickLine*>::iterator i = m_contents.begin(); 
	     i != m_contents.end(); ++i)
		(*i)->setCheckState(checked);
}

PickLine *
PickCategoryLine::getRow(int row)
{
	int accum_row = 0;
	for (vector<PickLine*>::iterator i = m_contents.begin(); 
		 i != m_contents.end(); ++i)
	{
		if (accum_row == row)
			return *i;
		++accum_row;
		if ((*i)->isContainer())
		{
			if (accum_row + (*i)->itemCount() > row)
				return (*i)->getRow(row - accum_row);
			accum_row += (*i)->itemCount();
		}
	}
	return NULL;
}

