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