comparison console/libgnt/gntentry.c @ 14311:fda9dc44807d

[gaim-migrate @ 17001] Wide-characters should no longer eat characters at the end of the line in textview. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 23 Aug 2006 13:46:51 +0000
parents 578a2c9af05e
children 1bd09d355f9e
comparison
equal deleted inserted replaced
14310:a766441af5ea 14311:fda9dc44807d
1 #include <ctype.h> 1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <string.h> 2 #include <string.h>
4 #include <wchar.h>
5 3
6 #include "gntbox.h" 4 #include "gntbox.h"
7 #include "gntentry.h" 5 #include "gntentry.h"
8 #include "gnttree.h" 6 #include "gnttree.h"
7 #include "gntutils.h"
9 8
10 enum 9 enum
11 { 10 {
12 SIGS = 1, 11 SIGS = 1,
13 }; 12 };
14 13
15 static GntWidgetClass *parent_class = NULL; 14 static GntWidgetClass *parent_class = NULL;
16 static guint signals[SIGS] = { 0 }; 15 static guint signals[SIGS] = { 0 };
17
18 static int
19 get_onscreen_width(const char *start, const char *end)
20 {
21 wchar_t wch;
22 int size;
23 int width = 0;
24
25 while (start < end) {
26 if ((size = mbtowc(&wch, start, end - start)) > 0) {
27 start += size;
28 width += wcwidth(wch);
29 } else {
30 ++width;
31 ++start;
32 }
33 }
34 return width;
35 }
36 16
37 static void 17 static void
38 destroy_suggest(GntEntry *entry) 18 destroy_suggest(GntEntry *entry)
39 { 19 {
40 if (entry->ddown) 20 if (entry->ddown)
70 if (entry->word) 50 if (entry->word)
71 { 51 {
72 char *s = get_beginning_of_word(entry); 52 char *s = get_beginning_of_word(entry);
73 suggest = g_strndup(s, entry->cursor - s); 53 suggest = g_strndup(s, entry->cursor - s);
74 if (entry->scroll < s) 54 if (entry->scroll < s)
75 offset = get_onscreen_width(entry->scroll, s); 55 offset = gnt_util_onscreen_width(entry->scroll, s);
76 } 56 }
77 else 57 else
78 suggest = g_strdup(entry->start); 58 suggest = g_strdup(entry->start);
79 len = strlen(suggest); /* Don't need to use the utf8-function here */ 59 len = strlen(suggest); /* Don't need to use the utf8-function here */
80 60
139 g_utf8_pointer_to_offset(entry->scroll, entry->end)); 119 g_utf8_pointer_to_offset(entry->scroll, entry->end));
140 } 120 }
141 else 121 else
142 mvwprintw(widget->window, 0, 0, "%s", entry->scroll); 122 mvwprintw(widget->window, 0, 0, "%s", entry->scroll);
143 123
144 stop = get_onscreen_width(entry->scroll, entry->end); 124 stop = gnt_util_onscreen_width(entry->scroll, entry->end);
145 if (stop < widget->priv.width) 125 if (stop < widget->priv.width)
146 whline(widget->window, ENTRY_CHAR, widget->priv.width - stop); 126 whline(widget->window, ENTRY_CHAR, widget->priv.width - stop);
147 127
148 if (focus) 128 if (focus)
149 mvwchgat(widget->window, 0, get_onscreen_width(entry->scroll, entry->cursor), 129 mvwchgat(widget->window, 0, gnt_util_onscreen_width(entry->scroll, entry->cursor),
150 1, A_REVERSE, COLOR_PAIR(GNT_COLOR_TEXT_NORMAL), NULL); 130 1, A_REVERSE, COLOR_PAIR(GNT_COLOR_TEXT_NORMAL), NULL);
151 131
152 DEBUG; 132 DEBUG;
153 } 133 }
154 134
192 move_forward(GntEntry *entry) 172 move_forward(GntEntry *entry)
193 { 173 {
194 if (entry->cursor >= entry->end) 174 if (entry->cursor >= entry->end)
195 return; 175 return;
196 entry->cursor = g_utf8_find_next_char(entry->cursor, NULL); 176 entry->cursor = g_utf8_find_next_char(entry->cursor, NULL);
197 while (get_onscreen_width(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width) 177 while (gnt_util_onscreen_width(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width)
198 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL); 178 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
199 entry_redraw(GNT_WIDGET(entry)); 179 entry_redraw(GNT_WIDGET(entry));
200 } 180 }
201 181
202 static void 182 static void
247 static void 227 static void
248 move_end(GntEntry *entry) 228 move_end(GntEntry *entry)
249 { 229 {
250 entry->cursor = entry->end; 230 entry->cursor = entry->end;
251 /* This should be better than this */ 231 /* This should be better than this */
252 while (get_onscreen_width(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width) 232 while (gnt_util_onscreen_width(entry->scroll, entry->cursor) >= GNT_WIDGET(entry)->priv.width)
253 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL); 233 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
254 entry_redraw(GNT_WIDGET(entry)); 234 entry_redraw(GNT_WIDGET(entry));
255 } 235 }
256 236
257 static gboolean 237 static gboolean
408 *entry->cursor = *str; 388 *entry->cursor = *str;
409 entry->cursor++; 389 entry->cursor++;
410 str++; 390 str++;
411 } 391 }
412 392
413 while (get_onscreen_width(entry->scroll, entry->cursor) >= widget->priv.width) 393 while (gnt_util_onscreen_width(entry->scroll, entry->cursor) >= widget->priv.width)
414 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL); 394 entry->scroll = g_utf8_find_next_char(entry->scroll, NULL);
415 395
416 if (entry->ddown) 396 if (entry->ddown)
417 show_suggest_dropdown(entry); 397 show_suggest_dropdown(entry);
418 } 398 }