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