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 
#define s_int32
32 bits long signed integer
Definition: types.h:44
void set_cursor_visible(const bool b)
Set visible cursor.
Definition: label.cc:189
label_input()
Constructor Initialize to : cursor_moveable, cursor_visible and editable.
Definition: label_input.cc:17
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
void build(const bool erase_all)
Build label.
Definition: label.cc:157
void fillrect(s_int16 x, s_int16 y, u_int16 l, u_int16 h, u_int32 col, drawing_area *da_opt=NULL)
Fills an area of the surface with a given color.
Definition: surface.cc:260
bool input_update()
Input update.
Definition: label_input.cc:29
#define u_int8
8 bits long unsigned integer
Definition: types.h:29
void unlock() const
Unlock the surface after you've worked on it's pixels with the get_pix () and put_pix () methods...
Definition: surface.cc:294
Definition: label.h:25
bool input_update()
Update input label, you can move the cursor if the cursor is moveable.
Definition: label.cc:596
void set_editable(const bool)
Set the label input in editable.
Definition: label_input.cc:24
void set_cursor_moveable(const bool b)
Set if the cursor can be moved with arrow key.
Definition: label.cc:198
void add_text(const string &text)
Add text.
Definition: label.cc:85
static u_int32 trans_col()
Returns the translucent color in screen's depth format.
Definition: screen.h:89
static s_int32 get_next_unicode()
Returns the next unicode on the input queue.
Definition: input.cc:107
void lock() const
Locks the surface.
Definition: surface.cc:287