/*  $Id: radiobutton_silver.cpp,v 1.5 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/Core/Resources/resource_manager.h"
#include "API/GUIStyleSilver/radiobutton_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"

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

CL_RadioButton_Silver::CL_RadioButton_Silver(
	CL_RadioButton *radiobutton,
	CL_StyleManager_Silver *style)
:
	CL_ComponentStyle(radiobutton),
	radiobutton(radiobutton),
	style(style)
{
	resources = style->get_resources();

	sur_checked = new CL_Surface("RadioButton/surface_checked", resources);
	sur_unchecked = new CL_Surface("RadioButton/surface_unchecked", resources);
	sur_checked_disabled = new CL_Surface("RadioButton/surface_checked_disabled", resources);
	sur_unchecked_disabled = new CL_Surface("RadioButton/surface_unchecked_disabled", resources);
	font = new CL_Font("RadioButton/font", resources);

	slot_paint = radiobutton->sig_paint().connect_virtual(
		this, &CL_RadioButton_Silver::on_paint);

	slot_get_preferred_size = radiobutton->sig_get_preferred_size().connect(
		this, &CL_RadioButton_Silver::on_get_preferred_size);
}

CL_RadioButton_Silver::~CL_RadioButton_Silver()
{
	delete font;
	delete sur_checked;
	delete sur_unchecked;
	delete sur_checked_disabled;
	delete sur_unchecked_disabled;
}

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

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

void CL_RadioButton_Silver::on_paint(CL_SlotParent_v0 &super)
{
	CL_Surface *show_surface;

	if (radiobutton->is_enabled())
	{
		if (radiobutton->is_checked())
			show_surface = sur_checked;
		else
			show_surface = sur_unchecked;

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

	show_surface->draw(0, 0);

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