#include <windows.h>
#include "../uifontstring.h"

int FontString_SetText(lua_State* L);
int FontString_GetText(lua_State* L);

// ----------------------------------------------------------------------------------
static const struct luaL_reg g_fontstringTable [] = {	
	{"SetText",			FontString_SetText},
	{"GetText",			FontString_GetText},
	{NULL, NULL}
};	

// ----------------------------------------------------------------------------------
void CUiLuaFontString::OpenLib(lua_State* L)
{
	luaL_newmetatable(L, UI_LUA_TYPE_FONTSTRING);		// create new metatable
	luaL_getmetatable(L, UI_LUA_TYPE_LAYOUT);			// inherit layout
	lua_setmetatable(L, -2);
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);
	luaL_openlib(L, NULL, g_fontstringTable, 0);
}

// ----------------------------------------------------------------------------------
int	CUiLuaFontString::SetMetaTable(lua_State* L)
{
	if (lua_istable(L, -1))
	{
		luaL_getmetatable(L, UI_LUA_TYPE_FONTSTRING);
		lua_setmetatable(L, -2);
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------------
int FontString_SetText(lua_State* L)
{
	CUiFontString* font = dynamic_cast<CUiFontString*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (font == NULL)
		lua_error(L);	

	const char* text = "";
	if ( lua_isstring(L, 2) )
		text = lua_tostring(L, 2);

	font->SetTextA( text );
	return 0;
}

// ----------------------------------------------------------------------------------
int FontString_GetText(lua_State* L)
{
	CUiFontString* font = dynamic_cast<CUiFontString*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (font == NULL)
		lua_error(L);

	const WCHAR* text = font->GetText();
	if (text)
	{
		char* tmp = TransToChar(text);
		lua_pushstring(L, tmp);
		UI_SAFE_DELETE_ARRAY(tmp);
		return 1;
	}
	return 0;
}