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

WidgetLayerBox::WidgetLayerBox(void) {
}

WidgetLayerBox::~WidgetLayerBox(void) {
}

// Inherited stuff.
void WidgetLayerBox::draw(void) {

	WidgetMultiParent::draw();

	pushClipArea(0, 0, 
		width, height);
		
	for(unsigned int i = 0; i < children.size(); i++) {
		if(children[i]) {
			children[i]->draw();
		}
	}
		
	popClipArea();
}

float WidgetLayerBox::getMinHeight(void) {
	float mySize = WidgetMultiParent::getMinHeight();
	
	for(unsigned int i = 0; i < children.size(); i++) {
		if(children[i]) {
			float childSize = children[i]->getMinHeight();
			if(childSize > mySize) mySize = childSize;
		}
	}
	
	return mySize;
}

float WidgetLayerBox::getMinWidth(void) {
	float mySize = WidgetMultiParent::getMinWidth();
	
	for(unsigned int i = 0; i < children.size(); i++) {
		if(children[i]) {
			float childSize = children[i]->getMinWidth();
			if(childSize > mySize) mySize = childSize;
		}
	}
	
	return mySize;
}

void WidgetLayerBox::setWidth(float newWidth) {
	WidgetMultiParent::setWidth(newWidth);
	
	// Set child sizes.
	for(unsigned int i = 0; i < children.size(); i++) {
		if(children[i]) {
			children[i]->setWidth(width);
		}
	}
}

void WidgetLayerBox::setHeight(float newHeight) {
	WidgetMultiParent::setHeight(newHeight);
	
	// Set child sizes.
	for(unsigned int i = 0; i < children.size(); i++) {
		if(children[i]) {
			children[i]->setHeight(height);
		}
	}
}

bool WidgetLayerBox::handleEvent(const WidgetEvent *event) {
	
	if(WidgetMultiParent::handleEvent(event)) {
		return true;
	}
	
	// Go through all the children from top to bottom and attempt
	// to have each handle the event until one returns true.
	
	for(int i = (int)children.size() - 1; i >= 0; i--) {
		if(children[i]) {
			if(children[i]->handleEvent(event)) {
				return true;
			}
		}
	}
		
	return false;
}
