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