/*
Solar Conquest
Copyright (C) 2006 Greg Beaman

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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

class CTextBox : public CMenuItem
{
public:
	CMenuItem* link;

	int maxText;

	CTextBox(int iid, int px, int py, int w, int h);
	virtual ~CTextBox();

	void DrawItem(bool selected);
	void DrawItemText(bool selected);
	void OnChar(char input);
};

CTextBox::CTextBox(int iid, int px, int py, int w, int h)
{
	id = iid;

	x = px;
	y = py;
	height = h;
	itemType = MENU_ITEM_TEXTBOX;
	strcpy(caption,"");

	maxText = w+1;
	if (maxText > MAX_STRING_SIZE)
		maxText = MAX_STRING_SIZE;

	width = w * (FONT_SIZE_X+FONT_SPACING) + 12;

	nextItem = NULL;
	link = NULL;
};

CTextBox::~CTextBox()
{
}

void CTextBox::DrawItem(bool selected)
{
	//Draw the box
	glBegin(GL_LINES);
		glColor4f(0,0,0.2,1);
		glVertex2f(x,y);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y+height);
		glVertex2f(x+width,y+height);
		glVertex2f(x,y+height);
		glVertex2f(x,y+height);
		glVertex2f(x,y);

		glVertex2f(x+6,y+6);
		glVertex2f(x+width-6,y+6);
		glVertex2f(x+width-6,y+6);
		glVertex2f(x+width-6,y+height-6);
		glVertex2f(x+width-6,y+height-6);
		glVertex2f(x+6,y+height-6);
		glVertex2f(x+6,y+height-6);
		glVertex2f(x+6,y+6);

		glColor4f(0,0,0.5,1);
		glVertex2f(x+1,y+1);
		glVertex2f(x+width-1,y+1);
		glVertex2f(x+width-1,y+1);
		glVertex2f(x+width-1,y+height-1);
		glVertex2f(x+width-1,y+height-1);
		glVertex2f(x+1,y+height-1);
		glVertex2f(x+1,y+height-1);
		glVertex2f(x+1,y+1);

		glVertex2f(x+5,y+5);
		glVertex2f(x+width-5,y+5);
		glVertex2f(x+width-5,y+5);
		glVertex2f(x+width-5,y+height-5);
		glVertex2f(x+width-5,y+height-5);
		glVertex2f(x+5,y+height-5);
		glVertex2f(x+5,y+height-5);
		glVertex2f(x+5,y+5);

		glColor4f(0,0,1,1);
		glVertex2f(x+2,y+2);
		glVertex2f(x+width-2,y+2);
		glVertex2f(x+width-2,y+2);
		glVertex2f(x+width-2,y+height-2);
		glVertex2f(x+width-2,y+height-2);
		glVertex2f(x+2,y+height-2);
		glVertex2f(x+2,y+height-2);
		glVertex2f(x+2,y+2);

		glVertex2f(x+4,y+4);
		glVertex2f(x+width-4,y+4);
		glVertex2f(x+width-4,y+4);
		glVertex2f(x+width-4,y+height-4);
		glVertex2f(x+width-4,y+height-4);
		glVertex2f(x+4,y+height-4);
		glVertex2f(x+4,y+height-4);
		glVertex2f(x+4,y+4);

		glColor4f(1,1,1,1);
		glVertex2f(x+3,y+3);
		glVertex2f(x+width-3,y+3);
		glVertex2f(x+width-3,y+3);
		glVertex2f(x+width-3,y+height-3);
		glVertex2f(x+width-3,y+height-3);
		glVertex2f(x+3,y+height-3);
		glVertex2f(x+3,y+height-3);
		glVertex2f(x+3,y+3);
	glEnd();

	glBegin(GL_TRIANGLES);
		//Top
		glColor4f(0,0,1,0);
		glVertex2f(x,y);
		glVertex2f(x+width,y);
		glColor4f(1,1,1,1);
		glVertex2f(x,y+3);

		glVertex2f(x,y+3);
		glVertex2f(x+width,y+3);
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y);

		glColor4f(0,0,1,0);
		glVertex2f(x,y+6);
		glVertex2f(x+width,y+6);
		glColor4f(1,1,1,1);
		glVertex2f(x,y+3);

		glVertex2f(x,y+3);
		glVertex2f(x+width,y+3);
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y+6);

		//Left side
		glColor4f(0,0,1,0);
		glVertex2f(x,y);
		glVertex2f(x,y+height);
		glColor4f(1,1,1,1);
		glVertex2f(x+3,y);

		glVertex2f(x+3,y);
		glVertex2f(x+3,y+height);
		glColor4f(0,0,1,0);
		glVertex2f(x,y+height);

		glVertex2f(x+6,y);
		glVertex2f(x+6,y+height);
		glColor4f(1,1,1,1);
		glVertex2f(x+3,y+height);

		glVertex2f(x+3,y);
		glVertex2f(x+3,y+height);
		glColor4f(0,0,1,0);
		glVertex2f(x+6,y);

		//Right side
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y+height);
		glColor4f(1,1,1,1);
		glVertex2f(x+width-3,y);

		glVertex2f(x+width-3,y);
		glVertex2f(x+width-3,y+height);
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y+height);

		glVertex2f(x+width-6,y);
		glVertex2f(x+width-6,y+height);
		glColor4f(1,1,1,1);
		glVertex2f(x+width-3,y+height);

		glVertex2f(x+width-3,y);
		glVertex2f(x+width-3,y+height);
		glColor4f(0,0,1,0);
		glVertex2f(x+width-6,y);

		//Bottom
		glColor4f(0,0,1,0);
		glVertex2f(x,y+height);
		glVertex2f(x+width,y+height);
		glColor4f(1,1,1,1);
		glVertex2f(x,y+height-3);

		glVertex2f(x,y+height-3);
		glVertex2f(x+width,y+height-3);
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y+height);

		glColor4f(0,0,1,0);
		glVertex2f(x,y+height-6);
		glVertex2f(x+width,y+height-6);
		glColor4f(1,1,1,1);
		glVertex2f(x,y+height-3);

		glVertex2f(x,y+height-3);
		glVertex2f(x+width,y+height-3);
		glColor4f(0,0,1,0);
		glVertex2f(x+width,y+height-6);
	glEnd();

}

void CTextBox::DrawItemText(bool selected)
{
	g_MainFont->RenderText(x+6,y+6,1,1,caption);
	if (selected)
		g_MainFont->RenderText(x+6 + (FONT_SIZE_X+FONT_SPACING)*strlen(caption),y+6,1,1,"|");
}

void CTextBox::OnChar(char input)
{
	//Tab
	if (input == '\t')
	{
		if (strlen(caption)+5 < maxText)
			strcat(caption, "     ");
		return;
	}

	//Return/newline
	if ((input == '\r') || (input == '\n'))
	{
		//Not ready for events yet
		return;
	}

	//Backspace
	if (input == '\b')
	{
		if (strlen(caption) <= 0)
			return;

		char temp[MAX_STRING_SIZE] = "";
		strncpy(temp, caption, strlen(caption)-1);
		strcpy(caption, temp);
		return;
	}

	if (strlen(caption) < maxText-1)
		strncat(caption,&input,1);
}