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