Mercurial > pidgin.yaz
annotate finch/libgnt/gnttextview.c @ 18680:fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Sat, 28 Jul 2007 11:25:07 +0000 |
parents | 1148da95ddeb |
children | c3d0b51d6c60 |
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 |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
23 #include "gntstyle.h" |
15818 | 24 #include "gnttextview.h" |
25 #include "gntutils.h" | |
26 | |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
27 #include <stdlib.h> |
15818 | 28 #include <string.h> |
18526
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
29 #include <unistd.h> |
15818 | 30 |
31 enum | |
32 { | |
33 SIGS = 1, | |
34 }; | |
35 | |
36 typedef struct | |
37 { | |
38 GntTextFormatFlags tvflag; | |
39 chtype flags; | |
40 int start; | |
41 int end; /* This is the next byte of the last character of this segment */ | |
42 } GntTextSegment; | |
43 | |
44 typedef struct | |
45 { | |
46 GList *segments; /* A list of GntTextSegments */ | |
47 int length; /* The current length of the line so far (ie. onscreen width) */ | |
48 gboolean soft; /* TRUE if it's an overflow from prev. line */ | |
49 } GntTextLine; | |
50 | |
51 typedef struct | |
52 { | |
53 char *name; | |
54 int start; | |
55 int end; | |
56 } GntTextTag; | |
57 | |
58 static GntWidgetClass *parent_class = NULL; | |
59 | |
60 static gchar *select_start; | |
61 static gchar *select_end; | |
62 static gboolean double_click; | |
63 | |
64 static void | |
65 gnt_text_view_draw(GntWidget *widget) | |
66 { | |
67 GntTextView *view = GNT_TEXT_VIEW(widget); | |
68 int i = 0; | |
69 GList *lines; | |
70 int rows, scrcol; | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
71 gboolean has_scroll = !(view->flags & GNT_TEXT_VIEW_NO_SCROLL); |
15818 | 72 |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
73 wbkgd(widget->window, COLOR_PAIR(GNT_COLOR_NORMAL)); |
15818 | 74 werase(widget->window); |
75 | |
76 for (i = 0, lines = view->list; i < widget->priv.height && lines; i++, lines = lines->next) | |
77 { | |
78 GList *iter; | |
79 GntTextLine *line = lines->data; | |
80 | |
81 wmove(widget->window, widget->priv.height - 1 - i, 0); | |
82 | |
83 for (iter = line->segments; iter; iter = iter->next) | |
84 { | |
85 GntTextSegment *seg = iter->data; | |
86 char *end = view->string->str + seg->end; | |
87 char back = *end; | |
88 chtype fl = seg->flags; | |
89 *end = '\0'; | |
90 if (select_start < view->string->str + seg->start && select_end > view->string->str + seg->end) { | |
91 fl |= A_REVERSE; | |
92 wattrset(widget->window, fl); | |
93 wprintw(widget->window, "%s", (view->string->str + seg->start)); | |
94 } else if (select_start && select_end && | |
95 ((select_start >= view->string->str + seg->start && select_start <= view->string->str + seg->end) || | |
96 (select_end <= view->string->str + seg->end && select_start <= view->string->str + seg->start))) { | |
97 char *cur = view->string->str + seg->start; | |
98 while (*cur != '\0') { | |
99 gchar *last = g_utf8_next_char(cur); | |
100 gchar *str; | |
101 if (cur >= select_start && cur <= select_end) | |
102 fl |= A_REVERSE; | |
103 else | |
104 fl = seg->flags; | |
105 str = g_strndup(cur, last - cur); | |
106 wattrset(widget->window, fl); | |
107 waddstr(widget->window, str); | |
108 g_free(str); | |
109 cur = g_utf8_next_char(cur); | |
110 } | |
111 } else { | |
112 wattrset(widget->window, fl); | |
113 wprintw(widget->window, "%s", (view->string->str + seg->start)); | |
114 } | |
115 *end = back; | |
116 } | |
117 wattroff(widget->window, A_UNDERLINE | A_BLINK | A_REVERSE); | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
118 whline(widget->window, ' ', widget->priv.width - line->length - has_scroll); |
15818 | 119 } |
120 | |
121 scrcol = widget->priv.width - 1; | |
122 rows = widget->priv.height - 2; | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
123 if (has_scroll && rows > 0) |
15818 | 124 { |
125 int total = g_list_length(g_list_first(view->list)); | |
126 int showing, position, up, down; | |
127 | |
128 showing = rows * rows / total + 1; | |
129 showing = MIN(rows, showing); | |
130 | |
131 total -= rows; | |
132 up = g_list_length(lines); | |
133 down = total - up; | |
134 | |
135 position = (rows - showing) * up / MAX(1, up + down); | |
136 position = MAX((lines != NULL), position); | |
137 | |
138 if (showing + position > rows) | |
139 position = rows - showing; | |
140 | |
141 if (showing + position == rows && view->list && view->list->prev) | |
142 position = MAX(1, rows - 1 - showing); | |
143 else if (showing + position < rows && view->list && !view->list->prev) | |
144 position = rows - showing; | |
145 | |
146 mvwvline(widget->window, position + 1, scrcol, | |
147 ACS_CKBOARD | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D), showing); | |
148 } | |
149 | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
150 if (has_scroll) { |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
151 mvwaddch(widget->window, 0, scrcol, |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
152 (lines ? ACS_UARROW : ' ') | COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
153 mvwaddch(widget->window, widget->priv.height - 1, scrcol, |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
154 ((view->list && view->list->prev) ? ACS_DARROW : ' ') | |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
155 COLOR_PAIR(GNT_COLOR_HIGHLIGHT_D)); |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
156 } |
15818 | 157 |
158 GNTDEBUG; | |
159 } | |
160 | |
161 static void | |
162 gnt_text_view_size_request(GntWidget *widget) | |
163 { | |
164 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_MAPPED)) | |
165 { | |
166 gnt_widget_set_size(widget, 64, 20); | |
167 } | |
168 } | |
169 | |
170 static void | |
171 gnt_text_view_map(GntWidget *widget) | |
172 { | |
173 if (widget->priv.width == 0 || widget->priv.height == 0) | |
174 gnt_widget_size_request(widget); | |
175 GNTDEBUG; | |
176 } | |
177 | |
178 static gboolean | |
179 gnt_text_view_key_pressed(GntWidget *widget, const char *text) | |
180 { | |
181 return FALSE; | |
182 } | |
183 | |
184 static void | |
185 free_text_segment(gpointer data, gpointer null) | |
186 { | |
187 GntTextSegment *seg = data; | |
188 g_free(seg); | |
189 } | |
190 | |
191 static void | |
192 free_text_line(gpointer data, gpointer null) | |
193 { | |
194 GntTextLine *line = data; | |
195 g_list_foreach(line->segments, free_text_segment, NULL); | |
196 g_list_free(line->segments); | |
197 g_free(line); | |
198 } | |
199 | |
200 static void | |
201 free_tag(gpointer data, gpointer null) | |
202 { | |
203 GntTextTag *tag = data; | |
204 g_free(tag->name); | |
205 g_free(tag); | |
206 } | |
207 | |
208 static void | |
209 gnt_text_view_destroy(GntWidget *widget) | |
210 { | |
211 GntTextView *view = GNT_TEXT_VIEW(widget); | |
212 view->list = g_list_first(view->list); | |
213 g_list_foreach(view->list, free_text_line, NULL); | |
214 g_list_free(view->list); | |
215 g_list_foreach(view->tags, free_tag, NULL); | |
216 g_list_free(view->tags); | |
217 g_string_free(view->string, TRUE); | |
218 } | |
219 | |
220 static char * | |
221 gnt_text_view_get_p(GntTextView *view, int x, int y) | |
222 { | |
223 int i = 0; | |
224 GntWidget *wid = GNT_WIDGET(view); | |
225 GntTextLine *line; | |
226 GList *lines; | |
227 GList *segs; | |
228 GntTextSegment *seg; | |
229 gchar *pos; | |
230 | |
231 y = wid->priv.height - y; | |
232 if (g_list_length(view->list) < y) { | |
233 x = 0; | |
234 y = g_list_length(view->list) - 1; | |
235 } | |
236 | |
237 lines = g_list_nth(view->list, y - 1); | |
238 if (!lines) | |
239 return NULL; | |
240 do { | |
241 line = lines->data; | |
242 lines = lines->next; | |
243 } while (line && !line->segments && lines); | |
244 | |
245 if (!line || !line->segments) /* no valid line */ | |
246 return NULL; | |
247 segs = line->segments; | |
248 seg = (GntTextSegment *)segs->data; | |
249 pos = view->string->str + seg->start; | |
250 x = MIN(x, line->length); | |
251 while (++i <= x) { | |
252 gunichar *u; | |
253 pos = g_utf8_next_char(pos); | |
254 u = g_utf8_to_ucs4(pos, -1, NULL, NULL, NULL); | |
255 if (u && g_unichar_iswide(*u)) | |
256 i++; | |
257 g_free(u); | |
258 } | |
259 return pos; | |
260 } | |
261 | |
262 static GString * | |
263 select_word_text(GntTextView *view, gchar *c) | |
264 { | |
265 gchar *start = c; | |
266 gchar *end = c; | |
267 gchar *t, *endsize; | |
268 while ((t = g_utf8_prev_char(start))) { | |
269 if (!g_ascii_isspace(*t)) { | |
270 if (start == view->string->str) | |
271 break; | |
272 start = t; | |
273 } else | |
274 break; | |
275 } | |
276 while ((t = g_utf8_next_char(end))) { | |
277 if (!g_ascii_isspace(*t)) | |
278 end = t; | |
279 else | |
280 break; | |
281 } | |
282 select_start = start; | |
283 select_end = end; | |
284 endsize = g_utf8_next_char(select_end); /* End at the correct byte */ | |
285 return g_string_new_len(start, endsize - start); | |
286 } | |
287 | |
288 static gboolean too_slow(gpointer n) | |
289 { | |
290 double_click = FALSE; | |
291 return FALSE; | |
292 } | |
293 | |
294 static gboolean | |
295 gnt_text_view_clicked(GntWidget *widget, GntMouseEvent event, int x, int y) | |
296 { | |
297 if (event == GNT_MOUSE_SCROLL_UP) { | |
298 gnt_text_view_scroll(GNT_TEXT_VIEW(widget), -1); | |
299 } else if (event == GNT_MOUSE_SCROLL_DOWN) { | |
300 gnt_text_view_scroll(GNT_TEXT_VIEW(widget), 1); | |
301 } else if (event == GNT_LEFT_MOUSE_DOWN) { | |
302 select_start = gnt_text_view_get_p(GNT_TEXT_VIEW(widget), x - widget->priv.x, y - widget->priv.y); | |
303 g_timeout_add(500, too_slow, NULL); | |
304 } else if (event == GNT_MOUSE_UP) { | |
305 if (select_start) { | |
306 GString *clip; | |
307 select_end = gnt_text_view_get_p(GNT_TEXT_VIEW(widget), x - widget->priv.x, y - widget->priv.y); | |
308 if (select_end < select_start) { | |
309 gchar *t = select_start; | |
310 select_start = select_end; | |
311 select_end = t; | |
312 } | |
313 if (select_start == select_end) { | |
314 if (double_click) { | |
315 clip = select_word_text(GNT_TEXT_VIEW(widget), select_start); | |
316 double_click = FALSE; | |
317 } else { | |
318 double_click = TRUE; | |
319 select_start = 0; | |
320 select_end = 0; | |
321 gnt_widget_draw(widget); | |
322 return TRUE; | |
323 } | |
324 } else { | |
325 gchar *endsize = g_utf8_next_char(select_end); /* End at the correct byte */ | |
326 clip = g_string_new_len(select_start, endsize - select_start); | |
327 } | |
328 gnt_widget_draw(widget); | |
329 gnt_set_clipboard_string(clip->str); | |
330 g_string_free(clip, TRUE); | |
331 } | |
332 } else | |
333 return FALSE; | |
334 return TRUE; | |
335 } | |
336 | |
337 static void | |
338 gnt_text_view_reflow(GntTextView *view) | |
339 { | |
340 /* This is pretty ugly, and inefficient. Someone do something about it. */ | |
341 GntTextLine *line; | |
342 GList *back, *iter, *list; | |
343 GString *string; | |
344 int pos = 0; /* no. of 'real' lines */ | |
345 | |
346 list = view->list; | |
347 while (list->prev) { | |
348 line = list->data; | |
349 if (!line->soft) | |
350 pos++; | |
351 list = list->prev; | |
352 } | |
353 | |
354 back = g_list_last(view->list); | |
355 view->list = NULL; | |
356 | |
357 string = view->string; | |
358 view->string = NULL; | |
359 gnt_text_view_clear(view); | |
360 | |
361 view->string = g_string_set_size(view->string, string->len); | |
362 view->string->len = 0; | |
363 GNT_WIDGET_SET_FLAGS(GNT_WIDGET(view), GNT_WIDGET_DRAWING); | |
364 | |
365 for (; back; back = back->prev) { | |
366 line = back->data; | |
367 if (back->next && !line->soft) { | |
368 gnt_text_view_append_text_with_flags(view, "\n", GNT_TEXT_FLAG_NORMAL); | |
369 } | |
370 | |
371 for (iter = line->segments; iter; iter = iter->next) { | |
372 GntTextSegment *seg = iter->data; | |
373 char *start = string->str + seg->start; | |
374 char *end = string->str + seg->end; | |
375 char back = *end; | |
376 *end = '\0'; | |
377 gnt_text_view_append_text_with_flags(view, start, seg->tvflag); | |
378 *end = back; | |
379 } | |
380 free_text_line(line, NULL); | |
381 } | |
382 g_list_free(list); | |
383 | |
384 list = view->list = g_list_first(view->list); | |
385 /* Go back to the line that was in view before resizing started */ | |
386 while (pos--) { | |
387 while (((GntTextLine*)list->data)->soft) | |
388 list = list->next; | |
389 list = list->next; | |
390 } | |
391 view->list = list; | |
392 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(view), GNT_WIDGET_DRAWING); | |
393 if (GNT_WIDGET(view)->window) | |
394 gnt_widget_draw(GNT_WIDGET(view)); | |
395 g_string_free(string, TRUE); | |
396 } | |
397 | |
398 static void | |
399 gnt_text_view_size_changed(GntWidget *widget, int w, int h) | |
400 { | |
18680
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
401 if (w != widget->priv.width && GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_MAPPED)) { |
15818 | 402 gnt_text_view_reflow(GNT_TEXT_VIEW(widget)); |
403 } | |
404 } | |
405 | |
406 static void | |
407 gnt_text_view_class_init(GntTextViewClass *klass) | |
408 { | |
409 parent_class = GNT_WIDGET_CLASS(klass); | |
410 parent_class->destroy = gnt_text_view_destroy; | |
411 parent_class->draw = gnt_text_view_draw; | |
412 parent_class->map = gnt_text_view_map; | |
413 parent_class->size_request = gnt_text_view_size_request; | |
414 parent_class->key_pressed = gnt_text_view_key_pressed; | |
415 parent_class->clicked = gnt_text_view_clicked; | |
416 parent_class->size_changed = gnt_text_view_size_changed; | |
417 | |
418 GNTDEBUG; | |
419 } | |
420 | |
421 static void | |
422 gnt_text_view_init(GTypeInstance *instance, gpointer class) | |
423 { | |
424 GntWidget *widget = GNT_WIDGET(instance); | |
18680
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
425 GntTextView *view = GNT_TEXT_VIEW(widget); |
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
426 GntTextLine *line = g_new0(GntTextLine, 1); |
15818 | 427 |
18680
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
428 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW | |
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
429 GNT_WIDGET_GROW_Y | GNT_WIDGET_GROW_X); |
15818 | 430 widget->priv.minw = 5; |
431 widget->priv.minh = 2; | |
18680
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
432 view->string = g_string_new(NULL); |
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
433 view->list = g_list_append(view->list, line); |
fd01bac27b79
Initialize the gobject in a more appropriate place. And fix an error message.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18539
diff
changeset
|
434 |
15818 | 435 GNTDEBUG; |
436 } | |
437 | |
438 /****************************************************************************** | |
439 * GntTextView API | |
440 *****************************************************************************/ | |
441 GType | |
442 gnt_text_view_get_gtype(void) | |
443 { | |
444 static GType type = 0; | |
445 | |
446 if(type == 0) | |
447 { | |
448 static const GTypeInfo info = { | |
449 sizeof(GntTextViewClass), | |
450 NULL, /* base_init */ | |
451 NULL, /* base_finalize */ | |
452 (GClassInitFunc)gnt_text_view_class_init, | |
453 NULL, /* class_finalize */ | |
454 NULL, /* class_data */ | |
455 sizeof(GntTextView), | |
456 0, /* n_preallocs */ | |
457 gnt_text_view_init, /* instance_init */ | |
458 NULL /* value_table */ | |
459 }; | |
460 | |
461 type = g_type_register_static(GNT_TYPE_WIDGET, | |
462 "GntTextView", | |
463 &info, 0); | |
464 } | |
465 | |
466 return type; | |
467 } | |
468 | |
469 GntWidget *gnt_text_view_new() | |
470 { | |
15931
f00f2e283ffb
Some define changes. This helps in generating the python bindings.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15818
diff
changeset
|
471 GntWidget *widget = g_object_new(GNT_TYPE_TEXT_VIEW, NULL); |
15818 | 472 |
473 return widget; | |
474 } | |
475 | |
476 void gnt_text_view_append_text_with_flags(GntTextView *view, const char *text, GntTextFormatFlags flags) | |
477 { | |
478 gnt_text_view_append_text_with_tag(view, text, flags, NULL); | |
479 } | |
480 | |
481 void gnt_text_view_append_text_with_tag(GntTextView *view, const char *text, | |
482 GntTextFormatFlags flags, const char *tagname) | |
483 { | |
484 GntWidget *widget = GNT_WIDGET(view); | |
485 int fl = 0; | |
486 const char *start, *end; | |
487 GList *list = view->list; | |
488 GntTextLine *line; | |
489 int len; | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
490 gboolean has_scroll = !(view->flags & GNT_TEXT_VIEW_NO_SCROLL); |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
491 gboolean wrap_word = !(view->flags & GNT_TEXT_VIEW_WRAP_CHAR); |
15818 | 492 |
493 if (text == NULL || *text == '\0') | |
494 return; | |
495 | |
496 fl = gnt_text_format_flag_to_chtype(flags); | |
497 | |
498 len = view->string->len; | |
499 view->string = g_string_append(view->string, text); | |
500 | |
501 if (tagname) { | |
502 GntTextTag *tag = g_new0(GntTextTag, 1); | |
503 tag->name = g_strdup(tagname); | |
504 tag->start = len; | |
505 tag->end = view->string->len; | |
506 view->tags = g_list_append(view->tags, tag); | |
507 } | |
508 | |
509 view->list = g_list_first(view->list); | |
510 | |
511 start = end = view->string->str + len; | |
512 | |
513 while (*start) { | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
514 GntTextLine *oldl; |
15818 | 515 GntTextSegment *seg = NULL; |
516 | |
517 if (*end == '\n' || *end == '\r') { | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
518 if (!strncmp(end, "\r\n", 2)) |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
519 end++; |
15818 | 520 end++; |
521 start = end; | |
522 gnt_text_view_next_line(view); | |
523 view->list = g_list_first(view->list); | |
524 continue; | |
525 } | |
526 | |
527 line = view->list->data; | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
528 if (line->length == widget->priv.width - has_scroll) { |
15818 | 529 /* The last added line was exactly the same width as the widget */ |
530 line = g_new0(GntTextLine, 1); | |
531 line->soft = TRUE; | |
532 view->list = g_list_prepend(view->list, line); | |
533 } | |
534 | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
535 if ((end = strchr(start, '\r')) != NULL || |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
536 (end = strchr(start, '\n')) != NULL) { |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
537 len = gnt_util_onscreen_width(start, end - has_scroll); |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
538 if (len >= widget->priv.width - line->length - has_scroll) { |
15818 | 539 end = NULL; |
540 } | |
541 } | |
542 | |
543 if (end == NULL) | |
544 end = gnt_util_onscreen_width_to_pointer(start, | |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
545 widget->priv.width - line->length - has_scroll, &len); |
15818 | 546 |
547 /* Try to append to the previous segment if possible */ | |
548 if (line->segments) { | |
549 seg = g_list_last(line->segments)->data; | |
550 if (seg->flags != fl) | |
551 seg = NULL; | |
552 } | |
553 | |
554 if (seg == NULL) { | |
555 seg = g_new0(GntTextSegment, 1); | |
556 seg->start = start - view->string->str; | |
557 seg->tvflag = flags; | |
558 seg->flags = fl; | |
559 line->segments = g_list_append(line->segments, seg); | |
560 } | |
561 | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
562 oldl = line; |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
563 if (wrap_word && *end && *end != '\n' && *end != '\r') { |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
564 const char *tmp = end; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
565 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
|
566 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
|
567 } |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
568 if (!end || !g_ascii_isspace(*end)) |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
569 end = tmp; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
570 else |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
571 end++; /* Remove the space */ |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
572 |
15818 | 573 line = g_new0(GntTextLine, 1); |
574 line->soft = TRUE; | |
575 view->list = g_list_prepend(view->list, line); | |
576 } | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
577 seg->end = end - view->string->str; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
578 oldl->length += len; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15931
diff
changeset
|
579 start = end; |
15818 | 580 } |
581 | |
582 view->list = list; | |
583 | |
584 gnt_widget_draw(widget); | |
585 } | |
586 | |
587 void gnt_text_view_scroll(GntTextView *view, int scroll) | |
588 { | |
589 if (scroll == 0) | |
590 { | |
591 view->list = g_list_first(view->list); | |
592 } | |
593 else if (scroll > 0) | |
594 { | |
595 GList *list = g_list_nth_prev(view->list, scroll); | |
596 if (list == NULL) | |
597 list = g_list_first(view->list); | |
598 view->list = list; | |
599 } | |
600 else if (scroll < 0) | |
601 { | |
602 GList *list = g_list_nth(view->list, -scroll); | |
603 if (list == NULL) | |
604 list = g_list_last(view->list); | |
605 view->list = list; | |
606 } | |
607 | |
608 gnt_widget_draw(GNT_WIDGET(view)); | |
609 } | |
610 | |
611 void gnt_text_view_next_line(GntTextView *view) | |
612 { | |
613 GntTextLine *line = g_new0(GntTextLine, 1); | |
614 GList *list = view->list; | |
615 | |
616 view->list = g_list_prepend(g_list_first(view->list), line); | |
617 view->list = list; | |
618 gnt_widget_draw(GNT_WIDGET(view)); | |
619 } | |
620 | |
621 chtype gnt_text_format_flag_to_chtype(GntTextFormatFlags flags) | |
622 { | |
623 chtype fl = 0; | |
624 | |
625 if (flags & GNT_TEXT_FLAG_BOLD) | |
626 fl |= A_BOLD; | |
627 if (flags & GNT_TEXT_FLAG_UNDERLINE) | |
628 fl |= A_UNDERLINE; | |
629 if (flags & GNT_TEXT_FLAG_BLINK) | |
630 fl |= A_BLINK; | |
631 | |
632 if (flags & GNT_TEXT_FLAG_DIM) | |
633 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_DISABLED)); | |
634 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT) | |
635 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_HIGHLIGHT)); | |
636 else | |
637 fl |= COLOR_PAIR(GNT_COLOR_NORMAL); | |
638 | |
639 return fl; | |
640 } | |
641 | |
642 void gnt_text_view_clear(GntTextView *view) | |
643 { | |
644 GntTextLine *line; | |
645 | |
646 g_list_foreach(view->list, free_text_line, NULL); | |
647 g_list_free(view->list); | |
648 view->list = NULL; | |
649 | |
650 line = g_new0(GntTextLine, 1); | |
651 view->list = g_list_append(view->list, line); | |
652 if (view->string) | |
653 g_string_free(view->string, TRUE); | |
654 view->string = g_string_new(NULL); | |
655 | |
656 if (GNT_WIDGET(view)->window) | |
657 gnt_widget_draw(GNT_WIDGET(view)); | |
658 } | |
659 | |
660 int gnt_text_view_get_lines_below(GntTextView *view) | |
661 { | |
662 int below = 0; | |
663 GList *list = view->list; | |
664 while ((list = list->prev)) | |
665 ++below; | |
666 return below; | |
667 } | |
668 | |
669 int gnt_text_view_get_lines_above(GntTextView *view) | |
670 { | |
671 int above = 0; | |
672 GList *list = view->list; | |
673 list = g_list_nth(view->list, GNT_WIDGET(view)->priv.height); | |
674 if (!list) | |
675 return 0; | |
676 while ((list = list->next)) | |
677 ++above; | |
678 return above; | |
679 } | |
680 | |
681 /** | |
682 * XXX: There are quite possibly more than a few bugs here. | |
683 */ | |
684 int gnt_text_view_tag_change(GntTextView *view, const char *name, const char *text, gboolean all) | |
685 { | |
686 GList *alllines = g_list_first(view->list); | |
687 GList *list, *next, *iter, *inext; | |
688 const int text_length = text ? strlen(text) : 0; | |
689 int count = 0; | |
690 for (list = view->tags; list; list = next) { | |
691 GntTextTag *tag = list->data; | |
692 next = list->next; | |
693 if (strcmp(tag->name, name) == 0) { | |
694 int change; | |
695 char *before, *after; | |
696 | |
697 count++; | |
698 | |
699 before = g_strndup(view->string->str, tag->start); | |
700 after = g_strdup(view->string->str + tag->end); | |
701 change = (tag->end - tag->start) - text_length; | |
702 | |
703 g_string_printf(view->string, "%s%s%s", before, text ? text : "", after); | |
704 g_free(before); | |
705 g_free(after); | |
706 | |
707 /* Update the offsets of the next tags */ | |
708 for (iter = next; iter; iter = iter->next) { | |
709 GntTextTag *t = iter->data; | |
710 t->start -= change; | |
711 t->end -= change; | |
712 } | |
713 | |
714 /* Update the offsets of the segments */ | |
715 for (iter = alllines; iter; iter = inext) { | |
716 GList *segs, *snext; | |
717 GntTextLine *line = iter->data; | |
718 inext = iter->next; | |
719 for (segs = line->segments; segs; segs = snext) { | |
720 GntTextSegment *seg = segs->data; | |
721 snext = segs->next; | |
722 if (seg->start >= tag->end) { | |
723 /* The segment is somewhere after the tag */ | |
724 seg->start -= change; | |
725 seg->end -= change; | |
726 } else if (seg->end <= tag->start) { | |
727 /* This segment is somewhere in front of the tag */ | |
728 } else if (seg->start >= tag->start) { | |
729 /* This segment starts in the middle of the tag */ | |
730 if (text == NULL) { | |
731 free_text_segment(seg, NULL); | |
732 line->segments = g_list_delete_link(line->segments, segs); | |
733 if (line->segments == NULL) { | |
734 free_text_line(line, NULL); | |
735 if (view->list == iter) { | |
736 if (inext) | |
737 view->list = inext; | |
738 else | |
739 view->list = iter->prev; | |
740 } | |
741 alllines = g_list_delete_link(alllines, iter); | |
742 } | |
743 } else { | |
744 /* XXX: (null) */ | |
745 seg->start = tag->start; | |
746 seg->end = tag->end - change; | |
747 } | |
748 line->length -= change; | |
749 /* XXX: Make things work if the tagged text spans over several lines. */ | |
750 } else { | |
751 /* XXX: handle the rest of the conditions */ | |
752 g_printerr("WTF! This needs to be handled properly!!\n"); | |
753 } | |
754 } | |
755 } | |
756 if (text == NULL) { | |
757 /* Remove the tag */ | |
758 view->tags = g_list_delete_link(view->tags, list); | |
759 free_tag(tag, NULL); | |
760 } else { | |
761 tag->end -= change; | |
762 } | |
763 if (!all) | |
764 break; | |
765 } | |
766 } | |
767 return count; | |
768 } | |
769 | |
16124
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
770 static gboolean |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
771 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
|
772 { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
773 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
|
774 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
|
775 } 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
|
776 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
|
777 } 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
|
778 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
|
779 } 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
|
780 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
|
781 } else { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
782 return FALSE; |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
783 } |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
784 return TRUE; |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
785 } |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
786 |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
787 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
|
788 { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
789 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
|
790 } |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
791 |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
792 void gnt_text_view_set_flag(GntTextView *view, GntTextViewFlag flag) |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
793 { |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
794 view->flags |= flag; |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
795 } |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18049
diff
changeset
|
796 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
797 /* Pager and editor setups */ |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
798 struct |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
799 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
800 GntTextView *tv; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
801 char *file; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
802 } pageditor; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
803 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
804 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
805 static void |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
806 cleanup_pageditor() |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
807 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
808 unlink(pageditor.file); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
809 g_free(pageditor.file); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
810 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
811 pageditor.file = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
812 pageditor.tv = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
813 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
814 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
815 static void |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
816 editor_end_cb(int status, gpointer data) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
817 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
818 if (status == 0) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
819 char *text = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
820 if (g_file_get_contents(pageditor.file, &text, NULL, NULL)) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
821 gnt_text_view_clear(pageditor.tv); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
822 gnt_text_view_append_text_with_flags(pageditor.tv, text, GNT_TEXT_FLAG_NORMAL); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
823 gnt_text_view_scroll(GNT_TEXT_VIEW(pageditor.tv), 0); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
824 g_free(text); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
825 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
826 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
827 cleanup_pageditor(); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
828 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
829 |
18526
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
830 static void |
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
831 pager_end_cb(int status, gpointer data) |
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
832 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
833 cleanup_pageditor(); |
18526
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
834 } |
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
835 |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
836 static gboolean |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
837 check_for_ext_cb(GntWidget *widget, const char *key, GntTextView *view) |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
838 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
839 static const char *pager = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
840 static const char *editor = NULL; |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
841 char *argv[] = {NULL, NULL, NULL}; |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
842 static char path[1024]; |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
843 static int len = -1; |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
844 FILE *file; |
18526
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
845 gboolean ret; |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
846 gboolean pg; |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
847 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
848 if (pager == NULL) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
849 pager = gnt_key_translate(gnt_style_get_from_name("pager", "key")); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
850 if (pager == NULL) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
851 pager = "\033" "v"; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
852 editor = gnt_key_translate(gnt_style_get_from_name("editor", "key")); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
853 if (editor == NULL) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
854 editor = "\033" "e"; |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
855 len = g_snprintf(path, sizeof(path), "%s" G_DIR_SEPARATOR_S "gnt", g_get_tmp_dir()); |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
856 } else { |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
857 g_snprintf(path + len, sizeof(path) - len, "XXXXXX"); |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
858 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
859 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
860 if (strcmp(key, pager) == 0) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
861 if (g_object_get_data(G_OBJECT(widget), "pager-for") != view) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
862 return FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
863 pg = TRUE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
864 } else if (strcmp(key, editor) == 0) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
865 if (g_object_get_data(G_OBJECT(widget), "editor-for") != view) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
866 return FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
867 pg = FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
868 } else { |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
869 return FALSE; |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
870 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
871 |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
872 file = fdopen(g_mkstemp(path), "wb"); |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
873 if (!file) |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
874 return FALSE; |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
875 fprintf(file, "%s", view->string->str); |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
876 fclose(file); |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
877 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
878 pageditor.tv = view; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
879 pageditor.file = g_strdup(path); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
880 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
881 argv[0] = gnt_style_get_from_name(pg ? "pager" : "editor", "path"); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
882 argv[0] = argv[0] ? argv[0] : getenv(pg ? "PAGER" : "EDITOR"); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
883 argv[0] = argv[0] ? argv[0] : (pg ? "less" : "vim"); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
884 argv[1] = path; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
885 ret = gnt_giveup_console(NULL, argv, NULL, NULL, NULL, NULL, pg ? pager_end_cb : editor_end_cb, NULL); |
18526
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
886 return ret; |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
887 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
888 |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
889 void gnt_text_view_attach_pager_widget(GntTextView *view, GntWidget *pager) |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
890 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
891 g_signal_connect(pager, "key_pressed", G_CALLBACK(check_for_ext_cb), view); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
892 g_object_set_data(G_OBJECT(pager), "pager-for", view); |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
893 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
894 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
895 void gnt_text_view_attach_editor_widget(GntTextView *view, GntWidget *wid) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
896 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
897 g_signal_connect(wid, "key_pressed", G_CALLBACK(check_for_ext_cb), view); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
898 g_object_set_data(G_OBJECT(wid), "editor-for", view); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
899 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
900 |