/*  $Id: frame_silver.cpp,v 1.7 2004/02/23 14:09:55 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/surface.h"
#include "API/GUIStyleSilver/frame_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"
#include "API/Core/Resources/resource_manager.h"
#include "API/Core/XML/dom_element.h"
#include "API/Core/System/error.h"

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

CL_Frame_Silver::CL_Frame_Silver(
	CL_Frame *_frame,
	CL_StyleManager_Silver *style) 
:
	CL_ComponentStyle(_frame),
	frame(_frame),
	style(style)
{
	resources = style->get_resources();
	sur_surface = 0;
	mode = tile;

	slot_set_options = frame->sig_set_options().connect(
		this, &CL_Frame_Silver::on_set_options);
	slot_paint = frame->sig_paint().connect(
		this, &CL_Frame_Silver::on_paint);
	slot_resize = frame->sig_resize().connect(
		this, &CL_Frame_Silver::on_resize);

	on_resize(0,0);
}

CL_Frame_Silver::~CL_Frame_Silver()
{
	if(sur_surface)
		delete sur_surface;
}

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

void CL_Frame_Silver::on_set_options(const CL_DomElement &options)
{
	if (options.has_attribute("surface"))
	{
		std::string surface = options.get_attribute("surface");
		sur_surface = new CL_Surface(surface, resources);
	}
	
	if (options.has_attribute("mode"))
	{
		std::string v = options.get_attribute("mode");
		
		if (v == "center") mode = center;
		else if (v == "stretch") mode = stretch;
		else if (v == "tile") mode = tile;
		else throw CL_Error("unknown frame mode");
	}
}

void CL_Frame_Silver::on_resize(int old_width, int old_height)
{
	CL_Rect client_rect(1, 1, frame->get_width() - 1, frame->get_height() - 1);
	frame->get_client_area()->set_position(client_rect);
}

void CL_Frame_Silver::on_paint()
{
	int width = frame->get_width();
	int height = frame->get_height();

	if (sur_surface)
	{
		switch (mode)
		{
		case center:
			sur_surface->draw(
				width / 2 - sur_surface->get_width() / 2,
				height / 2 - sur_surface->get_height() / 2);
			break;
			
		case stretch:
			sur_surface->draw(CL_Rect(0, 0, width, height));
			break;

		case tile:
			{
			for (int y = 0; y < height; y += sur_surface->get_height())
				for (int x = 0; x < width; x += sur_surface->get_width())
					sur_surface->draw(x, y);
			}
		}
	}
	else
	{
		CL_Display::draw_rect(CL_Rect(0, 0, width, height), CL_Color(204, 212, 222));

		if(frame->is_fill_enabled())
			CL_Display::fill_rect(CL_Rect(1, 1, width - 1, height - 1), CL_Color::white);
	}
}
