/*  $Id: scrollbar_silver.cpp,v 1.6 2003/09/18 17:53:29 mbn 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/Core/Resources/resource_manager.h"
#include "API/GUIStyleSilver/scrollbar_silver.h"
#include "API/GUIStyleSilver/button_silver.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"

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

CL_ScrollBar_Silver::CL_ScrollBar_Silver(
	CL_ScrollBar *scrollbar,
	CL_StyleManager_Silver *style)
:
	CL_ComponentStyle(scrollbar),
	timer_scroll(250),
	scrollbar(scrollbar),
	style(style)
{
	resources = style->get_resources();

	CL_Component *client_area = scrollbar->get_client_area();

	button_increase = new CL_Button("", scrollbar, style);
	((CL_Button_Silver *)button_increase->get_style())->set_scrollbar_increase_mode();

	button_decrease = new CL_Button("", scrollbar, style);
	((CL_Button_Silver *)button_decrease->get_style())->set_scrollbar_decrease_mode();

	slots.connect(client_area->sig_paint(), this, &CL_ScrollBar_Silver::on_paint);
	slots.connect(timer_scroll.sig_timer(), this, &CL_ScrollBar_Silver::on_timer_scroll);

	slots.connect(button_increase->sig_pressed(), this, &CL_ScrollBar_Silver::start_scroll, 1);
	slots.connect(button_decrease->sig_pressed(), this, &CL_ScrollBar_Silver::start_scroll, -1);
	slots.connect(button_increase->sig_released(), this, &CL_ScrollBar_Silver::stop_scroll);
	slots.connect(button_decrease->sig_released(), this, &CL_ScrollBar_Silver::stop_scroll);
	slots.connect(scrollbar->sig_resize(), this, &CL_ScrollBar_Silver::on_resize);
	slots.connect(scrollbar->sig_set_options(), this, &CL_ScrollBar_Silver::on_set_options);

	on_resize(0, 0);
}

CL_ScrollBar_Silver::~CL_ScrollBar_Silver()
{
	delete button_increase;
	delete button_decrease;
}

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

void CL_ScrollBar_Silver::on_set_options(const CL_DomElement &options)
{
}

void CL_ScrollBar_Silver::on_resize(int old_width, int old_height)
{
	CL_Rect rect_decrease;
	CL_Rect rect_increase;
	CL_Rect rect_client;

	if (scrollbar->is_vertical())
	{
		int button_size = scrollbar->get_width();
		rect_decrease = CL_Rect(0, 0, button_size, button_size);
		rect_increase = CL_Rect(0, scrollbar->get_height() - button_size, button_size, scrollbar->get_height());
		rect_client = CL_Rect(0, button_size, button_size, scrollbar->get_height() - button_size);
	}
	else
	{
		int button_size = scrollbar->get_height();
		rect_decrease = CL_Rect(0, 0, button_size, button_size);
		rect_increase = CL_Rect(scrollbar->get_width() - button_size, 0, scrollbar->get_width(), button_size);
		rect_client = CL_Rect(button_size, 0, scrollbar->get_width() - button_size, button_size);
	}

	button_decrease->set_position(rect_decrease);
	button_increase->set_position(rect_increase);
	scrollbar->get_client_area()->set_position(rect_client);
}

void CL_ScrollBar_Silver::on_timer_scroll()
{
	scrollbar->set_value(scrollbar->get_value() + scroll_delta);
}

void CL_ScrollBar_Silver::on_paint()
{
	int width = scrollbar->get_client_area()->get_width();
	int height = scrollbar->get_client_area()->get_height();

	CL_Rect rect = scrollbar->get_slider_rect();

//	CL_Display::draw_rect(CL_Rect(0, 0, width, height), CL_Color(128, 142, 159));

	CL_Display::fill_rect(
		CL_Rect(0, 0, width, height),
		CL_Gradient(CL_Color(230, 235, 240), CL_Color::white, CL_Color(230, 235, 240), CL_Color::white));
			
/*	CL_Display::draw_rect(
		CL_Rect(0, 0, width, height),
		CL_Color(238, 237, 229));
	
	CL_Display::fill_rect(
		CL_Rect(0, 0, width, height),
		CL_Gradient(CL_Color(243, 241, 236), CL_Color(254, 254, 251), CL_Color(243, 241, 236), CL_Color(254, 254, 251)));
*/
/*	CL_Display::draw_line(rect.left, rect.top + 1, rect.left, rect.bottom - 1, CL_Color(255, 147, 22));
	CL_Display::draw_line(rect.right - 1, rect.top + 1, rect.right - 1, rect.bottom - 1, CL_Color(255, 147, 22));
	CL_Display::draw_line(rect.left + 1, rect.top, rect.right - 1, rect.top, CL_Color(255, 147, 22));
	CL_Display::draw_line(rect.left + 1, rect.bottom - 1, rect.right - 1, rect.bottom - 1, CL_Color(255, 147, 22));
*/
	CL_Display::draw_line(rect.left, rect.top + 1, rect.left, rect.bottom - 1, CL_Color(128, 142, 159));
	CL_Display::draw_line(rect.right - 1, rect.top + 1, rect.right - 1, rect.bottom - 1, CL_Color(128, 142, 159));
	CL_Display::draw_line(rect.left + 1, rect.top, rect.right - 1, rect.top, CL_Color(128, 142, 159));
	CL_Display::draw_line(rect.left + 1, rect.bottom - 1, rect.right - 1, rect.bottom - 1, CL_Color(128, 142, 159));

	CL_Display::fill_rect(
		CL_Rect(rect.left + 1, rect.top + 1, rect.right - 1, rect.bottom - 1),
		CL_Gradient(CL_Color::white, CL_Color(230, 235, 240), CL_Color::white, CL_Color(230, 235, 240)));

	CL_Display::draw_line(
		rect.left + 5, rect.top + rect.get_height() / 2 - 2, rect.right - 5, rect.top + rect.get_height() / 2 - 2, 
		CL_Color(128, 142, 159));

	CL_Display::draw_line(
		rect.left + 5, rect.top + rect.get_height() / 2, rect.right - 5, rect.top + rect.get_height() / 2, 
		CL_Color(128, 142, 159));

	CL_Display::draw_line(
		rect.left + 5, rect.top + rect.get_height() / 2 + 2, rect.right - 5, rect.top + rect.get_height() / 2 + 2, 
		CL_Color(128, 142, 159));
		
