src/gui/text_box.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Text box widget
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) { // 1 byte char
00090           new_txt.insert(cursor_pos++, 1, (char)key.unicode);
00091       }
00092       else if (key.unicode < 0x800) // 2 byte char
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 // if (key.unicode < 0x10000) // 3 byte char
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       //sort of a hacky way to get the cursor pos, but I couldn't find anything better...
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 }

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7