// ---------------------------------------------------------------------------
// 
//   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 "widgetsingleparent.h"

WidgetSingleParent::WidgetSingleParent(void) : Widget() {
	child = NULL;
}

WidgetSingleParent::WidgetSingleParent(TiXmlElement *xml) : Widget(xml) {
	child = NULL;
	
	TiXmlElement *childElement = xml->FirstChildElement();
	if(childElement) {
		setChild(widgetCreateFromXMLElement(childElement));
	}
}

WidgetSingleParent::~WidgetSingleParent(void) {
	if(child) autoGenLuaWrapper_Widget_safeDelete(child);
}

bool WidgetSingleParent::handleEvent(const WidgetEvent *event) {
	// Parent is responsible for passing events to child.
	return Widget::handleEvent(event);
}	

void WidgetSingleParent::draw(void) {
	Widget::draw();
}

// This is usually the same for all WidgetSingleParents. If the child
// needs mouse events, then make sure we get them so we can send them
// to the child.
bool WidgetSingleParent::needsMouseEvents(void) {
	if(Widget::needsMouseEvents()) return true;
	if(child) {
		if(child->needsMouseEvents()) {
			return true;
		}
	}
	return false;
}

void WidgetSingleParent::setChild(Widget *child) {
	
    if(widgetIsAlreadySafeDeleted(child)) {
        out("error") << "Cannot set a child that is already getting deleted!" << endl;
        return;
	}

	if(widgetIsEventuallyMyParent(child)) {
		out("error") <<
			"Tried to add a child (" <<
			child->getName() <<
			") which is an eventual parent to this WidgetSingleParent (" <<
			getName() <<
			")." << endl;
			
		return;
	}
	
	if(child) {
		if(child->parent || child == widgetRoot) {
			out("error") <<
				"Tried to add a child (" <<
				child->getName() <<
				") to a single parent ("
				<< getName() <<
				") when the child already has a parent." << endl;
				
			return;
		}
		
		child->parent = this;
	}
	
	if(this->child) autoGenLuaWrapper_Widget_safeDelete(this->child);
	this->child = child;
}

Widget *WidgetSingleParent::getChild(void) {
	return child;
}

bool WidgetSingleParent::isSizeDirty(void) {
	bool dirty = Widget::isSizeDirty();
	if(child) dirty |= child->isSizeDirty();
	return dirty;
}

Widget *WidgetSingleParent::findChildByName(const std::string &name) {
	Widget *w = Widget::findChildByName(name);
	if(w)
		return w;
	
	if(child)
		return child->findChildByName(name);
		
	return NULL;
}

Widget *WidgetSingleParent::disconnectChild(void) {
	Widget *w = child;
	if(w) {
		w->parent = NULL;
	}
	child = NULL;
	return w;
}

