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
26 #include "gui-entry.h"
27 #include "gui-printtext.h"
30 const unichar empty_str[] = { 0 };
32 GUI_ENTRY_REC *active_entry;
34 static void entry_text_grow(GUI_ENTRY_REC *entry, int grow_size)
36 if (entry->text_len+grow_size < entry->text_alloc)
39 entry->text_alloc = nearest_power(entry->text_alloc+grow_size);
40 entry->text = g_realloc(entry->text,
41 sizeof(unichar) * entry->text_alloc);
44 GUI_ENTRY_REC *gui_entry_create(int xpos, int ypos, int width, int utf8)
48 rec = g_new0(GUI_ENTRY_REC, 1);
52 rec->text_alloc = 1024;
53 rec->text = g_new(unichar, rec->text_alloc);
59 void gui_entry_destroy(GUI_ENTRY_REC *entry)
61 g_return_if_fail(entry != NULL);
63 if (active_entry == entry)
64 gui_entry_set_active(NULL);
67 g_free(entry->prompt);
72 #define big5_width(ch) ((ch)>0xff ? 2:1)
74 void unichars_to_big5(const unichar *str, char *out)
76 for (; *str != '\0'; str++) {
78 *out++ = (*str >> 8) & 0xff;
84 int strlen_big5(const unsigned char *str)
88 if (term_type != TERM_TYPE_BIG5)
91 while (*str != '\0') {
92 if (is_big5(str[0], str[1]))
100 void unichars_to_big5_with_pos(const unichar *str, int spos, char *out, int *opos)
102 const unichar *sstart = str;
109 *out ++ = (*str >> 8) & 0xff;
110 *out ++ = *str & 0xff;
112 if(str - sstart == spos)
113 *opos = out - ostart;
118 void big5_to_unichars(const char *str, unichar *out)
120 const unsigned char *p = (const unsigned char *) str;
123 if (is_big5(p[0], p[1])) {
124 *out++ = p[0] << 8 | p[1];
133 /* ----------------------------- */
135 static int pos2scrpos(GUI_ENTRY_REC *entry, int pos)
140 for (p = entry->text; p - entry->text < pos; p++) {
141 if (term_type == TERM_TYPE_BIG5)
142 xpos += big5_width(*p);
143 else if (entry->utf8)
144 xpos += utf8_width(*p);
151 static int scrpos2pos(GUI_ENTRY_REC *entry, int pos)
155 for (i = 0, xpos = 0; entry->text[i]; i++) {
156 unichar *p = entry->text+i;
158 if (term_type == TERM_TYPE_BIG5)
159 width = big5_width(*p);
160 else if (entry->utf8)
161 width = utf8_width(*p);
165 if (xpos + width > pos)
176 /* Fixes the cursor position in screen */
177 static void gui_entry_fix_cursor(GUI_ENTRY_REC *entry)
181 /* assume prompt len == prompt scrlen */
182 int start = pos2scrpos(entry, entry->scrstart);
183 int now = pos2scrpos(entry, entry->pos);
185 old_scrstart = entry->scrstart;
186 if (now-start < entry->width - 2 - entry->promptlen && now-start > 0)
187 entry->scrpos = now-start;
188 else if (now < entry->width - 1 - entry->promptlen) {
192 entry->scrstart = scrpos2pos(entry, now-(entry->width -
193 entry->promptlen)*2/3);
194 start = pos2scrpos(entry, entry->scrstart);
195 entry->scrpos = now - start;
198 if (old_scrstart != entry->scrstart)
199 entry->redraw_needed_from = 0;
202 static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
207 xpos = entry->xpos + entry->promptlen +
208 pos2scrpos(entry, pos + entry->scrstart) -
209 pos2scrpos(entry, entry->scrstart);
210 end_xpos = entry->xpos + entry->width;
215 term_set_color(root_window, ATTR_RESET);
216 term_move(root_window, xpos, entry->ypos);
218 p = entry->scrstart + pos < entry->text_len ?
219 entry->text + entry->scrstart + pos : empty_str;
220 for (; *p != '\0'; p++) {
223 else if (term_type == TERM_TYPE_BIG5)
224 xpos += big5_width(*p);
225 else if (entry->utf8)
226 xpos += utf8_width(*p);
234 term_addch(root_window, ' ');
235 else if (*p >= 32 && (entry->utf8 || (*p & 127) >= 32))
236 term_add_unichar(root_window, *p);
238 term_set_color(root_window, ATTR_RESET|ATTR_REVERSE);
239 term_addch(root_window, *p+'A'-1);
240 term_set_color(root_window, ATTR_RESET);
244 /* clear the rest of the input line */
245 if (end_xpos == term_width-1)
246 term_clrtoeol(root_window);
248 while (xpos < end_xpos) {
249 term_addch(root_window, ' ');
255 static void gui_entry_draw(GUI_ENTRY_REC *entry)
257 if (entry->redraw_needed_from >= 0) {
258 gui_entry_draw_from(entry, entry->redraw_needed_from);
259 entry->redraw_needed_from = -1;
262 term_move_cursor(entry->xpos + entry->scrpos + entry->promptlen,
267 static void gui_entry_redraw_from(GUI_ENTRY_REC *entry, int pos)
269 pos -= entry->scrstart;
270 if (pos < 0) pos = 0;
272 if (entry->redraw_needed_from == -1 ||
273 entry->redraw_needed_from > pos)
274 entry->redraw_needed_from = pos;
277 void gui_entry_move(GUI_ENTRY_REC *entry, int xpos, int ypos, int width)
281 g_return_if_fail(entry != NULL);
283 if (entry->xpos != xpos || entry->ypos != ypos) {
284 /* position in screen changed - needs a full redraw */
287 entry->width = width;
288 gui_entry_redraw(entry);
292 if (entry->width == width)
293 return; /* no changes */
295 if (width > entry->width) {
296 /* input line grew - need to draw text at the end */
298 entry->width = width;
299 gui_entry_redraw_from(entry, old_width);
301 /* input line shrinked - make sure the cursor
302 is inside the input line */
303 entry->width = width;
304 if (entry->pos - entry->scrstart >
305 entry->width-2 - entry->promptlen) {
306 gui_entry_fix_cursor(entry);
310 gui_entry_draw(entry);
313 void gui_entry_set_active(GUI_ENTRY_REC *entry)
315 active_entry = entry;
318 term_move_cursor(entry->xpos + entry->scrpos +
319 entry->promptlen, entry->ypos);
324 void gui_entry_set_prompt(GUI_ENTRY_REC *entry, const char *str)
328 g_return_if_fail(entry != NULL);
330 oldlen = entry->promptlen;
332 g_free_not_null(entry->prompt);
333 entry->prompt = g_strdup(str);
334 entry->promptlen = format_get_length(str);
337 if (entry->prompt != NULL)
338 gui_printtext(entry->xpos, entry->ypos, entry->prompt);
340 if (entry->promptlen != oldlen) {
341 gui_entry_fix_cursor(entry);
342 gui_entry_draw(entry);
346 void gui_entry_set_hidden(GUI_ENTRY_REC *entry, int hidden)
348 g_return_if_fail(entry != NULL);
350 entry->hidden = hidden;
353 void gui_entry_set_utf8(GUI_ENTRY_REC *entry, int utf8)
355 g_return_if_fail(entry != NULL);
360 void gui_entry_set_text(GUI_ENTRY_REC *entry, const char *str)
362 g_return_if_fail(entry != NULL);
363 g_return_if_fail(str != NULL);
367 entry->text[0] = '\0';
369 gui_entry_insert_text(entry, str);
372 char *gui_entry_get_text(GUI_ENTRY_REC *entry)
377 g_return_val_if_fail(entry != NULL, NULL);
379 buf = g_malloc(entry->text_len*6 + 1);
381 utf16_to_utf8(entry->text, buf);
383 if (term_type == TERM_TYPE_BIG5)
384 unichars_to_big5(entry->text, buf);
386 for (i = 0; i <= entry->text_len; i++)
387 buf[i] = entry->text[i];
392 char *gui_entry_get_text_and_pos(GUI_ENTRY_REC *entry, int *pos)
397 g_return_val_if_fail(entry != NULL, NULL);
399 buf = g_malloc(entry->text_len*6 + 1);
401 utf16_to_utf8_with_pos(entry->text, entry->pos, buf, pos);
403 if(term_type==TERM_TYPE_BIG5)
404 unichars_to_big5_with_pos(entry->text, entry->pos, buf, pos);
407 for (i = 0; i <= entry->text_len; i++)
408 buf[i] = entry->text[i];
415 void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str)
420 g_return_if_fail(entry != NULL);
421 g_return_if_fail(str != NULL);
423 gui_entry_redraw_from(entry, entry->pos);
425 len = !entry->utf8 ? strlen_big5(str) : strlen_utf8(str);
426 entry_text_grow(entry, len);
428 /* make space for the string */
429 g_memmove(entry->text + entry->pos + len, entry->text + entry->pos,
430 (entry->text_len-entry->pos + 1) * sizeof(unichar));
433 if (term_type == TERM_TYPE_BIG5) {
434 chr = entry->text[entry->pos + len];
435 big5_to_unichars(str, entry->text + entry->pos);
436 entry->text[entry->pos + len] = chr;
438 for (i = 0; i < len; i++)
439 entry->text[entry->pos + i] = str[i];
442 chr = entry->text[entry->pos+len];
443 utf8_to_utf16(str, entry->text+entry->pos);
444 entry->text[entry->pos+len] = chr;
447 entry->text_len += len;
450 gui_entry_fix_cursor(entry);
451 gui_entry_draw(entry);
454 void gui_entry_insert_char(GUI_ENTRY_REC *entry, unichar chr)
456 g_return_if_fail(entry != NULL);
458 if (chr == 0 || chr == 13 || chr == 10)
459 return; /* never insert NUL, CR or LF characters */
461 gui_entry_redraw_from(entry, entry->pos);
463 entry_text_grow(entry, 1);
465 /* make space for the string */
466 g_memmove(entry->text + entry->pos + 1, entry->text + entry->pos,
467 (entry->text_len-entry->pos + 1) * sizeof(unichar));
469 entry->text[entry->pos] = chr;
473 gui_entry_fix_cursor(entry);
474 gui_entry_draw(entry);
477 char *gui_entry_get_cutbuffer(GUI_ENTRY_REC *entry)
482 g_return_val_if_fail(entry != NULL, NULL);
484 if (entry->cutbuffer == NULL)
487 buf = g_malloc(entry->cutbuffer_len*6 + 1);
489 utf16_to_utf8(entry->cutbuffer, buf);
490 else if (term_type == TERM_TYPE_BIG5) {
491 unichars_to_big5(entry->cutbuffer, buf);
493 for (i = 0; i <= entry->cutbuffer_len; i++)
494 buf[i] = entry->cutbuffer[i];
499 void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, int update_cutbuffer)
501 int newpos, size = 0;
503 g_return_if_fail(entry != NULL);
505 for (newpos = gui_entry_get_pos(entry); newpos > pos; size++)
507 gui_entry_erase(entry, size, update_cutbuffer);
510 void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer)
512 g_return_if_fail(entry != NULL);
514 if (entry->pos < size)
517 if (update_cutbuffer) {
518 /* put erased text to cutbuffer */
519 if (entry->cutbuffer == NULL || entry->cutbuffer_len < size) {
520 g_free(entry->cutbuffer);
521 entry->cutbuffer = g_new(unichar, size+1);
524 entry->cutbuffer_len = size;
525 entry->cutbuffer[size] = '\0';
526 memcpy(entry->cutbuffer, entry->text + entry->pos - size,
527 size * sizeof(unichar));
531 /* we just wanted to clear the cutbuffer */
535 g_memmove(entry->text + entry->pos - size, entry->text + entry->pos,
536 (entry->text_len-entry->pos+1) * sizeof(unichar));
539 entry->text_len -= size;
541 gui_entry_redraw_from(entry, entry->pos);
542 gui_entry_fix_cursor(entry);
543 gui_entry_draw(entry);
546 void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space)
550 g_return_if_fail(entry != NULL);
557 while (entry->text[to] == ' ' && to > 0)
559 while (entry->text[to] != ' ' && to > 0)
562 while (!i_isalnum(entry->text[to]) && to > 0)
564 while (i_isalnum(entry->text[to]) && to > 0)
569 gui_entry_erase(entry, entry->pos-to, TRUE);
572 void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space)
576 g_return_if_fail(entry != NULL);
577 if (entry->pos == entry->text_len)
582 while (entry->text[to] == ' ' && to < entry->text_len)
584 while (entry->text[to] != ' ' && to < entry->text_len)
587 while (!i_isalnum(entry->text[to]) && to < entry->text_len)
589 while (i_isalnum(entry->text[to]) && to < entry->text_len)
593 size = to-entry->pos;
595 gui_entry_erase(entry, size, TRUE);
598 void gui_entry_transpose_chars(GUI_ENTRY_REC *entry)
602 if (entry->pos == 0 || entry->text_len < 2)
605 if (entry->pos == entry->text_len)
609 chr = entry->text[entry->pos];
610 entry->text[entry->pos] = entry->text[entry->pos-1];
611 entry->text[entry->pos-1] = chr;
615 gui_entry_redraw_from(entry, entry->pos-2);
616 gui_entry_fix_cursor(entry);
617 gui_entry_draw(entry);
620 void gui_entry_transpose_words(GUI_ENTRY_REC *entry)
622 int spos1, epos1, spos2, epos2;
624 /* find last position */
626 while (epos2 < entry->text_len && !i_isalnum(entry->text[epos2]))
628 while (epos2 < entry->text_len && i_isalnum(entry->text[epos2]))
631 /* find other position */
633 while (spos2 > 0 && !i_isalnum(entry->text[spos2-1]))
635 while (spos2 > 0 && i_isalnum(entry->text[spos2-1]))
639 while (epos1 > 0 && !i_isalnum(entry->text[epos1-1]))
643 while (spos1 > 0 && i_isalnum(entry->text[spos1-1]))
646 /* do wordswap if any found */
647 if (spos1 < epos1 && epos1 < spos2 && spos2 < epos2) {
648 unichar *first, *sep, *second;
651 first = (unichar *) g_malloc( (epos1 - spos1) * sizeof(unichar) );
652 sep = (unichar *) g_malloc( (spos2 - epos1) * sizeof(unichar) );
653 second = (unichar *) g_malloc( (epos2 - spos2) * sizeof(unichar) );
655 for (i = spos1; i < epos1; i++)
656 first[i-spos1] = entry->text[i];
657 for (i = epos1; i < spos2; i++)
658 sep[i-epos1] = entry->text[i];
659 for (i = spos2; i < epos2; i++)
660 second[i-spos2] = entry->text[i];
663 for (i = 0; i < epos2-spos2; i++)
664 entry->text[entry->pos++] = second[i];
665 for (i = 0; i < spos2-epos1; i++)
666 entry->text[entry->pos++] = sep[i];
667 for (i = 0; i < epos1-spos1; i++)
668 entry->text[entry->pos++] = first[i];
676 gui_entry_redraw_from(entry, spos1);
677 gui_entry_fix_cursor(entry);
678 gui_entry_draw(entry);
681 void gui_entry_capitalize_word(GUI_ENTRY_REC *entry)
683 int pos = entry->pos;
684 while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
687 if (pos < entry->text_len) {
688 entry->text[pos] = i_toupper(entry->text[pos]);
692 while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
693 entry->text[pos] = i_tolower(entry->text[pos]);
697 gui_entry_redraw_from(entry, entry->pos);
699 gui_entry_fix_cursor(entry);
700 gui_entry_draw(entry);
703 void gui_entry_downcase_word(GUI_ENTRY_REC *entry)
705 int pos = entry->pos;
706 while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
709 while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
710 entry->text[pos] = i_tolower(entry->text[pos]);
714 gui_entry_redraw_from(entry, entry->pos);
716 gui_entry_fix_cursor(entry);
717 gui_entry_draw(entry);
720 void gui_entry_upcase_word(GUI_ENTRY_REC *entry)
722 int pos = entry->pos;
723 while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
726 while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
727 entry->text[pos] = i_toupper(entry->text[pos]);
731 gui_entry_redraw_from(entry, entry->pos);
733 gui_entry_fix_cursor(entry);
734 gui_entry_draw(entry);
737 int gui_entry_get_pos(GUI_ENTRY_REC *entry)
739 g_return_val_if_fail(entry != NULL, 0);
744 void gui_entry_set_pos(GUI_ENTRY_REC *entry, int pos)
746 g_return_if_fail(entry != NULL);
748 if (pos >= 0 && pos <= entry->text_len)
751 gui_entry_fix_cursor(entry);
752 gui_entry_draw(entry);
755 void gui_entry_move_pos(GUI_ENTRY_REC *entry, int pos)
757 g_return_if_fail(entry != NULL);
759 if (entry->pos + pos >= 0 && entry->pos + pos <= entry->text_len)
762 gui_entry_fix_cursor(entry);
763 gui_entry_draw(entry);
766 static void gui_entry_move_words_left(GUI_ENTRY_REC *entry, int count, int to_space)
771 while (count > 0 && pos > 0) {
773 while (pos > 0 && entry->text[pos-1] == ' ')
775 while (pos > 0 && entry->text[pos-1] != ' ')
778 while (pos > 0 && !i_isalnum(entry->text[pos-1]))
780 while (pos > 0 && i_isalnum(entry->text[pos-1]))
789 static void gui_entry_move_words_right(GUI_ENTRY_REC *entry, int count, int to_space)
794 while (count > 0 && pos < entry->text_len) {
796 while (pos < entry->text_len && entry->text[pos] == ' ')
798 while (pos < entry->text_len && entry->text[pos] != ' ')
801 while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
803 while (pos < entry->text_len && i_isalnum(entry->text[pos]))
812 void gui_entry_move_words(GUI_ENTRY_REC *entry, int count, int to_space)
814 g_return_if_fail(entry != NULL);
817 gui_entry_move_words_left(entry, -count, to_space);
819 gui_entry_move_words_right(entry, count, to_space);
821 gui_entry_fix_cursor(entry);
822 gui_entry_draw(entry);
825 void gui_entry_redraw(GUI_ENTRY_REC *entry)
827 g_return_if_fail(entry != NULL);
829 gui_entry_set_prompt(entry, NULL);
830 gui_entry_redraw_from(entry, 0);
831 gui_entry_fix_cursor(entry);
832 gui_entry_draw(entry);