//	CL_Gradient(CL_Color(255, 147, 22), CL_Color(240, 196, 138), CL_Color(255, 147, 22), CL_Color(240, 196, 138)));

/*	CL_Display::fill_rect(
		CL_Rect(width / 2, 0, width, height),
		CL_Gradient(CL_Color(230, 235, 240), CL_Color::white, CL_Color(230, 235, 240), CL_Color::white));
*/		
		// Background
/*	style->fill_rect(
		0, 0,
		width, height,
		GUICOLOR_SCROLLBAR);

	if(scrollbar->has_focus())
		style->draw_rect(
			0, 0,
			width, height,
			GUICOLOR_FOCUS);
		
	// Slider
	if(scrollbar->is_dragging_slider())
	{
		style->fill_rect(
			rect.left + 2,
			rect.top + 2,
			rect.right - 2,
			rect.bottom - 2,
			GUICOLOR_DARKER_SHADE);
	}
	else
	{
		style->fill_rect(
			rect.left + 2,
			rect.top + 2,
			rect.right - 2,
			rect.bottom - 2,
			GUICOLOR_WHITE);
	}

	style->draw_rect(
		rect.left + 1,
		rect.top + 1,
		rect.right - 1,
		rect.bottom - 1, 
		GUICOLOR_DARK_OUTLINE);
*/
}

/////////////////////////////////////////////////////////////////////////////
// Implementation:

void CL_ScrollBar_Silver::start_scroll(int delta)
{
	scroll_delta = delta;
	timer_scroll.enable();
	on_timer_scroll();
}

void CL_ScrollBar_Silver::stop_scroll()
{
	timer_scroll.disable();
}

