/*  $Id: button_generic.cpp,v 1.51 2004/05/08 10:39:04 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 "precomp.h"
#include "button_generic.h"
#include "API/Core/XML/dom_element.h"
#include "API/Display/keys.h"

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

CL_Button_Generic::CL_Button_Generic(
	CL_Button *self,
	const std::string &text,
	bool toggle_mode)
:
	text(text),
	toggle_mode(toggle_mode),
	toggled(false),
	visually_down(false),
	button(self)
{
	slots.connect(button->sig_set_options(), this, &CL_Button_Generic::on_set_options);
	slots.connect(button->sig_input_down(), this, &CL_Button_Generic::on_input_down);
	slots.connect(button->sig_input_up(), this, &CL_Button_Generic::on_input_up);
	slots.connect(button->sig_mouse_move(), this, &CL_Button_Generic::on_mouse_move);
}

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

void CL_Button_Generic::on_set_options(const CL_DomElement &options)
{
	if (options.has_attribute("text")) text = options.get_attribute("text");
	if (options.has_attribute("toggled")) toggled = CL_String::to_bool(options.get_attribute("toggled"));
	if (options.has_attribute("togglemode")) toggle_mode = CL_String::to_bool(options.get_attribute("togglemode"));

	visually_down = toggled;
}

void CL_Button_Generic::on_mouse_move(const CL_InputEvent &key)
{
	if(!button->is_mouse_captured())
		return;

	if(button->has_mouse_over())
		visually_down = !toggled;
	else
		visually_down = toggled;
}

void CL_Button_Generic::on_input_down(const CL_InputEvent &key)
{
	if(button->is_mouse_captured())
		return;

	if(key.id != CL_MOUSE_LEFT && key.id != CL_KEY_SPACE)
		return;

	sig_pressed();

	button->capture_mouse();

	visually_down = !visually_down;
}

void CL_Button_Generic::on_input_up(const CL_InputEvent &key)
{
	if(button->is_mouse_captured() == false)
		return;

	if(key.id != CL_MOUSE_LEFT && key.id != CL_KEY_SPACE)
		return;

	button->release_mouse();

	sig_released();

	bool mouse_over = button->has_mouse_over();
	if (mouse_over)
	{
		if (toggle_mode)
		{
			toggled = !toggled;
			sig_toggled(toggled);
		}
				
		sig_clicked();
	}

	visually_down = toggled;
}
