finch/libgnt/gnttextview.c

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

mercurial