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