/*  $Id: treeitem_silver.cpp,v 1.14 2004/01/12 08:20:41 sphair Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 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
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser 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
**
*/

#include "API/Display/display.h"
#include "API/Display/font.h"
#include "API/Display/surface.h"
#include "API/GUI/treenode.h"
#include "API/GUI/treeview.h"
#include "API/GUIStyleSilver/treeitem_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"

/////////////////////////////////////////////////////////////////////////////
// Construction:

CL_TreeItem_Silver::CL_TreeItem_Silver(
	CL_TreeItem *treeview_item,
	CL_StyleManager_Silver *style)
:
	CL_ComponentStyle(treeview_item),
	item(treeview_item),
	style(style)
{
	CL_ResourceManager *resources = style->get_resources();
	font = new CL_Font("TreeViewItem/font", resources);
		
	slot_paint = item->sig_paint().connect(
		this, &CL_TreeItem_Silver::on_paint);
	slot_get_preferred_size = item->sig_get_preferred_size().connect(
		this, &CL_TreeItem_Silver::on_get_preferred_size);
}

CL_TreeItem_Silver::~CL_TreeItem_Silver()
{
	delete font;
}

/////////////////////////////////////////////////////////////////////////////
// Callbacks:

void CL_TreeItem_Silver::on_get_preferred_size(CL_Size &size)
{
	size.width = item->get_node()->get_treeview()->get_client_area()->get_width();
	size.height = item->get_custom_height();

	if(size.height == 0)
	{
		if(item->get_icon())
			size.height = item->get_icon()->get_height() + 2;
		else
			size.height = font->get_height(item->get_text()) + 2;
	}
}

void CL_TreeItem_Silver::on_paint()
{
	int height = item->get_height();
	int width = item->get_width();
	
	if(item->has_mouse_over())
		CL_Display::fill_rect(CL_Rect(0, 0, width, height), CL_Color(232, 236, 241));
	if(item->get_node()->is_selected())
		CL_Display::fill_rect(CL_Rect(0, 0, width, height), CL_Color(184, 195, 209, 150));

	int font_height = font->get_height(item->get_text(0));

	CL_TreeView *treeview = item->get_node()->get_treeview();
	int columns = treeview->get_column_count();
	if(columns == 0)
		columns = 1;

	// Draw columns
	for(int x = 0, i = 0; i < columns; ++i)
	{
		int dx = 0;
		if(i == 0)
		{
			dx += item->get_text_margin();

			CL_Surface *icon = item->get_icon();
			if(icon)
			{
				icon->draw(x + 1, (height - icon->get_height()) / 2);
				dx += icon->get_width();
			}
		}

		CL_Component *comp = item->get_component(i);
		if(comp)
		{
			comp->set_position(CL_Rect(x + dx + 4, 0, x + dx + 4 + treeview->get_column_width(i), height));
			comp->paint();
		}
		else
			font->draw(x + dx + 4, (height - font_height) / 2, item->get_text(i));

		x += treeview->get_column_width(i);
		if(i == 0)
			x -= item->get_node()->get_placement_offset();
	}
}
