finch/libgnt/gnttextview.c

changeset 15878
f52832b611fe
parent 15874
46df7421a1b5
child 15886
13ed89c4f01d
child 15906
d05fbb788178
equal deleted inserted replaced
15877:ca1fabffc300 15878:f52832b611fe
1 #include "gnttextview.h"
2 #include "gntutils.h"
3
4 #include <string.h>
5
6 enum
7 {
8 SIGS = 1,
9 };
10
11 typedef struct
12 {
13 GntTextFormatFlags tvflag;
14 chtype flags;
15 int start;
16 int end; /* This is the next byte of the last character of this segment */
17 } GntTextSegment;
18
19 typedef struct
20 {
21 GList *segments; /* A list of GntTextSegments */
22 int length; /* The current length of the line so far (ie. onscreen width) */
23 gboolean soft; /* TRUE if it's an overflow from prev. line */
24 } GntTextLine;
25
26 typedef struct
27 {
28 char *name;
29 int start;
30 int end;
31 } GntTextTag;
32
33 static GntWidgetClass *parent_class = NULL;
34
35 static gchar *select_start;
36 static gchar *select_end;
37 static gboolean double_click;
38
39 static void
40 gnt_text_view_draw(GntWidget *widget)
41 {
42 GntTextView *view = GNT_TEXT_VIEW(widget);
43 int i = 0;
44 GList *lines;
45 int rows, scrcol;
46
47 werase(widget->window);
48
49 for (i = 0, lines = view->list; i < widget->priv.height && lines; i++, lines = lines->next)
50 {
51 GList *iter;
52 GntTextLine *line = lines->data;
53
54 wmove(widget->window, widget->priv.height - 1 - i, 0);
55
56 for (iter = line->segments; iter; iter = iter->next)
57 {
58 GntTextSegment *seg = iter->data;
59 char *end = view->string->str + seg->end;
60 char back = *end;
61 chtype fl = seg->flags;
62 *end = '\0';
63 if (select_start < view->string->str + seg->start && select_end > view->string->str + seg->end) {
64 fl |= A_REVERSE;
65 wattrset(widget->window, fl);
66 wprintw(widget->window, "%s", (view->string->str + seg->start));
67 } else if (select_start && select_end &&
68 ((select_start >= view->string->str + seg->start && select_start <= view->string->str + seg->end) ||
69 (select_end <= view->string->str + seg->end && select_start <= view->string->str + seg->start))) {
70 char *cur = view->string->str + seg->start;
71 while (*cur != '\0') {
72 gchar *last = g_utf8_next_char(cur);
73 gchar *str;
74 if (cur >= select_start && cur <= select_end)
75 fl |= A_REVERSE;
76 else
77 fl = seg->flags;
78 str = g_strndup(cur, last - cur);
79 wattrset(widget->window, fl);
80 waddstr(widget->window, str);
81 g_free(str);
82 cur = g_utf8_next_char(cur);
83 }
84 } else {
85 wattrset(widget->window, fl);
86 wprintw(widget->window, "%s", (view->string->str + seg->start));
87 }
88 *end = back;
89 }
90 wattroff(widget->window, A_UNDERLINE | A_BLINK | A_REVERSE);
91 whline(widget->window, ' ', widget->priv.width - line->length - 1);
92 }
93
94 scrcol = widget->priv.width - 1;
95 rows = widget->priv.height - 2;
96 if (rows > 0)
97 {
98 int total = g_list_length(g_list_first(view->list));
99 int showing, position, up, down;
100
101 showing = rows * rows / total + 1;
102 showing = MIN(rows, showing);
103
104 total -= rows;
105 up = g_list_length(lines);
106 down = total - up;
107
108 position = (rows - showing) * up / MAX(1, up + down);
109 position = MAX((lines != NULL), position);
110
111 if (showing + position > rows)
112 position = rows - showing;
113
114 if (showing + position == rows && view->list && view->list->prev)
115 position = MAX(1, rows - 1 - showing);
116 else if (showing + position < rows && view->list && !view->list->prev)
117 position = rows - showing;
118
119 mvwvline(widget->window, position + 1, scrcol,
120 ACS_CKBOARD | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D), showing);
121 }
122
123 mvwaddch(widget->window, 0, scrcol,
124 (lines ? ACS_UARROW : ' ') | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D));
125 mvwaddch(widget->window, widget->priv.height - 1, scrcol,
126 ((view->list && view->list->prev) ? ACS_DARROW : ' ') |
127 COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D));
128
129 GNTDEBUG;
130 }
131
132 static void
133 gnt_text_view_size_request(GntWidget *widget)
134 {
135 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_MAPPED))
136 {
137 gnt_widget_set_size(widget, 64, 20);
138 }
139 }
140
141 static void
142 gnt_text_view_map(GntWidget *widget)
143 {
144 if (widget->priv.width == 0 || widget->priv.height == 0)
145 gnt_widget_size_request(widget);
146 GNTDEBUG;
147 }
148
149 static gboolean
150 gnt_text_view_key_pressed(GntWidget *widget, const char *text)
151 {
152 return FALSE;
153 }
154
155 static void
156 free_text_segment(gpointer data, gpointer null)
157 {
158 GntTextSegment *seg = data;
159 g_free(seg);
160 }
161
162 static void
163 free_text_line(gpointer data, gpointer null)
164 {
165 GntTextLine *line = data;
166 g_list_foreach(line->segments, free_text_segment, NULL);
167 g_list_free(line->segments);
168 g_free(line);
169 }
170
171 static void
172 free_tag(gpointer data, gpointer null)
173 {
174 GntTextTag *tag = data;
175 g_free(tag->name);
176 g_free(tag);
177 }
178
179 static void
180 gnt_text_view_destroy(GntWidget *widget)
181 {
182 GntTextView *view = GNT_TEXT_VIEW(widget);
183 view->list = g_list_first(view->list);
184 g_list_foreach(view->list, free_text_line, NULL);
185 g_list_free(view->list);
186 g_list_foreach(view->tags, free_tag, NULL);
187 g_list_free(view->tags);
188 g_string_free(view->string, TRUE);
189 }
190
191 static char *
192 gnt_text_view_get_p(GntTextView *view, int x, int y)
193 {
194 int i = 0;
195 GntWidget *wid = GNT_WIDGET(view);
196 GntTextLine *line;
197 GList *lines;
198 GList *segs;
199 GntTextSegment *seg;
200 gchar *pos;
201
202 y = wid->priv.height - y;
203 if (g_list_length(view->list) < y) {
204 x = 0;
205 y = g_list_length(view->list) - 1;
206 }
207
208 lines = g_list_nth(view->list, y - 1);
209 if (!lines)
210 return NULL;
211 do {
212 line = lines->data;
213 lines = lines->next;
214 } while (line && !line->segments && lines);
215
216 if (!line || !line->segments) /* no valid line */
217 return NULL;
218 segs = line->segments;
219 seg = (GntTextSegment *)segs->data;
220 pos = view->string->str + seg->start;
221 x = MIN(x, line->length);
222 while (++i <= x) {
223 gunichar *u;
224 pos = g_utf8_next_char(pos);
225 u = g_utf8_to_ucs4(pos, -1, NULL, NULL, NULL);
226 if (u && g_unichar_iswide(*u))
227 i++;
228 g_free(u);
229 }
230 return pos;
231 }
232
233 static GString *
234 select_word_text(GntTextView *view, gchar *c)
235 {
236 gchar *start = c;
237 gchar *end = c;
238 gchar *t, *endsize;
239 while ((t = g_utf8_prev_char(start))) {
240 if (!g_ascii_isspace(*t)) {
241 if (start == view->string->str)
242 break;
243 start = t;
244 } else
245 break;
246 }
247 while ((t = g_utf8_next_char(end))) {
248 if (!g_ascii_isspace(*t))
249 end = t;
250 else
251 break;
252 }
253 select_start = start;
254 select_end = end;
255 endsize = g_utf8_next_char(select_end); /* End at the correct byte */
256 return g_string_new_len(start, endsize - start);
257 }
258
259 static gboolean too_slow(gpointer n)
260 {
261 double_click = FALSE;
262 return FALSE;
263 }
264
265 static gboolean
266 gnt_text_view_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
267 {
268 if (event == GNT_MOUSE_SCROLL_UP) {
269 gnt_text_view_scroll(GNT_TEXT_VIEW(widget), -1);
270 } else if (event == GNT_MOUSE_SCROLL_DOWN) {
271 gnt_text_view_scroll(GNT_TEXT_VIEW(widget), 1);
272 } else if (event == GNT_LEFT_MOUSE_DOWN) {
273 select_start = gnt_text_view_get_p(GNT_TEXT_VIEW(widget), x - widget->priv.x, y - widget->priv.y);
274 g_timeout_add(500, too_slow, NULL);
275 } else if (event == GNT_MOUSE_UP) {
276 if (select_start) {
277 GString *clip;
278 select_end = gnt_text_view_get_p(GNT_TEXT_VIEW(widget), x - widget->priv.x, y - widget->priv.y);
279 if (select_end < select_start) {
280 gchar *t = select_start;
281 select_start = select_end;
282 select_end = t;
283 }
284 if (select_start == select_end) {
285 if (double_click) {
286 clip = select_word_text(GNT_TEXT_VIEW(widget), select_start);
287 double_click = FALSE;
288 } else {
289 double_click = TRUE;
290 select_start = 0;
291 select_end = 0;
292 gnt_widget_draw(widget);
293 return TRUE;
294 }
295 } else {
296 gchar *endsize = g_utf8_next_char(select_end); /* End at the correct byte */
297 clip = g_string_new_len(select_start, endsize - select_start);
298 }
299 gnt_widget_draw(widget);
300 gnt_set_clipboard_string(clip->str);
301 g_string_free(clip, TRUE);
302 }
303 } else
304 return FALSE;
305 return TRUE;
306 }
307
308 static void
309 gnt_text_view_reflow(GntTextView *view)
310 {
311 /* This is pretty ugly, and inefficient. Someone do something about it. */
312 GntTextLine *line;
313 GList *back, *iter, *list;
314 GString *string;
315 int pos = 0; /* no. of 'real' lines */
316
317 list = view->list;
318 while (list->prev) {
319 line = list->data;
320 if (!line->soft)
321 pos++;
322 list = list->prev;
323 }
324
325 back = g_list_last(view->list);
326 view->list = NULL;
327
328 string = view->string;
329 view->string = NULL;
330 gnt_text_view_clear(view);
331
332 view->string = g_string_set_size(view->string, string->len);
333 view->string->len = 0;
334 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(view), GNT_WIDGET_DRAWING);
335
336 for (; back; back = back->prev) {
337 line = back->data;
338 if (back->next && !line->soft) {
339 gnt_text_view_append_text_with_flags(view, "\n", GNT_TEXT_FLAG_NORMAL);
340 }
341
342 for (iter = line->segments; iter; iter = iter->next) {
343 GntTextSegment *seg = iter->data;
344 char *start = string->str + seg->start;
345 char *end = string->str + seg->end;
346 char back = *end;
347 *end = '\0';
348 gnt_text_view_append_text_with_flags(view, start, seg->tvflag);
349 *end = back;
350 }
351 free_text_line(line, NULL);
352 }
353 g_list_free(list);
354
355 list = view->list = g_list_first(view->list);
356 /* Go back to the line that was in view before resizing started */
357 while (pos--) {
358 while (((GntTextLine*)list->data)->soft)
359 list = list->next;
360 list = list->next;
361 }
362 view->list = list;
363 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(view), GNT_WIDGET_DRAWING);
364 if (GNT_WIDGET(view)->window)
365 gnt_widget_draw(GNT_WIDGET(view));
366 g_string_free(string, TRUE);
367 }
368
369 static void
370 gnt_text_view_size_changed(GntWidget *widget, int w, int h)
371 {
372 if (w != widget->priv.width) {
373 gnt_text_view_reflow(GNT_TEXT_VIEW(widget));
374 }
375 }
376
377 static void
378 gnt_text_view_class_init(GntTextViewClass *klass)
379 {
380 parent_class = GNT_WIDGET_CLASS(klass);
381 parent_class->destroy = gnt_text_view_destroy;
382 parent_class->draw = gnt_text_view_draw;
383 parent_class->map = gnt_text_view_map;
384 parent_class->size_request = gnt_text_view_size_request;
385 parent_class->key_pressed = gnt_text_view_key_pressed;
386 parent_class->clicked = gnt_text_view_clicked;
387 parent_class->size_changed = gnt_text_view_size_changed;
388
389 GNTDEBUG;
390 }
391
392 static void
393 gnt_text_view_init(GTypeInstance *instance, gpointer class)
394 {
395 GntWidget *widget = GNT_WIDGET(instance);
396
397 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(instance), GNT_WIDGET_GROW_Y | GNT_WIDGET_GROW_X);
398
399 widget->priv.minw = 5;
400 widget->priv.minh = 2;
401 GNTDEBUG;
402 }
403
404 /******************************************************************************
405 * GntTextView API
406 *****************************************************************************/
407 GType
408 gnt_text_view_get_gtype(void)
409 {
410 static GType type = 0;
411
412 if(type == 0)
413 {
414 static const GTypeInfo info = {
415 sizeof(GntTextViewClass),
416 NULL, /* base_init */
417 NULL, /* base_finalize */
418 (GClassInitFunc)gnt_text_view_class_init,
419 NULL, /* class_finalize */
420 NULL, /* class_data */
421 sizeof(GntTextView),
422 0, /* n_preallocs */
423 gnt_text_view_init, /* instance_init */
424 NULL /* value_table */
425 };
426
427 type = g_type_register_static(GNT_TYPE_WIDGET,
428 "GntTextView",
429 &info, 0);
430 }
431
432 return type;
433 }
434
435 GntWidget *gnt_text_view_new()
436 {
437 GntWidget *widget = g_object_new(GNT_TYPE_TEXTVIEW, NULL);
438 GntTextView *view = GNT_TEXT_VIEW(widget);
439 GntTextLine *line = g_new0(GntTextLine, 1);
440
441 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
442
443 view->string = g_string_new(NULL);
444 view->list = g_list_append(view->list, line);
445
446 return widget;
447 }
448
449 void gnt_text_view_append_text_with_flags(GntTextView *view, const char *text, GntTextFormatFlags flags)
450 {
451 gnt_text_view_append_text_with_tag(view, text, flags, NULL);
452 }
453
454 void gnt_text_view_append_text_with_tag(GntTextView *view, const char *text,
455 GntTextFormatFlags flags, const char *tagname)
456 {
457 GntWidget *widget = GNT_WIDGET(view);
458 int fl = 0;
459 const char *start, *end;
460 GList *list = view->list;
461 GntTextLine *line;
462 int len;
463
464 if (text == NULL || *text == '\0')
465 return;
466
467 fl = gnt_text_format_flag_to_chtype(flags);
468
469 len = view->string->len;
470 view->string = g_string_append(view->string, text);
471
472 if (tagname) {
473 GntTextTag *tag = g_new0(GntTextTag, 1);
474 tag->name = g_strdup(tagname);
475 tag->start = len;
476 tag->end = view->string->len;
477 view->tags = g_list_append(view->tags, tag);
478 }
479
480 view->list = g_list_first(view->list);
481
482 start = end = view->string->str + len;
483
484 while (*start) {
485 GntTextSegment *seg = NULL;
486
487 if (*end == '\n' || *end == '\r') {
488 end++;
489 start = end;
490 gnt_text_view_next_line(view);
491 view->list = g_list_first(view->list);
492 continue;
493 }
494
495 line = view->list->data;
496 if (line->length == widget->priv.width - 1) {
497 /* The last added line was exactly the same width as the widget */
498 line = g_new0(GntTextLine, 1);
499 line->soft = TRUE;
500 view->list = g_list_prepend(view->list, line);
501 }
502
503 if ((end = strchr(start, '\n')) != NULL ||
504 (end = strchr(start, '\r')) != NULL) {
505 len = gnt_util_onscreen_width(start, end - 1);
506 if (len >= widget->priv.width - line->length - 1) {
507 end = NULL;
508 }
509 }
510
511 if (end == NULL)
512 end = gnt_util_onscreen_width_to_pointer(start,
513 widget->priv.width - line->length - 1, &len);
514
515 /* Try to append to the previous segment if possible */
516 if (line->segments) {
517 seg = g_list_last(line->segments)->data;
518 if (seg->flags != fl)
519 seg = NULL;
520 }
521
522 if (seg == NULL) {
523 seg = g_new0(GntTextSegment, 1);
524 seg->start = start - view->string->str;
525 seg->tvflag = flags;
526 seg->flags = fl;
527 line->segments = g_list_append(line->segments, seg);
528 }
529 seg->end = end - view->string->str;
530 line->length += len;
531
532 start = end;
533 if (*end && *end != '\n' && *end != '\r') {
534 line = g_new0(GntTextLine, 1);
535 line->soft = TRUE;
536 view->list = g_list_prepend(view->list, line);
537 }
538 }
539
540 view->list = list;
541
542 gnt_widget_draw(widget);
543 }
544
545 void gnt_text_view_scroll(GntTextView *view, int scroll)
546 {
547 if (scroll == 0)
548 {
549 view->list = g_list_first(view->list);
550 }
551 else if (scroll > 0)
552 {
553 GList *list = g_list_nth_prev(view->list, scroll);
554 if (list == NULL)
555 list = g_list_first(view->list);
556 view->list = list;
557 }
558 else if (scroll < 0)
559 {
560 GList *list = g_list_nth(view->list, -scroll);
561 if (list == NULL)
562 list = g_list_last(view->list);
563 view->list = list;
564 }
565
566 gnt_widget_draw(GNT_WIDGET(view));
567 }
568
569 void gnt_text_view_next_line(GntTextView *view)
570 {
571 GntTextLine *line = g_new0(GntTextLine, 1);
572 GList *list = view->list;
573
574 view->list = g_list_prepend(g_list_first(view->list), line);
575 view->list = list;
576 gnt_widget_draw(GNT_WIDGET(view));
577 }
578
579 chtype gnt_text_format_flag_to_chtype(GntTextFormatFlags flags)
580 {
581 chtype fl = 0;
582
583 if (flags & GNT_TEXT_FLAG_BOLD)
584 fl |= A_BOLD;
585 if (flags & GNT_TEXT_FLAG_UNDERLINE)
586 fl |= A_UNDERLINE;
587 if (flags & GNT_TEXT_FLAG_BLINK)
588 fl |= A_BLINK;
589
590 if (flags & GNT_TEXT_FLAG_DIM)
591 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_DISABLED));
592 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT)
593 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_HIGHLIGHT));
594 else
595 fl |= COLOR_PAIR(GNT_COLOR_NORMAL);
596
597 return fl;
598 }
599
600 void gnt_text_view_clear(GntTextView *view)
601 {
602 GntTextLine *line;
603
604 g_list_foreach(view->list, free_text_line, NULL);
605 g_list_free(view->list);
606 view->list = NULL;
607
608 line = g_new0(GntTextLine, 1);
609 view->list = g_list_append(view->list, line);
610 if (view->string)
611 g_string_free(view->string, TRUE);
612 view->string = g_string_new(NULL);
613
614 if (GNT_WIDGET(view)->window)
615 gnt_widget_draw(GNT_WIDGET(view));
616 }
617
618 int gnt_text_view_get_lines_below(GntTextView *view)
619 {
620 int below = 0;
621 GList *list = view->list;
622 while ((list = list->prev))
623 ++below;
624 return below;
625 }
626
627 int gnt_text_view_get_lines_above(GntTextView *view)
628 {
629 int above = 0;
630 GList *list = view->list;
631 list = g_list_nth(view->list, GNT_WIDGET(view)->priv.height);
632 if (!list)
633 return 0;
634 while ((list = list->next))
635 ++above;
636 return above;
637 }
638
639 /**
640 * XXX: There are quite possibly more than a few bugs here.
641 */
642 int gnt_text_view_tag_change(GntTextView *view, const char *name, const char *text, gboolean all)
643 {
644 GList *alllines = g_list_first(view->list);
645 GList *list, *next, *iter, *inext;
646 const int text_length = text ? strlen(text) : 0;
647 int count = 0;
648 for (list = view->tags; list; list = next) {
649 GntTextTag *tag = list->data;
650 next = list->next;
651 if (strcmp(tag->name, name) == 0) {
652 int change;
653 char *before, *after;
654
655 count++;
656
657 before = g_strndup(view->string->str, tag->start);
658 after = g_strdup(view->string->str + tag->end);
659 change = (tag->end - tag->start) - text_length;
660
661 g_string_printf(view->string, "%s%s%s", before, text ? text : "", after);
662 g_free(before);
663 g_free(after);
664
665 /* Update the offsets of the next tags */
666 for (iter = next; iter; iter = iter->next) {
667 GntTextTag *t = iter->data;
668 t->start -= change;
669 t->end -= change;
670 }
671
672 /* Update the offsets of the segments */
673 for (iter = alllines; iter; iter = inext) {
674 GList *segs, *snext;
675 GntTextLine *line = iter->data;
676 inext = iter->next;
677 for (segs = line->segments; segs; segs = snext) {
678 GntTextSegment *seg = segs->data;
679 snext = segs->next;
680 if (seg->start >= tag->end) {
681 /* The segment is somewhere after the tag */
682 seg->start -= change;
683 seg->end -= change;
684 } else if (seg->end <= tag->start) {
685 /* This segment is somewhere in front of the tag */
686 } else if (seg->start >= tag->start) {
687 /* This segment starts in the middle of the tag */
688 if (text == NULL) {
689 free_text_segment(seg, NULL);
690 line->segments = g_list_delete_link(line->segments, segs);
691 if (line->segments == NULL) {
692 free_text_line(line, NULL);
693 if (view->list == iter) {
694 if (inext)
695 view->list = inext;
696 else
697 view->list = iter->prev;
698 }
699 alllines = g_list_delete_link(alllines, iter);
700 }
701 } else {
702 /* XXX: (null) */
703 seg->start = tag->start;
704 seg->end = tag->end - change;
705 }
706 line->length -= change;
707 /* XXX: Make things work if the tagged text spans over several lines. */
708 } else {
709 /* XXX: handle the rest of the conditions */
710 g_printerr("WTF! This needs to be handled properly!!\n");
711 }
712 }
713 }
714 if (text == NULL) {
715 /* Remove the tag */
716 view->tags = g_list_delete_link(view->tags, list);
717 free_tag(tag, NULL);
718 } else {
719 tag->end -= change;
720 }
721 if (!all)
722 break;
723 }
724 }
725 return count;
726 }
727

mercurial