Adonthell 0.4
label_input.cc
00001 /*
00002    $Id: label_input.cc,v 1.14 2004/12/13 08:56:58 ksterker Exp $
00003 
00004    (C) Copyright 2000/2001/2003/2004 Joel Vennin
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details
00013 */
00014 
00015 #include "label_input.h"
00016 
00017 label_input::label_input () : label ()
00018 {
00019     set_cursor_visible (true);
00020     set_cursor_moveable (true);
00021     set_editable (true); 
00022 }
00023 
00024 void label_input::set_editable (const bool b)
00025 {
00026     editable_ = b; 
00027 }
00028  
00029 bool label_input::input_update()
00030 {
00031     if (!editable_) return false; 
00032     
00033     label::input_update();
00034 
00035     if (my_font_ == NULL) return false; 
00036     
00037     int count;
00038     static s_int32 c; 
00039 
00040     while ((c = input::get_next_unicode ()) > 0)
00041     {
00042         cursor_undraw ();
00043         if (c == SDLK_BACKSPACE || c == SDLK_DELETE)
00044         {            
00045             if (my_text_.empty () || my_cursor_.idx == 0) return true;
00046             
00047             // possibly delete multi-byte utf-8 char
00048             if (my_cursor_.idx > 2 && (u_int8) my_text_[my_cursor_.idx-3] == 0xEF) count = 3;
00049             else if (my_cursor_.idx > 1 && (u_int8) my_text_[my_cursor_.idx-2] == 0xC3) count = 2;
00050             else count = 1;
00051             
00052             my_cursor_.idx -= count;
00053             u_int16 idx = my_cursor_.idx;
00054             u_int16 glyph = ucd (idx);
00055             my_text_.erase (my_cursor_.idx, count);
00056 
00057             update_cursor ();
00058             my_old_cursor_ = my_cursor_; 
00059 
00060             lock (); 
00061             fillrect (my_cursor_.pos_x, my_cursor_.pos_y,
00062                       (*my_font_) [glyph].length (),
00063                       my_font_->height (), screen::trans_col ()); 
00064             unlock (); 
00065             
00066             build (false);
00067         }
00068         else if (c == SDLK_RETURN) add_text ("\n"); 
00069         else if (my_font_->in_table (c))
00070         {
00071             char r[3];
00072             
00073             // convert unicode to utf-8
00074             if (c < 0x80) count = 1;
00075             else if (c < 0x800) count = 2;
00076             else if (c < 0x10000) count = 3;
00077             
00078             switch (count) { /* note: code falls through cases! */
00079                 case 3: r[2] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x800;
00080                 case 2: r[1] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0xc0;
00081                 case 1: r[0] = c;
00082             }
00083             
00084             add_text (string (r, count)); 
00085         }
00086     }  
00087     return true;
00088 }
00089 
00090  
00091 
00092