comparison console/libgnt/gntentry.c @ 15231:4f0575a9264d

[gaim-migrate @ 18020] text-changed signal for GntEntry. Not used anywhere yet, but will be in the future. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Mon, 18 Dec 2006 19:41:38 +0000
parents 3fb20516042b
children 492f4e7416a3
comparison
equal deleted inserted replaced
15230:c42968092d15 15231:4f0575a9264d
7 #include "gnttree.h" 7 #include "gnttree.h"
8 #include "gntutils.h" 8 #include "gntutils.h"
9 9
10 enum 10 enum
11 { 11 {
12 SIGS = 1, 12 SIG_TEXT_CHANGED,
13 SIGS,
13 }; 14 };
15 static guint signals[SIGS] = { 0 };
14 16
15 static GntWidgetClass *parent_class = NULL; 17 static GntWidgetClass *parent_class = NULL;
18
19 static void gnt_entry_set_text_internal(GntEntry *entry, const char *text);
16 20
17 static void 21 static void
18 destroy_suggest(GntEntry *entry) 22 destroy_suggest(GntEntry *entry)
19 { 23 {
20 if (entry->ddown) 24 if (entry->ddown)
155 { 159 {
156 gnt_entry_draw(widget); 160 gnt_entry_draw(widget);
157 gnt_widget_queue_update(widget); 161 gnt_widget_queue_update(widget);
158 } 162 }
159 163
164 static void
165 entry_text_changed(GntEntry *entry)
166 {
167 g_signal_emit(entry, signals[SIG_TEXT_CHANGED], 0);
168 }
169
160 static gboolean 170 static gboolean
161 move_back(GntBindable *bind, GList *null) 171 move_back(GntBindable *bind, GList *null)
162 { 172 {
163 GntEntry *entry = GNT_ENTRY(bind); 173 GntEntry *entry = GNT_ENTRY(bind);
164 if (entry->cursor <= entry->start) 174 if (entry->cursor <= entry->start)
201 entry->scroll = g_utf8_find_prev_char(entry->start, entry->scroll); 211 entry->scroll = g_utf8_find_prev_char(entry->start, entry->scroll);
202 212
203 entry_redraw(GNT_WIDGET(entry)); 213 entry_redraw(GNT_WIDGET(entry));
204 if (entry->ddown) 214 if (entry->ddown)
205 show_suggest_dropdown(entry); 215 show_suggest_dropdown(entry);
216 entry_text_changed(entry);
206 return TRUE; 217 return TRUE;
207 } 218 }
208 219
209 static gboolean 220 static gboolean
210 delkey(GntBindable *bind, GList *null) 221 delkey(GntBindable *bind, GList *null)
220 entry->end -= len; 231 entry->end -= len;
221 entry_redraw(GNT_WIDGET(entry)); 232 entry_redraw(GNT_WIDGET(entry));
222 233
223 if (entry->ddown) 234 if (entry->ddown)
224 show_suggest_dropdown(entry); 235 show_suggest_dropdown(entry);
236 entry_text_changed(entry);
225 return TRUE; 237 return TRUE;
226 } 238 }
227 239
228 static gboolean 240 static gboolean
229 move_start(GntBindable *bind, GList *null) 241 move_start(GntBindable *bind, GList *null)
251 { 263 {
252 GntEntry *entry = GNT_ENTRY(bind); 264 GntEntry *entry = GNT_ENTRY(bind);
253 if (entry->histlength && entry->history->prev) 265 if (entry->histlength && entry->history->prev)
254 { 266 {
255 entry->history = entry->history->prev; 267 entry->history = entry->history->prev;
256 gnt_entry_set_text(entry, entry->history->data); 268 gnt_entry_set_text_internal(entry, entry->history->data);
257 destroy_suggest(entry); 269 destroy_suggest(entry);
270 entry_text_changed(entry);
258 271
259 return TRUE; 272 return TRUE;
260 } 273 }
261 return FALSE; 274 return FALSE;
262 } 275 }
274 g_free(entry->history->data); 287 g_free(entry->history->data);
275 entry->history->data = text; 288 entry->history->data = text;
276 } 289 }
277 290
278 entry->history = entry->history->next; 291 entry->history = entry->history->next;
279 gnt_entry_set_text(entry, entry->history->data); 292 gnt_entry_set_text_internal(entry, entry->history->data);
280 destroy_suggest(entry); 293 destroy_suggest(entry);
294 entry_text_changed(entry);
281 295
282 return TRUE; 296 return TRUE;
283 } 297 }
284 return FALSE; 298 return FALSE;
285 } 299 }
314 328
315 static gboolean 329 static gboolean
316 del_to_home(GntBindable *bind, GList *null) 330 del_to_home(GntBindable *bind, GList *null)
317 { 331 {
318 GntEntry *entry = GNT_ENTRY(bind); 332 GntEntry *entry = GNT_ENTRY(bind);
333 if (entry->cursor <= entry->start)
334 return TRUE;
319 memmove(entry->start, entry->cursor, entry->end - entry->cursor); 335 memmove(entry->start, entry->cursor, entry->end - entry->cursor);
320 entry->end -= (entry->cursor - entry->start); 336 entry->end -= (entry->cursor - entry->start);
321 entry->cursor = entry->scroll = entry->start; 337 entry->cursor = entry->scroll = entry->start;
322 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start)); 338 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start));
323 entry_redraw(GNT_WIDGET(bind)); 339 entry_redraw(GNT_WIDGET(bind));
340 entry_text_changed(entry);
324 return TRUE; 341 return TRUE;
325 } 342 }
326 343
327 static gboolean 344 static gboolean
328 del_to_end(GntBindable *bind, GList *null) 345 del_to_end(GntBindable *bind, GList *null)
329 { 346 {
330 GntEntry *entry = GNT_ENTRY(bind); 347 GntEntry *entry = GNT_ENTRY(bind);
348 if (entry->end <= entry->cursor)
349 return TRUE;
331 entry->end = entry->cursor; 350 entry->end = entry->cursor;
332 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start)); 351 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start));
333 entry_redraw(GNT_WIDGET(bind)); 352 entry_redraw(GNT_WIDGET(bind));
353 entry_text_changed(entry);
334 return TRUE; 354 return TRUE;
335 } 355 }
336 356
337 #define SAME(a,b) ((g_unichar_isalpha(a) && g_unichar_isalpha(b)) || \ 357 #define SAME(a,b) ((g_unichar_isalpha(a) && g_unichar_isalpha(b)) || \
338 (g_unichar_isdigit(a) && g_unichar_isdigit(b)) || \ 358 (g_unichar_isdigit(a) && g_unichar_isdigit(b)) || \
408 if (entry->scroll < entry->start) 428 if (entry->scroll < entry->start)
409 entry->scroll = entry->start; 429 entry->scroll = entry->start;
410 } 430 }
411 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start)); 431 memset(entry->end, '\0', entry->buffer - (entry->end - entry->start));
412 entry_redraw(widget); 432 entry_redraw(widget);
433 entry_text_changed(entry);
413 434
414 return TRUE; 435 return TRUE;
415 } 436 }
416 437
417 static gboolean 438 static gboolean
432 { 453 {
433 GntEntry *entry = GNT_ENTRY(bind); 454 GntEntry *entry = GNT_ENTRY(bind);
434 GntWidget *widget = GNT_WIDGET(bind); 455 GntWidget *widget = GNT_WIDGET(bind);
435 char *iter = (char *)next_begin_word(entry->cursor, entry->end); 456 char *iter = (char *)next_begin_word(entry->cursor, entry->end);
436 int len = entry->end - iter + 1; 457 int len = entry->end - iter + 1;
458 if (len <= 0)
459 return TRUE;
437 memmove(entry->cursor, iter, len); 460 memmove(entry->cursor, iter, len);
438 len = iter - entry->cursor; 461 len = iter - entry->cursor;
439 entry->end -= len; 462 entry->end -= len;
440 memset(entry->end, '\0', len); 463 memset(entry->end, '\0', len);
441 entry_redraw(widget); 464 entry_redraw(widget);
465 entry_text_changed(entry);
442 return TRUE; 466 return TRUE;
443 } 467 }
444 468
445 static gboolean 469 static gboolean
446 gnt_entry_key_pressed(GntWidget *widget, const char *text) 470 gnt_entry_key_pressed(GntWidget *widget, const char *text)
482 } 506 }
483 gnt_entry_key_pressed(widget, iter); 507 gnt_entry_key_pressed(widget, iter);
484 } 508 }
485 else 509 else
486 { 510 {
487 gnt_entry_set_text(entry, text); 511 gnt_entry_set_text_internal(entry, text);
488 } 512 }
489 g_free(text); 513 g_free(text);
514 entry_text_changed(entry);
490 return TRUE; 515 return TRUE;
491 } 516 }
492 517
493 if (!iscntrl(text[0])) 518 if (!iscntrl(text[0]))
494 { 519 {
517 542
518 if (entry->end + len - entry->start >= entry->buffer) 543 if (entry->end + len - entry->start >= entry->buffer)
519 { 544 {
520 /* This will cause the buffer to grow */ 545 /* This will cause the buffer to grow */
521 char *tmp = g_strdup_printf("%s%*s", entry->start, len, ""); 546 char *tmp = g_strdup_printf("%s%*s", entry->start, len, "");
522 gnt_entry_set_text(entry, tmp); 547 gnt_entry_set_text_internal(entry, tmp);
523 g_free(tmp); 548 g_free(tmp);
524 } 549 }
525 550
526 memmove(entry->cursor + len, entry->cursor, entry->end - entry->cursor + 1); 551 memmove(entry->cursor + len, entry->cursor, entry->end - entry->cursor + 1);
527 entry->end += len; 552 entry->end += len;
541 566
542 if (entry->ddown) 567 if (entry->ddown)
543 show_suggest_dropdown(entry); 568 show_suggest_dropdown(entry);
544 } 569 }
545 entry_redraw(widget); 570 entry_redraw(widget);
571 entry_text_changed(entry);
546 return TRUE; 572 return TRUE;
547 } 573 }
548 } 574 }
549 575
550 return FALSE; 576 return FALSE;
594 parent_class->draw = gnt_entry_draw; 620 parent_class->draw = gnt_entry_draw;
595 parent_class->map = gnt_entry_map; 621 parent_class->map = gnt_entry_map;
596 parent_class->size_request = gnt_entry_size_request; 622 parent_class->size_request = gnt_entry_size_request;
597 parent_class->key_pressed = gnt_entry_key_pressed; 623 parent_class->key_pressed = gnt_entry_key_pressed;
598 parent_class->lost_focus = gnt_entry_lost_focus; 624 parent_class->lost_focus = gnt_entry_lost_focus;
625
626 signals[SIG_TEXT_CHANGED] =
627 g_signal_new("text_changed",
628 G_TYPE_FROM_CLASS(klass),
629 G_SIGNAL_RUN_LAST,
630 G_STRUCT_OFFSET(GntEntryClass, text_changed),
631 NULL, NULL,
632 g_cclosure_marshal_VOID__VOID,
633 G_TYPE_NONE, 0);
599 634
600 gnt_bindable_class_register_action(bindable, "cursor-home", move_start, 635 gnt_bindable_class_register_action(bindable, "cursor-home", move_start,
601 GNT_KEY_CTRL_A, NULL); 636 GNT_KEY_CTRL_A, NULL);
602 gnt_bindable_register_binding(bindable, "cursor-home", GNT_KEY_HOME, NULL); 637 gnt_bindable_register_binding(bindable, "cursor-home", GNT_KEY_HOME, NULL);
603 gnt_bindable_class_register_action(bindable, "cursor-end", move_end, 638 gnt_bindable_class_register_action(bindable, "cursor-end", move_end,
701 GntWidget *gnt_entry_new(const char *text) 736 GntWidget *gnt_entry_new(const char *text)
702 { 737 {
703 GntWidget *widget = g_object_new(GNT_TYPE_ENTRY, NULL); 738 GntWidget *widget = g_object_new(GNT_TYPE_ENTRY, NULL);
704 GntEntry *entry = GNT_ENTRY(widget); 739 GntEntry *entry = GNT_ENTRY(widget);
705 740
706 gnt_entry_set_text(entry, text); 741 gnt_entry_set_text_internal(entry, text);
707 742
708 return widget; 743 return widget;
709 } 744 }
710 745
711 void gnt_entry_set_text(GntEntry *entry, const char *text) 746 static void
747 gnt_entry_set_text_internal(GntEntry *entry, const char *text)
712 { 748 {
713 int len; 749 int len;
714 int scroll, cursor; 750 int scroll, cursor;
715 751
716 g_free(entry->start); 752 g_free(entry->start);
739 775
740 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(entry), GNT_WIDGET_MAPPED)) 776 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(entry), GNT_WIDGET_MAPPED))
741 entry_redraw(GNT_WIDGET(entry)); 777 entry_redraw(GNT_WIDGET(entry));
742 } 778 }
743 779
780 void gnt_entry_set_text(GntEntry *entry, const char *text)
781 {
782 gboolean changed = TRUE;
783 if (text == NULL && entry->start == NULL)
784 changed = FALSE;
785 if (text && entry->start && g_utf8_collate(text, entry->start) == 0)
786 changed = FALSE;
787 gnt_entry_set_text_internal(entry, text);
788 if (changed)
789 entry_text_changed(entry);
790 }
791
744 void gnt_entry_set_max(GntEntry *entry, int max) 792 void gnt_entry_set_max(GntEntry *entry, int max)
745 { 793 {
746 entry->max = max; 794 entry->max = max;
747 } 795 }
748 796
757 return entry->start; 805 return entry->start;
758 } 806 }
759 807
760 void gnt_entry_clear(GntEntry *entry) 808 void gnt_entry_clear(GntEntry *entry)
761 { 809 {
762 gnt_entry_set_text(entry, NULL); 810 gnt_entry_set_text_internal(entry, NULL);
763 entry->scroll = entry->cursor = entry->end = entry->start; 811 entry->scroll = entry->cursor = entry->end = entry->start;
764 entry_redraw(GNT_WIDGET(entry)); 812 entry_redraw(GNT_WIDGET(entry));
765 destroy_suggest(entry); 813 destroy_suggest(entry);
814 entry_text_changed(entry);
766 } 815 }
767 816
768 void gnt_entry_set_masked(GntEntry *entry, gboolean set) 817 void gnt_entry_set_masked(GntEntry *entry, gboolean set)
769 { 818 {
770 entry->masked = set; 819 entry->masked = set;