Mercurial > pidgin.yaz
annotate src/gtkimhtml.c @ 5104:a20a644e0da4
[gaim-migrate @ 5467]
getting better...
gaim will ignore the <span> tags for now, until we can really make use of them
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 11 Apr 2003 00:52:02 +0000 |
parents | a7e9036cd46f |
children | 4cfc49e31c2e |
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" |
26 #include <gtk/gtk.h> | |
4895 | 27 #include <glib/gerror.h> |
4046 | 28 #include <gdk/gdkkeysyms.h> |
1428 | 29 #include <string.h> |
30 #include <ctype.h> | |
31 #include <stdio.h> | |
4629 | 32 #include <stdlib.h> |
1428 | 33 #include <math.h> |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
34 #ifdef HAVE_LANGINFO_CODESET |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
35 #include <langinfo.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
36 #include <locale.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
37 #endif |
1428 | 38 |
4417 | 39 #ifdef ENABLE_NLS |
40 # include <libintl.h> | |
41 # define _(x) gettext(x) | |
42 # ifdef gettext_noop | |
43 # define N_(String) gettext_noop (String) | |
44 # else | |
45 # define N_(String) (String) | |
46 # endif | |
47 #else | |
48 # define N_(String) (String) | |
49 # define _(x) (x) | |
50 #endif | |
51 | |
4735 | 52 #include <pango/pango-font.h> |
53 | |
54 #define TOOLTIP_TIMEOUT 500 | |
55 | |
4764 | 56 static gboolean gtk_motion_event_notify(GtkWidget *imhtml, GdkEventMotion *event, gpointer user_data); |
4944 | 57 static gboolean gtk_leave_event_notify(GtkWidget *imhtml, GdkEventCrossing *event, gpointer user_data); |
4764 | 58 |
4947 | 59 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data); |
5012 | 60 |
61 static gboolean gaim_im_image_clicked(GtkWidget *w, GdkEvent *event, gaim_im_image *image); | |
62 | |
4895 | 63 static gint gtk_imhtml_tip (gpointer data); |
4764 | 64 |
4735 | 65 |
3922 | 66 /* POINT_SIZE converts from AIM font sizes to point sizes. It probably should be redone in such a |
67 * way that it base the sizes off the default font size rather than using arbitrary font sizes. */ | |
68 #define MAX_FONT_SIZE 7 | |
69 #define POINT_SIZE(x) (_point_sizes [MIN ((x), MAX_FONT_SIZE) - 1]) | |
3928 | 70 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
|
71 |
3922 | 72 /* The four elements present in a <FONT> tag contained in a struct */ |
73 typedef struct _FontDetail FontDetail; | |
1428 | 74 struct _FontDetail { |
75 gushort size; | |
76 gchar *face; | |
3922 | 77 gchar *fore; |
78 gchar *back; | |
4032 | 79 gchar *sml; |
1428 | 80 }; |
81 | |
4032 | 82 struct _GtkSmileyTree { |
83 GString *values; | |
84 GtkSmileyTree **children; | |
4263 | 85 GtkIMHtmlSmiley *image; |
4032 | 86 }; |
87 | |
88 static GtkSmileyTree* | |
89 gtk_smiley_tree_new () | |
90 { | |
91 return g_new0 (GtkSmileyTree, 1); | |
92 } | |
93 | |
94 static void | |
95 gtk_smiley_tree_insert (GtkSmileyTree *tree, | |
4263 | 96 GtkIMHtmlSmiley *smiley) |
4032 | 97 { |
98 GtkSmileyTree *t = tree; | |
4263 | 99 const gchar *x = smiley->smile; |
4032 | 100 |
101 if (!strlen (x)) | |
102 return; | |
103 | |
104 while (*x) { | |
105 gchar *pos; | |
106 gint index; | |
107 | |
108 if (!t->values) | |
109 t->values = g_string_new (""); | |
110 | |
111 pos = strchr (t->values->str, *x); | |
112 if (!pos) { | |
113 t->values = g_string_append_c (t->values, *x); | |
114 index = t->values->len - 1; | |
115 t->children = g_realloc (t->children, t->values->len * sizeof (GtkSmileyTree *)); | |
116 t->children [index] = g_new0 (GtkSmileyTree, 1); | |
117 } else | |
118 index = (int) pos - (int) t->values->str; | |
119 | |
120 t = t->children [index]; | |
121 | |
122 x++; | |
123 } | |
124 | |
4263 | 125 t->image = smiley; |
4032 | 126 } |
4041 | 127 |
4263 | 128 |
4264 | 129 void gtk_smiley_tree_destroy (GtkSmileyTree *tree) |
4032 | 130 { |
131 GSList *list = g_slist_append (NULL, tree); | |
132 | |
133 while (list) { | |
134 GtkSmileyTree *t = list->data; | |
135 gint i; | |
136 list = g_slist_remove(list, t); | |
137 if (t->values) { | |
138 for (i = 0; i < t->values->len; i++) | |
139 list = g_slist_append (list, t->children [i]); | |
140 g_string_free (t->values, TRUE); | |
141 g_free (t->children); | |
142 } | |
143 g_free (t); | |
144 } | |
145 } | |
146 | |
4263 | 147 |
4032 | 148 static GtkTextViewClass *parent_class = NULL; |
149 | |
150 | |
3922 | 151 /* GtkIMHtml has one signal--URL_CLICKED */ |
1428 | 152 enum { |
153 URL_CLICKED, | |
154 LAST_SIGNAL | |
155 }; | |
156 static guint signals [LAST_SIGNAL] = { 0 }; | |
157 | |
4032 | 158 static void |
159 gtk_imhtml_finalize (GObject *object) | |
160 { | |
161 GtkIMHtml *imhtml = GTK_IMHTML(object); | |
4895 | 162 GList *scalables; |
163 | |
4138 | 164 g_hash_table_destroy(imhtml->smiley_data); |
4032 | 165 gtk_smiley_tree_destroy(imhtml->default_smilies); |
4138 | 166 gdk_cursor_unref(imhtml->hand_cursor); |
167 gdk_cursor_unref(imhtml->arrow_cursor); | |
4735 | 168 if(imhtml->tip_window){ |
169 gtk_widget_destroy(imhtml->tip_window); | |
170 } | |
171 if(imhtml->tip_timer) | |
172 gtk_timeout_remove(imhtml->tip_timer); | |
173 | |
4895 | 174 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { |
175 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); | |
176 scale->free(scale); | |
177 } | |
178 | |
179 g_list_free(imhtml->scalables); | |
4032 | 180 G_OBJECT_CLASS(parent_class)->finalize (object); |
181 } | |
1428 | 182 |
3922 | 183 /* Boring GTK stuff */ |
184 static void gtk_imhtml_class_init (GtkIMHtmlClass *class) | |
1428 | 185 { |
3922 | 186 GtkObjectClass *object_class; |
4032 | 187 GObjectClass *gobject_class; |
3922 | 188 object_class = (GtkObjectClass*) class; |
4032 | 189 gobject_class = (GObjectClass*) class; |
190 parent_class = gtk_type_class(GTK_TYPE_TEXT_VIEW); | |
4417 | 191 signals[URL_CLICKED] = g_signal_new("url_clicked", |
192 G_TYPE_FROM_CLASS(gobject_class), | |
193 G_SIGNAL_RUN_FIRST, | |
194 G_STRUCT_OFFSET(GtkIMHtmlClass, url_clicked), | |
195 NULL, | |
196 0, | |
197 g_cclosure_marshal_VOID__POINTER, | |
198 G_TYPE_NONE, 1, | |
199 G_TYPE_POINTER); | |
4032 | 200 gobject_class->finalize = gtk_imhtml_finalize; |
1428 | 201 } |
202 | |
3922 | 203 static void gtk_imhtml_init (GtkIMHtml *imhtml) |
1428 | 204 { |
3922 | 205 GtkTextIter iter; |
206 imhtml->text_buffer = gtk_text_buffer_new(NULL); | |
207 gtk_text_buffer_get_end_iter (imhtml->text_buffer, &iter); | |
208 imhtml->end = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, FALSE); | |
209 gtk_text_view_set_buffer(GTK_TEXT_VIEW(imhtml), imhtml->text_buffer); | |
210 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(imhtml), GTK_WRAP_WORD); | |
211 gtk_text_view_set_editable(GTK_TEXT_VIEW(imhtml), FALSE); | |
212 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(imhtml), 5); | |
213 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(imhtml), FALSE); | |
214 /*gtk_text_view_set_justification(GTK_TEXT_VIEW(imhtml), GTK_JUSTIFY_FILL);*/ | |
3465 | 215 |
3922 | 216 /* These tags will be used often and can be reused--we create them on init and then apply them by name |
217 * other tags (color, size, face, etc.) will have to be created and applied dynamically */ | |
218 gtk_text_buffer_create_tag(imhtml->text_buffer, "BOLD", "weight", PANGO_WEIGHT_BOLD, NULL); | |
219 gtk_text_buffer_create_tag(imhtml->text_buffer, "ITALICS", "style", PANGO_STYLE_ITALIC, NULL); | |
220 gtk_text_buffer_create_tag(imhtml->text_buffer, "UNDERLINE", "underline", PANGO_UNDERLINE_SINGLE, NULL); | |
221 gtk_text_buffer_create_tag(imhtml->text_buffer, "STRIKE", "strikethrough", TRUE, NULL); | |
222 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUB", "rise", -5000, NULL); | |
223 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUP", "rise", 5000, NULL); | |
224 gtk_text_buffer_create_tag(imhtml->text_buffer, "PRE", "family", "Monospace", NULL); | |
3465 | 225 |
3922 | 226 /* When hovering over a link, we show the hand cursor--elsewhere we show the plain ol' pointer cursor */ |
227 imhtml->hand_cursor = gdk_cursor_new (GDK_HAND2); | |
228 imhtml->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); | |
2993 | 229 |
4253 | 230 imhtml->show_smileys = TRUE; |
231 imhtml->show_comments = TRUE; | |
232 | |
4892 | 233 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 234 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4032 | 235 imhtml->default_smilies = gtk_smiley_tree_new(); |
4735 | 236 |
4944 | 237 g_signal_connect(G_OBJECT(imhtml), "size-allocate", G_CALLBACK(gtk_size_allocate_cb), NULL); |
4735 | 238 g_signal_connect(G_OBJECT(imhtml), "motion-notify-event", G_CALLBACK(gtk_motion_event_notify), NULL); |
4944 | 239 g_signal_connect(G_OBJECT(imhtml), "leave-notify-event", G_CALLBACK(gtk_leave_event_notify), NULL); |
240 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); | |
4735 | 241 |
242 imhtml->tip = NULL; | |
243 imhtml->tip_timer = 0; | |
244 imhtml->tip_window = NULL; | |
4895 | 245 |
246 imhtml->scalables = NULL; | |
2993 | 247 } |
248 | |
3922 | 249 GtkWidget *gtk_imhtml_new(void *a, void *b) |
1428 | 250 { |
4635 | 251 return GTK_WIDGET(g_object_new(gtk_imhtml_get_type(), NULL)); |
1428 | 252 } |
253 | |
4635 | 254 GType gtk_imhtml_get_type() |
1428 | 255 { |
4635 | 256 static GType imhtml_type = 0; |
1428 | 257 |
258 if (!imhtml_type) { | |
4635 | 259 static const GTypeInfo imhtml_info = { |
260 sizeof(GtkIMHtmlClass), | |
261 NULL, | |
262 NULL, | |
263 (GClassInitFunc) gtk_imhtml_class_init, | |
264 NULL, | |
265 NULL, | |
1428 | 266 sizeof (GtkIMHtml), |
4635 | 267 0, |
268 (GInstanceInitFunc) gtk_imhtml_init | |
1428 | 269 }; |
4635 | 270 |
271 imhtml_type = g_type_register_static(gtk_text_view_get_type(), | |
272 "GtkIMHtml", &imhtml_info, 0); | |
1428 | 273 } |
274 | |
275 return imhtml_type; | |
276 } | |
277 | |
4417 | 278 struct url_data { |
279 GObject *object; | |
280 gchar *url; | |
281 }; | |
282 | |
283 static void url_open(GtkWidget *w, struct url_data *data) { | |
284 if(!data) return; | |
285 | |
286 g_signal_emit(data->object, signals[URL_CLICKED], 0, data->url); | |
287 | |
288 g_object_unref(data->object); | |
289 g_free(data->url); | |
290 g_free(data); | |
291 } | |
292 static void url_copy(GtkWidget *w, gchar *url) { | |
293 GtkClipboard *clipboard; | |
294 | |
4419 | 295 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
4417 | 296 gtk_clipboard_set_text(clipboard, url, -1); |
297 } | |
298 | |
299 /* The callback for an event on a link tag. */ | |
5091 | 300 gboolean tag_event(GtkTextTag *tag, GObject *imhtml, GdkEvent *event, GtkTextIter *arg2, char *url) { |
4417 | 301 GdkEventButton *event_button = (GdkEventButton *) event; |
302 | |
3922 | 303 if (event->type == GDK_BUTTON_RELEASE) { |
4417 | 304 if (event_button->button == 1) { |
305 GtkTextIter start, end; | |
306 /* we shouldn't open a URL if the user has selected something: */ | |
307 gtk_text_buffer_get_selection_bounds( | |
308 gtk_text_iter_get_buffer(arg2), &start, &end); | |
309 if(gtk_text_iter_get_offset(&start) != | |
310 gtk_text_iter_get_offset(&end)) | |
311 return FALSE; | |
312 | |
313 /* A link was clicked--we emit the "url_clicked" signal | |
314 * with the URL as the argument */ | |
5091 | 315 g_signal_emit(imhtml, signals[URL_CLICKED], 0, url); |
4417 | 316 return FALSE; |
317 } else if(event_button->button == 3) { | |
4745 | 318 GtkWidget *img, *item, *menu; |
4417 | 319 struct url_data *tempdata = g_new(struct url_data, 1); |
5091 | 320 tempdata->object = g_object_ref(imhtml); |
4417 | 321 tempdata->url = g_strdup(url); |
4745 | 322 |
5091 | 323 /* Don't want the tooltip around if user right-clicked on link */ |
324 if (GTK_IMHTML(imhtml)->tip_window) { | |
325 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
326 GTK_IMHTML(imhtml)->tip_window = NULL; | |
327 } | |
328 if (GTK_IMHTML(imhtml)->tip_timer) { | |
329 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
330 GTK_IMHTML(imhtml)->tip_timer = 0; | |
331 } | |
332 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
4417 | 333 menu = gtk_menu_new(); |
4745 | 334 |
4417 | 335 /* buttons and such */ |
336 img = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU); | |
4420 | 337 item = gtk_image_menu_item_new_with_mnemonic(_("_Copy Link Location")); |
4417 | 338 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); |
339 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(url_copy), | |
340 url); | |
341 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
342 | |
343 img = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU); | |
4420 | 344 item = gtk_image_menu_item_new_with_mnemonic(_("_Open Link in Browser")); |
4417 | 345 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); |
346 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(url_open), | |
347 tempdata); | |
348 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
4756 | 349 |
4417 | 350 gtk_widget_show_all(menu); |
4756 | 351 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, |
352 event_button->button, event_button->time); | |
4745 | 353 |
4417 | 354 return TRUE; |
355 } | |
1428 | 356 } |
4417 | 357 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) |
358 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
359 be caught by the regular GtkTextView menu */ | |
360 else | |
361 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1428 | 362 } |
363 | |
4735 | 364 gboolean gtk_motion_event_notify(GtkWidget *imhtml, GdkEventMotion *event, gpointer data) |
365 { | |
366 GtkTextIter iter; | |
367 GdkWindow *win = event->window; | |
368 int x, y; | |
4740 | 369 char *tip = NULL; |
4735 | 370 GSList *tags = NULL, *templist = NULL; |
371 gdk_window_get_pointer(GTK_WIDGET(imhtml)->window, NULL, NULL, NULL); | |
372 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), GTK_TEXT_WINDOW_WIDGET, | |
373 event->x, event->y, &x, &y); | |
374 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); | |
375 tags = gtk_text_iter_get_tags(&iter); | |
376 | |
4740 | 377 templist = tags; |
378 while (templist) { | |
4735 | 379 GtkTextTag *tag = templist->data; |
4740 | 380 tip = g_object_get_data(G_OBJECT(tag), "link_url"); |
381 if (tip) | |
382 break; | |
383 templist = templist->next; | |
4735 | 384 } |
4740 | 385 |
386 if (GTK_IMHTML(imhtml)->tip) { | |
387 if ((tip == GTK_IMHTML(imhtml)->tip)) { | |
388 return FALSE; | |
389 } | |
390 /* We've left the cell. Remove the timeout and create a new one below */ | |
391 if (GTK_IMHTML(imhtml)->tip_window) { | |
392 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
393 GTK_IMHTML(imhtml)->tip_window = NULL; | |
394 } | |
395 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->arrow_cursor); | |
396 if (GTK_IMHTML(imhtml)->tip_timer) | |
397 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
398 GTK_IMHTML(imhtml)->tip_timer = 0; | |
399 } | |
400 | |
401 if(tip){ | |
4735 | 402 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->hand_cursor); |
4740 | 403 GTK_IMHTML(imhtml)->tip_timer = g_timeout_add (TOOLTIP_TIMEOUT, |
404 gtk_imhtml_tip, imhtml); | |
4735 | 405 } |
4740 | 406 |
407 GTK_IMHTML(imhtml)->tip = tip; | |
4735 | 408 g_slist_free(tags); |
409 return FALSE; | |
410 } | |
411 | |
4944 | 412 gboolean gtk_leave_event_notify(GtkWidget *imhtml, GdkEventCrossing *event, gpointer data) |
413 { | |
414 /* when leaving the widget, clear any current & pending tooltips and restore the cursor */ | |
415 if (GTK_IMHTML(imhtml)->tip_window) { | |
416 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
417 GTK_IMHTML(imhtml)->tip_window = NULL; | |
418 } | |
419 if (GTK_IMHTML(imhtml)->tip_timer) { | |
420 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
421 GTK_IMHTML(imhtml)->tip_timer = 0; | |
422 } | |
423 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
424 | |
425 /* propogate the event normally */ | |
426 return FALSE; | |
427 } | |
428 | |
4298 | 429 /* this isn't used yet |
4032 | 430 static void |
4263 | 431 gtk_smiley_tree_remove (GtkSmileyTree *tree, |
432 GtkIMHtmlSmiley *smiley) | |
4032 | 433 { |
434 GtkSmileyTree *t = tree; | |
4263 | 435 const gchar *x = smiley->smile; |
4032 | 436 gint len = 0; |
437 | |
438 while (*x) { | |
439 gchar *pos; | |
440 | |
441 if (!t->values) | |
442 return; | |
443 | |
444 pos = strchr (t->values->str, *x); | |
445 if (pos) | |
446 t = t->children [(int) pos - (int) t->values->str]; | |
447 else | |
448 return; | |
449 | |
450 x++; len++; | |
451 } | |
452 | |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
453 if (t->image) { |
4032 | 454 t->image = NULL; |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
455 } |
4032 | 456 } |
4298 | 457 */ |
458 | |
4032 | 459 |
460 static gint | |
461 gtk_smiley_tree_lookup (GtkSmileyTree *tree, | |
462 const gchar *text) | |
463 { | |
464 GtkSmileyTree *t = tree; | |
465 const gchar *x = text; | |
466 gint len = 0; | |
467 | |
468 while (*x) { | |
469 gchar *pos; | |
470 | |
471 if (!t->values) | |
472 break; | |
473 | |
474 pos = strchr (t->values->str, *x); | |
475 if (pos) | |
476 t = t->children [(int) pos - (int) t->values->str]; | |
477 else | |
478 break; | |
479 | |
480 x++; len++; | |
481 } | |
482 | |
483 if (t->image) | |
484 return len; | |
485 | |
486 return 0; | |
487 } | |
488 | |
489 void | |
4263 | 490 gtk_imhtml_associate_smiley (GtkIMHtml *imhtml, |
491 gchar *sml, | |
492 GtkIMHtmlSmiley *smiley) | |
4032 | 493 { |
494 GtkSmileyTree *tree; | |
495 g_return_if_fail (imhtml != NULL); | |
496 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
4263 | 497 |
4032 | 498 if (sml == NULL) |
499 tree = imhtml->default_smilies; | |
500 else if ((tree = g_hash_table_lookup(imhtml->smiley_data, sml))) { | |
501 } else { | |
502 tree = gtk_smiley_tree_new(); | |
4892 | 503 g_hash_table_insert(imhtml->smiley_data, g_strdup(sml), tree); |
4032 | 504 } |
505 | |
4263 | 506 gtk_smiley_tree_insert (tree, smiley); |
4032 | 507 } |
508 | |
509 static gboolean | |
510 gtk_imhtml_is_smiley (GtkIMHtml *imhtml, | |
511 GSList *fonts, | |
512 const gchar *text, | |
513 gint *len) | |
514 { | |
515 GtkSmileyTree *tree; | |
516 FontDetail *font; | |
517 char *sml = NULL; | |
518 | |
519 if (fonts) { | |
520 font = fonts->data; | |
521 sml = font->sml; | |
522 } | |
523 | |
524 if (sml == NULL) | |
525 tree = imhtml->default_smilies; | |
526 else { | |
527 tree = g_hash_table_lookup(imhtml->smiley_data, sml); | |
528 } | |
529 if (tree == NULL) | |
530 return FALSE; | |
531 | |
532 *len = gtk_smiley_tree_lookup (tree, text); | |
533 return (*len > 0); | |
534 } | |
535 | |
4263 | 536 GdkPixbuf* |
4032 | 537 gtk_smiley_tree_image (GtkIMHtml *imhtml, |
538 const gchar *sml, | |
539 const gchar *text) | |
540 { | |
541 GtkSmileyTree *t; | |
542 const gchar *x = text; | |
543 if (sml == NULL) | |
544 t = imhtml->default_smilies; | |
545 else | |
546 t = g_hash_table_lookup(imhtml->smiley_data, sml); | |
547 | |
548 | |
549 if (t == NULL) | |
550 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
551 | |
552 while (*x) { | |
553 gchar *pos; | |
554 | |
555 if (!t->values) { | |
556 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
557 } | |
558 | |
559 pos = strchr (t->values->str, *x); | |
560 if (pos) { | |
561 t = t->children [(int) pos - (int) t->values->str]; | |
562 } else { | |
563 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
564 } | |
565 x++; | |
566 } | |
567 | |
4263 | 568 if (!t->image->icon) |
569 t->image->icon = gdk_pixbuf_new_from_file(t->image->file, NULL); | |
570 | |
571 return t->image->icon; | |
4032 | 572 } |
4793 | 573 #define VALID_TAG(x) if (!g_ascii_strncasecmp (string, x ">", strlen (x ">"))) { \ |
3922 | 574 *tag = g_strndup (string, strlen (x)); \ |
575 *len = strlen (x) + 1; \ | |
576 return TRUE; \ | |
577 } \ | |
578 (*type)++ | |
1428 | 579 |
4793 | 580 #define VALID_OPT_TAG(x) if (!g_ascii_strncasecmp (string, x " ", strlen (x " "))) { \ |
3922 | 581 const gchar *c = string + strlen (x " "); \ |
582 gchar e = '"'; \ | |
583 gboolean quote = FALSE; \ | |
584 while (*c) { \ | |
585 if (*c == '"' || *c == '\'') { \ | |
586 if (quote && (*c == e)) \ | |
587 quote = !quote; \ | |
588 else if (!quote) { \ | |
589 quote = !quote; \ | |
590 e = *c; \ | |
591 } \ | |
592 } else if (!quote && (*c == '>')) \ | |
593 break; \ | |
594 c++; \ | |
595 } \ | |
596 if (*c) { \ | |
597 *tag = g_strndup (string, c - string); \ | |
598 *len = c - string + 1; \ | |
599 return TRUE; \ | |
600 } \ | |
601 } \ | |
602 (*type)++ | |
1428 | 603 |
604 | |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
605 static gboolean |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
606 gtk_imhtml_is_amp_escape (const gchar *string, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
607 gchar *replace, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
608 gint *length) |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
609 { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
610 g_return_val_if_fail (string != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
611 g_return_val_if_fail (replace != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
612 g_return_val_if_fail (length != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
613 |
4793 | 614 if (!g_ascii_strncasecmp (string, "&", 5)) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
615 *replace = '&'; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
616 *length = 5; |
4793 | 617 } else if (!g_ascii_strncasecmp (string, "<", 4)) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
618 *replace = '<'; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
619 *length = 4; |
4793 | 620 } else if (!g_ascii_strncasecmp (string, ">", 4)) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
621 *replace = '>'; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
622 *length = 4; |
4793 | 623 } else if (!g_ascii_strncasecmp (string, " ", 6)) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
624 *replace = ' '; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
625 *length = 6; |
4793 | 626 } else if (!g_ascii_strncasecmp (string, "©", 6)) { |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3705
diff
changeset
|
627 *replace = '©'; /* was: '©' */ |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
628 *length = 6; |
4793 | 629 } else if (!g_ascii_strncasecmp (string, """, 6)) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
630 *replace = '\"'; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
631 *length = 6; |
4793 | 632 } else if (!g_ascii_strncasecmp (string, "®", 5)) { |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3705
diff
changeset
|
633 *replace = '®'; /* was: '®' */ |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
634 *length = 5; |
5093 | 635 } else if (!g_ascii_strncasecmp (string, "'", 6)) { |
636 *replace = '\''; | |
637 *length = 6; | |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
638 } else if (*(string + 1) == '#') { |
2022
199ba82faacb
[gaim-migrate @ 2032]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2002
diff
changeset
|
639 guint pound = 0; |
3004 | 640 if ((sscanf (string, "&#%u;", £) == 1) && pound != 0) { |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
641 if (*(string + 3 + (gint)log10 (pound)) != ';') |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
642 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
643 *replace = (gchar)pound; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
644 *length = 2; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
645 while (isdigit ((gint) string [*length])) (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
646 if (string [*length] == ';') (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
647 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
648 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
649 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
650 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
651 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
652 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
653 |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
654 return TRUE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
655 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
656 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
657 static gboolean |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
658 gtk_imhtml_is_tag (const gchar *string, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
659 gchar **tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
660 gint *len, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
661 gint *type) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
662 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
663 *type = 1; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
664 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
665 if (!strchr (string, '>')) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
666 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
667 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
668 VALID_TAG ("B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
669 VALID_TAG ("BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
670 VALID_TAG ("/B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
671 VALID_TAG ("/BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
672 VALID_TAG ("I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
673 VALID_TAG ("ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
674 VALID_TAG ("/I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
675 VALID_TAG ("/ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
676 VALID_TAG ("U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
677 VALID_TAG ("UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
678 VALID_TAG ("/U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
679 VALID_TAG ("/UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
680 VALID_TAG ("S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
681 VALID_TAG ("STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
682 VALID_TAG ("/S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
683 VALID_TAG ("/STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
684 VALID_TAG ("SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
685 VALID_TAG ("/SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
686 VALID_TAG ("SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
687 VALID_TAG ("/SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
688 VALID_TAG ("PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
689 VALID_TAG ("/PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
690 VALID_TAG ("TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
691 VALID_TAG ("/TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
692 VALID_TAG ("BR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
693 VALID_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
694 VALID_TAG ("/FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
695 VALID_TAG ("/A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
696 VALID_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
697 VALID_TAG ("/P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
698 VALID_TAG ("H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
699 VALID_TAG ("/H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
700 VALID_TAG ("HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
701 VALID_TAG ("/HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
702 VALID_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
703 VALID_TAG ("/BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
704 VALID_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
705 VALID_TAG ("HEAD"); |
2993 | 706 VALID_TAG ("/HEAD"); |
707 VALID_TAG ("BINARY"); | |
708 VALID_TAG ("/BINARY"); | |
5093 | 709 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
710 VALID_OPT_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
711 VALID_OPT_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
712 VALID_OPT_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
713 VALID_OPT_TAG ("A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
714 VALID_OPT_TAG ("IMG"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
715 VALID_OPT_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
716 VALID_OPT_TAG ("H3"); |
5093 | 717 VALID_OPT_TAG ("HTML"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
718 |
5101 | 719 VALID_TAG ("CITE"); |
720 VALID_TAG ("/CITE"); | |
721 VALID_TAG ("EM"); | |
722 VALID_TAG ("/EM"); | |
723 VALID_TAG ("STRONG"); | |
724 VALID_TAG ("/STRONG"); | |
725 | |
5104 | 726 VALID_OPT_TAG ("SPAN"); |
727 VALID_TAG ("/SPAN"); | |
728 | |
4793 | 729 if (!g_ascii_strncasecmp(string, "!--", strlen ("!--"))) { |
2954
f6c4f2187c08
[gaim-migrate @ 2967]
Christian Hammond <chipx86@chipx86.com>
parents:
2898
diff
changeset
|
730 gchar *e = strstr (string + strlen("!--"), "-->"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
731 if (e) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
732 *len = e - string + strlen ("-->"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
733 *tag = g_strndup (string + strlen ("!--"), *len - strlen ("!---->")); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
734 return TRUE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
735 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
736 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
737 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
738 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
739 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
740 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
741 static gchar* |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
742 gtk_imhtml_get_html_opt (gchar *tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
743 const gchar *opt) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
744 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
745 gchar *t = tag; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
746 gchar *e, *a; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
747 |
4793 | 748 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
749 gboolean quote = FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
750 if (*t == '\0') break; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
751 while (*t && !((*t == ' ') && !quote)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
752 if (*t == '\"') |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
753 quote = ! quote; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
754 t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
755 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
756 while (*t && (*t == ' ')) t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
757 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
758 |
4793 | 759 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
760 t += strlen (opt); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
761 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
762 return NULL; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
763 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
764 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
765 if ((*t == '\"') || (*t == '\'')) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
766 e = a = ++t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
767 while (*e && (*e != *(t - 1))) e++; |
2993 | 768 if (*e == '\0') { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
769 return NULL; |
2993 | 770 } else |
771 return g_strndup (a, e - a); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
772 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
773 e = a = t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
774 while (*e && !isspace ((gint) *e)) e++; |
2993 | 775 return g_strndup (a, e - a); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
776 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
777 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
778 |
3922 | 779 |
780 | |
781 #define NEW_TEXT_BIT 0 | |
4343 | 782 #define NEW_COMMENT_BIT 2 |
4895 | 783 #define NEW_SCALABLE_BIT 1 |
3922 | 784 #define NEW_BIT(x) ws [wpos] = '\0'; \ |
785 mark2 = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); \ | |
786 gtk_text_buffer_insert(imhtml->text_buffer, &iter, ws, -1); \ | |
4895 | 787 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ |
3922 | 788 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, mark2); \ |
789 gtk_text_buffer_delete_mark(imhtml->text_buffer, mark2); \ | |
790 if (bold) \ | |
791 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &siter, &iter); \ | |
4895 | 792 if (italics) \ |
3922 | 793 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &siter, &iter); \ |
794 if (underline) \ | |
795 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &siter, &iter); \ | |
796 if (strike) \ | |
797 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &siter, &iter); \ | |
798 if (sub) \ | |
799 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUB", &siter, &iter); \ | |
800 if (sup) \ | |
801 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUP", &siter, &iter); \ | |
802 if (pre) \ | |
803 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "PRE", &siter, &iter); \ | |
804 if (bg) { \ | |
805 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", bg, NULL); \ | |
806 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
807 } \ | |
808 if (fonts) { \ | |
809 FontDetail *fd = fonts->data; \ | |
810 if (fd->fore) { \ | |
811 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", fd->fore, NULL); \ | |
812 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
813 } \ | |
814 if (fd->back) { \ | |
815 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", fd->back, NULL); \ | |
816 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
817 } \ | |
818 if (fd->face) { \ | |
819 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "font", fd->face, NULL); \ | |
820 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
821 } \ | |
822 if (fd->size) { \ | |
823 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "size-points", (double)POINT_SIZE(fd->size), NULL); \ | |
824 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
825 } \ | |
826 } \ | |
827 if (url) { \ | |
828 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); \ | |
5012 | 829 g_signal_connect(G_OBJECT(texttag), "event", G_CALLBACK(tag_event), g_strdup(url)); \ |
3922 | 830 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ |
4735 | 831 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, NULL); \ |
832 g_object_set_data(G_OBJECT(texttag), "link_url", g_strdup(url)); \ | |
833 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
3922 | 834 } \ |
835 wpos = 0; \ | |
836 ws[0] = 0; \ | |
837 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4895 | 838 if (x == NEW_SCALABLE_BIT) { \ |
839 GdkRectangle rect; \ | |
840 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); \ | |
841 scalable->add_to(scalable, imhtml, &iter); \ | |
842 scalable->scale(scalable, rect.width, rect.height); \ | |
843 imhtml->scalables = g_list_append(imhtml->scalables, scalable); \ | |
844 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4343 | 845 } \ |
3922 | 846 |
4895 | 847 |
848 | |
3922 | 849 GString* gtk_imhtml_append_text (GtkIMHtml *imhtml, |
850 const gchar *text, | |
851 gint len, | |
852 GtkIMHtmlOptions options) | |
1428 | 853 { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
854 gint pos = 0; |
3922 | 855 GString *str = NULL; |
856 GtkTextIter iter, siter; | |
857 GtkTextMark *mark, *mark2; | |
858 GtkTextTag *texttag; | |
859 gchar *ws; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
860 gchar *tag; |
3922 | 861 gchar *url = NULL; |
862 gchar *bg = NULL; | |
4032 | 863 gint tlen, smilelen, wpos=0; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
864 gint type; |
3922 | 865 const gchar *c; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
866 gchar amp; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
867 |
1428 | 868 guint bold = 0, |
869 italics = 0, | |
870 underline = 0, | |
871 strike = 0, | |
872 sub = 0, | |
873 sup = 0, | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
874 title = 0, |
3922 | 875 pre = 0; |
1428 | 876 |
3922 | 877 GSList *fonts = NULL; |
1428 | 878 |
4612 | 879 GdkRectangle rect; |
880 int y, height; | |
881 | |
4895 | 882 GtkIMHtmlScalable *scalable = NULL; |
883 | |
1428 | 884 g_return_val_if_fail (imhtml != NULL, NULL); |
885 g_return_val_if_fail (GTK_IS_IMHTML (imhtml), NULL); | |
886 g_return_val_if_fail (text != NULL, NULL); | |
3922 | 887 g_return_val_if_fail (len != 0, NULL); |
888 | |
889 c = text; | |
890 if (len == -1) | |
891 len = strlen(text); | |
892 ws = g_malloc(len + 1); | |
893 ws[0] = 0; | |
1428 | 894 |
895 if (options & GTK_IMHTML_RETURN_LOG) | |
3922 | 896 str = g_string_new(""); |
1428 | 897 |
3922 | 898 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); |
899 mark = gtk_text_buffer_create_mark (imhtml->text_buffer, NULL, &iter, /* right grav */ FALSE); | |
4612 | 900 |
901 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
902 gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
903 | |
904 if(((y + height) - (rect.y + rect.height)) > height | |
905 && gtk_text_buffer_get_char_count(imhtml->text_buffer)){ | |
906 options |= GTK_IMHTML_NO_SCROLL; | |
907 } | |
908 | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
909 while (pos < len) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
910 if (*c == '<' && gtk_imhtml_is_tag (c + 1, &tag, &tlen, &type)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
911 c++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
912 pos++; |
3922 | 913 switch (type) |
914 { | |
915 case 1: /* B */ | |
916 case 2: /* BOLD */ | |
5101 | 917 case 54: /* STRONG */ |
3922 | 918 NEW_BIT (NEW_TEXT_BIT); |
919 bold++; | |
920 break; | |
921 case 3: /* /B */ | |
922 case 4: /* /BOLD */ | |
5101 | 923 case 55: /* /STRONG */ |
3922 | 924 NEW_BIT (NEW_TEXT_BIT); |
925 if (bold) | |
926 bold--; | |
927 break; | |
928 case 5: /* I */ | |
929 case 6: /* ITALIC */ | |
5101 | 930 case 52: /* EM */ |
3922 | 931 NEW_BIT (NEW_TEXT_BIT); |
932 italics++; | |
933 break; | |
934 case 7: /* /I */ | |
935 case 8: /* /ITALIC */ | |
5101 | 936 case 53: /* /EM */ |
3922 | 937 NEW_BIT (NEW_TEXT_BIT); |
938 if (italics) | |
939 italics--; | |
940 break; | |
941 case 9: /* U */ | |
942 case 10: /* UNDERLINE */ | |
943 NEW_BIT (NEW_TEXT_BIT); | |
944 underline++; | |
945 break; | |
946 case 11: /* /U */ | |
947 case 12: /* /UNDERLINE */ | |
948 NEW_BIT (NEW_TEXT_BIT); | |
949 if (underline) | |
950 underline--; | |
951 break; | |
952 case 13: /* S */ | |
953 case 14: /* STRIKE */ | |
954 NEW_BIT (NEW_TEXT_BIT); | |
955 strike++; | |
956 break; | |
957 case 15: /* /S */ | |
958 case 16: /* /STRIKE */ | |
959 NEW_BIT (NEW_TEXT_BIT); | |
960 if (strike) | |
961 strike--; | |
962 break; | |
963 case 17: /* SUB */ | |
964 NEW_BIT (NEW_TEXT_BIT); | |
965 sub++; | |
966 break; | |
967 case 18: /* /SUB */ | |
968 NEW_BIT (NEW_TEXT_BIT); | |
969 if (sub) | |
970 sub--; | |
971 break; | |
972 case 19: /* SUP */ | |
973 NEW_BIT (NEW_TEXT_BIT); | |
974 sup++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
975 break; |
3922 | 976 case 20: /* /SUP */ |
977 NEW_BIT (NEW_TEXT_BIT); | |
978 if (sup) | |
979 sup--; | |
980 break; | |
981 case 21: /* PRE */ | |
982 NEW_BIT (NEW_TEXT_BIT); | |
983 pre++; | |
984 break; | |
985 case 22: /* /PRE */ | |
986 NEW_BIT (NEW_TEXT_BIT); | |
987 if (pre) | |
988 pre--; | |
989 break; | |
990 case 23: /* TITLE */ | |
991 NEW_BIT (NEW_TEXT_BIT); | |
992 title++; | |
993 break; | |
994 case 24: /* /TITLE */ | |
995 if (title) { | |
996 if (options & GTK_IMHTML_NO_TITLE) { | |
997 wpos = 0; | |
998 ws [wpos] = '\0'; | |
999 } | |
1000 title--; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1001 } |
3922 | 1002 break; |
1003 case 25: /* BR */ | |
1004 ws[wpos] = '\n'; | |
1005 wpos++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1006 NEW_BIT (NEW_TEXT_BIT); |
3922 | 1007 break; |
1008 case 26: /* HR */ | |
1009 case 42: /* HR (opt) */ | |
1010 ws[wpos++] = '\n'; | |
4895 | 1011 scalable = gaim_hr_new(); |
1012 NEW_BIT(NEW_SCALABLE_BIT); | |
4343 | 1013 ws[wpos++] = '\n'; |
3922 | 1014 break; |
1015 case 27: /* /FONT */ | |
1016 if (fonts) { | |
1017 FontDetail *font = fonts->data; | |
1018 NEW_BIT (NEW_TEXT_BIT); | |
1019 fonts = g_slist_remove (fonts, font); | |
1020 if (font->face) | |
1021 g_free (font->face); | |
1022 if (font->fore) | |
1023 g_free (font->fore); | |
1024 if (font->back) | |
1025 g_free (font->back); | |
4032 | 1026 if (font->sml) |
1027 g_free (font->sml); | |
3922 | 1028 g_free (font); |
1029 } | |
1030 break; | |
1031 case 28: /* /A */ | |
1032 if (url) { | |
1033 NEW_BIT(NEW_TEXT_BIT); | |
1034 g_free(url); | |
1035 url = NULL; | |
2993 | 1036 break; |
1037 } | |
3922 | 1038 case 29: /* P */ |
1039 case 30: /* /P */ | |
1040 case 31: /* H3 */ | |
1041 case 32: /* /H3 */ | |
1042 case 33: /* HTML */ | |
1043 case 34: /* /HTML */ | |
1044 case 35: /* BODY */ | |
1045 case 36: /* /BODY */ | |
1046 case 37: /* FONT */ | |
1047 case 38: /* HEAD */ | |
1048 case 39: /* /HEAD */ | |
1049 break; | |
1050 case 40: /* BINARY */ | |
4895 | 1051 NEW_BIT (NEW_TEXT_BIT); |
4997 | 1052 while (pos < len && g_ascii_strncasecmp("</BINARY>", c, strlen("</BINARY>"))) { |
1053 c++; | |
1054 pos++; | |
1055 } | |
1056 c = c - tlen; /* Because it will add this later */ | |
4895 | 1057 break; |
3922 | 1058 case 41: /* /BINARY */ |
1059 break; | |
1060 case 43: /* FONT (opt) */ | |
1061 { | |
4032 | 1062 gchar *color, *back, *face, *size, *sml; |
3922 | 1063 FontDetail *font, *oldfont = NULL; |
1064 color = gtk_imhtml_get_html_opt (tag, "COLOR="); | |
1065 back = gtk_imhtml_get_html_opt (tag, "BACK="); | |
1066 face = gtk_imhtml_get_html_opt (tag, "FACE="); | |
1067 size = gtk_imhtml_get_html_opt (tag, "SIZE="); | |
4032 | 1068 sml = gtk_imhtml_get_html_opt (tag, "SML="); |
1069 if (!(color || back || face || size || sml)) | |
3922 | 1070 break; |
1071 | |
1072 NEW_BIT (NEW_TEXT_BIT); | |
1073 | |
1074 font = g_new0 (FontDetail, 1); | |
1075 if (fonts) | |
1076 oldfont = fonts->data; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1077 |
3922 | 1078 if (color && !(options & GTK_IMHTML_NO_COLOURS)) |
1079 font->fore = color; | |
1080 else if (oldfont && oldfont->fore) | |
1081 font->fore = g_strdup(oldfont->fore); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1082 |
3922 | 1083 if (back && !(options & GTK_IMHTML_NO_COLOURS)) |
1084 font->back = back; | |
1085 else if (oldfont && oldfont->back) | |
1086 font->back = g_strdup(oldfont->back); | |
1087 | |
1088 if (face && !(options & GTK_IMHTML_NO_FONTS)) | |
1089 font->face = face; | |
1090 else if (oldfont && oldfont->face) | |
1091 font->face = g_strdup(oldfont->face); | |
4629 | 1092 if (font->face && (atoi(font->face) > 100)) { |
1093 g_free(font->face); | |
1094 font->face = g_strdup("100"); | |
1095 } | |
4032 | 1096 |
1097 if (sml) | |
1098 font->sml = sml; | |
1099 else if (oldfont && oldfont->sml) | |
1100 font->sml = g_strdup(oldfont->sml); | |
1101 | |
3922 | 1102 if (size && !(options & GTK_IMHTML_NO_SIZES)) { |
1103 if (*size == '+') { | |
1104 sscanf (size + 1, "%hd", &font->size); | |
1105 font->size += 3; | |
1106 } else if (*size == '-') { | |
1107 sscanf (size + 1, "%hd", &font->size); | |
1108 font->size = MAX (0, 3 - font->size); | |
1109 } else if (isdigit (*size)) { | |
1110 sscanf (size, "%hd", &font->size); | |
1111 } | |
1112 } else if (oldfont) | |
1113 font->size = oldfont->size; | |
1114 g_free(size); | |
1115 fonts = g_slist_prepend (fonts, font); | |
1116 } | |
1117 break; | |
1118 case 44: /* BODY (opt) */ | |
1119 if (!(options & GTK_IMHTML_NO_COLOURS)) { | |
1120 char *bgcolor = gtk_imhtml_get_html_opt (tag, "BGCOLOR="); | |
1121 if (bgcolor) { | |
1122 NEW_BIT(NEW_TEXT_BIT); | |
1123 if (bg) | |
1124 g_free(bg); | |
1125 bg = bgcolor; | |
2885
f72efa29c109
[gaim-migrate @ 2898]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2871
diff
changeset
|
1126 } |
1428 | 1127 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1128 break; |
3922 | 1129 case 45: /* A (opt) */ |
1130 { | |
1131 gchar *href = gtk_imhtml_get_html_opt (tag, "HREF="); | |
1132 if (href) { | |
1133 NEW_BIT (NEW_TEXT_BIT); | |
1134 if (url) | |
1135 g_free (url); | |
1136 url = href; | |
1137 } | |
2993 | 1138 } |
3922 | 1139 break; |
4895 | 1140 case 46: /* IMG (opt) */ |
1141 { | |
1142 gchar *src = gtk_imhtml_get_html_opt (tag, "SRC="); | |
1143 gchar *id = gtk_imhtml_get_html_opt (tag, "ID="); | |
1144 gchar *datasize = gtk_imhtml_get_html_opt (tag, "DATASIZE="); | |
1145 gint im_len = datasize?atoi(datasize):0; | |
1146 | |
1147 if (src && id && im_len && im_len <= len - pos) { | |
1148 /* This is an embedded IM image, or is it? */ | |
1149 char *tmp = NULL; | |
1150 const char *alltext; | |
1151 guchar *imagedata = NULL; | |
1152 | |
1153 GdkPixbufLoader *load; | |
1154 GdkPixbuf *imagepb = NULL; | |
1155 GError *error = NULL; | |
1156 | |
1157 tmp = g_strdup_printf("<DATA ID=\"%s\" SIZE=\"%s\">", id, datasize); | |
1158 alltext = strstr(c, tmp); | |
1159 imagedata = g_memdup(alltext + strlen(tmp), im_len); | |
1160 | |
1161 g_free(tmp); | |
1162 | |
1163 load = gdk_pixbuf_loader_new(); | |
1164 if (!gdk_pixbuf_loader_write(load, imagedata, im_len, &error)){ | |
1165 fprintf(stderr, "IM Image corrupted or unreadable.: %s\n", error->message); | |
1166 } else { | |
1167 imagepb = gdk_pixbuf_loader_get_pixbuf(load); | |
1168 if (imagepb) { | |
5012 | 1169 scalable = gaim_im_image_new(imagepb, g_strdup(src)); |
4895 | 1170 NEW_BIT(NEW_SCALABLE_BIT); |
5012 | 1171 g_object_unref(imagepb); |
4895 | 1172 } |
1173 } | |
1174 | |
1175 gdk_pixbuf_loader_close(load, NULL); | |
1176 | |
1177 | |
1178 g_free(imagedata); | |
1179 g_free(id); | |
1180 g_free(datasize); | |
1181 g_free(src); | |
1182 | |
1183 break; | |
1184 } | |
1185 g_free(id); | |
1186 g_free(datasize); | |
1187 g_free(src); | |
1188 } | |
3922 | 1189 case 47: /* P (opt) */ |
1190 case 48: /* H3 (opt) */ | |
5093 | 1191 case 49: /* HTML (opt) */ |
5101 | 1192 case 50: /* CITE */ |
1193 case 51: /* /CITE */ | |
5104 | 1194 case 56: /* SPAN */ |
1195 case 57: /* /SPAN */ | |
2993 | 1196 break; |
5104 | 1197 case 58: /* comment */ |
3922 | 1198 NEW_BIT (NEW_TEXT_BIT); |
4253 | 1199 if (imhtml->show_comments) |
1200 wpos = g_snprintf (ws, len, "%s", tag); | |
3922 | 1201 NEW_BIT (NEW_COMMENT_BIT); |
1202 break; | |
1203 default: | |
1204 break; | |
2993 | 1205 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1206 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1207 pos += tlen; |
4138 | 1208 if(tag) |
1209 g_free(tag); /* This was allocated back in VALID_TAG() */ | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1210 } else if (*c == '&' && gtk_imhtml_is_amp_escape (c, &, &tlen)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1211 ws [wpos++] = amp; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1212 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1213 pos += tlen; |
1428 | 1214 } else if (*c == '\n') { |
1215 if (!(options & GTK_IMHTML_NO_NEWLINE)) { | |
3922 | 1216 ws[wpos] = '\n'; |
1217 wpos++; | |
1428 | 1218 NEW_BIT (NEW_TEXT_BIT); |
1219 } | |
1220 c++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1221 pos++; |
4253 | 1222 } else if (imhtml->show_smileys && (gtk_imhtml_is_smiley (imhtml, fonts, c, &smilelen) || gtk_imhtml_is_smiley(imhtml, NULL, c, &smilelen))) { |
4032 | 1223 FontDetail *fd; |
1224 gchar *sml = NULL; | |
1225 if (fonts) { | |
1226 fd = fonts->data; | |
1227 sml = fd->sml; | |
1228 } | |
1229 NEW_BIT (NEW_TEXT_BIT); | |
1230 wpos = g_snprintf (ws, smilelen + 1, "%s", c); | |
4263 | 1231 gtk_text_buffer_insert_pixbuf(imhtml->text_buffer, &iter, gtk_smiley_tree_image (imhtml, sml, ws)); |
4032 | 1232 c += smilelen; |
1233 pos += smilelen; | |
1234 wpos = 0; | |
1235 ws[0] = 0; | |
1236 } else if (*c) { | |
1428 | 1237 ws [wpos++] = *c++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1238 pos++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1239 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1240 break; |
1428 | 1241 } |
1242 } | |
3922 | 1243 |
1244 NEW_BIT(NEW_TEXT_BIT); | |
1428 | 1245 if (url) { |
1246 g_free (url); | |
3922 | 1247 if (str) |
1248 str = g_string_append (str, "</A>"); | |
1428 | 1249 } |
3922 | 1250 |
4032 | 1251 while (fonts) { |
1252 FontDetail *font = fonts->data; | |
1253 fonts = g_slist_remove (fonts, font); | |
1254 if (font->face) | |
1255 g_free (font->face); | |
1256 if (font->fore) | |
1257 g_free (font->fore); | |
1258 if (font->back) | |
1259 g_free (font->back); | |
1260 if (font->sml) | |
1261 g_free (font->sml); | |
1262 g_free (font); | |
1263 if (str) | |
1264 str = g_string_append (str, "</FONT>"); | |
1265 } | |
1266 | |
3922 | 1267 if (str) { |
1428 | 1268 while (bold) { |
3922 | 1269 str = g_string_append (str, "</B>"); |
1428 | 1270 bold--; |
1271 } | |
1272 while (italics) { | |
3922 | 1273 str = g_string_append (str, "</I>"); |
1428 | 1274 italics--; |
1275 } | |
1276 while (underline) { | |
3922 | 1277 str = g_string_append (str, "</U>"); |
1428 | 1278 underline--; |
1279 } | |
1280 while (strike) { | |
3922 | 1281 str = g_string_append (str, "</S>"); |
1428 | 1282 strike--; |
1283 } | |
1284 while (sub) { | |
3922 | 1285 str = g_string_append (str, "</SUB>"); |
1428 | 1286 sub--; |
1287 } | |
1288 while (sup) { | |
3922 | 1289 str = g_string_append (str, "</SUP>"); |
1428 | 1290 sup--; |
1291 } | |
1292 while (title) { | |
3922 | 1293 str = g_string_append (str, "</TITLE>"); |
1428 | 1294 title--; |
1295 } | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1296 while (pre) { |
3922 | 1297 str = g_string_append (str, "</PRE>"); |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1298 pre--; |
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1299 } |
1428 | 1300 } |
4032 | 1301 g_free (ws); |
4630 | 1302 if(bg) |
1303 g_free(bg); | |
4032 | 1304 if (!(options & GTK_IMHTML_NO_SCROLL)) |
1305 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (imhtml), mark, | |
1306 0, TRUE, 0.0, 1.0); | |
3922 | 1307 gtk_text_buffer_delete_mark (imhtml->text_buffer, mark); |
1308 return str; | |
1309 } | |
1310 | |
4892 | 1311 void gtk_imhtml_remove_smileys(GtkIMHtml *imhtml) |
1312 { | |
4288 | 1313 g_hash_table_destroy(imhtml->smiley_data); |
1314 gtk_smiley_tree_destroy(imhtml->default_smilies); | |
4892 | 1315 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 1316 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4288 | 1317 imhtml->default_smilies = gtk_smiley_tree_new(); |
1318 } | |
3922 | 1319 void gtk_imhtml_show_smileys (GtkIMHtml *imhtml, |
4253 | 1320 gboolean show) |
1321 { | |
1322 imhtml->show_smileys = show; | |
1323 } | |
3922 | 1324 |
1325 void gtk_imhtml_show_comments (GtkIMHtml *imhtml, | |
4253 | 1326 gboolean show) |
1327 { | |
1328 imhtml->show_comments = show; | |
1329 } | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1330 |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1331 void |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1332 gtk_imhtml_clear (GtkIMHtml *imhtml) |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1333 { |
3922 | 1334 GtkTextIter start, end; |
2993 | 1335 |
3922 | 1336 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
1337 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
1338 gtk_text_buffer_delete(imhtml->text_buffer, &start, &end); | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1339 } |
2363
08c66712364c
[gaim-migrate @ 2376]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2349
diff
changeset
|
1340 |
4046 | 1341 void gtk_imhtml_page_up (GtkIMHtml *imhtml) |
1342 { | |
1343 | |
1344 } | |
3922 | 1345 void gtk_imhtml_page_down (GtkIMHtml *imhtml){} |
4735 | 1346 |
1347 static gint | |
1348 gtk_imhtml_tip_paint (GtkIMHtml *imhtml) | |
1349 { | |
1350 PangoLayout *layout; | |
1351 | |
1352 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
1353 | |
1354 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
1355 | |
1356 gtk_paint_flat_box (imhtml->tip_window->style, imhtml->tip_window->window, | |
1357 GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, imhtml->tip_window, | |
1358 "tooltip", 0, 0, -1, -1); | |
1359 | |
1360 gtk_paint_layout (imhtml->tip_window->style, imhtml->tip_window->window, GTK_STATE_NORMAL, | |
1361 FALSE, NULL, imhtml->tip_window, NULL, 4, 4, layout); | |
1362 | |
4764 | 1363 g_object_unref(layout); |
4735 | 1364 return FALSE; |
1365 } | |
1366 | |
1367 static gint | |
1368 gtk_imhtml_tip (gpointer data) | |
1369 { | |
1370 GtkIMHtml *imhtml = data; | |
1371 PangoFontMetrics *font; | |
1372 PangoLayout *layout; | |
1373 | |
1374 gint gap, x, y, h, w, scr_w, baseline_skip; | |
1375 | |
1376 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
1377 | |
1378 if (!imhtml->tip || !GTK_WIDGET_DRAWABLE (GTK_WIDGET(imhtml))) { | |
1379 imhtml->tip_timer = 0; | |
1380 return FALSE; | |
1381 } | |
1382 | |
1383 if (imhtml->tip_window){ | |
1384 gtk_widget_destroy (imhtml->tip_window); | |
1385 imhtml->tip_window = NULL; | |
1386 } | |
1387 | |
1388 imhtml->tip_timer = 0; | |
1389 imhtml->tip_window = gtk_window_new (GTK_WINDOW_POPUP); | |
1390 gtk_widget_set_app_paintable (imhtml->tip_window, TRUE); | |
1391 gtk_window_set_resizable (GTK_WINDOW (imhtml->tip_window), FALSE); | |
1392 gtk_widget_set_name (imhtml->tip_window, "gtk-tooltips"); | |
1393 g_signal_connect_swapped (G_OBJECT (imhtml->tip_window), "expose_event", | |
1394 G_CALLBACK (gtk_imhtml_tip_paint), imhtml); | |
1395 | |
1396 gtk_widget_ensure_style (imhtml->tip_window); | |
1397 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
1398 font = pango_font_get_metrics(pango_context_load_font(pango_layout_get_context(layout), | |
1399 imhtml->tip_window->style->font_desc), | |
1400 NULL); | |
1401 | |
1402 | |
1403 pango_layout_get_pixel_size(layout, &scr_w, NULL); | |
1404 gap = PANGO_PIXELS((pango_font_metrics_get_ascent(font) + | |
1405 pango_font_metrics_get_descent(font))/ 4); | |
1406 | |
1407 if (gap < 2) | |
1408 gap = 2; | |
1409 baseline_skip = PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
1410 pango_font_metrics_get_descent(font)); | |
1411 w = 8 + scr_w; | |
1412 h = 8 + baseline_skip; | |
1413 | |
1414 gdk_window_get_pointer (NULL, &x, &y, NULL); | |
1415 if (GTK_WIDGET_NO_WINDOW (GTK_WIDGET(imhtml))) | |
1416 y += GTK_WIDGET(imhtml)->allocation.y; | |
1417 | |
1418 scr_w = gdk_screen_width(); | |
1419 | |
1420 x -= ((w >> 1) + 4); | |
1421 | |
1422 if ((x + w) > scr_w) | |
1423 x -= (x + w) - scr_w; | |
1424 else if (x < 0) | |
1425 x = 0; | |
1426 | |
1427 y = y + PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
1428 pango_font_metrics_get_descent(font)); | |
1429 | |
1430 gtk_widget_set_size_request (imhtml->tip_window, w, h); | |
1431 gtk_widget_show (imhtml->tip_window); | |
1432 gtk_window_move (GTK_WINDOW(imhtml->tip_window), x, y); | |
1433 | |
1434 pango_font_metrics_unref(font); | |
4895 | 1435 g_object_unref(layout); |
4735 | 1436 |
1437 return FALSE; | |
1438 } | |
4895 | 1439 |
4947 | 1440 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data) |
4895 | 1441 { |
4903 | 1442 GdkRectangle rect; |
4895 | 1443 |
4903 | 1444 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &rect); |
4947 | 1445 if(widget->old_rect.width != rect.width || widget->old_rect.height != rect.height){ |
4902 | 1446 GList *iter = GTK_IMHTML(widget)->scalables; |
4895 | 1447 |
1448 while(iter){ | |
1449 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(iter->data); | |
4903 | 1450 scale->scale(scale, rect.width, rect.height); |
4895 | 1451 |
1452 iter = iter->next; | |
1453 } | |
1454 } | |
1455 | |
4947 | 1456 widget->old_rect = rect; |
4895 | 1457 return FALSE; |
1458 } | |
1459 | |
1460 /* GtkIMHtmlScalable, gaim_im_image, gaim_hr */ | |
5012 | 1461 GtkIMHtmlScalable *gaim_im_image_new(GdkPixbuf *img, gchar *filename) |
4895 | 1462 { |
1463 gaim_im_image *im_image = g_malloc(sizeof(gaim_im_image)); | |
5012 | 1464 GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(img)); |
4895 | 1465 |
1466 GTK_IMHTML_SCALABLE(im_image)->scale = gaim_im_image_scale; | |
1467 GTK_IMHTML_SCALABLE(im_image)->add_to = gaim_im_image_add_to; | |
1468 GTK_IMHTML_SCALABLE(im_image)->free = gaim_im_image_free; | |
5046 | 1469 |
1470 im_image->pixbuf = img; | |
5012 | 1471 im_image->image = image; |
4895 | 1472 im_image->width = gdk_pixbuf_get_width(img); |
1473 im_image->height = gdk_pixbuf_get_height(img); | |
1474 im_image->mark = NULL; | |
5012 | 1475 im_image->filename = filename; |
4895 | 1476 |
5046 | 1477 g_object_ref(img); |
4895 | 1478 return GTK_IMHTML_SCALABLE(im_image); |
1479 } | |
1480 | |
1481 void gaim_im_image_scale(GtkIMHtmlScalable *scale, int width, int height) | |
1482 { | |
1483 gaim_im_image *image = (gaim_im_image *)scale; | |
1484 | |
1485 if(image->width > width || image->height > height){ | |
1486 GdkPixbuf *new_image = NULL; | |
1487 float factor; | |
1488 int new_width = image->width, new_height = image->height; | |
1489 | |
1490 if(image->width > width){ | |
1491 factor = (float)(width)/image->width; | |
1492 new_width = width; | |
1493 new_height = image->height * factor; | |
1494 } | |
1495 if(new_height > height){ | |
1496 factor = (float)(height)/new_height; | |
1497 new_height = height; | |
1498 new_width = new_width * factor; | |
1499 } | |
1500 | |
5046 | 1501 new_image = gdk_pixbuf_scale_simple(image->pixbuf, new_width, new_height, GDK_INTERP_BILINEAR); |
5012 | 1502 gtk_image_set_from_pixbuf(image->image, new_image); |
4895 | 1503 g_object_unref(G_OBJECT(new_image)); |
1504 } | |
1505 } | |
1506 | |
1507 void gaim_im_image_free(GtkIMHtmlScalable *scale) | |
1508 { | |
1509 gaim_im_image *image = (gaim_im_image *)scale; | |
5046 | 1510 |
1511 g_object_unref(image->pixbuf); | |
5012 | 1512 g_free(image->filename); |
4895 | 1513 g_free(scale); |
1514 } | |
1515 | |
1516 void gaim_im_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1517 { | |
1518 gaim_im_image *image = (gaim_im_image *)scale; | |
5012 | 1519 GtkWidget *box = gtk_event_box_new(); |
1520 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
4895 | 1521 |
5012 | 1522 gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(image->image)); |
1523 | |
1524 gtk_widget_show(GTK_WIDGET(image->image)); | |
1525 gtk_widget_show(box); | |
1526 | |
1527 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), box, anchor); | |
1528 g_signal_connect(G_OBJECT(box), "event", G_CALLBACK(gaim_im_image_clicked), image); | |
4895 | 1529 } |
1530 | |
1531 GtkIMHtmlScalable *gaim_hr_new() | |
1532 { | |
1533 gaim_hr *hr = g_malloc(sizeof(gaim_hr)); | |
1534 | |
1535 GTK_IMHTML_SCALABLE(hr)->scale = gaim_hr_scale; | |
1536 GTK_IMHTML_SCALABLE(hr)->add_to = gaim_hr_add_to; | |
1537 GTK_IMHTML_SCALABLE(hr)->free = gaim_hr_free; | |
1538 | |
1539 hr->sep = gtk_hseparator_new(); | |
1540 gtk_widget_set_size_request(hr->sep, 5000, 2); | |
1541 gtk_widget_show(hr->sep); | |
1542 | |
1543 return GTK_IMHTML_SCALABLE(hr); | |
1544 } | |
1545 | |
1546 void gaim_hr_scale(GtkIMHtmlScalable *scale, int width, int height) | |
1547 { | |
1548 gtk_widget_set_size_request(((gaim_hr *)scale)->sep, width, 2); | |
1549 } | |
1550 | |
1551 void gaim_hr_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1552 { | |
1553 gaim_hr *hr = (gaim_hr *)scale; | |
1554 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
1555 | |
1556 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), hr->sep, anchor); | |
1557 } | |
1558 | |
1559 void gaim_hr_free(GtkIMHtmlScalable *scale) | |
1560 { | |
1561 /* gtk_widget_destroy(((gaim_hr *)scale)->sep); */ | |
1562 g_free(scale); | |
1563 } | |
5012 | 1564 |
1565 static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel) | |
1566 { | |
1567 const gchar *filename = gtk_file_selection_get_filename(sel); | |
1568 gaim_im_image *image = g_object_get_data(G_OBJECT(sel), "gaim_im_image"); | |
1569 gchar *type = NULL; | |
5019 | 1570 GError *error = NULL; |
5015 | 1571 #if GTK_CHECK_VERSION(2,2,0) |
5012 | 1572 GSList *formats = gdk_pixbuf_get_formats(); |
1573 | |
1574 while(formats){ | |
1575 GdkPixbufFormat *format = formats->data; | |
1576 gchar **extensions = gdk_pixbuf_format_get_extensions(format); | |
1577 gpointer p = extensions; | |
1578 | |
1579 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ | |
1580 gchar *fmt_ext = extensions[0]; | |
1581 const gchar* file_ext = filename + strlen(filename) - strlen(fmt_ext); | |
1582 | |
1583 if(!strcmp(fmt_ext, file_ext)){ | |
1584 type = gdk_pixbuf_format_get_name(format); | |
1585 break; | |
1586 } | |
1587 | |
1588 extensions++; | |
1589 } | |
1590 | |
1591 g_strfreev(p); | |
1592 | |
1593 if(type) | |
1594 break; | |
1595 | |
1596 formats = formats->next; | |
1597 } | |
1598 | |
5020 | 1599 g_slist_free(formats); |
1600 #else | |
1601 /* this is really ugly code, but I think it will work */ | |
1602 char *basename = g_path_get_basename(filename); | |
1603 char *ext = strrchr(basename, '.'); | |
1604 | |
1605 if(ext) { | |
1606 ext++; | |
1607 if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) | |
1608 type = g_strdup("jpeg"); | |
1609 else if(!g_ascii_strcasecmp(ext, "png")) | |
1610 type = g_strdup("png"); | |
1611 } | |
1612 | |
1613 g_free(basename); | |
1614 #endif | |
1615 | |
5012 | 1616 /* If I can't find a valid type, I will just tell the user about it and then assume |
1617 it's a png */ | |
1618 if(!type){ | |
1619 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
1620 _("Gaim was unable to guess the image type base on the file extension supplied. Defaulting to PNG.")); | |
1621 type = g_strdup("png"); | |
1622 } | |
1623 | |
5046 | 1624 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); |
5012 | 1625 |
1626 if(error){ | |
1627 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
1628 _("Error saving image: %s"), error->message); | |
1629 g_error_free(error); | |
1630 } | |
1631 | |
1632 g_free(type); | |
1633 } | |
1634 | |
1635 static void gaim_im_image_save(GtkWidget *w, gaim_im_image *image) | |
1636 { | |
1637 GtkWidget *sel = gtk_file_selection_new(_("Gaim - Save Image")); | |
1638 | |
1639 gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename); | |
1640 g_object_set_data(G_OBJECT(sel), "gaim_im_image", image); | |
1641 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
1642 G_CALLBACK(write_img_to_file), sel); | |
1643 | |
1644 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
1645 G_CALLBACK(gtk_widget_destroy), sel); | |
1646 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked", | |
1647 G_CALLBACK(gtk_widget_destroy), sel); | |
1648 | |
1649 gtk_widget_show(sel); | |
1650 } | |
1651 | |
1652 static gboolean gaim_im_image_clicked(GtkWidget *w, GdkEvent *event, gaim_im_image *image) | |
1653 { | |
1654 GdkEventButton *event_button = (GdkEventButton *) event; | |
1655 | |
1656 if (event->type == GDK_BUTTON_RELEASE) { | |
1657 if(event_button->button == 3) { | |
1658 GtkWidget *img, *item, *menu; | |
1659 gchar *text = g_strdup_printf(_("_Save Image...")); | |
1660 menu = gtk_menu_new(); | |
1661 | |
1662 /* buttons and such */ | |
1663 img = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU); | |
1664 item = gtk_image_menu_item_new_with_mnemonic(text); | |
1665 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); | |
1666 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gaim_im_image_save), image); | |
1667 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
1668 | |
1669 gtk_widget_show_all(menu); | |
1670 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
1671 event_button->button, event_button->time); | |
1672 | |
1673 g_free(text); | |
1674 return TRUE; | |
1675 } | |
1676 } | |
1677 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) | |
1678 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
1679 be caught by the regular GtkTextView menu */ | |
1680 else | |
1681 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1682 | |
1683 } |