/*  $Id: checkbox_silver.cpp,v 1.8 2004/01/03 23:03:22 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/surface.h"
#include "API/Display/font.h"
#include "API/GUIStyleSilver/checkbox_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"
#include "API/Core/Resources/resource_manager.h"
#include "API/Core/XML/dom_element.h"

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

CL_CheckBox_Silver::CL_CheckBox_Silver(
	CL_CheckBox *checkbox,
	CL_StyleManager_Silver *style)
: 
	CL_ComponentStyle(checkbox),
	checkbox(checkbox),
	style(style),
	font(0),
	font_disabled(0),
	delete_font(false),
	delete_font_disabled(false),
	sur_checked(0),
	sur_unchecked(0),
	sur_checked_disabled(0), 
	sur_unchecked_disabled(0),
	delete_sur_checked(false),
	delete_sur_unchecked(false),
	delete_sur_checked_disabled(false),
	delete_sur_unchecked_disabled(false)
{
	resources = style->get_resources();

	if(resources->exists("CheckBox/surface_checked"))
		sur_checked = new CL_Surface("CheckBox/surface_checked", resources);
	if(resources->exists("CheckBox/surface_unchecked"))
		sur_unchecked = new CL_Surface("CheckBox/surface_unchecked", resources);
	if(resources->exists("CheckBox/surface_checked_disabled"))
		sur_checked_disabled = new CL_Surface("CheckBox/surface_checked_disabled", resources);
	if(resources->exists("CheckBox/surface_unchecked_disabled"))
		sur_unchecked_disabled = new CL_Surface("CheckBox/surface_unchecked_disabled", resources);
	if(resources->exists("CheckBox/font"))
		font = new CL_Font("CheckBox/font", resources);
	if(resources->exists("CheckBox/font_disabled"))
		font_disabled = new CL_Font("CheckBox/font_disabled", resources);

	slot_set_options = checkbox->sig_set_options().connect(
		this, &CL_CheckBox_Silver::on_set_options);
	slot_paint = checkbox->sig_paint().connect_virtual(
		this, &CL_CheckBox_Silver::on_paint);
	slot_get_preferred_size = checkbox->sig_get_preferred_size().connect(
		this, &CL_CheckBox_Silver::on_get_preferred_size);
}

CL_CheckBox_Silver::~CL_CheckBox_Silver()
{
	set_font(0);
	set_font_disabled(0);
	set_surface_checked(0);
	set_surface_unchecked(0);
	set_surface_checked_disabled(0);
	set_surface_unchecked_disabled(0);
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

void CL_CheckBox_Silver::set_font(CL_Font *_font, bool _delete_font)
{
	if(delete_font && font)
		delete font;
	font = _font;
	delete_font = _delete_font;
}

void CL_CheckBox_Silver::set_font_disabled(CL_Font *_font, bool _delete_font)
{
	if(delete_font_disabled && font_disabled)
		delete font_disabled;
	font_disabled = _font;
	delete_font_disabled = _delete_font;
}

void CL_CheckBox_Silver::set_surface_checked(CL_Surface *_surface, bool _delete_surface)
{
	if(delete_sur_checked && sur_checked)
		delete sur_checked;
	sur_checked = _surface;
	delete_sur_checked = _delete_surface;
}

void CL_CheckBox_Silver::set_surface_unchecked(CL_Surface *_surface, bool _delete_surface)
{
	if(delete_sur_unchecked && sur_unchecked)
		delete sur_unchecked;
	sur_unchecked = _surface;
	delete_sur_unchecked = _delete_surface;
}

void CL_CheckBox_Silver::set_surface_checked_disabled(CL_Surface *_surface, bool _delete_surface)
{
	if(delete_sur_checked_disabled && sur_checked_disabled)
		delete sur_checked_disabled;
	sur_checked_disabled = _surface;
	delete_sur_checked_disabled = _delete_surface;
}

void CL_CheckBox_Silver::set_surface_unchecked_disabled(CL_Surface *_surface, bool _delete_surface)
{
	if(delete_sur_unchecked_disabled && sur_unchecked_disabled)
		delete sur_unchecked_disabled;
	sur_unchecked_disabled = _surface;
	delete_sur_unchecked_disabled = _delete_surface;
}

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

void CL_CheckBox_Silver::on_set_options(const CL_DomElement &options)
{
	if (options.has_attribute("surface_checked"))
		set_surface_checked(new CL_Surface(options.get_attribute("surface_checked"), resources), true);
	if (options.has_attribute("surface_unchecked"))
		set_surface_unchecked(new CL_Surface(options.get_attribute("surface_unchecked"), resources), true);
	if (options.has_attribute("surface_checked_disabled"))
		set_surface_checked_disabled(new CL_Surface(options.get_attribute("surface_checked_disabled"), resources), true);
	if (options.has_attribute("surface_unchecked_disabled"))
		set_surface_unchecked_disabled(new CL_Surface(options.get_attribute("surface_checked_disabled"), resources), true);
	if (options.has_attribute("font"))
		set_font(new CL_Font(options.get_attribute("font"), resources), true);
	if (options.has_attribute("font_disabled"))
		set_font_disabled(new CL_Font(options.get_attribute("font_disabled"), resources), true);
}

void CL_CheckBox_Silver::on_get_preferred_size(CL_Size &size)
{
	CL_Surface *s = sur_checked;
	size.width = s->get_width() + font->get_width(checkbox->get_text()) + 8;
	size.height = font->get_height() + 4;
	if(size.height < (int)s->get_height())
		size.height = s->get_height();
}

void CL_CheckBox_Silver::on_paint(CL_SlotParent_v0 &super)
{
	CL_Surface *show_surface;
	CL_Font *show_font;
	
	if (checkbox->is_enabled())
	{
		if (checkbox->is_checked())
			show_surface = sur_checked;
		else
			show_surface = sur_unchecked;

		show_font = font;
		// Focus
//		if(checkbox->has_focus())
//			style->fill_rect(0, 0, checkbox->get_width(), show_surface->get_height(), GUICOLOR_FOCUS);
	}
	else
	{
		if (checkbox->is_checked())
			show_surface = sur_checked_disabled;
		else
			show_surface = sur_unchecked_disabled;

		if(font_disabled)
			show_font = font_disabled;
		else
			show_font = font;
	}

	show_surface->draw(0, 0);

	if (checkbox->get_text().length() > 0)
		show_font->draw(4 + show_surface->get_width(), 0, checkbox->get_text());
}
