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


		#include "ui/widget_text.h"

		using namespace GS;
		using namespace GS::S2D;


//------------------------------------------------------------------------------
void				TextWidget::SetTextState(const TextState &_state)
{
	// Avoid useless invalidation.
	if	(Memory::Compare(&state, &_state, sizeof(TextState)))
	{
		state = _state;
		Invalidate();
	}
}
void				TextWidget::GetTextState(TextState &_state) const
{	_state = state;	}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				TextWidget::SetText(const char *t)
{
	text = t;
	Invalidate();
}
void				TextWidget::SetFont(FontEx *f)
{
	if	(state.font != f)
	{
		state.font = f;
		Invalidate();
	}
}
void				TextWidget::SetFontSize(int s)
{
	state.SetSize(s);
	if	(state.font.IsValid())
		state.font->SetPixelSize(s);
	Invalidate();
}
void				TextWidget::SetFontColor(float red, float green, float blue, float alpha)
{
	state.color.Set(red * 255.f, green * 255.f, blue * 255.f, alpha * 255.f);
	Invalidate();
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				TextWidget::Refresh()
{
	if	(!state.font || !dirty)
		return;
	rect = state.font->GetTextBoundRect(text);
}
void				TextWidget::Compose(Picture &output, const iRect &clip_rect)
{
	iRect text_rect = FontRenderer::Format(text.c_str(), state, clip_rect),
		  out_rect = Widget::FormatOutputRect(text_rect, clip_rect);

	rect = FontRenderer::Compose(output, text.c_str(), state, out_rect, clip_rect);
	dirty = false;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
TextWidget::TextWidget(int id, const char *t, FontEx *font, int s) : Widget(id)
{
	type = WidgetTypeText;

	state.format = TextState::Line;
	state.alignment = TextState::Left;

	h_align = AlignGrow;

	SetText(t);
	SetFont(font);

	SetFontSize(s);
	SetFontColor(1, 1, 1);
	SetColumnWidth(80);

	state.SetTracking(FontRenderer::default_text_tracking);
	state.SetLeading(FontRenderer::default_text_heading);
}
//------------------------------------------------------------------------------
