/*  $Id: menu.cpp,v 1.9 2004/04/14 21:08:48 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 "precomp.h"
#include "API/gui.h"
#include "API/core.h" // todo: remove/narrow?
#include "API/GUI/menu.h"
#include "API/GUI/menu_node.h"
#include "menu_generic.h"

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

CL_Menu::CL_Menu(
	const CL_Point &pos,
	CL_Component *parent,
	CL_StyleManager *style,
	bool vertical)
:
	CL_Component( CL_Rect(pos.x, pos.y, 0, 0), parent, style),
	impl(0)
{
	impl = new CL_Menu_Generic(this, vertical, 0);
	get_style_manager()->connect_styles("menu", this);
}

CL_Menu::CL_Menu(
	CL_Component *parent,
	CL_StyleManager *style,
	bool vertical)
:
	CL_Component( CL_Rect(0, 0, 0, 0), parent, style),
	impl(0)
{
	impl = new CL_Menu_Generic(this, vertical, 0);
	get_style_manager()->connect_styles("menu", this);
}

CL_Menu::CL_Menu(
	const CL_Rect &rect,
	CL_Component *parent,
	CL_StyleManager *style,
	bool vertical)
:
	CL_Component( rect, parent, style),
	impl(0)
{
	impl = new CL_Menu_Generic(this, vertical, 0);
	impl->auto_resize = false;
	get_style_manager()->connect_styles("menu", this);
}

CL_Menu::CL_Menu(
	CL_MenuNode *parent_node,
	CL_Component *parent,
	CL_StyleManager *style)
:
	CL_Component( parent, style),
	impl(0)
{
	impl = new CL_Menu_Generic(this, true, parent_node);
	parent_node->set_submenu(this);
	get_style_manager()->connect_styles("menu", this);
}

CL_Menu::~CL_Menu()
{
	delete impl;
}

/////////////////////////////////////////////////////////////////////////////
// Attributes:

bool CL_Menu::is_open()
{
	return impl->menu_open;
}

bool CL_Menu::is_vertical()
{
	return impl->vertical;
}

bool CL_Menu::is_root_collapsing()
{
	return impl->collapse_root;
}

bool CL_Menu::is_auto_resize()
{
	return impl->auto_resize;
}

CL_Menu *CL_Menu::get_root_menu()
{
	return impl->get_root_menu();
}

CL_MenuNode *CL_Menu::get_parent_node()
{
	return impl->parent_node;
}

std::list<CL_MenuNode*> &CL_Menu::get_node_list()
{
	// todo: CL_Component::get_children() does the same?
	return impl->children;
}

int CL_Menu::get_items_width()
{
	return impl->get_items_width();
}

int CL_Menu::get_items_height()
{
	return impl->get_items_height();
}

bool CL_Menu::has_mouse_over()
{
	return impl->has_mouse_over();
}

bool CL_Menu::has_mouse_in_submenus()
{
	return impl->has_mouse_in_submenus();
}

bool CL_Menu::is_click_to_open()
{
	return impl->click_to_open;
}

CL_Menu *CL_Menu::get_menu(const std::string &name)
{
	return impl->get_menu(CL_String::tokenize(name, "/"));
}

CL_MenuNode *CL_Menu::get_node(const std::string &name)
{
	return impl->get_node(CL_String::tokenize(name, "/"));
}

CL_Component *CL_Menu::get_item(const std::string &name)
{
	return impl->get_item(CL_String::tokenize(name, "/"));
}

/////////////////////////////////////////////////////////////////////////////
// Operations:

void CL_Menu::add_node(CL_MenuNode *node)
{
	impl->add_node(node);
}

void CL_Menu::remove_node(CL_MenuNode *node)
{
	impl->remove_node(node);
}

void CL_Menu::open()
{
	impl->open();
}

void CL_Menu::collapse()
{
	impl->collapse();
}

void CL_Menu::collapse_submenus()
{
	impl->collapse_submenus();
}

void CL_Menu::set_vertical(bool vertical)
{
	impl->vertical = vertical; 
}

void CL_Menu::set_root_collapsing(bool collapse)
{
	impl->collapse_root = collapse;
}

void CL_Menu::set_auto_resize(bool value)
{
	impl->auto_resize = value;
}

void CL_Menu::reposition()
{
	CL_Rect area = get_position(); 

	impl->sig_reposition(area.left, area.top);
}

void CL_Menu::reposition(int x, int y)
{
	impl->sig_reposition(x,y);
}

void CL_Menu::set_parent_node(CL_MenuNode *parent_node)
{
	impl->parent_node = parent_node;
}

void CL_Menu::set_click_to_open(bool click_to_open)
{
	impl->click_to_open = click_to_open;
}

CL_Menu *CL_Menu::create_menu(
	const std::string &path,
	const std::string &labels)
{
	std::vector<std::string> tokens = CL_String::tokenize(path, "/");

	if( labels.empty() )
		return impl->create_menu(tokens, tokens);

	return impl->create_menu(tokens, CL_String::tokenize(labels, "/"));
}

CL_MenuNode *CL_Menu::create_node(
	const std::string &path,
	const std::string &labels)
{
	std::vector<std::string> tokens = CL_String::tokenize(path, "/");

	if( labels.empty() )
		return impl->create_node(tokens, tokens);

	return impl->create_node(tokens, CL_String::tokenize(labels, "/"));
}

CL_MenuNode *CL_Menu::create_item(
	const std::string &path,
	const std::string &labels )
{
	std::vector<std::string> tokens = CL_String::tokenize(path, "/");

	if( labels.empty() )
		return impl->create_item(tokens, tokens);

	return impl->create_item(tokens, CL_String::tokenize(labels, "/"));
}

CL_MenuNode *CL_Menu::create_toggle_item(
	const std::string &path,
	const std::string &labels )
{
	std::vector<std::string> tokens = CL_String::tokenize(path, "/");

	if( labels.empty() )
		return impl->create_toggle_item(tokens, tokens);

	return impl->create_toggle_item(tokens, CL_String::tokenize(labels, "/"));
}


/////////////////////////////////////////////////////////////////////////////
// Signals:

CL_Signal_v2<int,int> &CL_Menu::sig_reposition()
{
	return impl->sig_reposition;
}
