Adonthell  0.4
label_input.cc
1 /*
2  $Id: label_input.cc,v 1.14 2004/12/13 08:56:58 ksterker Exp $
3 
4  (C) Copyright 2000/2001/2003/2004 Joel Vennin
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details
13 */
14 
15 #include "label_input.h"
16 
18 {
19  set_cursor_visible (true);
20  set_cursor_moveable (true);
21  set_editable (true);
22 }
23 
24 void label_input::set_editable (const bool b)
25 {
26  editable_ = b;
27 }
28 
30 {
31  if (!editable_) return false;
32 
34 
35  if (my_font_ == NULL) return false;
36 
37  int count;
38  static s_int32 c;
39 
40  while ((c = input::get_next_unicode ()) > 0)
41  {
42  cursor_undraw ();
43  if (c == SDLK_BACKSPACE || c == SDLK_DELETE)
44  {
45  if (my_text_.empty () || my_cursor_.idx == 0) return true;
46 
47  // possibly delete multi-byte utf-8 char
48  if (my_cursor_.idx > 2 && (u_int8) my_text_[my_cursor_.idx-3] == 0xEF) count = 3;
49  else if (my_cursor_.idx > 1 && (u_int8) my_text_[my_cursor_.idx-2] == 0xC3) count = 2;
50  else count = 1;
51 
52  my_cursor_.idx -= count;
53  u_int16 idx = my_cursor_.idx;
54  u_int16 glyph = ucd (idx);
55  my_text_.erase (my_cursor_.idx, count);
56 
57  update_cursor ();
58  my_old_cursor_ = my_cursor_;
59 
60  lock ();
61  fillrect (my_cursor_.pos_x, my_cursor_.pos_y,
62  (*my_font_) [glyph].length (),
63  my_font_->height (), screen::trans_col ());
64  unlock ();
65 
66  build (false);
67  }
68  else if (c == SDLK_RETURN) add_text ("\n");
69  else if (my_font_->in_table (c))
70  {
71  char r[3];
72 
73  // convert unicode to utf-8
74  if (c < 0x80) count = 1;
75  else if (c < 0x800) count = 2;
76  else if (c < 0x10000) count = 3;
77 
78  switch (count) { /* note: code falls through cases! */
79  case 3: r[2] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x800;
80  case 2: r[1] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0xc0;
81  case 1: r[0] = c;
82  }
83 
84  add_text (string (r, count));
85  }
86  }
87  return true;
88 }
89 
90 
91 
92