Mercurial > pidgin
annotate finch/libgnt/gnttextview.c @ 21205:ebbe7b5df08e
Move 2.2.2 to the right place.
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Wed, 10 Oct 2007 04:01:22 +0000 |
parents | 44b4e8bd759b |
children | 9187d331aebe |
rev | line source |
---|---|
17928
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
1 /** |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
3 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
6 * source distribution. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
7 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
11 * (at your option) any later version. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
12 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
16 * GNU General Public License for more details. |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
17 * |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
19681
44b4e8bd759b
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
19376
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
17928
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
parents:
16124
diff
changeset
|
21 */ |
8410511f4dbb
applied changes from 016401bd409e6229fae0ab1e80d9fef9365511b3
Eric Polino <aluink@pidgin.im>
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
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:
17928
diff
changeset
|
551 len = gnt_util_onscreen_width(start, end - has_scroll); |
19376
40c3e3857458
Fix #2642 (GntTextView Multiline append goes weird)
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18873
diff
changeset
|
552 if (widget->priv.width > 0 && |
40c3e3857458
Fix #2642 (GntTextView Multiline append goes weird)
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18873
diff
changeset
|
553 len >= widget->priv.width - line->length - has_scroll) { |
15817 | 554 end = NULL; |
555 } | |
556 } | |
557 | |
558 if (end == NULL) | |
559 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:
17928
diff
changeset
|
560 widget->priv.width - line->length - has_scroll, &len); |
15817 | 561 |
562 /* Try to append to the previous segment if possible */ | |
563 if (line->segments) { | |
564 seg = g_list_last(line->segments)->data; | |
565 if (seg->flags != fl) | |
566 seg = NULL; | |
567 } | |
568 | |
569 if (seg == NULL) { | |
570 seg = g_new0(GntTextSegment, 1); | |
571 seg->start = start - view->string->str; | |
572 seg->tvflag = flags; | |
573 seg->flags = fl; | |
574 line->segments = g_list_append(line->segments, seg); | |
575 } | |
576 | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
577 oldl = line; |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
578 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
|
579 const char *tmp = end; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
580 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
|
581 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
|
582 } |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
583 if (!end || !g_ascii_isspace(*end)) |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
584 end = tmp; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
585 else |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
586 end++; /* Remove the space */ |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
587 |
15817 | 588 line = g_new0(GntTextLine, 1); |
589 line->soft = TRUE; | |
590 view->list = g_list_prepend(view->list, line); | |
591 } | |
16011
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
592 seg->end = end - view->string->str; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
593 oldl->length += len; |
1f24fd9a6237
word wrapping, fix \r\n newlines
Richard Nelson <wabz@pidgin.im>
parents:
15928
diff
changeset
|
594 start = end; |
15817 | 595 } |
596 | |
597 view->list = list; | |
598 | |
599 gnt_widget_draw(widget); | |
600 } | |
601 | |
602 void gnt_text_view_scroll(GntTextView *view, int scroll) | |
603 { | |
604 if (scroll == 0) | |
605 { | |
606 view->list = g_list_first(view->list); | |
607 } | |
608 else if (scroll > 0) | |
609 { | |
610 GList *list = g_list_nth_prev(view->list, scroll); | |
611 if (list == NULL) | |
612 list = g_list_first(view->list); | |
613 view->list = list; | |
614 } | |
615 else if (scroll < 0) | |
616 { | |
617 GList *list = g_list_nth(view->list, -scroll); | |
618 if (list == NULL) | |
619 list = g_list_last(view->list); | |
620 view->list = list; | |
621 } | |
622 | |
623 gnt_widget_draw(GNT_WIDGET(view)); | |
624 } | |
625 | |
626 void gnt_text_view_next_line(GntTextView *view) | |
627 { | |
628 GntTextLine *line = g_new0(GntTextLine, 1); | |
629 GList *list = view->list; | |
630 | |
631 view->list = g_list_prepend(g_list_first(view->list), line); | |
632 view->list = list; | |
633 gnt_widget_draw(GNT_WIDGET(view)); | |
634 } | |
635 | |
636 chtype gnt_text_format_flag_to_chtype(GntTextFormatFlags flags) | |
637 { | |
638 chtype fl = 0; | |
639 | |
640 if (flags & GNT_TEXT_FLAG_BOLD) | |
641 fl |= A_BOLD; | |
642 if (flags & GNT_TEXT_FLAG_UNDERLINE) | |
643 fl |= A_UNDERLINE; | |
644 if (flags & GNT_TEXT_FLAG_BLINK) | |
645 fl |= A_BLINK; | |
646 | |
647 if (flags & GNT_TEXT_FLAG_DIM) | |
648 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_DISABLED)); | |
649 else if (flags & GNT_TEXT_FLAG_HIGHLIGHT) | |
650 fl |= (A_DIM | COLOR_PAIR(GNT_COLOR_HIGHLIGHT)); | |
651 else | |
652 fl |= COLOR_PAIR(GNT_COLOR_NORMAL); | |
653 | |
654 return fl; | |
655 } | |
656 | |
657 void gnt_text_view_clear(GntTextView *view) | |
658 { | |
659 GntTextLine *line; | |
660 | |
661 g_list_foreach(view->list, free_text_line, NULL); | |
662 g_list_free(view->list); | |
663 view->list = NULL; | |
664 | |
665 line = g_new0(GntTextLine, 1); | |
666 view->list = g_list_append(view->list, line); | |
667 if (view->string) | |
668 g_string_free(view->string, TRUE); | |
669 view->string = g_string_new(NULL); | |
670 | |
671 if (GNT_WIDGET(view)->window) | |
672 gnt_widget_draw(GNT_WIDGET(view)); | |
673 } | |
674 | |
675 int gnt_text_view_get_lines_below(GntTextView *view) | |
676 { | |
677 int below = 0; | |
678 GList *list = view->list; | |
679 while ((list = list->prev)) | |
680 ++below; | |
681 return below; | |
682 } | |
683 | |
684 int gnt_text_view_get_lines_above(GntTextView *view) | |
685 { | |
686 int above = 0; | |
687 GList *list = view->list; | |
688 list = g_list_nth(view->list, GNT_WIDGET(view)->priv.height); | |
689 if (!list) | |
690 return 0; | |
691 while ((list = list->next)) | |
692 ++above; | |
693 return above; | |
694 } | |
695 | |
696 /** | |
697 * XXX: There are quite possibly more than a few bugs here. | |
698 */ | |
699 int gnt_text_view_tag_change(GntTextView *view, const char *name, const char *text, gboolean all) | |
700 { | |
701 GList *alllines = g_list_first(view->list); | |
702 GList *list, *next, *iter, *inext; | |
703 const int text_length = text ? strlen(text) : 0; | |
704 int count = 0; | |
705 for (list = view->tags; list; list = next) { | |
706 GntTextTag *tag = list->data; | |
707 next = list->next; | |
708 if (strcmp(tag->name, name) == 0) { | |
709 int change; | |
710 char *before, *after; | |
711 | |
712 count++; | |
713 | |
714 before = g_strndup(view->string->str, tag->start); | |
715 after = g_strdup(view->string->str + tag->end); | |
716 change = (tag->end - tag->start) - text_length; | |
717 | |
718 g_string_printf(view->string, "%s%s%s", before, text ? text : "", after); | |
719 g_free(before); | |
720 g_free(after); | |
721 | |
722 /* Update the offsets of the next tags */ | |
723 for (iter = next; iter; iter = iter->next) { | |
724 GntTextTag *t = iter->data; | |
725 t->start -= change; | |
726 t->end -= change; | |
727 } | |
728 | |
729 /* Update the offsets of the segments */ | |
730 for (iter = alllines; iter; iter = inext) { | |
731 GList *segs, *snext; | |
732 GntTextLine *line = iter->data; | |
733 inext = iter->next; | |
734 for (segs = line->segments; segs; segs = snext) { | |
735 GntTextSegment *seg = segs->data; | |
736 snext = segs->next; | |
737 if (seg->start >= tag->end) { | |
738 /* The segment is somewhere after the tag */ | |
739 seg->start -= change; | |
740 seg->end -= change; | |
741 } else if (seg->end <= tag->start) { | |
742 /* This segment is somewhere in front of the tag */ | |
743 } else if (seg->start >= tag->start) { | |
744 /* This segment starts in the middle of the tag */ | |
745 if (text == NULL) { | |
746 free_text_segment(seg, NULL); | |
747 line->segments = g_list_delete_link(line->segments, segs); | |
748 if (line->segments == NULL) { | |
749 free_text_line(line, NULL); | |
750 if (view->list == iter) { | |
751 if (inext) | |
752 view->list = inext; | |
753 else | |
754 view->list = iter->prev; | |
755 } | |
756 alllines = g_list_delete_link(alllines, iter); | |
757 } | |
758 } else { | |
759 /* XXX: (null) */ | |
760 seg->start = tag->start; | |
761 seg->end = tag->end - change; | |
762 } | |
763 line->length -= change; | |
764 /* XXX: Make things work if the tagged text spans over several lines. */ | |
765 } else { | |
766 /* XXX: handle the rest of the conditions */ | |
767 g_printerr("WTF! This needs to be handled properly!!\n"); | |
768 } | |
769 } | |
770 } | |
771 if (text == NULL) { | |
772 /* Remove the tag */ | |
773 view->tags = g_list_delete_link(view->tags, list); | |
774 free_tag(tag, NULL); | |
775 } else { | |
776 tag->end -= change; | |
777 } | |
778 if (!all) | |
779 break; | |
780 } | |
781 } | |
782 return count; | |
783 } | |
784 | |
16124
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
785 static gboolean |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
786 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
|
787 { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
788 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
|
789 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
|
790 } 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
|
791 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
|
792 } 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
|
793 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
|
794 } 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
|
795 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
|
796 } else { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
797 return FALSE; |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
798 } |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
799 return TRUE; |
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 |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
802 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
|
803 { |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
804 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
|
805 } |
ab3f93232a2d
Add a utility function to assist scrolling in a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
16011
diff
changeset
|
806 |
18315
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
807 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:
17928
diff
changeset
|
808 { |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
809 view->flags |= flag; |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
810 } |
e659842fe66d
Add flags to a textview to decide whether to show scrollbars, and whether
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
17928
diff
changeset
|
811 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
812 /* Pager and editor setups */ |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
813 struct |
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 GntTextView *tv; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
816 char *file; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
817 } pageditor; |
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 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
820 static void |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
821 cleanup_pageditor() |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
822 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
823 unlink(pageditor.file); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
824 g_free(pageditor.file); |
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 pageditor.file = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
827 pageditor.tv = NULL; |
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 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
830 static void |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
831 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
|
832 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
833 if (status == 0) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
834 char *text = NULL; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
835 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
|
836 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
|
837 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
|
838 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
|
839 g_free(text); |
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 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
842 cleanup_pageditor(); |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
843 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
844 |
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 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
|
846 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
|
847 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
848 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
|
849 } |
09db6fec9dce
Allow having a callback after the child process terminates. Use the callback
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18426
diff
changeset
|
850 |
18426
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
851 static gboolean |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
852 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
|
853 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
854 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
|
855 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
|
856 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
|
857 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
|
858 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
|
859 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
|
860 gboolean ret; |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
861 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
|
862 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
863 if (pager == NULL) { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
864 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
|
865 if (pager == NULL) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
866 pager = "\033" "v"; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
867 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
|
868 if (editor == NULL) |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
869 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
|
870 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
|
871 } else { |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
872 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
|
873 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
874 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
875 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
|
876 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
|
877 return FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
878 pg = TRUE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
879 } 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
|
880 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
|
881 return FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
882 pg = FALSE; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
883 } 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
|
884 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
|
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 |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
887 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
|
888 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
|
889 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
|
890 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
|
891 fclose(file); |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
892 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
893 pageditor.tv = view; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
894 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
|
895 |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
896 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
|
897 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
|
898 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
|
899 argv[1] = path; |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
900 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
|
901 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
|
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 |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
904 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
|
905 { |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
906 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
|
907 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
|
908 } |
841670dd24e1
Utility function to start the pager with the contents of a textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18315
diff
changeset
|
909 |
18539
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
910 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
|
911 { |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
912 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
|
913 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
|
914 } |
1148da95ddeb
Allow changing the content of a textview using $EDITOR.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18526
diff
changeset
|
915 |