/*  $Id: image_silver.cpp,v 1.3 2004/03/28 00:13:14 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/Core/Resources/resource_manager.h"
#include "API/GUIStyleSilver/image_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"

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

CL_Image_Silver::CL_Image_Silver(
	CL_Image *image,
	CL_StyleManager_Silver *style) 
:
	CL_ComponentStyle(image),
	image(image)
{
	resources = style->get_resources();

	slot_paint = image->sig_paint().connect(
		this, &CL_Image_Silver::on_paint);

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

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

void CL_Image_Silver::on_get_preferred_size(CL_Size &size)
{
	CL_Surface * s = image->get_surface();
	if(s)
	{
		size.width = s->get_width();
		size.height = s->get_height();
	}
}

void CL_Image_Silver::on_paint()
{
	CL_Surface *surface = image->get_surface();

	if(surface == 0)
		return;

	int width = image->get_width();
	int height = image->get_height();

	CL_Image::Mode mode = image->get_mode();

	switch (mode)
	{
	case CL_Image::normal:
		surface->draw(0, 0);
		break;

	case CL_Image::center:
		surface->draw(
			(width - surface->get_width()) / 2,
			(height - surface->get_height()) / 2);
		break;
		
	case CL_Image::stretch:
		surface->draw(CL_Rect(0, 0, width, height));
		break;

	case CL_Image::tile:
		{
			for (int y = 0; y < height; y += surface->get_height())
				for (int x = 0; x < width; x += surface->get_width())
					surface->draw(x, y);
		}
		break;
	}
}
