/*  $Id: menu_node_silver.cpp,v 1.7 2004/02/20 20:58:31 harry 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/Core/Resources/resource_manager.h"
#include "API/Core/Math/rect.h"
#include "API/Display/surface.h"
#include "API/Core/XML/dom_element.h"
#include "API/Display/display.h"
#include "API/GUIStyleSilver/stylemanager_silver.h"
#include "API/GUI/menu.h"
#include "API/GUIStyleSilver/menu_node_silver.h"

CL_MenuNode_Silver::CL_MenuNode_Silver(
	CL_MenuNode *node,
	CL_StyleManager_Silver *style)
:
	CL_ComponentStyle(node),
	node(node),
	style(style),
	resources(0),
	arrow(0)
{
	resources = style->get_resources();

	if (resources->exists("Menu/submenu_arrow"))
		arrow = new CL_Surface("Menu/submenu_arrow", resources);	
	
	slots.connect(node->sig_set_options(), this, &CL_MenuNode_Silver::on_set_options);
	slots.connect(node->sig_paint(), this, &CL_MenuNode_Silver::on_paint);
	slots.connect(node->sig_reposition(), this, &CL_MenuNode_Silver::on_reposition); // todo: sync names?
	slots.connect(node->sig_get_preferred_size(), this, &CL_MenuNode_Silver::on_get_preferred_size);
}

CL_MenuNode_Silver::~CL_MenuNode_Silver()
{
	delete arrow;
}

void CL_MenuNode_Silver::on_set_options(const CL_DomElement &options)
{
	// ?
}

void CL_MenuNode_Silver::on_paint()
{
	CL_Rect area(1,0,node->get_width()-1,node->get_height());

	// mouseover fill
	if( node->has_mouse_over() && node->is_enabled() && node->get_parent_menu()->is_enabled() )
		CL_Display::fill_rect( area, 
			CL_Gradient(
				CL_Color(255,255,255,128), CL_Color(254, 207, 147, 128),
				CL_Color(255,255,255,128), CL_Color(254, 207, 147, 128) ));

	if( arrow && node->has_submenu() )
	{		
		if( node->get_parent_menu()->is_vertical() ) // no arrow in horizontal menus
			arrow->draw( area.right-8, 4 );
	}
}

void CL_MenuNode_Silver::on_reposition()
{
	CL_Rect menu_pos = node->get_parent_menu()->get_position();
	CL_Rect node_pos = node->get_position();
	CL_Menu *submenu = node->get_submenu();

	if( submenu )
	{
		if( node->get_parent_menu()->is_vertical() )
			submenu->reposition( menu_pos.right-2, menu_pos.top + node_pos.top+2 );
		else
		{
			submenu->reposition( menu_pos.left + node_pos.left, menu_pos.top+menu_pos.get_height() );
		}
	}
}

void CL_MenuNode_Silver::on_get_preferred_size(CL_Size &size)
{
	if( size.width < 220 )
		size.width = 220;
}
