Mercurial > pidgin
annotate src/gtkimhtml.c @ 7767:1f4005fcd872
[gaim-migrate @ 8412]
As Nathan pointed out, a printf'd stringref is probably desirable
committer: Tailor Script <tailor@pidgin.im>
author | Ethan Blanton <elb@pidgin.im> |
---|---|
date | Fri, 05 Dec 2003 17:28:22 +0000 |
parents | 9d6ba1c44cb7 |
children | 3db18c16f573 |
rev | line source |
---|---|
1428 | 1 /* |
2 * GtkIMHtml | |
3 * | |
4 * Copyright (C) 2000, Eric Warmenhoven <warmenhoven@yahoo.com> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
23 #include <config.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
24 #endif |
1428 | 25 #include "gtkimhtml.h" |
7358 | 26 #include "gtksourceiter.h" |
1428 | 27 #include <gtk/gtk.h> |
4895 | 28 #include <glib/gerror.h> |
4046 | 29 #include <gdk/gdkkeysyms.h> |
1428 | 30 #include <string.h> |
31 #include <ctype.h> | |
32 #include <stdio.h> | |
4629 | 33 #include <stdlib.h> |
1428 | 34 #include <math.h> |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
35 #ifdef HAVE_LANGINFO_CODESET |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
36 #include <langinfo.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
37 #include <locale.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
38 #endif |
1428 | 39 |
4417 | 40 #ifdef ENABLE_NLS |
41 # include <libintl.h> | |
42 # define _(x) gettext(x) | |
43 # ifdef gettext_noop | |
44 # define N_(String) gettext_noop (String) | |
45 # else | |
46 # define N_(String) (String) | |
47 # endif | |
48 #else | |
49 # define N_(String) (String) | |
50 # define _(x) (x) | |
51 #endif | |
52 | |
4735 | 53 #include <pango/pango-font.h> |
54 | |
5105 | 55 /* GTK+ < 2.2.2 hack, see ui.h for details. */ |
56 #ifndef GTK_WRAP_WORD_CHAR | |
57 #define GTK_WRAP_WORD_CHAR GTK_WRAP_WORD | |
58 #endif | |
59 | |
4735 | 60 #define TOOLTIP_TIMEOUT 500 |
61 | |
7694 | 62 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml); |
63 | |
3922 | 64 /* POINT_SIZE converts from AIM font sizes to point sizes. It probably should be redone in such a |
65 * way that it base the sizes off the default font size rather than using arbitrary font sizes. */ | |
66 #define MAX_FONT_SIZE 7 | |
5367 | 67 #define POINT_SIZE(x) (options & GTK_IMHTML_USE_POINTSIZE ? x : _point_sizes [MIN ((x), MAX_FONT_SIZE) - 1]) |
3928 | 68 static gint _point_sizes [] = { 8, 10, 12, 14, 20, 30, 40 }; |
2349
60c716c32c40
[gaim-migrate @ 2362]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2348
diff
changeset
|
69 |
7749 | 70 enum { |
71 TARGET_HTML, | |
72 TARGET_UTF8_STRING, | |
73 TARGET_COMPOUND_TEXT, | |
74 TARGET_STRING, | |
75 TARGET_TEXT | |
76 }; | |
77 | |
78 GtkTargetEntry selection_targets[] = { | |
79 { "text/html", 0, TARGET_HTML }, | |
80 { "UTF8_STRING", 0, TARGET_UTF8_STRING }, | |
81 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT }, | |
82 { "STRING", 0, TARGET_STRING }, | |
83 { "TEXT", 0, TARGET_TEXT}}; | |
84 | |
4032 | 85 static GtkSmileyTree* |
86 gtk_smiley_tree_new () | |
87 { | |
88 return g_new0 (GtkSmileyTree, 1); | |
89 } | |
90 | |
91 static void | |
92 gtk_smiley_tree_insert (GtkSmileyTree *tree, | |
4263 | 93 GtkIMHtmlSmiley *smiley) |
4032 | 94 { |
95 GtkSmileyTree *t = tree; | |
4263 | 96 const gchar *x = smiley->smile; |
4032 | 97 |
98 if (!strlen (x)) | |
99 return; | |
100 | |
101 while (*x) { | |
102 gchar *pos; | |
103 gint index; | |
104 | |
105 if (!t->values) | |
106 t->values = g_string_new (""); | |
107 | |
108 pos = strchr (t->values->str, *x); | |
109 if (!pos) { | |
110 t->values = g_string_append_c (t->values, *x); | |
111 index = t->values->len - 1; | |
112 t->children = g_realloc (t->children, t->values->len * sizeof (GtkSmileyTree *)); | |
113 t->children [index] = g_new0 (GtkSmileyTree, 1); | |
114 } else | |
7386 | 115 index = GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str); |
4032 | 116 |
117 t = t->children [index]; | |
118 | |
119 x++; | |
120 } | |
121 | |
4263 | 122 t->image = smiley; |
4032 | 123 } |
4041 | 124 |
4263 | 125 |
4264 | 126 void gtk_smiley_tree_destroy (GtkSmileyTree *tree) |
4032 | 127 { |
128 GSList *list = g_slist_append (NULL, tree); | |
129 | |
130 while (list) { | |
131 GtkSmileyTree *t = list->data; | |
132 gint i; | |
133 list = g_slist_remove(list, t); | |
7384 | 134 if (t && t->values) { |
4032 | 135 for (i = 0; i < t->values->len; i++) |
136 list = g_slist_append (list, t->children [i]); | |
137 g_string_free (t->values, TRUE); | |
138 g_free (t->children); | |
139 } | |
140 g_free (t); | |
141 } | |
142 } | |
143 | |
5967 | 144 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data) |
145 { | |
146 GdkRectangle rect; | |
147 | |
148 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &rect); | |
149 if(widget->old_rect.width != rect.width || widget->old_rect.height != rect.height){ | |
150 GList *iter = GTK_IMHTML(widget)->scalables; | |
151 | |
152 while(iter){ | |
153 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(iter->data); | |
154 scale->scale(scale, rect.width, rect.height); | |
155 | |
156 iter = iter->next; | |
157 } | |
158 } | |
159 | |
160 widget->old_rect = rect; | |
161 return FALSE; | |
162 } | |
163 | |
164 static gint | |
165 gtk_imhtml_tip_paint (GtkIMHtml *imhtml) | |
166 { | |
167 PangoLayout *layout; | |
168 | |
169 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
170 | |
171 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
172 | |
173 gtk_paint_flat_box (imhtml->tip_window->style, imhtml->tip_window->window, | |
174 GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, imhtml->tip_window, | |
175 "tooltip", 0, 0, -1, -1); | |
176 | |
177 gtk_paint_layout (imhtml->tip_window->style, imhtml->tip_window->window, GTK_STATE_NORMAL, | |
178 FALSE, NULL, imhtml->tip_window, NULL, 4, 4, layout); | |
179 | |
180 g_object_unref(layout); | |
181 return FALSE; | |
182 } | |
183 | |
184 static gint | |
185 gtk_imhtml_tip (gpointer data) | |
186 { | |
187 GtkIMHtml *imhtml = data; | |
188 PangoFontMetrics *font; | |
189 PangoLayout *layout; | |
190 | |
191 gint gap, x, y, h, w, scr_w, baseline_skip; | |
192 | |
193 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
194 | |
195 if (!imhtml->tip || !GTK_WIDGET_DRAWABLE (GTK_WIDGET(imhtml))) { | |
196 imhtml->tip_timer = 0; | |
197 return FALSE; | |
198 } | |
199 | |
200 if (imhtml->tip_window){ | |
201 gtk_widget_destroy (imhtml->tip_window); | |
202 imhtml->tip_window = NULL; | |
203 } | |
204 | |
205 imhtml->tip_timer = 0; | |
206 imhtml->tip_window = gtk_window_new (GTK_WINDOW_POPUP); | |
207 gtk_widget_set_app_paintable (imhtml->tip_window, TRUE); | |
208 gtk_window_set_resizable (GTK_WINDOW (imhtml->tip_window), FALSE); | |
209 gtk_widget_set_name (imhtml->tip_window, "gtk-tooltips"); | |
210 g_signal_connect_swapped (G_OBJECT (imhtml->tip_window), "expose_event", | |
211 G_CALLBACK (gtk_imhtml_tip_paint), imhtml); | |
212 | |
213 gtk_widget_ensure_style (imhtml->tip_window); | |
214 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
215 font = pango_font_get_metrics(pango_context_load_font(pango_layout_get_context(layout), | |
216 imhtml->tip_window->style->font_desc), | |
217 NULL); | |
218 | |
219 | |
220 pango_layout_get_pixel_size(layout, &scr_w, NULL); | |
221 gap = PANGO_PIXELS((pango_font_metrics_get_ascent(font) + | |
222 pango_font_metrics_get_descent(font))/ 4); | |
223 | |
224 if (gap < 2) | |
225 gap = 2; | |
226 baseline_skip = PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
227 pango_font_metrics_get_descent(font)); | |
228 w = 8 + scr_w; | |
229 h = 8 + baseline_skip; | |
230 | |
231 gdk_window_get_pointer (NULL, &x, &y, NULL); | |
232 if (GTK_WIDGET_NO_WINDOW (GTK_WIDGET(imhtml))) | |
233 y += GTK_WIDGET(imhtml)->allocation.y; | |
234 | |
235 scr_w = gdk_screen_width(); | |
236 | |
237 x -= ((w >> 1) + 4); | |
238 | |
239 if ((x + w) > scr_w) | |
240 x -= (x + w) - scr_w; | |
241 else if (x < 0) | |
242 x = 0; | |
243 | |
244 y = y + PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
245 pango_font_metrics_get_descent(font)); | |
246 | |
247 gtk_widget_set_size_request (imhtml->tip_window, w, h); | |
248 gtk_widget_show (imhtml->tip_window); | |
249 gtk_window_move (GTK_WINDOW(imhtml->tip_window), x, y); | |
250 | |
251 pango_font_metrics_unref(font); | |
252 g_object_unref(layout); | |
253 | |
254 return FALSE; | |
255 } | |
256 | |
257 gboolean gtk_motion_event_notify(GtkWidget *imhtml, GdkEventMotion *event, gpointer data) | |
258 { | |
259 GtkTextIter iter; | |
260 GdkWindow *win = event->window; | |
261 int x, y; | |
262 char *tip = NULL; | |
263 GSList *tags = NULL, *templist = NULL; | |
264 gdk_window_get_pointer(GTK_WIDGET(imhtml)->window, NULL, NULL, NULL); | |
265 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), GTK_TEXT_WINDOW_WIDGET, | |
266 event->x, event->y, &x, &y); | |
267 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); | |
268 tags = gtk_text_iter_get_tags(&iter); | |
269 | |
270 templist = tags; | |
271 while (templist) { | |
272 GtkTextTag *tag = templist->data; | |
273 tip = g_object_get_data(G_OBJECT(tag), "link_url"); | |
274 if (tip) | |
275 break; | |
276 templist = templist->next; | |
277 } | |
278 | |
279 if (GTK_IMHTML(imhtml)->tip) { | |
280 if ((tip == GTK_IMHTML(imhtml)->tip)) { | |
281 return FALSE; | |
282 } | |
283 /* We've left the cell. Remove the timeout and create a new one below */ | |
284 if (GTK_IMHTML(imhtml)->tip_window) { | |
285 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
286 GTK_IMHTML(imhtml)->tip_window = NULL; | |
287 } | |
7694 | 288 if (GTK_IMHTML(imhtml)->editable) |
289 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->text_cursor); | |
290 else | |
291 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 292 if (GTK_IMHTML(imhtml)->tip_timer) |
293 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
294 GTK_IMHTML(imhtml)->tip_timer = 0; | |
295 } | |
296 | |
297 if(tip){ | |
7709 | 298 if (!GTK_IMHTML(imhtml)->editable) |
7707 | 299 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->hand_cursor); |
5967 | 300 GTK_IMHTML(imhtml)->tip_timer = g_timeout_add (TOOLTIP_TIMEOUT, |
301 gtk_imhtml_tip, imhtml); | |
302 } | |
303 | |
304 GTK_IMHTML(imhtml)->tip = tip; | |
305 g_slist_free(tags); | |
306 return FALSE; | |
307 } | |
308 | |
309 gboolean gtk_leave_event_notify(GtkWidget *imhtml, GdkEventCrossing *event, gpointer data) | |
310 { | |
311 /* when leaving the widget, clear any current & pending tooltips and restore the cursor */ | |
312 if (GTK_IMHTML(imhtml)->tip_window) { | |
313 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
314 GTK_IMHTML(imhtml)->tip_window = NULL; | |
315 } | |
316 if (GTK_IMHTML(imhtml)->tip_timer) { | |
317 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
318 GTK_IMHTML(imhtml)->tip_timer = 0; | |
319 } | |
7694 | 320 if (GTK_IMHTML(imhtml)->editable) |
321 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->text_cursor); | |
322 else | |
323 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 324 |
325 /* propogate the event normally */ | |
326 return FALSE; | |
327 } | |
328 | |
6066 | 329 /* |
330 * XXX - This should be removed eventually. | |
331 * | |
332 * This function exists to work around a gross bug in GtkTextView. | |
333 * Basically, we short circuit ctrl+a and ctrl+end because they make | |
334 * el program go boom. | |
335 * | |
336 * It's supposed to be fixed in gtk2.2. You can view the bug report at | |
337 * http://bugzilla.gnome.org/show_bug.cgi?id=107939 | |
338 */ | |
339 gboolean gtk_key_pressed_cb(GtkWidget *imhtml, GdkEventKey *event, gpointer data) | |
340 { | |
341 if (event->state & GDK_CONTROL_MASK) | |
342 switch (event->keyval) { | |
343 case 'a': | |
344 return TRUE; | |
345 break; | |
346 | |
347 case GDK_Home: | |
348 return TRUE; | |
349 break; | |
350 | |
351 case GDK_End: | |
352 return TRUE; | |
353 break; | |
354 } | |
355 | |
356 return FALSE; | |
357 } | |
358 | |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
359 #if GTK_CHECK_VERSION(2,2,0) |
7749 | 360 static void gtk_imhtml_clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, GtkIMHtml *imhtml) { |
361 GtkTextIter start, end; | |
7346 | 362 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); |
363 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
364 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | |
365 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
366 | |
7749 | 367 char *text = gtk_imhtml_get_markup_range(imhtml, &start, &end); |
368 if (info == TARGET_HTML) { | |
369 int len; | |
370 GString *str = g_string_new(NULL); | |
371 | |
372 /* Mozilla asks that we start our text/html with the Unicode byte order mark */ | |
373 str = g_string_append_unichar(str, 0xfeff); | |
374 str = g_string_append(str, text); | |
7766 | 375 str = g_string_append_unichar(str, 0x0000); |
7749 | 376 char *selection = g_convert(str->str, str->len, "UCS-2", "UTF-8", NULL, &len, NULL); |
377 gtk_selection_data_set (selection_data, gdk_atom_intern("text/html", FALSE), 16, selection, len); | |
378 g_string_free(str, TRUE); | |
379 g_free(text); | |
380 g_free(selection); | |
381 } else { | |
382 gtk_selection_data_set_text(selection_data, text, strlen(text)); | |
7346 | 383 } |
7749 | 384 } |
385 | |
386 static void gtk_imhtml_clipboard_clear(GtkClipboard *clipboard, GtkIMHtml *imhtml) | |
387 { | |
7750 | 388 GtkTextIter insert; |
389 GtkTextIter selection_bound; | |
390 | |
391 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &insert, | |
392 gtk_text_buffer_get_mark (imhtml->text_buffer, "insert")); | |
393 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &selection_bound, | |
394 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound")); | |
395 | |
396 if (!gtk_text_iter_equal (&insert, &selection_bound)) | |
397 gtk_text_buffer_move_mark (imhtml->text_buffer, | |
398 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound"), | |
399 &insert); | |
7749 | 400 } |
7742 | 401 |
7749 | 402 static void copy_clipboard_cb(GtkIMHtml *imhtml, GtkClipboard *clipboard) |
403 { | |
7750 | 404 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), |
7749 | 405 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), |
406 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
7750 | 407 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); |
7749 | 408 |
409 g_signal_stop_emission_by_name(imhtml, "copy-clipboard"); | |
7346 | 410 } |
411 | |
7766 | 412 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, GtkIMHtml *imhtml) |
413 { | |
414 char *text; | |
415 guint16 c; | |
416 if (selection_data->length < 0) { | |
417 text = gtk_clipboard_wait_for_text(clipboard); | |
418 printf("%s\n", text); | |
419 } else { | |
420 text = g_malloc((selection_data->format / 8) * selection_data->length); | |
421 memcpy(text, selection_data->data, selection_data->length * (selection_data->format / 8)); | |
422 printf("%s\n", text); | |
423 } | |
424 | |
425 memcpy (&c, text, 2); | |
426 if (c == 0xfeff) { | |
427 /* This is UCS2 */ | |
428 char *utf8 = g_convert(text, selection_data->length * (selection_data->format / 8), "UTF-8", "UCS-2", NULL, NULL, NULL); | |
429 g_free(text); | |
430 text = utf8; | |
431 } | |
432 gtk_imhtml_append_text_with_images(imhtml, text, 0, NULL); | |
433 } | |
434 | |
435 | |
436 static void paste_clipboard_cb(GtkIMHtml *imhtml, gpointer blah) | |
437 { | |
438 | |
439 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD); | |
440 gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE), | |
441 paste_received_cb, imhtml); | |
442 g_signal_stop_emission_by_name(imhtml, "paste-clipboard"); | |
443 } | |
444 | |
7346 | 445 static gboolean button_release_cb(GtkIMHtml *imhtml, GdkEventButton event, gpointer the_foibles_of_man) |
446 { | |
7750 | 447 GtkClipboard *clipboard; |
448 if ((clipboard = gtk_widget_get_clipboard (GTK_WIDGET (imhtml), | |
449 GDK_SELECTION_PRIMARY))) | |
450 gtk_text_buffer_remove_selection_clipboard (imhtml->text_buffer, clipboard); | |
451 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY), | |
452 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
453 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
454 (GtkClipboardClearFunc)gtk_imhtml_clipboard_clear, G_OBJECT(imhtml)); | |
7346 | 455 return FALSE; |
456 } | |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
457 #endif |
5967 | 458 |
4263 | 459 |
4032 | 460 static GtkTextViewClass *parent_class = NULL; |
461 | |
3922 | 462 /* GtkIMHtml has one signal--URL_CLICKED */ |
1428 | 463 enum { |
464 URL_CLICKED, | |
465 LAST_SIGNAL | |
466 }; | |
467 static guint signals [LAST_SIGNAL] = { 0 }; | |
468 | |
4032 | 469 static void |
470 gtk_imhtml_finalize (GObject *object) | |
471 { | |
472 GtkIMHtml *imhtml = GTK_IMHTML(object); | |
4895 | 473 GList *scalables; |
7346 | 474 |
4138 | 475 g_hash_table_destroy(imhtml->smiley_data); |
4032 | 476 gtk_smiley_tree_destroy(imhtml->default_smilies); |
4138 | 477 gdk_cursor_unref(imhtml->hand_cursor); |
478 gdk_cursor_unref(imhtml->arrow_cursor); | |
7694 | 479 gdk_cursor_unref(imhtml->text_cursor); |
4735 | 480 if(imhtml->tip_window){ |
481 gtk_widget_destroy(imhtml->tip_window); | |
482 } | |
483 if(imhtml->tip_timer) | |
484 gtk_timeout_remove(imhtml->tip_timer); | |
485 | |
4895 | 486 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { |
487 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); | |
488 scale->free(scale); | |
489 } | |
7346 | 490 |
4895 | 491 g_list_free(imhtml->scalables); |
4032 | 492 G_OBJECT_CLASS(parent_class)->finalize (object); |
493 } | |
1428 | 494 |
3922 | 495 /* Boring GTK stuff */ |
496 static void gtk_imhtml_class_init (GtkIMHtmlClass *class) | |
1428 | 497 { |
3922 | 498 GtkObjectClass *object_class; |
4032 | 499 GObjectClass *gobject_class; |
3922 | 500 object_class = (GtkObjectClass*) class; |
4032 | 501 gobject_class = (GObjectClass*) class; |
502 parent_class = gtk_type_class(GTK_TYPE_TEXT_VIEW); | |
4417 | 503 signals[URL_CLICKED] = g_signal_new("url_clicked", |
504 G_TYPE_FROM_CLASS(gobject_class), | |
505 G_SIGNAL_RUN_FIRST, | |
506 G_STRUCT_OFFSET(GtkIMHtmlClass, url_clicked), | |
507 NULL, | |
508 0, | |
509 g_cclosure_marshal_VOID__POINTER, | |
510 G_TYPE_NONE, 1, | |
511 G_TYPE_POINTER); | |
4032 | 512 gobject_class->finalize = gtk_imhtml_finalize; |
1428 | 513 } |
514 | |
3922 | 515 static void gtk_imhtml_init (GtkIMHtml *imhtml) |
1428 | 516 { |
3922 | 517 GtkTextIter iter; |
518 imhtml->text_buffer = gtk_text_buffer_new(NULL); | |
519 gtk_text_buffer_get_end_iter (imhtml->text_buffer, &iter); | |
520 imhtml->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, FALSE); | |
521 gtk_text_view_set_buffer(GTK_TEXT_VIEW(imhtml), imhtml->text_buffer); | |
5105 | 522 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(imhtml), GTK_WRAP_WORD_CHAR); |
3922 | 523 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(imhtml), 5); |
524 /*gtk_text_view_set_justification(GTK_TEXT_VIEW(imhtml), GTK_JUSTIFY_FILL);*/ | |
3465 | 525 |
3922 | 526 /* These tags will be used often and can be reused--we create them on init and then apply them by name |
527 * other tags (color, size, face, etc.) will have to be created and applied dynamically */ | |
528 gtk_text_buffer_create_tag(imhtml->text_buffer, "BOLD", "weight", PANGO_WEIGHT_BOLD, NULL); | |
529 gtk_text_buffer_create_tag(imhtml->text_buffer, "ITALICS", "style", PANGO_STYLE_ITALIC, NULL); | |
530 gtk_text_buffer_create_tag(imhtml->text_buffer, "UNDERLINE", "underline", PANGO_UNDERLINE_SINGLE, NULL); | |
531 gtk_text_buffer_create_tag(imhtml->text_buffer, "STRIKE", "strikethrough", TRUE, NULL); | |
532 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUB", "rise", -5000, NULL); | |
533 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUP", "rise", 5000, NULL); | |
534 gtk_text_buffer_create_tag(imhtml->text_buffer, "PRE", "family", "Monospace", NULL); | |
7295 | 535 gtk_text_buffer_create_tag(imhtml->text_buffer, "search", "background", "#22ff00", "weight", "bold", NULL); |
7707 | 536 gtk_text_buffer_create_tag(imhtml->text_buffer, "LINK", "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); |
3922 | 537 /* When hovering over a link, we show the hand cursor--elsewhere we show the plain ol' pointer cursor */ |
538 imhtml->hand_cursor = gdk_cursor_new (GDK_HAND2); | |
539 imhtml->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); | |
7694 | 540 imhtml->text_cursor = gdk_cursor_new (GDK_XTERM); |
2993 | 541 |
4253 | 542 imhtml->show_smileys = TRUE; |
6124 | 543 imhtml->show_comments = TRUE; |
4253 | 544 |
4892 | 545 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 546 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4032 | 547 imhtml->default_smilies = gtk_smiley_tree_new(); |
4735 | 548 |
4944 | 549 g_signal_connect(G_OBJECT(imhtml), "size-allocate", G_CALLBACK(gtk_size_allocate_cb), NULL); |
4735 | 550 g_signal_connect(G_OBJECT(imhtml), "motion-notify-event", G_CALLBACK(gtk_motion_event_notify), NULL); |
4944 | 551 g_signal_connect(G_OBJECT(imhtml), "leave-notify-event", G_CALLBACK(gtk_leave_event_notify), NULL); |
6066 | 552 g_signal_connect(G_OBJECT(imhtml), "key_press_event", G_CALLBACK(gtk_key_pressed_cb), NULL); |
7694 | 553 g_signal_connect_after(G_OBJECT(imhtml->text_buffer), "insert-text", G_CALLBACK(insert_cb), imhtml); |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
554 #if GTK_CHECK_VERSION(2,2,0) |
7353 | 555 g_signal_connect(G_OBJECT(imhtml), "copy-clipboard", G_CALLBACK(copy_clipboard_cb), NULL); |
7766 | 556 g_signal_connect(G_OBJECT(imhtml), "paste-clipboard", G_CALLBACK(paste_clipboard_cb), NULL); |
7346 | 557 g_signal_connect(G_OBJECT(imhtml), "button-release-event", G_CALLBACK(button_release_cb), imhtml); |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
558 #endif |
4944 | 559 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); |
4735 | 560 |
561 imhtml->tip = NULL; | |
562 imhtml->tip_timer = 0; | |
563 imhtml->tip_window = NULL; | |
4895 | 564 |
7694 | 565 imhtml->edit.bold = NULL; |
566 imhtml->edit.italic = NULL; | |
567 imhtml->edit.underline = NULL; | |
7714 | 568 imhtml->edit.forecolor = NULL; |
569 imhtml->edit.backcolor = NULL; | |
7717 | 570 imhtml->edit.fontface = NULL; |
7740 | 571 imhtml->edit.sizespan = NULL; |
572 imhtml->edit.fontsize = 3; | |
7714 | 573 |
7694 | 574 imhtml->format_spans = NULL; |
575 | |
4895 | 576 imhtml->scalables = NULL; |
7750 | 577 |
7694 | 578 gtk_imhtml_set_editable(imhtml, FALSE); |
7749 | 579 |
2993 | 580 } |
581 | |
3922 | 582 GtkWidget *gtk_imhtml_new(void *a, void *b) |
1428 | 583 { |
4635 | 584 return GTK_WIDGET(g_object_new(gtk_imhtml_get_type(), NULL)); |
1428 | 585 } |
586 | |
4635 | 587 GType gtk_imhtml_get_type() |
1428 | 588 { |
4635 | 589 static GType imhtml_type = 0; |
1428 | 590 |
591 if (!imhtml_type) { | |
4635 | 592 static const GTypeInfo imhtml_info = { |
593 sizeof(GtkIMHtmlClass), | |
594 NULL, | |
595 NULL, | |
596 (GClassInitFunc) gtk_imhtml_class_init, | |
597 NULL, | |
598 NULL, | |
1428 | 599 sizeof (GtkIMHtml), |
4635 | 600 0, |
601 (GInstanceInitFunc) gtk_imhtml_init | |
1428 | 602 }; |
4635 | 603 |
604 imhtml_type = g_type_register_static(gtk_text_view_get_type(), | |
605 "GtkIMHtml", &imhtml_info, 0); | |
1428 | 606 } |
607 | |
608 return imhtml_type; | |
609 } | |
610 | |
4417 | 611 struct url_data { |
612 GObject *object; | |
613 gchar *url; | |
614 }; | |
615 | |
616 static void url_open(GtkWidget *w, struct url_data *data) { | |
617 if(!data) return; | |
618 | |
619 g_signal_emit(data->object, signals[URL_CLICKED], 0, data->url); | |
620 | |
621 g_object_unref(data->object); | |
622 g_free(data->url); | |
623 g_free(data); | |
624 } | |
5582 | 625 |
4417 | 626 static void url_copy(GtkWidget *w, gchar *url) { |
627 GtkClipboard *clipboard; | |
628 | |
5293
ead927e2543f
[gaim-migrate @ 5665]
Christian Hammond <chipx86@chipx86.com>
parents:
5282
diff
changeset
|
629 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
4417 | 630 gtk_clipboard_set_text(clipboard, url, -1); |
5582 | 631 |
632 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); | |
633 gtk_clipboard_set_text(clipboard, url, -1); | |
4417 | 634 } |
635 | |
636 /* The callback for an event on a link tag. */ | |
5091 | 637 gboolean tag_event(GtkTextTag *tag, GObject *imhtml, GdkEvent *event, GtkTextIter *arg2, char *url) { |
4417 | 638 GdkEventButton *event_button = (GdkEventButton *) event; |
639 | |
3922 | 640 if (event->type == GDK_BUTTON_RELEASE) { |
4417 | 641 if (event_button->button == 1) { |
642 GtkTextIter start, end; | |
643 /* we shouldn't open a URL if the user has selected something: */ | |
644 gtk_text_buffer_get_selection_bounds( | |
645 gtk_text_iter_get_buffer(arg2), &start, &end); | |
646 if(gtk_text_iter_get_offset(&start) != | |
647 gtk_text_iter_get_offset(&end)) | |
648 return FALSE; | |
649 | |
650 /* A link was clicked--we emit the "url_clicked" signal | |
651 * with the URL as the argument */ | |
5091 | 652 g_signal_emit(imhtml, signals[URL_CLICKED], 0, url); |
4417 | 653 return FALSE; |
654 } else if(event_button->button == 3) { | |
4745 | 655 GtkWidget *img, *item, *menu; |
4417 | 656 struct url_data *tempdata = g_new(struct url_data, 1); |
5091 | 657 tempdata->object = g_object_ref(imhtml); |
4417 | 658 tempdata->url = g_strdup(url); |
4745 | 659 |
5091 | 660 /* Don't want the tooltip around if user right-clicked on link */ |
661 if (GTK_IMHTML(imhtml)->tip_window) { | |
662 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
663 GTK_IMHTML(imhtml)->tip_window = NULL; | |
664 } | |
665 if (GTK_IMHTML(imhtml)->tip_timer) { | |
666 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
667 GTK_IMHTML(imhtml)->tip_timer = 0; | |
668 } | |
7694 | 669 if (GTK_IMHTML(imhtml)->editable) |
670 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->text_cursor); | |
671 else | |
672 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
4417 | 673 menu = gtk_menu_new(); |
4745 | 674 |
4417 | 675 /* buttons and such */ |
676 | |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
677 if (!strncmp(url, "mailto:", 7)) |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
678 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
679 /* Copy E-Mail Address */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
680 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
681 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
682 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
683 _("_Copy E-Mail Address")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
684 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
685 g_signal_connect(G_OBJECT(item), "activate", |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
686 G_CALLBACK(url_copy), url + 7); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
687 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
688 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
689 else |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
690 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
691 /* Copy Link Location */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
692 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
693 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
694 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
695 _("_Copy Link Location")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
696 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
697 g_signal_connect(G_OBJECT(item), "activate", |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
698 G_CALLBACK(url_copy), url); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
699 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
700 |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
701 /* Open Link in Browser */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
702 img = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
703 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
704 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
705 _("_Open Link in Browser")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
706 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
707 g_signal_connect(G_OBJECT(item), "activate", |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
708 G_CALLBACK(url_open), tempdata); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
709 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
710 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
711 |
4756 | 712 |
4417 | 713 gtk_widget_show_all(menu); |
4756 | 714 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, |
715 event_button->button, event_button->time); | |
4745 | 716 |
4417 | 717 return TRUE; |
718 } | |
1428 | 719 } |
4417 | 720 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) |
721 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
722 be caught by the regular GtkTextView menu */ | |
723 else | |
724 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1428 | 725 } |
726 | |
4298 | 727 /* this isn't used yet |
4032 | 728 static void |
4263 | 729 gtk_smiley_tree_remove (GtkSmileyTree *tree, |
730 GtkIMHtmlSmiley *smiley) | |
4032 | 731 { |
732 GtkSmileyTree *t = tree; | |
4263 | 733 const gchar *x = smiley->smile; |
4032 | 734 gint len = 0; |
735 | |
736 while (*x) { | |
737 gchar *pos; | |
738 | |
739 if (!t->values) | |
740 return; | |
741 | |
742 pos = strchr (t->values->str, *x); | |
743 if (pos) | |
744 t = t->children [(int) pos - (int) t->values->str]; | |
745 else | |
746 return; | |
747 | |
748 x++; len++; | |
749 } | |
750 | |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
751 if (t->image) { |
4032 | 752 t->image = NULL; |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
753 } |
4032 | 754 } |
4298 | 755 */ |
756 | |
4032 | 757 |
758 static gint | |
759 gtk_smiley_tree_lookup (GtkSmileyTree *tree, | |
760 const gchar *text) | |
761 { | |
762 GtkSmileyTree *t = tree; | |
763 const gchar *x = text; | |
764 gint len = 0; | |
765 | |
766 while (*x) { | |
767 gchar *pos; | |
768 | |
769 if (!t->values) | |
770 break; | |
771 | |
772 pos = strchr (t->values->str, *x); | |
773 if (pos) | |
7371 | 774 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 775 else |
776 break; | |
777 | |
778 x++; len++; | |
779 } | |
780 | |
781 if (t->image) | |
782 return len; | |
783 | |
784 return 0; | |
785 } | |
786 | |
787 void | |
4263 | 788 gtk_imhtml_associate_smiley (GtkIMHtml *imhtml, |
789 gchar *sml, | |
790 GtkIMHtmlSmiley *smiley) | |
4032 | 791 { |
792 GtkSmileyTree *tree; | |
793 g_return_if_fail (imhtml != NULL); | |
794 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
7371 | 795 |
4032 | 796 if (sml == NULL) |
797 tree = imhtml->default_smilies; | |
798 else if ((tree = g_hash_table_lookup(imhtml->smiley_data, sml))) { | |
799 } else { | |
800 tree = gtk_smiley_tree_new(); | |
4892 | 801 g_hash_table_insert(imhtml->smiley_data, g_strdup(sml), tree); |
4032 | 802 } |
803 | |
4263 | 804 gtk_smiley_tree_insert (tree, smiley); |
4032 | 805 } |
806 | |
807 static gboolean | |
808 gtk_imhtml_is_smiley (GtkIMHtml *imhtml, | |
809 GSList *fonts, | |
810 const gchar *text, | |
811 gint *len) | |
812 { | |
813 GtkSmileyTree *tree; | |
5967 | 814 GtkIMHtmlFontDetail *font; |
4032 | 815 char *sml = NULL; |
816 | |
817 if (fonts) { | |
818 font = fonts->data; | |
819 sml = font->sml; | |
820 } | |
821 | |
822 if (sml == NULL) | |
823 tree = imhtml->default_smilies; | |
824 else { | |
825 tree = g_hash_table_lookup(imhtml->smiley_data, sml); | |
826 } | |
827 if (tree == NULL) | |
828 return FALSE; | |
7371 | 829 |
4032 | 830 *len = gtk_smiley_tree_lookup (tree, text); |
831 return (*len > 0); | |
832 } | |
833 | |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
834 GdkPixbufAnimation * |
4032 | 835 gtk_smiley_tree_image (GtkIMHtml *imhtml, |
836 const gchar *sml, | |
837 const gchar *text) | |
838 { | |
839 GtkSmileyTree *t; | |
840 const gchar *x = text; | |
841 if (sml == NULL) | |
842 t = imhtml->default_smilies; | |
7371 | 843 else |
4032 | 844 t = g_hash_table_lookup(imhtml->smiley_data, sml); |
7371 | 845 |
4032 | 846 |
847 if (t == NULL) | |
848 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
849 | |
850 while (*x) { | |
851 gchar *pos; | |
852 | |
853 if (!t->values) { | |
854 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
855 } | |
7371 | 856 |
4032 | 857 pos = strchr (t->values->str, *x); |
858 if (pos) { | |
7371 | 859 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 860 } else { |
861 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
862 } | |
863 x++; | |
864 } | |
865 | |
4263 | 866 if (!t->image->icon) |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
867 t->image->icon = gdk_pixbuf_animation_new_from_file(t->image->file, NULL); |
4263 | 868 |
869 return t->image->icon; | |
4032 | 870 } |
4793 | 871 #define VALID_TAG(x) if (!g_ascii_strncasecmp (string, x ">", strlen (x ">"))) { \ |
3922 | 872 *tag = g_strndup (string, strlen (x)); \ |
873 *len = strlen (x) + 1; \ | |
874 return TRUE; \ | |
875 } \ | |
876 (*type)++ | |
1428 | 877 |
4793 | 878 #define VALID_OPT_TAG(x) if (!g_ascii_strncasecmp (string, x " ", strlen (x " "))) { \ |
3922 | 879 const gchar *c = string + strlen (x " "); \ |
880 gchar e = '"'; \ | |
881 gboolean quote = FALSE; \ | |
882 while (*c) { \ | |
883 if (*c == '"' || *c == '\'') { \ | |
884 if (quote && (*c == e)) \ | |
885 quote = !quote; \ | |
886 else if (!quote) { \ | |
887 quote = !quote; \ | |
888 e = *c; \ | |
889 } \ | |
890 } else if (!quote && (*c == '>')) \ | |
891 break; \ | |
892 c++; \ | |
893 } \ | |
894 if (*c) { \ | |
895 *tag = g_strndup (string, c - string); \ | |
896 *len = c - string + 1; \ | |
897 return TRUE; \ | |
898 } \ | |
899 } \ | |
900 (*type)++ | |
1428 | 901 |
902 | |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
903 static gboolean |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
904 gtk_imhtml_is_amp_escape (const gchar *string, |
7280 | 905 gchar **replace, |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
906 gint *length) |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
907 { |
7287 | 908 static char buf[7]; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
909 g_return_val_if_fail (string != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
910 g_return_val_if_fail (replace != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
911 g_return_val_if_fail (length != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
912 |
4793 | 913 if (!g_ascii_strncasecmp (string, "&", 5)) { |
7280 | 914 *replace = "&"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
915 *length = 5; |
4793 | 916 } else if (!g_ascii_strncasecmp (string, "<", 4)) { |
7280 | 917 *replace = "<"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
918 *length = 4; |
4793 | 919 } else if (!g_ascii_strncasecmp (string, ">", 4)) { |
7280 | 920 *replace = ">"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
921 *length = 4; |
4793 | 922 } else if (!g_ascii_strncasecmp (string, " ", 6)) { |
7280 | 923 *replace = " "; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
924 *length = 6; |
4793 | 925 } else if (!g_ascii_strncasecmp (string, "©", 6)) { |
7280 | 926 *replace = "©"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
927 *length = 6; |
4793 | 928 } else if (!g_ascii_strncasecmp (string, """, 6)) { |
7280 | 929 *replace = "\""; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
930 *length = 6; |
4793 | 931 } else if (!g_ascii_strncasecmp (string, "®", 5)) { |
7280 | 932 *replace = "®"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
933 *length = 5; |
5093 | 934 } else if (!g_ascii_strncasecmp (string, "'", 6)) { |
7280 | 935 *replace = "\'"; |
5093 | 936 *length = 6; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
937 } else if (*(string + 1) == '#') { |
2022
199ba82faacb
[gaim-migrate @ 2032]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2002
diff
changeset
|
938 guint pound = 0; |
3004 | 939 if ((sscanf (string, "&#%u;", £) == 1) && pound != 0) { |
7287 | 940 int buflen; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
941 if (*(string + 3 + (gint)log10 (pound)) != ';') |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
942 return FALSE; |
7287 | 943 buflen = g_unichar_to_utf8((gunichar)pound, buf); |
944 buf[buflen] = '\0'; | |
7280 | 945 *replace = buf; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
946 *length = 2; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
947 while (isdigit ((gint) string [*length])) (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
948 if (string [*length] == ';') (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
949 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
950 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
951 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
952 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
953 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
954 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
955 |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
956 return TRUE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
957 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
958 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
959 static gboolean |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
960 gtk_imhtml_is_tag (const gchar *string, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
961 gchar **tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
962 gint *len, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
963 gint *type) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
964 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
965 *type = 1; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
966 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
967 if (!strchr (string, '>')) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
968 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
969 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
970 VALID_TAG ("B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
971 VALID_TAG ("BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
972 VALID_TAG ("/B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
973 VALID_TAG ("/BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
974 VALID_TAG ("I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
975 VALID_TAG ("ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
976 VALID_TAG ("/I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
977 VALID_TAG ("/ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
978 VALID_TAG ("U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
979 VALID_TAG ("UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
980 VALID_TAG ("/U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
981 VALID_TAG ("/UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
982 VALID_TAG ("S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
983 VALID_TAG ("STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
984 VALID_TAG ("/S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
985 VALID_TAG ("/STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
986 VALID_TAG ("SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
987 VALID_TAG ("/SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
988 VALID_TAG ("SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
989 VALID_TAG ("/SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
990 VALID_TAG ("PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
991 VALID_TAG ("/PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
992 VALID_TAG ("TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
993 VALID_TAG ("/TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
994 VALID_TAG ("BR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
995 VALID_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
996 VALID_TAG ("/FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
997 VALID_TAG ("/A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
998 VALID_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
999 VALID_TAG ("/P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1000 VALID_TAG ("H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1001 VALID_TAG ("/H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1002 VALID_TAG ("HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1003 VALID_TAG ("/HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1004 VALID_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1005 VALID_TAG ("/BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1006 VALID_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1007 VALID_TAG ("HEAD"); |
2993 | 1008 VALID_TAG ("/HEAD"); |
1009 VALID_TAG ("BINARY"); | |
1010 VALID_TAG ("/BINARY"); | |
5093 | 1011 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1012 VALID_OPT_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1013 VALID_OPT_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1014 VALID_OPT_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1015 VALID_OPT_TAG ("A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1016 VALID_OPT_TAG ("IMG"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1017 VALID_OPT_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1018 VALID_OPT_TAG ("H3"); |
5093 | 1019 VALID_OPT_TAG ("HTML"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1020 |
5101 | 1021 VALID_TAG ("CITE"); |
1022 VALID_TAG ("/CITE"); | |
1023 VALID_TAG ("EM"); | |
1024 VALID_TAG ("/EM"); | |
1025 VALID_TAG ("STRONG"); | |
1026 VALID_TAG ("/STRONG"); | |
1027 | |
5104 | 1028 VALID_OPT_TAG ("SPAN"); |
1029 VALID_TAG ("/SPAN"); | |
5174 | 1030 VALID_TAG ("BR/"); /* hack until gtkimhtml handles things better */ |
6982 | 1031 VALID_TAG ("IMG"); |
5104 | 1032 |
4793 | 1033 if (!g_ascii_strncasecmp(string, "!--", strlen ("!--"))) { |
2954
f6c4f2187c08
[gaim-migrate @ 2967]
Christian Hammond <chipx86@chipx86.com>
parents:
2898
diff
changeset
|
1034 gchar *e = strstr (string + strlen("!--"), "-->"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1035 if (e) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1036 *len = e - string + strlen ("-->"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1037 *tag = g_strndup (string + strlen ("!--"), *len - strlen ("!---->")); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1038 return TRUE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1039 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1040 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1041 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1042 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1043 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1044 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1045 static gchar* |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1046 gtk_imhtml_get_html_opt (gchar *tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1047 const gchar *opt) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1048 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1049 gchar *t = tag; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1050 gchar *e, *a; |
5177 | 1051 gchar *val; |
1052 gint len; | |
7280 | 1053 gchar *c; |
5177 | 1054 GString *ret; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1055 |
4793 | 1056 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1057 gboolean quote = FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1058 if (*t == '\0') break; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1059 while (*t && !((*t == ' ') && !quote)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1060 if (*t == '\"') |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1061 quote = ! quote; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1062 t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1063 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1064 while (*t && (*t == ' ')) t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1065 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1066 |
4793 | 1067 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1068 t += strlen (opt); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1069 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1070 return NULL; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1071 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1072 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1073 if ((*t == '\"') || (*t == '\'')) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1074 e = a = ++t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1075 while (*e && (*e != *(t - 1))) e++; |
2993 | 1076 if (*e == '\0') { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1077 return NULL; |
5177 | 1078 } else |
1079 val = g_strndup(a, e - a); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1080 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1081 e = a = t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1082 while (*e && !isspace ((gint) *e)) e++; |
5177 | 1083 val = g_strndup(a, e - a); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1084 } |
5177 | 1085 |
1086 ret = g_string_new(""); | |
1087 e = val; | |
1088 while(*e) { | |
1089 if(gtk_imhtml_is_amp_escape(e, &c, &len)) { | |
7280 | 1090 ret = g_string_append(ret, c); |
5177 | 1091 e += len; |
1092 } else { | |
1093 ret = g_string_append_c(ret, *e); | |
1094 e++; | |
1095 } | |
1096 } | |
1097 | |
1098 g_free(val); | |
1099 val = ret->str; | |
1100 g_string_free(ret, FALSE); | |
1101 return val; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1102 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1103 |
3922 | 1104 |
1105 | |
1106 #define NEW_TEXT_BIT 0 | |
4343 | 1107 #define NEW_COMMENT_BIT 2 |
4895 | 1108 #define NEW_SCALABLE_BIT 1 |
3922 | 1109 #define NEW_BIT(x) ws [wpos] = '\0'; \ |
1110 mark2 = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); \ | |
1111 gtk_text_buffer_insert(imhtml->text_buffer, &iter, ws, -1); \ | |
4895 | 1112 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ |
3922 | 1113 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, mark2); \ |
1114 gtk_text_buffer_delete_mark(imhtml->text_buffer, mark2); \ | |
1115 if (bold) \ | |
1116 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &siter, &iter); \ | |
4895 | 1117 if (italics) \ |
3922 | 1118 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &siter, &iter); \ |
1119 if (underline) \ | |
1120 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &siter, &iter); \ | |
1121 if (strike) \ | |
1122 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &siter, &iter); \ | |
1123 if (sub) \ | |
1124 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUB", &siter, &iter); \ | |
1125 if (sup) \ | |
1126 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUP", &siter, &iter); \ | |
1127 if (pre) \ | |
1128 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "PRE", &siter, &iter); \ | |
1129 if (bg) { \ | |
1130 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", bg, NULL); \ | |
1131 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
1132 } \ | |
1133 if (fonts) { \ | |
5967 | 1134 GtkIMHtmlFontDetail *fd = fonts->data; \ |
3922 | 1135 if (fd->fore) { \ |
1136 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", fd->fore, NULL); \ | |
1137 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
1138 } \ | |
1139 if (fd->back) { \ | |
1140 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", fd->back, NULL); \ | |
1141 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
1142 } \ | |
1143 if (fd->face) { \ | |
6648 | 1144 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "family", fd->face, NULL); \ |
3922 | 1145 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ |
1146 } \ | |
1147 if (fd->size) { \ | |
5118 | 1148 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "size-points", (double)POINT_SIZE(fd->size), NULL); \ |
3922 | 1149 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ |
1150 } \ | |
1151 } \ | |
1152 if (url) { \ | |
7732 | 1153 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "LINK", &siter, &iter); \ |
1154 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); \ | |
5012 | 1155 g_signal_connect(G_OBJECT(texttag), "event", G_CALLBACK(tag_event), g_strdup(url)); \ |
7732 | 1156 g_object_set_data(G_OBJECT(texttag), "link_url", g_strdup(url)); \ |
4735 | 1157 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ |
3922 | 1158 } \ |
1159 wpos = 0; \ | |
1160 ws[0] = 0; \ | |
1161 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4895 | 1162 if (x == NEW_SCALABLE_BIT) { \ |
1163 GdkRectangle rect; \ | |
1164 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); \ | |
1165 scalable->add_to(scalable, imhtml, &iter); \ | |
1166 scalable->scale(scalable, rect.width, rect.height); \ | |
1167 imhtml->scalables = g_list_append(imhtml->scalables, scalable); \ | |
1168 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4343 | 1169 } \ |
3922 | 1170 |
4895 | 1171 |
1172 | |
6982 | 1173 GString* gtk_imhtml_append_text_with_images (GtkIMHtml *imhtml, |
1174 const gchar *text, | |
1175 GtkIMHtmlOptions options, | |
1176 GSList *images) | |
1428 | 1177 { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1178 gint pos = 0; |
3922 | 1179 GString *str = NULL; |
1180 GtkTextIter iter, siter; | |
1181 GtkTextMark *mark, *mark2; | |
7707 | 1182 GtkTextTag *texttag = NULL; |
3922 | 1183 gchar *ws; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1184 gchar *tag; |
3922 | 1185 gchar *url = NULL; |
1186 gchar *bg = NULL; | |
6982 | 1187 gint len; |
4032 | 1188 gint tlen, smilelen, wpos=0; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1189 gint type; |
3922 | 1190 const gchar *c; |
7280 | 1191 gchar *amp; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1192 |
1428 | 1193 guint bold = 0, |
1194 italics = 0, | |
1195 underline = 0, | |
1196 strike = 0, | |
1197 sub = 0, | |
1198 sup = 0, | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1199 title = 0, |
3922 | 1200 pre = 0; |
1428 | 1201 |
3922 | 1202 GSList *fonts = NULL; |
1428 | 1203 |
4612 | 1204 GdkRectangle rect; |
1205 int y, height; | |
1206 | |
4895 | 1207 GtkIMHtmlScalable *scalable = NULL; |
1208 | |
1428 | 1209 g_return_val_if_fail (imhtml != NULL, NULL); |
1210 g_return_val_if_fail (GTK_IS_IMHTML (imhtml), NULL); | |
1211 g_return_val_if_fail (text != NULL, NULL); | |
6982 | 1212 |
3922 | 1213 c = text; |
6982 | 1214 len = strlen(text); |
3922 | 1215 ws = g_malloc(len + 1); |
1216 ws[0] = 0; | |
1428 | 1217 |
1218 if (options & GTK_IMHTML_RETURN_LOG) | |
3922 | 1219 str = g_string_new(""); |
1428 | 1220 |
3922 | 1221 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); |
1222 mark = gtk_text_buffer_create_mark (imhtml->text_buffer, NULL, &iter, /* right grav */ FALSE); | |
4612 | 1223 |
1224 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
1225 gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
1226 | |
1227 if(((y + height) - (rect.y + rect.height)) > height | |
1228 && gtk_text_buffer_get_char_count(imhtml->text_buffer)){ | |
1229 options |= GTK_IMHTML_NO_SCROLL; | |
1230 } | |
1231 | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1232 while (pos < len) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1233 if (*c == '<' && gtk_imhtml_is_tag (c + 1, &tag, &tlen, &type)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1234 c++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1235 pos++; |
3922 | 1236 switch (type) |
1237 { | |
1238 case 1: /* B */ | |
1239 case 2: /* BOLD */ | |
5101 | 1240 case 54: /* STRONG */ |
3922 | 1241 NEW_BIT (NEW_TEXT_BIT); |
1242 bold++; | |
1243 break; | |
1244 case 3: /* /B */ | |
1245 case 4: /* /BOLD */ | |
5101 | 1246 case 55: /* /STRONG */ |
3922 | 1247 NEW_BIT (NEW_TEXT_BIT); |
1248 if (bold) | |
1249 bold--; | |
1250 break; | |
1251 case 5: /* I */ | |
1252 case 6: /* ITALIC */ | |
5101 | 1253 case 52: /* EM */ |
3922 | 1254 NEW_BIT (NEW_TEXT_BIT); |
1255 italics++; | |
1256 break; | |
1257 case 7: /* /I */ | |
1258 case 8: /* /ITALIC */ | |
5101 | 1259 case 53: /* /EM */ |
3922 | 1260 NEW_BIT (NEW_TEXT_BIT); |
1261 if (italics) | |
1262 italics--; | |
1263 break; | |
1264 case 9: /* U */ | |
1265 case 10: /* UNDERLINE */ | |
1266 NEW_BIT (NEW_TEXT_BIT); | |
1267 underline++; | |
1268 break; | |
1269 case 11: /* /U */ | |
1270 case 12: /* /UNDERLINE */ | |
1271 NEW_BIT (NEW_TEXT_BIT); | |
1272 if (underline) | |
1273 underline--; | |
1274 break; | |
1275 case 13: /* S */ | |
1276 case 14: /* STRIKE */ | |
1277 NEW_BIT (NEW_TEXT_BIT); | |
1278 strike++; | |
1279 break; | |
1280 case 15: /* /S */ | |
1281 case 16: /* /STRIKE */ | |
1282 NEW_BIT (NEW_TEXT_BIT); | |
1283 if (strike) | |
1284 strike--; | |
1285 break; | |
1286 case 17: /* SUB */ | |
1287 NEW_BIT (NEW_TEXT_BIT); | |
1288 sub++; | |
1289 break; | |
1290 case 18: /* /SUB */ | |
1291 NEW_BIT (NEW_TEXT_BIT); | |
1292 if (sub) | |
1293 sub--; | |
1294 break; | |
1295 case 19: /* SUP */ | |
1296 NEW_BIT (NEW_TEXT_BIT); | |
1297 sup++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1298 break; |
3922 | 1299 case 20: /* /SUP */ |
1300 NEW_BIT (NEW_TEXT_BIT); | |
1301 if (sup) | |
1302 sup--; | |
1303 break; | |
1304 case 21: /* PRE */ | |
1305 NEW_BIT (NEW_TEXT_BIT); | |
1306 pre++; | |
1307 break; | |
1308 case 22: /* /PRE */ | |
1309 NEW_BIT (NEW_TEXT_BIT); | |
1310 if (pre) | |
1311 pre--; | |
1312 break; | |
1313 case 23: /* TITLE */ | |
1314 NEW_BIT (NEW_TEXT_BIT); | |
1315 title++; | |
1316 break; | |
1317 case 24: /* /TITLE */ | |
1318 if (title) { | |
1319 if (options & GTK_IMHTML_NO_TITLE) { | |
1320 wpos = 0; | |
1321 ws [wpos] = '\0'; | |
1322 } | |
1323 title--; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1324 } |
3922 | 1325 break; |
1326 case 25: /* BR */ | |
5174 | 1327 case 58: /* BR/ */ |
3922 | 1328 ws[wpos] = '\n'; |
1329 wpos++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1330 NEW_BIT (NEW_TEXT_BIT); |
6982 | 1331 break; |
3922 | 1332 case 26: /* HR */ |
1333 case 42: /* HR (opt) */ | |
1334 ws[wpos++] = '\n'; | |
5967 | 1335 scalable = gtk_imhtml_hr_new(); |
4895 | 1336 NEW_BIT(NEW_SCALABLE_BIT); |
4343 | 1337 ws[wpos++] = '\n'; |
3922 | 1338 break; |
1339 case 27: /* /FONT */ | |
1340 if (fonts) { | |
5967 | 1341 GtkIMHtmlFontDetail *font = fonts->data; |
3922 | 1342 NEW_BIT (NEW_TEXT_BIT); |
1343 fonts = g_slist_remove (fonts, font); | |
1344 if (font->face) | |
1345 g_free (font->face); | |
1346 if (font->fore) | |
1347 g_free (font->fore); | |
1348 if (font->back) | |
1349 g_free (font->back); | |
4032 | 1350 if (font->sml) |
1351 g_free (font->sml); | |
3922 | 1352 g_free (font); |
1353 } | |
1354 break; | |
1355 case 28: /* /A */ | |
1356 if (url) { | |
1357 NEW_BIT(NEW_TEXT_BIT); | |
1358 g_free(url); | |
1359 url = NULL; | |
2993 | 1360 break; |
1361 } | |
3922 | 1362 case 29: /* P */ |
1363 case 30: /* /P */ | |
1364 case 31: /* H3 */ | |
1365 case 32: /* /H3 */ | |
1366 case 33: /* HTML */ | |
1367 case 34: /* /HTML */ | |
1368 case 35: /* BODY */ | |
1369 case 36: /* /BODY */ | |
1370 case 37: /* FONT */ | |
1371 case 38: /* HEAD */ | |
1372 case 39: /* /HEAD */ | |
6982 | 1373 case 40: /* BINARY */ |
1374 case 41: /* /BINARY */ | |
3922 | 1375 break; |
1376 case 43: /* FONT (opt) */ | |
1377 { | |
4032 | 1378 gchar *color, *back, *face, *size, *sml; |
5967 | 1379 GtkIMHtmlFontDetail *font, *oldfont = NULL; |
3922 | 1380 color = gtk_imhtml_get_html_opt (tag, "COLOR="); |
1381 back = gtk_imhtml_get_html_opt (tag, "BACK="); | |
1382 face = gtk_imhtml_get_html_opt (tag, "FACE="); | |
1383 size = gtk_imhtml_get_html_opt (tag, "SIZE="); | |
4032 | 1384 sml = gtk_imhtml_get_html_opt (tag, "SML="); |
1385 if (!(color || back || face || size || sml)) | |
3922 | 1386 break; |
1387 | |
1388 NEW_BIT (NEW_TEXT_BIT); | |
1389 | |
5967 | 1390 font = g_new0 (GtkIMHtmlFontDetail, 1); |
3922 | 1391 if (fonts) |
1392 oldfont = fonts->data; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1393 |
3922 | 1394 if (color && !(options & GTK_IMHTML_NO_COLOURS)) |
1395 font->fore = color; | |
1396 else if (oldfont && oldfont->fore) | |
1397 font->fore = g_strdup(oldfont->fore); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1398 |
3922 | 1399 if (back && !(options & GTK_IMHTML_NO_COLOURS)) |
1400 font->back = back; | |
1401 else if (oldfont && oldfont->back) | |
1402 font->back = g_strdup(oldfont->back); | |
1403 | |
1404 if (face && !(options & GTK_IMHTML_NO_FONTS)) | |
1405 font->face = face; | |
1406 else if (oldfont && oldfont->face) | |
1407 font->face = g_strdup(oldfont->face); | |
4629 | 1408 if (font->face && (atoi(font->face) > 100)) { |
1409 g_free(font->face); | |
1410 font->face = g_strdup("100"); | |
1411 } | |
4032 | 1412 |
1413 if (sml) | |
1414 font->sml = sml; | |
1415 else if (oldfont && oldfont->sml) | |
1416 font->sml = g_strdup(oldfont->sml); | |
1417 | |
3922 | 1418 if (size && !(options & GTK_IMHTML_NO_SIZES)) { |
1419 if (*size == '+') { | |
1420 sscanf (size + 1, "%hd", &font->size); | |
1421 font->size += 3; | |
1422 } else if (*size == '-') { | |
1423 sscanf (size + 1, "%hd", &font->size); | |
1424 font->size = MAX (0, 3 - font->size); | |
1425 } else if (isdigit (*size)) { | |
1426 sscanf (size, "%hd", &font->size); | |
1427 } | |
6042 | 1428 if (font->size > 100) |
1429 font->size = 100; | |
3922 | 1430 } else if (oldfont) |
1431 font->size = oldfont->size; | |
1432 g_free(size); | |
1433 fonts = g_slist_prepend (fonts, font); | |
1434 } | |
1435 break; | |
1436 case 44: /* BODY (opt) */ | |
1437 if (!(options & GTK_IMHTML_NO_COLOURS)) { | |
1438 char *bgcolor = gtk_imhtml_get_html_opt (tag, "BGCOLOR="); | |
1439 if (bgcolor) { | |
1440 NEW_BIT(NEW_TEXT_BIT); | |
1441 if (bg) | |
1442 g_free(bg); | |
1443 bg = bgcolor; | |
2885
f72efa29c109
[gaim-migrate @ 2898]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2871
diff
changeset
|
1444 } |
1428 | 1445 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1446 break; |
3922 | 1447 case 45: /* A (opt) */ |
1448 { | |
1449 gchar *href = gtk_imhtml_get_html_opt (tag, "HREF="); | |
1450 if (href) { | |
1451 NEW_BIT (NEW_TEXT_BIT); | |
1452 if (url) | |
1453 g_free (url); | |
1454 url = href; | |
1455 } | |
2993 | 1456 } |
3922 | 1457 break; |
4895 | 1458 case 46: /* IMG (opt) */ |
6982 | 1459 case 59: /* IMG */ |
4895 | 1460 { |
6982 | 1461 GdkPixbuf *img = NULL; |
1462 const gchar *filename = NULL; | |
4895 | 1463 |
6982 | 1464 if (images && images->data) { |
1465 img = images->data; | |
1466 images = images->next; | |
1467 filename = g_object_get_data(G_OBJECT(img), "filename"); | |
1468 g_object_ref(G_OBJECT(img)); | |
1469 } else { | |
1470 img = gtk_widget_render_icon(GTK_WIDGET(imhtml), | |
1471 GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON, | |
1472 "gtkimhtml-missing-image"); | |
1473 } | |
4895 | 1474 |
6982 | 1475 scalable = gtk_imhtml_image_new(img, filename); |
1476 NEW_BIT(NEW_SCALABLE_BIT); | |
1477 g_object_unref(G_OBJECT(img)); | |
4895 | 1478 } |
3922 | 1479 case 47: /* P (opt) */ |
1480 case 48: /* H3 (opt) */ | |
5093 | 1481 case 49: /* HTML (opt) */ |
5101 | 1482 case 50: /* CITE */ |
1483 case 51: /* /CITE */ | |
5104 | 1484 case 56: /* SPAN */ |
1485 case 57: /* /SPAN */ | |
2993 | 1486 break; |
6982 | 1487 case 60: /* comment */ |
3922 | 1488 NEW_BIT (NEW_TEXT_BIT); |
6124 | 1489 if (imhtml->show_comments) |
1490 wpos = g_snprintf (ws, len, "%s", tag); | |
3922 | 1491 NEW_BIT (NEW_COMMENT_BIT); |
1492 break; | |
1493 default: | |
6882 | 1494 break; |
2993 | 1495 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1496 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1497 pos += tlen; |
4138 | 1498 if(tag) |
1499 g_free(tag); /* This was allocated back in VALID_TAG() */ | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1500 } else if (*c == '&' && gtk_imhtml_is_amp_escape (c, &, &tlen)) { |
7280 | 1501 while(*amp) { |
1502 ws [wpos++] = *amp++; | |
1503 } | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1504 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1505 pos += tlen; |
1428 | 1506 } else if (*c == '\n') { |
1507 if (!(options & GTK_IMHTML_NO_NEWLINE)) { | |
3922 | 1508 ws[wpos] = '\n'; |
1509 wpos++; | |
1428 | 1510 NEW_BIT (NEW_TEXT_BIT); |
1511 } | |
1512 c++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1513 pos++; |
4253 | 1514 } else if (imhtml->show_smileys && (gtk_imhtml_is_smiley (imhtml, fonts, c, &smilelen) || gtk_imhtml_is_smiley(imhtml, NULL, c, &smilelen))) { |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
1515 GtkTextChildAnchor *anchor; |
6882 | 1516 GtkWidget *icon = NULL; |
7344 | 1517 GtkTextIter copy; |
6882 | 1518 GdkPixbufAnimation *annipixbuf = NULL; |
1519 GdkPixbuf *pixbuf = NULL; | |
5967 | 1520 GtkIMHtmlFontDetail *fd; |
7346 | 1521 |
4032 | 1522 gchar *sml = NULL; |
1523 if (fonts) { | |
1524 fd = fonts->data; | |
1525 sml = fd->sml; | |
1526 } | |
1527 NEW_BIT (NEW_TEXT_BIT); | |
1528 wpos = g_snprintf (ws, smilelen + 1, "%s", c); | |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
1529 anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, &iter); |
6882 | 1530 annipixbuf = gtk_smiley_tree_image(imhtml, sml, ws); |
1531 if(annipixbuf) { | |
1532 if(gdk_pixbuf_animation_is_static_image(annipixbuf)) { | |
1533 pixbuf = gdk_pixbuf_animation_get_static_image(annipixbuf); | |
1534 if(pixbuf) | |
1535 icon = gtk_image_new_from_pixbuf(pixbuf); | |
1536 } else { | |
1537 icon = gtk_image_new_from_animation(annipixbuf); | |
1538 } | |
1539 } | |
1540 | |
1541 if (icon) { | |
6839
bdc4e73f354d
[gaim-migrate @ 7384]
Christian Hammond <chipx86@chipx86.com>
parents:
6814
diff
changeset
|
1542 gtk_widget_show(icon); |
bdc4e73f354d
[gaim-migrate @ 7384]
Christian Hammond <chipx86@chipx86.com>
parents:
6814
diff
changeset
|
1543 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), icon, anchor); |
bdc4e73f354d
[gaim-migrate @ 7384]
Christian Hammond <chipx86@chipx86.com>
parents:
6814
diff
changeset
|
1544 } |
7344 | 1545 |
1546 copy = iter; | |
1547 gtk_text_iter_backward_char(©); | |
1548 if (bg) { | |
1549 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", bg, NULL); | |
1550 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &iter, ©); | |
1551 } | |
1552 if (fonts) { | |
1553 GtkIMHtmlFontDetail *fd = fonts->data; | |
1554 if (fd->back) { | |
1555 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", fd->back, NULL); | |
1556 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &iter, ©); | |
1557 } | |
1558 } | |
4032 | 1559 c += smilelen; |
1560 pos += smilelen; | |
1561 wpos = 0; | |
1562 ws[0] = 0; | |
1563 } else if (*c) { | |
1428 | 1564 ws [wpos++] = *c++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1565 pos++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1566 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1567 break; |
1428 | 1568 } |
1569 } | |
3922 | 1570 |
1571 NEW_BIT(NEW_TEXT_BIT); | |
1428 | 1572 if (url) { |
1573 g_free (url); | |
3922 | 1574 if (str) |
1575 str = g_string_append (str, "</A>"); | |
1428 | 1576 } |
3922 | 1577 |
4032 | 1578 while (fonts) { |
5967 | 1579 GtkIMHtmlFontDetail *font = fonts->data; |
4032 | 1580 fonts = g_slist_remove (fonts, font); |
1581 if (font->face) | |
1582 g_free (font->face); | |
1583 if (font->fore) | |
1584 g_free (font->fore); | |
1585 if (font->back) | |
1586 g_free (font->back); | |
1587 if (font->sml) | |
1588 g_free (font->sml); | |
1589 g_free (font); | |
1590 if (str) | |
1591 str = g_string_append (str, "</FONT>"); | |
1592 } | |
1593 | |
3922 | 1594 if (str) { |
1428 | 1595 while (bold) { |
3922 | 1596 str = g_string_append (str, "</B>"); |
1428 | 1597 bold--; |
1598 } | |
1599 while (italics) { | |
3922 | 1600 str = g_string_append (str, "</I>"); |
1428 | 1601 italics--; |
1602 } | |
1603 while (underline) { | |
3922 | 1604 str = g_string_append (str, "</U>"); |
1428 | 1605 underline--; |
1606 } | |
1607 while (strike) { | |
3922 | 1608 str = g_string_append (str, "</S>"); |
1428 | 1609 strike--; |
1610 } | |
1611 while (sub) { | |
3922 | 1612 str = g_string_append (str, "</SUB>"); |
1428 | 1613 sub--; |
1614 } | |
1615 while (sup) { | |
3922 | 1616 str = g_string_append (str, "</SUP>"); |
1428 | 1617 sup--; |
1618 } | |
1619 while (title) { | |
3922 | 1620 str = g_string_append (str, "</TITLE>"); |
1428 | 1621 title--; |
1622 } | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1623 while (pre) { |
3922 | 1624 str = g_string_append (str, "</PRE>"); |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1625 pre--; |
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1626 } |
1428 | 1627 } |
4032 | 1628 g_free (ws); |
4630 | 1629 if(bg) |
1630 g_free(bg); | |
4032 | 1631 if (!(options & GTK_IMHTML_NO_SCROLL)) |
1632 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (imhtml), mark, | |
1633 0, TRUE, 0.0, 1.0); | |
3922 | 1634 gtk_text_buffer_delete_mark (imhtml->text_buffer, mark); |
1635 return str; | |
1636 } | |
1637 | |
4892 | 1638 void gtk_imhtml_remove_smileys(GtkIMHtml *imhtml) |
1639 { | |
4288 | 1640 g_hash_table_destroy(imhtml->smiley_data); |
1641 gtk_smiley_tree_destroy(imhtml->default_smilies); | |
4892 | 1642 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 1643 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4288 | 1644 imhtml->default_smilies = gtk_smiley_tree_new(); |
1645 } | |
3922 | 1646 void gtk_imhtml_show_smileys (GtkIMHtml *imhtml, |
4253 | 1647 gboolean show) |
1648 { | |
1649 imhtml->show_smileys = show; | |
1650 } | |
3922 | 1651 |
1652 void gtk_imhtml_show_comments (GtkIMHtml *imhtml, | |
4253 | 1653 gboolean show) |
1654 { | |
6124 | 1655 imhtml->show_comments = show; |
4253 | 1656 } |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1657 |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1658 void |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1659 gtk_imhtml_clear (GtkIMHtml *imhtml) |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1660 { |
3922 | 1661 GtkTextIter start, end; |
7700 | 1662 GList *del = imhtml->format_spans; |
2993 | 1663 |
3922 | 1664 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
1665 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
1666 gtk_text_buffer_delete(imhtml->text_buffer, &start, &end); | |
7694 | 1667 |
1668 while (imhtml->format_spans) { | |
1669 GtkIMHtmlFormatSpan *span = imhtml->format_spans->data; | |
1670 if (span->start_tag) | |
1671 g_free(span->start_tag); | |
1672 if (span->end_tag) | |
1673 g_free(span->end_tag); | |
1674 g_free(span); | |
1675 imhtml->format_spans = imhtml->format_spans->next; | |
1676 } | |
7700 | 1677 g_list_free(del); |
7694 | 1678 imhtml->edit.bold = NULL; |
1679 imhtml->edit.italic = NULL; | |
1680 imhtml->edit.underline = NULL; | |
7717 | 1681 imhtml->edit.fontface = NULL; |
7728 | 1682 imhtml->edit.forecolor = NULL; |
1683 imhtml->edit.backcolor = NULL; | |
7740 | 1684 imhtml->edit.sizespan = NULL; |
1685 imhtml->edit.fontsize = 3; | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1686 } |
2363
08c66712364c
[gaim-migrate @ 2376]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2349
diff
changeset
|
1687 |
4046 | 1688 void gtk_imhtml_page_up (GtkIMHtml *imhtml) |
1689 { | |
5282 | 1690 GdkRectangle rect; |
1691 GtkTextIter iter; | |
4046 | 1692 |
5282 | 1693 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
1694 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
1695 rect.y - rect.height); | |
1696 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
1697 | |
4046 | 1698 } |
5282 | 1699 void gtk_imhtml_page_down (GtkIMHtml *imhtml) |
1700 { | |
1701 GdkRectangle rect; | |
1702 GtkTextIter iter; | |
1703 | |
1704 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
1705 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
1706 rect.y + rect.height); | |
1707 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
1708 } | |
4735 | 1709 |
5967 | 1710 /* GtkIMHtmlScalable, gtk_imhtml_image, gtk_imhtml_hr */ |
6982 | 1711 GtkIMHtmlScalable *gtk_imhtml_image_new(GdkPixbuf *img, const gchar *filename) |
4735 | 1712 { |
5967 | 1713 GtkIMHtmlImage *im_image = g_malloc(sizeof(GtkIMHtmlImage)); |
5012 | 1714 GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(img)); |
4895 | 1715 |
5967 | 1716 GTK_IMHTML_SCALABLE(im_image)->scale = gtk_imhtml_image_scale; |
1717 GTK_IMHTML_SCALABLE(im_image)->add_to = gtk_imhtml_image_add_to; | |
1718 GTK_IMHTML_SCALABLE(im_image)->free = gtk_imhtml_image_free; | |
5046 | 1719 |
1720 im_image->pixbuf = img; | |
5012 | 1721 im_image->image = image; |
4895 | 1722 im_image->width = gdk_pixbuf_get_width(img); |
1723 im_image->height = gdk_pixbuf_get_height(img); | |
1724 im_image->mark = NULL; | |
6982 | 1725 im_image->filename = filename ? g_strdup(filename) : NULL; |
4895 | 1726 |
5046 | 1727 g_object_ref(img); |
4895 | 1728 return GTK_IMHTML_SCALABLE(im_image); |
1729 } | |
1730 | |
5967 | 1731 void gtk_imhtml_image_scale(GtkIMHtmlScalable *scale, int width, int height) |
4895 | 1732 { |
5967 | 1733 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; |
4895 | 1734 |
1735 if(image->width > width || image->height > height){ | |
1736 GdkPixbuf *new_image = NULL; | |
1737 float factor; | |
1738 int new_width = image->width, new_height = image->height; | |
1739 | |
1740 if(image->width > width){ | |
1741 factor = (float)(width)/image->width; | |
1742 new_width = width; | |
1743 new_height = image->height * factor; | |
1744 } | |
1745 if(new_height > height){ | |
1746 factor = (float)(height)/new_height; | |
1747 new_height = height; | |
1748 new_width = new_width * factor; | |
1749 } | |
1750 | |
5046 | 1751 new_image = gdk_pixbuf_scale_simple(image->pixbuf, new_width, new_height, GDK_INTERP_BILINEAR); |
5012 | 1752 gtk_image_set_from_pixbuf(image->image, new_image); |
4895 | 1753 g_object_unref(G_OBJECT(new_image)); |
1754 } | |
1755 } | |
1756 | |
5012 | 1757 static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel) |
1758 { | |
1759 const gchar *filename = gtk_file_selection_get_filename(sel); | |
5967 | 1760 gchar *dirname; |
1761 GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage"); | |
5012 | 1762 gchar *type = NULL; |
5019 | 1763 GError *error = NULL; |
5015 | 1764 #if GTK_CHECK_VERSION(2,2,0) |
5012 | 1765 GSList *formats = gdk_pixbuf_get_formats(); |
6162 | 1766 #else |
1767 char *basename = g_path_get_basename(filename); | |
1768 char *ext = strrchr(basename, '.'); | |
5959 | 1769 #endif |
5012 | 1770 |
5967 | 1771 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { |
1772 /* append a / if needed */ | |
1773 if (filename[strlen(filename) - 1] != '/') { | |
1774 dirname = g_strconcat(filename, "/", NULL); | |
1775 } else { | |
1776 dirname = g_strdup(filename); | |
1777 } | |
1778 gtk_file_selection_set_filename(sel, dirname); | |
1779 g_free(dirname); | |
5959 | 1780 return; |
5967 | 1781 } |
5959 | 1782 |
1783 #if GTK_CHECK_VERSION(2,2,0) | |
5012 | 1784 while(formats){ |
1785 GdkPixbufFormat *format = formats->data; | |
1786 gchar **extensions = gdk_pixbuf_format_get_extensions(format); | |
1787 gpointer p = extensions; | |
1788 | |
1789 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ | |
1790 gchar *fmt_ext = extensions[0]; | |
1791 const gchar* file_ext = filename + strlen(filename) - strlen(fmt_ext); | |
1792 | |
1793 if(!strcmp(fmt_ext, file_ext)){ | |
1794 type = gdk_pixbuf_format_get_name(format); | |
1795 break; | |
1796 } | |
1797 | |
1798 extensions++; | |
1799 } | |
1800 | |
1801 g_strfreev(p); | |
1802 | |
1803 if(type) | |
1804 break; | |
1805 | |
1806 formats = formats->next; | |
1807 } | |
1808 | |
5020 | 1809 g_slist_free(formats); |
1810 #else | |
1811 /* this is really ugly code, but I think it will work */ | |
1812 if(ext) { | |
1813 ext++; | |
1814 if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) | |
1815 type = g_strdup("jpeg"); | |
1816 else if(!g_ascii_strcasecmp(ext, "png")) | |
1817 type = g_strdup("png"); | |
1818 } | |
1819 | |
1820 g_free(basename); | |
1821 #endif | |
1822 | |
5012 | 1823 /* If I can't find a valid type, I will just tell the user about it and then assume |
1824 it's a png */ | |
1825 if(!type){ | |
1826 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
5967 | 1827 _("Unable to guess the image type based on the file extension supplied. Defaulting to PNG.")); |
5012 | 1828 type = g_strdup("png"); |
1829 } | |
1830 | |
5046 | 1831 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); |
5012 | 1832 |
1833 if(error){ | |
1834 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
1835 _("Error saving image: %s"), error->message); | |
1836 g_error_free(error); | |
1837 } | |
1838 | |
1839 g_free(type); | |
1840 } | |
1841 | |
5967 | 1842 static void gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image) |
5012 | 1843 { |
5967 | 1844 GtkWidget *sel = gtk_file_selection_new(_("Save Image")); |
5012 | 1845 |
6982 | 1846 if (image->filename) |
1847 gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename); | |
5967 | 1848 g_object_set_data(G_OBJECT(sel), "GtkIMHtmlImage", image); |
5012 | 1849 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", |
1850 G_CALLBACK(write_img_to_file), sel); | |
1851 | |
1852 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
1853 G_CALLBACK(gtk_widget_destroy), sel); | |
1854 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked", | |
1855 G_CALLBACK(gtk_widget_destroy), sel); | |
1856 | |
1857 gtk_widget_show(sel); | |
1858 } | |
1859 | |
5967 | 1860 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image) |
5012 | 1861 { |
1862 GdkEventButton *event_button = (GdkEventButton *) event; | |
1863 | |
1864 if (event->type == GDK_BUTTON_RELEASE) { | |
1865 if(event_button->button == 3) { | |
1866 GtkWidget *img, *item, *menu; | |
1867 gchar *text = g_strdup_printf(_("_Save Image...")); | |
1868 menu = gtk_menu_new(); | |
1869 | |
1870 /* buttons and such */ | |
1871 img = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU); | |
1872 item = gtk_image_menu_item_new_with_mnemonic(text); | |
1873 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); | |
5967 | 1874 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_imhtml_image_save), image); |
5012 | 1875 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
1876 | |
1877 gtk_widget_show_all(menu); | |
1878 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
1879 event_button->button, event_button->time); | |
1880 | |
1881 g_free(text); | |
1882 return TRUE; | |
1883 } | |
1884 } | |
1885 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) | |
1886 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
1887 be caught by the regular GtkTextView menu */ | |
1888 else | |
1889 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1890 | |
1891 } | |
5967 | 1892 void gtk_imhtml_image_free(GtkIMHtmlScalable *scale) |
1893 { | |
1894 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
1895 | |
1896 g_object_unref(image->pixbuf); | |
6982 | 1897 if (image->filename) |
1898 g_free(image->filename); | |
5967 | 1899 g_free(scale); |
1900 } | |
1901 | |
1902 void gtk_imhtml_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1903 { | |
1904 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
1905 GtkWidget *box = gtk_event_box_new(); | |
1906 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
1907 | |
1908 gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(image->image)); | |
1909 | |
1910 gtk_widget_show(GTK_WIDGET(image->image)); | |
1911 gtk_widget_show(box); | |
1912 | |
1913 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), box, anchor); | |
1914 g_signal_connect(G_OBJECT(box), "event", G_CALLBACK(gtk_imhtml_image_clicked), image); | |
1915 } | |
1916 | |
1917 GtkIMHtmlScalable *gtk_imhtml_hr_new() | |
1918 { | |
1919 GtkIMHtmlHr *hr = g_malloc(sizeof(GtkIMHtmlHr)); | |
1920 | |
1921 GTK_IMHTML_SCALABLE(hr)->scale = gtk_imhtml_hr_scale; | |
1922 GTK_IMHTML_SCALABLE(hr)->add_to = gtk_imhtml_hr_add_to; | |
1923 GTK_IMHTML_SCALABLE(hr)->free = gtk_imhtml_hr_free; | |
1924 | |
1925 hr->sep = gtk_hseparator_new(); | |
1926 gtk_widget_set_size_request(hr->sep, 5000, 2); | |
1927 gtk_widget_show(hr->sep); | |
1928 | |
1929 return GTK_IMHTML_SCALABLE(hr); | |
1930 } | |
1931 | |
1932 void gtk_imhtml_hr_scale(GtkIMHtmlScalable *scale, int width, int height) | |
1933 { | |
1934 gtk_widget_set_size_request(((GtkIMHtmlHr *)scale)->sep, width, 2); | |
1935 } | |
1936 | |
1937 void gtk_imhtml_hr_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1938 { | |
1939 GtkIMHtmlHr *hr = (GtkIMHtmlHr *)scale; | |
1940 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
1941 | |
1942 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), hr->sep, anchor); | |
1943 } | |
1944 | |
1945 void gtk_imhtml_hr_free(GtkIMHtmlScalable *scale) | |
1946 { | |
1947 g_free(scale); | |
1948 } | |
7295 | 1949 |
1950 gboolean gtk_imhtml_search_find(GtkIMHtml *imhtml, const gchar *text) | |
1951 { | |
1952 GtkTextIter iter, start, end; | |
1953 gboolean new_search = TRUE; | |
1954 | |
1955 g_return_val_if_fail(imhtml != NULL, FALSE); | |
1956 g_return_val_if_fail(text != NULL, FALSE); | |
7754 | 1957 |
7295 | 1958 if (imhtml->search_string && !strcmp(text, imhtml->search_string)) |
1959 new_search = FALSE; | |
7754 | 1960 |
7295 | 1961 if (new_search) { |
1962 gtk_imhtml_search_clear(imhtml); | |
1963 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &iter); | |
1964 } else { | |
1965 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, | |
1966 gtk_text_buffer_get_mark(imhtml->text_buffer, "search")); | |
1967 } | |
1968 imhtml->search_string = g_strdup(text); | |
1969 | |
7358 | 1970 if (gtk_source_iter_forward_search(&iter, imhtml->search_string, |
1971 GTK_SOURCE_SEARCH_VISIBLE_ONLY | GTK_SOURCE_SEARCH_CASE_INSENSITIVE, | |
7295 | 1972 &start, &end, NULL)) { |
1973 | |
1974 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &start, 0, TRUE, 0, 0); | |
1975 gtk_text_buffer_create_mark(imhtml->text_buffer, "search", &end, FALSE); | |
1976 if (new_search) { | |
1977 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &iter, &end); | |
1978 do | |
1979 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "search", &start, &end); | |
7358 | 1980 while (gtk_source_iter_forward_search(&end, imhtml->search_string, |
1981 GTK_SOURCE_SEARCH_VISIBLE_ONLY | | |
1982 GTK_SOURCE_SEARCH_CASE_INSENSITIVE, | |
7295 | 1983 &start, &end, NULL)); |
1984 } | |
1985 return TRUE; | |
1986 } | |
7754 | 1987 |
1988 gtk_imhtml_search_clear(imhtml); | |
1989 | |
7295 | 1990 return FALSE; |
1991 } | |
1992 | |
1993 void gtk_imhtml_search_clear(GtkIMHtml *imhtml) | |
1994 { | |
1995 GtkTextIter start, end; | |
1996 | |
1997 g_return_if_fail(imhtml != NULL); | |
1998 | |
1999 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
2000 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
2001 | |
2002 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &start, &end); | |
2003 if (imhtml->search_string) | |
2004 g_free(imhtml->search_string); | |
2005 imhtml->search_string = NULL; | |
2006 } | |
7694 | 2007 |
2008 /* Editable stuff */ | |
2009 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml) | |
2010 { | |
2011 GtkIMHtmlFormatSpan *span = NULL; | |
7734 | 2012 GtkTextIter end; |
2013 | |
7717 | 2014 gtk_text_iter_forward_chars(iter, len); |
7734 | 2015 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); |
2016 gtk_text_iter_forward_char(&end); | |
2017 | |
2018 if (!gtk_text_iter_equal(&end, iter)) | |
2019 return; | |
7694 | 2020 |
2021 if (!imhtml->editable) | |
2022 return; | |
2023 | |
2024 if ((span = imhtml->edit.bold)) { | |
2025 GtkTextIter bold; | |
2026 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &bold, span->start); | |
2027 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &bold, iter); | |
2028 } | |
7729 | 2029 |
7694 | 2030 if ((span = imhtml->edit.italic)) { |
2031 GtkTextIter italic; | |
2032 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &italic, span->start); | |
7729 | 2033 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &italic, |
2034 iter); | |
7694 | 2035 } |
7729 | 2036 |
2037 if ((span = imhtml->edit.underline)) { | |
2038 GtkTextIter underline; | |
2039 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &underline, span->start); | |
2040 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &underline, | |
2041 iter); | |
2042 } | |
2043 | |
7714 | 2044 if ((span = imhtml->edit.forecolor)) { |
2045 GtkTextIter fore; | |
2046 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &fore, span->start); | |
2047 gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &fore, iter); | |
7694 | 2048 } |
7729 | 2049 |
7714 | 2050 if ((span = imhtml->edit.backcolor)) { |
2051 GtkTextIter back; | |
2052 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &back, span->start); | |
2053 gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &back, iter); | |
2054 } | |
7729 | 2055 |
7717 | 2056 if ((span = imhtml->edit.fontface)) { |
2057 GtkTextIter face; | |
2058 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &face, span->start); | |
2059 gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &face, iter); | |
2060 } | |
7740 | 2061 |
2062 if ((span = imhtml->edit.sizespan)) { | |
2063 GtkTextIter size; | |
2064 /* We create the tags here so that one can grow font or shrink font several times | |
2065 * in a row without creating unnecessary tags */ | |
2066 if (span->tag == NULL) { | |
2067 span->tag = gtk_text_buffer_create_tag | |
2068 (imhtml->text_buffer, NULL, "size-points", (double)_point_sizes [imhtml->edit.fontsize-1], NULL); | |
2069 span->start_tag = g_strdup_printf("<font size='%d'>", imhtml->edit.fontsize); | |
2070 span->end_tag = g_strdup("</font>"); | |
2071 } | |
2072 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &size, span->start); | |
2073 gtk_text_buffer_apply_tag(imhtml->text_buffer, span->tag, &size, iter); | |
2074 } | |
7694 | 2075 } |
2076 | |
2077 void gtk_imhtml_set_editable(GtkIMHtml *imhtml, gboolean editable) | |
2078 { | |
2079 gtk_text_view_set_editable(GTK_TEXT_VIEW(imhtml), editable); | |
2080 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(imhtml), editable); | |
2081 imhtml->editable = editable; | |
2082 } | |
2083 | |
2084 gboolean gtk_imhtml_get_editable(GtkIMHtml *imhtml) | |
2085 { | |
2086 return imhtml->editable; | |
2087 } | |
2088 | |
2089 gboolean gtk_imhtml_toggle_bold(GtkIMHtml *imhtml) | |
2090 { | |
7707 | 2091 GtkIMHtmlFormatSpan *span; |
7694 | 2092 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); |
2093 GtkTextIter iter; | |
2094 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2095 if (!imhtml->edit.bold) { | |
7707 | 2096 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); |
7694 | 2097 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); |
2098 span->start_tag = g_strdup("<b>"); | |
2099 span->end = NULL; | |
2100 span->end_tag = g_strdup("</b>"); | |
2101 span->buffer = imhtml->text_buffer; | |
7714 | 2102 span->tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "BOLD"); |
7694 | 2103 imhtml->edit.bold = span; |
2104 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2105 } else { | |
2106 span = imhtml->edit.bold; | |
2107 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2108 imhtml->edit.bold = NULL; | |
2109 } | |
2110 return imhtml->edit.bold != NULL; | |
2111 } | |
2112 | |
2113 gboolean gtk_imhtml_toggle_italic(GtkIMHtml *imhtml) | |
2114 { | |
7707 | 2115 GtkIMHtmlFormatSpan *span; |
7694 | 2116 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); |
2117 GtkTextIter iter; | |
2118 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2119 if (!imhtml->edit.italic) { | |
7707 | 2120 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); |
7694 | 2121 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); |
2122 span->start_tag = g_strdup("<i>"); | |
2123 span->end = NULL; | |
2124 span->end_tag = g_strdup("</i>"); | |
2125 span->buffer = imhtml->text_buffer; | |
7714 | 2126 span->tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "ITALIC"); |
7694 | 2127 imhtml->edit.italic = span; |
2128 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2129 } else { | |
2130 span = imhtml->edit.italic; | |
2131 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2132 imhtml->edit.italic = NULL; | |
2133 } | |
2134 return imhtml->edit.italic != NULL; | |
2135 } | |
7714 | 2136 |
7694 | 2137 gboolean gtk_imhtml_toggle_underline(GtkIMHtml *imhtml) |
2138 { | |
7707 | 2139 GtkIMHtmlFormatSpan *span; |
7694 | 2140 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); |
2141 GtkTextIter iter; | |
2142 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2143 if (!imhtml->edit.underline) { | |
7707 | 2144 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); |
7694 | 2145 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); |
2146 span->start_tag = g_strdup("<u>"); | |
2147 span->end = NULL; | |
2148 span->end_tag = g_strdup("</u>"); | |
2149 span->buffer = imhtml->text_buffer; | |
7714 | 2150 span->tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "UNDERLINE"); |
7694 | 2151 imhtml->edit.underline = span; |
2152 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2153 } else { | |
7697 | 2154 span = imhtml->edit.underline; |
7694 | 2155 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); |
2156 imhtml->edit.underline = NULL; | |
2157 } | |
2158 return imhtml->edit.underline != NULL; | |
2159 } | |
2160 | |
7740 | 2161 void gtk_imhtml_font_shrink(GtkIMHtml *imhtml) |
2162 { | |
2163 GtkIMHtmlFormatSpan *span; | |
2164 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2165 GtkTextIter iter; | |
2166 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2167 if (imhtml->edit.fontsize == 1) | |
2168 return; | |
2169 | |
2170 imhtml->edit.fontsize--; | |
2171 | |
2172 if (imhtml->edit.sizespan) { | |
2173 GtkTextIter iter2; | |
2174 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter2, imhtml->edit.sizespan->start); | |
2175 if (gtk_text_iter_equal(&iter2, &iter)) | |
2176 return; | |
2177 span = imhtml->edit.sizespan; | |
2178 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2179 } | |
2180 | |
2181 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2182 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2183 span->end = NULL; | |
2184 span->buffer = imhtml->text_buffer; | |
2185 span->tag = NULL; | |
2186 imhtml->edit.sizespan = span; | |
2187 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2188 } | |
2189 | |
2190 void gtk_imhtml_font_grow(GtkIMHtml *imhtml) | |
2191 { | |
2192 GtkIMHtmlFormatSpan *span; | |
2193 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2194 GtkTextIter iter; | |
2195 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2196 if (imhtml->edit.fontsize == MAX_FONT_SIZE) | |
2197 return; | |
2198 | |
2199 imhtml->edit.fontsize++; | |
2200 | |
2201 if (imhtml->edit.sizespan) { | |
2202 GtkTextIter iter2; | |
2203 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter2, imhtml->edit.sizespan->start); | |
2204 if (gtk_text_iter_equal(&iter2, &iter)) | |
2205 return; | |
2206 span = imhtml->edit.sizespan; | |
2207 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2208 } | |
2209 | |
2210 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2211 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2212 span->end = NULL; | |
2213 span->tag = NULL; | |
2214 span->buffer = imhtml->text_buffer; | |
2215 imhtml->edit.sizespan = span; | |
2216 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2217 } | |
2218 | |
7714 | 2219 gboolean gtk_imhtml_toggle_forecolor(GtkIMHtml *imhtml, const char *color) |
2220 { | |
2221 GtkIMHtmlFormatSpan *span; | |
2222 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2223 GtkTextIter iter; | |
2224 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2225 if (!imhtml->edit.forecolor) { | |
2226 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2227 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2228 span->start_tag = g_strdup_printf("<font color='%s'>", color); | |
2229 span->end = NULL; | |
2230 span->end_tag = g_strdup("</font>"); | |
2231 span->buffer = imhtml->text_buffer; | |
2232 span->tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", color, NULL); | |
2233 imhtml->edit.forecolor = span; | |
2234 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2235 } else { | |
2236 span = imhtml->edit.forecolor; | |
2237 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2238 imhtml->edit.forecolor = NULL; | |
2239 } | |
2240 return imhtml->edit.forecolor != NULL; | |
2241 } | |
2242 | |
2243 gboolean gtk_imhtml_toggle_backcolor(GtkIMHtml *imhtml, const char *color) | |
2244 { | |
2245 GtkIMHtmlFormatSpan *span; | |
2246 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2247 GtkTextIter iter; | |
2248 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2249 if (!imhtml->edit.backcolor) { | |
2250 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2251 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2252 span->start_tag = g_strdup_printf("<body bgcolor='%s'>", color); | |
2253 span->end = NULL; | |
2254 span->end_tag = g_strdup("</font>"); | |
2255 span->buffer = imhtml->text_buffer; | |
2256 span->tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", color, NULL); | |
2257 imhtml->edit.backcolor = span; | |
2258 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2259 } else { | |
2260 span = imhtml->edit.backcolor; | |
2261 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2262 imhtml->edit.backcolor = NULL; | |
2263 } | |
2264 return imhtml->edit.backcolor != NULL; | |
2265 } | |
2266 | |
7717 | 2267 gboolean gtk_imhtml_toggle_fontface(GtkIMHtml *imhtml, const char *face) |
2268 { | |
2269 GtkIMHtmlFormatSpan *span; | |
2270 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2271 GtkTextIter iter; | |
2272 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2273 if (!imhtml->edit.fontface) { | |
2274 span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2275 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2276 span->start_tag = g_strdup_printf("<font face='%s'>", face); | |
2277 span->end = NULL; | |
2278 span->end_tag = g_strdup("</font>"); | |
2279 span->buffer = imhtml->text_buffer; | |
2280 span->tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "family", face, NULL); | |
2281 imhtml->edit.fontface = span; | |
2282 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2283 } else { | |
2284 span = imhtml->edit.fontface; | |
2285 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2286 imhtml->edit.fontface = NULL; | |
2287 } | |
2288 return imhtml->edit.fontface != NULL; | |
2289 } | |
2290 | |
7707 | 2291 void gtk_imhtml_insert_link(GtkIMHtml *imhtml, const char *url, const char *text) |
2292 { | |
2293 GtkIMHtmlFormatSpan *span = g_malloc(sizeof(GtkIMHtmlFormatSpan)); | |
2294 GtkTextMark *mark = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2295 GtkTextIter iter; | |
2296 GtkTextTag *tag, *linktag; | |
2297 | |
2298 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, NULL); | |
2299 g_object_set_data(G_OBJECT(tag), "link_url", g_strdup(url)); | |
2300 | |
2301 linktag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), "LINK"); | |
2302 | |
2303 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
2304 span->start = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2305 span->buffer = imhtml->text_buffer; | |
2306 span->start_tag = g_strdup_printf("<a href='%s'>", url); | |
2307 span->end_tag = g_strdup("</a>"); | |
2308 | |
2309 gtk_text_buffer_insert_with_tags(imhtml->text_buffer, &iter, text, strlen(text), linktag, tag, NULL); | |
2310 span->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); | |
2311 imhtml->format_spans = g_list_append(imhtml->format_spans, span); | |
2312 } | |
2313 | |
7736 | 2314 void gtk_imhtml_insert_smiley(GtkIMHtml *imhtml, const char *sml, char *smiley) |
7735 | 2315 { |
2316 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
2317 GtkTextIter iter; | |
2318 GdkPixbuf *pixbuf = NULL; | |
2319 GdkPixbufAnimation *annipixbuf = NULL; | |
2320 GtkWidget *icon = NULL; | |
7749 | 2321 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, &iter); |
2322 | |
2323 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, ins); | |
2324 g_object_set_data(G_OBJECT(anchor), "text_tag", smiley); | |
7735 | 2325 |
7736 | 2326 annipixbuf = gtk_smiley_tree_image(imhtml, sml, smiley); |
7735 | 2327 if(annipixbuf) { |
7749 | 2328 if(gdk_pixbuf_animation_is_static_image(annipixbuf)) { |
7735 | 2329 pixbuf = gdk_pixbuf_animation_get_static_image(annipixbuf); |
2330 if(pixbuf) | |
2331 icon = gtk_image_new_from_pixbuf(pixbuf); | |
2332 } else { | |
2333 icon = gtk_image_new_from_animation(annipixbuf); | |
2334 } | |
2335 } | |
2336 | |
2337 if (icon) { | |
2338 gtk_widget_show(icon); | |
2339 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), icon, anchor); | |
2340 } | |
2341 } | |
2342 | |
7694 | 2343 int span_compare_begin(const GtkIMHtmlFormatSpan *a, const GtkIMHtmlFormatSpan *b, GtkTextBuffer *buffer) |
2344 { | |
2345 GtkTextIter ia, ib; | |
2346 gtk_text_buffer_get_iter_at_mark(buffer, &ia, a->start); | |
2347 gtk_text_buffer_get_iter_at_mark(buffer, &ib, b->start); | |
2348 return gtk_text_iter_compare(&ia, &ib); | |
2349 } | |
2350 | |
2351 int span_compare_end(GtkIMHtmlFormatSpan *a, GtkIMHtmlFormatSpan *b) | |
2352 { | |
2353 GtkTextIter ia, ib; | |
2354 gtk_text_buffer_get_iter_at_mark(a->buffer, &ia, a->start); | |
2355 gtk_text_buffer_get_iter_at_mark(b->buffer, &ib, b->start); | |
2356 return gtk_text_iter_compare(&ia, &ib); | |
2357 } | |
2358 | |
2359 /* Basic notion here: traverse through the text buffer one-by-one, non-character elements, such | |
2360 * as smileys and IM images are represented by the Unicode "unknown" character. Handle them. Else | |
2361 * check the list of formatted strings, sorted by the position of the starting tags and apply them as | |
2362 * needed. After applying the start tags, add the end tags to the "closers" list, which is sorted by | |
2363 * location of ending tags. These get applied in a similar fashion. Finally, replace <, >, &, and " | |
2364 * with their HTML equivilent. */ | |
7749 | 2365 char *gtk_imhtml_get_markup_range(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end) |
7694 | 2366 { |
2367 gunichar c; | |
2368 GtkIMHtmlFormatSpan *sspan = NULL, *espan = NULL; | |
2369 GtkTextIter iter, siter, eiter; | |
2370 GList *starters = imhtml->format_spans; | |
7749 | 2371 GList *closers = NULL; |
7694 | 2372 GString *str = g_string_new(""); |
2373 g_list_sort_with_data(starters, (GCompareDataFunc)span_compare_begin, imhtml->text_buffer); | |
7749 | 2374 |
2375 gtk_text_iter_order(start, end); | |
2376 iter = *start; | |
2377 | |
7694 | 2378 |
2379 /* Initialize these to the end iter */ | |
2380 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &siter); | |
2381 eiter = siter; | |
2382 | |
2383 if (starters) { | |
7749 | 2384 while (starters) { |
2385 GtkTextIter tagend; | |
2386 sspan = (GtkIMHtmlFormatSpan*)starters->data; | |
2387 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, sspan->start); | |
2388 if (gtk_text_iter_compare(&siter, start) > 0) | |
2389 break; | |
2390 if (sspan->end) | |
2391 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &tagend, sspan->end); | |
2392 if (sspan->end == NULL || gtk_text_iter_compare(&tagend, start) > 0) { | |
2393 str = g_string_append(str, sspan->start_tag); | |
2394 closers = g_list_append(closers, sspan); | |
2395 } | |
2396 sspan = (GtkIMHtmlFormatSpan*)starters->data; | |
2397 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, sspan->start); | |
2398 starters = starters->next; | |
2399 } | |
2400 if (!starters) { | |
2401 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &siter); | |
2402 sspan = NULL; | |
2403 } | |
7694 | 2404 } |
2405 | |
7749 | 2406 while ((c = gtk_text_iter_get_char(&iter)) != 0 && !gtk_text_iter_equal(&iter, end)) { |
7694 | 2407 if (c == 0xFFFC) { |
7735 | 2408 GtkTextChildAnchor* anchor = gtk_text_iter_get_child_anchor(&iter); |
2409 char *text = g_object_get_data(G_OBJECT(anchor), "text_tag"); | |
2410 str = g_string_append(str, text); | |
7694 | 2411 } else { |
2412 while (gtk_text_iter_equal(&eiter, &iter)) { | |
2413 /* This is where we shall insert the ending tag of | |
2414 * this format span */ | |
2415 str = g_string_append(str, espan->end_tag); | |
2416 closers = g_list_remove(closers, espan); | |
2417 if (closers) { | |
2418 espan = (GtkIMHtmlFormatSpan*)closers->data; | |
2419 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &eiter, espan->end); | |
2420 } else { | |
2421 espan = NULL; | |
2422 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &eiter); | |
2423 } | |
2424 } | |
2425 while (gtk_text_iter_equal(&siter, &iter)) { | |
2426 /* This is where we shall insert the starting tag of | |
2427 * this format span */ | |
2428 str = g_string_append(str, sspan->start_tag); | |
2429 if (sspan->end) { | |
2430 espan = sspan; | |
2431 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &eiter, espan->end); | |
2432 closers = g_list_insert_sorted(closers, sspan, (GCompareFunc)span_compare_end); | |
2433 } | |
2434 starters = starters->next; | |
2435 if (starters) { | |
2436 sspan = (GtkIMHtmlFormatSpan*)starters->data; | |
2437 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, sspan->start); | |
2438 } else { | |
2439 sspan = NULL; | |
2440 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &siter); | |
2441 } | |
2442 | |
2443 } | |
7715 | 2444 |
2445 if (c == '<') | |
2446 str = g_string_append(str, "<"); | |
2447 else if (c == '>') | |
2448 str = g_string_append(str, ">"); | |
2449 else if (c == '&') | |
2450 str = g_string_append(str, "&"); | |
2451 else if (c == '"') | |
2452 str = g_string_append(str, """); | |
7716 | 2453 else |
2454 str = g_string_append_unichar(str, c); | |
7694 | 2455 } |
2456 gtk_text_iter_forward_char(&iter); | |
2457 } | |
2458 return g_string_free(str, FALSE); | |
2459 } | |
2460 | |
7749 | 2461 char *gtk_imhtml_get_markup(GtkIMHtml *imhtml) |
2462 { | |
2463 GtkTextIter start, end; | |
2464 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
2465 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
2466 return gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
2467 } | |
2468 | |
7694 | 2469 char *gtk_imhtml_get_text(GtkIMHtml *imhtml) |
2470 { | |
2471 GtkTextIter start_iter, end_iter; | |
2472 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start_iter); | |
2473 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end_iter); | |
2474 return gtk_text_buffer_get_text(imhtml->text_buffer, &start_iter, &end_iter, FALSE); | |
2475 | |
2476 } |