/*
 * 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 "PickItemLine.h"
#include "PickView.h"

using namespace Setup;
using namespace std;

PickItemLine::PickItemLine(PickView & aView, std::string const & aKey)
	: PickLine(aKey), m_view(aView), m_checkstate(CheckStateUnchecked)
{
	m_columns.resize(aView.getNumColumns());
}

void
PickItemLine::setColumn(int col, String const & text)
{
	if (size_t(col) >= m_columns.size())
		m_columns.resize(col + 1);
	m_columns[col] = text;
}

void
PickItemLine::paint(HDC hdc, int x, int y, int row, int indent)
{
	int r = y + row * m_view.m_rowheight;
	int rb = r + m_view.m_textmetrics.tmHeight;
	int by = rb - 11; // top of box images
	int oldDC = ::SaveDC(hdc);
	if (!oldDC)
		return;
	HRGN oldClip = ::CreateRectRgn(0, 0, 0, 0);
	if (::GetRandomRgn(hdc, oldClip, SYSRGN) == -1)
	{
		::RestoreDC(hdc, oldDC);
		return;
	}
	HRGN oldClip2;
	if (Win32::IsNT())
	{
		unsigned int regionsize = ::GetRegionData(oldClip, 0, 0);
		LPRGNDATA oldClipData = reinterpret_cast<LPRGNDATA>(::operator new(regionsize));
		if (::GetRegionData(oldClip, regionsize, oldClipData) > regionsize)
		{
			::operator delete(oldClipData);
			::RestoreDC(hdc, oldDC);
			::DeleteObject(oldClip);
			return;
		}
		for (unsigned int n = 0; n < oldClipData->rdh.nCount; n++)
			for (unsigned int t = 0; t < 2; t++)
				::ScreenToClient(::WindowFromDC(hdc), 
						 &((POINT *)oldClipData->Buffer)[t + n * 2]);

		oldClip2 = ::ExtCreateRegion(NULL, regionsize, oldClipData);
		::operator delete(oldClipData);
	}
	else 
		oldClip2 = oldClip;

	::SelectClipRgn(hdc, oldClip2);
	if (m_columns.size() > 0)
	{
		int colx = x + m_view.m_headers.front().x;
		DrawCheck(m_view, hdc, oldClip2, colx + indent, by, m_checkstate);
		int labelx = colx + indent + CHECK_SIZE + ICON_MARGIN;
		::IntersectClipRect(hdc, labelx, r, colx + m_view.m_headers.front().width, rb);
		::TextOut(hdc, labelx + HMARGIN / 2, r, m_columns.front().c_str(), m_columns.front().size());
		::SelectClipRgn(hdc, oldClip2);

		for (size_t num_column = 1; num_column < m_columns.size(); ++num_column)
		{
			colx = x + m_view.m_headers[num_column].x;
			::IntersectClipRect(hdc, colx, r, colx + m_view.m_headers[num_column].width, rb);
			::TextOut(hdc, colx + HMARGIN / 2, r, m_columns[num_column].c_str(), m_columns[num_column].size());
			::SelectClipRgn(hdc, oldClip2);
		}
	}
	else
	{
		DrawCheck(m_view, hdc, oldClip2, x + indent, by, m_checkstate);
		String label(m_key);
		::TextOut(hdc, x + indent + CHECK_SIZE + ICON_MARGIN, r, label.c_str(), label.size());
	}

	::DeleteObject(oldClip);
	::DeleteObject(oldClip2);
	::RestoreDC(hdc, oldDC);
}

int
PickItemLine::click(int const myrow, int const ClickedRow, int const x, int indent)
{
	if (x < indent + CHECK_SIZE + ICON_MARGIN)
	{
		setCheckState(NextCheckState(getCheckState()));
		return 1;
	}
	return 0;
}

