Mercurial > pidgin
annotate src/gtkimhtml.c @ 8681:e2e56231023c
[gaim-migrate @ 9434]
I think this just about perfects rich-text copy/paste. Tim's done a real good
job with gtkimhtml.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 17 Apr 2004 17:39:15 +0000 |
parents | cc2ce209cc46 |
children | 140b0d020c43 |
rev | line source |
---|---|
1428 | 1 /* |
2 * GtkIMHtml | |
3 * | |
8046 | 4 * Gaim is the legal property of its developers, whose names are too numerous |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
1428 | 7 * |
8 * This program is free software; you can redistribute it and/or modify | |
9 * under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
24 #ifdef HAVE_CONFIG_H |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
25 #include <config.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
26 #endif |
8526 | 27 #include "debug.h" |
8091 | 28 #include "util.h" |
1428 | 29 #include "gtkimhtml.h" |
7358 | 30 #include "gtksourceiter.h" |
1428 | 31 #include <gtk/gtk.h> |
4895 | 32 #include <glib/gerror.h> |
4046 | 33 #include <gdk/gdkkeysyms.h> |
1428 | 34 #include <string.h> |
35 #include <ctype.h> | |
36 #include <stdio.h> | |
4629 | 37 #include <stdlib.h> |
1428 | 38 #include <math.h> |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
39 #ifdef HAVE_LANGINFO_CODESET |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
40 #include <langinfo.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
41 #include <locale.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
42 #endif |
1428 | 43 |
4417 | 44 #ifdef ENABLE_NLS |
45 # include <libintl.h> | |
46 # define _(x) gettext(x) | |
47 # ifdef gettext_noop | |
48 # define N_(String) gettext_noop (String) | |
49 # else | |
50 # define N_(String) (String) | |
51 # endif | |
52 #else | |
53 # define N_(String) (String) | |
54 # define _(x) (x) | |
55 #endif | |
56 | |
4735 | 57 #include <pango/pango-font.h> |
58 | |
5105 | 59 /* GTK+ < 2.2.2 hack, see ui.h for details. */ |
60 #ifndef GTK_WRAP_WORD_CHAR | |
61 #define GTK_WRAP_WORD_CHAR GTK_WRAP_WORD | |
62 #endif | |
63 | |
4735 | 64 #define TOOLTIP_TIMEOUT 500 |
65 | |
8677 | 66 static void preinsert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml); |
8061 | 67 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml); |
8505 | 68 static gboolean gtk_imhtml_is_amp_escape (const gchar *string, gchar **replace, gint *length); |
8061 | 69 void gtk_imhtml_close_tags(GtkIMHtml *imhtml); |
8091 | 70 static void gtk_imhtml_link_drag_rcv_cb(GtkWidget *widget, GdkDragContext *dc, guint x, guint y, GtkSelectionData *sd, guint info, guint t, GtkIMHtml *imhtml); |
8677 | 71 static void mark_set_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, GtkIMHtml *imhtml); |
8061 | 72 |
3922 | 73 /* POINT_SIZE converts from AIM font sizes to point sizes. It probably should be redone in such a |
74 * way that it base the sizes off the default font size rather than using arbitrary font sizes. */ | |
75 #define MAX_FONT_SIZE 7 | |
5367 | 76 #define POINT_SIZE(x) (options & GTK_IMHTML_USE_POINTSIZE ? x : _point_sizes [MIN ((x), MAX_FONT_SIZE) - 1]) |
8380 | 77 static gdouble _point_sizes [] = { .69444444, .8333333, 1, 1.2, 1.44, 1.728, 2.0736}; |
2349
60c716c32c40
[gaim-migrate @ 2362]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2348
diff
changeset
|
78 |
8498 | 79 enum { |
8677 | 80 TARGET_HTML, |
8061 | 81 TARGET_UTF8_STRING, |
82 TARGET_COMPOUND_TEXT, | |
83 TARGET_STRING, | |
84 TARGET_TEXT | |
85 }; | |
86 | |
8091 | 87 enum { |
88 DRAG_URL | |
89 }; | |
90 | |
8420 | 91 enum { |
92 URL_CLICKED, | |
93 BUTTONS_UPDATE, | |
94 TOGGLE_FORMAT, | |
8427 | 95 CLEAR_FORMAT, |
8506 | 96 UPDATE_FORMAT, |
8420 | 97 LAST_SIGNAL |
98 }; | |
99 static guint signals [LAST_SIGNAL] = { 0 }; | |
100 | |
8061 | 101 GtkTargetEntry selection_targets[] = { |
8566 | 102 { "text/html", 0, TARGET_HTML }, |
8061 | 103 { "UTF8_STRING", 0, TARGET_UTF8_STRING }, |
104 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT }, | |
105 { "STRING", 0, TARGET_STRING }, | |
106 { "TEXT", 0, TARGET_TEXT}}; | |
107 | |
8091 | 108 GtkTargetEntry link_drag_drop_targets[] = { |
109 {"x-url/ftp", 0, DRAG_URL}, | |
110 {"x-url/http", 0, DRAG_URL}, | |
111 {"text/uri-list", 0, DRAG_URL}, | |
112 {"_NETSCAPE_URL", 0, DRAG_URL}}; | |
113 | |
4032 | 114 static GtkSmileyTree* |
115 gtk_smiley_tree_new () | |
116 { | |
117 return g_new0 (GtkSmileyTree, 1); | |
118 } | |
119 | |
120 static void | |
121 gtk_smiley_tree_insert (GtkSmileyTree *tree, | |
4263 | 122 GtkIMHtmlSmiley *smiley) |
4032 | 123 { |
124 GtkSmileyTree *t = tree; | |
4263 | 125 const gchar *x = smiley->smile; |
4032 | 126 |
127 if (!strlen (x)) | |
128 return; | |
129 | |
130 while (*x) { | |
131 gchar *pos; | |
132 gint index; | |
133 | |
134 if (!t->values) | |
135 t->values = g_string_new (""); | |
136 | |
137 pos = strchr (t->values->str, *x); | |
138 if (!pos) { | |
139 t->values = g_string_append_c (t->values, *x); | |
140 index = t->values->len - 1; | |
141 t->children = g_realloc (t->children, t->values->len * sizeof (GtkSmileyTree *)); | |
142 t->children [index] = g_new0 (GtkSmileyTree, 1); | |
143 } else | |
7386 | 144 index = GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str); |
8061 | 145 |
4032 | 146 t = t->children [index]; |
8061 | 147 |
4032 | 148 x++; |
149 } | |
8061 | 150 |
4263 | 151 t->image = smiley; |
4032 | 152 } |
4041 | 153 |
4263 | 154 |
4264 | 155 void gtk_smiley_tree_destroy (GtkSmileyTree *tree) |
4032 | 156 { |
157 GSList *list = g_slist_append (NULL, tree); | |
158 | |
159 while (list) { | |
160 GtkSmileyTree *t = list->data; | |
161 gint i; | |
162 list = g_slist_remove(list, t); | |
7384 | 163 if (t && t->values) { |
4032 | 164 for (i = 0; i < t->values->len; i++) |
165 list = g_slist_append (list, t->children [i]); | |
166 g_string_free (t->values, TRUE); | |
167 g_free (t->children); | |
168 } | |
169 g_free (t); | |
170 } | |
171 } | |
172 | |
5967 | 173 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data) |
174 { | |
175 GdkRectangle rect; | |
176 | |
177 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &rect); | |
178 if(widget->old_rect.width != rect.width || widget->old_rect.height != rect.height){ | |
179 GList *iter = GTK_IMHTML(widget)->scalables; | |
180 | |
181 while(iter){ | |
182 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(iter->data); | |
183 scale->scale(scale, rect.width, rect.height); | |
184 | |
185 iter = iter->next; | |
186 } | |
187 } | |
188 | |
189 widget->old_rect = rect; | |
190 return FALSE; | |
191 } | |
192 | |
193 static gint | |
194 gtk_imhtml_tip_paint (GtkIMHtml *imhtml) | |
195 { | |
196 PangoLayout *layout; | |
197 | |
198 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
199 | |
200 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
201 | |
8061 | 202 gtk_paint_flat_box (imhtml->tip_window->style, imhtml->tip_window->window, |
5967 | 203 GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, imhtml->tip_window, |
204 "tooltip", 0, 0, -1, -1); | |
205 | |
206 gtk_paint_layout (imhtml->tip_window->style, imhtml->tip_window->window, GTK_STATE_NORMAL, | |
207 FALSE, NULL, imhtml->tip_window, NULL, 4, 4, layout); | |
208 | |
209 g_object_unref(layout); | |
210 return FALSE; | |
211 } | |
212 | |
213 static gint | |
214 gtk_imhtml_tip (gpointer data) | |
215 { | |
216 GtkIMHtml *imhtml = data; | |
8526 | 217 PangoFontMetrics *font_metrics; |
5967 | 218 PangoLayout *layout; |
8526 | 219 PangoFont *font; |
5967 | 220 |
221 gint gap, x, y, h, w, scr_w, baseline_skip; | |
222 | |
223 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
224 | |
225 if (!imhtml->tip || !GTK_WIDGET_DRAWABLE (GTK_WIDGET(imhtml))) { | |
226 imhtml->tip_timer = 0; | |
227 return FALSE; | |
228 } | |
8061 | 229 |
5967 | 230 if (imhtml->tip_window){ |
231 gtk_widget_destroy (imhtml->tip_window); | |
232 imhtml->tip_window = NULL; | |
233 } | |
234 | |
235 imhtml->tip_timer = 0; | |
236 imhtml->tip_window = gtk_window_new (GTK_WINDOW_POPUP); | |
237 gtk_widget_set_app_paintable (imhtml->tip_window, TRUE); | |
238 gtk_window_set_resizable (GTK_WINDOW (imhtml->tip_window), FALSE); | |
239 gtk_widget_set_name (imhtml->tip_window, "gtk-tooltips"); | |
240 g_signal_connect_swapped (G_OBJECT (imhtml->tip_window), "expose_event", | |
241 G_CALLBACK (gtk_imhtml_tip_paint), imhtml); | |
242 | |
243 gtk_widget_ensure_style (imhtml->tip_window); | |
244 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
8526 | 245 font = pango_context_load_font(pango_layout_get_context(layout), |
246 imhtml->tip_window->style->font_desc); | |
247 | |
248 if (font == NULL) { | |
249 char *tmp = pango_font_description_to_string( | |
250 imhtml->tip_window->style->font_desc); | |
251 | |
252 gaim_debug(GAIM_DEBUG_ERROR, "gtk_imhtml_tip", | |
253 "pango_context_load_font() couldn't load font: '%s'\n", | |
254 tmp); | |
255 g_free(tmp); | |
256 | |
257 return FALSE; | |
258 | |
259 } | |
260 | |
261 font_metrics = pango_font_get_metrics(font, NULL); | |
8309 | 262 |
5967 | 263 |
264 pango_layout_get_pixel_size(layout, &scr_w, NULL); | |
8526 | 265 gap = PANGO_PIXELS((pango_font_metrics_get_ascent(font_metrics) + |
266 pango_font_metrics_get_descent(font_metrics))/ 4); | |
5967 | 267 |
268 if (gap < 2) | |
269 gap = 2; | |
8526 | 270 baseline_skip = PANGO_PIXELS(pango_font_metrics_get_ascent(font_metrics) + |
271 pango_font_metrics_get_descent(font_metrics)); | |
5967 | 272 w = 8 + scr_w; |
273 h = 8 + baseline_skip; | |
274 | |
275 gdk_window_get_pointer (NULL, &x, &y, NULL); | |
276 if (GTK_WIDGET_NO_WINDOW (GTK_WIDGET(imhtml))) | |
277 y += GTK_WIDGET(imhtml)->allocation.y; | |
278 | |
279 scr_w = gdk_screen_width(); | |
280 | |
281 x -= ((w >> 1) + 4); | |
282 | |
283 if ((x + w) > scr_w) | |
284 x -= (x + w) - scr_w; | |
285 else if (x < 0) | |
286 x = 0; | |
287 | |
8526 | 288 y = y + PANGO_PIXELS(pango_font_metrics_get_ascent(font_metrics) + |
289 pango_font_metrics_get_descent(font_metrics)); | |
5967 | 290 |
291 gtk_widget_set_size_request (imhtml->tip_window, w, h); | |
292 gtk_widget_show (imhtml->tip_window); | |
293 gtk_window_move (GTK_WINDOW(imhtml->tip_window), x, y); | |
294 | |
8526 | 295 pango_font_metrics_unref(font_metrics); |
5967 | 296 g_object_unref(layout); |
297 | |
298 return FALSE; | |
299 } | |
300 | |
301 gboolean gtk_motion_event_notify(GtkWidget *imhtml, GdkEventMotion *event, gpointer data) | |
8061 | 302 { |
5967 | 303 GtkTextIter iter; |
304 GdkWindow *win = event->window; | |
305 int x, y; | |
306 char *tip = NULL; | |
307 GSList *tags = NULL, *templist = NULL; | |
308 gdk_window_get_pointer(GTK_WIDGET(imhtml)->window, NULL, NULL, NULL); | |
309 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), GTK_TEXT_WINDOW_WIDGET, | |
310 event->x, event->y, &x, &y); | |
311 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); | |
312 tags = gtk_text_iter_get_tags(&iter); | |
313 | |
314 templist = tags; | |
315 while (templist) { | |
316 GtkTextTag *tag = templist->data; | |
317 tip = g_object_get_data(G_OBJECT(tag), "link_url"); | |
318 if (tip) | |
319 break; | |
320 templist = templist->next; | |
321 } | |
8061 | 322 |
5967 | 323 if (GTK_IMHTML(imhtml)->tip) { |
324 if ((tip == GTK_IMHTML(imhtml)->tip)) { | |
325 return FALSE; | |
326 } | |
327 /* We've left the cell. Remove the timeout and create a new one below */ | |
328 if (GTK_IMHTML(imhtml)->tip_window) { | |
329 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
330 GTK_IMHTML(imhtml)->tip_window = NULL; | |
331 } | |
8061 | 332 if (GTK_IMHTML(imhtml)->editable) |
333 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->text_cursor); | |
334 else | |
335 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 336 if (GTK_IMHTML(imhtml)->tip_timer) |
337 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
338 GTK_IMHTML(imhtml)->tip_timer = 0; | |
339 } | |
8061 | 340 |
5967 | 341 if(tip){ |
8061 | 342 if (!GTK_IMHTML(imhtml)->editable) |
343 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->hand_cursor); | |
344 GTK_IMHTML(imhtml)->tip_timer = g_timeout_add (TOOLTIP_TIMEOUT, | |
5967 | 345 gtk_imhtml_tip, imhtml); |
346 } | |
8061 | 347 |
5967 | 348 GTK_IMHTML(imhtml)->tip = tip; |
349 g_slist_free(tags); | |
350 return FALSE; | |
351 } | |
352 | |
353 gboolean gtk_leave_event_notify(GtkWidget *imhtml, GdkEventCrossing *event, gpointer data) | |
354 { | |
355 /* when leaving the widget, clear any current & pending tooltips and restore the cursor */ | |
356 if (GTK_IMHTML(imhtml)->tip_window) { | |
357 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
358 GTK_IMHTML(imhtml)->tip_window = NULL; | |
359 } | |
360 if (GTK_IMHTML(imhtml)->tip_timer) { | |
361 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
362 GTK_IMHTML(imhtml)->tip_timer = 0; | |
363 } | |
8061 | 364 if (GTK_IMHTML(imhtml)->editable) |
365 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->text_cursor); | |
366 else | |
367 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 368 |
8568 | 369 /* propagate the event normally */ |
5967 | 370 return FALSE; |
371 } | |
372 | |
6066 | 373 /* |
374 * XXX - This should be removed eventually. | |
375 * | |
8061 | 376 * This function exists to work around a gross bug in GtkTextView. |
377 * Basically, we short circuit ctrl+a and ctrl+end because they make | |
6066 | 378 * el program go boom. |
379 * | |
8061 | 380 * It's supposed to be fixed in gtk2.2. You can view the bug report at |
6066 | 381 * http://bugzilla.gnome.org/show_bug.cgi?id=107939 |
382 */ | |
8317 | 383 |
384 /* | |
385 * I'm adding some keyboard shortcuts too. | |
386 */ | |
387 | |
388 gboolean gtk_key_pressed_cb(GtkIMHtml *imhtml, GdkEventKey *event, gpointer data) | |
8677 | 389 { |
8317 | 390 char buf[7]; |
391 buf[0] = '\0'; | |
392 | |
6066 | 393 if (event->state & GDK_CONTROL_MASK) |
394 switch (event->keyval) { | |
8539 | 395 #if (!GTK_CHECK_VERSION(2,2,0)) |
8317 | 396 case 'a': |
397 return TRUE; | |
398 break; | |
8677 | 399 |
8317 | 400 case GDK_Home: |
401 return TRUE; | |
402 break; | |
403 | |
404 case GDK_End: | |
405 return TRUE; | |
406 break; | |
8539 | 407 #endif /* Gtk+ >= 2.2.0 */ |
8317 | 408 |
409 case 'b': /* ctrl-b is GDK_Left, which moves backwards. */ | |
410 case 'B': | |
8420 | 411 if (imhtml->format_functions & GTK_IMHTML_BOLD) { |
8456 | 412 if(imhtml->html_shortcuts) { |
413 gtk_imhtml_toggle_bold(imhtml); | |
8466 | 414 return TRUE; |
8456 | 415 } |
8420 | 416 } |
8466 | 417 return FALSE; |
8317 | 418 break; |
8654 | 419 |
420 #ifdef LAY_OFF_MY_FRIGGIN_KEYBINDINGS | |
8317 | 421 case 'f': |
422 case 'F': | |
423 /*set_toggle(gtkconv->toolbar.font, | |
424 !gtk_toggle_button_get_active( | |
425 GTK_TOGGLE_BUTTON(gtkconv->toolbar.font)));*/ | |
426 | |
427 return TRUE; | |
428 break; | |
8654 | 429 #endif /* this doesn't even DO anything */ |
8317 | 430 |
431 case 'i': | |
432 case 'I': | |
8481 | 433 if (imhtml->format_functions & GTK_IMHTML_ITALIC) { |
8466 | 434 if(imhtml->html_shortcuts) { |
8456 | 435 gtk_imhtml_toggle_italic(imhtml); |
8466 | 436 return TRUE; |
437 } | |
8481 | 438 } |
8466 | 439 return FALSE; |
8317 | 440 break; |
441 | |
442 case 'u': /* ctrl-u is GDK_Clear, which clears the line. */ | |
443 case 'U': | |
8481 | 444 if (imhtml->format_functions & GTK_IMHTML_UNDERLINE) { |
8466 | 445 if(imhtml->html_shortcuts) { |
8456 | 446 gtk_imhtml_toggle_underline(imhtml); |
8466 | 447 return TRUE; |
448 } | |
8481 | 449 } |
8466 | 450 return FALSE; |
8317 | 451 break; |
452 | |
453 case '-': | |
8420 | 454 if (imhtml->format_functions & GTK_IMHTML_SHRINK) |
455 gtk_imhtml_font_shrink(imhtml); | |
8317 | 456 return TRUE; |
457 break; | |
458 | |
459 case '=': | |
460 case '+': | |
8420 | 461 if (imhtml->format_functions & GTK_IMHTML_GROW) |
462 gtk_imhtml_font_grow(imhtml); | |
8317 | 463 return TRUE; |
464 break; | |
8677 | 465 |
8317 | 466 case '1': strcpy(buf, ":-)"); break; |
467 case '2': strcpy(buf, ":-("); break; | |
468 case '3': strcpy(buf, ";-)"); break; | |
469 case '4': strcpy(buf, ":-P"); break; | |
470 case '5': strcpy(buf, "=-O"); break; | |
471 case '6': strcpy(buf, ":-*"); break; | |
472 case '7': strcpy(buf, ">:o"); break; | |
473 case '8': strcpy(buf, "8-)"); break; | |
474 case '!': strcpy(buf, ":-$"); break; | |
475 case '@': strcpy(buf, ":-!"); break; | |
476 case '#': strcpy(buf, ":-["); break; | |
477 case '$': strcpy(buf, "O:-)"); break; | |
478 case '%': strcpy(buf, ":-/"); break; | |
479 case '^': strcpy(buf, ":'("); break; | |
480 case '&': strcpy(buf, ":-X"); break; | |
481 case '*': strcpy(buf, ":-D"); break; | |
6066 | 482 } |
8456 | 483 if (*buf && imhtml->smiley_shortcuts) { |
484 gtk_imhtml_insert_smiley(imhtml, imhtml->protocol_name, buf); | |
8317 | 485 return TRUE; |
8677 | 486 } |
6066 | 487 return FALSE; |
488 } | |
489 | |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
490 #if GTK_CHECK_VERSION(2,2,0) |
8061 | 491 static void gtk_imhtml_clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, GtkIMHtml *imhtml) { |
8681 | 492 char *text; |
8061 | 493 GtkTextIter start, end; |
494 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
495 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
8681 | 496 |
8061 | 497 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); |
498 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
8681 | 499 gboolean primary = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY) == clipboard; |
8061 | 500 |
501 if (info == TARGET_HTML) { | |
8149 | 502 int len; |
8148 | 503 char *selection; |
8061 | 504 GString *str = g_string_new(NULL); |
8681 | 505 if (primary) { |
506 text = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
507 } else | |
508 text = imhtml->clipboard_html_string; | |
8061 | 509 |
510 /* Mozilla asks that we start our text/html with the Unicode byte order mark */ | |
511 str = g_string_append_unichar(str, 0xfeff); | |
512 str = g_string_append(str, text); | |
513 str = g_string_append_unichar(str, 0x0000); | |
8148 | 514 selection = g_convert(str->str, str->len, "UCS-2", "UTF-8", NULL, &len, NULL); |
8061 | 515 gtk_selection_data_set (selection_data, gdk_atom_intern("text/html", FALSE), 16, selection, len); |
516 g_string_free(str, TRUE); | |
517 g_free(selection); | |
518 } else { | |
8681 | 519 if (primary) { |
520 text = gtk_imhtml_get_text(imhtml, &start, &end); | |
521 } else | |
522 text = imhtml->clipboard_text_string; | |
8061 | 523 gtk_selection_data_set_text(selection_data, text, strlen(text)); |
524 } | |
8681 | 525 if (primary) /* This was allocated here */ |
526 g_free(text); | |
527 } | |
8061 | 528 |
529 static void gtk_imhtml_primary_clipboard_clear(GtkClipboard *clipboard, GtkIMHtml *imhtml) | |
7749 | 530 { |
8061 | 531 GtkTextIter insert; |
532 GtkTextIter selection_bound; | |
533 | |
534 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &insert, | |
535 gtk_text_buffer_get_mark (imhtml->text_buffer, "insert")); | |
536 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &selection_bound, | |
537 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound")); | |
538 | |
539 if (!gtk_text_iter_equal (&insert, &selection_bound)) | |
540 gtk_text_buffer_move_mark (imhtml->text_buffer, | |
541 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound"), | |
542 &insert); | |
7749 | 543 } |
7742 | 544 |
8677 | 545 static void copy_clipboard_cb(GtkIMHtml *imhtml, gpointer unused) |
7749 | 546 { |
8681 | 547 GtkTextIter start, end; |
548 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
549 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
550 | |
551 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | |
552 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
553 | |
8061 | 554 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), |
555 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
556 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
557 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); | |
7346 | 558 |
8681 | 559 if (imhtml->clipboard_html_string) { |
560 g_free(imhtml->clipboard_html_string); | |
561 g_free(imhtml->clipboard_text_string); | |
562 } | |
563 | |
564 imhtml->clipboard_html_string = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
565 imhtml->clipboard_text_string = gtk_imhtml_get_text(imhtml, &start, &end); | |
566 | |
8061 | 567 g_signal_stop_emission_by_name(imhtml, "copy-clipboard"); |
568 } | |
569 | |
570 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data) | |
571 { | |
572 char *text; | |
573 guint16 c; | |
574 GtkIMHtml *imhtml = data; | |
8677 | 575 GtkTextIter iter; |
7809 | 576 |
8123 | 577 if (!gtk_text_view_get_editable(GTK_TEXT_VIEW(imhtml))) |
8105 | 578 return; |
579 | |
8061 | 580 if (selection_data->length < 0) { |
581 text = gtk_clipboard_wait_for_text(clipboard); | |
8128
9aafd344230d
[gaim-migrate @ 8833]
Christian Hammond <chipx86@chipx86.com>
parents:
8123
diff
changeset
|
582 |
9aafd344230d
[gaim-migrate @ 8833]
Christian Hammond <chipx86@chipx86.com>
parents:
8123
diff
changeset
|
583 if (text == NULL) |
9aafd344230d
[gaim-migrate @ 8833]
Christian Hammond <chipx86@chipx86.com>
parents:
8123
diff
changeset
|
584 return; |
9aafd344230d
[gaim-migrate @ 8833]
Christian Hammond <chipx86@chipx86.com>
parents:
8123
diff
changeset
|
585 |
8061 | 586 } else { |
587 text = g_malloc((selection_data->format / 8) * selection_data->length); | |
588 memcpy(text, selection_data->data, selection_data->length * (selection_data->format / 8)); | |
7766 | 589 } |
8061 | 590 |
591 memcpy (&c, text, 2); | |
592 if (c == 0xfeff) { | |
593 /* This is UCS2 */ | |
594 char *utf8 = g_convert(text+2, (selection_data->length * (selection_data->format / 8)) - 2, "UTF-8", "UCS-2", NULL, NULL, NULL); | |
595 g_free(text); | |
596 text = utf8; | |
597 } | |
8677 | 598 if (!imhtml->wbfo) |
599 gtk_imhtml_close_tags(imhtml); | |
600 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, gtk_text_buffer_get_insert(imhtml->text_buffer)); | |
601 gtk_imhtml_insert_html_at_iter(imhtml, text, GTK_IMHTML_NO_NEWLINE, &iter); | |
602 gtk_text_buffer_move_mark_by_name(imhtml->text_buffer, "insert", &iter); | |
8681 | 603 |
604 g_free(text); | |
605 | |
8061 | 606 } |
607 | |
608 | |
609 static void paste_clipboard_cb(GtkIMHtml *imhtml, gpointer blah) | |
610 { | |
611 | |
612 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD); | |
613 gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE), | |
614 paste_received_cb, imhtml); | |
615 g_signal_stop_emission_by_name(imhtml, "paste-clipboard"); | |
7766 | 616 } |
617 | |
8677 | 618 static void imhtml_realized_remove_primary(GtkIMHtml *imhtml, gpointer unused) |
619 { | |
620 gtk_text_buffer_remove_selection_clipboard(GTK_IMHTML(imhtml)->text_buffer, | |
621 gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY)); | |
622 | |
623 } | |
624 | |
625 #endif | |
626 | |
627 static void mark_set_so_update_selection_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, GtkIMHtml *imhtml) | |
628 { | |
629 if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL)) { | |
630 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY), | |
631 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
632 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
633 (GtkClipboardClearFunc)gtk_imhtml_primary_clipboard_clear, G_OBJECT(imhtml)); | |
634 } | |
635 } | |
636 | |
637 /* does this go in the #ifdef too? I need to keep track of which functions are 2.2 only */ | |
638 /* adapted from gtktextview.c */ | |
639 static gboolean gtk_imhtml_button_press_event(GtkIMHtml *imhtml, GdkEventButton *event, gpointer unused) | |
7346 | 640 { |
8677 | 641 if (event->button == 2) { |
642 int x, y; | |
643 GtkTextIter iter; | |
644 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY); | |
645 | |
646 if (!imhtml->editable) | |
647 return FALSE; | |
648 | |
649 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), | |
650 GTK_TEXT_WINDOW_TEXT, | |
651 event->x, | |
652 event->y, | |
653 &x, | |
654 &y); | |
655 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); | |
656 gtk_text_buffer_place_cursor(imhtml->text_buffer, &iter); | |
657 | |
658 gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE), | |
659 paste_received_cb, imhtml); | |
660 | |
661 return TRUE; | |
662 } | |
663 | |
7346 | 664 return FALSE; |
665 } | |
4263 | 666 |
4032 | 667 static GtkTextViewClass *parent_class = NULL; |
668 | |
669 static void | |
670 gtk_imhtml_finalize (GObject *object) | |
671 { | |
672 GtkIMHtml *imhtml = GTK_IMHTML(object); | |
4895 | 673 GList *scalables; |
8061 | 674 |
4138 | 675 g_hash_table_destroy(imhtml->smiley_data); |
4032 | 676 gtk_smiley_tree_destroy(imhtml->default_smilies); |
4138 | 677 gdk_cursor_unref(imhtml->hand_cursor); |
678 gdk_cursor_unref(imhtml->arrow_cursor); | |
8061 | 679 gdk_cursor_unref(imhtml->text_cursor); |
8677 | 680 |
4735 | 681 if(imhtml->tip_window){ |
682 gtk_widget_destroy(imhtml->tip_window); | |
683 } | |
684 if(imhtml->tip_timer) | |
685 gtk_timeout_remove(imhtml->tip_timer); | |
686 | |
4895 | 687 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { |
688 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); | |
689 scale->free(scale); | |
690 } | |
7991 | 691 |
8681 | 692 if (imhtml->clipboard_text_string) { |
693 g_free(imhtml->clipboard_text_string); | |
694 g_free(imhtml->clipboard_html_string); | |
695 } | |
696 | |
697 | |
4895 | 698 g_list_free(imhtml->scalables); |
4032 | 699 G_OBJECT_CLASS(parent_class)->finalize (object); |
700 } | |
1428 | 701 |
3922 | 702 /* Boring GTK stuff */ |
8519 | 703 static void gtk_imhtml_class_init (GtkIMHtmlClass *klass) |
1428 | 704 { |
3922 | 705 GtkObjectClass *object_class; |
4032 | 706 GObjectClass *gobject_class; |
8519 | 707 object_class = (GtkObjectClass*) klass; |
708 gobject_class = (GObjectClass*) klass; | |
4032 | 709 parent_class = gtk_type_class(GTK_TYPE_TEXT_VIEW); |
4417 | 710 signals[URL_CLICKED] = g_signal_new("url_clicked", |
711 G_TYPE_FROM_CLASS(gobject_class), | |
712 G_SIGNAL_RUN_FIRST, | |
713 G_STRUCT_OFFSET(GtkIMHtmlClass, url_clicked), | |
714 NULL, | |
715 0, | |
716 g_cclosure_marshal_VOID__POINTER, | |
717 G_TYPE_NONE, 1, | |
718 G_TYPE_POINTER); | |
8506 | 719 signals[BUTTONS_UPDATE] = g_signal_new("format_buttons_update", |
8420 | 720 G_TYPE_FROM_CLASS(gobject_class), |
721 G_SIGNAL_RUN_FIRST, | |
722 G_STRUCT_OFFSET(GtkIMHtmlClass, buttons_update), | |
723 NULL, | |
724 0, | |
725 g_cclosure_marshal_VOID__POINTER, | |
726 G_TYPE_NONE, 1, | |
727 G_TYPE_INT); | |
728 signals[TOGGLE_FORMAT] = g_signal_new("format_function_toggle", | |
729 G_TYPE_FROM_CLASS(gobject_class), | |
730 G_SIGNAL_RUN_FIRST, | |
731 G_STRUCT_OFFSET(GtkIMHtmlClass, toggle_format), | |
732 NULL, | |
733 0, | |
734 g_cclosure_marshal_VOID__POINTER, | |
8427 | 735 G_TYPE_NONE, 1, |
8420 | 736 G_TYPE_INT); |
8427 | 737 signals[CLEAR_FORMAT] = g_signal_new("format_function_clear", |
738 G_TYPE_FROM_CLASS(gobject_class), | |
739 G_SIGNAL_RUN_FIRST, | |
740 G_STRUCT_OFFSET(GtkIMHtmlClass, clear_format), | |
741 NULL, | |
742 0, | |
8481 | 743 g_cclosure_marshal_VOID__VOID, |
744 G_TYPE_NONE, 0); | |
8506 | 745 signals[UPDATE_FORMAT] = g_signal_new("format_function_update", |
746 G_TYPE_FROM_CLASS(gobject_class), | |
747 G_SIGNAL_RUN_FIRST, | |
748 G_STRUCT_OFFSET(GtkIMHtmlClass, update_format), | |
749 NULL, | |
750 0, | |
751 g_cclosure_marshal_VOID__VOID, | |
752 G_TYPE_NONE, 0); | |
4032 | 753 gobject_class->finalize = gtk_imhtml_finalize; |
1428 | 754 } |
755 | |
3922 | 756 static void gtk_imhtml_init (GtkIMHtml *imhtml) |
1428 | 757 { |
3922 | 758 GtkTextIter iter; |
759 imhtml->text_buffer = gtk_text_buffer_new(NULL); | |
760 gtk_text_buffer_get_end_iter (imhtml->text_buffer, &iter); | |
8677 | 761 imhtml->scrollpoint = gtk_text_buffer_create_mark(imhtml->text_buffer, NULL, &iter, FALSE); |
3922 | 762 gtk_text_view_set_buffer(GTK_TEXT_VIEW(imhtml), imhtml->text_buffer); |
5105 | 763 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(imhtml), GTK_WRAP_WORD_CHAR); |
3922 | 764 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(imhtml), 5); |
8677 | 765 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(imhtml), 2); |
766 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(imhtml), 2); | |
8061 | 767 /*gtk_text_view_set_indent(GTK_TEXT_VIEW(imhtml), -15);*/ |
3922 | 768 /*gtk_text_view_set_justification(GTK_TEXT_VIEW(imhtml), GTK_JUSTIFY_FILL);*/ |
8061 | 769 |
3922 | 770 /* These tags will be used often and can be reused--we create them on init and then apply them by name |
8061 | 771 * other tags (color, size, face, etc.) will have to be created and applied dynamically */ |
3922 | 772 gtk_text_buffer_create_tag(imhtml->text_buffer, "BOLD", "weight", PANGO_WEIGHT_BOLD, NULL); |
773 gtk_text_buffer_create_tag(imhtml->text_buffer, "ITALICS", "style", PANGO_STYLE_ITALIC, NULL); | |
774 gtk_text_buffer_create_tag(imhtml->text_buffer, "UNDERLINE", "underline", PANGO_UNDERLINE_SINGLE, NULL); | |
775 gtk_text_buffer_create_tag(imhtml->text_buffer, "STRIKE", "strikethrough", TRUE, NULL); | |
776 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUB", "rise", -5000, NULL); | |
777 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUP", "rise", 5000, NULL); | |
778 gtk_text_buffer_create_tag(imhtml->text_buffer, "PRE", "family", "Monospace", NULL); | |
7295 | 779 gtk_text_buffer_create_tag(imhtml->text_buffer, "search", "background", "#22ff00", "weight", "bold", NULL); |
8677 | 780 |
3922 | 781 /* When hovering over a link, we show the hand cursor--elsewhere we show the plain ol' pointer cursor */ |
782 imhtml->hand_cursor = gdk_cursor_new (GDK_HAND2); | |
783 imhtml->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); | |
8061 | 784 imhtml->text_cursor = gdk_cursor_new (GDK_XTERM); |
2993 | 785 |
4253 | 786 imhtml->show_smileys = TRUE; |
6124 | 787 imhtml->show_comments = TRUE; |
4253 | 788 |
4892 | 789 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 790 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4032 | 791 imhtml->default_smilies = gtk_smiley_tree_new(); |
4735 | 792 |
4944 | 793 g_signal_connect(G_OBJECT(imhtml), "size-allocate", G_CALLBACK(gtk_size_allocate_cb), NULL); |
4735 | 794 g_signal_connect(G_OBJECT(imhtml), "motion-notify-event", G_CALLBACK(gtk_motion_event_notify), NULL); |
4944 | 795 g_signal_connect(G_OBJECT(imhtml), "leave-notify-event", G_CALLBACK(gtk_leave_event_notify), NULL); |
6066 | 796 g_signal_connect(G_OBJECT(imhtml), "key_press_event", G_CALLBACK(gtk_key_pressed_cb), NULL); |
8677 | 797 g_signal_connect(G_OBJECT(imhtml), "button_press_event", G_CALLBACK(gtk_imhtml_button_press_event), NULL); |
798 g_signal_connect(G_OBJECT(imhtml->text_buffer), "insert-text", G_CALLBACK(preinsert_cb), imhtml); | |
8061 | 799 g_signal_connect_after(G_OBJECT(imhtml->text_buffer), "insert-text", G_CALLBACK(insert_cb), imhtml); |
8091 | 800 |
801 gtk_drag_dest_set(GTK_WIDGET(imhtml), 0, | |
802 link_drag_drop_targets, sizeof(link_drag_drop_targets) / sizeof(GtkTargetEntry), | |
803 GDK_ACTION_COPY); | |
804 g_signal_connect(G_OBJECT(imhtml), "drag_data_received", G_CALLBACK(gtk_imhtml_link_drag_rcv_cb), imhtml); | |
805 | |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
806 #if GTK_CHECK_VERSION(2,2,0) |
7353 | 807 g_signal_connect(G_OBJECT(imhtml), "copy-clipboard", G_CALLBACK(copy_clipboard_cb), NULL); |
8061 | 808 g_signal_connect(G_OBJECT(imhtml), "paste-clipboard", G_CALLBACK(paste_clipboard_cb), NULL); |
8677 | 809 //g_signal_connect_after(G_OBJECT(imhtml), "button-release-event", G_CALLBACK(button_release_cb), imhtml); |
810 g_signal_connect_after(G_OBJECT(imhtml), "realize", G_CALLBACK(imhtml_realized_remove_primary), NULL); | |
7404
d889a99e0eb1
[gaim-migrate @ 8000]
Christian Hammond <chipx86@chipx86.com>
parents:
7386
diff
changeset
|
811 #endif |
8677 | 812 |
813 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", | |
814 G_CALLBACK(mark_set_so_update_selection_cb), imhtml); | |
815 | |
4944 | 816 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); |
4735 | 817 |
8681 | 818 imhtml->clipboard_text_string = NULL; |
819 imhtml->clipboard_html_string = NULL; | |
820 | |
4735 | 821 imhtml->tip = NULL; |
822 imhtml->tip_timer = 0; | |
823 imhtml->tip_window = NULL; | |
4895 | 824 |
8677 | 825 imhtml->edit.bold = FALSE; |
826 imhtml->edit.italic = FALSE; | |
827 imhtml->edit.underline = FALSE; | |
8061 | 828 imhtml->edit.forecolor = NULL; |
829 imhtml->edit.backcolor = NULL; | |
830 imhtml->edit.fontface = NULL; | |
8677 | 831 imhtml->edit.fontsize = 0; |
832 imhtml->edit.link = NULL; | |
833 | |
8061 | 834 |
4895 | 835 imhtml->scalables = NULL; |
8061 | 836 |
837 gtk_imhtml_set_editable(imhtml, FALSE); | |
2993 | 838 } |
839 | |
3922 | 840 GtkWidget *gtk_imhtml_new(void *a, void *b) |
1428 | 841 { |
4635 | 842 return GTK_WIDGET(g_object_new(gtk_imhtml_get_type(), NULL)); |
1428 | 843 } |
844 | |
4635 | 845 GType gtk_imhtml_get_type() |
1428 | 846 { |
4635 | 847 static GType imhtml_type = 0; |
1428 | 848 |
849 if (!imhtml_type) { | |
4635 | 850 static const GTypeInfo imhtml_info = { |
851 sizeof(GtkIMHtmlClass), | |
852 NULL, | |
853 NULL, | |
854 (GClassInitFunc) gtk_imhtml_class_init, | |
855 NULL, | |
856 NULL, | |
1428 | 857 sizeof (GtkIMHtml), |
4635 | 858 0, |
859 (GInstanceInitFunc) gtk_imhtml_init | |
1428 | 860 }; |
4635 | 861 |
862 imhtml_type = g_type_register_static(gtk_text_view_get_type(), | |
863 "GtkIMHtml", &imhtml_info, 0); | |
1428 | 864 } |
865 | |
866 return imhtml_type; | |
867 } | |
868 | |
4417 | 869 struct url_data { |
870 GObject *object; | |
871 gchar *url; | |
872 }; | |
873 | |
8677 | 874 static void url_data_destroy(gpointer mydata) |
875 { | |
876 struct url_data *data = mydata; | |
877 g_object_unref(data->object); | |
878 g_free(data->url); | |
879 g_free(data); | |
880 } | |
881 | |
4417 | 882 static void url_open(GtkWidget *w, struct url_data *data) { |
883 if(!data) return; | |
8061 | 884 g_signal_emit(data->object, signals[URL_CLICKED], 0, data->url); |
7988 | 885 |
4417 | 886 } |
5582 | 887 |
4417 | 888 static void url_copy(GtkWidget *w, gchar *url) { |
889 GtkClipboard *clipboard; | |
890 | |
5293
ead927e2543f
[gaim-migrate @ 5665]
Christian Hammond <chipx86@chipx86.com>
parents:
5282
diff
changeset
|
891 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
4417 | 892 gtk_clipboard_set_text(clipboard, url, -1); |
5582 | 893 |
894 clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); | |
895 gtk_clipboard_set_text(clipboard, url, -1); | |
4417 | 896 } |
897 | |
898 /* The callback for an event on a link tag. */ | |
8677 | 899 gboolean tag_event(GtkTextTag *tag, GObject *imhtml, GdkEvent *event, GtkTextIter *arg2, gpointer unused) { |
4417 | 900 GdkEventButton *event_button = (GdkEventButton *) event; |
8061 | 901 if (GTK_IMHTML(imhtml)->editable) |
902 return FALSE; | |
3922 | 903 if (event->type == GDK_BUTTON_RELEASE) { |
8061 | 904 if (event_button->button == 1) { |
4417 | 905 GtkTextIter start, end; |
906 /* we shouldn't open a URL if the user has selected something: */ | |
8677 | 907 if (gtk_text_buffer_get_selection_bounds( |
908 gtk_text_iter_get_buffer(arg2), &start, &end)) | |
4417 | 909 return FALSE; |
910 | |
911 /* A link was clicked--we emit the "url_clicked" signal | |
912 * with the URL as the argument */ | |
8677 | 913 g_object_ref(G_OBJECT(tag)); |
914 g_signal_emit(imhtml, signals[URL_CLICKED], 0, g_object_get_data(G_OBJECT(tag), "link_url")); | |
915 g_object_unref(G_OBJECT(tag)); | |
4417 | 916 return FALSE; |
917 } else if(event_button->button == 3) { | |
4745 | 918 GtkWidget *img, *item, *menu; |
4417 | 919 struct url_data *tempdata = g_new(struct url_data, 1); |
5091 | 920 tempdata->object = g_object_ref(imhtml); |
8677 | 921 tempdata->url = g_strdup(g_object_get_data(G_OBJECT(tag), "link_url")); |
4745 | 922 |
5091 | 923 /* Don't want the tooltip around if user right-clicked on link */ |
924 if (GTK_IMHTML(imhtml)->tip_window) { | |
925 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
926 GTK_IMHTML(imhtml)->tip_window = NULL; | |
927 } | |
928 if (GTK_IMHTML(imhtml)->tip_timer) { | |
929 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
930 GTK_IMHTML(imhtml)->tip_timer = 0; | |
931 } | |
8061 | 932 if (GTK_IMHTML(imhtml)->editable) |
933 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->text_cursor); | |
934 else | |
935 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
4417 | 936 menu = gtk_menu_new(); |
8677 | 937 g_object_set_data_full(G_OBJECT(menu), "x-imhtml-url-data", tempdata, url_data_destroy); |
4745 | 938 |
4417 | 939 /* buttons and such */ |
940 | |
8677 | 941 if (!strncmp(tempdata->url, "mailto:", 7)) |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
942 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
943 /* Copy E-Mail Address */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
944 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
945 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
946 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
947 _("_Copy E-Mail Address")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
948 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
|
949 g_signal_connect(G_OBJECT(item), "activate", |
8677 | 950 G_CALLBACK(url_copy), tempdata->url + 7); |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
951 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
952 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
953 else |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
954 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
955 /* Copy Link Location */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
956 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
957 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
958 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
959 _("_Copy Link Location")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
960 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
|
961 g_signal_connect(G_OBJECT(item), "activate", |
8677 | 962 G_CALLBACK(url_copy), tempdata->url); |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
963 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
964 |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
965 /* Open Link in Browser */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
966 img = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
967 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
968 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
969 _("_Open Link in Browser")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
970 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
|
971 g_signal_connect(G_OBJECT(item), "activate", |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
972 G_CALLBACK(url_open), tempdata); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
973 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
974 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
975 |
4756 | 976 |
4417 | 977 gtk_widget_show_all(menu); |
4756 | 978 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, |
979 event_button->button, event_button->time); | |
4745 | 980 |
4417 | 981 return TRUE; |
982 } | |
1428 | 983 } |
4417 | 984 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) |
985 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
986 be caught by the regular GtkTextView menu */ | |
987 else | |
988 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1428 | 989 } |
990 | |
8091 | 991 static void |
992 gtk_imhtml_link_drag_rcv_cb(GtkWidget *widget, GdkDragContext *dc, guint x, guint y, | |
993 GtkSelectionData *sd, guint info, guint t, GtkIMHtml *imhtml) | |
994 { | |
995 if(gtk_imhtml_get_editable(imhtml) && sd->data){ | |
996 gchar **links; | |
997 gchar *link; | |
998 | |
999 gaim_str_strip_cr(sd->data); | |
1000 | |
1001 links = g_strsplit(sd->data, "\n", 0); | |
1002 while((link = *links++) != NULL){ | |
1003 if(gaim_str_has_prefix(link, "http://") || | |
1004 gaim_str_has_prefix(link, "https://") || | |
1005 gaim_str_has_prefix(link, "ftp://")){ | |
8677 | 1006 gtk_imhtml_insert_link(imhtml, gtk_text_buffer_get_insert(imhtml->text_buffer), link, link); |
8091 | 1007 } else if (link=='\0') { |
8177 | 1008 /* Ignore blank lines */ |
8091 | 1009 } else { |
8177 | 1010 /* Special reasons, aka images being put in via other tag, etc. */ |
8091 | 1011 } |
1012 } | |
1013 | |
1014 gtk_drag_finish(dc, TRUE, (dc->action == GDK_ACTION_MOVE), t); | |
1015 } else { | |
1016 gtk_drag_finish(dc, FALSE, FALSE, t); | |
1017 } | |
1018 } | |
1019 | |
4298 | 1020 /* this isn't used yet |
4032 | 1021 static void |
4263 | 1022 gtk_smiley_tree_remove (GtkSmileyTree *tree, |
1023 GtkIMHtmlSmiley *smiley) | |
4032 | 1024 { |
1025 GtkSmileyTree *t = tree; | |
4263 | 1026 const gchar *x = smiley->smile; |
4032 | 1027 gint len = 0; |
1028 | |
1029 while (*x) { | |
1030 gchar *pos; | |
1031 | |
1032 if (!t->values) | |
1033 return; | |
1034 | |
1035 pos = strchr (t->values->str, *x); | |
1036 if (pos) | |
1037 t = t->children [(int) pos - (int) t->values->str]; | |
1038 else | |
1039 return; | |
1040 | |
1041 x++; len++; | |
1042 } | |
1043 | |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
1044 if (t->image) { |
4032 | 1045 t->image = NULL; |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
1046 } |
4032 | 1047 } |
4298 | 1048 */ |
1049 | |
4032 | 1050 |
1051 static gint | |
1052 gtk_smiley_tree_lookup (GtkSmileyTree *tree, | |
1053 const gchar *text) | |
1054 { | |
1055 GtkSmileyTree *t = tree; | |
1056 const gchar *x = text; | |
1057 gint len = 0; | |
8505 | 1058 gchar *amp; |
1059 gint alen; | |
4032 | 1060 |
1061 while (*x) { | |
1062 gchar *pos; | |
1063 | |
1064 if (!t->values) | |
1065 break; | |
1066 | |
8505 | 1067 if(*x == '&' && gtk_imhtml_is_amp_escape(x, &, &alen)) { |
1068 len += alen - strlen(amp); | |
1069 x += alen - strlen(amp); | |
1070 pos = strchr (t->values->str, *amp); | |
1071 } | |
1072 else | |
1073 pos = strchr (t->values->str, *x); | |
1074 | |
4032 | 1075 if (pos) |
7371 | 1076 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 1077 else |
1078 break; | |
1079 | |
1080 x++; len++; | |
1081 } | |
1082 | |
1083 if (t->image) | |
1084 return len; | |
1085 | |
1086 return 0; | |
1087 } | |
1088 | |
1089 void | |
4263 | 1090 gtk_imhtml_associate_smiley (GtkIMHtml *imhtml, |
1091 gchar *sml, | |
1092 GtkIMHtmlSmiley *smiley) | |
4032 | 1093 { |
1094 GtkSmileyTree *tree; | |
1095 g_return_if_fail (imhtml != NULL); | |
1096 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
7371 | 1097 |
4032 | 1098 if (sml == NULL) |
1099 tree = imhtml->default_smilies; | |
1100 else if ((tree = g_hash_table_lookup(imhtml->smiley_data, sml))) { | |
1101 } else { | |
1102 tree = gtk_smiley_tree_new(); | |
4892 | 1103 g_hash_table_insert(imhtml->smiley_data, g_strdup(sml), tree); |
4032 | 1104 } |
1105 | |
4263 | 1106 gtk_smiley_tree_insert (tree, smiley); |
4032 | 1107 } |
1108 | |
1109 static gboolean | |
1110 gtk_imhtml_is_smiley (GtkIMHtml *imhtml, | |
1111 GSList *fonts, | |
1112 const gchar *text, | |
1113 gint *len) | |
1114 { | |
1115 GtkSmileyTree *tree; | |
5967 | 1116 GtkIMHtmlFontDetail *font; |
4032 | 1117 char *sml = NULL; |
1118 | |
1119 if (fonts) { | |
1120 font = fonts->data; | |
1121 sml = font->sml; | |
1122 } | |
1123 | |
1124 if (sml == NULL) | |
1125 tree = imhtml->default_smilies; | |
1126 else { | |
1127 tree = g_hash_table_lookup(imhtml->smiley_data, sml); | |
1128 } | |
1129 if (tree == NULL) | |
1130 return FALSE; | |
7371 | 1131 |
8505 | 1132 *len = gtk_smiley_tree_lookup (tree, text); |
4032 | 1133 return (*len > 0); |
1134 } | |
1135 | |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
1136 GdkPixbufAnimation * |
4032 | 1137 gtk_smiley_tree_image (GtkIMHtml *imhtml, |
1138 const gchar *sml, | |
1139 const gchar *text) | |
1140 { | |
1141 GtkSmileyTree *t; | |
1142 const gchar *x = text; | |
1143 if (sml == NULL) | |
1144 t = imhtml->default_smilies; | |
7371 | 1145 else |
4032 | 1146 t = g_hash_table_lookup(imhtml->smiley_data, sml); |
7371 | 1147 |
4032 | 1148 |
1149 if (t == NULL) | |
1150 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
1151 | |
1152 while (*x) { | |
1153 gchar *pos; | |
1154 | |
1155 if (!t->values) { | |
1156 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
1157 } | |
7371 | 1158 |
4032 | 1159 pos = strchr (t->values->str, *x); |
1160 if (pos) { | |
7371 | 1161 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 1162 } else { |
1163 return sml ? gtk_smiley_tree_image(imhtml, NULL, text) : NULL; | |
1164 } | |
1165 x++; | |
1166 } | |
1167 | |
4263 | 1168 if (!t->image->icon) |
6814
782907a6ae65
[gaim-migrate @ 7354]
Christian Hammond <chipx86@chipx86.com>
parents:
6648
diff
changeset
|
1169 t->image->icon = gdk_pixbuf_animation_new_from_file(t->image->file, NULL); |
4263 | 1170 |
1171 return t->image->icon; | |
4032 | 1172 } |
4793 | 1173 #define VALID_TAG(x) if (!g_ascii_strncasecmp (string, x ">", strlen (x ">"))) { \ |
3922 | 1174 *tag = g_strndup (string, strlen (x)); \ |
1175 *len = strlen (x) + 1; \ | |
1176 return TRUE; \ | |
1177 } \ | |
1178 (*type)++ | |
1428 | 1179 |
4793 | 1180 #define VALID_OPT_TAG(x) if (!g_ascii_strncasecmp (string, x " ", strlen (x " "))) { \ |
3922 | 1181 const gchar *c = string + strlen (x " "); \ |
1182 gchar e = '"'; \ | |
1183 gboolean quote = FALSE; \ | |
1184 while (*c) { \ | |
1185 if (*c == '"' || *c == '\'') { \ | |
1186 if (quote && (*c == e)) \ | |
1187 quote = !quote; \ | |
1188 else if (!quote) { \ | |
1189 quote = !quote; \ | |
1190 e = *c; \ | |
1191 } \ | |
1192 } else if (!quote && (*c == '>')) \ | |
1193 break; \ | |
1194 c++; \ | |
1195 } \ | |
1196 if (*c) { \ | |
1197 *tag = g_strndup (string, c - string); \ | |
1198 *len = c - string + 1; \ | |
1199 return TRUE; \ | |
1200 } \ | |
1201 } \ | |
1202 (*type)++ | |
1428 | 1203 |
1204 | |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1205 static gboolean |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1206 gtk_imhtml_is_amp_escape (const gchar *string, |
7280 | 1207 gchar **replace, |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1208 gint *length) |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1209 { |
7287 | 1210 static char buf[7]; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1211 g_return_val_if_fail (string != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1212 g_return_val_if_fail (replace != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1213 g_return_val_if_fail (length != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1214 |
4793 | 1215 if (!g_ascii_strncasecmp (string, "&", 5)) { |
7280 | 1216 *replace = "&"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1217 *length = 5; |
4793 | 1218 } else if (!g_ascii_strncasecmp (string, "<", 4)) { |
7280 | 1219 *replace = "<"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1220 *length = 4; |
4793 | 1221 } else if (!g_ascii_strncasecmp (string, ">", 4)) { |
7280 | 1222 *replace = ">"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1223 *length = 4; |
4793 | 1224 } else if (!g_ascii_strncasecmp (string, " ", 6)) { |
7280 | 1225 *replace = " "; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1226 *length = 6; |
4793 | 1227 } else if (!g_ascii_strncasecmp (string, "©", 6)) { |
7280 | 1228 *replace = "©"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1229 *length = 6; |
4793 | 1230 } else if (!g_ascii_strncasecmp (string, """, 6)) { |
7280 | 1231 *replace = "\""; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1232 *length = 6; |
4793 | 1233 } else if (!g_ascii_strncasecmp (string, "®", 5)) { |
7280 | 1234 *replace = "®"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1235 *length = 5; |
5093 | 1236 } else if (!g_ascii_strncasecmp (string, "'", 6)) { |
7280 | 1237 *replace = "\'"; |
5093 | 1238 *length = 6; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1239 } else if (*(string + 1) == '#') { |
2022
199ba82faacb
[gaim-migrate @ 2032]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2002
diff
changeset
|
1240 guint pound = 0; |
3004 | 1241 if ((sscanf (string, "&#%u;", £) == 1) && pound != 0) { |
7287 | 1242 int buflen; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1243 if (*(string + 3 + (gint)log10 (pound)) != ';') |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1244 return FALSE; |
7287 | 1245 buflen = g_unichar_to_utf8((gunichar)pound, buf); |
1246 buf[buflen] = '\0'; | |
7280 | 1247 *replace = buf; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1248 *length = 2; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1249 while (isdigit ((gint) string [*length])) (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1250 if (string [*length] == ';') (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1251 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1252 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1253 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1254 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1255 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1256 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1257 |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1258 return TRUE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1259 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1260 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1261 static gboolean |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1262 gtk_imhtml_is_tag (const gchar *string, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1263 gchar **tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1264 gint *len, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1265 gint *type) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1266 { |
8061 | 1267 char *close; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1268 *type = 1; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1269 |
8118 | 1270 |
8061 | 1271 if (!(close = strchr (string, '>'))) |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1272 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1273 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1274 VALID_TAG ("B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1275 VALID_TAG ("BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1276 VALID_TAG ("/B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1277 VALID_TAG ("/BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1278 VALID_TAG ("I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1279 VALID_TAG ("ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1280 VALID_TAG ("/I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1281 VALID_TAG ("/ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1282 VALID_TAG ("U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1283 VALID_TAG ("UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1284 VALID_TAG ("/U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1285 VALID_TAG ("/UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1286 VALID_TAG ("S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1287 VALID_TAG ("STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1288 VALID_TAG ("/S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1289 VALID_TAG ("/STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1290 VALID_TAG ("SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1291 VALID_TAG ("/SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1292 VALID_TAG ("SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1293 VALID_TAG ("/SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1294 VALID_TAG ("PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1295 VALID_TAG ("/PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1296 VALID_TAG ("TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1297 VALID_TAG ("/TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1298 VALID_TAG ("BR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1299 VALID_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1300 VALID_TAG ("/FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1301 VALID_TAG ("/A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1302 VALID_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1303 VALID_TAG ("/P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1304 VALID_TAG ("H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1305 VALID_TAG ("/H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1306 VALID_TAG ("HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1307 VALID_TAG ("/HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1308 VALID_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1309 VALID_TAG ("/BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1310 VALID_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1311 VALID_TAG ("HEAD"); |
2993 | 1312 VALID_TAG ("/HEAD"); |
1313 VALID_TAG ("BINARY"); | |
1314 VALID_TAG ("/BINARY"); | |
5093 | 1315 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1316 VALID_OPT_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1317 VALID_OPT_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1318 VALID_OPT_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1319 VALID_OPT_TAG ("A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1320 VALID_OPT_TAG ("IMG"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1321 VALID_OPT_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1322 VALID_OPT_TAG ("H3"); |
5093 | 1323 VALID_OPT_TAG ("HTML"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1324 |
5101 | 1325 VALID_TAG ("CITE"); |
1326 VALID_TAG ("/CITE"); | |
1327 VALID_TAG ("EM"); | |
1328 VALID_TAG ("/EM"); | |
1329 VALID_TAG ("STRONG"); | |
1330 VALID_TAG ("/STRONG"); | |
1331 | |
5104 | 1332 VALID_OPT_TAG ("SPAN"); |
1333 VALID_TAG ("/SPAN"); | |
5174 | 1334 VALID_TAG ("BR/"); /* hack until gtkimhtml handles things better */ |
6982 | 1335 VALID_TAG ("IMG"); |
8026 | 1336 VALID_TAG("SPAN"); |
8061 | 1337 VALID_OPT_TAG("BR"); |
7988 | 1338 |
4793 | 1339 if (!g_ascii_strncasecmp(string, "!--", strlen ("!--"))) { |
2954
f6c4f2187c08
[gaim-migrate @ 2967]
Christian Hammond <chipx86@chipx86.com>
parents:
2898
diff
changeset
|
1340 gchar *e = strstr (string + strlen("!--"), "-->"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1341 if (e) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1342 *len = e - string + strlen ("-->"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1343 *tag = g_strndup (string + strlen ("!--"), *len - strlen ("!---->")); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1344 return TRUE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1345 } |
8118 | 1346 } |
1347 | |
8061 | 1348 *type = -1; |
1349 *len = close - string + 1; | |
1350 *tag = g_strndup(string, *len - 1); | |
1351 return TRUE; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1352 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1353 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1354 static gchar* |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1355 gtk_imhtml_get_html_opt (gchar *tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1356 const gchar *opt) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1357 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1358 gchar *t = tag; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1359 gchar *e, *a; |
5177 | 1360 gchar *val; |
1361 gint len; | |
7280 | 1362 gchar *c; |
5177 | 1363 GString *ret; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1364 |
4793 | 1365 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1366 gboolean quote = FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1367 if (*t == '\0') break; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1368 while (*t && !((*t == ' ') && !quote)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1369 if (*t == '\"') |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1370 quote = ! quote; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1371 t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1372 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1373 while (*t && (*t == ' ')) t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1374 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1375 |
4793 | 1376 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1377 t += strlen (opt); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1378 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1379 return NULL; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1380 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1381 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1382 if ((*t == '\"') || (*t == '\'')) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1383 e = a = ++t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1384 while (*e && (*e != *(t - 1))) e++; |
2993 | 1385 if (*e == '\0') { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1386 return NULL; |
5177 | 1387 } else |
1388 val = g_strndup(a, e - a); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1389 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1390 e = a = t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1391 while (*e && !isspace ((gint) *e)) e++; |
5177 | 1392 val = g_strndup(a, e - a); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1393 } |
5177 | 1394 |
1395 ret = g_string_new(""); | |
1396 e = val; | |
1397 while(*e) { | |
1398 if(gtk_imhtml_is_amp_escape(e, &c, &len)) { | |
7280 | 1399 ret = g_string_append(ret, c); |
5177 | 1400 e += len; |
1401 } else { | |
1402 ret = g_string_append_c(ret, *e); | |
1403 e++; | |
1404 } | |
1405 } | |
1406 | |
1407 g_free(val); | |
8568 | 1408 |
1409 return g_string_free(ret, FALSE); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1410 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1411 |
8118 | 1412 /* Inline CSS Support - Douglas Thrift */ |
1413 static gchar* | |
1414 gtk_imhtml_get_css_opt (gchar *style, | |
1415 const gchar *opt) | |
1416 { | |
1417 gchar *t = style; | |
1418 gchar *e, *a; | |
1419 gchar *val; | |
1420 gint len; | |
1421 gchar *c; | |
1422 GString *ret; | |
1423 | |
1424 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { | |
8177 | 1425 /* gboolean quote = FALSE; */ |
8118 | 1426 if (*t == '\0') break; |
1427 while (*t && !((*t == ' ') /*&& !quote*/)) { | |
1428 /* if (*t == '\"') | |
8177 | 1429 quote = ! quote; */ |
8118 | 1430 t++; |
1431 } | |
1432 while (*t && (*t == ' ')) t++; | |
1433 } | |
1434 | |
1435 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { | |
1436 t += strlen (opt); | |
1437 } else { | |
1438 return NULL; | |
1439 } | |
1440 | |
1441 /* if ((*t == '\"') || (*t == '\'')) { | |
1442 e = a = ++t; | |
1443 while (*e && (*e != *(t - 1))) e++; | |
1444 if (*e == '\0') { | |
1445 return NULL; | |
1446 } else | |
1447 val = g_strndup(a, e - a); | |
1448 } else { | |
1449 e = a = t; | |
1450 while (*e && !isspace ((gint) *e)) e++; | |
1451 val = g_strndup(a, e - a); | |
1452 }*/ | |
1453 | |
1454 e = a = t; | |
1455 while (*e && *e != ';') e++; | |
1456 val = g_strndup(a, e - a); | |
1457 | |
1458 ret = g_string_new(""); | |
1459 e = val; | |
1460 while(*e) { | |
1461 if(gtk_imhtml_is_amp_escape(e, &c, &len)) { | |
1462 ret = g_string_append(ret, c); | |
1463 e += len; | |
1464 } else { | |
1465 ret = g_string_append_c(ret, *e); | |
1466 e++; | |
1467 } | |
1468 } | |
1469 | |
1470 g_free(val); | |
1471 val = ret->str; | |
1472 g_string_free(ret, FALSE); | |
1473 return val; | |
1474 } | |
3922 | 1475 |
8334 | 1476 static const char *accepted_protocols[] = { |
1477 "http://", | |
1478 "https://", | |
1479 "ftp://" | |
1480 }; | |
1481 | |
1482 static const int accepted_protocols_size = 3; | |
1483 | |
1484 /* returns if the beginning of the text is a protocol. If it is the protocol, returns the length so | |
1485 the caller knows how long the protocol string is. */ | |
1486 int gtk_imhtml_is_protocol(const char *text) | |
1487 { | |
1488 gint i; | |
1489 | |
1490 for(i=0; i<accepted_protocols_size; i++){ | |
1491 if( strncasecmp(text, accepted_protocols[i], strlen(accepted_protocols[i])) == 0 ){ | |
1492 return strlen(accepted_protocols[i]); | |
1493 } | |
1494 } | |
1495 return 0; | |
1496 } | |
1497 | |
8677 | 1498 /* |
1499 <KingAnt> marv: The two IM image functions in oscar are gaim_odc_send_im and gaim_odc_incoming | |
1500 | |
1501 | |
1502 [19:58] <Robot101> marv: images go into the imgstore, a refcounted... well.. hash. :) | |
1503 [19:59] <KingAnt> marv: I think the image tag used by the core is something like <img id="#"/> | |
1504 [19:59] Ro0tSiEgE robert42 RobFlynn Robot101 ross22 roz | |
1505 [20:00] <KingAnt> marv: Where the ID is the what is returned when you add the image to the imgstore using gaim_imgstore_add | |
1506 [20:00] <marv> Robot101: so how does the image get passed to serv_got_im() and serv_send_im()? just as the <img id="#" and then the prpl looks it up from the store? | |
1507 [20:00] <KingAnt> marv: Right | |
1508 [20:00] <marv> alright | |
1509 | |
1510 Here's my plan with IMImages. make gtk_imhtml_[append|insert]_text_with_images instead just | |
1511 gtkimhtml_[append|insert]_text (hrm maybe it should be called html instead of text), add a | |
1512 function for gaim to register for look up images, i.e. gtk_imhtml_set_get_img_fnc, so that | |
1513 images can be looked up like that, instead of passing a GSList of them. | |
1514 */ | |
1515 | |
1516 void gtk_imhtml_append_text_with_images (GtkIMHtml *imhtml, | |
1517 const gchar *text, | |
1518 GtkIMHtmlOptions options, | |
1519 GSList *unused) | |
1428 | 1520 { |
8677 | 1521 GtkTextIter iter, ins, sel; |
1522 GdkRectangle rect; | |
1523 int y, height, ins_offset = 0, sel_offset = 0; | |
1524 gboolean fixins = FALSE, fixsel = FALSE; | |
1525 | |
1526 g_return_if_fail (imhtml != NULL); | |
1527 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
1528 g_return_if_fail (text != NULL); | |
1529 | |
1530 | |
1531 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); | |
1532 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &ins, gtk_text_buffer_get_insert(imhtml->text_buffer)); | |
1533 if (gtk_text_iter_equal(&iter, &ins) && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) { | |
1534 fixins = TRUE; | |
1535 ins_offset = gtk_text_iter_get_offset(&ins); | |
1536 } | |
1537 | |
1538 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &sel, gtk_text_buffer_get_selection_bound(imhtml->text_buffer)); | |
1539 if (gtk_text_iter_equal(&iter, &sel) && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) { | |
1540 fixsel = TRUE; | |
1541 sel_offset = gtk_text_iter_get_offset(&sel); | |
1542 } | |
1543 | |
1544 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
1545 gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
1546 | |
1547 | |
1548 if(((y + height) - (rect.y + rect.height)) > height | |
1549 && gtk_text_buffer_get_char_count(imhtml->text_buffer)){ | |
1550 options |= GTK_IMHTML_NO_SCROLL; | |
1551 } | |
1552 | |
1553 gtk_imhtml_insert_html_at_iter(imhtml, text, options, &iter); | |
1554 | |
1555 if (fixins) { | |
1556 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &ins, ins_offset); | |
1557 gtk_text_buffer_move_mark(imhtml->text_buffer, gtk_text_buffer_get_insert(imhtml->text_buffer), &ins); | |
1558 } | |
1559 | |
1560 if (fixsel) { | |
1561 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &sel, sel_offset); | |
1562 gtk_text_buffer_move_mark(imhtml->text_buffer, gtk_text_buffer_get_selection_bound(imhtml->text_buffer), &sel); | |
1563 } | |
1564 | |
1565 if (!(options & GTK_IMHTML_NO_SCROLL)) { | |
1566 /* If this seems backwards at first glance, well it's not. | |
1567 * It means scroll such that the mark is closest to the top, | |
1568 * and closest to the right as possible. Remember kids, you have | |
1569 * to scroll left to move a given spot closest to the right, | |
1570 * and scroll down to move a spot closest to the top. | |
1571 */ | |
1572 gtk_text_iter_set_line_offset(&iter, 0); | |
1573 gtk_text_buffer_move_mark(imhtml->text_buffer, imhtml->scrollpoint, &iter); | |
1574 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(imhtml), imhtml->scrollpoint, | |
1575 0, TRUE, 1.0, 0.0); | |
1576 } | |
1577 } | |
1578 | |
1579 void gtk_imhtml_insert_html_at_iter(GtkIMHtml *imhtml, | |
1580 const gchar *text, | |
1581 GtkIMHtmlOptions options, | |
1582 GtkTextIter *iter) | |
1583 { | |
8061 | 1584 GdkRectangle rect; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1585 gint pos = 0; |
3922 | 1586 gchar *ws; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1587 gchar *tag; |
3922 | 1588 gchar *bg = NULL; |
6982 | 1589 gint len; |
4032 | 1590 gint tlen, smilelen, wpos=0; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1591 gint type; |
3922 | 1592 const gchar *c; |
7280 | 1593 gchar *amp; |
8334 | 1594 gint len_protocol; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1595 |
1428 | 1596 guint bold = 0, |
1597 italics = 0, | |
1598 underline = 0, | |
1599 strike = 0, | |
1600 sub = 0, | |
1601 sup = 0, | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
1602 title = 0, |
8061 | 1603 pre = 0; |
1428 | 1604 |
3922 | 1605 GSList *fonts = NULL; |
8506 | 1606 GObject *object; |
8061 | 1607 GtkIMHtmlScalable *scalable = NULL; |
8677 | 1608 |
1609 g_return_if_fail (imhtml != NULL); | |
1610 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
1611 g_return_if_fail (text != NULL); | |
3922 | 1612 c = text; |
6982 | 1613 len = strlen(text); |
3922 | 1614 ws = g_malloc(len + 1); |
1615 ws[0] = 0; | |
1428 | 1616 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1617 while (pos < len) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1618 if (*c == '<' && gtk_imhtml_is_tag (c + 1, &tag, &tlen, &type)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1619 c++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1620 pos++; |
8061 | 1621 ws[wpos] = '\0'; |
1622 switch (type) | |
3922 | 1623 { |
1624 case 1: /* B */ | |
1625 case 2: /* BOLD */ | |
5101 | 1626 case 54: /* STRONG */ |
8677 | 1627 |
1628 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
1629 | |
1630 if ((bold == 0) && (imhtml->format_functions & GTK_IMHTML_BOLD)) | |
8061 | 1631 gtk_imhtml_toggle_bold(imhtml); |
3922 | 1632 bold++; |
8061 | 1633 ws[0] = '\0'; wpos = 0; |
3922 | 1634 break; |
1635 case 3: /* /B */ | |
1636 case 4: /* /BOLD */ | |
5101 | 1637 case 55: /* /STRONG */ |
8677 | 1638 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1639 ws[0] = '\0'; wpos = 0; |
1640 | |
3922 | 1641 if (bold) |
1642 bold--; | |
8677 | 1643 if ((bold == 0) && (imhtml->format_functions & GTK_IMHTML_BOLD) && !imhtml->wbfo) |
8061 | 1644 gtk_imhtml_toggle_bold(imhtml); |
3922 | 1645 break; |
1646 case 5: /* I */ | |
1647 case 6: /* ITALIC */ | |
5101 | 1648 case 52: /* EM */ |
8677 | 1649 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1650 ws[0] = '\0'; wpos = 0; |
8677 | 1651 if ((italics == 0) && (imhtml->format_functions & GTK_IMHTML_ITALIC)) |
8061 | 1652 gtk_imhtml_toggle_italic(imhtml); |
3922 | 1653 italics++; |
1654 break; | |
1655 case 7: /* /I */ | |
1656 case 8: /* /ITALIC */ | |
5101 | 1657 case 53: /* /EM */ |
8677 | 1658 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1659 ws[0] = '\0'; wpos = 0; |
3922 | 1660 if (italics) |
1661 italics--; | |
8677 | 1662 if ((italics == 0) && (imhtml->format_functions & GTK_IMHTML_ITALIC) && !imhtml->wbfo) |
8061 | 1663 gtk_imhtml_toggle_italic(imhtml); |
3922 | 1664 break; |
1665 case 9: /* U */ | |
1666 case 10: /* UNDERLINE */ | |
8677 | 1667 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1668 ws[0] = '\0'; wpos = 0; |
8677 | 1669 if ((underline == 0) && (imhtml->format_functions & GTK_IMHTML_UNDERLINE)) |
8061 | 1670 gtk_imhtml_toggle_underline(imhtml); |
3922 | 1671 underline++; |
1672 break; | |
1673 case 11: /* /U */ | |
1674 case 12: /* /UNDERLINE */ | |
8677 | 1675 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1676 ws[0] = '\0'; wpos = 0; |
3922 | 1677 if (underline) |
1678 underline--; | |
8677 | 1679 if ((underline == 0) && (imhtml->format_functions & GTK_IMHTML_UNDERLINE) && !imhtml->wbfo) |
8061 | 1680 gtk_imhtml_toggle_underline(imhtml); |
3922 | 1681 break; |
1682 case 13: /* S */ | |
1683 case 14: /* STRIKE */ | |
8677 | 1684 /* FIXME: reimplement this */ |
3922 | 1685 strike++; |
1686 break; | |
1687 case 15: /* /S */ | |
1688 case 16: /* /STRIKE */ | |
8677 | 1689 /* FIXME: reimplement this */ |
3922 | 1690 if (strike) |
1691 strike--; | |
1692 break; | |
1693 case 17: /* SUB */ | |
8677 | 1694 /* FIXME: reimpliment this */ |
3922 | 1695 sub++; |
1696 break; | |
1697 case 18: /* /SUB */ | |
8677 | 1698 /* FIXME: reimpliment this */ |
3922 | 1699 if (sub) |
1700 sub--; | |
1701 break; | |
1702 case 19: /* SUP */ | |
8677 | 1703 /* FIXME: reimplement this */ |
3922 | 1704 sup++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1705 break; |
3922 | 1706 case 20: /* /SUP */ |
8677 | 1707 /* FIXME: reimplement this */ |
3922 | 1708 if (sup) |
1709 sup--; | |
1710 break; | |
1711 case 21: /* PRE */ | |
8677 | 1712 /* FIXME: reimplement this */ |
3922 | 1713 pre++; |
1714 break; | |
1715 case 22: /* /PRE */ | |
8677 | 1716 /* FIXME: reimplement this */ |
3922 | 1717 if (pre) |
1718 pre--; | |
1719 break; | |
1720 case 23: /* TITLE */ | |
8677 | 1721 /* FIXME: what was this supposed to do anyway? */ |
3922 | 1722 title++; |
1723 break; | |
1724 case 24: /* /TITLE */ | |
8677 | 1725 /* FIXME: make this undo whatever 23 was supposed to do */ |
3922 | 1726 if (title) { |
1727 if (options & GTK_IMHTML_NO_TITLE) { | |
1728 wpos = 0; | |
1729 ws [wpos] = '\0'; | |
1730 } | |
1731 title--; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1732 } |
3922 | 1733 break; |
1734 case 25: /* BR */ | |
5174 | 1735 case 58: /* BR/ */ |
8061 | 1736 case 61: /* BR (opt) */ |
3922 | 1737 ws[wpos] = '\n'; |
1738 wpos++; | |
6982 | 1739 break; |
3922 | 1740 case 26: /* HR */ |
1741 case 42: /* HR (opt) */ | |
1742 ws[wpos++] = '\n'; | |
8677 | 1743 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
1744 | |
5967 | 1745 scalable = gtk_imhtml_hr_new(); |
8061 | 1746 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
8677 | 1747 scalable->add_to(scalable, imhtml, iter); |
8061 | 1748 scalable->scale(scalable, rect.width, rect.height); |
1749 imhtml->scalables = g_list_append(imhtml->scalables, scalable); | |
1750 ws[0] = '\0'; wpos = 0; | |
7942 | 1751 ws[wpos++] = '\n'; |
8061 | 1752 |
3922 | 1753 break; |
1754 case 27: /* /FONT */ | |
8677 | 1755 if (fonts && !imhtml->wbfo) { |
5967 | 1756 GtkIMHtmlFontDetail *font = fonts->data; |
8677 | 1757 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1758 ws[0] = '\0'; wpos = 0; |
8177 | 1759 /* NEW_BIT (NEW_TEXT_BIT); */ |
8677 | 1760 |
8061 | 1761 if (font->face) { |
1762 gtk_imhtml_toggle_fontface(imhtml, NULL); | |
3922 | 1763 g_free (font->face); |
8061 | 1764 } |
1765 if (font->fore) { | |
1766 gtk_imhtml_toggle_forecolor(imhtml, NULL); | |
3922 | 1767 g_free (font->fore); |
8061 | 1768 } |
1769 if (font->back) { | |
1770 gtk_imhtml_toggle_backcolor(imhtml, NULL); | |
3922 | 1771 g_free (font->back); |
8061 | 1772 } |
4032 | 1773 if (font->sml) |
1774 g_free (font->sml); | |
3922 | 1775 g_free (font); |
8309 | 1776 |
1777 if (font->size != 3) | |
1778 gtk_imhtml_font_set_size(imhtml, 3); | |
1779 | |
1780 fonts = fonts->next; | |
1781 if (fonts) { | |
1782 GtkIMHtmlFontDetail *font = fonts->data; | |
8677 | 1783 |
1784 if (font->face) | |
8309 | 1785 gtk_imhtml_toggle_fontface(imhtml, font->face); |
8677 | 1786 if (font->fore) |
8309 | 1787 gtk_imhtml_toggle_forecolor(imhtml, font->fore); |
8677 | 1788 if (font->back) |
8309 | 1789 gtk_imhtml_toggle_backcolor(imhtml, font->back); |
1790 if (font->size != 3) | |
1791 gtk_imhtml_font_set_size(imhtml, font->size); | |
1792 } | |
3922 | 1793 } |
8309 | 1794 break; |
3922 | 1795 case 28: /* /A */ |
8677 | 1796 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
1797 gtk_imhtml_toggle_link(imhtml, NULL); | |
1798 ws[0] = '\0'; wpos = 0; | |
8061 | 1799 break; |
8118 | 1800 |
3922 | 1801 case 29: /* P */ |
1802 case 30: /* /P */ | |
1803 case 31: /* H3 */ | |
1804 case 32: /* /H3 */ | |
1805 case 33: /* HTML */ | |
1806 case 34: /* /HTML */ | |
1807 case 35: /* BODY */ | |
1808 case 36: /* /BODY */ | |
1809 case 37: /* FONT */ | |
1810 case 38: /* HEAD */ | |
1811 case 39: /* /HEAD */ | |
6982 | 1812 case 40: /* BINARY */ |
1813 case 41: /* /BINARY */ | |
3922 | 1814 break; |
1815 case 43: /* FONT (opt) */ | |
1816 { | |
4032 | 1817 gchar *color, *back, *face, *size, *sml; |
5967 | 1818 GtkIMHtmlFontDetail *font, *oldfont = NULL; |
3922 | 1819 color = gtk_imhtml_get_html_opt (tag, "COLOR="); |
1820 back = gtk_imhtml_get_html_opt (tag, "BACK="); | |
1821 face = gtk_imhtml_get_html_opt (tag, "FACE="); | |
1822 size = gtk_imhtml_get_html_opt (tag, "SIZE="); | |
4032 | 1823 sml = gtk_imhtml_get_html_opt (tag, "SML="); |
1824 if (!(color || back || face || size || sml)) | |
3922 | 1825 break; |
8061 | 1826 |
8677 | 1827 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 1828 ws[0] = '\0'; wpos = 0; |
1829 | |
5967 | 1830 font = g_new0 (GtkIMHtmlFontDetail, 1); |
3922 | 1831 if (fonts) |
1832 oldfont = fonts->data; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1833 |
8677 | 1834 if (color && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) { |
3922 | 1835 font->fore = color; |
8061 | 1836 gtk_imhtml_toggle_forecolor(imhtml, font->fore); |
8677 | 1837 } |
8309 | 1838 //else if (oldfont && oldfont->fore) |
1839 // font->fore = g_strdup(oldfont->fore); | |
8677 | 1840 |
1841 if (back && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) { | |
3922 | 1842 font->back = back; |
8061 | 1843 gtk_imhtml_toggle_backcolor(imhtml, font->back); |
8309 | 1844 } |
1845 //else if (oldfont && oldfont->back) | |
1846 // font->back = g_strdup(oldfont->back); | |
8677 | 1847 |
1848 if (face && !(options & GTK_IMHTML_NO_FONTS) && (imhtml->format_functions & GTK_IMHTML_FACE)) { | |
3922 | 1849 font->face = face; |
8061 | 1850 gtk_imhtml_toggle_fontface(imhtml, font->face); |
8309 | 1851 } |
1852 //else if (oldfont && oldfont->face) | |
1853 // font->face = g_strdup(oldfont->face); | |
4032 | 1854 |
1855 if (sml) | |
1856 font->sml = sml; | |
1857 else if (oldfont && oldfont->sml) | |
1858 font->sml = g_strdup(oldfont->sml); | |
1859 | |
8677 | 1860 if (size && !(options & GTK_IMHTML_NO_SIZES) && (imhtml->format_functions & (GTK_IMHTML_GROW|GTK_IMHTML_SHRINK))) { |
3922 | 1861 if (*size == '+') { |
1862 sscanf (size + 1, "%hd", &font->size); | |
1863 font->size += 3; | |
1864 } else if (*size == '-') { | |
1865 sscanf (size + 1, "%hd", &font->size); | |
1866 font->size = MAX (0, 3 - font->size); | |
1867 } else if (isdigit (*size)) { | |
1868 sscanf (size, "%hd", &font->size); | |
8061 | 1869 } |
6042 | 1870 if (font->size > 100) |
1871 font->size = 100; | |
3922 | 1872 } else if (oldfont) |
1873 font->size = oldfont->size; | |
8309 | 1874 else |
1875 font->size = 3; | |
1876 gtk_imhtml_font_set_size(imhtml, font->size); | |
3922 | 1877 g_free(size); |
1878 fonts = g_slist_prepend (fonts, font); | |
1879 } | |
1880 break; | |
1881 case 44: /* BODY (opt) */ | |
1882 if (!(options & GTK_IMHTML_NO_COLOURS)) { | |
1883 char *bgcolor = gtk_imhtml_get_html_opt (tag, "BGCOLOR="); | |
8677 | 1884 if (bgcolor && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) { |
1885 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8061 | 1886 ws[0] = '\0'; wpos = 0; |
8177 | 1887 /* NEW_BIT(NEW_TEXT_BIT); */ |
3922 | 1888 if (bg) |
1889 g_free(bg); | |
1890 bg = bgcolor; | |
8061 | 1891 gtk_imhtml_toggle_backcolor(imhtml, bg); |
2885
f72efa29c109
[gaim-migrate @ 2898]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2871
diff
changeset
|
1892 } |
1428 | 1893 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1894 break; |
3922 | 1895 case 45: /* A (opt) */ |
1896 { | |
1897 gchar *href = gtk_imhtml_get_html_opt (tag, "HREF="); | |
8677 | 1898 if (href && (imhtml->format_functions & GTK_IMHTML_LINK)) { |
1899 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8061 | 1900 ws[0] = '\0'; wpos = 0; |
8677 | 1901 gtk_imhtml_toggle_link(imhtml, href); |
3922 | 1902 } |
2993 | 1903 } |
3922 | 1904 break; |
4895 | 1905 case 46: /* IMG (opt) */ |
6982 | 1906 case 59: /* IMG */ |
4895 | 1907 { |
8677 | 1908 #if 0 |
1909 /* disabling this for now, it's easy to add it back... */ | |
6982 | 1910 GdkPixbuf *img = NULL; |
1911 const gchar *filename = NULL; | |
4895 | 1912 |
8677 | 1913 if (!(imhtml->format_functions & GTK_IMHTML_IMAGE)) |
1914 break; | |
1915 | |
6982 | 1916 if (images && images->data) { |
1917 img = images->data; | |
1918 images = images->next; | |
1919 filename = g_object_get_data(G_OBJECT(img), "filename"); | |
1920 g_object_ref(G_OBJECT(img)); | |
1921 } else { | |
1922 img = gtk_widget_render_icon(GTK_WIDGET(imhtml), | |
1923 GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON, | |
1924 "gtkimhtml-missing-image"); | |
1925 } | |
4895 | 1926 |
6982 | 1927 scalable = gtk_imhtml_image_new(img, filename); |
8177 | 1928 /* NEW_BIT(NEW_SCALABLE_BIT); */ |
8677 | 1929 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
1930 scalable->add_to(scalable, imhtml, iter); | |
1931 scalable->scale(scalable, rect.width, rect.height); | |
1932 imhtml->scalables = g_list_append(imhtml->scalables, scalable); | |
1933 | |
6982 | 1934 g_object_unref(G_OBJECT(img)); |
8677 | 1935 #endif |
4895 | 1936 } |
3922 | 1937 case 47: /* P (opt) */ |
1938 case 48: /* H3 (opt) */ | |
5093 | 1939 case 49: /* HTML (opt) */ |
5101 | 1940 case 50: /* CITE */ |
1941 case 51: /* /CITE */ | |
8026 | 1942 case 56: /* SPAN (opt) */ |
8118 | 1943 /* Inline CSS Support - Douglas Thrift |
1944 * | |
1945 * color | |
1946 * font-family | |
1947 * font-size | |
1948 */ | |
1949 { | |
1950 gchar *style, *color, *family, *size; | |
1951 GtkIMHtmlFontDetail *font, *oldfont = NULL; | |
1952 style = gtk_imhtml_get_html_opt (tag, "style="); | |
1953 | |
1954 if (!style) break; | |
1955 | |
1956 color = gtk_imhtml_get_css_opt (style, "color: "); | |
1957 family = gtk_imhtml_get_css_opt (style, | |
1958 "font-family: "); | |
1959 size = gtk_imhtml_get_css_opt (style, "font-size: "); | |
1960 | |
8120 | 1961 if (!(color || family || size)) { |
1962 g_free(style); | |
1963 break; | |
1964 } | |
8118 | 1965 |
8677 | 1966 |
1967 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8118 | 1968 ws[0] = '\0'; wpos = 0; |
8177 | 1969 /* NEW_BIT (NEW_TEXT_BIT); */ |
8118 | 1970 |
1971 font = g_new0 (GtkIMHtmlFontDetail, 1); | |
1972 if (fonts) | |
1973 oldfont = fonts->data; | |
1974 | |
8677 | 1975 if (color && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) |
8118 | 1976 font->fore = color; |
1977 else if (oldfont && oldfont->fore) | |
1978 font->fore = g_strdup(oldfont->fore); | |
1979 | |
8677 | 1980 if (oldfont && oldfont->back && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) |
8118 | 1981 font->back = g_strdup(oldfont->back); |
1982 | |
8677 | 1983 if (family && !(options & GTK_IMHTML_NO_FONTS) && (imhtml->format_functions & GTK_IMHTML_FACE)) |
8118 | 1984 font->face = family; |
1985 else if (oldfont && oldfont->face) | |
1986 font->face = g_strdup(oldfont->face); | |
1987 if (font->face && (atoi(font->face) > 100)) { | |
8677 | 1988 /* WTF is this? */ |
8118 | 1989 g_free(font->face); |
1990 font->face = g_strdup("100"); | |
1991 } | |
1992 | |
1993 if (oldfont && oldfont->sml) | |
1994 font->sml = g_strdup(oldfont->sml); | |
1995 | |
8677 | 1996 if (size && !(options & GTK_IMHTML_NO_SIZES) && (imhtml->format_functions & (GTK_IMHTML_SHRINK|GTK_IMHTML_GROW))) { |
8118 | 1997 if (g_ascii_strcasecmp(size, "smaller") == 0) |
1998 { | |
1999 font->size = 2; | |
2000 } | |
2001 else if (g_ascii_strcasecmp(size, "larger") == 0) | |
2002 { | |
2003 font->size = 4; | |
2004 } | |
2005 else | |
2006 { | |
2007 font->size = 3; | |
2008 } | |
2009 } else if (oldfont) | |
2010 font->size = oldfont->size; | |
2011 | |
2012 g_free(style); | |
2013 g_free(size); | |
2014 fonts = g_slist_prepend (fonts, font); | |
2015 } | |
2016 break; | |
5104 | 2017 case 57: /* /SPAN */ |
8118 | 2018 /* Inline CSS Support - Douglas Thrift */ |
8677 | 2019 if (fonts && !imhtml->wbfo) { |
8118 | 2020 GtkIMHtmlFontDetail *font = fonts->data; |
8677 | 2021 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8118 | 2022 ws[0] = '\0'; wpos = 0; |
8177 | 2023 /* NEW_BIT (NEW_TEXT_BIT); */ |
8118 | 2024 fonts = g_slist_remove (fonts, font); |
2025 if (font->face) | |
2026 g_free (font->face); | |
2027 if (font->fore) | |
2028 g_free (font->fore); | |
2029 if (font->back) | |
2030 g_free (font->back); | |
2031 if (font->sml) | |
2032 g_free (font->sml); | |
2033 g_free (font); | |
2034 } | |
2035 break; | |
8026 | 2036 case 60: /* SPAN */ |
2993 | 2037 break; |
8061 | 2038 case 62: /* comment */ |
8177 | 2039 /* NEW_BIT (NEW_TEXT_BIT); */ |
8317 | 2040 ws[wpos] = '\0'; |
8677 | 2041 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2042 | |
6124 | 2043 if (imhtml->show_comments) |
2044 wpos = g_snprintf (ws, len, "%s", tag); | |
8177 | 2045 /* NEW_BIT (NEW_COMMENT_BIT); */ |
3922 | 2046 break; |
2047 default: | |
6882 | 2048 break; |
2993 | 2049 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2050 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2051 pos += tlen; |
4138 | 2052 if(tag) |
2053 g_free(tag); /* This was allocated back in VALID_TAG() */ | |
8473 | 2054 } else if (imhtml->show_smileys && (gtk_imhtml_is_smiley (imhtml, fonts, c, &smilelen) || gtk_imhtml_is_smiley(imhtml, NULL, c, &smilelen))) { |
2055 GtkIMHtmlFontDetail *fd; | |
2056 | |
2057 gchar *sml = NULL; | |
2058 if (fonts) { | |
2059 fd = fonts->data; | |
2060 sml = fd->sml; | |
2061 } | |
8677 | 2062 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8505 | 2063 wpos = g_snprintf (ws, smilelen + 1, "%s", c); |
8473 | 2064 |
8677 | 2065 gtk_imhtml_insert_smiley_at_iter(imhtml, sml, ws, iter); |
8473 | 2066 |
8505 | 2067 c += smilelen; |
2068 pos += smilelen; | |
8473 | 2069 wpos = 0; |
2070 ws[0] = 0; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2071 } else if (*c == '&' && gtk_imhtml_is_amp_escape (c, &, &tlen)) { |
7280 | 2072 while(*amp) { |
2073 ws [wpos++] = *amp++; | |
2074 } | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2075 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2076 pos += tlen; |
1428 | 2077 } else if (*c == '\n') { |
2078 if (!(options & GTK_IMHTML_NO_NEWLINE)) { | |
3922 | 2079 ws[wpos] = '\n'; |
2080 wpos++; | |
8677 | 2081 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2082 ws[0] = '\0'; |
2083 wpos = 0; | |
8177 | 2084 /* NEW_BIT (NEW_TEXT_BIT); */ |
1428 | 2085 } |
2086 c++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2087 pos++; |
8334 | 2088 } else if ((len_protocol = gtk_imhtml_is_protocol(c)) > 0){ |
2089 while(len_protocol--){ | |
8677 | 2090 /* Skip the next len_protocol characters, but make sure they're |
8334 | 2091 copied into the ws array. |
2092 */ | |
2093 ws [wpos++] = *c++; | |
2094 pos++; | |
2095 } | |
8061 | 2096 } else if (*c) { |
1428 | 2097 ws [wpos++] = *c++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2098 pos++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2099 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2100 break; |
1428 | 2101 } |
2102 } | |
8677 | 2103 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2104 ws[0] = '\0'; wpos = 0; |
2105 | |
8177 | 2106 /* NEW_BIT(NEW_TEXT_BIT); */ |
8061 | 2107 |
4032 | 2108 while (fonts) { |
5967 | 2109 GtkIMHtmlFontDetail *font = fonts->data; |
4032 | 2110 fonts = g_slist_remove (fonts, font); |
2111 if (font->face) | |
2112 g_free (font->face); | |
2113 if (font->fore) | |
2114 g_free (font->fore); | |
2115 if (font->back) | |
2116 g_free (font->back); | |
2117 if (font->sml) | |
2118 g_free (font->sml); | |
2119 g_free (font); | |
8677 | 2120 //if (str) |
2121 // str = g_string_append (str, "</FONT>"); | |
4032 | 2122 } |
8677 | 2123 #if 0 |
3922 | 2124 if (str) { |
1428 | 2125 while (bold) { |
8677 | 2126 // str = g_string_append (str, "</B>"); |
1428 | 2127 bold--; |
2128 } | |
2129 while (italics) { | |
8677 | 2130 // str = g_string_append (str, "</I>"); |
1428 | 2131 italics--; |
2132 } | |
2133 while (underline) { | |
8677 | 2134 //str = g_string_append (str, "</U>"); |
1428 | 2135 underline--; |
2136 } | |
2137 while (strike) { | |
8677 | 2138 //str = g_string_append (str, "</S>"); |
1428 | 2139 strike--; |
2140 } | |
2141 while (sub) { | |
8677 | 2142 //str = g_string_append (str, "</SUB>"); |
1428 | 2143 sub--; |
2144 } | |
2145 while (sup) { | |
8677 | 2146 //str = g_string_append (str, "</SUP>"); |
1428 | 2147 sup--; |
2148 } | |
2149 while (title) { | |
8677 | 2150 //str = g_string_append (str, "</TITLE>"); |
1428 | 2151 title--; |
2152 } | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
2153 while (pre) { |
8677 | 2154 //str = g_string_append (str, "</PRE>"); |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
2155 pre--; |
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
2156 } |
1428 | 2157 } |
8677 | 2158 #endif |
4032 | 2159 g_free (ws); |
4630 | 2160 if(bg) |
2161 g_free(bg); | |
8677 | 2162 |
2163 if (!imhtml->wbfo) | |
2164 gtk_imhtml_close_tags(imhtml); | |
8506 | 2165 |
2166 object = g_object_ref(G_OBJECT(imhtml)); | |
2167 g_signal_emit(object, signals[UPDATE_FORMAT], 0); | |
2168 g_object_unref(object); | |
2169 | |
3922 | 2170 } |
2171 | |
4892 | 2172 void gtk_imhtml_remove_smileys(GtkIMHtml *imhtml) |
2173 { | |
4288 | 2174 g_hash_table_destroy(imhtml->smiley_data); |
2175 gtk_smiley_tree_destroy(imhtml->default_smilies); | |
4892 | 2176 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 2177 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4288 | 2178 imhtml->default_smilies = gtk_smiley_tree_new(); |
2179 } | |
8481 | 2180 |
3922 | 2181 void gtk_imhtml_show_smileys (GtkIMHtml *imhtml, |
4253 | 2182 gboolean show) |
2183 { | |
2184 imhtml->show_smileys = show; | |
2185 } | |
3922 | 2186 |
2187 void gtk_imhtml_show_comments (GtkIMHtml *imhtml, | |
4253 | 2188 gboolean show) |
2189 { | |
6124 | 2190 imhtml->show_comments = show; |
4253 | 2191 } |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2192 |
8456 | 2193 void gtk_imhtml_html_shortcuts (GtkIMHtml *imhtml, |
2194 gboolean allow) | |
2195 { | |
2196 imhtml->html_shortcuts = allow; | |
2197 } | |
2198 | |
2199 void gtk_imhtml_smiley_shortcuts (GtkIMHtml *imhtml, | |
2200 gboolean allow) | |
2201 { | |
2202 imhtml->smiley_shortcuts = allow; | |
2203 } | |
2204 | |
2205 void | |
2206 gtk_imhtml_set_protocol_name(GtkIMHtml *imhtml, gchar *protocol_name) { | |
2207 imhtml->protocol_name = protocol_name; | |
2208 } | |
2209 | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2210 void |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2211 gtk_imhtml_clear (GtkIMHtml *imhtml) |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2212 { |
7991 | 2213 GList *del; |
3922 | 2214 GtkTextIter start, end; |
8427 | 2215 GObject *object = g_object_ref(G_OBJECT(imhtml)); |
7991 | 2216 |
3922 | 2217 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
2218 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
2219 gtk_text_buffer_delete(imhtml->text_buffer, &start, &end); | |
7991 | 2220 |
2221 for(del = imhtml->scalables; del; del = del->next) { | |
2222 GtkIMHtmlScalable *scale = del->data; | |
2223 scale->free(scale); | |
2224 } | |
2225 g_list_free(imhtml->scalables); | |
2226 imhtml->scalables = NULL; | |
8061 | 2227 |
8677 | 2228 imhtml->edit.bold = FALSE; |
2229 imhtml->edit.italic = FALSE; | |
2230 imhtml->edit.underline = FALSE; | |
2231 | |
2232 if (imhtml->edit.fontface) | |
2233 g_free(imhtml->edit.fontface); | |
8061 | 2234 imhtml->edit.fontface = NULL; |
8677 | 2235 |
2236 if (imhtml->edit.forecolor) | |
2237 g_free(imhtml->edit.forecolor); | |
8061 | 2238 imhtml->edit.forecolor = NULL; |
8677 | 2239 |
2240 if (imhtml->edit.backcolor) | |
2241 g_free(imhtml->edit.backcolor); | |
8061 | 2242 imhtml->edit.backcolor = NULL; |
8677 | 2243 |
2244 imhtml->edit.fontsize = 0; | |
8481 | 2245 |
8427 | 2246 g_signal_emit(object, signals[CLEAR_FORMAT], 0); |
2247 g_object_unref(object); | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2248 } |
2363
08c66712364c
[gaim-migrate @ 2376]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2349
diff
changeset
|
2249 |
4046 | 2250 void gtk_imhtml_page_up (GtkIMHtml *imhtml) |
2251 { | |
5282 | 2252 GdkRectangle rect; |
2253 GtkTextIter iter; | |
4046 | 2254 |
5282 | 2255 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
2256 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
2257 rect.y - rect.height); | |
2258 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
8061 | 2259 |
4046 | 2260 } |
5282 | 2261 void gtk_imhtml_page_down (GtkIMHtml *imhtml) |
2262 { | |
2263 GdkRectangle rect; | |
2264 GtkTextIter iter; | |
2265 | |
2266 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
2267 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
2268 rect.y + rect.height); | |
2269 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
2270 } | |
4735 | 2271 |
5967 | 2272 /* GtkIMHtmlScalable, gtk_imhtml_image, gtk_imhtml_hr */ |
6982 | 2273 GtkIMHtmlScalable *gtk_imhtml_image_new(GdkPixbuf *img, const gchar *filename) |
4735 | 2274 { |
5967 | 2275 GtkIMHtmlImage *im_image = g_malloc(sizeof(GtkIMHtmlImage)); |
5012 | 2276 GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(img)); |
4895 | 2277 |
5967 | 2278 GTK_IMHTML_SCALABLE(im_image)->scale = gtk_imhtml_image_scale; |
2279 GTK_IMHTML_SCALABLE(im_image)->add_to = gtk_imhtml_image_add_to; | |
2280 GTK_IMHTML_SCALABLE(im_image)->free = gtk_imhtml_image_free; | |
5046 | 2281 |
2282 im_image->pixbuf = img; | |
5012 | 2283 im_image->image = image; |
4895 | 2284 im_image->width = gdk_pixbuf_get_width(img); |
2285 im_image->height = gdk_pixbuf_get_height(img); | |
2286 im_image->mark = NULL; | |
6982 | 2287 im_image->filename = filename ? g_strdup(filename) : NULL; |
4895 | 2288 |
5046 | 2289 g_object_ref(img); |
4895 | 2290 return GTK_IMHTML_SCALABLE(im_image); |
2291 } | |
2292 | |
5967 | 2293 void gtk_imhtml_image_scale(GtkIMHtmlScalable *scale, int width, int height) |
4895 | 2294 { |
5967 | 2295 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; |
4895 | 2296 |
2297 if(image->width > width || image->height > height){ | |
2298 GdkPixbuf *new_image = NULL; | |
2299 float factor; | |
2300 int new_width = image->width, new_height = image->height; | |
2301 | |
8588 | 2302 if(image->width > (width - 2)){ |
4895 | 2303 factor = (float)(width)/image->width; |
2304 new_width = width; | |
2305 new_height = image->height * factor; | |
2306 } | |
8588 | 2307 if(new_height >= (height - 2)){ |
4895 | 2308 factor = (float)(height)/new_height; |
2309 new_height = height; | |
2310 new_width = new_width * factor; | |
2311 } | |
2312 | |
5046 | 2313 new_image = gdk_pixbuf_scale_simple(image->pixbuf, new_width, new_height, GDK_INTERP_BILINEAR); |
5012 | 2314 gtk_image_set_from_pixbuf(image->image, new_image); |
4895 | 2315 g_object_unref(G_OBJECT(new_image)); |
2316 } | |
2317 } | |
2318 | |
5012 | 2319 static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel) |
2320 { | |
2321 const gchar *filename = gtk_file_selection_get_filename(sel); | |
5967 | 2322 gchar *dirname; |
2323 GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage"); | |
5012 | 2324 gchar *type = NULL; |
5019 | 2325 GError *error = NULL; |
5015 | 2326 #if GTK_CHECK_VERSION(2,2,0) |
5012 | 2327 GSList *formats = gdk_pixbuf_get_formats(); |
6162 | 2328 #else |
2329 char *basename = g_path_get_basename(filename); | |
2330 char *ext = strrchr(basename, '.'); | |
5959 | 2331 #endif |
5012 | 2332 |
5967 | 2333 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { |
2334 /* append a / if needed */ | |
2335 if (filename[strlen(filename) - 1] != '/') { | |
2336 dirname = g_strconcat(filename, "/", NULL); | |
2337 } else { | |
2338 dirname = g_strdup(filename); | |
2339 } | |
2340 gtk_file_selection_set_filename(sel, dirname); | |
2341 g_free(dirname); | |
5959 | 2342 return; |
5967 | 2343 } |
5959 | 2344 |
2345 #if GTK_CHECK_VERSION(2,2,0) | |
5012 | 2346 while(formats){ |
2347 GdkPixbufFormat *format = formats->data; | |
2348 gchar **extensions = gdk_pixbuf_format_get_extensions(format); | |
2349 gpointer p = extensions; | |
2350 | |
2351 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ | |
2352 gchar *fmt_ext = extensions[0]; | |
2353 const gchar* file_ext = filename + strlen(filename) - strlen(fmt_ext); | |
2354 | |
2355 if(!strcmp(fmt_ext, file_ext)){ | |
2356 type = gdk_pixbuf_format_get_name(format); | |
2357 break; | |
2358 } | |
2359 | |
2360 extensions++; | |
2361 } | |
2362 | |
2363 g_strfreev(p); | |
2364 | |
2365 if(type) | |
2366 break; | |
2367 | |
2368 formats = formats->next; | |
2369 } | |
2370 | |
5020 | 2371 g_slist_free(formats); |
2372 #else | |
2373 /* this is really ugly code, but I think it will work */ | |
2374 if(ext) { | |
2375 ext++; | |
2376 if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) | |
2377 type = g_strdup("jpeg"); | |
2378 else if(!g_ascii_strcasecmp(ext, "png")) | |
2379 type = g_strdup("png"); | |
2380 } | |
2381 | |
2382 g_free(basename); | |
2383 #endif | |
2384 | |
5012 | 2385 /* If I can't find a valid type, I will just tell the user about it and then assume |
2386 it's a png */ | |
2387 if(!type){ | |
2388 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
5967 | 2389 _("Unable to guess the image type based on the file extension supplied. Defaulting to PNG.")); |
5012 | 2390 type = g_strdup("png"); |
2391 } | |
2392 | |
5046 | 2393 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); |
5012 | 2394 |
2395 if(error){ | |
2396 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, | |
2397 _("Error saving image: %s"), error->message); | |
2398 g_error_free(error); | |
2399 } | |
2400 | |
2401 g_free(type); | |
2402 } | |
2403 | |
5967 | 2404 static void gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image) |
5012 | 2405 { |
5967 | 2406 GtkWidget *sel = gtk_file_selection_new(_("Save Image")); |
5012 | 2407 |
6982 | 2408 if (image->filename) |
2409 gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename); | |
5967 | 2410 g_object_set_data(G_OBJECT(sel), "GtkIMHtmlImage", image); |
5012 | 2411 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", |
2412 G_CALLBACK(write_img_to_file), sel); | |
2413 | |
2414 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked", | |
2415 G_CALLBACK(gtk_widget_destroy), sel); | |
2416 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked", | |
8061 | 2417 G_CALLBACK(gtk_widget_destroy), sel); |
5012 | 2418 |
2419 gtk_widget_show(sel); | |
2420 } | |
2421 | |
5967 | 2422 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image) |
5012 | 2423 { |
2424 GdkEventButton *event_button = (GdkEventButton *) event; | |
2425 | |
2426 if (event->type == GDK_BUTTON_RELEASE) { | |
2427 if(event_button->button == 3) { | |
2428 GtkWidget *img, *item, *menu; | |
2429 gchar *text = g_strdup_printf(_("_Save Image...")); | |
2430 menu = gtk_menu_new(); | |
2431 | |
2432 /* buttons and such */ | |
2433 img = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU); | |
2434 item = gtk_image_menu_item_new_with_mnemonic(text); | |
2435 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); | |
5967 | 2436 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_imhtml_image_save), image); |
5012 | 2437 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
2438 | |
2439 gtk_widget_show_all(menu); | |
2440 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
2441 event_button->button, event_button->time); | |
2442 | |
2443 g_free(text); | |
2444 return TRUE; | |
2445 } | |
2446 } | |
2447 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) | |
2448 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
2449 be caught by the regular GtkTextView menu */ | |
2450 else | |
2451 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
2452 | |
2453 } | |
5967 | 2454 void gtk_imhtml_image_free(GtkIMHtmlScalable *scale) |
2455 { | |
2456 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
2457 | |
2458 g_object_unref(image->pixbuf); | |
6982 | 2459 if (image->filename) |
2460 g_free(image->filename); | |
5967 | 2461 g_free(scale); |
2462 } | |
2463 | |
2464 void gtk_imhtml_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
2465 { | |
2466 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
2467 GtkWidget *box = gtk_event_box_new(); | |
2468 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
2469 | |
2470 gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(image->image)); | |
2471 | |
2472 gtk_widget_show(GTK_WIDGET(image->image)); | |
2473 gtk_widget_show(box); | |
2474 | |
2475 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), box, anchor); | |
2476 g_signal_connect(G_OBJECT(box), "event", G_CALLBACK(gtk_imhtml_image_clicked), image); | |
2477 } | |
2478 | |
2479 GtkIMHtmlScalable *gtk_imhtml_hr_new() | |
2480 { | |
2481 GtkIMHtmlHr *hr = g_malloc(sizeof(GtkIMHtmlHr)); | |
2482 | |
2483 GTK_IMHTML_SCALABLE(hr)->scale = gtk_imhtml_hr_scale; | |
2484 GTK_IMHTML_SCALABLE(hr)->add_to = gtk_imhtml_hr_add_to; | |
2485 GTK_IMHTML_SCALABLE(hr)->free = gtk_imhtml_hr_free; | |
2486 | |
2487 hr->sep = gtk_hseparator_new(); | |
2488 gtk_widget_set_size_request(hr->sep, 5000, 2); | |
2489 gtk_widget_show(hr->sep); | |
2490 | |
2491 return GTK_IMHTML_SCALABLE(hr); | |
2492 } | |
2493 | |
2494 void gtk_imhtml_hr_scale(GtkIMHtmlScalable *scale, int width, int height) | |
2495 { | |
8588 | 2496 gtk_widget_set_size_request(((GtkIMHtmlHr *)scale)->sep, width - 2, 2); |
5967 | 2497 } |
2498 | |
2499 void gtk_imhtml_hr_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
2500 { | |
2501 GtkIMHtmlHr *hr = (GtkIMHtmlHr *)scale; | |
2502 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
8061 | 2503 g_object_set_data(G_OBJECT(anchor), "text_tag", "<hr>"); |
5967 | 2504 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), hr->sep, anchor); |
2505 } | |
2506 | |
2507 void gtk_imhtml_hr_free(GtkIMHtmlScalable *scale) | |
2508 { | |
2509 g_free(scale); | |
2510 } | |
7295 | 2511 |
2512 gboolean gtk_imhtml_search_find(GtkIMHtml *imhtml, const gchar *text) | |
2513 { | |
2514 GtkTextIter iter, start, end; | |
2515 gboolean new_search = TRUE; | |
2516 | |
2517 g_return_val_if_fail(imhtml != NULL, FALSE); | |
2518 g_return_val_if_fail(text != NULL, FALSE); | |
8061 | 2519 |
7295 | 2520 if (imhtml->search_string && !strcmp(text, imhtml->search_string)) |
2521 new_search = FALSE; | |
8061 | 2522 |
7295 | 2523 if (new_search) { |
2524 gtk_imhtml_search_clear(imhtml); | |
2525 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &iter); | |
2526 } else { | |
2527 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, | |
8061 | 2528 gtk_text_buffer_get_mark(imhtml->text_buffer, "search")); |
7295 | 2529 } |
2530 imhtml->search_string = g_strdup(text); | |
2531 | |
7358 | 2532 if (gtk_source_iter_forward_search(&iter, imhtml->search_string, |
2533 GTK_SOURCE_SEARCH_VISIBLE_ONLY | GTK_SOURCE_SEARCH_CASE_INSENSITIVE, | |
7295 | 2534 &start, &end, NULL)) { |
2535 | |
2536 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &start, 0, TRUE, 0, 0); | |
2537 gtk_text_buffer_create_mark(imhtml->text_buffer, "search", &end, FALSE); | |
2538 if (new_search) { | |
2539 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &iter, &end); | |
8061 | 2540 do |
7295 | 2541 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "search", &start, &end); |
8061 | 2542 while (gtk_source_iter_forward_search(&end, imhtml->search_string, |
2543 GTK_SOURCE_SEARCH_VISIBLE_ONLY | | |
7358 | 2544 GTK_SOURCE_SEARCH_CASE_INSENSITIVE, |
7295 | 2545 &start, &end, NULL)); |
2546 } | |
2547 return TRUE; | |
2548 } | |
8061 | 2549 |
2550 gtk_imhtml_search_clear(imhtml); | |
2551 | |
7295 | 2552 return FALSE; |
2553 } | |
2554 | |
2555 void gtk_imhtml_search_clear(GtkIMHtml *imhtml) | |
2556 { | |
2557 GtkTextIter start, end; | |
8061 | 2558 |
7295 | 2559 g_return_if_fail(imhtml != NULL); |
8061 | 2560 |
7295 | 2561 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
2562 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
2563 | |
2564 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &start, &end); | |
2565 if (imhtml->search_string) | |
2566 g_free(imhtml->search_string); | |
2567 imhtml->search_string = NULL; | |
2568 } | |
8061 | 2569 |
8677 | 2570 static GtkTextTag *find_font_forecolor_tag(GtkIMHtml *imhtml, gchar *color) |
2571 { | |
2572 gchar str[18]; | |
2573 GtkTextTag *tag; | |
2574 | |
2575 g_snprintf(str, sizeof(str), "FORECOLOR %s", color); | |
2576 | |
2577 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
2578 if (!tag) | |
2579 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "foreground", color, NULL); | |
2580 | |
2581 return tag; | |
2582 } | |
2583 | |
2584 static GtkTextTag *find_font_backcolor_tag(GtkIMHtml *imhtml, gchar *color) | |
2585 { | |
2586 gchar str[18]; | |
2587 GtkTextTag *tag; | |
2588 | |
2589 g_snprintf(str, sizeof(str), "BACKCOLOR %s", color); | |
2590 | |
2591 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
2592 if (!tag) | |
2593 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "background", color, NULL); | |
2594 | |
2595 return tag; | |
2596 } | |
2597 | |
2598 static GtkTextTag *find_font_face_tag(GtkIMHtml *imhtml, gchar *face) | |
8061 | 2599 { |
8677 | 2600 gchar str[256]; |
2601 GtkTextTag *tag; | |
2602 | |
2603 g_snprintf(str, sizeof(str), "FONT FACE %s", face); | |
2604 str[255] = '\0'; | |
2605 | |
2606 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
2607 if (!tag) | |
2608 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "family", face, NULL); | |
2609 | |
2610 return tag; | |
2611 } | |
2612 | |
2613 static GtkTextTag *find_font_size_tag(GtkIMHtml *imhtml, int size) | |
2614 { | |
2615 gchar str[24]; | |
2616 GtkTextTag *tag; | |
2617 | |
2618 g_snprintf(str, sizeof(str), "FONT SIZE %d", size); | |
2619 str[23] = '\0'; | |
2620 | |
2621 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
2622 if (!tag) { | |
2623 /* For reasons I don't understand, setting "scale" here scaled based on some default | |
2624 * size other than my theme's default size. Our size 4 was actually smaller than | |
2625 * our size 3 for me. So this works around that oddity. | |
2626 */ | |
2627 GtkTextAttributes *attr = gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(imhtml)); | |
2628 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "size", | |
2629 (gint) (pango_font_description_get_size(attr->font) * | |
2630 (double) _point_sizes[size-1]), NULL); | |
2631 gtk_text_attributes_unref(attr); | |
8061 | 2632 } |
2633 | |
8677 | 2634 return tag; |
2635 } | |
2636 | |
2637 static void remove_tag_by_prefix(GtkIMHtml *imhtml, const GtkTextIter *i, const GtkTextIter *e, | |
2638 const char *prefix, guint len, gboolean homo) | |
2639 { | |
2640 GSList *tags, *l; | |
2641 GtkTextIter iter; | |
2642 | |
2643 tags = gtk_text_iter_get_tags(i); | |
2644 | |
2645 for (l = tags; l; l = l->next) { | |
2646 GtkTextTag *tag = l->data; | |
2647 | |
2648 if (tag->name && !strncmp(tag->name, prefix, len)) | |
2649 gtk_text_buffer_remove_tag(imhtml->text_buffer, tag, i, e); | |
8061 | 2650 } |
2651 | |
8677 | 2652 g_slist_free(tags); |
2653 | |
2654 if (homo) | |
2655 return; | |
2656 | |
2657 iter = *i; | |
2658 | |
2659 while (gtk_text_iter_forward_char(&iter) && !gtk_text_iter_equal(&iter, e)) { | |
2660 if (gtk_text_iter_begins_tag(&iter, NULL)) { | |
2661 tags = gtk_text_iter_get_toggled_tags(&iter, TRUE); | |
2662 | |
2663 for (l = tags; l; l = l->next) { | |
2664 GtkTextTag *tag = l->data; | |
2665 | |
2666 if (tag->name && !strncmp(tag->name, prefix, len)) | |
2667 gtk_text_buffer_remove_tag(imhtml->text_buffer, tag, &iter, e); | |
2668 } | |
2669 | |
2670 g_slist_free(tags); | |
2671 } | |
8061 | 2672 } |
8677 | 2673 } |
2674 | |
2675 static void remove_font_size(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
2676 { | |
2677 remove_tag_by_prefix(imhtml, i, e, "FONT SIZE ", 10, homo); | |
2678 } | |
2679 | |
2680 static void remove_font_face(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
2681 { | |
2682 remove_tag_by_prefix(imhtml, i, e, "FONT FACE ", 10, homo); | |
2683 } | |
2684 | |
2685 static void remove_font_forecolor(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
2686 { | |
2687 remove_tag_by_prefix(imhtml, i, e, "FORECOLOR ", 10, homo); | |
2688 } | |
2689 | |
2690 static void remove_font_backcolor(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
2691 { | |
2692 remove_tag_by_prefix(imhtml, i, e, "BACKCOLOR ", 10, homo); | |
2693 } | |
2694 | |
2695 static void remove_font_link(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
2696 { | |
2697 remove_tag_by_prefix(imhtml, i, e, "LINK ", 5, homo); | |
2698 } | |
2699 | |
2700 /* Editable stuff */ | |
2701 static void preinsert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml) | |
2702 { | |
2703 imhtml->insert_offset = gtk_text_iter_get_offset(iter); | |
2704 } | |
2705 | |
2706 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *end, gchar *text, gint len, GtkIMHtml *imhtml) | |
2707 { | |
2708 GtkTextIter start; | |
2709 | |
2710 if (!len) | |
2711 return; | |
2712 | |
2713 start = *end; | |
2714 gtk_text_iter_set_offset(&start, imhtml->insert_offset); | |
2715 | |
2716 if (imhtml->edit.bold) | |
2717 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, end); | |
2718 else | |
2719 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, end); | |
2720 | |
2721 if (imhtml->edit.italic) | |
2722 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, end); | |
2723 else | |
2724 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, end); | |
2725 | |
2726 if (imhtml->edit.underline) | |
2727 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, end); | |
2728 else | |
2729 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, end); | |
2730 | |
2731 if (imhtml->edit.forecolor) { | |
2732 remove_font_forecolor(imhtml, &start, end, TRUE); | |
2733 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2734 find_font_forecolor_tag(imhtml, imhtml->edit.forecolor), | |
2735 &start, end); | |
8061 | 2736 } |
2737 | |
8677 | 2738 if (imhtml->edit.backcolor) { |
2739 remove_font_backcolor(imhtml, &start, end, TRUE); | |
2740 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2741 find_font_backcolor_tag(imhtml, imhtml->edit.backcolor), | |
2742 &start, end); | |
2743 } | |
2744 | |
2745 if (imhtml->edit.fontface) { | |
2746 remove_font_face(imhtml, &start, end, TRUE); | |
2747 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2748 find_font_face_tag(imhtml, imhtml->edit.fontface), | |
2749 &start, end); | |
8061 | 2750 } |
8677 | 2751 |
2752 if (imhtml->edit.fontsize) { | |
2753 remove_font_size(imhtml, &start, end, TRUE); | |
2754 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2755 find_font_size_tag(imhtml, imhtml->edit.fontsize), | |
2756 &start, end); | |
2757 } | |
2758 | |
2759 if (imhtml->edit.link) { | |
2760 remove_font_link(imhtml, &start, end, TRUE); | |
2761 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2762 imhtml->edit.link, | |
2763 &start, end); | |
2764 } | |
2765 | |
8061 | 2766 } |
2767 | |
2768 void gtk_imhtml_set_editable(GtkIMHtml *imhtml, gboolean editable) | |
2769 { | |
2770 gtk_text_view_set_editable(GTK_TEXT_VIEW(imhtml), editable); | |
8177 | 2771 /* |
2772 * We need a visible caret for accessibility, so mouseless | |
2773 * people can highlight stuff. | |
2774 */ | |
2775 /* gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(imhtml), editable); */ | |
8061 | 2776 imhtml->editable = editable; |
8677 | 2777 imhtml->format_functions = GTK_IMHTML_ALL; |
2778 | |
2779 if (editable) | |
2780 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", | |
2781 G_CALLBACK(mark_set_cb), imhtml); | |
2782 } | |
2783 | |
2784 void gtk_imhtml_set_whole_buffer_formatting_only(GtkIMHtml *imhtml, gboolean wbfo) | |
2785 { | |
2786 g_return_if_fail(imhtml != NULL); | |
2787 | |
2788 imhtml->wbfo = wbfo; | |
8420 | 2789 } |
2790 | |
2791 void gtk_imhtml_set_format_functions(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons) | |
2792 { | |
2793 GObject *object = g_object_ref(G_OBJECT(imhtml)); | |
8677 | 2794 imhtml->format_functions = buttons; |
8420 | 2795 g_signal_emit(object, signals[BUTTONS_UPDATE], 0, buttons); |
2796 g_object_unref(object); | |
8061 | 2797 } |
2798 | |
8516 | 2799 |
2800 void gtk_imhtml_get_current_format(GtkIMHtml *imhtml, gboolean *bold, | |
2801 gboolean *italic, gboolean *underline) | |
8481 | 2802 { |
8677 | 2803 if (imhtml->edit.bold) |
2804 (*bold) = TRUE; | |
2805 if (imhtml->edit.italic) | |
2806 (*italic) = TRUE; | |
2807 if (imhtml->edit.underline) | |
2808 (*underline) = TRUE; | |
8481 | 2809 } |
2810 | |
8061 | 2811 gboolean gtk_imhtml_get_editable(GtkIMHtml *imhtml) |
2812 { | |
2813 return imhtml->editable; | |
2814 } | |
2815 | |
8677 | 2816 /* |
2817 * I had this crazy idea about changing the text cursor color to reflex the foreground color | |
2818 * of the text about to be entered. This is the place you'd do it, along with the place where | |
2819 * we actually set a new foreground color. | |
2820 * I may not do this, because people will bitch about Gaim overriding their gtk theme's cursor | |
2821 * colors. | |
2822 * | |
2823 * Just in case I do do this, I asked about what to set the secondary text cursor to. | |
2824 * | |
2825 (12:45:27) ?? ???: secondary_cursor_color = (rgb(background) + rgb(primary_cursor_color) ) / 2 * | |
2826 (12:45:55) ?? ???: understand? * | |
2827 * (12:46:14) Tim: yeah. i didn't know there was an exact formula | |
2828 (12:46:56) ?? ???: u might need to exactract separate each color from RGB * | |
2829 */ | |
2830 | |
2831 static void mark_set_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, | |
2832 GtkIMHtml *imhtml) | |
2833 { | |
2834 GSList *tags, *l; | |
2835 GtkTextIter iter; | |
2836 | |
2837 if (mark != gtk_text_buffer_get_insert(buffer)) | |
2838 return; | |
2839 | |
2840 if (!gtk_text_buffer_get_char_count(buffer)) | |
2841 return; | |
2842 | |
2843 imhtml->edit.bold = imhtml->edit.italic = imhtml->edit.underline = FALSE; | |
2844 if (imhtml->edit.forecolor) | |
2845 g_free(imhtml->edit.forecolor); | |
2846 imhtml->edit.forecolor = NULL; | |
2847 if (imhtml->edit.backcolor) | |
2848 g_free(imhtml->edit.backcolor); | |
2849 imhtml->edit.backcolor = NULL; | |
2850 if (imhtml->edit.fontface) | |
2851 g_free(imhtml->edit.fontface); | |
2852 imhtml->edit.fontface = NULL; | |
2853 imhtml->edit.fontsize = 0; | |
2854 imhtml->edit.link = NULL; | |
2855 | |
2856 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
2857 | |
2858 | |
2859 if (gtk_text_iter_is_end(&iter)) | |
2860 tags = gtk_text_iter_get_toggled_tags(&iter, FALSE); | |
2861 else | |
2862 tags = gtk_text_iter_get_tags(&iter); | |
2863 | |
2864 for (l = tags; l != NULL; l = l->next) { | |
2865 GtkTextTag *tag = GTK_TEXT_TAG(l->data); | |
2866 | |
2867 if (tag->name) { | |
2868 if (strcmp(tag->name, "BOLD") == 0) | |
2869 imhtml->edit.bold = TRUE; | |
2870 if (strcmp(tag->name, "ITALICS") == 0) | |
2871 imhtml->edit.italic = TRUE; | |
2872 if (strcmp(tag->name, "UNDERLINE") == 0) | |
2873 imhtml->edit.underline = TRUE; | |
2874 if (strncmp(tag->name, "FORECOLOR ", 10) == 0) | |
2875 imhtml->edit.forecolor = g_strdup(&(tag->name)[10]); | |
2876 if (strncmp(tag->name, "BACKCOLOR ", 10) == 0) | |
2877 imhtml->edit.backcolor = g_strdup(&(tag->name)[10]); | |
2878 if (strncmp(tag->name, "FONT FACE ", 10) == 0) | |
2879 imhtml->edit.fontface = g_strdup(&(tag->name)[10]); | |
2880 if (strncmp(tag->name, "FONT SIZE ", 10) == 0) | |
2881 imhtml->edit.fontsize = strtol(&(tag->name)[10], NULL, 10); | |
2882 if (strncmp(tag->name, "LINK ", 5) == 0) | |
2883 imhtml->edit.link = tag; | |
2884 } | |
2885 } | |
2886 | |
2887 g_slist_free(tags); | |
2888 } | |
2889 | |
8061 | 2890 gboolean gtk_imhtml_toggle_bold(GtkIMHtml *imhtml) |
2891 { | |
8481 | 2892 GObject *object; |
8677 | 2893 GtkTextIter start, end; |
2894 | |
2895 imhtml->edit.bold = !imhtml->edit.bold; | |
2896 | |
2897 if (imhtml->wbfo) { | |
2898 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
2899 if (imhtml->edit.bold) | |
2900 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
2901 else | |
2902 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
2903 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
2904 if (imhtml->edit.bold) | |
2905 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
2906 else | |
2907 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
2908 | |
8061 | 2909 } |
8481 | 2910 object = g_object_ref(G_OBJECT(imhtml)); |
2911 g_signal_emit(object, signals[TOGGLE_FORMAT], 0, GTK_IMHTML_BOLD); | |
2912 g_object_unref(object); | |
2913 | |
8677 | 2914 return (imhtml->edit.bold != FALSE); |
8061 | 2915 } |
2916 | |
2917 gboolean gtk_imhtml_toggle_italic(GtkIMHtml *imhtml) | |
2918 { | |
8481 | 2919 GObject *object; |
8677 | 2920 GtkTextIter start, end; |
2921 | |
2922 imhtml->edit.italic = !imhtml->edit.italic; | |
2923 | |
2924 if (imhtml->wbfo) { | |
2925 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
2926 if (imhtml->edit.italic) | |
2927 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
2928 else | |
2929 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
2930 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
2931 if (imhtml->edit.italic) | |
2932 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
2933 else | |
2934 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
8061 | 2935 } |
8481 | 2936 object = g_object_ref(G_OBJECT(imhtml)); |
2937 g_signal_emit(object, signals[TOGGLE_FORMAT], 0, GTK_IMHTML_ITALIC); | |
2938 g_object_unref(object); | |
2939 | |
8677 | 2940 return imhtml->edit.italic != FALSE; |
8061 | 2941 } |
2942 | |
2943 gboolean gtk_imhtml_toggle_underline(GtkIMHtml *imhtml) | |
2944 { | |
8481 | 2945 GObject *object; |
8677 | 2946 GtkTextIter start, end; |
2947 | |
2948 imhtml->edit.underline = !imhtml->edit.underline; | |
2949 | |
2950 if (imhtml->wbfo) { | |
2951 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
2952 if (imhtml->edit.underline) | |
2953 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
2954 else | |
2955 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
2956 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
2957 if (imhtml->edit.underline) | |
2958 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
2959 else | |
2960 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
8061 | 2961 } |
8481 | 2962 object = g_object_ref(G_OBJECT(imhtml)); |
2963 g_signal_emit(object, signals[TOGGLE_FORMAT], 0, GTK_IMHTML_UNDERLINE); | |
2964 g_object_unref(object); | |
2965 | |
8677 | 2966 return imhtml->edit.underline != FALSE; |
8061 | 2967 } |
2968 | |
2969 void gtk_imhtml_font_set_size(GtkIMHtml *imhtml, gint size) | |
2970 { | |
8677 | 2971 GtkTextIter start, end; |
8061 | 2972 |
2973 imhtml->edit.fontsize = size; | |
2974 | |
8677 | 2975 |
2976 if (imhtml->wbfo) { | |
2977 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
2978 remove_font_size(imhtml, &start, &end, TRUE); | |
2979 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2980 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
2981 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
2982 remove_font_size(imhtml, &start, &end, FALSE); | |
2983 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
2984 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 2985 } |
8677 | 2986 |
8061 | 2987 } |
2988 | |
2989 void gtk_imhtml_font_shrink(GtkIMHtml *imhtml) | |
2990 { | |
8677 | 2991 GtkTextIter start, end; |
2992 | |
8061 | 2993 if (imhtml->edit.fontsize == 1) |
2994 return; | |
2995 | |
8677 | 2996 if (!imhtml->edit.fontsize) |
2997 imhtml->edit.fontsize = 2; | |
2998 else | |
2999 imhtml->edit.fontsize--; | |
3000 | |
3001 if (imhtml->wbfo) { | |
3002 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3003 remove_font_size(imhtml, &start, &end, TRUE); | |
3004 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3005 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
3006 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3007 remove_font_size(imhtml, &start, &end, FALSE); | |
3008 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3009 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 3010 } |
3011 } | |
3012 | |
3013 void gtk_imhtml_font_grow(GtkIMHtml *imhtml) | |
3014 { | |
8677 | 3015 GtkTextIter start, end; |
3016 | |
8061 | 3017 if (imhtml->edit.fontsize == MAX_FONT_SIZE) |
3018 return; | |
3019 | |
8677 | 3020 if (!imhtml->edit.fontsize) |
3021 imhtml->edit.fontsize = 4; | |
3022 else | |
3023 imhtml->edit.fontsize++; | |
3024 | |
3025 if (imhtml->wbfo) { | |
3026 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3027 remove_font_size(imhtml, &start, &end, TRUE); | |
3028 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3029 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
3030 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3031 remove_font_size(imhtml, &start, &end, FALSE); | |
3032 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3033 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 3034 } |
3035 } | |
3036 | |
3037 gboolean gtk_imhtml_toggle_forecolor(GtkIMHtml *imhtml, const char *color) | |
3038 { | |
8677 | 3039 GtkTextIter start, end; |
3040 | |
3041 if (imhtml->edit.forecolor != NULL) | |
3042 g_free(imhtml->edit.forecolor); | |
3043 | |
8429 | 3044 if (color) { |
8677 | 3045 imhtml->edit.forecolor = g_strdup(color); |
3046 if (imhtml->wbfo) { | |
3047 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3048 remove_font_forecolor(imhtml, &start, &end, TRUE); | |
3049 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3050 find_font_forecolor_tag(imhtml, imhtml->edit.forecolor), &start, &end); | |
3051 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3052 remove_font_forecolor(imhtml, &start, &end, FALSE); | |
3053 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3054 find_font_forecolor_tag(imhtml, imhtml->edit.forecolor), | |
3055 &start, &end); | |
3056 } | |
8061 | 3057 } else { |
3058 imhtml->edit.forecolor = NULL; | |
3059 } | |
3060 | |
3061 return imhtml->edit.forecolor != NULL; | |
3062 } | |
3063 | |
3064 gboolean gtk_imhtml_toggle_backcolor(GtkIMHtml *imhtml, const char *color) | |
3065 { | |
8677 | 3066 GtkTextIter start, end; |
3067 | |
3068 if (imhtml->edit.backcolor != NULL) | |
3069 g_free(imhtml->edit.backcolor); | |
3070 | |
8429 | 3071 if (color) { |
8677 | 3072 imhtml->edit.backcolor = g_strdup(color); |
3073 | |
3074 if (imhtml->wbfo) { | |
3075 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3076 remove_font_backcolor(imhtml, &start, &end, TRUE); | |
3077 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3078 find_font_backcolor_tag(imhtml, imhtml->edit.backcolor), &start, &end); | |
3079 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3080 remove_font_backcolor(imhtml, &start, &end, FALSE); | |
3081 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3082 find_font_backcolor_tag(imhtml, | |
3083 imhtml->edit.backcolor), &start, &end); | |
3084 } | |
8061 | 3085 } else { |
3086 imhtml->edit.backcolor = NULL; | |
3087 } | |
8677 | 3088 |
8061 | 3089 return imhtml->edit.backcolor != NULL; |
3090 } | |
3091 | |
3092 gboolean gtk_imhtml_toggle_fontface(GtkIMHtml *imhtml, const char *face) | |
3093 { | |
8677 | 3094 GtkTextIter start, end; |
3095 | |
3096 if (imhtml->edit.fontface != NULL) | |
3097 g_free(imhtml->edit.fontface); | |
3098 | |
8429 | 3099 if (face) { |
8677 | 3100 imhtml->edit.fontface = g_strdup(face); |
3101 | |
3102 if (imhtml->wbfo) { | |
3103 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3104 remove_font_face(imhtml, &start, &end, TRUE); | |
3105 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3106 find_font_face_tag(imhtml, imhtml->edit.fontface), &start, &end); | |
3107 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3108 remove_font_face(imhtml, &start, &end, FALSE); | |
3109 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3110 find_font_face_tag(imhtml, imhtml->edit.fontface), | |
3111 &start, &end); | |
3112 } | |
8061 | 3113 } else { |
3114 imhtml->edit.fontface = NULL; | |
3115 } | |
8677 | 3116 |
8061 | 3117 return imhtml->edit.fontface != NULL; |
3118 } | |
3119 | |
8677 | 3120 void gtk_imhtml_toggle_link(GtkIMHtml *imhtml, const char *url) |
8061 | 3121 { |
8677 | 3122 GtkTextIter start, end; |
3123 GtkTextTag *linktag; | |
3124 static guint linkno = 0; | |
3125 gchar str[48]; | |
3126 | |
3127 imhtml->edit.link = NULL; | |
3128 | |
3129 | |
3130 | |
3131 if (url) { | |
3132 g_snprintf(str, sizeof(str), "LINK %d", linkno++); | |
3133 str[47] = '\0'; | |
3134 | |
3135 imhtml->edit.link = linktag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); | |
3136 g_object_set_data_full(G_OBJECT(linktag), "link_url", g_strdup(url), g_free); | |
3137 g_signal_connect(G_OBJECT(linktag), "event", G_CALLBACK(tag_event), NULL); | |
3138 | |
3139 if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3140 remove_font_link(imhtml, &start, &end, FALSE); | |
3141 gtk_text_buffer_apply_tag(imhtml->text_buffer, linktag, &start, &end); | |
3142 } | |
3143 } | |
3144 } | |
3145 | |
3146 void gtk_imhtml_insert_link(GtkIMHtml *imhtml, GtkTextMark *mark, const char *url, const char *text) | |
3147 { | |
8061 | 3148 GtkTextIter iter; |
8677 | 3149 |
3150 gtk_imhtml_toggle_link(imhtml, url); | |
8061 | 3151 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); |
8677 | 3152 gtk_text_buffer_insert(imhtml->text_buffer, &iter, text, -1); |
3153 gtk_imhtml_toggle_link(imhtml, NULL); | |
8061 | 3154 } |
3155 | |
3156 void gtk_imhtml_insert_smiley(GtkIMHtml *imhtml, const char *sml, char *smiley) | |
3157 { | |
8677 | 3158 GtkTextMark *mark; |
8061 | 3159 GtkTextIter iter; |
8677 | 3160 |
3161 mark = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
3162 | |
3163 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
3164 gtk_imhtml_insert_smiley_at_iter(imhtml, sml, smiley, &iter); | |
3165 } | |
3166 | |
3167 void gtk_imhtml_insert_smiley_at_iter(GtkIMHtml *imhtml, const char *sml, char *smiley, GtkTextIter *iter) | |
3168 { | |
8061 | 3169 GdkPixbuf *pixbuf = NULL; |
3170 GdkPixbufAnimation *annipixbuf = NULL; | |
3171 GtkWidget *icon = NULL; | |
3172 GtkTextChildAnchor *anchor; | |
8505 | 3173 char *unescaped = gaim_unescape_html(smiley); |
8061 | 3174 |
8515 | 3175 if (!imhtml->show_smileys) { |
8677 | 3176 gtk_text_buffer_insert(imhtml->text_buffer, iter, smiley, strlen(smiley)); |
8515 | 3177 return; |
3178 } | |
3179 | |
8677 | 3180 anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); |
8505 | 3181 g_object_set_data(G_OBJECT(anchor), "text_tag", unescaped); |
3182 | |
3183 annipixbuf = gtk_smiley_tree_image(imhtml, sml, unescaped); | |
8061 | 3184 if(annipixbuf) { |
3185 if(gdk_pixbuf_animation_is_static_image(annipixbuf)) { | |
3186 pixbuf = gdk_pixbuf_animation_get_static_image(annipixbuf); | |
3187 if(pixbuf) | |
3188 icon = gtk_image_new_from_pixbuf(pixbuf); | |
3189 } else { | |
3190 icon = gtk_image_new_from_animation(annipixbuf); | |
3191 } | |
3192 } | |
3193 | |
3194 if (icon) { | |
3195 gtk_widget_show(icon); | |
3196 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), icon, anchor); | |
3197 } | |
3198 } | |
3199 | |
8677 | 3200 static const gchar *tag_to_html_start(GtkTextTag *tag) |
8061 | 3201 { |
8677 | 3202 const gchar *name; |
3203 static gchar buf[1024]; | |
3204 | |
3205 name = tag->name; | |
3206 g_return_val_if_fail(name != NULL, ""); | |
3207 | |
3208 if (strcmp(name, "BOLD") == 0) { | |
3209 return "<b>"; | |
3210 } else if (strcmp(name, "ITALICS") == 0) { | |
3211 return "<i>"; | |
3212 } else if (strcmp(name, "UNDERLINE") == 0) { | |
3213 return "<u>"; | |
3214 } else if (strncmp(name, "LINK ", 5) == 0) { | |
3215 char *tmp = g_object_get_data(G_OBJECT(tag), "link_url"); | |
3216 if (tmp) { | |
3217 g_snprintf(buf, sizeof(buf), "<a href=\"%s\">", tmp); | |
3218 buf[sizeof(buf)-1] = '\0'; | |
3219 return buf; | |
3220 } else { | |
3221 return ""; | |
3222 } | |
3223 } else if (strncmp(name, "FORECOLOR ", 10) == 0) { | |
3224 g_snprintf(buf, sizeof(buf), "<font color=\"%s\">", &name[10]); | |
3225 return buf; | |
3226 } else if (strncmp(name, "BACKCOLOR ", 10) == 0) { | |
3227 g_snprintf(buf, sizeof(buf), "<font back=\"%s\">", &name[10]); | |
3228 return buf; | |
3229 } else if (strncmp(name, "FONT FACE ", 10) == 0) { | |
3230 g_snprintf(buf, sizeof(buf), "<font face=\"%s\">", &name[10]); | |
3231 return buf; | |
3232 } else if (strncmp(name, "FONT SIZE ", 10) == 0) { | |
3233 g_snprintf(buf, sizeof(buf), "<font size=\"%s\">", &name[10]); | |
3234 return buf; | |
3235 } else { | |
3236 return ""; | |
3237 } | |
8061 | 3238 } |
3239 | |
8677 | 3240 static const gchar *tag_to_html_end(GtkTextTag *tag) |
8061 | 3241 { |
8677 | 3242 const gchar *name; |
3243 | |
3244 name = tag->name; | |
3245 g_return_val_if_fail(name != NULL, ""); | |
3246 | |
3247 if (strcmp(name, "BOLD") == 0) { | |
3248 return "</b>"; | |
3249 } else if (strcmp(name, "ITALICS") == 0) { | |
3250 return "</i>"; | |
3251 } else if (strcmp(name, "UNDERLINE") == 0) { | |
3252 return "</u>"; | |
3253 } else if (strncmp(name, "LINK ", 5) == 0) { | |
3254 return "</a>"; | |
3255 } else if (strncmp(name, "FORECOLOR ", 10) == 0) { | |
3256 return "</font>"; | |
3257 } else if (strncmp(name, "BACKCOLOR ", 10) == 0) { | |
3258 return "</font>"; | |
3259 } else if (strncmp(name, "FONT FACE ", 10) == 0) { | |
3260 return "</font>"; | |
3261 } else if (strncmp(name, "FONT SIZE ", 10) == 0) { | |
3262 return "</font>"; | |
3263 } else { | |
3264 return ""; | |
3265 } | |
3266 } | |
3267 | |
3268 static gboolean tag_ends_here(GtkTextTag *tag, GtkTextIter *iter, GtkTextIter *niter) | |
3269 { | |
3270 return ((gtk_text_iter_has_tag(iter, GTK_TEXT_TAG(tag)) && | |
3271 !gtk_text_iter_has_tag(niter, GTK_TEXT_TAG(tag))) || | |
3272 gtk_text_iter_is_end(niter)); | |
8061 | 3273 } |
3274 | |
3275 /* Basic notion here: traverse through the text buffer one-by-one, non-character elements, such | |
3276 * as smileys and IM images are represented by the Unicode "unknown" character. Handle them. Else | |
8677 | 3277 * check for tags that are toggled on, insert their html form, and push them on the queue. Then insert |
3278 * the actual text. Then check for tags that are toggled off and insert them, after checking the queue. | |
3279 * Finally, replace <, >, &, and " with their HTML equivilent. | |
3280 */ | |
8061 | 3281 char *gtk_imhtml_get_markup_range(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end) |
3282 { | |
3283 gunichar c; | |
8677 | 3284 GtkTextIter iter, nextiter; |
8061 | 3285 GString *str = g_string_new(""); |
8677 | 3286 GSList *tags, *sl; |
3287 GQueue *q, *r; | |
3288 GtkTextTag *tag; | |
3289 | |
3290 q = g_queue_new(); | |
3291 r = g_queue_new(); | |
3292 | |
8061 | 3293 |
3294 gtk_text_iter_order(start, end); | |
8677 | 3295 nextiter = iter = *start; |
3296 gtk_text_iter_forward_char(&nextiter); | |
3297 | |
3298 /* First add the tags that are already in progress */ | |
3299 tags = gtk_text_iter_get_tags(start); | |
3300 | |
3301 for (sl = tags; sl; sl = sl->next) { | |
3302 tag = sl->data; | |
3303 if (!gtk_text_iter_toggles_tag(start, GTK_TEXT_TAG(tag))) { | |
3304 g_string_append(str, tag_to_html_start(GTK_TEXT_TAG(tag))); | |
3305 g_queue_push_tail(q, tag); | |
8061 | 3306 } |
3307 } | |
8677 | 3308 g_slist_free(tags); |
8061 | 3309 |
3310 while ((c = gtk_text_iter_get_char(&iter)) != 0 && !gtk_text_iter_equal(&iter, end)) { | |
8677 | 3311 |
3312 tags = gtk_text_iter_get_tags(&iter); | |
3313 | |
3314 for (sl = tags; sl; sl = sl->next) { | |
3315 tag = sl->data; | |
3316 if (gtk_text_iter_begins_tag(&iter, GTK_TEXT_TAG(tag))) { | |
3317 g_string_append(str, tag_to_html_start(GTK_TEXT_TAG(tag))); | |
3318 g_queue_push_tail(q, tag); | |
3319 } | |
3320 } | |
3321 | |
3322 | |
8061 | 3323 if (c == 0xFFFC) { |
3324 GtkTextChildAnchor* anchor = gtk_text_iter_get_child_anchor(&iter); | |
3325 char *text = g_object_get_data(G_OBJECT(anchor), "text_tag"); | |
3326 str = g_string_append(str, text); | |
8677 | 3327 } else if (c == '<') { |
3328 str = g_string_append(str, "<"); | |
3329 } else if (c == '>') { | |
3330 str = g_string_append(str, ">"); | |
3331 } else if (c == '&') { | |
3332 str = g_string_append(str, "&"); | |
3333 } else if (c == '"') { | |
3334 str = g_string_append(str, """); | |
3335 } else if (c == '\n') { | |
3336 str = g_string_append(str, "<br>"); | |
8061 | 3337 } else { |
8677 | 3338 str = g_string_append_unichar(str, c); |
3339 } | |
3340 | |
3341 tags = g_slist_reverse(tags); | |
3342 for (sl = tags; sl; sl = sl->next) { | |
3343 tag = sl->data; | |
3344 if (tag_ends_here(tag, &iter, &nextiter)) { | |
3345 | |
3346 GtkTextTag *tmp; | |
3347 | |
3348 while ((tmp = g_queue_pop_tail(q)) != tag) { | |
3349 if (tmp == NULL) | |
3350 break; | |
3351 | |
3352 if (!tag_ends_here(tmp, &iter, &nextiter)) | |
3353 g_queue_push_tail(r, tmp); | |
3354 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tmp))); | |
3355 } | |
3356 | |
3357 if (tmp == NULL) | |
3358 gaim_debug_warning("gtkimhtml", "empty queue, more closing tags than open tags!\n"); | |
3359 else | |
3360 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tag))); | |
3361 | |
3362 while ((tmp = g_queue_pop_head(r))) { | |
3363 g_string_append(str, tag_to_html_start(GTK_TEXT_TAG(tmp))); | |
3364 g_queue_push_tail(q, tmp); | |
8061 | 3365 } |
3366 } | |
3367 } | |
8677 | 3368 |
3369 g_slist_free(tags); | |
8061 | 3370 gtk_text_iter_forward_char(&iter); |
8677 | 3371 gtk_text_iter_forward_char(&nextiter); |
8061 | 3372 } |
8677 | 3373 |
3374 while ((tag = g_queue_pop_tail(q))) | |
3375 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tag))); | |
3376 | |
3377 g_queue_free(q); | |
3378 g_queue_free(r); | |
8061 | 3379 return g_string_free(str, FALSE); |
3380 } | |
3381 | |
3382 void gtk_imhtml_close_tags(GtkIMHtml *imhtml) | |
3383 { | |
8677 | 3384 GtkTextIter iter; |
8061 | 3385 |
3386 if (imhtml->edit.bold) | |
3387 gtk_imhtml_toggle_bold(imhtml); | |
3388 | |
3389 if (imhtml->edit.italic) | |
3390 gtk_imhtml_toggle_italic(imhtml); | |
3391 | |
3392 if (imhtml->edit.underline) | |
3393 gtk_imhtml_toggle_underline(imhtml); | |
3394 | |
3395 if (imhtml->edit.forecolor) | |
3396 gtk_imhtml_toggle_forecolor(imhtml, NULL); | |
3397 | |
3398 if (imhtml->edit.backcolor) | |
3399 gtk_imhtml_toggle_backcolor(imhtml, NULL); | |
3400 | |
3401 if (imhtml->edit.fontface) | |
3402 gtk_imhtml_toggle_fontface(imhtml, NULL); | |
3403 | |
8677 | 3404 imhtml->edit.fontsize = 0; |
3405 | |
3406 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); | |
3407 gtk_text_buffer_remove_all_tags(imhtml->text_buffer, &iter, &iter); | |
8061 | 3408 |
3409 } | |
3410 | |
3411 char *gtk_imhtml_get_markup(GtkIMHtml *imhtml) | |
3412 { | |
3413 GtkTextIter start, end; | |
3414 | |
3415 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
3416 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
3417 return gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
3418 } | |
3419 | |
8677 | 3420 char **gtk_imhtml_get_markup_lines(GtkIMHtml *imhtml) |
3421 { | |
3422 int i, j, lines; | |
3423 GtkTextIter start, end; | |
3424 char **ret; | |
3425 | |
3426 lines = gtk_text_buffer_get_line_count(imhtml->text_buffer); | |
3427 ret = g_new0(char *, lines + 1); | |
3428 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
3429 end = start; | |
3430 gtk_text_iter_forward_to_line_end(&end); | |
3431 | |
3432 for (i = 0, j = 0; i < lines; i++) { | |
3433 ret[j] = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
3434 if (ret[j] != NULL) | |
3435 j++; | |
3436 gtk_text_iter_forward_line(&start); | |
3437 end = start; | |
3438 gtk_text_iter_forward_to_line_end(&end); | |
3439 } | |
3440 | |
3441 return ret; | |
3442 } | |
3443 | |
3444 char *gtk_imhtml_get_text(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *stop) | |
8061 | 3445 { |
8519 | 3446 GString *str = g_string_new(""); |
3447 GtkTextIter iter, end; | |
3448 gunichar c; | |
3449 | |
8677 | 3450 if (start == NULL) |
3451 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &iter); | |
3452 else | |
3453 iter = *start; | |
3454 | |
3455 if (stop == NULL) | |
3456 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
3457 else | |
3458 end = *stop; | |
3459 | |
3460 gtk_text_iter_order(&iter, &end); | |
8519 | 3461 |
3462 while ((c = gtk_text_iter_get_char(&iter)) != 0 && !gtk_text_iter_equal(&iter, &end)) { | |
3463 if (c == 0xFFFC) { | |
8677 | 3464 GtkTextChildAnchor* anchor; |
3465 char *text = NULL; | |
3466 | |
3467 anchor = gtk_text_iter_get_child_anchor(&iter); | |
3468 if (anchor) | |
3469 text = g_object_get_data(G_OBJECT(anchor), "text_tag"); | |
3470 if (text) | |
3471 str = g_string_append(str, text); | |
8519 | 3472 } else { |
3473 g_string_append_unichar(str, c); | |
3474 } | |
3475 gtk_text_iter_forward_char(&iter); | |
3476 } | |
3477 | |
3478 return g_string_free(str, FALSE); | |
8061 | 3479 } |