#ifndef TEXTBOX_H
#define TEXTBOX_H

//Textbox.h

#include "MenuObject.h"

class Textbox :public MenuObject
{
	private:

		string text; // an empty string for editting
		string::iterator iter ;// string iterator
		int scolor;
		int ecolor;
		int tra;
		int caret ; // tracks the text caret
		bool insert ;  // true of should text be inserted
		
		
		
		
	public:
int Get_scolor();
		int Get_ecolor();
void Set_scolor(int);
		
		void Set_ecolor(int);
void Set_defaltText(string);
		Textbox();
		Textbox(string);

		~Textbox()
		{

		}
		string draw(BITMAP *bmp);

};

Textbox::Textbox()
{

selceted = false;
text="";// an empty string for editting
iter = text.begin(); // string iterator
x=mouse_x;
y=mouse_y;
caret = x-87;// tracks the text caret
w=100;
h=25;
insert = true;  // true of should text be inserted
tra=text.length();
scolor = makecol(0,255,0);
ecolor = makecol(255,0,0);
}

Textbox::Textbox(string t)
{

selceted = false;
text= t;
iter = text.end(); // string iterator
x=mouse_x;
y=mouse_y;
caret = x-87;// tracks the text caret
w=100;
h=25;
insert = true;  // true of should text be inserted
tra=text.length();
scolor = makecol(0,255,0);
ecolor = makecol(255,0,0);
}
int Textbox::Get_scolor()
			{
return scolor;
	}
	int Textbox::Get_ecolor()
			{
return ecolor;
	}

void Textbox::Set_scolor(int q)
{
	scolor = q;
}
	void Textbox::Set_ecolor(int q)
			{
ecolor=q;
	}


	void Textbox::Set_defaltText(string t)
	{
		text = t;
		tra=text.length();
		iter = text.end();
	}








string Textbox::draw(BITMAP *bmp)
{
	if( selceted == true)
	{
while(keypressed())
                {
                        int  newkey   = readkey();
                        char ASCII    = newkey & 0xff;
                        char scancode = newkey >> 8;
                        
                        // a character key was pressed; add it to the string
                        if(ASCII >= 32 && ASCII <= 126)
                        {
                                // add the new char, inserting or replacing as need be
                                if(insert || iter == text.end())
                                iter = text.insert(iter, ASCII);
                                else
                                text.replace(caret, 1, 1, ASCII);
                                
                                // increment both the caret and the iterator
                                caret++;
                                iter++;
                        }
                        // some other, "special" key was pressed; handle it here
                        else
                        switch(scancode)
                        {
                                case KEY_DEL:
                                if(iter != text.end()) iter = text.erase(iter);
                                break;
                                
                                case KEY_BACKSPACE:
                                if(iter != text.begin())
                                {
                                        caret--;
                                        iter--;
                                        iter = text.erase(iter);
                                }
                                break;
                                
                                case KEY_RIGHT:
                                if(iter != text.end())   caret++, iter++;
                                break;
                                
                                case KEY_LEFT:
                                if(iter != text.begin()) caret--, iter--;
                                break;
                                
                                case KEY_INSERT:
                                if(insert) insert = 0; else insert = 1;
                                break;
                                
								case KEY_ENTER:
                                if(iter != text.begin())
								{
									//this give runtime error
									if(computerId==severId)
									{
										//need to put in life contter
									//serverdiolog = text;
									//outboxbuffer +="9999|" + NumberToString(computerId) + "|003|" + NumberToString(computerId) + "," + text +"`~";
									
									}
									else
									{
									//need somthing for sending msg to serten clients
									//outboxbuffer +="0|" + NumberToString(computerId) + "|003|" + NumberToString(computerId) + "," + text +"`~";
									}
									//tra=text.length();
									//caret-=(tra);
									//iter-=(tra);
									//text.erase(0,tra+1);
									
								}
                                break;
                                default:
                                
                                break;
                        }
                }
				// output some stats using Allegro's printf functions
                //textprintf(bmp, font,  x, 20, makecol(255,255,255), "length:   %d", text.length());
                //textprintf(bmp, font,  x, 30, makecol(255,255,255), "capacity: %d", text.capacity());
                //textprintf(bmp, font,  x, 40, makecol(255,255,255), "empty`:   %d", text.empty());
                //if(insert)
                //textout(bmp, font, "Inserting", x, 50, makecol(255,255,255));
               //else
                //textout(bmp, font, "Replacing", x, 50, makecol(255,255,255));
				// draw the caret
                vline(bmp, caret * 8, 8, 18, makecol(255,255,255));
	}
                // clear bmp
                //clear(buffer);
                
                
                
                
                
                
                

// output the string to the bmp
textout(bmp,font,Get_lable().c_str(),Get_x() ,Get_y()-10,makecol(255,255,255));
textout(bmp, font, text.c_str(), x+10, y+10, makecol(255,255,255));
drawRectangle(bmp,Get_x(),  Get_y(), Get_w(), Get_h(), Get_color(), 2, Get_color());
return text;
}

#endif

						   


                      














