2 * $Id: inputstr.c,v 1.69 2011/01/16 21:52:35 tom Exp $
4 * inputstr.c -- functions for input/display of a string
6 * Copyright 2000-2010,2011 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
33 #if defined(HAVE_SEARCH_H) && defined(HAVE_TSEARCH)
43 #if defined(USE_WIDE_CURSES)
45 #elif defined(HAVE_XDIALOG)
46 #define USE_CACHING 1 /* editbox really needs caching! */
51 typedef struct _cache {
54 struct _cache *cache_at; /* unique: associate caches by CACHE */
55 const char *string_at; /* unique: associate caches by char* */
57 size_t s_len; /* strlen(string) - we add 1 for EOS */
58 size_t i_len; /* length(list) - we add 1 for EOS */
59 char *string; /* a copy of the last-processed string */
60 int *list; /* indices into the string */
64 #define SAME_CACHE(c,s,l) (c->string != 0 && memcmp(c->string,s,l) == 0)
66 static CACHE *cache_list;
69 static void *sorted_cache;
72 #ifdef USE_WIDE_CURSES
76 static int result = -1;
78 char *test = setlocale(LC_ALL, 0);
79 if (test == 0 || *test == 0) {
81 } else if (strcmp(test, "C") && strcmp(test, "POSIX")) {
93 compare_cache(const void *a, const void *b)
95 const CACHE *p = (const CACHE *) a;
96 const CACHE *q = (const CACHE *) b;
98 result = (int) (p->cache_at - q->cache_at);
100 result = (int) (p->string_at - q->string_at);
106 find_cache(CACHE * cache, const char *string)
114 memset(&find, 0, sizeof(find));
115 find.cache_at = cache;
116 find.string_at = string;
118 if ((pp = tfind(&find, &sorted_cache, compare_cache)) != 0) {
124 for (p = cache_list; p != 0; p = p->next) {
125 if (p->cache_at == cache
126 && p->string_at == string) {
135 make_cache(CACHE * cache, const char *string)
139 p = dlg_calloc(CACHE, 1);
140 assert_ptr(p, "load_cache");
141 p->next = cache_list;
145 p->string_at = string;
149 (void) tsearch(p, &sorted_cache, compare_cache);
154 load_cache(CACHE * cache, const char *string)
158 if ((p = find_cache(cache, string)) != 0) {
161 make_cache(cache, string);
166 save_cache(CACHE * cache, const char *string)
170 if ((p = find_cache(cache, string)) != 0) {
177 #define SAME_CACHE(c,s,l) (c->string != 0)
178 #define load_cache(cache, string) /* nothing */
179 #define save_cache(cache, string) /* nothing */
180 #endif /* USE_WIDE_CURSES */
183 * If the given string has not changed, we do not need to update the index.
184 * If we need to update the index, allocate enough memory for it.
187 same_cache2(CACHE * cache, const char *string, unsigned i_len)
190 size_t s_len = strlen(string);
192 if (cache->s_len != 0
193 && cache->s_len >= s_len
195 && SAME_CACHE(cache, string, (size_t) s_len)) {
200 if (cache->list == 0) {
201 cache->list = dlg_malloc(int, need);
202 } else if (cache->i_len < i_len) {
203 cache->list = dlg_realloc(int, need, cache->list);
205 cache->i_len = i_len;
207 if (cache->s_len >= s_len && cache->string != 0) {
208 strcpy(cache->string, string);
210 if (cache->string != 0)
212 cache->string = dlg_strclone(string);
214 cache->s_len = s_len;
219 #ifdef USE_WIDE_CURSES
221 * Like same_cache2(), but we are only concerned about caching a copy of the
222 * string and its associated length.
225 same_cache1(CACHE * cache, const char *string, size_t i_len)
227 size_t s_len = strlen(string);
229 if (cache->s_len == s_len
230 && SAME_CACHE(cache, string, (size_t) s_len)) {
234 if (cache->s_len >= s_len && cache->string != 0) {
235 strcpy(cache->string, string);
237 if (cache->string != 0)
239 cache->string = dlg_strclone(string);
241 cache->s_len = s_len;
242 cache->i_len = i_len;
246 #endif /* USE_CACHING */
249 * Counts the number of bytes that make up complete wide-characters, up to byte
250 * 'len'. If there is no locale set, simply return the original length.
252 #ifdef USE_WIDE_CURSES
254 dlg_count_wcbytes(const char *string, size_t len)
261 load_cache(&cache, string);
262 if (!same_cache1(&cache, string, len)) {
266 const char *src = cache.string;
268 char save = cache.string[len];
270 cache.string[len] = '\0';
271 memset(&state, 0, sizeof(state));
272 code = mbsrtowcs((wchar_t *) 0, &src, len, &state);
273 cache.string[len] = save;
274 if ((int) code >= 0) {
281 save_cache(&cache, string);
283 result = (int) cache.i_len;
289 #endif /* USE_WIDE_CURSES */
292 * Counts the number of wide-characters in the string.
295 dlg_count_wchars(const char *string)
299 #ifdef USE_WIDE_CURSES
302 size_t len = strlen(string);
304 load_cache(&cache, string);
305 if (!same_cache1(&cache, string, len)) {
306 const char *src = cache.string;
308 int part = dlg_count_wcbytes(cache.string, len);
309 char save = cache.string[part];
311 wchar_t *temp = dlg_calloc(wchar_t, len + 1);
313 cache.string[part] = '\0';
314 memset(&state, 0, sizeof(state));
315 code = mbsrtowcs(temp, &src, (size_t) part, &state);
316 cache.i_len = ((int) code >= 0) ? wcslen(temp) : 0;
317 cache.string[part] = save;
319 save_cache(&cache, string);
321 result = (int) cache.i_len;
323 #endif /* USE_WIDE_CURSES */
325 result = (int) strlen(string);
331 * Build an index of the wide-characters in the string, so we can easily tell
332 * which byte-offset begins a given wide-character.
335 dlg_index_wchars(const char *string)
338 unsigned len = (unsigned) dlg_count_wchars(string);
341 load_cache(&cache, string);
342 if (!same_cache2(&cache, string, len)) {
343 const char *current = string;
346 for (inx = 1; inx <= len; ++inx) {
347 #ifdef USE_WIDE_CURSES
351 memset(&state, 0, sizeof(state));
352 width = (int) mbrlen(current, strlen(current), &state);
354 width = 1; /* FIXME: what if we have a control-char? */
356 cache.list[inx] = cache.list[inx - 1] + width;
358 #endif /* USE_WIDE_CURSES */
361 cache.list[inx] = (int) inx;
364 save_cache(&cache, string);
370 * Given the character-offset to find in the list, return the corresponding
374 dlg_find_index(const int *list, int limit, int to_find)
377 for (result = 0; result <= limit; ++result) {
378 if (to_find == list[result]
380 || to_find < list[result + 1])
387 * Build a list of the display-columns for the given string's characters.
390 dlg_index_columns(const char *string)
393 unsigned len = (unsigned) dlg_count_wchars(string);
396 load_cache(&cache, string);
397 if (!same_cache2(&cache, string, len)) {
399 #ifdef USE_WIDE_CURSES
401 size_t num_bytes = strlen(string);
402 const int *inx_wchars = dlg_index_wchars(string);
405 for (inx = 0; inx < len; ++inx) {
410 if (string[inx_wchars[inx]] == TAB) {
411 result = ((cache.list[inx] | 7) + 1) - cache.list[inx];
413 memset(&state, 0, sizeof(state));
414 memset(temp, 0, sizeof(temp));
415 check = mbrtowc(temp,
416 string + inx_wchars[inx],
417 num_bytes - (size_t) inx_wchars[inx],
419 if ((int) check <= 0) {
422 result = wcwidth(temp[0]);
425 const wchar_t *printable;
426 cchar_t temp2, *temp2p = &temp2;
427 setcchar(temp2p, temp, 0, 0, 0);
428 printable = wunctrl(temp2p);
429 result = printable ? (int) wcslen(printable) : 1;
432 cache.list[inx + 1] = result;
434 cache.list[inx + 1] += cache.list[inx];
437 #endif /* USE_WIDE_CURSES */
439 for (inx = 0; inx < len; ++inx) {
440 chtype ch = UCH(string[inx]);
443 cache.list[inx + 1] =
444 ((cache.list[inx] | 7) + 1) - cache.list[inx];
445 else if (isprint(ch))
446 cache.list[inx + 1] = 1;
448 const char *printable;
449 printable = unctrl(ch);
450 cache.list[inx + 1] = (printable
451 ? (int) strlen(printable)
455 cache.list[inx + 1] += cache.list[inx];
458 save_cache(&cache, string);
464 * Returns the number of columns used for a string. That happens to be the
465 * end-value of the cols[] array.
468 dlg_count_columns(const char *string)
471 int limit = dlg_count_wchars(string);
473 const int *cols = dlg_index_columns(string);
474 result = cols[limit];
476 result = (int) strlen(string);
482 * Given a column limit, count the number of wide characters that can fit
483 * into that limit. The offset is used to skip over a leading character
484 * that was already written.
487 dlg_limit_columns(const char *string, int limit, int offset)
489 const int *cols = dlg_index_columns(string);
490 int result = dlg_count_wchars(string);
492 while (result > 0 && (cols[result] - cols[offset]) > limit)
498 * Updates the string and character-offset, given various editing characters
499 * or literal characters which are inserted at the character-offset.
502 dlg_edit_string(char *string, int *chr_offset, int key, int fkey, bool force)
505 int len = (int) strlen(string);
506 int limit = dlg_count_wchars(string);
507 const int *indx = dlg_index_wchars(string);
508 int offset = dlg_find_index(indx, limit, *chr_offset);
509 int max_len = dlg_max_input(MAX_LEN);
512 /* transform editing characters into equivalent function-keys */
514 fkey = TRUE; /* assume we transform */
520 fkey = FALSE; /* this is used for navigation */
523 fkey = FALSE; /* ...no, we did not transform */
530 case 0: /* special case for loop entry */
535 *chr_offset = indx[offset - 1];
537 case DLGK_GRID_RIGHT:
539 *chr_offset = indx[offset + 1];
547 *chr_offset = indx[limit];
549 case DLGK_DELETE_LEFT:
551 int gap = indx[offset] - indx[offset - 1];
552 *chr_offset = indx[offset - 1];
554 for (i = *chr_offset;
555 (string[i] = string[i + gap]) != '\0';
562 case DLGK_DELETE_RIGHT:
565 string[*chr_offset = 0] = '\0';
567 int gap = ((offset <= limit)
568 ? (indx[offset + 1] - indx[offset])
571 for (i = indx[offset];
572 (string[i] = string[i + gap]) != '\0';
576 } else if (offset > 0) {
577 string[indx[offset - 1]] = '\0';
579 if (*chr_offset > indx[limit])
580 *chr_offset = indx[limit];
584 case DLGK_DELETE_ALL:
585 string[*chr_offset = 0] = '\0';
597 case DLGK_FIELD_NEXT:
598 case DLGK_FIELD_PREV:
609 if (key == ESC || key == ERR) {
613 for (i = ++len; i > *chr_offset; i--)
614 string[i] = string[i - 1];
615 string[*chr_offset] = (char) key;
626 compute_edit_offset(const char *string,
632 const int *cols = dlg_index_columns(string);
633 const int *indx = dlg_index_wchars(string);
634 int limit = dlg_count_wchars(string);
635 int offset = dlg_find_index(indx, limit, chr_offset);
640 for (n = offset2 = 0; n <= offset; ++n) {
641 if ((cols[offset] - cols[n]) < x_last
642 && (offset == limit || (cols[offset + 1] - cols[n]) < x_last)) {
648 dpy_column = cols[offset] - cols[offset2];
650 if (p_dpy_column != 0)
651 *p_dpy_column = dpy_column;
652 if (p_scroll_amt != 0)
653 *p_scroll_amt = offset2;
657 * Given the character-offset in the string, returns the display-offset where
658 * we will position the cursor.
661 dlg_edit_offset(char *string, int chr_offset, int x_last)
665 compute_edit_offset(string, chr_offset, x_last, &result, 0);
671 * Displays the string, shifted as necessary, to fit within the box and show
672 * the current character-offset.
675 dlg_show_string(WINDOW *win,
676 const char *string, /* string to display (may be multibyte) */
677 int chr_offset, /* character (not bytes) offset */
678 chtype attr, /* window-attributes */
679 int y_base, /* beginning row on screen */
680 int x_base, /* beginning column on screen */
681 int x_last, /* number of columns on screen */
682 bool hidden, /* if true, do not echo */
683 bool force) /* if true, force repaint */
685 x_last = MIN(x_last + x_base, getmaxx(win)) - x_base;
687 if (hidden && !dialog_vars.insecure) {
689 (void) wmove(win, y_base, x_base);
693 const int *cols = dlg_index_columns(string);
694 const int *indx = dlg_index_wchars(string);
695 int limit = dlg_count_wchars(string);
701 compute_edit_offset(string, chr_offset, x_last, &input_x, &scrollamt);
704 (void) wmove(win, y_base, x_base);
705 for (i = scrollamt, k = 0; i < limit && k < x_last; ++i) {
706 int check = cols[i + 1] - cols[scrollamt];
707 if (check <= x_last) {
708 for (j = indx[i]; j < indx[i + 1]; ++j) {
709 chtype ch = UCH(string[j]);
710 if (hidden && dialog_vars.insecure) {
712 } else if (ch == TAB) {
713 int count = cols[i + 1] - cols[i];
727 (void) wmove(win, y_base, x_base + input_x);
734 _dlg_inputstr_leaks(void)
737 while (cache_list != 0) {
738 CACHE *next = cache_list->next;
740 tdelete(cache_list, &sorted_cache, compare_cache);
742 if (cache_list->string != 0)
743 free(cache_list->string);
744 if (cache_list->list != 0)
745 free(cache_list->list);
749 #endif /* USE_CACHING */
751 #endif /* NO_LEAKS */