comparison finch/libgnt/gntentry.c @ 16889:c31328dba5c2

Fix some ickyness in the tab-completion. Now, if there's only one suggest word, then the first tab will just complete the suggestion. If there's only one suggest word, and it's already completed, then tab will take focus to the next widget. If there is a dropdown, then you can select a suggest word by pressing tabs to move to it, then either space or enter to accept it.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Fri, 04 May 2007 15:53:24 +0000
parents efbced3f38ac
children 6502a3846264
comparison
equal deleted inserted replaced
16888:d2904afe89e2 16889:c31328dba5c2
42 } 42 }
43 return s; 43 return s;
44 } 44 }
45 45
46 static gboolean 46 static gboolean
47 complete_suggest(GntEntry *entry, const char *text)
48 {
49 gboolean changed = FALSE;
50 if (entry->word) {
51 char *s = get_beginning_of_word(entry);
52 const char *iter = text;
53 while (*iter && toupper(*s) == toupper(*iter)) {
54 if (*s != *iter)
55 changed = TRUE;
56 *s++ = *iter++;
57 }
58 if (*iter) {
59 gnt_entry_key_pressed(GNT_WIDGET(entry), iter);
60 changed = TRUE;
61 }
62 } else {
63 gnt_entry_set_text_internal(entry, text);
64 changed = TRUE;
65 }
66 return changed;
67 }
68
69 static gboolean
47 show_suggest_dropdown(GntEntry *entry) 70 show_suggest_dropdown(GntEntry *entry)
48 { 71 {
49 char *suggest = NULL; 72 char *suggest = NULL;
50 int len; 73 int len;
51 int offset = 0, x, y; 74 int offset = 0, x, y;
52 int count = 0; 75 int count = 0;
53 GList *iter; 76 GList *iter;
77 const char *text = NULL;
54 78
55 if (entry->word) 79 if (entry->word)
56 { 80 {
57 char *s = get_beginning_of_word(entry); 81 char *s = get_beginning_of_word(entry);
58 suggest = g_strndup(s, entry->cursor - s); 82 suggest = g_strndup(s, entry->cursor - s);
83 else 107 else
84 gnt_tree_remove_all(GNT_TREE(entry->ddown)); 108 gnt_tree_remove_all(GNT_TREE(entry->ddown));
85 109
86 for (count = 0, iter = entry->suggests; iter; iter = iter->next) 110 for (count = 0, iter = entry->suggests; iter; iter = iter->next)
87 { 111 {
88 const char *text = iter->data; 112 text = iter->data;
89 if (g_ascii_strncasecmp(suggest, text, len) == 0 && strlen(text) >= len) 113 if (g_ascii_strncasecmp(suggest, text, len) == 0 && strlen(text) >= len)
90 { 114 {
91 gnt_tree_add_row_after(GNT_TREE(entry->ddown), (gpointer)text, 115 gnt_tree_add_row_after(GNT_TREE(entry->ddown), (gpointer)text,
92 gnt_tree_create_row(GNT_TREE(entry->ddown), text), 116 gnt_tree_create_row(GNT_TREE(entry->ddown), text),
93 NULL, NULL); 117 NULL, NULL);
94 count++; 118 count++;
95 } 119 }
96 } 120 }
97 g_free(suggest); 121 g_free(suggest);
98 122
99 if (count == 0) 123 if (count == 0) {
100 {
101 destroy_suggest(entry); 124 destroy_suggest(entry);
102 return FALSE; 125 return FALSE;
103 } 126 } else if (count == 1) {
104 127 destroy_suggest(entry);
105 gnt_widget_draw(entry->ddown->parent); 128 return complete_suggest(entry, text);
129 } else {
130 gnt_widget_draw(entry->ddown->parent);
131 }
132
106 return TRUE; 133 return TRUE;
107 } 134 }
108 135
109 static void 136 static void
110 gnt_entry_draw(GntWidget *widget) 137 gnt_entry_draw(GntWidget *widget)
322 static gboolean 349 static gboolean
323 suggest_show(GntBindable *bind, GList *null) 350 suggest_show(GntBindable *bind, GList *null)
324 { 351 {
325 GntEntry *entry = GNT_ENTRY(bind); 352 GntEntry *entry = GNT_ENTRY(bind);
326 if (entry->ddown) { 353 if (entry->ddown) {
327 if (g_list_length(GNT_TREE(entry->ddown)->list) == 1) 354 gnt_bindable_perform_action_named(GNT_BINDABLE(entry->ddown), "move-down");
328 gnt_entry_key_pressed(GNT_WIDGET(entry), "\r");
329 else
330 gnt_bindable_perform_action_named(GNT_BINDABLE(entry->ddown), "move-down");
331 return TRUE; 355 return TRUE;
332 } 356 }
333 return show_suggest_dropdown(entry); 357 return show_suggest_dropdown(entry);
334 } 358 }
335 359
510 534
511 return FALSE; 535 return FALSE;
512 } 536 }
513 else 537 else
514 { 538 {
515 if (text[0] == '\t') 539 if ((text[0] == '\r' || text[0] == ' ') && entry->ddown)
516 {
517 if (entry->ddown)
518 destroy_suggest(entry);
519 else if (entry->suggests)
520 return show_suggest_dropdown(entry);
521
522 return FALSE;
523 }
524 else if (text[0] == '\r' && entry->ddown)
525 { 540 {
526 char *text = g_strdup(gnt_tree_get_selection_data(GNT_TREE(entry->ddown))); 541 char *text = g_strdup(gnt_tree_get_selection_data(GNT_TREE(entry->ddown)));
527 destroy_suggest(entry); 542 destroy_suggest(entry);
528 if (entry->word) 543 complete_suggest(entry, text);
529 {
530 char *s = get_beginning_of_word(entry);
531 char *iter = text;
532 while (*iter && toupper(*s) == toupper(*iter))
533 {
534 *s++ = *iter++;
535 }
536 gnt_entry_key_pressed(widget, iter);
537 }
538 else
539 {
540 gnt_entry_set_text_internal(entry, text);
541 }
542 g_free(text); 544 g_free(text);
543 entry_text_changed(entry); 545 entry_text_changed(entry);
544 return TRUE; 546 return TRUE;
545 } 547 }
546 548