/*  $Id: window_silver.cpp,v 1.12 2004/04/11 15:37:31 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/font.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"
#include "API/GUIStyleSilver/window_silver.h"

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

CL_Window_Silver::CL_Window_Silver(
	CL_Window *window,
	CL_StyleManager_Silver *style) 
:
	CL_ComponentStyle(window),
	window(window),
	style(style),
	titlebar_height(22)
{
	resources = style->get_resources();
	
	font = new CL_Font("Window/font", resources);
	font_disabled = new CL_Font("Window/font_disabled", resources);
	
	// Make the window movable by dragging titlebar
	move_handler = new CL_ComponentMoveHandler(window);

	// Make the window resizable by dragging corner
	resize_handler = new CL_ComponentResizeHandler(window);

	// Create a close button on the titlebar
	button_close = new CL_Button(window, style);

	slots.connect(button_close->sig_clicked(), this, &CL_Window_Silver::on_close);
	slots.connect(window->sig_paint(), this, &CL_Window_Silver::on_paint);
	slots.connect(window->sig_resize(), this, &CL_Window_Silver::on_resize);

	on_resize(0,0);
}

CL_Window_Silver::~CL_Window_Silver()
{
	delete font;
	delete font_disabled;
	delete resize_handler;
	delete move_handler;
	delete button_close;
}

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

void CL_Window_Silver::on_paint()
{
	int width = window->get_width();
	int height = window->get_height();
	
	// Titlebar
	CL_Display::fill_rect(
		CL_Rect(2, 2, width - 2, titlebar_height - 1),
		CL_Gradient(CL_Color(245, 246, 247), CL_Color(245, 246, 247), CL_Color(234, 237, 240), CL_Color(234, 237, 240)));
//		CL_Gradient(CL_Color(245, 246, 247), CL_Color(245, 246, 247), CL_Color(234, 237, 240), CL_Color(234, 237, 240)));
	CL_Display::draw_line(
		2, titlebar_height - 1, width - 2, titlebar_height - 1,
		CL_Color(212, 217, 223));
		
	// Window frame
	CL_Display::draw_rect(
		CL_Rect(1, 1, width - 1, height - 1),
		CL_Color(161, 172, 185));
	CL_Display::draw_rect(
		CL_Rect(0, 0, width, height),
		CL_Color(161, 172, 185));

	// Client area
	CL_Display::fill_rect(
		CL_Rect(2, titlebar_height, width - 2, height - 2),
		CL_Gradient(CL_Color(255,255,255,235), CL_Color(255,255,255,235), CL_Color(224, 228, 232,235), CL_Color(224, 228, 232,235)));
		
/*	if(window->is_enabled() == false)
		style->fill_rect(2, 2, width - 2, titlebar_height - 1, GUICOLOR_WINDOW_TITLEBAR_DISABLED);
	else
		style->fill_rect(2, 2, width - 2, titlebar_height - 1, GUICOLOR_WINDOW_TITLEBAR);
*/
	int y = (titlebar_height + 2 - font->get_height()) / 2;

	if(window->is_enabled() == false)
		font_disabled->draw(7, y, window->get_title());
	else
		font->draw(7, y, window->get_title());

	CL_Display::push_cliprect();
	CL_Display::set_cliprect(window->get_parent()->get_position());

	CL_Display::push_modelview();
	CL_Display::set_translate(0, 0);
	
	CL_Rect rect = window->get_screen_rect();

	// Vertical shadows
	CL_Display::draw_line(rect.right + 0, rect.top + 5, rect.right + 0, rect.bottom + 0, CL_Color(0, 0, 0, 64));
	CL_Display::draw_line(rect.right + 1, rect.top + 5, rect.right + 1, rect.bottom + 1, CL_Color(0, 0, 0, 51));
	CL_Display::draw_line(rect.right + 2, rect.top + 5, rect.right + 2, rect.bottom + 2, CL_Color(0, 0, 0, 38));
	CL_Display::draw_line(rect.right + 3, rect.top + 5, rect.right + 3, rect.bottom + 3, CL_Color(0, 0, 0, 25));
	CL_Display::draw_line(rect.right + 4, rect.top + 5, rect.right + 4, rect.bottom + 4, CL_Color(0, 0, 0, 13));

	// Horizontal shadows
	CL_Display::draw_line(rect.left + 5, rect.bottom + 0, rect.right + 1, rect.bottom + 0, CL_Color(0, 0, 0, 64));
	CL_Display::draw_line(rect.left + 5, rect.bottom + 1, rect.right + 2, rect.bottom + 1, CL_Color(0, 0, 0, 51));
	CL_Display::draw_line(rect.left + 5, rect.bottom + 2, rect.right + 3, rect.bottom + 2, CL_Color(0, 0, 0, 38));
	CL_Display::draw_line(rect.left + 5, rect.bottom + 3, rect.right + 4, rect.bottom + 3, CL_Color(0, 0, 0, 25));
	CL_Display::draw_line(rect.left + 5, rect.bottom + 4, rect.right + 5, rect.bottom + 4, CL_Color(0, 0, 0, 13));

	CL_Display::pop_cliprect();
	CL_Display::pop_modelview();
}

void CL_Window_Silver::on_close()
{
	if (window->is_modal())
		window->quit();
	else
		window->close();
}

void CL_Window_Silver::on_resize(int old_width, int old_height)
{
	CL_Rect client_rect(3, 24, window->get_width() - 3, window->get_height() - 3);
	window->get_client_area()->set_position(client_rect);

	CL_Rect close_rect(window->get_width() - 20, 3, window->get_width() - 3, 20);
	button_close->set_position(close_rect);

	CL_Rect move_rect(0, 0, window->get_width() - 19, titlebar_height);
	move_handler->set_position(move_rect);
	
	CL_Rect resize_rect(window->get_width() - 3, window->get_height() - 3, window->get_width(), window->get_height());
	resize_handler->set_position(resize_rect);
}
