/*  $Id: radiogroup_generic.cpp,v 1.8 2003/07/20 22:23:22 grumbel 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/radiobutton.h"
#include "radiogroup_generic.h"

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

CL_RadioButton *CL_RadioGroup_Generic::get_toggled() const
{
	std::vector<CL_RadioButton *>::const_iterator it;
	for(it = buttons.begin(); it != buttons.end(); ++it)
		if((*it)->is_checked())
			return (*it);

	return 0;
}

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

void CL_RadioGroup_Generic::add(CL_RadioButton *button, bool delete_component)
{
	// TODO: Use delete_component
	
	buttons.push_back(button);

	slots.connect(button->sig_clicked(), this, &CL_RadioGroup_Generic::on_button_clicked, button);
}

void CL_RadioGroup_Generic::remove(CL_RadioButton *button)
{
	// TODO: Remove clicked-slot from slots as well
	
	std::vector<CL_RadioButton *>::iterator it;
	for(it = buttons.begin(); it != buttons.end(); ++it)
	{
		if((*it) == button)
		{
			buttons.erase(it);
			return;
		}	
	}
}

void CL_RadioGroup_Generic::set_checked(CL_RadioButton *button)
{
	if(button->is_checked())
		return;
		
	std::vector<CL_RadioButton *>::const_iterator it;
	for(it = buttons.begin(); it != buttons.end(); ++it)
		(*it)->set_checked((*it) == button);

	sig_selection_changed(button);
}

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

void CL_RadioGroup_Generic::on_button_clicked(CL_RadioButton *button)
{
	std::vector<CL_RadioButton *>::iterator it;
	for(it = buttons.begin(); it != buttons.end(); ++it)
	{
		if((*it) != button)
			(*it)->set_checked(false);
		else
		{
			button->set_checked(true);
			sig_selection_changed(button);
		}
	}
}
