// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program 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
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include <iostream>
using namespace std;

#include "../console/console.h"
using namespace Console;

#include "widgetbutton.h"
#include "widgetradiogroup.h"

WidgetRadioGroup::WidgetRadioGroup(void) {
}

WidgetRadioGroup::~WidgetRadioGroup(void) {
}


void WidgetRadioGroup::setSelectedOption(Widget *w) {
	WidgetButton *oldSelection = dynamic_cast<WidgetButton*>(widgetGetByUID(selectedObjectUID));
	
	if(oldSelection == w) {
		// Set it to what it already is?
		return;
	}
	
	// Deselect the old selection.
	if(oldSelection) {
		oldSelection->setToggleValue(false);
	}
	
	// Set the new one.
	if(!w) {
		selectedObjectUID = "";
	} else {
		selectedObjectUID = w->getUID();
	}
}

Widget *WidgetRadioGroup::getSelectedOption(void) {
	return widgetGetByUID(selectedObjectUID);
}

void WidgetRadioGroup::draw(void) {
	WidgetSingleParent::draw();
	
	if(child) child->draw();
}

void WidgetRadioGroup::setWidth(float newWidth) {
	WidgetSingleParent::setWidth(newWidth);
	if(child) child->setWidth(width);
}

void WidgetRadioGroup::setHeight(float newHeight) {
	WidgetSingleParent::setHeight(newHeight);
	if(child) child->setHeight(height);
}

bool WidgetRadioGroup::handleEvent(const WidgetEvent *event) {
	bool ret = WidgetSingleParent::handleEvent(event);
	
	if(!ret) {
		ret |= child->handleEvent(event);
	}
	
	return ret;
}

float WidgetRadioGroup::getMinHeight(void) {
	float childSize = child ? child->getMinHeight() : 0;
	float mySize    = WidgetSingleParent::getMinHeight();
	return mySize > childSize ? mySize : childSize;
}

float WidgetRadioGroup::getMinWidth(void) {
	float childSize = child ? child->getMinWidth() : 0;
	float mySize    = WidgetSingleParent::getMinWidth();
	return mySize > childSize ? mySize : childSize;
}
