Mercurial > pidgin
annotate src/gtkimhtml.c @ 5097:2b9df97b2b21
[gaim-migrate @ 5459]
Daniel Yacob wrote a Amharic translation of gaim.
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Thu, 10 Apr 2003 17:37:50 +0000 |
parents | 89c0c811befa |
children | a7e9036cd46f |
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 |
4793 | 719 if (!g_ascii_strncasecmp(string, "!--", strlen ("!--"))) { |
2954
f6c4f2187c08
[gaim-migrate @ 2967]
Christian Hammond <chipx86@chipx86.com>
parents:
2898
diff
changeset
|
720 gchar *e = strstr (string + strlen("!--"), "-->"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
721 if (e) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
722 *len = e - string + strlen ("-->"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
723 *tag = g_strndup (string + strlen ("!--"), *len - strlen ("!---->")); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
724 return TRUE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
725 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
726 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
727 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
728 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
729 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
730 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
731 static gchar* |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
732 gtk_imhtml_get_html_opt (gchar *tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
733 const gchar *opt) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
734 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
735 gchar *t = tag; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
736 gchar *e, *a; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
737 |
4793 | 738 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
739 gboolean quote = FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
740 if (*t == '\0') break; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
741 while (*t && !((*t == ' ') && !quote)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
742 if (*t == '\"') |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
743 quote = ! quote; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
744 t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
745 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
746 while (*t && (*t == ' ')) t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
747 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
748 |
4793 | 749 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
750 t += strlen (opt); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
751 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
752 return NULL; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
753 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
754 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
755 if ((*t == '\"') || (*t == '\'')) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
756 e = a = ++t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
757 while (*e && (*e != *(t - 1))) e++; |
2993 | 758 if (*e == '\0') { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
759 return NULL; |
2993 | 760 } else |
761 return g_strndup (a, e - a); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
762 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
763 e = a = t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
764 while (*e && !isspace ((gint) *e)) e++; |
2993 | 765 return g_strndup (a, e - a); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
766 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
767 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
768 |
3922 | 769 |
770 | |
771 #define NEW_TEXT_BIT 0 | |
4343 | 772 #define NEW_COMMENT_BIT 2 |
4895 | 773 #define NEW_SCALABLE_BIT 1 |
3922 | 774 #define NEW_BIT(x) ws [wpos] = '\0'; \ |
775 mark2 = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, TRUE); \ | |
776 gtk_text_buffer_insert(imhtml->text_buffer, &iter, ws, -1); \ | |
4895 | 777 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ |
3922 | 778 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &siter, mark2); \ |
779 gtk_text_buffer_delete_mark(imhtml->text_buffer, mark2); \ | |
780 if (bold) \ | |
781 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &siter, &iter); \ | |
4895 | 782 if (italics) \ |
3922 | 783 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &siter, &iter); \ |
784 if (underline) \ | |
785 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &siter, &iter); \ | |
786 if (strike) \ | |
787 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &siter, &iter); \ | |
788 if (sub) \ | |
789 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUB", &siter, &iter); \ | |
790 if (sup) \ | |
791 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "SUP", &siter, &iter); \ | |
792 if (pre) \ | |
793 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "PRE", &siter, &iter); \ | |
794 if (bg) { \ | |
795 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", bg, NULL); \ | |
796 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
797 } \ | |
798 if (fonts) { \ | |
799 FontDetail *fd = fonts->data; \ | |
800 if (fd->fore) { \ | |
801 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", fd->fore, NULL); \ | |
802 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
803 } \ | |
804 if (fd->back) { \ | |
805 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "background", fd->back, NULL); \ | |
806 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
807 } \ | |
808 if (fd->face) { \ | |
809 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "font", fd->face, NULL); \ | |
810 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
811 } \ | |
812 if (fd->size) { \ | |
813 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "size-points", (double)POINT_SIZE(fd->size), NULL); \ | |
814 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
815 } \ | |
816 } \ | |
817 if (url) { \ | |
818 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); \ | |
5012 | 819 g_signal_connect(G_OBJECT(texttag), "event", G_CALLBACK(tag_event), g_strdup(url)); \ |
3922 | 820 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ |
4735 | 821 texttag = gtk_text_buffer_create_tag(imhtml->text_buffer, NULL, NULL); \ |
822 g_object_set_data(G_OBJECT(texttag), "link_url", g_strdup(url)); \ | |
823 gtk_text_buffer_apply_tag(imhtml->text_buffer, texttag, &siter, &iter); \ | |
3922 | 824 } \ |
825 wpos = 0; \ | |
826 ws[0] = 0; \ | |
827 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4895 | 828 if (x == NEW_SCALABLE_BIT) { \ |
829 GdkRectangle rect; \ | |
830 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); \ | |
831 scalable->add_to(scalable, imhtml, &iter); \ | |
832 scalable->scale(scalable, rect.width, rect.height); \ | |
833 imhtml->scalables = g_list_append(imhtml->scalables, scalable); \ | |
834 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); \ | |
4343 | 835 } \ |
3922 | 836 |
4895 | 837 |
838 | |
3922 | 839 GString* gtk_imhtml_append_text (GtkIMHtml *imhtml, |
840 const gchar *text, | |
841 gint len, | |
842 GtkIMHtmlOptions options) | |
1428 | 843 { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
844 gint pos = 0; |
3922 | 845 GString *str = NULL; |
846 GtkTextIter iter, siter; | |
847 GtkTextMark *mark, *mark2; | |
848 GtkTextTag *texttag; | |
849 gchar *ws; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
850 gchar *tag; |
3922 | 851 gchar *url = NULL; |
852 gchar *bg = NULL; | |
4032 | 853 gint tlen, smilelen, wpos=0; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
854 gint type; |
3922 | 855 const gchar *c; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
856 gchar amp; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
857 |
1428 | 858 guint bold = 0, |
859 italics = 0, | |
860 underline = 0, | |
861 strike = 0, | |
862 sub = 0, | |
863 sup = 0, | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
864 title = 0, |
3922 | 865 pre = 0; |
1428 | 866 |
3922 | 867 GSList *fonts = NULL; |
1428 | 868 |
4612 | 869 GdkRectangle rect; |
870 int y, height; | |
871 | |
4895 | 872 GtkIMHtmlScalable *scalable = NULL; |
873 | |
1428 | 874 g_return_val_if_fail (imhtml != NULL, NULL); |
875 g_return_val_if_fail (GTK_IS_IMHTML (imhtml), NULL); | |
876 g_return_val_if_fail (text != NULL, NULL); | |
3922 | 877 g_return_val_if_fail (len != 0, NULL); |
878 | |
879 c = text; | |
880 if (len == -1) | |
881 len = strlen(text); | |
882 ws = g_malloc(len + 1); | |
883 ws[0] = 0; | |
1428 | 884 |
885 if (options & GTK_IMHTML_RETURN_LOG) | |
3922 | 886 str = g_string_new(""); |
1428 | 887 |
3922 | 888 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); |
889 mark = gtk_text_buffer_create_mark (imhtml->text_buffer, NULL, &iter, /* right grav */ FALSE); | |
4612 | 890 |
891 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
892 gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
893 | |
894 if(((y + height) - (rect.y + rect.height)) > height | |
895 && gtk_text_buffer_get_char_count(imhtml->text_buffer)){ | |
896 options |= GTK_IMHTML_NO_SCROLL; | |
897 } | |
898 | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
899 while (pos < len) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
900 if (*c == '<' && gtk_imhtml_is_tag (c + 1, &tag, &tlen, &type)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
901 c++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
902 pos++; |
3922 | 903 switch (type) |
904 { | |
905 case 1: /* B */ | |
906 case 2: /* BOLD */ | |
907 NEW_BIT (NEW_TEXT_BIT); | |
908 bold++; | |
909 break; | |
910 case 3: /* /B */ | |
911 case 4: /* /BOLD */ | |
912 NEW_BIT (NEW_TEXT_BIT); | |
913 if (bold) | |
914 bold--; | |
915 break; | |
916 case 5: /* I */ | |
917 case 6: /* ITALIC */ | |
918 NEW_BIT (NEW_TEXT_BIT); | |
919 italics++; | |
920 break; | |
921 case 7: /* /I */ | |
922 case 8: /* /ITALIC */ | |
923 NEW_BIT (NEW_TEXT_BIT); | |
924 if (italics) | |
925 italics--; | |
926 break; | |
927 case 9: /* U */ | |
928 case 10: /* UNDERLINE */ | |
929 NEW_BIT (NEW_TEXT_BIT); | |
930 underline++; | |
931 break; | |
932 case 11: /* /U */ | |
933 case 12: /* /UNDERLINE */ | |
934 NEW_BIT (NEW_TEXT_BIT); | |
935 if (underline) | |
936 underline--; | |
937 break; | |
938 case 13: /* S */ | |
939 case 14: /* STRIKE */ | |
940 NEW_BIT (NEW_TEXT_BIT); | |
941 strike++; | |
942 break; | |
943 case 15: /* /S */ | |
944 case 16: /* /STRIKE */ | |
945 NEW_BIT (NEW_TEXT_BIT); | |
946 if (strike) | |
947 strike--; | |
948 break; | |
949 case 17: /* SUB */ | |
950 NEW_BIT (NEW_TEXT_BIT); | |
951 sub++; | |
952 break; | |
953 case 18: /* /SUB */ | |
954 NEW_BIT (NEW_TEXT_BIT); | |
955 if (sub) | |
956 sub--; | |
957 break; | |
958 case 19: /* SUP */ | |
959 NEW_BIT (NEW_TEXT_BIT); | |
960 sup++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
961 break; |
3922 | 962 case 20: /* /SUP */ |
963 NEW_BIT (NEW_TEXT_BIT); | |
964 if (sup) | |
965 sup--; | |
966 break; | |
967 case 21: /* PRE */ | |
968 NEW_BIT (NEW_TEXT_BIT); | |
969 pre++; | |
970 break; | |
971 case 22: /* /PRE */ | |
972 NEW_BIT (NEW_TEXT_BIT); | |
973 if (pre) | |
974 pre--; | |
975 break; | |
976 case 23: /* TITLE */ | |
977 NEW_BIT (NEW_TEXT_BIT); | |
978 title++; | |
979 break; | |
980 case 24: /* /TITLE */ | |
981 if (title) { | |
982 if (options & GTK_IMHTML_NO_TITLE) { | |
983 wpos = 0; | |
984 ws [wpos] = '\0'; | |
985 } | |
986 title--; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
987 } |
3922 | 988 break; |
989 case 25: /* BR */ | |
990 ws[wpos] = '\n'; | |
991 wpos++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
992 NEW_BIT (NEW_TEXT_BIT); |
3922 | 993 break; |
994 case 26: /* HR */ | |
995 case 42: /* HR (opt) */ | |
996 ws[wpos++] = '\n'; | |
4895 | 997 scalable = gaim_hr_new(); |
998 NEW_BIT(NEW_SCALABLE_BIT); | |
4343 | 999 ws[wpos++] = '\n'; |
3922 | 1000 break; |
1001 case 27: /* /FONT */ | |
1002 if (fonts) { | |
1003 FontDetail *font = fonts->data; | |
1004 NEW_BIT (NEW_TEXT_BIT); | |
1005 fonts = g_slist_remove (fonts, font); | |
1006 if (font->face) | |
1007 g_free (font->face); | |
1008 if (font->fore) | |
1009 g_free (font->fore); | |
1010 if (font->back) | |
1011 g_free (font->back); | |
4032 | 1012 if (font->sml) |
1013 g_free (font->sml); | |
3922 | 1014 g_free (font); |
1015 } | |
1016 break; | |
1017 case 28: /* /A */ | |
1018 if (url) { | |
1019 NEW_BIT(NEW_TEXT_BIT); | |
1020 g_free(url); | |
1021 url = NULL; | |
2993 | 1022 break; |
1023 } | |
3922 | 1024 case 29: /* P */ |
1025 case 30: /* /P */ | |
1026 case 31: /* H3 */ | |
1027 case 32: /* /H3 */ | |
1028 case 33: /* HTML */ | |
1029 case 34: /* /HTML */ | |
1030 case 35: /* BODY */ | |
1031 case 36: /* /BODY */ | |
1032 case 37: /* FONT */ | |
1033 case 38: /* HEAD */ | |
1034 case 39: /* /HEAD */ | |
1035 break; | |
1036 case 40: /* BINARY */ | |
4895 | 1037 NEW_BIT (NEW_TEXT_BIT); |
4997 | 1038 while (pos < len && g_ascii_strncasecmp("</BINARY>", c, strlen("</BINARY>"))) { |
1039 c++; | |
1040 pos++; | |
1041 } | |
1042 c = c - tlen; /* Because it will add this later */ | |
4895 | 1043 break; |
3922 | 1044 case 41: /* /BINARY */ |
1045 break; | |
1046 case 43: /* FONT (opt) */ | |
1047 { | |
4032 | 1048 gchar *color, *back, *face, *size, *sml; |
3922 | 1049 FontDetail *font, *oldfont = NULL; |
1050 color = gtk_imhtml_get_html_opt (tag, "COLOR="); | |
1051 back = gtk_imhtml_get_html_opt (tag, "BACK="); | |
1052 face = gtk_imhtml_get_html_opt (tag, "FACE="); | |
1053 size = gtk_imhtml_get_html_opt (tag, "SIZE="); | |
4032 | 1054 sml = gtk_imhtml_get_html_opt (tag, "SML="); |
1055 if (!(color || back || face || size || sml)) | |
3922 | 1056 break; |
1057 | |
1058 NEW_BIT (NEW_TEXT_BIT); | |
1059 | |
1060 font = g_new0 (FontDetail, 1); | |
1061 if (fonts) | |
1062 oldfont = fonts->data; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1063 |
3922 | 1064 if (color && !(options & GTK_IMHTML_NO_COLOURS)) |
1065 font->fore = color; | |
1066 else if (oldfont && oldfont->fore) | |
1067 font->fore = g_strdup(oldfont->fore); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1068 |
3922 | 1069 if (back && !(options & GTK_IMHTML_NO_COLOURS)) |
1070 font->back = back; | |
1071 else if (oldfont && oldfont->back) | |
1072 font->back = g_strdup(oldfont->back); | |
1073 | |
1074 if (face && !(options & GTK_IMHTML_NO_FONTS)) | |
1075 font->face = face; | |
1076 else if (oldfont && oldfont->face) | |
1077 font->face = g_strdup(oldfont->face); | |
4629 | 1078 if (font->face && (atoi(font->face) > 100)) { |
1079 g_free(font->face); | |
1080 font->face = g_strdup("100"); | |
1081 } | |
4032 | 1082 |
1083 if (sml) | |
1084 font->sml = sml; | |
1085 else if (oldfont && oldfont->sml) | |
1086 font->sml = g_strdup(oldfont->sml); | |
1087 | |
3922 | 1088 if (size && !(options & GTK_IMHTML_NO_SIZES)) { |
1089 if (*size == '+') { | |
1090 sscanf (size + 1, "%hd", &font->size); | |
1091 font->size += 3; | |
1092 } else if (*size == '-') { | |
1093 sscanf (size + 1, "%hd", &font->size); | |
1094 font->size = MAX (0, 3 - font->size); | |
1095 } else if (isdigit (*size)) { | |
1096 sscanf (size, "%hd", &font->size); | |
1097 } | |
1098 } else if (oldfont) | |
1099 font->size = oldfont->size; | |
1100 g_free(size); | |
1101 fonts = g_slist_prepend (fonts, font); | |
1102 } | |
1103 break; | |
1104 case 44: /* BODY (opt) */ | |
1105 if (!(options & GTK_IMHTML_NO_COLOURS)) { | |
1106 char *bgcolor = gtk_imhtml_get_html_opt (tag, "BGCOLOR="); | |
1107 if (bgcolor) { | |
1108 NEW_BIT(NEW_TEXT_BIT); | |
1109 if (bg) | |
1110 g_free(bg); | |
1111 bg = bgcolor; | |
2885
f72efa29c109
[gaim-migrate @ 2898]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2871
diff
changeset
|
1112 } |
1428 | 1113 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1114 break; |
3922 | 1115 case 45: /* A (opt) */ |
1116 { | |
1117 gchar *href = gtk_imhtml_get_html_opt (tag, "HREF="); | |
1118 if (href) { | |
1119 NEW_BIT (NEW_TEXT_BIT); | |
1120 if (url) | |
1121 g_free (url); | |
1122 url = href; | |
1123 } | |
2993 | 1124 } |
3922 | 1125 break; |
4895 | 1126 case 46: /* IMG (opt) */ |
1127 { | |
1128 gchar *src = gtk_imhtml_get_html_opt (tag, "SRC="); | |
1129 gchar *id = gtk_imhtml_get_html_opt (tag, "ID="); | |
1130 gchar *datasize = gtk_imhtml_get_html_opt (tag, "DATASIZE="); | |
1131 gint im_len = datasize?atoi(datasize):0; | |
1132 | |
1133 if (src && id && im_len && im_len <= len - pos) { | |
1134 /* This is an embedded IM image, or is it? */ | |
1135 char *tmp = NULL; | |
1136 const char *alltext; | |
1137 guchar *imagedata = NULL; | |
1138 | |
1139 GdkPixbufLoader *load; | |
1140 GdkPixbuf *imagepb = NULL; | |
1141 GError *error = NULL; | |
1142 | |
1143 tmp = g_strdup_printf("<DATA ID=\"%s\" SIZE=\"%s\">", id, datasize); | |
1144 alltext = strstr(c, tmp); | |
1145 imagedata = g_memdup(alltext + strlen(tmp), im_len); | |
1146 | |
1147 g_free(tmp); | |
1148 | |
1149 load = gdk_pixbuf_loader_new(); | |
1150 if (!gdk_pixbuf_loader_write(load, imagedata, im_len, &error)){ | |
1151 fprintf(stderr, "IM Image corrupted or unreadable.: %s\n", error->message); | |
1152 } else { | |
1153 imagepb = gdk_pixbuf_loader_get_pixbuf(load); | |
1154 if (imagepb) { | |
5012 | 1155 scalable = gaim_im_image_new(imagepb, g_strdup(src)); |
4895 | 1156 NEW_BIT(NEW_SCALABLE_BIT); |
5012 | 1157 g_object_unref(imagepb); |
4895 | 1158 } |
1159 } | |
1160 | |
1161 gdk_pixbuf_loader_close(load, NULL); | |
1162 | |
1163 | |
1164 g_free(imagedata); | |
1165 g_free(id); | |
1166 g_free(datasize); | |
1167 g_free(src); | |
1168 | |
1169 break; | |
1170 } | |
1171 g_free(id); | |
1172 g_free(datasize); | |
1173 g_free(src); | |
1174 } | |
3922 | 1175 case 47: /* P (opt) */ |
1176 case 48: /* H3 (opt) */ | |
5093 | 1177 case 49: /* HTML (opt) */ |
2993 | 1178 break; |
5093 | 1179 case 50: /* comment */ |
3922 | 1180 NEW_BIT (NEW_TEXT_BIT); |
4253 | 1181 if (imhtml->show_comments) |
1182 wpos = g_snprintf (ws, len, "%s", tag); | |
3922 | 1183 NEW_BIT (NEW_COMMENT_BIT); |
1184 break; | |
1185 default: | |
1186 break; | |
2993 | 1187 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1188 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1189 pos += tlen; |
4138 | 1190 if(tag) |
1191 g_free(tag); /* This was allocated back in VALID_TAG() */ | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1192 } else if (*c == '&' && gtk_imhtml_is_amp_escape (c, &, &tlen)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1193 ws [wpos++] = amp; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1194 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1195 pos += tlen; |
1428 | 1196 } else if (*c == '\n') { |
1197 if (!(options & GTK_IMHTML_NO_NEWLINE)) { | |
3922 | 1198 ws[wpos] = '\n'; |
1199 wpos++; | |
1428 | 1200 NEW_BIT (NEW_TEXT_BIT); |
1201 } | |
1202 c++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1203 pos++; |
4253 | 1204 } else if (imhtml->show_smileys && (gtk_imhtml_is_smiley (imhtml, fonts, c, &smilelen) || gtk_imhtml_is_smiley(imhtml, NULL, c, &smilelen))) { |
4032 | 1205 FontDetail *fd; |
1206 gchar *sml = NULL; | |
1207 if (fonts) { | |
1208 fd = fonts->data; | |
1209 sml = fd->sml; | |
1210 } | |
1211 NEW_BIT (NEW_TEXT_BIT); | |
1212 wpos = g_snprintf (ws, smilelen + 1, "%s", c); | |
4263 | 1213 gtk_text_buffer_insert_pixbuf(imhtml->text_buffer, &iter, gtk_smiley_tree_image (imhtml, sml, ws)); |
4032 | 1214 c += smilelen; |
1215 pos += smilelen; | |
1216 wpos = 0; | |
1217 ws[0] = 0; | |
1218 } else if (*c) { | |
1428 | 1219 ws [wpos++] = *c++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1220 pos++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1221 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1222 break; |
1428 | 1223 } |
1224 } | |
3922 | 1225 |
1226 NEW_BIT(NEW_TEXT_BIT); | |
1428 | 1227 if (url) { |
1228 g_free (url); | |
3922 | 1229 if (str) |
1230 str = g_string_append (str, "</A>"); | |
1428 | 1231 } |
3922 | 1232 |
4032 | 1233 while (fonts) { |
1234 FontDetail *font = fonts->data; | |
1235 fonts = g_slist_remove (fonts, font); | |
1236 if (font->face) | |
1237 g_free (font->face); | |
1238 if (font->fore) | |
1239 g_free (font->fore); | |
1240 if (font->back) | |
1241 g_free (font->back); | |
1242 if (font->sml) | |
1243 g_free (font->sml); | |
1244 g_free (font); | |
1245 if (str) | |
1246 str = g_string_append (str, "</FONT>"); | |
1247 } | |
1248 | |
3922 | 1249 if (str) { |
1428 | 1250 while (bold) { |
3922 | 1251 str = g_string_append (str, "</B>"); |
1428 | 1252 bold--; |
1253 } | |
1254 while (italics) { | |
3922 | 1255 str = g_string_append (str, "</I>"); |
1428 | 1256 italics--; |
1257 } | |
1258 while (underline) { | |
3922 | 1259 str = g_string_append (str, "</U>"); |
1428 | 1260 underline--; |
1261 } | |
1262 while (strike) { | |
3922 | 1263 str = g_string_append (str, "</S>"); |
1428 | 1264 strike--; |
1265 } | |
1266 while (sub) { | |
3922 | 1267 str = g_string_append (str, "</SUB>"); |
1428 | 1268 sub--; |
1269 } | |
1270 while (sup) { | |
3922 | 1271 str = g_string_append (str, "</SUP>"); |
1428 | 1272 sup--; |
1273 } | |
1274 while (title) { | |
3922 | 1275 str = g_string_append (str, "</TITLE>"); |
1428 | 1276 title--; |
1277 } | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1278 while (pre) { |
3922 | 1279 str = g_string_append (str, "</PRE>"); |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1280 pre--; |
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1281 } |
1428 | 1282 } |
4032 | 1283 g_free (ws); |
4630 | 1284 if(bg) |
1285 g_free(bg); | |
4032 | 1286 if (!(options & GTK_IMHTML_NO_SCROLL)) |
1287 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (imhtml), mark, | |
1288 0, TRUE, 0.0, 1.0); | |
3922 | 1289 gtk_text_buffer_delete_mark (imhtml->text_buffer, mark); |
1290 return str; | |
1291 } | |
1292 | |
4892 | 1293 void gtk_imhtml_remove_smileys(GtkIMHtml *imhtml) |
1294 { | |
4288 | 1295 g_hash_table_destroy(imhtml->smiley_data); |
1296 gtk_smiley_tree_destroy(imhtml->default_smilies); | |
4892 | 1297 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 1298 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4288 | 1299 imhtml->default_smilies = gtk_smiley_tree_new(); |
1300 } | |
3922 | 1301 void gtk_imhtml_show_smileys (GtkIMHtml *imhtml, |
4253 | 1302 gboolean show) |
1303 { | |
1304 imhtml->show_smileys = show; | |
1305 } | |
3922 | 1306 |
1307 void gtk_imhtml_show_comments (GtkIMHtml *imhtml, | |
4253 | 1308 gboolean show) |
1309 { | |
1310 imhtml->show_comments = show; | |
1311 } | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1312 |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1313 void |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1314 gtk_imhtml_clear (GtkIMHtml *imhtml) |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1315 { |
3922 | 1316 GtkTextIter start, end; |
2993 | 1317 |
3922 | 1318 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
1319 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
1320 gtk_text_buffer_delete(imhtml->text_buffer, &start, &end); | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
1321 } |
2363
08c66712364c
[gaim-migrate @ 2376]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2349
diff
changeset
|
1322 |
4046 | 1323 void gtk_imhtml_page_up (GtkIMHtml *imhtml) |
1324 { | |
1325 | |
1326 } | |
3922 | 1327 void gtk_imhtml_page_down (GtkIMHtml *imhtml){} |
4735 | 1328 |
1329 static gint | |
1330 gtk_imhtml_tip_paint (GtkIMHtml *imhtml) | |
1331 { | |
1332 PangoLayout *layout; | |
1333 | |
1334 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
1335 | |
1336 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
1337 | |
1338 gtk_paint_flat_box (imhtml->tip_window->style, imhtml->tip_window->window, | |
1339 GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, imhtml->tip_window, | |
1340 "tooltip", 0, 0, -1, -1); | |
1341 | |
1342 gtk_paint_layout (imhtml->tip_window->style, imhtml->tip_window->window, GTK_STATE_NORMAL, | |
1343 FALSE, NULL, imhtml->tip_window, NULL, 4, 4, layout); | |
1344 | |
4764 | 1345 g_object_unref(layout); |
4735 | 1346 return FALSE; |
1347 } | |
1348 | |
1349 static gint | |
1350 gtk_imhtml_tip (gpointer data) | |
1351 { | |
1352 GtkIMHtml *imhtml = data; | |
1353 PangoFontMetrics *font; | |
1354 PangoLayout *layout; | |
1355 | |
1356 gint gap, x, y, h, w, scr_w, baseline_skip; | |
1357 | |
1358 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
1359 | |
1360 if (!imhtml->tip || !GTK_WIDGET_DRAWABLE (GTK_WIDGET(imhtml))) { | |
1361 imhtml->tip_timer = 0; | |
1362 return FALSE; | |
1363 } | |
1364 | |
1365 if (imhtml->tip_window){ | |
1366 gtk_widget_destroy (imhtml->tip_window); | |
1367 imhtml->tip_window = NULL; | |
1368 } | |
1369 | |
1370 imhtml->tip_timer = 0; | |
1371 imhtml->tip_window = gtk_window_new (GTK_WINDOW_POPUP); | |
1372 gtk_widget_set_app_paintable (imhtml->tip_window, TRUE); | |
1373 gtk_window_set_resizable (GTK_WINDOW (imhtml->tip_window), FALSE); | |
1374 gtk_widget_set_name (imhtml->tip_window, "gtk-tooltips"); | |
1375 g_signal_connect_swapped (G_OBJECT (imhtml->tip_window), "expose_event", | |
1376 G_CALLBACK (gtk_imhtml_tip_paint), imhtml); | |
1377 | |
1378 gtk_widget_ensure_style (imhtml->tip_window); | |
1379 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
1380 font = pango_font_get_metrics(pango_context_load_font(pango_layout_get_context(layout), | |
1381 imhtml->tip_window->style->font_desc), | |
1382 NULL); | |
1383 | |
1384 | |
1385 pango_layout_get_pixel_size(layout, &scr_w, NULL); | |
1386 gap = PANGO_PIXELS((pango_font_metrics_get_ascent(font) + | |
1387 pango_font_metrics_get_descent(font))/ 4); | |
1388 | |
1389 if (gap < 2) | |
1390 gap = 2; | |
1391 baseline_skip = PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
1392 pango_font_metrics_get_descent(font)); | |
1393 w = 8 + scr_w; | |
1394 h = 8 + baseline_skip; | |
1395 | |
1396 gdk_window_get_pointer (NULL, &x, &y, NULL); | |
1397 if (GTK_WIDGET_NO_WINDOW (GTK_WIDGET(imhtml))) | |
1398 y += GTK_WIDGET(imhtml)->allocation.y; | |
1399 | |
1400 scr_w = gdk_screen_width(); | |
1401 | |
1402 x -= ((w >> 1) + 4); | |
1403 | |
1404 if ((x + w) > scr_w) | |
1405 x -= (x + w) - scr_w; | |
1406 else if (x < 0) | |
1407 x = 0; | |
1408 | |
1409 y = y + PANGO_PIXELS(pango_font_metrics_get_ascent(font) + | |
1410 pango_font_metrics_get_descent(font)); | |
1411 | |
1412 gtk_widget_set_size_request (imhtml->tip_window, w, h); | |
1413 gtk_widget_show (imhtml->tip_window); | |
1414 gtk_window_move (GTK_WINDOW(imhtml->tip_window), x, y); | |
1415 | |
1416 pango_font_metrics_unref(font); | |
4895 | 1417 g_object_unref(layout); |
4735 | 1418 |
1419 return FALSE; | |
1420 } | |
4895 | 1421 |
4947 | 1422 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data) |
4895 | 1423 { |
4903 | 1424 GdkRectangle rect; |
4895 | 1425 |
4903 | 1426 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &rect); |
4947 | 1427 if(widget->old_rect.width != rect.width || widget->old_rect.height != rect.height){ |
4902 | 1428 GList *iter = GTK_IMHTML(widget)->scalables; |
4895 | 1429 |
1430 while(iter){ | |
1431 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(iter->data); | |
4903 | 1432 scale->scale(scale, rect.width, rect.height); |
4895 | 1433 |
1434 iter = iter->next; | |
1435 } | |
1436 } | |
1437 | |
4947 | 1438 widget->old_rect = rect; |
4895 | 1439 return FALSE; |
1440 } | |
1441 | |
1442 /* GtkIMHtmlScalable, gaim_im_image, gaim_hr */ | |
5012 | 1443 GtkIMHtmlScalable *gaim_im_image_new(GdkPixbuf *img, gchar *filename) |
4895 | 1444 { |
1445 gaim_im_image *im_image = g_malloc(sizeof(gaim_im_image)); | |
5012 | 1446 GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(img)); |
4895 | 1447 |
1448 GTK_IMHTML_SCALABLE(im_image)->scale = gaim_im_image_scale; | |
1449 GTK_IMHTML_SCALABLE(im_image)->add_to = gaim_im_image_add_to; | |
1450 GTK_IMHTML_SCALABLE(im_image)->free = gaim_im_image_free; | |
5046 | 1451 |
1452 im_image->pixbuf = img; | |
5012 | 1453 im_image->image = image; |
4895 | 1454 im_image->width = gdk_pixbuf_get_width(img); |
1455 im_image->height = gdk_pixbuf_get_height(img); | |
1456 im_image->mark = NULL; | |
5012 | 1457 im_image->filename = filename; |
4895 | 1458 |
5046 | 1459 g_object_ref(img); |
4895 | 1460 return GTK_IMHTML_SCALABLE(im_image); |
1461 } | |
1462 | |
1463 void gaim_im_image_scale(GtkIMHtmlScalable *scale, int width, int height) | |
1464 { | |
1465 gaim_im_image *image = (gaim_im_image *)scale; | |
1466 | |
1467 if(image->width > width || image->height > height){ | |
1468 GdkPixbuf *new_image = NULL; | |
1469 float factor; | |
1470 int new_width = image->width, new_height = image->height; | |
1471 | |
1472 if(image->width > width){ | |
1473 factor = (float)(width)/image->width; | |
1474 new_width = width; | |
1475 new_height = image->height * factor; | |
1476 } | |
1477 if(new_height > height){ | |
1478 factor = (float)(height)/new_height; | |
1479 new_height = height; | |
1480 new_width = new_width * factor; | |
1481 } | |
1482 | |
5046 | 1483 new_image = gdk_pixbuf_scale_simple(image->pixbuf, new_width, new_height, GDK_INTERP_BILINEAR); |
5012 | 1484 gtk_image_set_from_pixbuf(image->image, new_image); |
4895 | 1485 g_object_unref(G_OBJECT(new_image)); |
1486 } | |
1487 } | |
1488 | |
1489 void gaim_im_image_free(GtkIMHtmlScalable *scale) | |
1490 { | |
1491 gaim_im_image *image = (gaim_im_image *)scale; | |
5046 | 1492 |
1493 g_object_unref(image->pixbuf); | |
5012 | 1494 g_free(image->filename); |
4895 | 1495 g_free(scale); |
1496 } | |
1497 | |
1498 void gaim_im_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1499 { | |
1500 gaim_im_image *image = (gaim_im_image *)scale; | |
5012 | 1501 GtkWidget *box = gtk_event_box_new(); |
1502 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
4895 | 1503 |
5012 | 1504 gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(image->image)); |
1505 | |
1506 gtk_widget_show(GTK_WIDGET(image->image)); | |
1507 gtk_widget_show(box); | |
1508 | |
1509 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), box, anchor); | |
1510 g_signal_connect(G_OBJECT(box), "event", G_CALLBACK(gaim_im_image_clicked), image); | |
4895 | 1511 } |
1512 | |
1513 GtkIMHtmlScalable *gaim_hr_new() | |
1514 { | |
1515 gaim_hr *hr = g_malloc(sizeof(gaim_hr)); | |
1516 | |
1517 GTK_IMHTML_SCALABLE(hr)->scale = gaim_hr_scale; | |
1518 GTK_IMHTML_SCALABLE(hr)->add_to = gaim_hr_add_to; | |
1519 GTK_IMHTML_SCALABLE(hr)->free = gaim_hr_free; | |
1520 | |
1521 hr->sep = gtk_hseparator_new(); | |
1522 gtk_widget_set_size_request(hr->sep, 5000, 2); | |
1523 gtk_widget_show(hr->sep); | |
1524 | |
1525 return GTK_IMHTML_SCALABLE(hr); | |
1526 } | |
1527 | |
1528 void gaim_hr_scale(GtkIMHtmlScalable *scale, int width, int height) | |
1529 { | |
1530 gtk_widget_set_size_request(((gaim_hr *)scale)->sep, width, 2); | |
1531 } | |
1532 | |
1533 void gaim_hr_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
1534 { | |
1535 gaim_hr *hr = (gaim_hr *)scale; | |
1536 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
1537 | |
1538 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), hr->sep, anchor); | |
1539 } | |
1540 | |
1541 void gaim_hr_free(GtkIMHtmlScalable *scale) | |
1542 { | |
1543 /* gtk_widget_destroy(((gaim_hr *)scale)->sep); */ | |
1544 g_free(scale); | |
1545 } | |
5012 | 1546 |
1547 static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel) | |
1548 { | |
1549 const gchar *filename = gtk_file_selection_get_filename(sel); | |
1550 gaim_im_image *image = g_object_get_data(G_OBJECT(sel), "gaim_im_image"); | |
1551 gchar *type = NULL; | |
5019 | 1552 GError *error = NULL; |
5015 | 1553 #if GTK_CHECK_VERSION(2,2,0) |
5012 | 1554 GSList *formats = gdk_pixbuf_get_formats(); |
1555 | |
1556 while(formats){ | |
1557 GdkPixbufFormat *format = formats->data; | |
1558 gchar **extensions = gdk_pixbuf_format_get_extensions(format); | |
1559 gpointer p = extensions; | |
1560 | |
1561 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ | |
1562 gchar *fmt_ext = extensions[0]; | |
1563 const gchar* file_ext = filename + strlen(filename) - strlen(fmt_ext); | |
1564 | |
1565 if(!strcmp(fmt_ext, file_ext)){ | |
1566 type = gdk_pixbuf_format_get_name(format); | |
1567 break; | |
1568 } | |
1569 | |
1570 extensions++; | |
1571 } | |
1572 | |
1573 g_strfreev(p); | |
1574 | |
1575 if(type) | |
1576 break; | |
1577 | |
1578 formats = formats->next; | |
1579 } | |
1580 | |
5020 | 1581 g_slist_free(formats); |
1582 #else | |
1583 /* this is really ugly code, but I think it will work */ | |
1584 char *basename = g_path_get_basename(filename); | |
1585 char *ext = strrchr(basename, '.'); | |
1586 | |
1587 if(ext) { | |
1588 ext++; | |
1589 if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) | |
1590 type = g_strdup("jpeg"); | |
1591 else if(!g_ascii_strcasecmp(ext, "png")) | |
1592 type = g_strdup("png"); | |
1593 } | |
1594 | |
1595 g_free(basename); | |
1596 #endif | |
1597 | |
5012 | 1598 /* If I can't find a valid type, I will just tell the user about it and then assume |
1599 it's a png */ | |
1600 if(!type){ | |
1601 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
1602 _("Gaim was unable to guess the image type base on the file extension supplied. Defaulting to PNG.")); | |
1603 type = g_strdup("png"); | |
1604 } | |
1605 | |
5046 | 1606 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); |
5012 | 1607 |
1608 if(error){ | |
1609 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
1610 _("Error saving image: %s"), error->message); | |
1611 g_error_free(error); | |
1612 } | |
1613 | |
1614 g_free(type); | |
1615 } | |
1616 | |
1617 static void gaim_im_image_save(GtkWidget *w, gaim_im_image *image) | |
1618 { | |
1619 GtkWidget *sel = gtk_file_selection_new(_("Gaim - Save Image")); | |
1620 | |
1621 gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename); | |
1622 g_object_set_data(G_OBJECT(sel), "gaim_im_image", image); | |
1623 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
1624 G_CALLBACK(write_img_to_file), sel); | |
1625 | |
1626 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
1627 G_CALLBACK(gtk_widget_destroy), sel); | |
1628 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked", | |
1629 G_CALLBACK(gtk_widget_destroy), sel); | |
1630 | |
1631 gtk_widget_show(sel); | |
1632 } | |
1633 | |
1634 static gboolean gaim_im_image_clicked(GtkWidget *w, GdkEvent *event, gaim_im_image *image) | |
1635 { | |
1636 GdkEventButton *event_button = (GdkEventButton *) event; | |
1637 | |
1638 if (event->type == GDK_BUTTON_RELEASE) { | |
1639 if(event_button->button == 3) { | |
1640 GtkWidget *img, *item, *menu; | |
1641 gchar *text = g_strdup_printf(_("_Save Image...")); | |
1642 menu = gtk_menu_new(); | |
1643 | |
1644 /* buttons and such */ | |
1645 img = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU); | |
1646 item = gtk_image_menu_item_new_with_mnemonic(text); | |
1647 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); | |
1648 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gaim_im_image_save), image); | |
1649 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
1650 | |
1651 gtk_widget_show_all(menu); | |
1652 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
1653 event_button->button, event_button->time); | |
1654 | |
1655 g_free(text); | |
1656 return TRUE; | |
1657 } | |
1658 } | |
1659 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) | |
1660 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
1661 be caught by the regular GtkTextView menu */ | |
1662 else | |
1663 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1664 | |
1665 } |