/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "ui/widget_staticcontainer.h"
		#include "log/log.h"

		using namespace GS;
		using namespace GS::S2D;


//-------------------------------------------------------------------------
ContainerCell		*ContainerWidget::GetCell(const Widget *widget) const
//-------------------------------------------------------------------------
{
	ListForeachPtr(ContainerCell *, cell, cell_list)
		if	(cell->widget == widget)
			return cell;
	return NULL;
}

//------------------------------------------------------------------------------
Widget				*ContainerWidget::Add(Widget *widget, const iRect &cell_rect)
{
	if	(!widget)
		__ERR__(__LOG_E__ << "Cannot create a cell for a NULL widget.\n", NULL)

	ContainerCell	*cell = new ContainerCell;
	if	(!cell)
		__ERR__(__LOG_E__ << "Failed to allocate new container cell.\n", NULL)

	widget->SetParent(this);

	cell->cell_rect = cell_rect;
	cell->widget = widget;
	cell_list.Add(cell, true, false);

	children.Add(widget);

	return widget;
}
bool				ContainerWidget::Remove(Widget *widget)
{
	ContainerCell *e = GetCell(widget);
	if	(!e)
		return false;

	cell_list.Remove(e);
	_safe_delete(e);

	children.Remove(widget);
	return true;
}
//------------------------------------------------------------------------------

//------------------------------------------------
void				ContainerWidget::Invalidate()
//------------------------------------------------
{
	ListForeachPtr(ContainerCell *, cell, cell_list)
		cell->widget->Invalidate();
	Widget::Invalidate();
}

//---------------------------------------------
void				ContainerWidget::Refresh()
//---------------------------------------------
{
	ListForeachPtr(ContainerCell *, cell, cell_list)
	{
		cell->widget->Refresh();
		if	(cell->widget->IsDirty())
			dirty = true;
	}
}

//------------------------------------------------------------------------------
void				ContainerWidget::Compose(Picture &output, const iRect &out_rect)
{
	if	(hidden || !dirty || !cell_list.GetCount())
		return;

	rect = out_rect;
	ListForeachPtr(ContainerCell *, cell, cell_list)
		if	(!cell->widget->IsHidden())
			cell->widget->Compose(output, cell->cell_rect.Offset(rect.sx, rect.sy));
		else
			cell->widget->Validate();

	dirty = false;
}
ContainerWidget::~ContainerWidget()
{
	/*
		Only cells are deleted here since children are handled by the base
		Widget class destructor.
	*/
	ListDeleteAllPtr(ContainerCell *, cell_list)
}
//------------------------------------------------------------------------------
