Adonthell  0.4
label_input.cc
1 /*
2  (C) Copyright 2000/2001/2003/2004 Joel Vennin
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "label_input.h"
20 
22 {
23  set_cursor_visible (true);
24  set_cursor_moveable (true);
25  set_editable (true);
26 }
27 
28 label_input::~label_input()
29 {
30  if (input::is_text_input())
31  {
32  input::stop_text_input();
33  }
34 }
35 
36 void label_input::set_editable (const bool b)
37 {
38  editable_ = b;
39 }
40 
42 {
43  if (!editable_) return false;
44 
45  if (!input::is_text_input())
46  {
47  input::start_text_input();
48  }
49 
51 
52  if (my_font_ == NULL) return false;
53 
54  int count;
55  std::string c = input::get_next_unicode ();
56 
57  while (!c.empty())
58  {
59  cursor_undraw ();
60  if (c[0] == SDLK_BACKSPACE || c[0] == SDLK_DELETE)
61  {
62  if (my_text_.empty () || my_cursor_.idx == 0) return true;
63 
64  // possibly delete multi-byte utf-8 char
65  if (my_cursor_.idx > 2 && (u_int8) my_text_[my_cursor_.idx-3] == 0xEF) count = 3;
66  else if (my_cursor_.idx > 1 && (u_int8) my_text_[my_cursor_.idx-2] == 0xC3) count = 2;
67  else count = 1;
68 
69  my_cursor_.idx -= count;
70  u_int16 idx = my_cursor_.idx;
71  u_int16 glyph = ucd (idx);
72  my_text_.erase (my_cursor_.idx, count);
73 
74  update_cursor ();
75  my_old_cursor_ = my_cursor_;
76 
77  offset_x_ = my_cursor_.offset_x;
78  fillrect (my_cursor_.pos_x, my_cursor_.pos_y,
79  (*my_font_) [glyph].length (),
80  my_font_->height (), screen::trans_col ());
81 
82  build (false);
83  }
84  else if (c[0] == SDLK_RETURN) add_text ("\n");
85  else
86  {
87  u_int16 idx = 0;
88  if (my_font_->in_table (ucd(c, idx)))
89  {
90  add_text (c);
91  }
92  else
93  {
94  add_text("?");
95  }
96  }
97 
99  }
100 
101  return true;
102 }
void set_cursor_visible(const bool b)
Set visible cursor.
Definition: label.cc:198
label_input()
Constructor Initialize to : cursor_moveable, cursor_visible and editable.
Definition: label_input.cc:21
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
void build(const bool erase_all)
Build label.
Definition: label.cc:166
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:239
bool input_update()
Input update.
Definition: label_input.cc:41
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
Definition: label.h:29
s_int16 offset_x_
sub-pixel offset
Definition: surface.h:446
bool input_update()
Update input label, you can move the cursor if the cursor is moveable.
Definition: label.cc:673
void set_editable(const bool)
Set the label input in editable.
Definition: label_input.cc:36
void set_cursor_moveable(const bool b)
Set if the cursor can be moved with arrow key.
Definition: label.cc:207
void add_text(const string &text)
Add text.
Definition: label.cc:90
static u_int32 trans_col()
Returns the translucent color in screen&#39;s depth format.
Definition: screen.h:110
static std::string get_next_unicode()
Returns the next text input on the input queue encoded as utf8.
Definition: input.cc:143