4 Copyright (C) 1999 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "gui-entry.h"
26 #include "gui-printtext.h"
29 GUI_ENTRY_REC *active_entry;
31 GUI_ENTRY_REC *gui_entry_create(int xpos, int ypos, int width, int utf8)
35 rec = g_new0(GUI_ENTRY_REC, 1);
39 rec->text = g_string_new(NULL);
44 void gui_entry_destroy(GUI_ENTRY_REC *entry)
46 g_return_if_fail(entry != NULL);
48 if (active_entry == entry)
49 gui_entry_set_active(NULL);
51 g_free_not_null(entry->prompt);
52 g_string_free(entry->text, TRUE);
56 /* Fixes the cursor position in screen */
57 static void gui_entry_fix_cursor(GUI_ENTRY_REC *entry)
61 old_scrstart = entry->scrstart;
62 if (entry->pos - entry->scrstart < entry->width-2 - entry->promptlen &&
63 entry->pos - entry->scrstart > 0) {
64 entry->scrpos = entry->pos - entry->scrstart;
65 } else if (entry->pos < entry->width-1 - entry->promptlen) {
67 entry->scrpos = entry->pos;
69 entry->scrpos = (entry->width - entry->promptlen)*2/3;
70 entry->scrstart = entry->pos - entry->scrpos;
73 if (old_scrstart != entry->scrstart)
74 entry->redraw_needed_from = 0;
77 static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
79 const unsigned char *p, *end;
83 /* FIXME: a stupid kludge to make the chars output correctly */
87 xpos = entry->xpos + entry->promptlen + pos;
88 end_xpos = entry->xpos + entry->width;
92 term_set_color(root_window, ATTR_RESET);
93 term_move(root_window, xpos, entry->ypos);
95 p = (unsigned char *) (entry->scrstart + pos >= entry->text->len ? "" :
96 entry->text->str + entry->scrstart + pos);
97 for (; *p != '\0' && xpos < end_xpos; p++, xpos++) {
103 term_addch(root_window, ' ');
104 else if (*p >= 32 && (end != p || (*p & 127) >= 32)) {
106 term_addch(root_window, *p);
107 term_addch(root_window, *p);
109 term_set_color(root_window, ATTR_RESET|ATTR_REVERSE);
110 term_addch(root_window, *p+'A'-1);
111 term_set_color(root_window, ATTR_RESET);
115 /* clear the rest of the input line */
116 if (end_xpos == term_width)
117 term_clrtoeol(root_window);
119 while (xpos < end_xpos) {
120 term_addch(root_window, ' ');
126 static void gui_entry_draw(GUI_ENTRY_REC *entry)
128 if (entry->redraw_needed_from >= 0) {
129 gui_entry_draw_from(entry, entry->redraw_needed_from);
130 entry->redraw_needed_from = -1;
133 term_move_cursor(entry->xpos + entry->scrpos + entry->promptlen,
138 static void gui_entry_redraw_from(GUI_ENTRY_REC *entry, int pos)
140 pos -= entry->scrstart;
141 if (pos < 0) pos = 0;
143 if (entry->redraw_needed_from == -1 ||
144 entry->redraw_needed_from > pos)
145 entry->redraw_needed_from = pos;
148 void gui_entry_move(GUI_ENTRY_REC *entry, int xpos, int ypos, int width)
152 g_return_if_fail(entry != NULL);
154 if (entry->xpos != xpos || entry->ypos != ypos) {
155 /* position in screen changed - needs a full redraw */
158 entry->width = width;
159 gui_entry_redraw(entry);
163 if (entry->width == width)
164 return; /* no changes */
166 if (width > entry->width) {
167 /* input line grew - need to draw text at the end */
169 entry->width = width;
170 gui_entry_redraw_from(entry, old_width);
172 /* input line shrinked - make sure the cursor
173 is inside the input line */
174 entry->width = width;
175 if (entry->pos - entry->scrstart >
176 entry->width-2 - entry->promptlen) {
177 gui_entry_fix_cursor(entry);
181 gui_entry_draw(entry);
184 void gui_entry_set_active(GUI_ENTRY_REC *entry)
186 active_entry = entry;
189 term_move_cursor(entry->xpos + entry->scrpos +
190 entry->promptlen, entry->ypos);
195 void gui_entry_set_prompt(GUI_ENTRY_REC *entry, const char *str)
199 g_return_if_fail(entry != NULL);
201 oldlen = entry->promptlen;
203 g_free_not_null(entry->prompt);
204 entry->prompt = g_strdup(str);
205 entry->promptlen = format_get_length(str);
208 if (entry->prompt != NULL)
209 gui_printtext(entry->xpos, entry->ypos, entry->prompt);
211 if (entry->promptlen != oldlen) {
212 gui_entry_fix_cursor(entry);
213 gui_entry_draw(entry);
217 void gui_entry_set_hidden(GUI_ENTRY_REC *entry, int hidden)
219 g_return_if_fail(entry != NULL);
221 entry->hidden = hidden;
224 void gui_entry_set_utf8(GUI_ENTRY_REC *entry, int utf8)
226 g_return_if_fail(entry != NULL);
231 void gui_entry_set_text(GUI_ENTRY_REC *entry, const char *str)
233 g_return_if_fail(entry != NULL);
234 g_return_if_fail(str != NULL);
236 g_string_assign(entry->text, str);
237 entry->pos = entry->text->len;
239 gui_entry_redraw_from(entry, 0);
240 gui_entry_fix_cursor(entry);
241 gui_entry_draw(entry);
244 char *gui_entry_get_text(GUI_ENTRY_REC *entry)
246 g_return_val_if_fail(entry != NULL, NULL);
248 return entry->text->str;
251 void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str)
253 g_return_if_fail(entry != NULL);
254 g_return_if_fail(str != NULL);
256 gui_entry_redraw_from(entry, entry->pos);
257 g_string_insert(entry->text, entry->pos, str);
258 entry->pos += strlen(str);
260 gui_entry_fix_cursor(entry);
261 gui_entry_draw(entry);
264 void gui_entry_insert_char(GUI_ENTRY_REC *entry, char chr)
266 g_return_if_fail(entry != NULL);
268 if (chr == 0 || chr == 13 || chr == 10)
269 return; /* never insert NUL, CR or LF characters */
271 gui_entry_redraw_from(entry, entry->pos);
272 g_string_insert_c(entry->text, entry->pos, chr);
275 gui_entry_fix_cursor(entry);
276 gui_entry_draw(entry);
279 void gui_entry_erase(GUI_ENTRY_REC *entry, int size)
281 g_return_if_fail(entry != NULL);
283 if (entry->pos < size)
287 if (is_big5(entry->text->str[entry->pos-2],
288 entry->text->str[entry->pos-1]))
293 g_string_erase(entry->text, entry->pos, size);
295 gui_entry_redraw_from(entry, entry->pos);
296 gui_entry_fix_cursor(entry);
297 gui_entry_draw(entry);
300 void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space)
304 g_return_if_fail(entry != NULL);
311 while (entry->text->str[to] == ' ' && to > 0)
313 while (entry->text->str[to] != ' ' && to > 0)
316 while (!i_isalnum(entry->text->str[to]) && to > 0)
318 while (i_isalnum(entry->text->str[to]) && to > 0)
323 g_string_erase(entry->text, to, entry->pos - to);
326 gui_entry_redraw_from(entry, entry->pos);
327 gui_entry_fix_cursor(entry);
328 gui_entry_draw(entry);
331 void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space)
335 g_return_if_fail(entry != NULL);
336 if (entry->pos == entry->text->len)
341 while (entry->text->str[to] == ' ' && to < entry->text->len)
343 while (entry->text->str[to] != ' ' && to < entry->text->len)
346 while (!i_isalnum(entry->text->str[to]) && to < entry->text->len)
348 while (i_isalnum(entry->text->str[to]) && to < entry->text->len)
352 g_string_erase(entry->text, entry->pos, to - entry->pos);
354 gui_entry_redraw_from(entry, entry->pos);
355 gui_entry_fix_cursor(entry);
356 gui_entry_draw(entry);
359 int gui_entry_get_pos(GUI_ENTRY_REC *entry)
361 g_return_val_if_fail(entry != NULL, 0);
366 void gui_entry_set_pos(GUI_ENTRY_REC *entry, int pos)
368 g_return_if_fail(entry != NULL);
370 if (pos >= 0 && pos <= entry->text->len)
373 gui_entry_fix_cursor(entry);
374 gui_entry_draw(entry);
377 void gui_entry_move_pos(GUI_ENTRY_REC *entry, int pos)
379 g_return_if_fail(entry != NULL);
382 if (pos > 0 && is_big5(entry->text->str[entry->pos],
383 entry->text->str[entry->pos+1]))
385 else if (pos < 0 && is_big5(entry->text->str[entry->pos-1],
386 entry->text->str[entry->pos]))
390 if (entry->pos+pos >= 0 && entry->pos+pos <= entry->text->len)
393 gui_entry_fix_cursor(entry);
394 gui_entry_draw(entry);
397 static void gui_entry_move_words_left(GUI_ENTRY_REC *entry, int count, int to_space)
402 while (count > 0 && pos > 0) {
404 while (pos > 0 && entry->text->str[pos-1] == ' ')
406 while (pos > 0 && entry->text->str[pos-1] != ' ')
409 while (pos > 0 && !i_isalnum(entry->text->str[pos-1]))
411 while (pos > 0 && i_isalnum(entry->text->str[pos-1]))
420 static void gui_entry_move_words_right(GUI_ENTRY_REC *entry, int count, int to_space)
425 while (count > 0 && pos < entry->text->len) {
427 while (pos < entry->text->len && entry->text->str[pos] == ' ')
429 while (pos < entry->text->len && entry->text->str[pos] != ' ')
432 while (pos < entry->text->len && !i_isalnum(entry->text->str[pos]))
434 while (pos < entry->text->len && i_isalnum(entry->text->str[pos]))
443 void gui_entry_move_words(GUI_ENTRY_REC *entry, int count, int to_space)
445 g_return_if_fail(entry != NULL);
448 gui_entry_move_words_left(entry, -count, to_space);
450 gui_entry_move_words_right(entry, count, to_space);
452 gui_entry_fix_cursor(entry);
453 gui_entry_draw(entry);
456 void gui_entry_redraw(GUI_ENTRY_REC *entry)
458 g_return_if_fail(entry != NULL);
460 gui_entry_set_prompt(entry, NULL);
461 gui_entry_redraw_from(entry, 0);
462 gui_entry_fix_cursor(entry);
463 gui_entry_draw(entry);