00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include "../include/app.h"
00023 #include "text_box.h"
00024 #include "label.h"
00025 
00026 TextBox::TextBox (const std::string &label, const Rectanglei &rect, Font& _font) :
00027   Label(label, rect, _font),
00028   cursor_pos(label.size())
00029 {
00030 }
00031 
00032 void TextBox::SetText(std::string const &new_txt)
00033 {
00034   Label::SetText(new_txt);
00035   cursor_pos = new_txt.size();
00036 }
00037 
00038 void TextBox::SetCursor(std::string::size_type pos)
00039 {
00040   if(pos > GetText().size())
00041   {
00042     cursor_pos = GetText().size();
00043   }
00044   else
00045   {
00046     cursor_pos = pos;
00047   }
00048 }
00049 
00050 TextBox::~TextBox(){
00051 }
00052 
00053 void TextBox::SendKey(SDL_keysym key)
00054 {
00055   need_redrawing = true;
00056 
00057   std::string new_txt = GetText();
00058 
00059   if (strcmp(SDL_GetKeyName(key.sym),"backspace")==0)
00060   {
00061     if(cursor_pos != 0)
00062     {
00063       while((new_txt[--cursor_pos] & 0xc0) == 0x80)
00064       {
00065         new_txt.erase(cursor_pos, 1);
00066       }
00067       new_txt.erase(cursor_pos, 1);
00068       Label::SetText(new_txt);
00069     }
00070   }
00071   else if(strcmp(SDL_GetKeyName(key.sym),"left")==0)
00072   {
00073     if(cursor_pos != 0)
00074     {
00075       while((new_txt[--cursor_pos] & 0xc0) == 0x80);
00076     }
00077   }
00078   else if(strcmp(SDL_GetKeyName(key.sym),"right")==0)
00079   {
00080     if(cursor_pos < new_txt.size())
00081     {
00082       while((new_txt[++cursor_pos] & 0xc0) == 0x80);
00083     }
00084   }
00085   else
00086   {
00087     if(key.unicode > 0)
00088     {
00089       if(key.unicode < 0x80) { 
00090           new_txt.insert(cursor_pos++, 1, (char)key.unicode);
00091       }
00092       else if (key.unicode < 0x800) 
00093       {
00094         new_txt.insert(cursor_pos++, 1, (char)(((key.unicode & 0x7c0) >> 6) | 0xc0));
00095         new_txt.insert(cursor_pos++, 1, (char)((key.unicode & 0x3f) | 0x80));
00096       }
00097       else 
00098       {
00099         new_txt.insert(cursor_pos++, 1, (char)(((key.unicode & 0xf000) >> 12) | 0xe0));
00100         new_txt.insert(cursor_pos++, 1, (char)(((key.unicode & 0xfc0) >> 6) | 0x80));
00101         new_txt.insert(cursor_pos++, 1, (char)((key.unicode & 0x3f) | 0x80));
00102       }
00103     }
00104     Label::SetText(new_txt);
00105   }
00106 }
00107 
00108 void TextBox::Draw(const Point2i &mousePosition, Surface& surf) const
00109 {
00110   if (!hidden) 
00111     {
00112       if(have_focus)
00113         surf.BoxColor(*this, highlightOptionColorBox);
00114 
00115       surf.RectangleColor(*this, defaultOptionColorRect);
00116 
00117       Label::Draw(mousePosition, surf);
00118 
00119       
00120       Text txt_before_cursor(*txt_label);
00121       txt_before_cursor.Set(GetText().substr(0, cursor_pos));
00122 
00123       surf.VlineColor(GetPositionX()+txt_before_cursor.GetWidth(),
00124               GetPositionY()+2,
00125               GetPositionY()+GetSizeY()-4, c_white);
00126     }
00127 }