comparison finch/libgnt/gnttextview.c @ 18315:e659842fe66d

Add flags to a textview to decide whether to show scrollbars, and whether to use word-wrapping or character-wrapping.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 27 Jun 2007 20:35:11 +0000
parents 1cedd520cd18
children 841670dd24e1
comparison
equal deleted inserted replaced
18314:7a8283c1eb75 18315:e659842fe66d
63 { 63 {
64 GntTextView *view = GNT_TEXT_VIEW(widget); 64 GntTextView *view = GNT_TEXT_VIEW(widget);
65 int i = 0; 65 int i = 0;
66 GList *lines; 66 GList *lines;
67 int rows, scrcol; 67 int rows, scrcol;
68 gboolean has_scroll = !(view->flags & GNT_TEXT_VIEW_NO_SCROLL);
68 69
69 wbkgd(widget->window, COLOR_PAIR(GNT_COLOR_NORMAL)); 70 wbkgd(widget->window, COLOR_PAIR(GNT_COLOR_NORMAL));
70 werase(widget->window); 71 werase(widget->window);
71 72
72 for (i = 0, lines = view->list; i < widget->priv.height && lines; i++, lines = lines->next) 73 for (i = 0, lines = view->list; i < widget->priv.height && lines; i++, lines = lines->next)
109 wprintw(widget->window, "%s", (view->string->str + seg->start)); 110 wprintw(widget->window, "%s", (view->string->str + seg->start));
110 } 111 }
111 *end = back; 112 *end = back;
112 } 113 }
113 wattroff(widget->window, A_UNDERLINE | A_BLINK | A_REVERSE); 114 wattroff(widget->window, A_UNDERLINE | A_BLINK | A_REVERSE);
114 whline(widget->window, ' ', widget->priv.width - line->length - 1); 115 whline(widget->window, ' ', widget->priv.width - line->length - has_scroll);
115 } 116 }
116 117
117 scrcol = widget->priv.width - 1; 118 scrcol = widget->priv.width - 1;
118 rows = widget->priv.height - 2; 119 rows = widget->priv.height - 2;
119 if (rows > 0) 120 if (has_scroll && rows > 0)
120 { 121 {
121 int total = g_list_length(g_list_first(view->list)); 122 int total = g_list_length(g_list_first(view->list));
122 int showing, position, up, down; 123 int showing, position, up, down;
123 124
124 showing = rows * rows / total + 1; 125 showing = rows * rows / total + 1;
141 142
142 mvwvline(widget->window, position + 1, scrcol, 143 mvwvline(widget->window, position + 1, scrcol,
143 ACS_CKBOARD | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D), showing); 144 ACS_CKBOARD | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D), showing);
144 } 145 }
145 146
146 mvwaddch(widget->window, 0, scrcol, 147 if (has_scroll) {
147 (lines ? ACS_UARROW : ' ') | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); 148 mvwaddch(widget->window, 0, scrcol,
148 mvwaddch(widget->window, widget->priv.height - 1, scrcol, 149 (lines ? ACS_UARROW : ' ') | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D));
149 ((view->list && view->list->prev) ? ACS_DARROW : ' ') | 150 mvwaddch(widget->window, widget->priv.height - 1, scrcol,
150 COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); 151 ((view->list && view->list->prev) ? ACS_DARROW : ' ') |
152 COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D));
153 }
151 154
152 GNTDEBUG; 155 GNTDEBUG;
153 } 156 }
154 157
155 static void 158 static void
481 int fl = 0; 484 int fl = 0;
482 const char *start, *end; 485 const char *start, *end;
483 GList *list = view->list; 486 GList *list = view->list;
484 GntTextLine *line; 487 GntTextLine *line;
485 int len; 488 int len;
489 gboolean has_scroll = !(view->flags & GNT_TEXT_VIEW_NO_SCROLL);
490 gboolean wrap_word = !(view->flags & GNT_TEXT_VIEW_WRAP_CHAR);
486 491
487 if (text == NULL || *text == '\0') 492 if (text == NULL || *text == '\0')
488 return; 493 return;
489 494
490 fl = gnt_text_format_flag_to_chtype(flags); 495 fl = gnt_text_format_flag_to_chtype(flags);
517 view->list = g_list_first(view->list); 522 view->list = g_list_first(view->list);
518 continue; 523 continue;
519 } 524 }
520 525
521 line = view->list->data; 526 line = view->list->data;
522 if (line->length == widget->priv.width - 1) { 527 if (line->length == widget->priv.width - has_scroll) {
523 /* The last added line was exactly the same width as the widget */ 528 /* The last added line was exactly the same width as the widget */
524 line = g_new0(GntTextLine, 1); 529 line = g_new0(GntTextLine, 1);
525 line->soft = TRUE; 530 line->soft = TRUE;
526 view->list = g_list_prepend(view->list, line); 531 view->list = g_list_prepend(view->list, line);
527 } 532 }
528 533
529 if ((end = strchr(start, '\r')) != NULL || 534 if ((end = strchr(start, '\r')) != NULL ||
530 (end = strchr(start, '\n')) != NULL) { 535 (end = strchr(start, '\n')) != NULL) {
531 len = gnt_util_onscreen_width(start, end - 1); 536 len = gnt_util_onscreen_width(start, end - has_scroll);
532 if (len >= widget->priv.width - line->length - 1) { 537 if (len >= widget->priv.width - line->length - has_scroll) {
533 end = NULL; 538 end = NULL;
534 } 539 }
535 } 540 }
536 541
537 if (end == NULL) 542 if (end == NULL)
538 end = gnt_util_onscreen_width_to_pointer(start, 543 end = gnt_util_onscreen_width_to_pointer(start,
539 widget->priv.width - line->length - 1, &len); 544 widget->priv.width - line->length - has_scroll, &len);
540 545
541 /* Try to append to the previous segment if possible */ 546 /* Try to append to the previous segment if possible */
542 if (line->segments) { 547 if (line->segments) {
543 seg = g_list_last(line->segments)->data; 548 seg = g_list_last(line->segments)->data;
544 if (seg->flags != fl) 549 if (seg->flags != fl)
552 seg->flags = fl; 557 seg->flags = fl;
553 line->segments = g_list_append(line->segments, seg); 558 line->segments = g_list_append(line->segments, seg);
554 } 559 }
555 560
556 oldl = line; 561 oldl = line;
557 if (*end && *end != '\n' && *end != '\r') { 562 if (wrap_word && *end && *end != '\n' && *end != '\r') {
558 const char *tmp = end; 563 const char *tmp = end;
559 while (end && *end != '\n' && *end != '\r' && !g_ascii_isspace(*end)) { 564 while (end && *end != '\n' && *end != '\r' && !g_ascii_isspace(*end)) {
560 end = g_utf8_find_prev_char(seg->start + view->string->str, end); 565 end = g_utf8_find_prev_char(seg->start + view->string->str, end);
561 } 566 }
562 if (!end || !g_ascii_isspace(*end)) 567 if (!end || !g_ascii_isspace(*end))
781 void gnt_text_view_attach_scroll_widget(GntTextView *view, GntWidget *widget) 786 void gnt_text_view_attach_scroll_widget(GntTextView *view, GntWidget *widget)
782 { 787 {
783 g_signal_connect(G_OBJECT(widget), "key_pressed", G_CALLBACK(scroll_tv), view); 788 g_signal_connect(G_OBJECT(widget), "key_pressed", G_CALLBACK(scroll_tv), view);
784 } 789 }
785 790
791 void gnt_text_view_set_flag(GntTextView *view, GntTextViewFlag flag)
792 {
793 view->flags |= flag;
794 }
795