Mercurial > pidgin.yaz
annotate src/gtkimhtml.c @ 10852:62b5117114d2
[gaim-migrate @ 12526]
do we need this any more? it is annoyying as all get out.
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Tue, 19 Apr 2005 16:00:20 +0000 |
parents | c203cd637f95 |
children | 06e71aa662af |
rev | line source |
---|---|
1428 | 1 /* |
10297
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10243
diff
changeset
|
2 * @file gtkimhtml.c GTK+ IMHtml |
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10243
diff
changeset
|
3 * @ingroup gtkui |
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10243
diff
changeset
|
4 * |
ec140184437b
[gaim-migrate @ 11480]
Luke Schierer <lschiere@pidgin.im>
parents:
10243
diff
changeset
|
5 * gaim |
1428 | 6 * |
8046 | 7 * Gaim is the legal property of its developers, whose names are too numerous |
8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
9 * source distribution. | |
1428 | 10 * |
11 * This program is free software; you can redistribute it and/or modify | |
12 * under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 * | |
25 */ | |
26 | |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
27 #ifdef HAVE_CONFIG_H |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
28 #include <config.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
29 #endif |
8526 | 30 #include "debug.h" |
8091 | 31 #include "util.h" |
1428 | 32 #include "gtkimhtml.h" |
7358 | 33 #include "gtksourceiter.h" |
1428 | 34 #include <gtk/gtk.h> |
4895 | 35 #include <glib/gerror.h> |
4046 | 36 #include <gdk/gdkkeysyms.h> |
1428 | 37 #include <string.h> |
38 #include <ctype.h> | |
39 #include <stdio.h> | |
4629 | 40 #include <stdlib.h> |
1428 | 41 #include <math.h> |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
42 #ifdef HAVE_LANGINFO_CODESET |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
43 #include <langinfo.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
44 #include <locale.h> |
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2365
diff
changeset
|
45 #endif |
8692 | 46 #ifdef _WIN32 |
47 #include <windows.h> | |
48 #endif | |
1428 | 49 |
4417 | 50 #ifdef ENABLE_NLS |
51 # include <libintl.h> | |
52 # define _(x) gettext(x) | |
53 # ifdef gettext_noop | |
54 # define N_(String) gettext_noop (String) | |
55 # else | |
56 # define N_(String) (String) | |
57 # endif | |
58 #else | |
59 # define N_(String) (String) | |
60 # define _(x) (x) | |
61 #endif | |
62 | |
4735 | 63 #include <pango/pango-font.h> |
64 | |
10062 | 65 /* GTK+ < 2.4.x hack, see gtkgaim.h for details. */ |
66 #if (!GTK_CHECK_VERSION(2,4,0)) | |
5105 | 67 #define GTK_WRAP_WORD_CHAR GTK_WRAP_WORD |
68 #endif | |
69 | |
4735 | 70 #define TOOLTIP_TIMEOUT 500 |
71 | |
8786 | 72 /* GTK+ 2.0 hack */ |
73 #if (!GTK_CHECK_VERSION(2,2,0)) | |
74 #define gtk_widget_get_clipboard(x, y) gtk_clipboard_get(y) | |
75 #endif | |
76 | |
10100 | 77 static GtkTextViewClass *parent_class = NULL; |
78 | |
9300 | 79 static gboolean |
80 gtk_text_view_drag_motion (GtkWidget *widget, | |
81 GdkDragContext *context, | |
82 gint x, | |
83 gint y, | |
84 guint time); | |
85 | |
8677 | 86 static void preinsert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml); |
8061 | 87 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml); |
10169 | 88 static void insert_ca_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextChildAnchor *arg2, gpointer user_data); |
89 static void gtk_imhtml_apply_tags_on_insert(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end); | |
8505 | 90 static gboolean gtk_imhtml_is_amp_escape (const gchar *string, gchar **replace, gint *length); |
8698 | 91 void gtk_imhtml_close_tags(GtkIMHtml *imhtml, GtkTextIter *iter); |
9300 | 92 static void gtk_imhtml_link_drop_cb(GtkWidget *widget, GdkDragContext *context, gint x, gint y, guint time, gpointer user_data); |
8091 | 93 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 | 94 static void mark_set_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, GtkIMHtml *imhtml); |
8931 | 95 static void hijack_menu_cb(GtkIMHtml *imhtml, GtkMenu *menu, gpointer data); |
96 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data); | |
97 static void paste_plaintext_received_cb (GtkClipboard *clipboard, const gchar *text, gpointer data); | |
8061 | 98 |
3922 | 99 /* POINT_SIZE converts from AIM font sizes to point sizes. It probably should be redone in such a |
100 * way that it base the sizes off the default font size rather than using arbitrary font sizes. */ | |
101 #define MAX_FONT_SIZE 7 | |
5367 | 102 #define POINT_SIZE(x) (options & GTK_IMHTML_USE_POINTSIZE ? x : _point_sizes [MIN ((x), MAX_FONT_SIZE) - 1]) |
8380 | 103 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
|
104 |
10184 | 105 enum { |
8677 | 106 TARGET_HTML, |
8061 | 107 TARGET_UTF8_STRING, |
108 TARGET_COMPOUND_TEXT, | |
109 TARGET_STRING, | |
110 TARGET_TEXT | |
111 }; | |
112 | |
8091 | 113 enum { |
8420 | 114 URL_CLICKED, |
115 BUTTONS_UPDATE, | |
116 TOGGLE_FORMAT, | |
8427 | 117 CLEAR_FORMAT, |
8506 | 118 UPDATE_FORMAT, |
10108 | 119 MESSAGE_SEND, |
8420 | 120 LAST_SIGNAL |
121 }; | |
122 static guint signals [LAST_SIGNAL] = { 0 }; | |
123 | |
8061 | 124 GtkTargetEntry selection_targets[] = { |
8566 | 125 { "text/html", 0, TARGET_HTML }, |
8061 | 126 { "UTF8_STRING", 0, TARGET_UTF8_STRING }, |
127 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT }, | |
128 { "STRING", 0, TARGET_STRING }, | |
129 { "TEXT", 0, TARGET_TEXT}}; | |
130 | |
8091 | 131 GtkTargetEntry link_drag_drop_targets[] = { |
10145 | 132 GTK_IMHTML_DND_TARGETS |
133 }; | |
8091 | 134 |
8692 | 135 #ifdef _WIN32 |
136 /* Win32 clipboard format value, and functions to convert back and | |
137 * forth between HTML and the clipboard format. | |
138 */ | |
139 static UINT win_html_fmt; | |
140 | |
141 static gchar * | |
142 clipboard_win32_to_html(char *clipboard) { | |
9465 | 143 const char *header; |
8693 | 144 const char *begin, *end; |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
145 gint start = 0; |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
146 gint finish = 0; |
8692 | 147 gchar *html; |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
148 gchar **split; |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
149 int clipboard_length = 0; |
9465 | 150 |
151 #if 0 /* Debugging for Windows clipboard */ | |
9467 | 152 FILE *fd; |
153 | |
9465 | 154 gaim_debug_info("imhtml clipboard", "from clipboard: %s\n", clipboard); |
155 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10574
diff
changeset
|
156 fd = g_fopen("e:\\gaimcb.txt", "wb"); |
9465 | 157 fprintf(fd, "%s", clipboard); |
158 fclose(fd); | |
159 #endif | |
160 | |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
161 clipboard_length = strlen(clipboard); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
162 |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
163 if (!(header = strstr(clipboard, "StartFragment:")) || (header - clipboard) >= clipboard_length) |
9465 | 164 return NULL; |
165 sscanf(header, "StartFragment:%d", &start); | |
166 | |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
167 if (!(header = strstr(clipboard, "EndFragment:")) || (header - clipboard) >= clipboard_length) |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
168 return NULL; |
9465 | 169 sscanf(header, "EndFragment:%d", &finish); |
170 | |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
171 if (finish > clipboard_length) |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
172 finish = clipboard_length; |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
173 |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
174 if (start > finish) |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
175 start = finish; |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
176 |
9465 | 177 begin = clipboard + start; |
178 | |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
179 end = clipboard + finish; |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
180 |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
181 html = g_strndup(begin, end - begin); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
182 |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
183 /* any newlines in the string will now be \r\n, so we need to strip out the \r */ |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
184 split = g_strsplit(html, "\r\n", 0); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
185 g_free(html); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
186 html = g_strjoinv("\n", split); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
187 g_strfreev(split); |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
188 |
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
189 html = g_strstrip(html); |
9465 | 190 |
191 #if 0 /* Debugging for Windows clipboard */ | |
10016
5b4a0af99bf7
[gaim-migrate @ 10935]
Luke Schierer <lschiere@pidgin.im>
parents:
10013
diff
changeset
|
192 gaim_debug_info("imhtml clipboard", "HTML fragment: '%s'\n", html); |
9465 | 193 #endif |
194 | |
8707 | 195 return html; |
8692 | 196 } |
197 | |
198 static gchar * | |
199 clipboard_html_to_win32(char *html) { | |
8693 | 200 int length; |
8692 | 201 gchar *ret; |
202 GString *clipboard; | |
203 | |
8693 | 204 if (html == NULL) |
205 return NULL; | |
8692 | 206 |
207 length = strlen(html); | |
9465 | 208 clipboard = g_string_new ("Version:1.0\r\n"); |
8692 | 209 g_string_append(clipboard, "StartHTML:0000000105\r\n"); |
9465 | 210 g_string_append(clipboard, g_strdup_printf("EndHTML:%010d\r\n", 147 + length)); |
211 g_string_append(clipboard, "StartFragment:0000000127\r\n"); | |
212 g_string_append(clipboard, g_strdup_printf("EndFragment:%010d\r\n", 127 + length)); | |
213 g_string_append(clipboard, "<!--StartFragment-->\r\n"); | |
8692 | 214 g_string_append(clipboard, html); |
9465 | 215 g_string_append(clipboard, "\r\n<!--EndFragment-->"); |
8692 | 216 ret = clipboard->str; |
217 g_string_free(clipboard, FALSE); | |
9465 | 218 |
219 #if 0 /* Debugging for Windows clipboard */ | |
220 gaim_debug_info("imhtml clipboard", "from gaim: %s\n", ret); | |
221 #endif | |
222 | |
8692 | 223 return ret; |
224 } | |
225 #endif | |
226 | |
4032 | 227 static GtkSmileyTree* |
228 gtk_smiley_tree_new () | |
229 { | |
230 return g_new0 (GtkSmileyTree, 1); | |
231 } | |
232 | |
233 static void | |
234 gtk_smiley_tree_insert (GtkSmileyTree *tree, | |
4263 | 235 GtkIMHtmlSmiley *smiley) |
4032 | 236 { |
237 GtkSmileyTree *t = tree; | |
4263 | 238 const gchar *x = smiley->smile; |
4032 | 239 |
240 if (!strlen (x)) | |
241 return; | |
242 | |
243 while (*x) { | |
244 gchar *pos; | |
245 gint index; | |
246 | |
247 if (!t->values) | |
248 t->values = g_string_new (""); | |
249 | |
250 pos = strchr (t->values->str, *x); | |
251 if (!pos) { | |
252 t->values = g_string_append_c (t->values, *x); | |
253 index = t->values->len - 1; | |
254 t->children = g_realloc (t->children, t->values->len * sizeof (GtkSmileyTree *)); | |
255 t->children [index] = g_new0 (GtkSmileyTree, 1); | |
256 } else | |
7386 | 257 index = GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str); |
8061 | 258 |
4032 | 259 t = t->children [index]; |
8061 | 260 |
4032 | 261 x++; |
262 } | |
8061 | 263 |
4263 | 264 t->image = smiley; |
4032 | 265 } |
4041 | 266 |
4263 | 267 |
4264 | 268 void gtk_smiley_tree_destroy (GtkSmileyTree *tree) |
4032 | 269 { |
270 GSList *list = g_slist_append (NULL, tree); | |
271 | |
272 while (list) { | |
273 GtkSmileyTree *t = list->data; | |
274 gint i; | |
275 list = g_slist_remove(list, t); | |
7384 | 276 if (t && t->values) { |
4032 | 277 for (i = 0; i < t->values->len; i++) |
278 list = g_slist_append (list, t->children [i]); | |
279 g_string_free (t->values, TRUE); | |
280 g_free (t->children); | |
281 } | |
282 g_free (t); | |
283 } | |
284 } | |
285 | |
5967 | 286 static gboolean gtk_size_allocate_cb(GtkIMHtml *widget, GtkAllocation *alloc, gpointer user_data) |
287 { | |
288 GdkRectangle rect; | |
8726 | 289 int xminus; |
5967 | 290 |
291 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &rect); | |
292 if(widget->old_rect.width != rect.width || widget->old_rect.height != rect.height){ | |
293 GList *iter = GTK_IMHTML(widget)->scalables; | |
294 | |
8726 | 295 xminus = gtk_text_view_get_left_margin(GTK_TEXT_VIEW(widget)) + |
296 gtk_text_view_get_right_margin(GTK_TEXT_VIEW(widget)); | |
297 | |
5967 | 298 while(iter){ |
299 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(iter->data); | |
8726 | 300 scale->scale(scale, rect.width - xminus, rect.height); |
5967 | 301 |
302 iter = iter->next; | |
303 } | |
304 } | |
305 | |
306 widget->old_rect = rect; | |
307 return FALSE; | |
308 } | |
309 | |
310 static gint | |
311 gtk_imhtml_tip_paint (GtkIMHtml *imhtml) | |
312 { | |
313 PangoLayout *layout; | |
314 | |
315 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
316 | |
317 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
318 | |
8061 | 319 gtk_paint_flat_box (imhtml->tip_window->style, imhtml->tip_window->window, |
5967 | 320 GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, imhtml->tip_window, |
321 "tooltip", 0, 0, -1, -1); | |
322 | |
323 gtk_paint_layout (imhtml->tip_window->style, imhtml->tip_window->window, GTK_STATE_NORMAL, | |
324 FALSE, NULL, imhtml->tip_window, NULL, 4, 4, layout); | |
325 | |
326 g_object_unref(layout); | |
327 return FALSE; | |
328 } | |
329 | |
330 static gint | |
331 gtk_imhtml_tip (gpointer data) | |
332 { | |
333 GtkIMHtml *imhtml = data; | |
8526 | 334 PangoFontMetrics *font_metrics; |
5967 | 335 PangoLayout *layout; |
8526 | 336 PangoFont *font; |
5967 | 337 |
338 gint gap, x, y, h, w, scr_w, baseline_skip; | |
339 | |
340 g_return_val_if_fail(GTK_IS_IMHTML(imhtml), FALSE); | |
341 | |
342 if (!imhtml->tip || !GTK_WIDGET_DRAWABLE (GTK_WIDGET(imhtml))) { | |
343 imhtml->tip_timer = 0; | |
344 return FALSE; | |
345 } | |
8061 | 346 |
5967 | 347 if (imhtml->tip_window){ |
348 gtk_widget_destroy (imhtml->tip_window); | |
349 imhtml->tip_window = NULL; | |
350 } | |
351 | |
352 imhtml->tip_timer = 0; | |
353 imhtml->tip_window = gtk_window_new (GTK_WINDOW_POPUP); | |
354 gtk_widget_set_app_paintable (imhtml->tip_window, TRUE); | |
355 gtk_window_set_resizable (GTK_WINDOW (imhtml->tip_window), FALSE); | |
356 gtk_widget_set_name (imhtml->tip_window, "gtk-tooltips"); | |
357 g_signal_connect_swapped (G_OBJECT (imhtml->tip_window), "expose_event", | |
358 G_CALLBACK (gtk_imhtml_tip_paint), imhtml); | |
359 | |
360 gtk_widget_ensure_style (imhtml->tip_window); | |
361 layout = gtk_widget_create_pango_layout(imhtml->tip_window, imhtml->tip); | |
8526 | 362 font = pango_context_load_font(pango_layout_get_context(layout), |
363 imhtml->tip_window->style->font_desc); | |
364 | |
365 if (font == NULL) { | |
366 char *tmp = pango_font_description_to_string( | |
367 imhtml->tip_window->style->font_desc); | |
368 | |
369 gaim_debug(GAIM_DEBUG_ERROR, "gtk_imhtml_tip", | |
370 "pango_context_load_font() couldn't load font: '%s'\n", | |
371 tmp); | |
372 g_free(tmp); | |
373 | |
374 return FALSE; | |
375 } | |
376 | |
377 font_metrics = pango_font_get_metrics(font, NULL); | |
5967 | 378 |
379 pango_layout_get_pixel_size(layout, &scr_w, NULL); | |
8526 | 380 gap = PANGO_PIXELS((pango_font_metrics_get_ascent(font_metrics) + |
381 pango_font_metrics_get_descent(font_metrics))/ 4); | |
5967 | 382 |
383 if (gap < 2) | |
384 gap = 2; | |
8526 | 385 baseline_skip = PANGO_PIXELS(pango_font_metrics_get_ascent(font_metrics) + |
386 pango_font_metrics_get_descent(font_metrics)); | |
5967 | 387 w = 8 + scr_w; |
388 h = 8 + baseline_skip; | |
389 | |
390 gdk_window_get_pointer (NULL, &x, &y, NULL); | |
391 if (GTK_WIDGET_NO_WINDOW (GTK_WIDGET(imhtml))) | |
392 y += GTK_WIDGET(imhtml)->allocation.y; | |
393 | |
394 scr_w = gdk_screen_width(); | |
395 | |
396 x -= ((w >> 1) + 4); | |
397 | |
398 if ((x + w) > scr_w) | |
399 x -= (x + w) - scr_w; | |
400 else if (x < 0) | |
401 x = 0; | |
402 | |
8526 | 403 y = y + PANGO_PIXELS(pango_font_metrics_get_ascent(font_metrics) + |
404 pango_font_metrics_get_descent(font_metrics)); | |
5967 | 405 |
406 gtk_widget_set_size_request (imhtml->tip_window, w, h); | |
407 gtk_widget_show (imhtml->tip_window); | |
408 gtk_window_move (GTK_WINDOW(imhtml->tip_window), x, y); | |
409 | |
8526 | 410 pango_font_metrics_unref(font_metrics); |
5967 | 411 g_object_unref(layout); |
412 | |
413 return FALSE; | |
414 } | |
415 | |
416 gboolean gtk_motion_event_notify(GtkWidget *imhtml, GdkEventMotion *event, gpointer data) | |
8061 | 417 { |
5967 | 418 GtkTextIter iter; |
419 GdkWindow *win = event->window; | |
420 int x, y; | |
421 char *tip = NULL; | |
422 GSList *tags = NULL, *templist = NULL; | |
10799 | 423 GdkColor *norm, *pre; |
424 GtkTextTag *tag = NULL, *oldprelit_tag; | |
425 | |
426 oldprelit_tag = GTK_IMHTML(imhtml)->prelit_tag; | |
427 | |
5967 | 428 gdk_window_get_pointer(GTK_WIDGET(imhtml)->window, NULL, NULL, NULL); |
429 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), GTK_TEXT_WINDOW_WIDGET, | |
10799 | 430 event->x, event->y, &x, &y); |
5967 | 431 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); |
432 tags = gtk_text_iter_get_tags(&iter); | |
433 | |
434 templist = tags; | |
435 while (templist) { | |
10799 | 436 tag = templist->data; |
5967 | 437 tip = g_object_get_data(G_OBJECT(tag), "link_url"); |
438 if (tip) | |
439 break; | |
440 templist = templist->next; | |
441 } | |
8061 | 442 |
10799 | 443 if (tip) { |
444 gtk_widget_style_get(GTK_WIDGET(imhtml), "hyperlink-prelight-color", &pre, NULL); | |
445 GTK_IMHTML(imhtml)->prelit_tag = tag; | |
446 if (tag != oldprelit_tag) { | |
447 if (pre) | |
448 g_object_set(G_OBJECT(tag), "foreground-gdk", pre, NULL); | |
449 else | |
450 g_object_set(G_OBJECT(tag), "foreground", "light blue", NULL); | |
451 } | |
452 } else { | |
453 GTK_IMHTML(imhtml)->prelit_tag = NULL; | |
454 } | |
455 | |
10834 | 456 if ((oldprelit_tag != NULL) && (GTK_IMHTML(imhtml)->prelit_tag != oldprelit_tag)) { |
10799 | 457 gtk_widget_style_get(GTK_WIDGET(imhtml), "hyperlink-color", &norm, NULL); |
458 if (norm) | |
459 g_object_set(G_OBJECT(oldprelit_tag), "foreground-gdk", norm, NULL); | |
460 else | |
461 g_object_set(G_OBJECT(oldprelit_tag), "foreground", "blue", NULL); | |
462 } | |
463 | |
5967 | 464 if (GTK_IMHTML(imhtml)->tip) { |
465 if ((tip == GTK_IMHTML(imhtml)->tip)) { | |
466 return FALSE; | |
467 } | |
468 /* We've left the cell. Remove the timeout and create a new one below */ | |
469 if (GTK_IMHTML(imhtml)->tip_window) { | |
470 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
471 GTK_IMHTML(imhtml)->tip_window = NULL; | |
472 } | |
8061 | 473 if (GTK_IMHTML(imhtml)->editable) |
474 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->text_cursor); | |
475 else | |
476 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 477 if (GTK_IMHTML(imhtml)->tip_timer) |
478 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
479 GTK_IMHTML(imhtml)->tip_timer = 0; | |
480 } | |
8061 | 481 |
10799 | 482 if (tip){ |
8061 | 483 if (!GTK_IMHTML(imhtml)->editable) |
484 gdk_window_set_cursor(win, GTK_IMHTML(imhtml)->hand_cursor); | |
485 GTK_IMHTML(imhtml)->tip_timer = g_timeout_add (TOOLTIP_TIMEOUT, | |
5967 | 486 gtk_imhtml_tip, imhtml); |
487 } | |
8061 | 488 |
5967 | 489 GTK_IMHTML(imhtml)->tip = tip; |
490 g_slist_free(tags); | |
491 return FALSE; | |
492 } | |
493 | |
494 gboolean gtk_leave_event_notify(GtkWidget *imhtml, GdkEventCrossing *event, gpointer data) | |
495 { | |
496 /* when leaving the widget, clear any current & pending tooltips and restore the cursor */ | |
10799 | 497 if (GTK_IMHTML(imhtml)->prelit_tag) { |
498 GdkColor *norm; | |
499 gtk_widget_style_get(GTK_WIDGET(imhtml), "hyperlink-color", &norm, NULL); | |
500 if (norm) | |
501 g_object_set(G_OBJECT(GTK_IMHTML(imhtml)->prelit_tag), "foreground-gdk", norm, NULL); | |
502 else | |
503 g_object_set(G_OBJECT(GTK_IMHTML(imhtml)->prelit_tag), "foreground", "blue", NULL); | |
504 GTK_IMHTML(imhtml)->prelit_tag = NULL; | |
505 } | |
506 | |
5967 | 507 if (GTK_IMHTML(imhtml)->tip_window) { |
508 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
509 GTK_IMHTML(imhtml)->tip_window = NULL; | |
510 } | |
511 if (GTK_IMHTML(imhtml)->tip_timer) { | |
512 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
513 GTK_IMHTML(imhtml)->tip_timer = 0; | |
514 } | |
8061 | 515 if (GTK_IMHTML(imhtml)->editable) |
516 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->text_cursor); | |
517 else | |
518 gdk_window_set_cursor(event->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
5967 | 519 |
8568 | 520 /* propagate the event normally */ |
5967 | 521 return FALSE; |
522 } | |
523 | |
10844 | 524 #if (!GTK_CHECK_VERSION(2,2,0)) |
6066 | 525 /* |
526 * XXX - This should be removed eventually. | |
527 * | |
8061 | 528 * This function exists to work around a gross bug in GtkTextView. |
529 * Basically, we short circuit ctrl+a and ctrl+end because they make | |
6066 | 530 * el program go boom. |
531 * | |
8061 | 532 * It's supposed to be fixed in gtk2.2. You can view the bug report at |
6066 | 533 * http://bugzilla.gnome.org/show_bug.cgi?id=107939 |
534 */ | |
10849 | 535 static gboolean |
536 gtk_key_pressed_cb(GtkIMHtml *imhtml, GdkEventKey *event, gpointer data) | |
8677 | 537 { |
10844 | 538 if (event->state & GDK_CONTROL_MASK) { |
6066 | 539 switch (event->keyval) { |
10844 | 540 case 'a': |
541 case GDK_Home: | |
542 case GDK_End: | |
543 return TRUE; | |
6066 | 544 } |
10844 | 545 } |
6066 | 546 return FALSE; |
547 } | |
10844 | 548 #endif /* !(GTK+ >= 2.2.0) */ |
10692 | 549 |
550 static gint | |
551 gtk_imhtml_expose_event (GtkWidget *widget, | |
552 GdkEventExpose *event) | |
553 { | |
10776 | 554 GtkTextIter start, end, cur; |
555 int buf_x, buf_y; | |
556 GdkRectangle visible_rect; | |
10777 | 557 GdkGC *gc = gdk_gc_new(GDK_DRAWABLE(event->window)); |
558 GdkColor gcolor; | |
10844 | 559 |
10776 | 560 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &visible_rect); |
561 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(widget), | |
562 GTK_TEXT_WINDOW_TEXT, | |
563 visible_rect.x, | |
564 visible_rect.y, | |
565 &visible_rect.x, | |
566 &visible_rect.y); | |
567 | |
568 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT, | |
569 event->area.x, event->area.y, &buf_x, &buf_y); | |
570 | |
10777 | 571 if (GTK_IMHTML(widget)->editable || GTK_IMHTML(widget)->wbfo) { |
572 | |
573 if (GTK_IMHTML(widget)->edit.background) { | |
574 gdk_color_parse(GTK_IMHTML(widget)->edit.background, &gcolor); | |
575 gdk_gc_set_rgb_fg_color(gc, &gcolor); | |
576 } else { | |
577 gdk_gc_set_rgb_fg_color(gc, &(widget->style->base[GTK_WIDGET_STATE(widget)])); | |
578 } | |
579 | |
580 gdk_draw_rectangle(event->window, | |
581 gc, | |
582 TRUE, | |
583 visible_rect.x, visible_rect.y, visible_rect.width, visible_rect.height); | |
584 gdk_gc_unref(gc); | |
585 | |
586 if (GTK_WIDGET_CLASS (parent_class)->expose_event) | |
587 return (* GTK_WIDGET_CLASS (parent_class)->expose_event) | |
588 (widget, event); | |
589 | |
590 return FALSE; | |
591 | |
592 } | |
593 | |
10776 | 594 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &start, buf_x, buf_y); |
595 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(widget), &end, | |
596 buf_x + event->area.width, buf_y + event->area.height); | |
597 | |
598 | |
599 | |
600 cur = start; | |
601 | |
602 while (gtk_text_iter_in_range(&cur, &start, &end)) { | |
10795 | 603 GSList *tags = gtk_text_iter_get_tags(&cur); |
604 GSList *l; | |
10776 | 605 |
606 for (l = tags; l; l = l->next) { | |
607 GtkTextTag *tag = l->data; | |
608 GdkRectangle rect; | |
609 GdkRectangle tag_area; | |
610 const char *color; | |
10777 | 611 |
10776 | 612 if (strncmp(tag->name, "BACKGROUND ", 11)) |
613 continue; | |
614 | |
615 if (gtk_text_iter_ends_tag(&cur, tag)) | |
616 continue; | |
617 | |
618 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(widget), &cur, &tag_area); | |
619 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(widget), | |
620 GTK_TEXT_WINDOW_TEXT, | |
621 tag_area.x, | |
622 tag_area.y, | |
623 &tag_area.x, | |
624 &tag_area.y); | |
625 rect.x = visible_rect.x; | |
626 rect.y = tag_area.y; | |
627 | |
10795 | 628 do |
10776 | 629 gtk_text_iter_forward_to_tag_toggle(&cur, tag); |
10795 | 630 while (!gtk_text_iter_is_end(&cur) && gtk_text_iter_begins_tag(&cur, tag)); |
631 | |
10776 | 632 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(widget), &cur, &tag_area); |
633 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(widget), | |
634 GTK_TEXT_WINDOW_TEXT, | |
635 tag_area.x, | |
636 tag_area.y, | |
637 &tag_area.x, | |
638 &tag_area.y); | |
639 | |
640 rect.width = visible_rect.width; | |
641 if (gtk_text_iter_is_end(&cur)) | |
642 rect.height = visible_rect.y + visible_rect.height - rect.y; | |
643 else | |
644 rect.height = tag_area.y + tag_area.height - rect.y | |
645 + gtk_text_view_get_pixels_below_lines(GTK_TEXT_VIEW(widget)); | |
646 | |
647 color = tag->name + 11; | |
648 | |
649 gdk_color_parse(color, &gcolor); | |
650 gdk_gc_set_rgb_fg_color(gc, &gcolor); | |
651 | |
652 | |
653 gdk_draw_rectangle(event->window, | |
654 gc, | |
655 TRUE, | |
656 rect.x, rect.y, rect.width, rect.height); | |
10795 | 657 gtk_text_iter_backward_char(&cur); /* go back one, in case the end is the begining is the end |
658 * note that above, we always moved cur ahead by at least | |
659 * one character */ | |
10776 | 660 break; |
661 } | |
662 | |
663 g_slist_free(tags); | |
10795 | 664 |
665 /* loop until another tag begins, or no tag begins */ | |
666 while (gtk_text_iter_forward_to_tag_toggle(&cur, NULL) && | |
667 !gtk_text_iter_is_end(&cur) && | |
668 !gtk_text_iter_begins_tag(&cur, NULL)); | |
669 } | |
670 | |
10777 | 671 gdk_gc_unref(gc); |
672 | |
10692 | 673 if (GTK_WIDGET_CLASS (parent_class)->expose_event) |
674 return (* GTK_WIDGET_CLASS (parent_class)->expose_event) | |
675 (widget, event); | |
10776 | 676 |
10692 | 677 return FALSE; |
678 } | |
679 | |
680 | |
8931 | 681 static void paste_unformatted_cb(GtkMenuItem *menu, GtkIMHtml *imhtml) |
682 { | |
683 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD); | |
684 | |
685 gtk_clipboard_request_text(clipboard, paste_plaintext_received_cb, imhtml); | |
686 | |
687 } | |
688 | |
10692 | 689 |
690 | |
8931 | 691 static void hijack_menu_cb(GtkIMHtml *imhtml, GtkMenu *menu, gpointer data) |
692 { | |
693 GtkWidget *menuitem; | |
694 | |
695 menuitem = gtk_menu_item_new_with_mnemonic(_("Pa_ste As Text")); | |
696 gtk_widget_show(menuitem); | |
697 gtk_widget_set_sensitive(menuitem, | |
698 (imhtml->editable && | |
699 gtk_clipboard_wait_is_text_available( | |
700 gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD)))); | |
701 /* put it after "Paste" */ | |
702 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), menuitem, 3); | |
703 | |
704 g_signal_connect(G_OBJECT(menuitem), "activate", | |
705 G_CALLBACK(paste_unformatted_cb), imhtml); | |
706 } | |
707 | |
8061 | 708 static void gtk_imhtml_clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, GtkIMHtml *imhtml) { |
8681 | 709 char *text; |
8782
5a2b5e4abf3a
[gaim-migrate @ 9544]
Christian Hammond <chipx86@chipx86.com>
parents:
8758
diff
changeset
|
710 gboolean primary; |
8061 | 711 GtkTextIter start, end; |
712 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
713 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
10013 | 714 |
8061 | 715 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); |
716 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
8782
5a2b5e4abf3a
[gaim-migrate @ 9544]
Christian Hammond <chipx86@chipx86.com>
parents:
8758
diff
changeset
|
717 primary = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY) == clipboard; |
8061 | 718 |
719 if (info == TARGET_HTML) { | |
8907 | 720 gsize len; |
8148 | 721 char *selection; |
8061 | 722 GString *str = g_string_new(NULL); |
8681 | 723 if (primary) { |
724 text = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
10013 | 725 } else |
8681 | 726 text = imhtml->clipboard_html_string; |
8061 | 727 |
728 /* Mozilla asks that we start our text/html with the Unicode byte order mark */ | |
729 str = g_string_append_unichar(str, 0xfeff); | |
730 str = g_string_append(str, text); | |
731 str = g_string_append_unichar(str, 0x0000); | |
8148 | 732 selection = g_convert(str->str, str->len, "UCS-2", "UTF-8", NULL, &len, NULL); |
8719 | 733 gtk_selection_data_set(selection_data, gdk_atom_intern("text/html", FALSE), 16, selection, len); |
8061 | 734 g_string_free(str, TRUE); |
735 g_free(selection); | |
736 } else { | |
8681 | 737 if (primary) { |
738 text = gtk_imhtml_get_text(imhtml, &start, &end); | |
739 } else | |
740 text = imhtml->clipboard_text_string; | |
8061 | 741 gtk_selection_data_set_text(selection_data, text, strlen(text)); |
742 } | |
8681 | 743 if (primary) /* This was allocated here */ |
744 g_free(text); | |
745 } | |
8061 | 746 |
747 static void gtk_imhtml_primary_clipboard_clear(GtkClipboard *clipboard, GtkIMHtml *imhtml) | |
7749 | 748 { |
8061 | 749 GtkTextIter insert; |
750 GtkTextIter selection_bound; | |
751 | |
752 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &insert, | |
753 gtk_text_buffer_get_mark (imhtml->text_buffer, "insert")); | |
754 gtk_text_buffer_get_iter_at_mark (imhtml->text_buffer, &selection_bound, | |
755 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound")); | |
756 | |
757 if (!gtk_text_iter_equal (&insert, &selection_bound)) | |
758 gtk_text_buffer_move_mark (imhtml->text_buffer, | |
759 gtk_text_buffer_get_mark (imhtml->text_buffer, "selection_bound"), | |
760 &insert); | |
7749 | 761 } |
7742 | 762 |
8677 | 763 static void copy_clipboard_cb(GtkIMHtml *imhtml, gpointer unused) |
7749 | 764 { |
8681 | 765 GtkTextIter start, end; |
766 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
767 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
768 | |
769 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | |
770 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
771 | |
8061 | 772 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), |
773 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
774 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
775 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); | |
7346 | 776 |
8681 | 777 if (imhtml->clipboard_html_string) { |
778 g_free(imhtml->clipboard_html_string); | |
779 g_free(imhtml->clipboard_text_string); | |
780 } | |
781 | |
782 imhtml->clipboard_html_string = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
783 imhtml->clipboard_text_string = gtk_imhtml_get_text(imhtml, &start, &end); | |
784 | |
8692 | 785 #ifdef _WIN32 |
786 /* We're going to still copy plain text, but let's toss the "HTML Format" | |
787 we need into the windows clipboard now as well. */ | |
788 HGLOBAL hdata; | |
789 gchar *clipboard = clipboard_html_to_win32(imhtml->clipboard_html_string); | |
790 gchar *buffer; | |
791 gint length = strlen(clipboard); | |
792 if(clipboard != NULL) { | |
8693 | 793 OpenClipboard(NULL); |
8692 | 794 hdata = GlobalAlloc(GMEM_MOVEABLE, length); |
795 buffer = GlobalLock(hdata); | |
796 memcpy(buffer, clipboard, length); | |
797 GlobalUnlock(hdata); | |
798 SetClipboardData(win_html_fmt, hdata); | |
799 CloseClipboard(); | |
8693 | 800 g_free(clipboard); |
8692 | 801 } |
802 #endif | |
803 | |
8061 | 804 g_signal_stop_emission_by_name(imhtml, "copy-clipboard"); |
805 } | |
806 | |
8698 | 807 static void cut_clipboard_cb(GtkIMHtml *imhtml, gpointer unused) |
808 { | |
809 GtkTextIter start, end; | |
810 GtkTextMark *sel = gtk_text_buffer_get_selection_bound(imhtml->text_buffer); | |
811 GtkTextMark *ins = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
812 | |
813 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, sel); | |
814 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &end, ins); | |
815 | |
816 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD), | |
817 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
818 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
819 (GtkClipboardClearFunc)NULL, G_OBJECT(imhtml)); | |
820 | |
821 if (imhtml->clipboard_html_string) { | |
822 g_free(imhtml->clipboard_html_string); | |
823 g_free(imhtml->clipboard_text_string); | |
824 } | |
825 | |
826 imhtml->clipboard_html_string = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
827 imhtml->clipboard_text_string = gtk_imhtml_get_text(imhtml, &start, &end); | |
828 | |
829 #ifdef _WIN32 | |
830 /* We're going to still copy plain text, but let's toss the "HTML Format" | |
831 we need into the windows clipboard now as well. */ | |
832 HGLOBAL hdata; | |
833 gchar *clipboard = clipboard_html_to_win32(imhtml->clipboard_html_string); | |
834 gchar *buffer; | |
835 gint length = strlen(clipboard); | |
836 if(clipboard != NULL) { | |
837 OpenClipboard(NULL); | |
838 hdata = GlobalAlloc(GMEM_MOVEABLE, length); | |
839 buffer = GlobalLock(hdata); | |
840 memcpy(buffer, clipboard, length); | |
841 GlobalUnlock(hdata); | |
842 SetClipboardData(win_html_fmt, hdata); | |
843 CloseClipboard(); | |
844 g_free(clipboard); | |
845 } | |
846 #endif | |
847 | |
848 if (imhtml->editable) | |
849 gtk_text_buffer_delete_selection(imhtml->text_buffer, FALSE, FALSE); | |
850 g_signal_stop_emission_by_name(imhtml, "cut-clipboard"); | |
851 } | |
852 | |
8931 | 853 static void imhtml_paste_insert(GtkIMHtml *imhtml, const char *text, gboolean plaintext) |
854 { | |
855 GtkTextIter iter; | |
9465 | 856 GtkIMHtmlOptions flags = plaintext ? 0 : (GTK_IMHTML_NO_NEWLINE | GTK_IMHTML_NO_COMMENTS); |
8931 | 857 |
9028 | 858 if (gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) |
859 gtk_text_buffer_delete_selection(imhtml->text_buffer, TRUE, TRUE); | |
860 | |
8931 | 861 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, gtk_text_buffer_get_insert(imhtml->text_buffer)); |
862 if (!imhtml->wbfo && !plaintext) | |
863 gtk_imhtml_close_tags(imhtml, &iter); | |
864 | |
865 gtk_imhtml_insert_html_at_iter(imhtml, text, flags, &iter); | |
10666 | 866 if (!imhtml->wbfo && !plaintext) |
867 gtk_imhtml_close_tags(imhtml, &iter); | |
868 gtk_text_buffer_move_mark_by_name(imhtml->text_buffer, "insert", &iter); | |
8931 | 869 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(imhtml), gtk_text_buffer_get_insert(imhtml->text_buffer), |
870 0, FALSE, 0.0, 0.0); | |
871 } | |
872 | |
873 static void paste_plaintext_received_cb (GtkClipboard *clipboard, const gchar *text, gpointer data) | |
874 { | |
875 char *tmp; | |
876 | |
877 if (text == NULL) | |
878 return; | |
879 | |
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10699
diff
changeset
|
880 tmp = g_markup_escape_text(text, -1); |
8931 | 881 imhtml_paste_insert(data, tmp, TRUE); |
882 g_free(tmp); | |
883 } | |
884 | |
8061 | 885 static void paste_received_cb (GtkClipboard *clipboard, GtkSelectionData *selection_data, gpointer data) |
886 { | |
887 char *text; | |
888 GtkIMHtml *imhtml = data; | |
7809 | 889 |
8123 | 890 if (!gtk_text_view_get_editable(GTK_TEXT_VIEW(imhtml))) |
8105 | 891 return; |
892 | |
8061 | 893 if (selection_data->length < 0) { |
8931 | 894 gtk_clipboard_request_text(clipboard, paste_plaintext_received_cb, imhtml); |
895 return; | |
8061 | 896 } else { |
8719 | 897 #if 0 |
898 /* Here's some debug code, for figuring out what sent to us over the clipboard. */ | |
899 { | |
900 int i; | |
901 | |
902 gaim_debug_misc("gtkimhtml", "In paste_received_cb():\n\tformat = %d, length = %d\n\t", | |
903 selection_data->format, selection_data->length); | |
904 | |
905 for (i = 0; i < (/*(selection_data->format / 8) **/ selection_data->length); i++) { | |
906 if ((i % 70) == 0) | |
907 printf("\n\t"); | |
908 if (selection_data->data[i] == '\0') | |
909 printf("."); | |
910 else | |
911 printf("%c", selection_data->data[i]); | |
912 } | |
913 printf("\n"); | |
914 } | |
915 #endif | |
916 text = g_malloc(selection_data->length); | |
917 memcpy(text, selection_data->data, selection_data->length); | |
7766 | 918 } |
8061 | 919 |
8869 | 920 if (selection_data->length >= 2 && |
921 (*(guint16 *)text == 0xfeff || *(guint16 *)text == 0xfffe)) { | |
922 /* This is UCS-2 */ | |
8909 | 923 char *tmp; |
8869 | 924 char *utf8 = g_convert(text, selection_data->length, "UTF-8", "UCS-2", NULL, NULL, NULL); |
8061 | 925 g_free(text); |
926 text = utf8; | |
8698 | 927 if (!text) { |
8869 | 928 gaim_debug_warning("gtkimhtml", "g_convert from UCS-2 failed in paste_received_cb\n"); |
8698 | 929 return; |
930 } | |
8909 | 931 tmp = g_utf8_next_char(text); |
932 memmove(text, tmp, strlen(tmp) + 1); | |
8061 | 933 } |
9621 | 934 |
8698 | 935 if (!(*text) || !g_utf8_validate(text, -1, NULL)) { |
936 gaim_debug_warning("gtkimhtml", "empty string or invalid UTF-8 in paste_received_cb\n"); | |
937 g_free(text); | |
938 return; | |
939 } | |
940 | |
8931 | 941 imhtml_paste_insert(imhtml, text, FALSE); |
8681 | 942 g_free(text); |
8061 | 943 } |
944 | |
945 static void paste_clipboard_cb(GtkIMHtml *imhtml, gpointer blah) | |
946 { | |
8931 | 947 #ifdef _WIN32 |
948 /* If we're on windows, let's see if we can get data from the HTML Format | |
10814
364a2ef907ae
[gaim-migrate @ 12468]
Luke Schierer <lschiere@pidgin.im>
parents:
10799
diff
changeset
|
949 clipboard before we try to paste from the GTK+ buffer */ |
8931 | 950 HGLOBAL hdata; |
951 DWORD err; | |
952 char *buffer; | |
953 char *text; | |
954 | |
955 if (!gtk_text_view_get_editable(GTK_TEXT_VIEW(imhtml))) | |
956 return; | |
957 | |
958 if (IsClipboardFormatAvailable(win_html_fmt)) { | |
959 OpenClipboard(NULL); | |
960 hdata = GetClipboardData(win_html_fmt); | |
961 if (hdata == NULL) { | |
962 err = GetLastError(); | |
963 gaim_debug_info("html clipboard", "error number %u! See http://msdn.microsoft.com/library/en-us/debug/base/system_error_codes.asp\n", err); | |
964 CloseClipboard(); | |
965 return; | |
966 } | |
967 buffer = GlobalLock(hdata); | |
968 if (buffer == NULL) { | |
969 err = GetLastError(); | |
970 gaim_debug_info("html clipboard", "error number %u! See http://msdn.microsoft.com/library/en-us/debug/base/system_error_codes.asp\n", err); | |
971 CloseClipboard(); | |
972 return; | |
973 } | |
974 text = clipboard_win32_to_html(buffer); | |
975 GlobalUnlock(hdata); | |
976 CloseClipboard(); | |
977 | |
978 imhtml_paste_insert(imhtml, text, FALSE); | |
979 g_free(text); | |
980 } else { | |
981 #endif | |
8061 | 982 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_CLIPBOARD); |
983 gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE), | |
984 paste_received_cb, imhtml); | |
8931 | 985 #ifdef _WIN32 |
986 } | |
987 #endif | |
8061 | 988 g_signal_stop_emission_by_name(imhtml, "paste-clipboard"); |
7766 | 989 } |
990 | |
8677 | 991 static void imhtml_realized_remove_primary(GtkIMHtml *imhtml, gpointer unused) |
992 { | |
993 gtk_text_buffer_remove_selection_clipboard(GTK_IMHTML(imhtml)->text_buffer, | |
994 gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY)); | |
995 | |
996 } | |
997 | |
8740
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
998 static void imhtml_destroy_add_primary(GtkIMHtml *imhtml, gpointer unused) |
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
999 { |
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
1000 gtk_text_buffer_add_selection_clipboard(GTK_IMHTML(imhtml)->text_buffer, |
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
1001 gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY)); |
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
1002 } |
8677 | 1003 |
1004 static void mark_set_so_update_selection_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, GtkIMHtml *imhtml) | |
1005 { | |
1006 if (gtk_text_buffer_get_selection_bounds(buffer, NULL, NULL)) { | |
1007 gtk_clipboard_set_with_owner(gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY), | |
1008 selection_targets, sizeof(selection_targets) / sizeof(GtkTargetEntry), | |
1009 (GtkClipboardGetFunc)gtk_imhtml_clipboard_get, | |
1010 (GtkClipboardClearFunc)gtk_imhtml_primary_clipboard_clear, G_OBJECT(imhtml)); | |
1011 } | |
1012 } | |
1013 | |
1014 static gboolean gtk_imhtml_button_press_event(GtkIMHtml *imhtml, GdkEventButton *event, gpointer unused) | |
7346 | 1015 { |
8677 | 1016 if (event->button == 2) { |
1017 int x, y; | |
1018 GtkTextIter iter; | |
1019 GtkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(imhtml), GDK_SELECTION_PRIMARY); | |
1020 | |
1021 if (!imhtml->editable) | |
1022 return FALSE; | |
1023 | |
1024 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(imhtml), | |
1025 GTK_TEXT_WINDOW_TEXT, | |
1026 event->x, | |
1027 event->y, | |
1028 &x, | |
1029 &y); | |
1030 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, x, y); | |
1031 gtk_text_buffer_place_cursor(imhtml->text_buffer, &iter); | |
1032 | |
1033 gtk_clipboard_request_contents(clipboard, gdk_atom_intern("text/html", FALSE), | |
1034 paste_received_cb, imhtml); | |
1035 | |
1036 return TRUE; | |
1037 } | |
1038 | |
7346 | 1039 return FALSE; |
1040 } | |
4263 | 1041 |
10108 | 1042 static gboolean imhtml_message_send(GtkIMHtml *imhtml) |
1043 { | |
1044 return FALSE; | |
1045 } | |
1046 | |
10100 | 1047 static void imhtml_toggle_format(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons) |
1048 { | |
10699 | 1049 /* since this function is the handler for the formatting keystrokes, |
1050 we need to check here that the formatting attempted is permitted */ | |
1051 if (!(imhtml->format_functions & buttons)) | |
1052 return; | |
1053 | |
10100 | 1054 switch (buttons) { |
1055 case GTK_IMHTML_BOLD: | |
1056 gtk_imhtml_toggle_bold(imhtml); | |
1057 break; | |
1058 case GTK_IMHTML_ITALIC: | |
1059 gtk_imhtml_toggle_italic(imhtml); | |
1060 break; | |
1061 case GTK_IMHTML_UNDERLINE: | |
1062 gtk_imhtml_toggle_underline(imhtml); | |
1063 break; | |
1064 case GTK_IMHTML_SHRINK: | |
1065 gtk_imhtml_font_shrink(imhtml); | |
1066 break; | |
1067 case GTK_IMHTML_GROW: | |
1068 gtk_imhtml_font_grow(imhtml); | |
1069 break; | |
1070 default: | |
1071 break; | |
1072 } | |
1073 } | |
4032 | 1074 |
1075 static void | |
1076 gtk_imhtml_finalize (GObject *object) | |
1077 { | |
1078 GtkIMHtml *imhtml = GTK_IMHTML(object); | |
4895 | 1079 GList *scalables; |
8962 | 1080 GSList *l; |
8061 | 1081 |
10798 | 1082 if (imhtml->scroll_src) |
1083 g_source_remove(imhtml->scroll_src); | |
1084 if (imhtml->scroll_time) | |
1085 g_timer_destroy(imhtml->scroll_time); | |
1086 | |
4138 | 1087 g_hash_table_destroy(imhtml->smiley_data); |
4032 | 1088 gtk_smiley_tree_destroy(imhtml->default_smilies); |
4138 | 1089 gdk_cursor_unref(imhtml->hand_cursor); |
1090 gdk_cursor_unref(imhtml->arrow_cursor); | |
8061 | 1091 gdk_cursor_unref(imhtml->text_cursor); |
8677 | 1092 |
4735 | 1093 if(imhtml->tip_window){ |
1094 gtk_widget_destroy(imhtml->tip_window); | |
1095 } | |
1096 if(imhtml->tip_timer) | |
1097 gtk_timeout_remove(imhtml->tip_timer); | |
1098 | |
4895 | 1099 for(scalables = imhtml->scalables; scalables; scalables = scalables->next) { |
1100 GtkIMHtmlScalable *scale = GTK_IMHTML_SCALABLE(scalables->data); | |
1101 scale->free(scale); | |
1102 } | |
7991 | 1103 |
8962 | 1104 for (l = imhtml->im_images; l; l = l->next) { |
1105 int id; | |
1106 id = GPOINTER_TO_INT(l->data); | |
1107 if (imhtml->funcs->image_unref) | |
1108 imhtml->funcs->image_unref(id); | |
1109 } | |
1110 | |
8681 | 1111 if (imhtml->clipboard_text_string) { |
1112 g_free(imhtml->clipboard_text_string); | |
1113 g_free(imhtml->clipboard_html_string); | |
1114 } | |
1115 | |
4895 | 1116 g_list_free(imhtml->scalables); |
8962 | 1117 g_slist_free(imhtml->im_images); |
9029 | 1118 if (imhtml->protocol_name) |
1119 g_free(imhtml->protocol_name); | |
10574 | 1120 if (imhtml->search_string) |
1121 g_free(imhtml->search_string); | |
4032 | 1122 G_OBJECT_CLASS(parent_class)->finalize (object); |
1123 } | |
1428 | 1124 |
10814
364a2ef907ae
[gaim-migrate @ 12468]
Luke Schierer <lschiere@pidgin.im>
parents:
10799
diff
changeset
|
1125 /* Boring GTK+ stuff */ |
8519 | 1126 static void gtk_imhtml_class_init (GtkIMHtmlClass *klass) |
1428 | 1127 { |
9007 | 1128 GtkWidgetClass *widget_class = (GtkWidgetClass *) klass; |
3922 | 1129 GtkObjectClass *object_class; |
10100 | 1130 GtkBindingSet *binding_set; |
4032 | 1131 GObjectClass *gobject_class; |
8519 | 1132 object_class = (GtkObjectClass*) klass; |
1133 gobject_class = (GObjectClass*) klass; | |
4032 | 1134 parent_class = gtk_type_class(GTK_TYPE_TEXT_VIEW); |
4417 | 1135 signals[URL_CLICKED] = g_signal_new("url_clicked", |
1136 G_TYPE_FROM_CLASS(gobject_class), | |
1137 G_SIGNAL_RUN_FIRST, | |
1138 G_STRUCT_OFFSET(GtkIMHtmlClass, url_clicked), | |
1139 NULL, | |
1140 0, | |
1141 g_cclosure_marshal_VOID__POINTER, | |
1142 G_TYPE_NONE, 1, | |
1143 G_TYPE_POINTER); | |
8506 | 1144 signals[BUTTONS_UPDATE] = g_signal_new("format_buttons_update", |
8420 | 1145 G_TYPE_FROM_CLASS(gobject_class), |
1146 G_SIGNAL_RUN_FIRST, | |
1147 G_STRUCT_OFFSET(GtkIMHtmlClass, buttons_update), | |
1148 NULL, | |
1149 0, | |
10076 | 1150 g_cclosure_marshal_VOID__INT, |
8420 | 1151 G_TYPE_NONE, 1, |
1152 G_TYPE_INT); | |
1153 signals[TOGGLE_FORMAT] = g_signal_new("format_function_toggle", | |
1154 G_TYPE_FROM_CLASS(gobject_class), | |
10100 | 1155 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
8420 | 1156 G_STRUCT_OFFSET(GtkIMHtmlClass, toggle_format), |
1157 NULL, | |
1158 0, | |
10076 | 1159 g_cclosure_marshal_VOID__INT, |
1160 G_TYPE_NONE, 1, | |
8420 | 1161 G_TYPE_INT); |
8427 | 1162 signals[CLEAR_FORMAT] = g_signal_new("format_function_clear", |
1163 G_TYPE_FROM_CLASS(gobject_class), | |
1164 G_SIGNAL_RUN_FIRST, | |
1165 G_STRUCT_OFFSET(GtkIMHtmlClass, clear_format), | |
1166 NULL, | |
1167 0, | |
10100 | 1168 g_cclosure_marshal_VOID__VOID, |
1169 G_TYPE_NONE, 0); | |
8506 | 1170 signals[UPDATE_FORMAT] = g_signal_new("format_function_update", |
10100 | 1171 G_TYPE_FROM_CLASS(gobject_class), |
1172 G_SIGNAL_RUN_FIRST, | |
1173 G_STRUCT_OFFSET(GtkIMHtmlClass, update_format), | |
1174 NULL, | |
1175 0, | |
1176 g_cclosure_marshal_VOID__VOID, | |
1177 G_TYPE_NONE, 0); | |
10108 | 1178 signals[MESSAGE_SEND] = g_signal_new("message_send", |
1179 G_TYPE_FROM_CLASS(gobject_class), | |
1180 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, | |
1181 G_STRUCT_OFFSET(GtkIMHtmlClass, message_send), | |
1182 NULL, | |
1183 0, g_cclosure_marshal_VOID__VOID, | |
1184 G_TYPE_NONE, 0); | |
10100 | 1185 |
1186 klass->toggle_format = imhtml_toggle_format; | |
10108 | 1187 klass->message_send = imhtml_message_send; |
10184 | 1188 |
4032 | 1189 gobject_class->finalize = gtk_imhtml_finalize; |
10184 | 1190 widget_class->drag_motion = gtk_text_view_drag_motion; |
10692 | 1191 widget_class->expose_event = gtk_imhtml_expose_event; |
9007 | 1192 gtk_widget_class_install_style_property(widget_class, g_param_spec_boxed("hyperlink-color", |
1193 _("Hyperlink color"), | |
1194 _("Color to draw hyperlinks."), | |
1195 GDK_TYPE_COLOR, G_PARAM_READABLE)); | |
10799 | 1196 gtk_widget_class_install_style_property(widget_class, g_param_spec_boxed("hyperlink-prelight-color", |
1197 _("Hyperlink prelight color"), | |
1198 _("Color to draw hyperlinks when mouse is over them."), | |
1199 GDK_TYPE_COLOR, G_PARAM_READABLE)); | |
10100 | 1200 |
1201 binding_set = gtk_binding_set_by_class (parent_class); | |
10110 | 1202 gtk_binding_entry_add_signal (binding_set, GDK_b, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_BOLD); |
10100 | 1203 gtk_binding_entry_add_signal (binding_set, GDK_i, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_ITALIC); |
1204 gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_UNDERLINE); | |
1205 gtk_binding_entry_add_signal (binding_set, GDK_plus, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_GROW); | |
1206 gtk_binding_entry_add_signal (binding_set, GDK_equal, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_GROW); | |
1207 gtk_binding_entry_add_signal (binding_set, GDK_minus, GDK_CONTROL_MASK, "format_function_toggle", 1, G_TYPE_INT, GTK_IMHTML_SHRINK); | |
10108 | 1208 binding_set = gtk_binding_set_by_class(klass); |
1209 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0, "message_send", 0); | |
1210 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0, "message_send", 0); | |
1428 | 1211 } |
1212 | |
3922 | 1213 static void gtk_imhtml_init (GtkIMHtml *imhtml) |
1428 | 1214 { |
3922 | 1215 GtkTextIter iter; |
1216 imhtml->text_buffer = gtk_text_buffer_new(NULL); | |
1217 gtk_text_buffer_get_end_iter (imhtml->text_buffer, &iter); | |
1218 gtk_text_view_set_buffer(GTK_TEXT_VIEW(imhtml), imhtml->text_buffer); | |
5105 | 1219 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(imhtml), GTK_WRAP_WORD_CHAR); |
3922 | 1220 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(imhtml), 5); |
8677 | 1221 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(imhtml), 2); |
1222 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(imhtml), 2); | |
8061 | 1223 /*gtk_text_view_set_indent(GTK_TEXT_VIEW(imhtml), -15);*/ |
3922 | 1224 /*gtk_text_view_set_justification(GTK_TEXT_VIEW(imhtml), GTK_JUSTIFY_FILL);*/ |
8061 | 1225 |
3922 | 1226 /* These tags will be used often and can be reused--we create them on init and then apply them by name |
8932 | 1227 * other tags (color, size, face, etc.) will have to be created and applied dynamically |
9924 | 1228 * Note that even though we created SUB, SUP, and PRE tags here, we don't really |
8932 | 1229 * apply them anywhere yet. */ |
3922 | 1230 gtk_text_buffer_create_tag(imhtml->text_buffer, "BOLD", "weight", PANGO_WEIGHT_BOLD, NULL); |
1231 gtk_text_buffer_create_tag(imhtml->text_buffer, "ITALICS", "style", PANGO_STYLE_ITALIC, NULL); | |
1232 gtk_text_buffer_create_tag(imhtml->text_buffer, "UNDERLINE", "underline", PANGO_UNDERLINE_SINGLE, NULL); | |
1233 gtk_text_buffer_create_tag(imhtml->text_buffer, "STRIKE", "strikethrough", TRUE, NULL); | |
1234 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUB", "rise", -5000, NULL); | |
1235 gtk_text_buffer_create_tag(imhtml->text_buffer, "SUP", "rise", 5000, NULL); | |
1236 gtk_text_buffer_create_tag(imhtml->text_buffer, "PRE", "family", "Monospace", NULL); | |
7295 | 1237 gtk_text_buffer_create_tag(imhtml->text_buffer, "search", "background", "#22ff00", "weight", "bold", NULL); |
8677 | 1238 |
3922 | 1239 /* When hovering over a link, we show the hand cursor--elsewhere we show the plain ol' pointer cursor */ |
1240 imhtml->hand_cursor = gdk_cursor_new (GDK_HAND2); | |
1241 imhtml->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); | |
8061 | 1242 imhtml->text_cursor = gdk_cursor_new (GDK_XTERM); |
2993 | 1243 |
6124 | 1244 imhtml->show_comments = TRUE; |
4253 | 1245 |
4892 | 1246 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 1247 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4032 | 1248 imhtml->default_smilies = gtk_smiley_tree_new(); |
4735 | 1249 |
4944 | 1250 g_signal_connect(G_OBJECT(imhtml), "size-allocate", G_CALLBACK(gtk_size_allocate_cb), NULL); |
4735 | 1251 g_signal_connect(G_OBJECT(imhtml), "motion-notify-event", G_CALLBACK(gtk_motion_event_notify), NULL); |
4944 | 1252 g_signal_connect(G_OBJECT(imhtml), "leave-notify-event", G_CALLBACK(gtk_leave_event_notify), NULL); |
10844 | 1253 #if (!GTK_CHECK_VERSION(2,2,0)) |
1254 /* See the comment for gtk_key_pressed_cb */ | |
6066 | 1255 g_signal_connect(G_OBJECT(imhtml), "key_press_event", G_CALLBACK(gtk_key_pressed_cb), NULL); |
10844 | 1256 #endif |
8677 | 1257 g_signal_connect(G_OBJECT(imhtml), "button_press_event", G_CALLBACK(gtk_imhtml_button_press_event), NULL); |
1258 g_signal_connect(G_OBJECT(imhtml->text_buffer), "insert-text", G_CALLBACK(preinsert_cb), imhtml); | |
8061 | 1259 g_signal_connect_after(G_OBJECT(imhtml->text_buffer), "insert-text", G_CALLBACK(insert_cb), imhtml); |
10169 | 1260 g_signal_connect_after(G_OBJECT(imhtml->text_buffer), "insert-child-anchor", G_CALLBACK(insert_ca_cb), imhtml); |
8091 | 1261 gtk_drag_dest_set(GTK_WIDGET(imhtml), 0, |
1262 link_drag_drop_targets, sizeof(link_drag_drop_targets) / sizeof(GtkTargetEntry), | |
1263 GDK_ACTION_COPY); | |
1264 g_signal_connect(G_OBJECT(imhtml), "drag_data_received", G_CALLBACK(gtk_imhtml_link_drag_rcv_cb), imhtml); | |
9300 | 1265 g_signal_connect(G_OBJECT(imhtml), "drag_drop", G_CALLBACK(gtk_imhtml_link_drop_cb), imhtml); |
8091 | 1266 |
7353 | 1267 g_signal_connect(G_OBJECT(imhtml), "copy-clipboard", G_CALLBACK(copy_clipboard_cb), NULL); |
8698 | 1268 g_signal_connect(G_OBJECT(imhtml), "cut-clipboard", G_CALLBACK(cut_clipboard_cb), NULL); |
8061 | 1269 g_signal_connect(G_OBJECT(imhtml), "paste-clipboard", G_CALLBACK(paste_clipboard_cb), NULL); |
8677 | 1270 g_signal_connect_after(G_OBJECT(imhtml), "realize", G_CALLBACK(imhtml_realized_remove_primary), NULL); |
8740
61a090413b93
[gaim-migrate @ 9495]
Christian Hammond <chipx86@chipx86.com>
parents:
8735
diff
changeset
|
1271 g_signal_connect(G_OBJECT(imhtml), "unrealize", G_CALLBACK(imhtml_destroy_add_primary), NULL); |
8677 | 1272 |
1273 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", | |
1274 G_CALLBACK(mark_set_so_update_selection_cb), imhtml); | |
1275 | |
4944 | 1276 gtk_widget_add_events(GTK_WIDGET(imhtml), GDK_LEAVE_NOTIFY_MASK); |
4735 | 1277 |
8681 | 1278 imhtml->clipboard_text_string = NULL; |
1279 imhtml->clipboard_html_string = NULL; | |
1280 | |
4735 | 1281 imhtml->tip = NULL; |
1282 imhtml->tip_timer = 0; | |
1283 imhtml->tip_window = NULL; | |
4895 | 1284 |
8677 | 1285 imhtml->edit.bold = FALSE; |
1286 imhtml->edit.italic = FALSE; | |
1287 imhtml->edit.underline = FALSE; | |
8061 | 1288 imhtml->edit.forecolor = NULL; |
1289 imhtml->edit.backcolor = NULL; | |
1290 imhtml->edit.fontface = NULL; | |
8677 | 1291 imhtml->edit.fontsize = 0; |
1292 imhtml->edit.link = NULL; | |
1293 | |
9300 | 1294 |
4895 | 1295 imhtml->scalables = NULL; |
8061 | 1296 |
1297 gtk_imhtml_set_editable(imhtml, FALSE); | |
8931 | 1298 g_signal_connect(G_OBJECT(imhtml), "populate-popup", |
1299 G_CALLBACK(hijack_menu_cb), NULL); | |
1300 | |
8692 | 1301 #ifdef _WIN32 |
1302 /* Register HTML Format as desired clipboard format */ | |
1303 win_html_fmt = RegisterClipboardFormat("HTML Format"); | |
1304 #endif | |
2993 | 1305 } |
1306 | |
3922 | 1307 GtkWidget *gtk_imhtml_new(void *a, void *b) |
1428 | 1308 { |
4635 | 1309 return GTK_WIDGET(g_object_new(gtk_imhtml_get_type(), NULL)); |
1428 | 1310 } |
1311 | |
9037 | 1312 GType gtk_imhtml_get_type() |
1428 | 1313 { |
9037 | 1314 static GType imhtml_type = 0; |
1428 | 1315 |
1316 if (!imhtml_type) { | |
9037 | 1317 static const GTypeInfo imhtml_info = { |
4635 | 1318 sizeof(GtkIMHtmlClass), |
1319 NULL, | |
1320 NULL, | |
1321 (GClassInitFunc) gtk_imhtml_class_init, | |
1322 NULL, | |
1323 NULL, | |
1428 | 1324 sizeof (GtkIMHtml), |
4635 | 1325 0, |
1326 (GInstanceInitFunc) gtk_imhtml_init | |
1428 | 1327 }; |
4635 | 1328 |
1329 imhtml_type = g_type_register_static(gtk_text_view_get_type(), | |
1330 "GtkIMHtml", &imhtml_info, 0); | |
1428 | 1331 } |
1332 | |
1333 return imhtml_type; | |
1334 } | |
1335 | |
4417 | 1336 struct url_data { |
1337 GObject *object; | |
1338 gchar *url; | |
1339 }; | |
1340 | |
8677 | 1341 static void url_data_destroy(gpointer mydata) |
1342 { | |
1343 struct url_data *data = mydata; | |
1344 g_object_unref(data->object); | |
1345 g_free(data->url); | |
1346 g_free(data); | |
1347 } | |
1348 | |
4417 | 1349 static void url_open(GtkWidget *w, struct url_data *data) { |
1350 if(!data) return; | |
8061 | 1351 g_signal_emit(data->object, signals[URL_CLICKED], 0, data->url); |
7988 | 1352 |
4417 | 1353 } |
5582 | 1354 |
4417 | 1355 static void url_copy(GtkWidget *w, gchar *url) { |
1356 GtkClipboard *clipboard; | |
1357 | |
8931 | 1358 clipboard = gtk_widget_get_clipboard(w, GDK_SELECTION_PRIMARY); |
4417 | 1359 gtk_clipboard_set_text(clipboard, url, -1); |
5582 | 1360 |
8931 | 1361 clipboard = gtk_widget_get_clipboard(w, GDK_SELECTION_CLIPBOARD); |
5582 | 1362 gtk_clipboard_set_text(clipboard, url, -1); |
4417 | 1363 } |
1364 | |
1365 /* The callback for an event on a link tag. */ | |
8677 | 1366 gboolean tag_event(GtkTextTag *tag, GObject *imhtml, GdkEvent *event, GtkTextIter *arg2, gpointer unused) { |
4417 | 1367 GdkEventButton *event_button = (GdkEventButton *) event; |
8061 | 1368 if (GTK_IMHTML(imhtml)->editable) |
1369 return FALSE; | |
3922 | 1370 if (event->type == GDK_BUTTON_RELEASE) { |
8957 | 1371 if ((event_button->button == 1) || (event_button->button == 2)) { |
4417 | 1372 GtkTextIter start, end; |
1373 /* we shouldn't open a URL if the user has selected something: */ | |
8677 | 1374 if (gtk_text_buffer_get_selection_bounds( |
1375 gtk_text_iter_get_buffer(arg2), &start, &end)) | |
4417 | 1376 return FALSE; |
1377 | |
1378 /* A link was clicked--we emit the "url_clicked" signal | |
1379 * with the URL as the argument */ | |
8677 | 1380 g_object_ref(G_OBJECT(tag)); |
1381 g_signal_emit(imhtml, signals[URL_CLICKED], 0, g_object_get_data(G_OBJECT(tag), "link_url")); | |
1382 g_object_unref(G_OBJECT(tag)); | |
4417 | 1383 return FALSE; |
1384 } else if(event_button->button == 3) { | |
4745 | 1385 GtkWidget *img, *item, *menu; |
4417 | 1386 struct url_data *tempdata = g_new(struct url_data, 1); |
5091 | 1387 tempdata->object = g_object_ref(imhtml); |
8677 | 1388 tempdata->url = g_strdup(g_object_get_data(G_OBJECT(tag), "link_url")); |
4745 | 1389 |
5091 | 1390 /* Don't want the tooltip around if user right-clicked on link */ |
1391 if (GTK_IMHTML(imhtml)->tip_window) { | |
1392 gtk_widget_destroy(GTK_IMHTML(imhtml)->tip_window); | |
1393 GTK_IMHTML(imhtml)->tip_window = NULL; | |
1394 } | |
1395 if (GTK_IMHTML(imhtml)->tip_timer) { | |
1396 g_source_remove(GTK_IMHTML(imhtml)->tip_timer); | |
1397 GTK_IMHTML(imhtml)->tip_timer = 0; | |
1398 } | |
8061 | 1399 if (GTK_IMHTML(imhtml)->editable) |
1400 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->text_cursor); | |
1401 else | |
1402 gdk_window_set_cursor(event_button->window, GTK_IMHTML(imhtml)->arrow_cursor); | |
4417 | 1403 menu = gtk_menu_new(); |
8677 | 1404 g_object_set_data_full(G_OBJECT(menu), "x-imhtml-url-data", tempdata, url_data_destroy); |
4745 | 1405 |
4417 | 1406 /* buttons and such */ |
1407 | |
8677 | 1408 if (!strncmp(tempdata->url, "mailto:", 7)) |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1409 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1410 /* Copy E-Mail Address */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1411 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1412 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1413 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1414 _("_Copy E-Mail Address")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1415 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
|
1416 g_signal_connect(G_OBJECT(item), "activate", |
8677 | 1417 G_CALLBACK(url_copy), tempdata->url + 7); |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1418 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1419 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1420 else |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1421 { |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1422 /* Copy Link Location */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1423 img = gtk_image_new_from_stock(GTK_STOCK_COPY, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1424 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1425 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1426 _("_Copy Link Location")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1427 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
|
1428 g_signal_connect(G_OBJECT(item), "activate", |
8677 | 1429 G_CALLBACK(url_copy), tempdata->url); |
7140
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1430 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1431 |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1432 /* Open Link in Browser */ |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1433 img = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1434 GTK_ICON_SIZE_MENU); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1435 item = gtk_image_menu_item_new_with_mnemonic( |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1436 _("_Open Link in Browser")); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1437 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
|
1438 g_signal_connect(G_OBJECT(item), "activate", |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1439 G_CALLBACK(url_open), tempdata); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1440 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1441 } |
48cc5d5d5a6c
[gaim-migrate @ 7707]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
1442 |
4756 | 1443 |
4417 | 1444 gtk_widget_show_all(menu); |
4756 | 1445 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, |
1446 event_button->button, event_button->time); | |
4745 | 1447 |
4417 | 1448 return TRUE; |
1449 } | |
1428 | 1450 } |
4417 | 1451 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) |
1452 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
1453 be caught by the regular GtkTextView menu */ | |
1454 else | |
1455 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
1428 | 1456 } |
1457 | |
9300 | 1458 static gboolean |
1459 gtk_text_view_drag_motion (GtkWidget *widget, | |
1460 GdkDragContext *context, | |
1461 gint x, | |
1462 gint y, | |
1463 guint time) | |
1464 { | |
1465 GdkDragAction suggested_action = 0; | |
1466 | |
10145 | 1467 if (gtk_drag_dest_find_target (widget, context, NULL) == GDK_NONE) { |
9300 | 1468 /* can't accept any of the offered targets */ |
1469 } else { | |
1470 GtkWidget *source_widget; | |
1471 suggested_action = context->suggested_action; | |
1472 source_widget = gtk_drag_get_source_widget (context); | |
1473 if (source_widget == widget) { | |
1474 /* Default to MOVE, unless the user has | |
1475 * pressed ctrl or alt to affect available actions | |
1476 */ | |
1477 if ((context->actions & GDK_ACTION_MOVE) != 0) | |
1478 suggested_action = GDK_ACTION_MOVE; | |
1479 } | |
1480 } | |
1481 | |
10145 | 1482 gdk_drag_status (context, suggested_action, time); |
9300 | 1483 |
1484 /* TRUE return means don't propagate the drag motion to parent | |
1485 * widgets that may also be drop sites. | |
1486 */ | |
1487 return TRUE; | |
1488 } | |
1489 | |
1490 static void | |
1491 gtk_imhtml_link_drop_cb(GtkWidget *widget, GdkDragContext *context, gint x, gint y, guint time, gpointer user_data) | |
1492 { | |
1493 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL); | |
1494 | |
1495 if (target != GDK_NONE) | |
1496 gtk_drag_get_data (widget, context, target, time); | |
1497 else | |
1498 gtk_drag_finish (context, FALSE, FALSE, time); | |
1499 | |
1500 return; | |
1501 } | |
1502 | |
8091 | 1503 static void |
1504 gtk_imhtml_link_drag_rcv_cb(GtkWidget *widget, GdkDragContext *dc, guint x, guint y, | |
1505 GtkSelectionData *sd, guint info, guint t, GtkIMHtml *imhtml) | |
1506 { | |
9300 | 1507 gchar **links; |
1508 gchar *link; | |
1509 char *text = sd->data; | |
1510 GtkTextMark *mark = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
1511 GtkTextIter iter; | |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1512 gint i = 0; |
9300 | 1513 |
1514 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
1515 | |
8091 | 1516 if(gtk_imhtml_get_editable(imhtml) && sd->data){ |
9300 | 1517 switch (info) { |
10145 | 1518 case GTK_IMHTML_DRAG_URL: |
9300 | 1519 gaim_str_strip_cr(sd->data); |
1520 | |
1521 links = g_strsplit(sd->data, "\n", 0); | |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1522 while((link = links[i]) != NULL){ |
9300 | 1523 if(gaim_str_has_prefix(link, "http://") || |
1524 gaim_str_has_prefix(link, "https://") || | |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1525 gaim_str_has_prefix(link, "ftp://")) |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1526 { |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1527 gchar *label; |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1528 |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1529 if(links[i + 1]) |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1530 i++; |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1531 |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1532 label = links[i]; |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1533 |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1534 gtk_imhtml_insert_link(imhtml, mark, link, label); |
9300 | 1535 } else if (link=='\0') { |
1536 /* Ignore blank lines */ | |
1537 } else { | |
1538 /* Special reasons, aka images being put in via other tag, etc. */ | |
10345 | 1539 /* ... don't pretend we handled it if we didn't */ |
1540 gtk_drag_finish(dc, FALSE, FALSE, t); | |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1541 g_strfreev(links); |
10345 | 1542 return; |
9300 | 1543 } |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1544 |
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1545 i++; |
8091 | 1546 } |
10782
93fd90cbf45c
[gaim-migrate @ 12410]
Luke Schierer <lschiere@pidgin.im>
parents:
10777
diff
changeset
|
1547 g_strfreev(links); |
9300 | 1548 break; |
10145 | 1549 case GTK_IMHTML_DRAG_HTML: |
10243 | 1550 { |
1551 char *utf8 = NULL; | |
1552 /* Ewww. This is all because mozilla thinks that text/html is 'for internal use only.' | |
1553 * as explained by this comment in gtkhtml: | |
1554 * | |
1555 * FIXME This hack decides the charset of the selection. It seems that | |
1556 * mozilla/netscape alway use ucs2 for text/html | |
1557 * and openoffice.org seems to always use utf8 so we try to validate | |
1558 * the string as utf8 and if that fails we assume it is ucs2 | |
1559 * | |
1560 * See also the comment on text/html here: | |
1561 * http://mail.gnome.org/archives/gtk-devel-list/2001-September/msg00114.html | |
1562 */ | |
1563 if (sd->length >= 2 && !g_utf8_validate(text, sd->length - 1, NULL)) { | |
1564 utf8 = g_convert(text, sd->length, "UTF-8", "UCS-2", NULL, NULL, NULL); | |
1565 | |
1566 if (!utf8) { | |
9300 | 1567 gaim_debug_warning("gtkimhtml", "g_convert from UCS-2 failed in drag_rcv_cb\n"); |
1568 return; | |
1569 } | |
10243 | 1570 |
1571 if (*(guint16 *)text == 0xfeff || *(guint16 *)text == 0xfffe || TRUE) { | |
1572 char *tmp; | |
1573 tmp = g_utf8_next_char(utf8); | |
1574 memmove(utf8, tmp, strlen(tmp) + 1); | |
1575 } | |
1576 } else if (!(*text) || !g_utf8_validate(text, -1, NULL)) { | |
9300 | 1577 gaim_debug_warning("gtkimhtml", "empty string or invalid UTF-8 in drag_rcv_cb\n"); |
1578 return; | |
1579 } | |
10243 | 1580 |
1581 gtk_imhtml_insert_html_at_iter(imhtml, utf8 ? utf8 : text, 0, &iter); | |
1582 g_free(utf8); | |
9300 | 1583 break; |
10243 | 1584 } |
10145 | 1585 case GTK_IMHTML_DRAG_TEXT: |
1586 if (!(*text) || !g_utf8_validate(text, -1, NULL)) { | |
1587 gaim_debug_warning("gtkimhtml", "empty string or invalid UTF-8 in drag_rcv_cb\n"); | |
1588 return; | |
1589 } else { | |
10732
c4cb90065e1d
[gaim-migrate @ 12334]
Luke Schierer <lschiere@pidgin.im>
parents:
10699
diff
changeset
|
1590 char *tmp = g_markup_escape_text(text, -1); |
10145 | 1591 gtk_imhtml_insert_html_at_iter(imhtml, tmp, 0, &iter); |
1592 g_free(tmp); | |
1593 } | |
1594 break; | |
9300 | 1595 default: |
10145 | 1596 gtk_drag_finish(dc, FALSE, FALSE, t); |
1597 return; | |
8091 | 1598 } |
1599 gtk_drag_finish(dc, TRUE, (dc->action == GDK_ACTION_MOVE), t); | |
1600 } else { | |
1601 gtk_drag_finish(dc, FALSE, FALSE, t); | |
1602 } | |
1603 } | |
1604 | |
4298 | 1605 /* this isn't used yet |
9300 | 1606 static void gtk_smiley_tree_remove (GtkSmileyTree *tree, |
4263 | 1607 GtkIMHtmlSmiley *smiley) |
4032 | 1608 { |
1609 GtkSmileyTree *t = tree; | |
4263 | 1610 const gchar *x = smiley->smile; |
4032 | 1611 gint len = 0; |
1612 | |
1613 while (*x) { | |
1614 gchar *pos; | |
1615 | |
1616 if (!t->values) | |
1617 return; | |
1618 | |
1619 pos = strchr (t->values->str, *x); | |
1620 if (pos) | |
1621 t = t->children [(int) pos - (int) t->values->str]; | |
1622 else | |
1623 return; | |
1624 | |
1625 x++; len++; | |
1626 } | |
1627 | |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
1628 if (t->image) { |
4032 | 1629 t->image = NULL; |
4141
ccec4fde84f4
[gaim-migrate @ 4359]
Christian Hammond <chipx86@chipx86.com>
parents:
4140
diff
changeset
|
1630 } |
4032 | 1631 } |
4298 | 1632 */ |
1633 | |
4032 | 1634 |
1635 static gint | |
1636 gtk_smiley_tree_lookup (GtkSmileyTree *tree, | |
1637 const gchar *text) | |
1638 { | |
1639 GtkSmileyTree *t = tree; | |
1640 const gchar *x = text; | |
1641 gint len = 0; | |
8505 | 1642 gchar *amp; |
1643 gint alen; | |
4032 | 1644 |
1645 while (*x) { | |
1646 gchar *pos; | |
1647 | |
1648 if (!t->values) | |
1649 break; | |
1650 | |
8505 | 1651 if(*x == '&' && gtk_imhtml_is_amp_escape(x, &, &alen)) { |
1652 len += alen - strlen(amp); | |
1653 x += alen - strlen(amp); | |
1654 pos = strchr (t->values->str, *amp); | |
1655 } | |
9636 | 1656 else if (*x == '<') /* Because we're all WYSIWYG now, a '<' |
1657 * char should only appear as the start of a tag. Perhaps a safer (but costlier) | |
1658 * check would be to call gtk_imhtml_is_tag on it */ | |
10600 | 1659 break; |
8505 | 1660 else |
1661 pos = strchr (t->values->str, *x); | |
1662 | |
4032 | 1663 if (pos) |
7371 | 1664 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 1665 else |
1666 break; | |
1667 | |
1668 x++; len++; | |
1669 } | |
1670 | |
1671 if (t->image) | |
1672 return len; | |
1673 | |
1674 return 0; | |
1675 } | |
1676 | |
1677 void | |
4263 | 1678 gtk_imhtml_associate_smiley (GtkIMHtml *imhtml, |
10537 | 1679 const gchar *sml, |
4263 | 1680 GtkIMHtmlSmiley *smiley) |
4032 | 1681 { |
1682 GtkSmileyTree *tree; | |
1683 g_return_if_fail (imhtml != NULL); | |
1684 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
7371 | 1685 |
4032 | 1686 if (sml == NULL) |
1687 tree = imhtml->default_smilies; | |
1688 else if ((tree = g_hash_table_lookup(imhtml->smiley_data, sml))) { | |
1689 } else { | |
1690 tree = gtk_smiley_tree_new(); | |
4892 | 1691 g_hash_table_insert(imhtml->smiley_data, g_strdup(sml), tree); |
4032 | 1692 } |
1693 | |
4263 | 1694 gtk_smiley_tree_insert (tree, smiley); |
4032 | 1695 } |
1696 | |
1697 static gboolean | |
1698 gtk_imhtml_is_smiley (GtkIMHtml *imhtml, | |
1699 GSList *fonts, | |
1700 const gchar *text, | |
1701 gint *len) | |
1702 { | |
1703 GtkSmileyTree *tree; | |
5967 | 1704 GtkIMHtmlFontDetail *font; |
4032 | 1705 char *sml = NULL; |
1706 | |
1707 if (fonts) { | |
1708 font = fonts->data; | |
1709 sml = font->sml; | |
1710 } | |
1711 | |
9029 | 1712 if (!sml) |
1713 sml = imhtml->protocol_name; | |
1714 | |
1715 if (!sml || !(tree = g_hash_table_lookup(imhtml->smiley_data, sml))) | |
4032 | 1716 tree = imhtml->default_smilies; |
9029 | 1717 |
4032 | 1718 if (tree == NULL) |
1719 return FALSE; | |
7371 | 1720 |
8505 | 1721 *len = gtk_smiley_tree_lookup (tree, text); |
4032 | 1722 return (*len > 0); |
1723 } | |
1724 | |
10526 | 1725 GtkIMHtmlSmiley * |
1726 gtk_imhtml_smiley_get(GtkIMHtml *imhtml, | |
1727 const gchar *sml, | |
1728 const gchar *text) | |
4032 | 1729 { |
1730 GtkSmileyTree *t; | |
1731 const gchar *x = text; | |
1732 if (sml == NULL) | |
1733 t = imhtml->default_smilies; | |
7371 | 1734 else |
4032 | 1735 t = g_hash_table_lookup(imhtml->smiley_data, sml); |
7371 | 1736 |
4032 | 1737 |
1738 if (t == NULL) | |
10526 | 1739 return sml ? gtk_imhtml_smiley_get(imhtml, NULL, text) : NULL; |
4032 | 1740 |
1741 while (*x) { | |
1742 gchar *pos; | |
1743 | |
1744 if (!t->values) { | |
10526 | 1745 return sml ? gtk_imhtml_smiley_get(imhtml, NULL, text) : NULL; |
4032 | 1746 } |
7371 | 1747 |
4032 | 1748 pos = strchr (t->values->str, *x); |
1749 if (pos) { | |
7371 | 1750 t = t->children [GPOINTER_TO_INT(pos) - GPOINTER_TO_INT(t->values->str)]; |
4032 | 1751 } else { |
10526 | 1752 return sml ? gtk_imhtml_smiley_get(imhtml, NULL, text) : NULL; |
4032 | 1753 } |
1754 x++; | |
1755 } | |
1756 | |
10526 | 1757 return t->image; |
1758 } | |
1759 | |
1760 GdkPixbufAnimation * | |
1761 gtk_smiley_tree_image (GtkIMHtml *imhtml, | |
1762 const gchar *sml, | |
1763 const gchar *text) | |
1764 { | |
1765 | |
1766 GtkIMHtmlSmiley *smiley; | |
1767 | |
1768 smiley = gtk_imhtml_smiley_get(imhtml,sml,text); | |
1769 | |
1770 if (!smiley) | |
8890 | 1771 return NULL; |
1772 | |
10526 | 1773 if (!smiley->icon && smiley->file) { |
1774 smiley->icon = gdk_pixbuf_animation_new_from_file(smiley->file, NULL); | |
1775 } else if (!smiley->icon && smiley->loader) { | |
1776 smiley->icon = gdk_pixbuf_loader_get_animation(smiley->loader); | |
1777 if (smiley->icon) | |
1778 g_object_ref(G_OBJECT(smiley->icon)); | |
1779 } | |
1780 | |
1781 return smiley->icon; | |
4032 | 1782 } |
8890 | 1783 |
4793 | 1784 #define VALID_TAG(x) if (!g_ascii_strncasecmp (string, x ">", strlen (x ">"))) { \ |
3922 | 1785 *tag = g_strndup (string, strlen (x)); \ |
1786 *len = strlen (x) + 1; \ | |
1787 return TRUE; \ | |
1788 } \ | |
1789 (*type)++ | |
1428 | 1790 |
4793 | 1791 #define VALID_OPT_TAG(x) if (!g_ascii_strncasecmp (string, x " ", strlen (x " "))) { \ |
3922 | 1792 const gchar *c = string + strlen (x " "); \ |
1793 gchar e = '"'; \ | |
1794 gboolean quote = FALSE; \ | |
1795 while (*c) { \ | |
1796 if (*c == '"' || *c == '\'') { \ | |
1797 if (quote && (*c == e)) \ | |
1798 quote = !quote; \ | |
1799 else if (!quote) { \ | |
1800 quote = !quote; \ | |
1801 e = *c; \ | |
1802 } \ | |
1803 } else if (!quote && (*c == '>')) \ | |
1804 break; \ | |
1805 c++; \ | |
1806 } \ | |
1807 if (*c) { \ | |
1808 *tag = g_strndup (string, c - string); \ | |
1809 *len = c - string + 1; \ | |
1810 return TRUE; \ | |
1811 } \ | |
1812 } \ | |
1813 (*type)++ | |
1428 | 1814 |
1815 | |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1816 static gboolean |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1817 gtk_imhtml_is_amp_escape (const gchar *string, |
7280 | 1818 gchar **replace, |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1819 gint *length) |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1820 { |
7287 | 1821 static char buf[7]; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1822 g_return_val_if_fail (string != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1823 g_return_val_if_fail (replace != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1824 g_return_val_if_fail (length != NULL, FALSE); |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1825 |
4793 | 1826 if (!g_ascii_strncasecmp (string, "&", 5)) { |
7280 | 1827 *replace = "&"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1828 *length = 5; |
4793 | 1829 } else if (!g_ascii_strncasecmp (string, "<", 4)) { |
7280 | 1830 *replace = "<"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1831 *length = 4; |
4793 | 1832 } else if (!g_ascii_strncasecmp (string, ">", 4)) { |
7280 | 1833 *replace = ">"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1834 *length = 4; |
4793 | 1835 } else if (!g_ascii_strncasecmp (string, " ", 6)) { |
7280 | 1836 *replace = " "; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1837 *length = 6; |
4793 | 1838 } else if (!g_ascii_strncasecmp (string, "©", 6)) { |
7280 | 1839 *replace = "©"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1840 *length = 6; |
4793 | 1841 } else if (!g_ascii_strncasecmp (string, """, 6)) { |
7280 | 1842 *replace = "\""; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1843 *length = 6; |
4793 | 1844 } else if (!g_ascii_strncasecmp (string, "®", 5)) { |
7280 | 1845 *replace = "®"; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1846 *length = 5; |
5093 | 1847 } else if (!g_ascii_strncasecmp (string, "'", 6)) { |
7280 | 1848 *replace = "\'"; |
5093 | 1849 *length = 6; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1850 } else if (*(string + 1) == '#') { |
2022
199ba82faacb
[gaim-migrate @ 2032]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2002
diff
changeset
|
1851 guint pound = 0; |
3004 | 1852 if ((sscanf (string, "&#%u;", £) == 1) && pound != 0) { |
7287 | 1853 int buflen; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1854 if (*(string + 3 + (gint)log10 (pound)) != ';') |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1855 return FALSE; |
7287 | 1856 buflen = g_unichar_to_utf8((gunichar)pound, buf); |
1857 buf[buflen] = '\0'; | |
7280 | 1858 *replace = buf; |
1472
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1859 *length = 2; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1860 while (isdigit ((gint) string [*length])) (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1861 if (string [*length] == ';') (*length)++; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1862 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1863 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1864 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1865 } else { |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1866 return FALSE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1867 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1868 |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1869 return TRUE; |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1870 } |
be620a051d6d
[gaim-migrate @ 1482]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1463
diff
changeset
|
1871 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1872 static gboolean |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1873 gtk_imhtml_is_tag (const gchar *string, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1874 gchar **tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1875 gint *len, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1876 gint *type) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1877 { |
8061 | 1878 char *close; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1879 *type = 1; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1880 |
8118 | 1881 |
8061 | 1882 if (!(close = strchr (string, '>'))) |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1883 return FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1884 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1885 VALID_TAG ("B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1886 VALID_TAG ("BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1887 VALID_TAG ("/B"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1888 VALID_TAG ("/BOLD"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1889 VALID_TAG ("I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1890 VALID_TAG ("ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1891 VALID_TAG ("/I"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1892 VALID_TAG ("/ITALIC"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1893 VALID_TAG ("U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1894 VALID_TAG ("UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1895 VALID_TAG ("/U"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1896 VALID_TAG ("/UNDERLINE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1897 VALID_TAG ("S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1898 VALID_TAG ("STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1899 VALID_TAG ("/S"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1900 VALID_TAG ("/STRIKE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1901 VALID_TAG ("SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1902 VALID_TAG ("/SUB"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1903 VALID_TAG ("SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1904 VALID_TAG ("/SUP"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1905 VALID_TAG ("PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1906 VALID_TAG ("/PRE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1907 VALID_TAG ("TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1908 VALID_TAG ("/TITLE"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1909 VALID_TAG ("BR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1910 VALID_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1911 VALID_TAG ("/FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1912 VALID_TAG ("/A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1913 VALID_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1914 VALID_TAG ("/P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1915 VALID_TAG ("H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1916 VALID_TAG ("/H3"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1917 VALID_TAG ("HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1918 VALID_TAG ("/HTML"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1919 VALID_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1920 VALID_TAG ("/BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1921 VALID_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1922 VALID_TAG ("HEAD"); |
2993 | 1923 VALID_TAG ("/HEAD"); |
1924 VALID_TAG ("BINARY"); | |
1925 VALID_TAG ("/BINARY"); | |
5093 | 1926 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1927 VALID_OPT_TAG ("HR"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1928 VALID_OPT_TAG ("FONT"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1929 VALID_OPT_TAG ("BODY"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1930 VALID_OPT_TAG ("A"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1931 VALID_OPT_TAG ("IMG"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1932 VALID_OPT_TAG ("P"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1933 VALID_OPT_TAG ("H3"); |
5093 | 1934 VALID_OPT_TAG ("HTML"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1935 |
5101 | 1936 VALID_TAG ("CITE"); |
1937 VALID_TAG ("/CITE"); | |
1938 VALID_TAG ("EM"); | |
1939 VALID_TAG ("/EM"); | |
1940 VALID_TAG ("STRONG"); | |
1941 VALID_TAG ("/STRONG"); | |
1942 | |
5104 | 1943 VALID_OPT_TAG ("SPAN"); |
1944 VALID_TAG ("/SPAN"); | |
5174 | 1945 VALID_TAG ("BR/"); /* hack until gtkimhtml handles things better */ |
6982 | 1946 VALID_TAG ("IMG"); |
8026 | 1947 VALID_TAG("SPAN"); |
8061 | 1948 VALID_OPT_TAG("BR"); |
7988 | 1949 |
4793 | 1950 if (!g_ascii_strncasecmp(string, "!--", strlen ("!--"))) { |
2954
f6c4f2187c08
[gaim-migrate @ 2967]
Christian Hammond <chipx86@chipx86.com>
parents:
2898
diff
changeset
|
1951 gchar *e = strstr (string + strlen("!--"), "-->"); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1952 if (e) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1953 *len = e - string + strlen ("-->"); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1954 *tag = g_strndup (string + strlen ("!--"), *len - strlen ("!---->")); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1955 return TRUE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1956 } |
8118 | 1957 } |
1958 | |
8061 | 1959 *type = -1; |
1960 *len = close - string + 1; | |
1961 *tag = g_strndup(string, *len - 1); | |
1962 return TRUE; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1963 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1964 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1965 static gchar* |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1966 gtk_imhtml_get_html_opt (gchar *tag, |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1967 const gchar *opt) |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1968 { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1969 gchar *t = tag; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1970 gchar *e, *a; |
5177 | 1971 gchar *val; |
1972 gint len; | |
7280 | 1973 gchar *c; |
5177 | 1974 GString *ret; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1975 |
4793 | 1976 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1977 gboolean quote = FALSE; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1978 if (*t == '\0') break; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1979 while (*t && !((*t == ' ') && !quote)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1980 if (*t == '\"') |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1981 quote = ! quote; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1982 t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1983 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1984 while (*t && (*t == ' ')) t++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1985 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1986 |
4793 | 1987 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1988 t += strlen (opt); |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1989 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1990 return NULL; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1991 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1992 |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1993 if ((*t == '\"') || (*t == '\'')) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1994 e = a = ++t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1995 while (*e && (*e != *(t - 1))) e++; |
2993 | 1996 if (*e == '\0') { |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
1997 return NULL; |
5177 | 1998 } else |
1999 val = g_strndup(a, e - a); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2000 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2001 e = a = t; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2002 while (*e && !isspace ((gint) *e)) e++; |
5177 | 2003 val = g_strndup(a, e - a); |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2004 } |
5177 | 2005 |
2006 ret = g_string_new(""); | |
2007 e = val; | |
2008 while(*e) { | |
2009 if(gtk_imhtml_is_amp_escape(e, &c, &len)) { | |
7280 | 2010 ret = g_string_append(ret, c); |
5177 | 2011 e += len; |
2012 } else { | |
2013 ret = g_string_append_c(ret, *e); | |
2014 e++; | |
2015 } | |
2016 } | |
2017 | |
2018 g_free(val); | |
8568 | 2019 |
2020 return g_string_free(ret, FALSE); | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2021 } |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2022 |
8118 | 2023 /* Inline CSS Support - Douglas Thrift */ |
2024 static gchar* | |
2025 gtk_imhtml_get_css_opt (gchar *style, | |
2026 const gchar *opt) | |
2027 { | |
2028 gchar *t = style; | |
2029 gchar *e, *a; | |
2030 gchar *val; | |
2031 gint len; | |
2032 gchar *c; | |
2033 GString *ret; | |
2034 | |
2035 while (g_ascii_strncasecmp (t, opt, strlen (opt))) { | |
8177 | 2036 /* gboolean quote = FALSE; */ |
8118 | 2037 if (*t == '\0') break; |
2038 while (*t && !((*t == ' ') /*&& !quote*/)) { | |
2039 /* if (*t == '\"') | |
8177 | 2040 quote = ! quote; */ |
8118 | 2041 t++; |
2042 } | |
2043 while (*t && (*t == ' ')) t++; | |
2044 } | |
2045 | |
2046 if (!g_ascii_strncasecmp (t, opt, strlen (opt))) { | |
2047 t += strlen (opt); | |
10457 | 2048 while (*t && (*t == ' ')) t++; |
2049 if (!*t) | |
2050 return NULL; | |
8118 | 2051 } else { |
2052 return NULL; | |
2053 } | |
2054 | |
2055 /* if ((*t == '\"') || (*t == '\'')) { | |
2056 e = a = ++t; | |
2057 while (*e && (*e != *(t - 1))) e++; | |
2058 if (*e == '\0') { | |
2059 return NULL; | |
2060 } else | |
2061 val = g_strndup(a, e - a); | |
2062 } else { | |
2063 e = a = t; | |
2064 while (*e && !isspace ((gint) *e)) e++; | |
2065 val = g_strndup(a, e - a); | |
2066 }*/ | |
2067 | |
2068 e = a = t; | |
2069 while (*e && *e != ';') e++; | |
2070 val = g_strndup(a, e - a); | |
2071 | |
2072 ret = g_string_new(""); | |
2073 e = val; | |
2074 while(*e) { | |
2075 if(gtk_imhtml_is_amp_escape(e, &c, &len)) { | |
2076 ret = g_string_append(ret, c); | |
2077 e += len; | |
2078 } else { | |
2079 ret = g_string_append_c(ret, *e); | |
2080 e++; | |
2081 } | |
2082 } | |
2083 | |
2084 g_free(val); | |
2085 val = ret->str; | |
2086 g_string_free(ret, FALSE); | |
2087 return val; | |
2088 } | |
3922 | 2089 |
8334 | 2090 static const char *accepted_protocols[] = { |
2091 "http://", | |
2092 "https://", | |
2093 "ftp://" | |
2094 }; | |
2095 | |
2096 static const int accepted_protocols_size = 3; | |
2097 | |
2098 /* returns if the beginning of the text is a protocol. If it is the protocol, returns the length so | |
2099 the caller knows how long the protocol string is. */ | |
2100 int gtk_imhtml_is_protocol(const char *text) | |
2101 { | |
2102 gint i; | |
2103 | |
2104 for(i=0; i<accepted_protocols_size; i++){ | |
2105 if( strncasecmp(text, accepted_protocols[i], strlen(accepted_protocols[i])) == 0 ){ | |
2106 return strlen(accepted_protocols[i]); | |
2107 } | |
2108 } | |
2109 return 0; | |
2110 } | |
2111 | |
8677 | 2112 /* |
2113 <KingAnt> marv: The two IM image functions in oscar are gaim_odc_send_im and gaim_odc_incoming | |
2114 | |
2115 | |
2116 [19:58] <Robot101> marv: images go into the imgstore, a refcounted... well.. hash. :) | |
2117 [19:59] <KingAnt> marv: I think the image tag used by the core is something like <img id="#"/> | |
2118 [19:59] Ro0tSiEgE robert42 RobFlynn Robot101 ross22 roz | |
2119 [20:00] <KingAnt> marv: Where the ID is the what is returned when you add the image to the imgstore using gaim_imgstore_add | |
2120 [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? | |
2121 [20:00] <KingAnt> marv: Right | |
2122 [20:00] <marv> alright | |
2123 | |
2124 Here's my plan with IMImages. make gtk_imhtml_[append|insert]_text_with_images instead just | |
2125 gtkimhtml_[append|insert]_text (hrm maybe it should be called html instead of text), add a | |
2126 function for gaim to register for look up images, i.e. gtk_imhtml_set_get_img_fnc, so that | |
2127 images can be looked up like that, instead of passing a GSList of them. | |
2128 */ | |
2129 | |
2130 void gtk_imhtml_append_text_with_images (GtkIMHtml *imhtml, | |
2131 const gchar *text, | |
2132 GtkIMHtmlOptions options, | |
2133 GSList *unused) | |
1428 | 2134 { |
8677 | 2135 GtkTextIter iter, ins, sel; |
2136 GdkRectangle rect; | |
2137 int y, height, ins_offset = 0, sel_offset = 0; | |
2138 gboolean fixins = FALSE, fixsel = FALSE; | |
2139 | |
2140 g_return_if_fail (imhtml != NULL); | |
2141 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
2142 g_return_if_fail (text != NULL); | |
2143 | |
2144 | |
2145 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &iter); | |
2146 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &ins, gtk_text_buffer_get_insert(imhtml->text_buffer)); | |
2147 if (gtk_text_iter_equal(&iter, &ins) && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) { | |
2148 fixins = TRUE; | |
2149 ins_offset = gtk_text_iter_get_offset(&ins); | |
2150 } | |
2151 | |
2152 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &sel, gtk_text_buffer_get_selection_bound(imhtml->text_buffer)); | |
2153 if (gtk_text_iter_equal(&iter, &sel) && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) { | |
2154 fixsel = TRUE; | |
2155 sel_offset = gtk_text_iter_get_offset(&sel); | |
2156 } | |
2157 | |
2158 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
2159 gtk_text_view_get_line_yrange(GTK_TEXT_VIEW(imhtml), &iter, &y, &height); | |
2160 | |
2161 | |
2162 if(((y + height) - (rect.y + rect.height)) > height | |
2163 && gtk_text_buffer_get_char_count(imhtml->text_buffer)){ | |
2164 options |= GTK_IMHTML_NO_SCROLL; | |
2165 } | |
2166 | |
2167 gtk_imhtml_insert_html_at_iter(imhtml, text, options, &iter); | |
2168 | |
2169 if (fixins) { | |
2170 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &ins, ins_offset); | |
2171 gtk_text_buffer_move_mark(imhtml->text_buffer, gtk_text_buffer_get_insert(imhtml->text_buffer), &ins); | |
2172 } | |
2173 | |
2174 if (fixsel) { | |
2175 gtk_text_buffer_get_iter_at_offset(imhtml->text_buffer, &sel, sel_offset); | |
2176 gtk_text_buffer_move_mark(imhtml->text_buffer, gtk_text_buffer_get_selection_bound(imhtml->text_buffer), &sel); | |
2177 } | |
2178 | |
2179 if (!(options & GTK_IMHTML_NO_SCROLL)) { | |
8729 | 2180 gtk_imhtml_scroll_to_end(imhtml); |
8677 | 2181 } |
2182 } | |
2183 | |
10826 | 2184 #define MAX_SCROLL_TIME 0.4 |
10798 | 2185 |
2186 gboolean scroll_cb(gpointer data) | |
2187 { | |
2188 GtkIMHtml *imhtml = data; | |
2189 GtkAdjustment *adj = GTK_TEXT_VIEW(imhtml)->vadjustment; | |
2190 gdouble val = adj->upper - adj->page_size; | |
2191 gdouble t; | |
2192 | |
2193 t = g_timer_elapsed(imhtml->scroll_time, NULL); | |
2194 | |
2195 if (adj->value >= val || t >= MAX_SCROLL_TIME) { | |
2196 gaim_debug_info("gtkimhtml", "scroll_cb: out of time\n"); | |
2197 gtk_adjustment_set_value(adj, val); | |
2198 } else { | |
2199 gtk_adjustment_set_value(adj, | |
2200 adj->value + (((val - adj->value) * t ) / MAX_SCROLL_TIME)); | |
2201 } | |
2202 | |
2203 if (adj->value >= val) { | |
2204 g_timer_destroy(imhtml->scroll_time); | |
2205 imhtml->scroll_time = NULL; | |
2206 return FALSE; | |
2207 } else | |
2208 return TRUE; | |
2209 } | |
2210 | |
10797 | 2211 gboolean scroll_idle_cb(gpointer data) |
2212 { | |
10798 | 2213 GtkIMHtml *imhtml = data; |
2214 imhtml->scroll_src = g_timeout_add(33, scroll_cb, imhtml); | |
10797 | 2215 return FALSE; |
2216 } | |
2217 | |
8729 | 2218 void gtk_imhtml_scroll_to_end(GtkIMHtml *imhtml) |
2219 { | |
10798 | 2220 if (imhtml->scroll_time) |
2221 g_timer_destroy(imhtml->scroll_time); | |
2222 imhtml->scroll_time = g_timer_new(); | |
2223 if (imhtml->scroll_src) | |
2224 g_source_remove(imhtml->scroll_src); | |
2225 imhtml->scroll_src = g_idle_add_full(G_PRIORITY_LOW, scroll_idle_cb, imhtml, NULL); | |
8729 | 2226 } |
2227 | |
8677 | 2228 void gtk_imhtml_insert_html_at_iter(GtkIMHtml *imhtml, |
2229 const gchar *text, | |
2230 GtkIMHtmlOptions options, | |
2231 GtkTextIter *iter) | |
2232 { | |
8061 | 2233 GdkRectangle rect; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2234 gint pos = 0; |
3922 | 2235 gchar *ws; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2236 gchar *tag; |
3922 | 2237 gchar *bg = NULL; |
6982 | 2238 gint len; |
4032 | 2239 gint tlen, smilelen, wpos=0; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2240 gint type; |
3922 | 2241 const gchar *c; |
7280 | 2242 gchar *amp; |
8334 | 2243 gint len_protocol; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2244 |
1428 | 2245 guint bold = 0, |
2246 italics = 0, | |
2247 underline = 0, | |
2248 strike = 0, | |
2249 sub = 0, | |
2250 sup = 0, | |
1691
d802b115800f
[gaim-migrate @ 1701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1647
diff
changeset
|
2251 title = 0, |
8061 | 2252 pre = 0; |
1428 | 2253 |
10217 | 2254 gboolean br = FALSE; |
2255 | |
3922 | 2256 GSList *fonts = NULL; |
8506 | 2257 GObject *object; |
8061 | 2258 GtkIMHtmlScalable *scalable = NULL; |
8677 | 2259 |
2260 g_return_if_fail (imhtml != NULL); | |
2261 g_return_if_fail (GTK_IS_IMHTML (imhtml)); | |
2262 g_return_if_fail (text != NULL); | |
3922 | 2263 c = text; |
6982 | 2264 len = strlen(text); |
3922 | 2265 ws = g_malloc(len + 1); |
2266 ws[0] = 0; | |
1428 | 2267 |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2268 while (pos < len) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2269 if (*c == '<' && gtk_imhtml_is_tag (c + 1, &tag, &tlen, &type)) { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2270 c++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2271 pos++; |
8061 | 2272 ws[wpos] = '\0'; |
10217 | 2273 br = FALSE; |
8061 | 2274 switch (type) |
3922 | 2275 { |
2276 case 1: /* B */ | |
2277 case 2: /* BOLD */ | |
5101 | 2278 case 54: /* STRONG */ |
8677 | 2279 |
2280 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
2281 | |
2282 if ((bold == 0) && (imhtml->format_functions & GTK_IMHTML_BOLD)) | |
8061 | 2283 gtk_imhtml_toggle_bold(imhtml); |
3922 | 2284 bold++; |
8061 | 2285 ws[0] = '\0'; wpos = 0; |
3922 | 2286 break; |
2287 case 3: /* /B */ | |
2288 case 4: /* /BOLD */ | |
5101 | 2289 case 55: /* /STRONG */ |
8677 | 2290 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2291 ws[0] = '\0'; wpos = 0; |
2292 | |
3922 | 2293 if (bold) |
2294 bold--; | |
8677 | 2295 if ((bold == 0) && (imhtml->format_functions & GTK_IMHTML_BOLD) && !imhtml->wbfo) |
8061 | 2296 gtk_imhtml_toggle_bold(imhtml); |
3922 | 2297 break; |
2298 case 5: /* I */ | |
2299 case 6: /* ITALIC */ | |
5101 | 2300 case 52: /* EM */ |
8677 | 2301 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2302 ws[0] = '\0'; wpos = 0; |
8677 | 2303 if ((italics == 0) && (imhtml->format_functions & GTK_IMHTML_ITALIC)) |
8061 | 2304 gtk_imhtml_toggle_italic(imhtml); |
3922 | 2305 italics++; |
2306 break; | |
2307 case 7: /* /I */ | |
2308 case 8: /* /ITALIC */ | |
5101 | 2309 case 53: /* /EM */ |
8677 | 2310 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2311 ws[0] = '\0'; wpos = 0; |
3922 | 2312 if (italics) |
2313 italics--; | |
8677 | 2314 if ((italics == 0) && (imhtml->format_functions & GTK_IMHTML_ITALIC) && !imhtml->wbfo) |
8061 | 2315 gtk_imhtml_toggle_italic(imhtml); |
3922 | 2316 break; |
2317 case 9: /* U */ | |
2318 case 10: /* UNDERLINE */ | |
8677 | 2319 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2320 ws[0] = '\0'; wpos = 0; |
8677 | 2321 if ((underline == 0) && (imhtml->format_functions & GTK_IMHTML_UNDERLINE)) |
8061 | 2322 gtk_imhtml_toggle_underline(imhtml); |
3922 | 2323 underline++; |
2324 break; | |
2325 case 11: /* /U */ | |
2326 case 12: /* /UNDERLINE */ | |
8677 | 2327 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2328 ws[0] = '\0'; wpos = 0; |
3922 | 2329 if (underline) |
2330 underline--; | |
8677 | 2331 if ((underline == 0) && (imhtml->format_functions & GTK_IMHTML_UNDERLINE) && !imhtml->wbfo) |
8061 | 2332 gtk_imhtml_toggle_underline(imhtml); |
3922 | 2333 break; |
2334 case 13: /* S */ | |
2335 case 14: /* STRIKE */ | |
9924 | 2336 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2337 ws[0] = '\0'; wpos = 0; | |
2338 if ((strike == 0) && (imhtml->format_functions & GTK_IMHTML_STRIKE)) | |
2339 gtk_imhtml_toggle_strike(imhtml); | |
3922 | 2340 strike++; |
2341 break; | |
2342 case 15: /* /S */ | |
2343 case 16: /* /STRIKE */ | |
9924 | 2344 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2345 ws[0] = '\0'; wpos = 0; | |
3922 | 2346 if (strike) |
2347 strike--; | |
9924 | 2348 if ((strike == 0) && (imhtml->format_functions & GTK_IMHTML_STRIKE) && !imhtml->wbfo) |
2349 gtk_imhtml_toggle_strike(imhtml); | |
3922 | 2350 break; |
2351 case 17: /* SUB */ | |
8677 | 2352 /* FIXME: reimpliment this */ |
3922 | 2353 sub++; |
2354 break; | |
2355 case 18: /* /SUB */ | |
8677 | 2356 /* FIXME: reimpliment this */ |
3922 | 2357 if (sub) |
2358 sub--; | |
2359 break; | |
2360 case 19: /* SUP */ | |
8677 | 2361 /* FIXME: reimplement this */ |
3922 | 2362 sup++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2363 break; |
3922 | 2364 case 20: /* /SUP */ |
8677 | 2365 /* FIXME: reimplement this */ |
3922 | 2366 if (sup) |
2367 sup--; | |
2368 break; | |
2369 case 21: /* PRE */ | |
8677 | 2370 /* FIXME: reimplement this */ |
3922 | 2371 pre++; |
2372 break; | |
2373 case 22: /* /PRE */ | |
8677 | 2374 /* FIXME: reimplement this */ |
3922 | 2375 if (pre) |
2376 pre--; | |
2377 break; | |
2378 case 23: /* TITLE */ | |
8677 | 2379 /* FIXME: what was this supposed to do anyway? */ |
3922 | 2380 title++; |
2381 break; | |
2382 case 24: /* /TITLE */ | |
8677 | 2383 /* FIXME: make this undo whatever 23 was supposed to do */ |
3922 | 2384 if (title) { |
2385 if (options & GTK_IMHTML_NO_TITLE) { | |
2386 wpos = 0; | |
2387 ws [wpos] = '\0'; | |
2388 } | |
2389 title--; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2390 } |
3922 | 2391 break; |
2392 case 25: /* BR */ | |
5174 | 2393 case 58: /* BR/ */ |
8061 | 2394 case 61: /* BR (opt) */ |
3922 | 2395 ws[wpos] = '\n'; |
2396 wpos++; | |
10217 | 2397 br = TRUE; |
6982 | 2398 break; |
3922 | 2399 case 26: /* HR */ |
2400 case 42: /* HR (opt) */ | |
8726 | 2401 { |
2402 int minus; | |
2403 | |
3922 | 2404 ws[wpos++] = '\n'; |
8677 | 2405 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2406 | |
5967 | 2407 scalable = gtk_imhtml_hr_new(); |
8061 | 2408 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
8677 | 2409 scalable->add_to(scalable, imhtml, iter); |
8726 | 2410 minus = gtk_text_view_get_left_margin(GTK_TEXT_VIEW(imhtml)) + |
2411 gtk_text_view_get_right_margin(GTK_TEXT_VIEW(imhtml)); | |
2412 scalable->scale(scalable, rect.width - minus, rect.height); | |
8061 | 2413 imhtml->scalables = g_list_append(imhtml->scalables, scalable); |
2414 ws[0] = '\0'; wpos = 0; | |
7942 | 2415 ws[wpos++] = '\n'; |
8061 | 2416 |
3922 | 2417 break; |
8726 | 2418 } |
3922 | 2419 case 27: /* /FONT */ |
8677 | 2420 if (fonts && !imhtml->wbfo) { |
5967 | 2421 GtkIMHtmlFontDetail *font = fonts->data; |
8677 | 2422 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2423 ws[0] = '\0'; wpos = 0; |
8177 | 2424 /* NEW_BIT (NEW_TEXT_BIT); */ |
8677 | 2425 |
8698 | 2426 if (font->face && (imhtml->format_functions & GTK_IMHTML_FACE)) { |
8061 | 2427 gtk_imhtml_toggle_fontface(imhtml, NULL); |
3922 | 2428 g_free (font->face); |
8061 | 2429 } |
8698 | 2430 if (font->fore && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) { |
8061 | 2431 gtk_imhtml_toggle_forecolor(imhtml, NULL); |
3922 | 2432 g_free (font->fore); |
8061 | 2433 } |
8698 | 2434 if (font->back && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) { |
8061 | 2435 gtk_imhtml_toggle_backcolor(imhtml, NULL); |
3922 | 2436 g_free (font->back); |
8061 | 2437 } |
4032 | 2438 if (font->sml) |
2439 g_free (font->sml); | |
8309 | 2440 |
8698 | 2441 if ((font->size != 3) && (imhtml->format_functions & (GTK_IMHTML_GROW|GTK_IMHTML_SHRINK))) |
8309 | 2442 gtk_imhtml_font_set_size(imhtml, 3); |
2443 | |
10761 | 2444 |
2445 fonts = g_slist_remove (fonts, font); | |
9245 | 2446 g_free(font); |
2447 | |
8309 | 2448 if (fonts) { |
2449 GtkIMHtmlFontDetail *font = fonts->data; | |
8677 | 2450 |
8698 | 2451 if (font->face && (imhtml->format_functions & GTK_IMHTML_FACE)) |
8309 | 2452 gtk_imhtml_toggle_fontface(imhtml, font->face); |
8698 | 2453 if (font->fore && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) |
8309 | 2454 gtk_imhtml_toggle_forecolor(imhtml, font->fore); |
8698 | 2455 if (font->back && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) |
8309 | 2456 gtk_imhtml_toggle_backcolor(imhtml, font->back); |
8698 | 2457 if ((font->size != 3) && (imhtml->format_functions & (GTK_IMHTML_GROW|GTK_IMHTML_SHRINK))) |
8309 | 2458 gtk_imhtml_font_set_size(imhtml, font->size); |
2459 } | |
3922 | 2460 } |
8309 | 2461 break; |
3922 | 2462 case 28: /* /A */ |
8677 | 2463 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2464 gtk_imhtml_toggle_link(imhtml, NULL); | |
2465 ws[0] = '\0'; wpos = 0; | |
8061 | 2466 break; |
8118 | 2467 |
3922 | 2468 case 29: /* P */ |
2469 case 30: /* /P */ | |
2470 case 31: /* H3 */ | |
2471 case 32: /* /H3 */ | |
2472 case 33: /* HTML */ | |
2473 case 34: /* /HTML */ | |
2474 case 35: /* BODY */ | |
10776 | 2475 break; |
3922 | 2476 case 36: /* /BODY */ |
10786 | 2477 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2478 ws[0] = '\0'; wpos = 0; | |
10776 | 2479 gtk_imhtml_toggle_background(imhtml, NULL); |
2480 break; | |
3922 | 2481 case 37: /* FONT */ |
2482 case 38: /* HEAD */ | |
2483 case 39: /* /HEAD */ | |
6982 | 2484 case 40: /* BINARY */ |
2485 case 41: /* /BINARY */ | |
3922 | 2486 break; |
2487 case 43: /* FONT (opt) */ | |
2488 { | |
4032 | 2489 gchar *color, *back, *face, *size, *sml; |
5967 | 2490 GtkIMHtmlFontDetail *font, *oldfont = NULL; |
3922 | 2491 color = gtk_imhtml_get_html_opt (tag, "COLOR="); |
2492 back = gtk_imhtml_get_html_opt (tag, "BACK="); | |
2493 face = gtk_imhtml_get_html_opt (tag, "FACE="); | |
2494 size = gtk_imhtml_get_html_opt (tag, "SIZE="); | |
4032 | 2495 sml = gtk_imhtml_get_html_opt (tag, "SML="); |
2496 if (!(color || back || face || size || sml)) | |
3922 | 2497 break; |
8061 | 2498 |
8677 | 2499 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2500 ws[0] = '\0'; wpos = 0; |
2501 | |
5967 | 2502 font = g_new0 (GtkIMHtmlFontDetail, 1); |
3922 | 2503 if (fonts) |
2504 oldfont = fonts->data; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2505 |
8677 | 2506 if (color && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) { |
3922 | 2507 font->fore = color; |
8061 | 2508 gtk_imhtml_toggle_forecolor(imhtml, font->fore); |
8677 | 2509 } |
8309 | 2510 //else if (oldfont && oldfont->fore) |
2511 // font->fore = g_strdup(oldfont->fore); | |
8677 | 2512 |
2513 if (back && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) { | |
3922 | 2514 font->back = back; |
8061 | 2515 gtk_imhtml_toggle_backcolor(imhtml, font->back); |
8309 | 2516 } |
2517 //else if (oldfont && oldfont->back) | |
2518 // font->back = g_strdup(oldfont->back); | |
8677 | 2519 |
2520 if (face && !(options & GTK_IMHTML_NO_FONTS) && (imhtml->format_functions & GTK_IMHTML_FACE)) { | |
3922 | 2521 font->face = face; |
8061 | 2522 gtk_imhtml_toggle_fontface(imhtml, font->face); |
8309 | 2523 } |
2524 //else if (oldfont && oldfont->face) | |
2525 // font->face = g_strdup(oldfont->face); | |
4032 | 2526 |
2527 if (sml) | |
2528 font->sml = sml; | |
2529 else if (oldfont && oldfont->sml) | |
2530 font->sml = g_strdup(oldfont->sml); | |
2531 | |
8677 | 2532 if (size && !(options & GTK_IMHTML_NO_SIZES) && (imhtml->format_functions & (GTK_IMHTML_GROW|GTK_IMHTML_SHRINK))) { |
3922 | 2533 if (*size == '+') { |
2534 sscanf (size + 1, "%hd", &font->size); | |
2535 font->size += 3; | |
2536 } else if (*size == '-') { | |
2537 sscanf (size + 1, "%hd", &font->size); | |
2538 font->size = MAX (0, 3 - font->size); | |
2539 } else if (isdigit (*size)) { | |
2540 sscanf (size, "%hd", &font->size); | |
8061 | 2541 } |
6042 | 2542 if (font->size > 100) |
2543 font->size = 100; | |
3922 | 2544 } else if (oldfont) |
2545 font->size = oldfont->size; | |
8309 | 2546 else |
2547 font->size = 3; | |
8698 | 2548 if ((imhtml->format_functions & (GTK_IMHTML_GROW|GTK_IMHTML_SHRINK))) |
2549 gtk_imhtml_font_set_size(imhtml, font->size); | |
3922 | 2550 g_free(size); |
2551 fonts = g_slist_prepend (fonts, font); | |
2552 } | |
2553 break; | |
2554 case 44: /* BODY (opt) */ | |
2555 if (!(options & GTK_IMHTML_NO_COLOURS)) { | |
2556 char *bgcolor = gtk_imhtml_get_html_opt (tag, "BGCOLOR="); | |
8677 | 2557 if (bgcolor && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) { |
2558 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8061 | 2559 ws[0] = '\0'; wpos = 0; |
8177 | 2560 /* NEW_BIT(NEW_TEXT_BIT); */ |
3922 | 2561 if (bg) |
2562 g_free(bg); | |
2563 bg = bgcolor; | |
10776 | 2564 gtk_imhtml_toggle_background(imhtml, bg); |
2885
f72efa29c109
[gaim-migrate @ 2898]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2871
diff
changeset
|
2565 } |
1428 | 2566 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2567 break; |
3922 | 2568 case 45: /* A (opt) */ |
2569 { | |
2570 gchar *href = gtk_imhtml_get_html_opt (tag, "HREF="); | |
8677 | 2571 if (href && (imhtml->format_functions & GTK_IMHTML_LINK)) { |
2572 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8061 | 2573 ws[0] = '\0'; wpos = 0; |
8677 | 2574 gtk_imhtml_toggle_link(imhtml, href); |
3922 | 2575 } |
10504 | 2576 if (href) |
2577 g_free(href); | |
2993 | 2578 } |
3922 | 2579 break; |
4895 | 2580 case 46: /* IMG (opt) */ |
6982 | 2581 case 59: /* IMG */ |
4895 | 2582 { |
8962 | 2583 const char *id; |
2584 | |
2585 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
2586 ws[0] = '\0'; wpos = 0; | |
4895 | 2587 |
8677 | 2588 if (!(imhtml->format_functions & GTK_IMHTML_IMAGE)) |
2589 break; | |
2590 | |
8962 | 2591 id = gtk_imhtml_get_html_opt(tag, "ID="); |
9186 | 2592 if (!id) |
2593 break; | |
8962 | 2594 gtk_imhtml_insert_image_at_iter(imhtml, atoi(id), iter); |
2595 break; | |
4895 | 2596 } |
3922 | 2597 case 47: /* P (opt) */ |
2598 case 48: /* H3 (opt) */ | |
5093 | 2599 case 49: /* HTML (opt) */ |
5101 | 2600 case 50: /* CITE */ |
2601 case 51: /* /CITE */ | |
8026 | 2602 case 56: /* SPAN (opt) */ |
8118 | 2603 /* Inline CSS Support - Douglas Thrift |
2604 * | |
2605 * color | |
8686 | 2606 * background |
8118 | 2607 * font-family |
2608 * font-size | |
8686 | 2609 * text-decoration: underline |
10483 | 2610 * |
2611 * TODO: | |
2612 * background-color | |
2613 * font-style | |
2614 * font-weight | |
8118 | 2615 */ |
2616 { | |
8686 | 2617 gchar *style, *color, *background, *family, *size; |
2618 gchar *textdec; | |
8118 | 2619 GtkIMHtmlFontDetail *font, *oldfont = NULL; |
2620 style = gtk_imhtml_get_html_opt (tag, "style="); | |
2621 | |
2622 if (!style) break; | |
2623 | |
10457 | 2624 color = gtk_imhtml_get_css_opt (style, "color:"); |
2625 background = gtk_imhtml_get_css_opt (style, "background:"); | |
8118 | 2626 family = gtk_imhtml_get_css_opt (style, |
10457 | 2627 "font-family:"); |
2628 size = gtk_imhtml_get_css_opt (style, "font-size:"); | |
2629 textdec = gtk_imhtml_get_css_opt (style, "text-decoration:"); | |
8686 | 2630 |
2631 if (!(color || family || size || background || textdec)) { | |
8120 | 2632 g_free(style); |
2633 break; | |
2634 } | |
8118 | 2635 |
8677 | 2636 |
2637 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
8118 | 2638 ws[0] = '\0'; wpos = 0; |
8177 | 2639 /* NEW_BIT (NEW_TEXT_BIT); */ |
8118 | 2640 |
2641 font = g_new0 (GtkIMHtmlFontDetail, 1); | |
2642 if (fonts) | |
2643 oldfont = fonts->data; | |
2644 | |
8677 | 2645 if (color && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_FORECOLOR)) |
8686 | 2646 { |
8118 | 2647 font->fore = color; |
8686 | 2648 gtk_imhtml_toggle_forecolor(imhtml, font->fore); |
2649 } | |
8118 | 2650 else if (oldfont && oldfont->fore) |
2651 font->fore = g_strdup(oldfont->fore); | |
2652 | |
8686 | 2653 if (background && !(options & GTK_IMHTML_NO_COLOURS) && (imhtml->format_functions & GTK_IMHTML_BACKCOLOR)) |
2654 { | |
2655 font->back = background; | |
2656 gtk_imhtml_toggle_backcolor(imhtml, font->back); | |
2657 } | |
2658 else if (oldfont && oldfont->back) | |
8118 | 2659 font->back = g_strdup(oldfont->back); |
2660 | |
8677 | 2661 if (family && !(options & GTK_IMHTML_NO_FONTS) && (imhtml->format_functions & GTK_IMHTML_FACE)) |
8686 | 2662 { |
8118 | 2663 font->face = family; |
8686 | 2664 gtk_imhtml_toggle_fontface(imhtml, font->face); |
2665 } | |
8118 | 2666 else if (oldfont && oldfont->face) |
2667 font->face = g_strdup(oldfont->face); | |
2668 if (font->face && (atoi(font->face) > 100)) { | |
8677 | 2669 /* WTF is this? */ |
9696 | 2670 /* Maybe it sets a max size on the font face? I seem to |
2671 * remember bad things happening if the font size was | |
2672 * 2 billion */ | |
8118 | 2673 g_free(font->face); |
2674 font->face = g_strdup("100"); | |
2675 } | |
2676 | |
2677 if (oldfont && oldfont->sml) | |
2678 font->sml = g_strdup(oldfont->sml); | |
2679 | |
8677 | 2680 if (size && !(options & GTK_IMHTML_NO_SIZES) && (imhtml->format_functions & (GTK_IMHTML_SHRINK|GTK_IMHTML_GROW))) { |
8686 | 2681 if (g_ascii_strcasecmp(size, "xx-small") == 0) |
2682 font->size = 1; | |
2683 else if (g_ascii_strcasecmp(size, "smaller") == 0 | |
2684 || g_ascii_strcasecmp(size, "x-small") == 0) | |
8118 | 2685 font->size = 2; |
8686 | 2686 else if (g_ascii_strcasecmp(size, "larger") == 0 |
2687 || g_ascii_strcasecmp(size, "medium") == 0) | |
8118 | 2688 font->size = 4; |
8686 | 2689 else if (g_ascii_strcasecmp(size, "large") == 0) |
2690 font->size = 5; | |
2691 else if (g_ascii_strcasecmp(size, "x-large") == 0) | |
2692 font->size = 6; | |
2693 else if (g_ascii_strcasecmp(size, "xx-large") == 0) | |
2694 font->size = 7; | |
8118 | 2695 else |
2696 font->size = 3; | |
8686 | 2697 gtk_imhtml_font_set_size(imhtml, font->size); |
2698 } | |
2699 else if (oldfont) | |
2700 { | |
2701 font->size = oldfont->size; | |
2702 } | |
2703 | |
2704 if (oldfont) | |
2705 { | |
2706 font->underline = oldfont->underline; | |
2707 } | |
2708 if (textdec && font->underline != 1 | |
9025 | 2709 && g_ascii_strcasecmp(textdec, "underline") == 0 |
8686 | 2710 && (imhtml->format_functions & GTK_IMHTML_UNDERLINE)) |
2711 { | |
2712 gtk_imhtml_toggle_underline(imhtml); | |
2713 font->underline = 1; | |
2714 } | |
8118 | 2715 |
2716 g_free(style); | |
2717 g_free(size); | |
2718 fonts = g_slist_prepend (fonts, font); | |
2719 } | |
2720 break; | |
5104 | 2721 case 57: /* /SPAN */ |
8118 | 2722 /* Inline CSS Support - Douglas Thrift */ |
8677 | 2723 if (fonts && !imhtml->wbfo) { |
8686 | 2724 GtkIMHtmlFontDetail *oldfont = NULL; |
8118 | 2725 GtkIMHtmlFontDetail *font = fonts->data; |
8677 | 2726 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8118 | 2727 ws[0] = '\0'; wpos = 0; |
8177 | 2728 /* NEW_BIT (NEW_TEXT_BIT); */ |
8118 | 2729 fonts = g_slist_remove (fonts, font); |
8692 | 2730 if (fonts) |
2731 oldfont = fonts->data; | |
2732 | |
2733 if (!oldfont) { | |
2734 gtk_imhtml_font_set_size(imhtml, 3); | |
2735 if (font->underline) | |
2736 gtk_imhtml_toggle_underline(imhtml); | |
2737 gtk_imhtml_toggle_fontface(imhtml, NULL); | |
2738 gtk_imhtml_toggle_forecolor(imhtml, NULL); | |
2739 gtk_imhtml_toggle_backcolor(imhtml, NULL); | |
8686 | 2740 } |
8692 | 2741 else |
8686 | 2742 { |
8692 | 2743 |
2744 if (font->size != oldfont->size) | |
2745 gtk_imhtml_font_set_size(imhtml, oldfont->size); | |
2746 | |
2747 if (font->underline != oldfont->underline) | |
2748 gtk_imhtml_toggle_underline(imhtml); | |
2749 | |
9286 | 2750 if (font->face && (!oldfont->face || strcmp(font->face, oldfont->face) != 0)) |
8692 | 2751 gtk_imhtml_toggle_fontface(imhtml, oldfont->face); |
2752 | |
9286 | 2753 if (font->fore && (!oldfont->fore || strcmp(font->fore, oldfont->fore) != 0)) |
8692 | 2754 gtk_imhtml_toggle_forecolor(imhtml, oldfont->fore); |
2755 | |
9286 | 2756 if (font->back && (!oldfont->back || strcmp(font->back, oldfont->back) != 0)) |
8692 | 2757 gtk_imhtml_toggle_backcolor(imhtml, oldfont->back); |
8686 | 2758 } |
8692 | 2759 |
2760 g_free (font->face); | |
2761 g_free (font->fore); | |
2762 g_free (font->back); | |
2763 g_free (font->sml); | |
2764 | |
8118 | 2765 g_free (font); |
2766 } | |
2767 break; | |
8026 | 2768 case 60: /* SPAN */ |
2993 | 2769 break; |
8061 | 2770 case 62: /* comment */ |
8177 | 2771 /* NEW_BIT (NEW_TEXT_BIT); */ |
8317 | 2772 ws[wpos] = '\0'; |
9465 | 2773 |
8677 | 2774 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2775 | |
10815 | 2776 if (imhtml->show_comments && !(options & GTK_IMHTML_NO_COMMENTS)) { |
6124 | 2777 wpos = g_snprintf (ws, len, "%s", tag); |
10815 | 2778 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
2779 } | |
2780 ws[0] = '\0'; wpos = 0; | |
2781 | |
8177 | 2782 /* NEW_BIT (NEW_COMMENT_BIT); */ |
3922 | 2783 break; |
2784 default: | |
6882 | 2785 break; |
2993 | 2786 } |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2787 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2788 pos += tlen; |
4138 | 2789 if(tag) |
2790 g_free(tag); /* This was allocated back in VALID_TAG() */ | |
9029 | 2791 } else if (gtk_imhtml_is_smiley(imhtml, fonts, c, &smilelen)) { |
8473 | 2792 GtkIMHtmlFontDetail *fd; |
2793 | |
2794 gchar *sml = NULL; | |
2795 if (fonts) { | |
2796 fd = fonts->data; | |
2797 sml = fd->sml; | |
2798 } | |
9029 | 2799 if (!sml) |
2800 sml = imhtml->protocol_name; | |
2801 | |
8677 | 2802 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8505 | 2803 wpos = g_snprintf (ws, smilelen + 1, "%s", c); |
8473 | 2804 |
8677 | 2805 gtk_imhtml_insert_smiley_at_iter(imhtml, sml, ws, iter); |
8473 | 2806 |
8505 | 2807 c += smilelen; |
2808 pos += smilelen; | |
8473 | 2809 wpos = 0; |
2810 ws[0] = 0; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2811 } else if (*c == '&' && gtk_imhtml_is_amp_escape (c, &, &tlen)) { |
7280 | 2812 while(*amp) { |
2813 ws [wpos++] = *amp++; | |
2814 } | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2815 c += tlen; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2816 pos += tlen; |
1428 | 2817 } else if (*c == '\n') { |
2818 if (!(options & GTK_IMHTML_NO_NEWLINE)) { | |
3922 | 2819 ws[wpos] = '\n'; |
2820 wpos++; | |
8677 | 2821 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2822 ws[0] = '\0'; |
2823 wpos = 0; | |
8177 | 2824 /* NEW_BIT (NEW_TEXT_BIT); */ |
10217 | 2825 } else if (!br) { /* Don't insert a space immediately after an HTML break */ |
9621 | 2826 /* A newline is defined by HTML as whitespace, which means we have to replace it with a word boundary. |
2827 * word breaks vary depending on the language used, so the correct thing to do is to use Pango to determine | |
2828 * what language this is, determine the proper word boundary to use, and insert that. I'm just going to insert | |
2829 * a space instead. What are the non-English speakers going to do? Complain in a language I'll understand? | |
2830 * Bu-wahaha! */ | |
2831 ws[wpos] = ' '; | |
2832 wpos++; | |
2833 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); | |
2834 ws[0] = '\0'; | |
2835 wpos = 0; | |
1428 | 2836 } |
2837 c++; | |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2838 pos++; |
8334 | 2839 } else if ((len_protocol = gtk_imhtml_is_protocol(c)) > 0){ |
2840 while(len_protocol--){ | |
8677 | 2841 /* Skip the next len_protocol characters, but make sure they're |
8334 | 2842 copied into the ws array. |
2843 */ | |
2844 ws [wpos++] = *c++; | |
2845 pos++; | |
2846 } | |
8061 | 2847 } else if (*c) { |
1428 | 2848 ws [wpos++] = *c++; |
2856
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2849 pos++; |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2850 } else { |
b1e300a85678
[gaim-migrate @ 2869]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2826
diff
changeset
|
2851 break; |
1428 | 2852 } |
2853 } | |
8677 | 2854 gtk_text_buffer_insert(imhtml->text_buffer, iter, ws, wpos); |
8061 | 2855 ws[0] = '\0'; wpos = 0; |
2856 | |
8177 | 2857 /* NEW_BIT(NEW_TEXT_BIT); */ |
8061 | 2858 |
4032 | 2859 while (fonts) { |
5967 | 2860 GtkIMHtmlFontDetail *font = fonts->data; |
4032 | 2861 fonts = g_slist_remove (fonts, font); |
2862 if (font->face) | |
2863 g_free (font->face); | |
2864 if (font->fore) | |
2865 g_free (font->fore); | |
2866 if (font->back) | |
2867 g_free (font->back); | |
2868 if (font->sml) | |
2869 g_free (font->sml); | |
2870 g_free (font); | |
2871 } | |
8932 | 2872 |
2873 g_free(ws); | |
2874 if (bg) | |
4630 | 2875 g_free(bg); |
8677 | 2876 |
2877 if (!imhtml->wbfo) | |
8698 | 2878 gtk_imhtml_close_tags(imhtml, iter); |
8506 | 2879 |
2880 object = g_object_ref(G_OBJECT(imhtml)); | |
2881 g_signal_emit(object, signals[UPDATE_FORMAT], 0); | |
2882 g_object_unref(object); | |
2883 | |
3922 | 2884 } |
2885 | |
4892 | 2886 void gtk_imhtml_remove_smileys(GtkIMHtml *imhtml) |
2887 { | |
4288 | 2888 g_hash_table_destroy(imhtml->smiley_data); |
2889 gtk_smiley_tree_destroy(imhtml->default_smilies); | |
4892 | 2890 imhtml->smiley_data = g_hash_table_new_full(g_str_hash, g_str_equal, |
4902 | 2891 g_free, (GDestroyNotify)gtk_smiley_tree_destroy); |
4288 | 2892 imhtml->default_smilies = gtk_smiley_tree_new(); |
2893 } | |
8481 | 2894 |
3922 | 2895 void gtk_imhtml_show_comments (GtkIMHtml *imhtml, |
4253 | 2896 gboolean show) |
2897 { | |
6124 | 2898 imhtml->show_comments = show; |
4253 | 2899 } |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2900 |
8962 | 2901 void |
9029 | 2902 gtk_imhtml_set_protocol_name(GtkIMHtml *imhtml, const gchar *protocol_name) { |
2903 if (imhtml->protocol_name) | |
2904 g_free(imhtml->protocol_name); | |
2905 imhtml->protocol_name = protocol_name ? g_strdup(protocol_name) : NULL; | |
8456 | 2906 } |
2907 | |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2908 void |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2909 gtk_imhtml_clear (GtkIMHtml *imhtml) |
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2910 { |
7991 | 2911 GList *del; |
3922 | 2912 GtkTextIter start, end; |
8427 | 2913 GObject *object = g_object_ref(G_OBJECT(imhtml)); |
10692 | 2914 |
3922 | 2915 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
2916 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
2917 gtk_text_buffer_delete(imhtml->text_buffer, &start, &end); | |
7991 | 2918 |
2919 for(del = imhtml->scalables; del; del = del->next) { | |
2920 GtkIMHtmlScalable *scale = del->data; | |
2921 scale->free(scale); | |
2922 } | |
10692 | 2923 |
7991 | 2924 g_list_free(imhtml->scalables); |
2925 imhtml->scalables = NULL; | |
8061 | 2926 |
8427 | 2927 g_object_unref(object); |
1780
d7cbedd1d651
[gaim-migrate @ 1790]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1738
diff
changeset
|
2928 } |
2363
08c66712364c
[gaim-migrate @ 2376]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2349
diff
changeset
|
2929 |
4046 | 2930 void gtk_imhtml_page_up (GtkIMHtml *imhtml) |
2931 { | |
5282 | 2932 GdkRectangle rect; |
2933 GtkTextIter iter; | |
4046 | 2934 |
5282 | 2935 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); |
2936 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
2937 rect.y - rect.height); | |
2938 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
8061 | 2939 |
4046 | 2940 } |
5282 | 2941 void gtk_imhtml_page_down (GtkIMHtml *imhtml) |
2942 { | |
2943 GdkRectangle rect; | |
2944 GtkTextIter iter; | |
2945 | |
2946 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
2947 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(imhtml), &iter, rect.x, | |
2948 rect.y + rect.height); | |
2949 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &iter, 0, TRUE, 0, 0); | |
2950 } | |
4735 | 2951 |
5967 | 2952 /* GtkIMHtmlScalable, gtk_imhtml_image, gtk_imhtml_hr */ |
8962 | 2953 GtkIMHtmlScalable *gtk_imhtml_image_new(GdkPixbuf *img, const gchar *filename, int id) |
4735 | 2954 { |
5967 | 2955 GtkIMHtmlImage *im_image = g_malloc(sizeof(GtkIMHtmlImage)); |
5012 | 2956 GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(img)); |
4895 | 2957 |
5967 | 2958 GTK_IMHTML_SCALABLE(im_image)->scale = gtk_imhtml_image_scale; |
2959 GTK_IMHTML_SCALABLE(im_image)->add_to = gtk_imhtml_image_add_to; | |
2960 GTK_IMHTML_SCALABLE(im_image)->free = gtk_imhtml_image_free; | |
5046 | 2961 |
2962 im_image->pixbuf = img; | |
5012 | 2963 im_image->image = image; |
4895 | 2964 im_image->width = gdk_pixbuf_get_width(img); |
2965 im_image->height = gdk_pixbuf_get_height(img); | |
2966 im_image->mark = NULL; | |
6982 | 2967 im_image->filename = filename ? g_strdup(filename) : NULL; |
8962 | 2968 im_image->id = id; |
9573 | 2969 im_image->filesel = NULL; |
4895 | 2970 |
5046 | 2971 g_object_ref(img); |
4895 | 2972 return GTK_IMHTML_SCALABLE(im_image); |
2973 } | |
2974 | |
5967 | 2975 void gtk_imhtml_image_scale(GtkIMHtmlScalable *scale, int width, int height) |
4895 | 2976 { |
5967 | 2977 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; |
4895 | 2978 |
2979 if(image->width > width || image->height > height){ | |
2980 GdkPixbuf *new_image = NULL; | |
2981 float factor; | |
2982 int new_width = image->width, new_height = image->height; | |
2983 | |
8588 | 2984 if(image->width > (width - 2)){ |
4895 | 2985 factor = (float)(width)/image->width; |
2986 new_width = width; | |
2987 new_height = image->height * factor; | |
2988 } | |
8588 | 2989 if(new_height >= (height - 2)){ |
4895 | 2990 factor = (float)(height)/new_height; |
2991 new_height = height; | |
2992 new_width = new_width * factor; | |
2993 } | |
2994 | |
5046 | 2995 new_image = gdk_pixbuf_scale_simple(image->pixbuf, new_width, new_height, GDK_INTERP_BILINEAR); |
5012 | 2996 gtk_image_set_from_pixbuf(image->image, new_image); |
4895 | 2997 g_object_unref(G_OBJECT(new_image)); |
2998 } | |
2999 } | |
3000 | |
9573 | 3001 static void |
3002 image_save_yes_cb(GtkIMHtmlImage *image, const char *filename) | |
5012 | 3003 { |
3004 gchar *type = NULL; | |
5019 | 3005 GError *error = NULL; |
5015 | 3006 #if GTK_CHECK_VERSION(2,2,0) |
5012 | 3007 GSList *formats = gdk_pixbuf_get_formats(); |
6162 | 3008 #else |
3009 char *basename = g_path_get_basename(filename); | |
3010 char *ext = strrchr(basename, '.'); | |
5959 | 3011 #endif |
5012 | 3012 |
9573 | 3013 gtk_widget_destroy(image->filesel); |
3014 image->filesel = NULL; | |
5959 | 3015 |
3016 #if GTK_CHECK_VERSION(2,2,0) | |
9573 | 3017 while (formats) { |
5012 | 3018 GdkPixbufFormat *format = formats->data; |
3019 gchar **extensions = gdk_pixbuf_format_get_extensions(format); | |
3020 gpointer p = extensions; | |
3021 | |
3022 while(gdk_pixbuf_format_is_writable(format) && extensions && extensions[0]){ | |
3023 gchar *fmt_ext = extensions[0]; | |
3024 const gchar* file_ext = filename + strlen(filename) - strlen(fmt_ext); | |
3025 | |
3026 if(!strcmp(fmt_ext, file_ext)){ | |
3027 type = gdk_pixbuf_format_get_name(format); | |
3028 break; | |
3029 } | |
3030 | |
3031 extensions++; | |
3032 } | |
3033 | |
3034 g_strfreev(p); | |
3035 | |
9573 | 3036 if (type) |
5012 | 3037 break; |
3038 | |
3039 formats = formats->next; | |
3040 } | |
3041 | |
5020 | 3042 g_slist_free(formats); |
3043 #else | |
3044 /* this is really ugly code, but I think it will work */ | |
9573 | 3045 if (ext) { |
5020 | 3046 ext++; |
9573 | 3047 if (!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg")) |
5020 | 3048 type = g_strdup("jpeg"); |
9573 | 3049 else if (!g_ascii_strcasecmp(ext, "png")) |
5020 | 3050 type = g_strdup("png"); |
3051 } | |
3052 | |
3053 g_free(basename); | |
3054 #endif | |
3055 | |
5012 | 3056 /* If I can't find a valid type, I will just tell the user about it and then assume |
3057 it's a png */ | |
9573 | 3058 if (!type){ |
5012 | 3059 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, |
9717 | 3060 _("Unable to guess the image type based on the file extension supplied. Defaulting to PNG.")); |
3061 type = g_strdup("png"); | |
5012 | 3062 } |
3063 | |
5046 | 3064 gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL); |
5012 | 3065 |
9573 | 3066 if (error){ |
5012 | 3067 gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, |
3068 _("Error saving image: %s"), error->message); | |
3069 g_error_free(error); | |
3070 } | |
3071 | |
3072 g_free(type); | |
3073 } | |
3074 | |
9573 | 3075 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ |
3076 static void | |
3077 image_save_check_if_exists_cb(GtkWidget *widget, gint response, GtkIMHtmlImage *image) | |
3078 { | |
3079 gchar *filename; | |
3080 | |
3081 if (response != GTK_RESPONSE_ACCEPT) { | |
3082 gtk_widget_destroy(widget); | |
3083 image->filesel = NULL; | |
3084 return; | |
3085 } | |
3086 | |
3087 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)); | |
3088 #else /* FILECHOOSER */ | |
3089 static void | |
3090 image_save_check_if_exists_cb(GtkWidget *button, GtkIMHtmlImage *image) | |
5012 | 3091 { |
9573 | 3092 gchar *filename; |
3093 | |
3094 filename = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(image->filesel))); | |
3095 | |
3096 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { | |
3097 gchar *dirname; | |
3098 /* append a / is needed */ | |
3099 if (filename[strlen(filename) - 1] != G_DIR_SEPARATOR) { | |
3100 dirname = g_strconcat(filename, G_DIR_SEPARATOR_S, NULL); | |
3101 } else { | |
3102 dirname = g_strdup(filename); | |
3103 } | |
9574 | 3104 gtk_file_selection_set_filename(GTK_FILE_SELECTION(image->filesel), dirname); |
9573 | 3105 g_free(dirname); |
3106 g_free(filename); | |
3107 return; | |
3108 } | |
3109 #endif /* FILECHOOSER */ | |
3110 | |
3111 /* | |
3112 * XXX - We should probably prompt the user to determine if they really | |
3113 * want to overwrite the file or not. However, I don't feel like doing | |
3114 * that, so we're just always going to overwrite if the file exists. | |
3115 */ | |
3116 /* | |
3117 if (g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
3118 } else | |
3119 image_save_yes_cb(image, filename); | |
3120 */ | |
3121 | |
3122 image_save_yes_cb(image, filename); | |
3123 | |
3124 g_free(filename); | |
3125 } | |
3126 | |
3127 #if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ | |
3128 static void | |
3129 image_save_cancel_cb(GtkIMHtmlImage *image) | |
3130 { | |
3131 gtk_widget_destroy(image->filesel); | |
3132 image->filesel = NULL; | |
3133 } | |
3134 #endif /* FILECHOOSER */ | |
3135 | |
3136 static void | |
3137 gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image) | |
3138 { | |
3139 if (image->filesel != NULL) { | |
3140 gtk_window_present(GTK_WINDOW(image->filesel)); | |
3141 return; | |
3142 } | |
3143 | |
3144 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ | |
3145 image->filesel = gtk_file_chooser_dialog_new(_("Save Image"), | |
3146 NULL, | |
3147 GTK_FILE_CHOOSER_ACTION_SAVE, | |
3148 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
3149 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, | |
3150 NULL); | |
3151 gtk_dialog_set_default_response(GTK_DIALOG(image->filesel), GTK_RESPONSE_ACCEPT); | |
3152 if (image->filename != NULL) | |
3153 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(image->filesel), image->filename); | |
3154 g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(image->filesel)), "response", | |
3155 G_CALLBACK(image_save_check_if_exists_cb), image); | |
3156 #else /* FILECHOOSER */ | |
3157 image->filesel = gtk_file_selection_new(_("Save Image")); | |
3158 if (image->filename != NULL) | |
3159 gtk_file_selection_set_filename(GTK_FILE_SELECTION(image->filesel), image->filename); | |
9574 | 3160 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)), "delete_event", |
3161 G_CALLBACK(image_save_cancel_cb), image); | |
3162 g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->cancel_button), | |
3163 "clicked", G_CALLBACK(image_save_cancel_cb), image); | |
9573 | 3164 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->ok_button), "clicked", |
3165 G_CALLBACK(image_save_check_if_exists_cb), image); | |
3166 #endif /* FILECHOOSER */ | |
3167 | |
3168 gtk_widget_show(image->filesel); | |
5012 | 3169 } |
3170 | |
9815 | 3171 /* |
3172 * So, um, AIM Direct IM lets you send any file, not just images. You can | |
3173 * just insert a sound or a file or whatever in a conversation. It's | |
3174 * basically like file transfer, except there is an icon to open the file | |
3175 * embedded in the conversation. Someone should make the Gaim core handle | |
3176 * all of that. | |
3177 */ | |
5967 | 3178 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image) |
5012 | 3179 { |
3180 GdkEventButton *event_button = (GdkEventButton *) event; | |
3181 | |
3182 if (event->type == GDK_BUTTON_RELEASE) { | |
3183 if(event_button->button == 3) { | |
3184 GtkWidget *img, *item, *menu; | |
3185 gchar *text = g_strdup_printf(_("_Save Image...")); | |
3186 menu = gtk_menu_new(); | |
3187 | |
3188 /* buttons and such */ | |
3189 img = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU); | |
3190 item = gtk_image_menu_item_new_with_mnemonic(text); | |
3191 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img); | |
5967 | 3192 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_imhtml_image_save), image); |
5012 | 3193 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); |
3194 | |
3195 gtk_widget_show_all(menu); | |
3196 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
3197 event_button->button, event_button->time); | |
3198 | |
3199 g_free(text); | |
3200 return TRUE; | |
3201 } | |
3202 } | |
3203 if(event->type == GDK_BUTTON_PRESS && event_button->button == 3) | |
3204 return TRUE; /* Clicking the right mouse button on a link shouldn't | |
3205 be caught by the regular GtkTextView menu */ | |
3206 else | |
3207 return FALSE; /* Let clicks go through if we didn't catch anything */ | |
3208 | |
3209 } | |
5967 | 3210 void gtk_imhtml_image_free(GtkIMHtmlScalable *scale) |
3211 { | |
3212 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
3213 | |
3214 g_object_unref(image->pixbuf); | |
6982 | 3215 if (image->filename) |
3216 g_free(image->filename); | |
9573 | 3217 if (image->filesel) |
3218 gtk_widget_destroy(image->filesel); | |
5967 | 3219 g_free(scale); |
3220 } | |
3221 | |
3222 void gtk_imhtml_image_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
3223 { | |
3224 GtkIMHtmlImage *image = (GtkIMHtmlImage *)scale; | |
3225 GtkWidget *box = gtk_event_box_new(); | |
8962 | 3226 char *tag; |
5967 | 3227 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); |
3228 | |
3229 gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(image->image)); | |
9229 | 3230 |
3231 if(!gtk_check_version(2, 4, 0)) | |
3232 g_object_set(G_OBJECT(box), "visible-window", FALSE, NULL); | |
5967 | 3233 |
3234 gtk_widget_show(GTK_WIDGET(image->image)); | |
3235 gtk_widget_show(box); | |
3236 | |
8962 | 3237 tag = g_strdup_printf("<IMG ID=\"%d\">", image->id); |
3238 g_object_set_data_full(G_OBJECT(anchor), "gtkimhtml_htmltext", tag, g_free); | |
3239 g_object_set_data(G_OBJECT(anchor), "gtkimhtml_plaintext", "[Image]"); | |
3240 | |
5967 | 3241 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), box, anchor); |
3242 g_signal_connect(G_OBJECT(box), "event", G_CALLBACK(gtk_imhtml_image_clicked), image); | |
3243 } | |
3244 | |
3245 GtkIMHtmlScalable *gtk_imhtml_hr_new() | |
3246 { | |
3247 GtkIMHtmlHr *hr = g_malloc(sizeof(GtkIMHtmlHr)); | |
3248 | |
3249 GTK_IMHTML_SCALABLE(hr)->scale = gtk_imhtml_hr_scale; | |
3250 GTK_IMHTML_SCALABLE(hr)->add_to = gtk_imhtml_hr_add_to; | |
3251 GTK_IMHTML_SCALABLE(hr)->free = gtk_imhtml_hr_free; | |
3252 | |
3253 hr->sep = gtk_hseparator_new(); | |
3254 gtk_widget_set_size_request(hr->sep, 5000, 2); | |
3255 gtk_widget_show(hr->sep); | |
3256 | |
3257 return GTK_IMHTML_SCALABLE(hr); | |
3258 } | |
3259 | |
3260 void gtk_imhtml_hr_scale(GtkIMHtmlScalable *scale, int width, int height) | |
3261 { | |
8588 | 3262 gtk_widget_set_size_request(((GtkIMHtmlHr *)scale)->sep, width - 2, 2); |
5967 | 3263 } |
3264 | |
3265 void gtk_imhtml_hr_add_to(GtkIMHtmlScalable *scale, GtkIMHtml *imhtml, GtkTextIter *iter) | |
3266 { | |
3267 GtkIMHtmlHr *hr = (GtkIMHtmlHr *)scale; | |
3268 GtkTextChildAnchor *anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); | |
8698 | 3269 g_object_set_data(G_OBJECT(anchor), "gtkimhtml_htmltext", "<hr>"); |
3270 g_object_set_data(G_OBJECT(anchor), "gtkimhtml_plaintext", "\n---\n"); | |
5967 | 3271 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), hr->sep, anchor); |
3272 } | |
3273 | |
3274 void gtk_imhtml_hr_free(GtkIMHtmlScalable *scale) | |
3275 { | |
3276 g_free(scale); | |
3277 } | |
7295 | 3278 |
3279 gboolean gtk_imhtml_search_find(GtkIMHtml *imhtml, const gchar *text) | |
3280 { | |
3281 GtkTextIter iter, start, end; | |
3282 gboolean new_search = TRUE; | |
3283 | |
3284 g_return_val_if_fail(imhtml != NULL, FALSE); | |
3285 g_return_val_if_fail(text != NULL, FALSE); | |
8061 | 3286 |
7295 | 3287 if (imhtml->search_string && !strcmp(text, imhtml->search_string)) |
3288 new_search = FALSE; | |
8061 | 3289 |
7295 | 3290 if (new_search) { |
3291 gtk_imhtml_search_clear(imhtml); | |
3292 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &iter); | |
3293 } else { | |
3294 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, | |
8061 | 3295 gtk_text_buffer_get_mark(imhtml->text_buffer, "search")); |
7295 | 3296 } |
10574 | 3297 g_free(imhtml->search_string); |
7295 | 3298 imhtml->search_string = g_strdup(text); |
3299 | |
7358 | 3300 if (gtk_source_iter_forward_search(&iter, imhtml->search_string, |
3301 GTK_SOURCE_SEARCH_VISIBLE_ONLY | GTK_SOURCE_SEARCH_CASE_INSENSITIVE, | |
7295 | 3302 &start, &end, NULL)) { |
3303 | |
3304 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(imhtml), &start, 0, TRUE, 0, 0); | |
3305 gtk_text_buffer_create_mark(imhtml->text_buffer, "search", &end, FALSE); | |
3306 if (new_search) { | |
3307 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &iter, &end); | |
8061 | 3308 do |
7295 | 3309 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "search", &start, &end); |
8061 | 3310 while (gtk_source_iter_forward_search(&end, imhtml->search_string, |
3311 GTK_SOURCE_SEARCH_VISIBLE_ONLY | | |
7358 | 3312 GTK_SOURCE_SEARCH_CASE_INSENSITIVE, |
7295 | 3313 &start, &end, NULL)); |
3314 } | |
3315 return TRUE; | |
3316 } | |
8061 | 3317 |
3318 gtk_imhtml_search_clear(imhtml); | |
3319 | |
7295 | 3320 return FALSE; |
3321 } | |
3322 | |
3323 void gtk_imhtml_search_clear(GtkIMHtml *imhtml) | |
3324 { | |
3325 GtkTextIter start, end; | |
8061 | 3326 |
7295 | 3327 g_return_if_fail(imhtml != NULL); |
8061 | 3328 |
7295 | 3329 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); |
3330 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
3331 | |
3332 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "search", &start, &end); | |
3333 if (imhtml->search_string) | |
3334 g_free(imhtml->search_string); | |
3335 imhtml->search_string = NULL; | |
3336 } | |
8061 | 3337 |
8677 | 3338 static GtkTextTag *find_font_forecolor_tag(GtkIMHtml *imhtml, gchar *color) |
3339 { | |
3340 gchar str[18]; | |
3341 GtkTextTag *tag; | |
3342 | |
3343 g_snprintf(str, sizeof(str), "FORECOLOR %s", color); | |
3344 | |
3345 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
3346 if (!tag) | |
3347 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "foreground", color, NULL); | |
3348 | |
3349 return tag; | |
3350 } | |
3351 | |
3352 static GtkTextTag *find_font_backcolor_tag(GtkIMHtml *imhtml, gchar *color) | |
3353 { | |
3354 gchar str[18]; | |
3355 GtkTextTag *tag; | |
3356 | |
3357 g_snprintf(str, sizeof(str), "BACKCOLOR %s", color); | |
3358 | |
3359 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
3360 if (!tag) | |
3361 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "background", color, NULL); | |
3362 | |
3363 return tag; | |
3364 } | |
3365 | |
10776 | 3366 static GtkTextTag *find_font_background_tag(GtkIMHtml *imhtml, gchar *color) |
3367 { | |
3368 gchar str[19]; | |
3369 GtkTextTag *tag; | |
3370 | |
3371 g_snprintf(str, sizeof(str), "BACKGROUND %s", color); | |
3372 | |
3373 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
3374 if (!tag) | |
3375 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, NULL); | |
3376 | |
3377 return tag; | |
3378 } | |
3379 | |
8677 | 3380 static GtkTextTag *find_font_face_tag(GtkIMHtml *imhtml, gchar *face) |
8061 | 3381 { |
8677 | 3382 gchar str[256]; |
3383 GtkTextTag *tag; | |
3384 | |
3385 g_snprintf(str, sizeof(str), "FONT FACE %s", face); | |
3386 str[255] = '\0'; | |
3387 | |
3388 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
3389 if (!tag) | |
3390 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "family", face, NULL); | |
3391 | |
3392 return tag; | |
3393 } | |
3394 | |
3395 static GtkTextTag *find_font_size_tag(GtkIMHtml *imhtml, int size) | |
3396 { | |
3397 gchar str[24]; | |
3398 GtkTextTag *tag; | |
3399 | |
3400 g_snprintf(str, sizeof(str), "FONT SIZE %d", size); | |
3401 str[23] = '\0'; | |
3402 | |
3403 tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(imhtml->text_buffer), str); | |
3404 if (!tag) { | |
10525
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3405 /* For reasons I don't understand, setting "scale" here scaled |
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3406 * based on some default size other than my theme's default |
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3407 * size. Our size 4 was actually smaller than our size 3 for |
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3408 * me. So this works around that oddity. |
8677 | 3409 */ |
10525
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3410 GtkTextAttributes *attr = gtk_text_view_get_default_attributes(GTK_TEXT_VIEW(imhtml)); |
8677 | 3411 tag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "size", |
10525
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3412 (gint) (pango_font_description_get_size(attr->font) * |
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3413 (double) _point_sizes[size-1]), NULL); |
ddea15f4cbc2
[gaim-migrate @ 11842]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10524
diff
changeset
|
3414 gtk_text_attributes_unref(attr); |
8061 | 3415 } |
3416 | |
8677 | 3417 return tag; |
3418 } | |
3419 | |
3420 static void remove_tag_by_prefix(GtkIMHtml *imhtml, const GtkTextIter *i, const GtkTextIter *e, | |
3421 const char *prefix, guint len, gboolean homo) | |
3422 { | |
3423 GSList *tags, *l; | |
3424 GtkTextIter iter; | |
3425 | |
3426 tags = gtk_text_iter_get_tags(i); | |
3427 | |
3428 for (l = tags; l; l = l->next) { | |
3429 GtkTextTag *tag = l->data; | |
3430 | |
3431 if (tag->name && !strncmp(tag->name, prefix, len)) | |
3432 gtk_text_buffer_remove_tag(imhtml->text_buffer, tag, i, e); | |
8061 | 3433 } |
3434 | |
8677 | 3435 g_slist_free(tags); |
3436 | |
3437 if (homo) | |
3438 return; | |
3439 | |
3440 iter = *i; | |
3441 | |
3442 while (gtk_text_iter_forward_char(&iter) && !gtk_text_iter_equal(&iter, e)) { | |
3443 if (gtk_text_iter_begins_tag(&iter, NULL)) { | |
3444 tags = gtk_text_iter_get_toggled_tags(&iter, TRUE); | |
3445 | |
3446 for (l = tags; l; l = l->next) { | |
3447 GtkTextTag *tag = l->data; | |
3448 | |
3449 if (tag->name && !strncmp(tag->name, prefix, len)) | |
3450 gtk_text_buffer_remove_tag(imhtml->text_buffer, tag, &iter, e); | |
3451 } | |
3452 | |
3453 g_slist_free(tags); | |
3454 } | |
8061 | 3455 } |
8677 | 3456 } |
3457 | |
3458 static void remove_font_size(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
3459 { | |
3460 remove_tag_by_prefix(imhtml, i, e, "FONT SIZE ", 10, homo); | |
3461 } | |
3462 | |
3463 static void remove_font_face(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
3464 { | |
3465 remove_tag_by_prefix(imhtml, i, e, "FONT FACE ", 10, homo); | |
3466 } | |
3467 | |
3468 static void remove_font_forecolor(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
3469 { | |
3470 remove_tag_by_prefix(imhtml, i, e, "FORECOLOR ", 10, homo); | |
3471 } | |
3472 | |
3473 static void remove_font_backcolor(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) | |
3474 { | |
3475 remove_tag_by_prefix(imhtml, i, e, "BACKCOLOR ", 10, homo); | |
3476 } | |
3477 | |
10776 | 3478 static void remove_font_background(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) |
3479 { | |
3480 remove_tag_by_prefix(imhtml, i, e, "BACKGROUND ", 10, homo); | |
3481 } | |
3482 | |
8677 | 3483 static void remove_font_link(GtkIMHtml *imhtml, GtkTextIter *i, GtkTextIter *e, gboolean homo) |
3484 { | |
3485 remove_tag_by_prefix(imhtml, i, e, "LINK ", 5, homo); | |
3486 } | |
3487 | |
3488 /* Editable stuff */ | |
3489 static void preinsert_cb(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *text, gint len, GtkIMHtml *imhtml) | |
3490 { | |
3491 imhtml->insert_offset = gtk_text_iter_get_offset(iter); | |
3492 } | |
3493 | |
10169 | 3494 static void insert_ca_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextChildAnchor *arg2, gpointer user_data) |
3495 { | |
3496 GtkTextIter start; | |
3497 | |
3498 start = *arg1; | |
3499 gtk_text_iter_backward_char(&start); | |
3500 | |
3501 gtk_imhtml_apply_tags_on_insert(user_data, &start, arg1); | |
3502 } | |
3503 | |
8677 | 3504 static void insert_cb(GtkTextBuffer *buffer, GtkTextIter *end, gchar *text, gint len, GtkIMHtml *imhtml) |
3505 { | |
3506 GtkTextIter start; | |
3507 | |
3508 if (!len) | |
3509 return; | |
3510 | |
3511 start = *end; | |
3512 gtk_text_iter_set_offset(&start, imhtml->insert_offset); | |
3513 | |
10169 | 3514 gtk_imhtml_apply_tags_on_insert(imhtml, &start, end); |
3515 } | |
3516 | |
3517 static void gtk_imhtml_apply_tags_on_insert(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end) | |
3518 { | |
8677 | 3519 if (imhtml->edit.bold) |
10169 | 3520 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", start, end); |
8677 | 3521 else |
10169 | 3522 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", start, end); |
8677 | 3523 |
3524 if (imhtml->edit.italic) | |
10169 | 3525 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", start, end); |
8677 | 3526 else |
10169 | 3527 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", start, end); |
8677 | 3528 |
3529 if (imhtml->edit.underline) | |
10169 | 3530 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", start, end); |
8677 | 3531 else |
10169 | 3532 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", start, end); |
8677 | 3533 |
9924 | 3534 if (imhtml->edit.strike) |
10169 | 3535 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", start, end); |
9924 | 3536 else |
10169 | 3537 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", start, end); |
9924 | 3538 |
8677 | 3539 if (imhtml->edit.forecolor) { |
10169 | 3540 remove_font_forecolor(imhtml, start, end, TRUE); |
8677 | 3541 gtk_text_buffer_apply_tag(imhtml->text_buffer, |
3542 find_font_forecolor_tag(imhtml, imhtml->edit.forecolor), | |
10169 | 3543 start, end); |
8061 | 3544 } |
3545 | |
8677 | 3546 if (imhtml->edit.backcolor) { |
10169 | 3547 remove_font_backcolor(imhtml, start, end, TRUE); |
8677 | 3548 gtk_text_buffer_apply_tag(imhtml->text_buffer, |
3549 find_font_backcolor_tag(imhtml, imhtml->edit.backcolor), | |
10169 | 3550 start, end); |
8677 | 3551 } |
3552 | |
10776 | 3553 if (imhtml->edit.background) { |
3554 remove_font_background(imhtml, start, end, TRUE); | |
3555 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3556 find_font_background_tag(imhtml, imhtml->edit.background), | |
3557 start, end); | |
3558 } | |
8677 | 3559 if (imhtml->edit.fontface) { |
10169 | 3560 remove_font_face(imhtml, start, end, TRUE); |
8677 | 3561 gtk_text_buffer_apply_tag(imhtml->text_buffer, |
3562 find_font_face_tag(imhtml, imhtml->edit.fontface), | |
10169 | 3563 start, end); |
8061 | 3564 } |
8677 | 3565 |
3566 if (imhtml->edit.fontsize) { | |
10169 | 3567 remove_font_size(imhtml, start, end, TRUE); |
8677 | 3568 gtk_text_buffer_apply_tag(imhtml->text_buffer, |
3569 find_font_size_tag(imhtml, imhtml->edit.fontsize), | |
10169 | 3570 start, end); |
8677 | 3571 } |
3572 | |
3573 if (imhtml->edit.link) { | |
10169 | 3574 remove_font_link(imhtml, start, end, TRUE); |
8677 | 3575 gtk_text_buffer_apply_tag(imhtml->text_buffer, |
3576 imhtml->edit.link, | |
10169 | 3577 start, end); |
8677 | 3578 } |
8061 | 3579 } |
3580 | |
3581 void gtk_imhtml_set_editable(GtkIMHtml *imhtml, gboolean editable) | |
3582 { | |
3583 gtk_text_view_set_editable(GTK_TEXT_VIEW(imhtml), editable); | |
8177 | 3584 /* |
3585 * We need a visible caret for accessibility, so mouseless | |
3586 * people can highlight stuff. | |
3587 */ | |
3588 /* gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(imhtml), editable); */ | |
8061 | 3589 imhtml->editable = editable; |
8677 | 3590 imhtml->format_functions = GTK_IMHTML_ALL; |
3591 | |
3592 if (editable) | |
3593 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", | |
3594 G_CALLBACK(mark_set_cb), imhtml); | |
3595 } | |
3596 | |
3597 void gtk_imhtml_set_whole_buffer_formatting_only(GtkIMHtml *imhtml, gboolean wbfo) | |
3598 { | |
3599 g_return_if_fail(imhtml != NULL); | |
3600 | |
3601 imhtml->wbfo = wbfo; | |
8420 | 3602 } |
3603 | |
3604 void gtk_imhtml_set_format_functions(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons) | |
3605 { | |
3606 GObject *object = g_object_ref(G_OBJECT(imhtml)); | |
8677 | 3607 imhtml->format_functions = buttons; |
8420 | 3608 g_signal_emit(object, signals[BUTTONS_UPDATE], 0, buttons); |
3609 g_object_unref(object); | |
8061 | 3610 } |
3611 | |
8788 | 3612 GtkIMHtmlButtons gtk_imhtml_get_format_functions(GtkIMHtml *imhtml) |
3613 { | |
3614 return imhtml->format_functions; | |
3615 } | |
8516 | 3616 |
3617 void gtk_imhtml_get_current_format(GtkIMHtml *imhtml, gboolean *bold, | |
3618 gboolean *italic, gboolean *underline) | |
8481 | 3619 { |
8677 | 3620 if (imhtml->edit.bold) |
3621 (*bold) = TRUE; | |
3622 if (imhtml->edit.italic) | |
3623 (*italic) = TRUE; | |
3624 if (imhtml->edit.underline) | |
3625 (*underline) = TRUE; | |
8481 | 3626 } |
3627 | |
9025 | 3628 char * |
3629 gtk_imhtml_get_current_fontface(GtkIMHtml *imhtml) | |
3630 { | |
3631 if (imhtml->edit.fontface) | |
3632 return g_strdup(imhtml->edit.fontface); | |
3633 else | |
3634 return NULL; | |
3635 } | |
3636 | |
3637 char * | |
3638 gtk_imhtml_get_current_forecolor(GtkIMHtml *imhtml) | |
3639 { | |
3640 if (imhtml->edit.forecolor) | |
3641 return g_strdup(imhtml->edit.forecolor); | |
3642 else | |
3643 return NULL; | |
3644 } | |
3645 | |
3646 char * | |
3647 gtk_imhtml_get_current_backcolor(GtkIMHtml *imhtml) | |
3648 { | |
3649 if (imhtml->edit.backcolor) | |
3650 return g_strdup(imhtml->edit.backcolor); | |
3651 else | |
3652 return NULL; | |
3653 } | |
3654 | |
3655 gint | |
3656 gtk_imhtml_get_current_fontsize(GtkIMHtml *imhtml) | |
3657 { | |
3658 return imhtml->edit.fontsize; | |
3659 } | |
3660 | |
8061 | 3661 gboolean gtk_imhtml_get_editable(GtkIMHtml *imhtml) |
3662 { | |
3663 return imhtml->editable; | |
3664 } | |
3665 | |
8677 | 3666 /* |
3667 * I had this crazy idea about changing the text cursor color to reflex the foreground color | |
3668 * of the text about to be entered. This is the place you'd do it, along with the place where | |
3669 * we actually set a new foreground color. | |
3670 * I may not do this, because people will bitch about Gaim overriding their gtk theme's cursor | |
3671 * colors. | |
3672 * | |
3673 * Just in case I do do this, I asked about what to set the secondary text cursor to. | |
3674 * | |
8719 | 3675 * (12:45:27) ?? ???: secondary_cursor_color = (rgb(background) + rgb(primary_cursor_color) ) / 2 |
3676 * (12:45:55) ?? ???: understand? | |
8677 | 3677 * (12:46:14) Tim: yeah. i didn't know there was an exact formula |
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8729
diff
changeset
|
3678 * (12:46:56) ?? ???: u might need to extract separate each color from RGB |
8677 | 3679 */ |
3680 | |
3681 static void mark_set_cb(GtkTextBuffer *buffer, GtkTextIter *arg1, GtkTextMark *mark, | |
3682 GtkIMHtml *imhtml) | |
3683 { | |
3684 GSList *tags, *l; | |
3685 GtkTextIter iter; | |
3686 | |
3687 if (mark != gtk_text_buffer_get_insert(buffer)) | |
3688 return; | |
3689 | |
3690 if (!gtk_text_buffer_get_char_count(buffer)) | |
3691 return; | |
3692 | |
9924 | 3693 imhtml->edit.bold = imhtml->edit.italic = imhtml->edit.underline = imhtml->edit.strike = FALSE; |
8677 | 3694 if (imhtml->edit.forecolor) |
3695 g_free(imhtml->edit.forecolor); | |
3696 imhtml->edit.forecolor = NULL; | |
3697 if (imhtml->edit.backcolor) | |
3698 g_free(imhtml->edit.backcolor); | |
3699 imhtml->edit.backcolor = NULL; | |
3700 if (imhtml->edit.fontface) | |
3701 g_free(imhtml->edit.fontface); | |
3702 imhtml->edit.fontface = NULL; | |
3703 imhtml->edit.fontsize = 0; | |
3704 imhtml->edit.link = NULL; | |
3705 | |
3706 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
3707 | |
3708 | |
3709 if (gtk_text_iter_is_end(&iter)) | |
3710 tags = gtk_text_iter_get_toggled_tags(&iter, FALSE); | |
3711 else | |
3712 tags = gtk_text_iter_get_tags(&iter); | |
3713 | |
3714 for (l = tags; l != NULL; l = l->next) { | |
3715 GtkTextTag *tag = GTK_TEXT_TAG(l->data); | |
3716 | |
3717 if (tag->name) { | |
3718 if (strcmp(tag->name, "BOLD") == 0) | |
3719 imhtml->edit.bold = TRUE; | |
3720 if (strcmp(tag->name, "ITALICS") == 0) | |
3721 imhtml->edit.italic = TRUE; | |
3722 if (strcmp(tag->name, "UNDERLINE") == 0) | |
3723 imhtml->edit.underline = TRUE; | |
9924 | 3724 if (strcmp(tag->name, "STRIKE") == 0) |
3725 imhtml->edit.strike = TRUE; | |
8677 | 3726 if (strncmp(tag->name, "FORECOLOR ", 10) == 0) |
3727 imhtml->edit.forecolor = g_strdup(&(tag->name)[10]); | |
3728 if (strncmp(tag->name, "BACKCOLOR ", 10) == 0) | |
3729 imhtml->edit.backcolor = g_strdup(&(tag->name)[10]); | |
3730 if (strncmp(tag->name, "FONT FACE ", 10) == 0) | |
3731 imhtml->edit.fontface = g_strdup(&(tag->name)[10]); | |
3732 if (strncmp(tag->name, "FONT SIZE ", 10) == 0) | |
3733 imhtml->edit.fontsize = strtol(&(tag->name)[10], NULL, 10); | |
8719 | 3734 if ((strncmp(tag->name, "LINK ", 5) == 0) && !gtk_text_iter_is_end(&iter)) |
8677 | 3735 imhtml->edit.link = tag; |
3736 } | |
3737 } | |
3738 | |
3739 g_slist_free(tags); | |
3740 } | |
3741 | |
8061 | 3742 gboolean gtk_imhtml_toggle_bold(GtkIMHtml *imhtml) |
3743 { | |
8481 | 3744 GObject *object; |
8677 | 3745 GtkTextIter start, end; |
3746 | |
3747 imhtml->edit.bold = !imhtml->edit.bold; | |
3748 | |
3749 if (imhtml->wbfo) { | |
3750 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3751 if (imhtml->edit.bold) | |
3752 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
3753 else | |
3754 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
3755 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3756 if (imhtml->edit.bold) | |
3757 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
3758 else | |
3759 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "BOLD", &start, &end); | |
3760 | |
8061 | 3761 } |
8481 | 3762 object = g_object_ref(G_OBJECT(imhtml)); |
3763 g_object_unref(object); | |
3764 | |
8677 | 3765 return (imhtml->edit.bold != FALSE); |
8061 | 3766 } |
3767 | |
3768 gboolean gtk_imhtml_toggle_italic(GtkIMHtml *imhtml) | |
3769 { | |
8481 | 3770 GObject *object; |
8677 | 3771 GtkTextIter start, end; |
3772 | |
3773 imhtml->edit.italic = !imhtml->edit.italic; | |
3774 | |
3775 if (imhtml->wbfo) { | |
3776 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3777 if (imhtml->edit.italic) | |
3778 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
3779 else | |
3780 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
3781 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3782 if (imhtml->edit.italic) | |
3783 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
3784 else | |
3785 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "ITALICS", &start, &end); | |
8061 | 3786 } |
8481 | 3787 object = g_object_ref(G_OBJECT(imhtml)); |
3788 g_object_unref(object); | |
3789 | |
8677 | 3790 return imhtml->edit.italic != FALSE; |
8061 | 3791 } |
3792 | |
3793 gboolean gtk_imhtml_toggle_underline(GtkIMHtml *imhtml) | |
3794 { | |
8481 | 3795 GObject *object; |
8677 | 3796 GtkTextIter start, end; |
3797 | |
3798 imhtml->edit.underline = !imhtml->edit.underline; | |
3799 | |
3800 if (imhtml->wbfo) { | |
3801 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3802 if (imhtml->edit.underline) | |
3803 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
3804 else | |
3805 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
3806 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3807 if (imhtml->edit.underline) | |
3808 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
3809 else | |
3810 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "UNDERLINE", &start, &end); | |
8061 | 3811 } |
8481 | 3812 object = g_object_ref(G_OBJECT(imhtml)); |
3813 g_object_unref(object); | |
3814 | |
8677 | 3815 return imhtml->edit.underline != FALSE; |
8061 | 3816 } |
3817 | |
9924 | 3818 gboolean gtk_imhtml_toggle_strike(GtkIMHtml *imhtml) |
3819 { | |
3820 GObject *object; | |
3821 GtkTextIter start, end; | |
3822 | |
3823 imhtml->edit.strike = !imhtml->edit.strike; | |
3824 | |
3825 if (imhtml->wbfo) { | |
3826 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3827 if (imhtml->edit.strike) | |
3828 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end); | |
3829 else | |
3830 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end); | |
3831 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3832 if (imhtml->edit.strike) | |
3833 gtk_text_buffer_apply_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end); | |
3834 else | |
3835 gtk_text_buffer_remove_tag_by_name(imhtml->text_buffer, "STRIKE", &start, &end); | |
3836 } | |
3837 object = g_object_ref(G_OBJECT(imhtml)); | |
3838 g_object_unref(object); | |
3839 | |
3840 return imhtml->edit.strike != FALSE; | |
3841 } | |
3842 | |
8061 | 3843 void gtk_imhtml_font_set_size(GtkIMHtml *imhtml, gint size) |
3844 { | |
9025 | 3845 GObject *object; |
8677 | 3846 GtkTextIter start, end; |
9025 | 3847 GtkIMHtmlButtons b = 0; |
8061 | 3848 |
3849 imhtml->edit.fontsize = size; | |
3850 | |
8677 | 3851 |
3852 if (imhtml->wbfo) { | |
3853 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3854 remove_font_size(imhtml, &start, &end, TRUE); | |
3855 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3856 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
3857 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3858 remove_font_size(imhtml, &start, &end, FALSE); | |
3859 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3860 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 3861 } |
8677 | 3862 |
9025 | 3863 object = g_object_ref(G_OBJECT(imhtml)); |
3864 b |= GTK_IMHTML_SHRINK; | |
3865 b |= GTK_IMHTML_GROW; | |
3866 g_object_unref(object); | |
8061 | 3867 } |
3868 | |
3869 void gtk_imhtml_font_shrink(GtkIMHtml *imhtml) | |
3870 { | |
9025 | 3871 GObject *object; |
8677 | 3872 GtkTextIter start, end; |
3873 | |
8061 | 3874 if (imhtml->edit.fontsize == 1) |
3875 return; | |
3876 | |
8677 | 3877 if (!imhtml->edit.fontsize) |
3878 imhtml->edit.fontsize = 2; | |
3879 else | |
3880 imhtml->edit.fontsize--; | |
3881 | |
3882 if (imhtml->wbfo) { | |
3883 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3884 remove_font_size(imhtml, &start, &end, TRUE); | |
3885 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3886 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
3887 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3888 remove_font_size(imhtml, &start, &end, FALSE); | |
3889 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3890 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 3891 } |
9025 | 3892 object = g_object_ref(G_OBJECT(imhtml)); |
3893 g_object_unref(object); | |
8061 | 3894 } |
3895 | |
3896 void gtk_imhtml_font_grow(GtkIMHtml *imhtml) | |
3897 { | |
9025 | 3898 GObject *object; |
8677 | 3899 GtkTextIter start, end; |
3900 | |
8061 | 3901 if (imhtml->edit.fontsize == MAX_FONT_SIZE) |
3902 return; | |
3903 | |
8677 | 3904 if (!imhtml->edit.fontsize) |
3905 imhtml->edit.fontsize = 4; | |
3906 else | |
3907 imhtml->edit.fontsize++; | |
3908 | |
3909 if (imhtml->wbfo) { | |
3910 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); | |
3911 remove_font_size(imhtml, &start, &end, TRUE); | |
3912 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3913 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
3914 } else if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
3915 remove_font_size(imhtml, &start, &end, FALSE); | |
3916 gtk_text_buffer_apply_tag(imhtml->text_buffer, | |
3917 find_font_size_tag(imhtml, imhtml->edit.fontsize), &start, &end); | |
8061 | 3918 } |
9025 | 3919 object = g_object_ref(G_OBJECT(imhtml)); |
3920 g_object_unref(object); | |
8061 | 3921 } |
3922 | |
10776 | 3923 #define gtk_imhtml_toggle_str_tag(imhtml, color, edit_field, remove_func, find_func) { \ |
3924 GObject *object; \ | |
3925 GtkTextIter start, end; \ | |
3926 \ | |
3927 g_free(edit_field); \ | |
3928 edit_field = NULL; \ | |
3929 \ | |
3930 if (color && strcmp(color, "") != 0) { \ | |
3931 edit_field = g_strdup(color); \ | |
3932 \ | |
3933 if (imhtml->wbfo) { \ | |
3934 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); \ | |
3935 remove_func(imhtml, &start, &end, TRUE); \ | |
3936 gtk_text_buffer_apply_tag(imhtml->text_buffer, \ | |
3937 find_func(imhtml, edit_field), &start, &end); \ | |
3938 } else { \ | |
3939 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &start, \ | |
3940 gtk_text_buffer_get_mark(imhtml->text_buffer, "insert")); \ | |
3941 if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { \ | |
3942 remove_func(imhtml, &start, &end, FALSE); \ | |
3943 gtk_text_buffer_apply_tag(imhtml->text_buffer, \ | |
3944 find_func(imhtml, \ | |
3945 edit_field), \ | |
3946 &start, &end); \ | |
3947 } \ | |
3948 } \ | |
3949 } else { \ | |
3950 if (imhtml->wbfo) { \ | |
3951 gtk_text_buffer_get_bounds(imhtml->text_buffer, &start, &end); \ | |
3952 remove_func(imhtml, &start, &end, TRUE); \ | |
3953 } \ | |
3954 } \ | |
3955 \ | |
3956 object = g_object_ref(G_OBJECT(imhtml)); \ | |
3957 g_object_unref(object); \ | |
3958 \ | |
3959 return edit_field != NULL; \ | |
3960 } | |
3961 | |
8061 | 3962 gboolean gtk_imhtml_toggle_forecolor(GtkIMHtml *imhtml, const char *color) |
3963 { | |
10776 | 3964 gtk_imhtml_toggle_str_tag(imhtml, color, imhtml->edit.forecolor, remove_font_forecolor, find_font_forecolor_tag); |
8061 | 3965 } |
3966 | |
3967 gboolean gtk_imhtml_toggle_backcolor(GtkIMHtml *imhtml, const char *color) | |
3968 { | |
10776 | 3969 gtk_imhtml_toggle_str_tag(imhtml, color, imhtml->edit.backcolor, remove_font_backcolor, find_font_backcolor_tag); |
3970 } | |
3971 | |
3972 gboolean gtk_imhtml_toggle_background(GtkIMHtml *imhtml, const char *color) | |
3973 { | |
3974 gtk_imhtml_toggle_str_tag(imhtml, color, imhtml->edit.background, remove_font_background, find_font_background_tag); | |
8061 | 3975 } |
3976 | |
3977 gboolean gtk_imhtml_toggle_fontface(GtkIMHtml *imhtml, const char *face) | |
3978 { | |
10776 | 3979 gtk_imhtml_toggle_str_tag(imhtml, face, imhtml->edit.fontface, remove_font_face, find_font_face_tag); |
8061 | 3980 } |
3981 | |
8677 | 3982 void gtk_imhtml_toggle_link(GtkIMHtml *imhtml, const char *url) |
8061 | 3983 { |
9025 | 3984 GObject *object; |
8677 | 3985 GtkTextIter start, end; |
3986 GtkTextTag *linktag; | |
3987 static guint linkno = 0; | |
3988 gchar str[48]; | |
9007 | 3989 GdkColor *color = NULL; |
8677 | 3990 |
3991 imhtml->edit.link = NULL; | |
3992 | |
3993 | |
3994 | |
3995 if (url) { | |
3996 g_snprintf(str, sizeof(str), "LINK %d", linkno++); | |
3997 str[47] = '\0'; | |
3998 | |
9007 | 3999 gtk_widget_style_get(GTK_WIDGET(imhtml), "hyperlink-color", &color, NULL); |
9008 | 4000 if (color) { |
9007 | 4001 imhtml->edit.link = linktag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "foreground-gdk", color, "underline", PANGO_UNDERLINE_SINGLE, NULL); |
9008 | 4002 gdk_color_free(color); |
4003 } else { | |
9007 | 4004 imhtml->edit.link = linktag = gtk_text_buffer_create_tag(imhtml->text_buffer, str, "foreground", "blue", "underline", PANGO_UNDERLINE_SINGLE, NULL); |
9008 | 4005 } |
8677 | 4006 g_object_set_data_full(G_OBJECT(linktag), "link_url", g_strdup(url), g_free); |
4007 g_signal_connect(G_OBJECT(linktag), "event", G_CALLBACK(tag_event), NULL); | |
4008 | |
4009 if (imhtml->editable && gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, &start, &end)) { | |
4010 remove_font_link(imhtml, &start, &end, FALSE); | |
4011 gtk_text_buffer_apply_tag(imhtml->text_buffer, linktag, &start, &end); | |
4012 } | |
4013 } | |
9025 | 4014 |
4015 object = g_object_ref(G_OBJECT(imhtml)); | |
4016 g_signal_emit(object, signals[TOGGLE_FORMAT], 0, GTK_IMHTML_LINK); | |
4017 g_object_unref(object); | |
8677 | 4018 } |
4019 | |
4020 void gtk_imhtml_insert_link(GtkIMHtml *imhtml, GtkTextMark *mark, const char *url, const char *text) | |
4021 { | |
8061 | 4022 GtkTextIter iter; |
8677 | 4023 |
9599 | 4024 if (gtk_text_buffer_get_selection_bounds(imhtml->text_buffer, NULL, NULL)) |
4025 gtk_text_buffer_delete_selection(imhtml->text_buffer, TRUE, TRUE); | |
4026 | |
8677 | 4027 gtk_imhtml_toggle_link(imhtml, url); |
8061 | 4028 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); |
8677 | 4029 gtk_text_buffer_insert(imhtml->text_buffer, &iter, text, -1); |
4030 gtk_imhtml_toggle_link(imhtml, NULL); | |
8061 | 4031 } |
4032 | |
4033 void gtk_imhtml_insert_smiley(GtkIMHtml *imhtml, const char *sml, char *smiley) | |
4034 { | |
8677 | 4035 GtkTextMark *mark; |
8061 | 4036 GtkTextIter iter; |
8677 | 4037 |
4038 mark = gtk_text_buffer_get_insert(imhtml->text_buffer); | |
4039 | |
4040 gtk_text_buffer_get_iter_at_mark(imhtml->text_buffer, &iter, mark); | |
4041 gtk_imhtml_insert_smiley_at_iter(imhtml, sml, smiley, &iter); | |
4042 } | |
4043 | |
4044 void gtk_imhtml_insert_smiley_at_iter(GtkIMHtml *imhtml, const char *sml, char *smiley, GtkTextIter *iter) | |
4045 { | |
8061 | 4046 GdkPixbuf *pixbuf = NULL; |
4047 GdkPixbufAnimation *annipixbuf = NULL; | |
4048 GtkWidget *icon = NULL; | |
4049 GtkTextChildAnchor *anchor; | |
8505 | 4050 char *unescaped = gaim_unescape_html(smiley); |
8061 | 4051 |
10526 | 4052 if (imhtml->format_functions & GTK_IMHTML_SMILEY) { |
10522 | 4053 annipixbuf = gtk_smiley_tree_image(imhtml, sml, unescaped); |
10526 | 4054 if (annipixbuf) { |
4055 if (gdk_pixbuf_animation_is_static_image(annipixbuf)) { | |
10522 | 4056 pixbuf = gdk_pixbuf_animation_get_static_image(annipixbuf); |
10526 | 4057 if (pixbuf) |
10522 | 4058 icon = gtk_image_new_from_pixbuf(pixbuf); |
4059 } else { | |
4060 icon = gtk_image_new_from_animation(annipixbuf); | |
4061 } | |
8061 | 4062 } |
4063 } | |
10526 | 4064 #if 0 |
4065 else { | |
4066 GtkIMHtmlSmiley *imhtml_smiley; | |
4067 | |
4068 if (imhtml_smiley->loader) { ; } | |
4069 icon = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_MENU); | |
4070 imhtml_smiley = gtk_get_imhtml_smiley(imhtml, sml, unescaped); | |
4071 if (!imhtml_smiley) { | |
4072 gaim_debug_info("gtkimhtml", "geezz couldnt find smiley struct\n"); | |
4073 } | |
4074 imhtml_smiley->orphan = g_slist_append(imhtml_smiley->orphan, icon); | |
4075 } | |
4076 #endif | |
8061 | 4077 |
4078 if (icon) { | |
8890 | 4079 anchor = gtk_text_buffer_create_child_anchor(imhtml->text_buffer, iter); |
4080 g_object_set_data_full(G_OBJECT(anchor), "gtkimhtml_plaintext", g_strdup(unescaped), g_free); | |
4081 g_object_set_data_full(G_OBJECT(anchor), "gtkimhtml_htmltext", g_strdup(smiley), g_free); | |
4082 | |
8061 | 4083 gtk_widget_show(icon); |
4084 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(imhtml), icon, anchor); | |
8890 | 4085 } else { |
4086 gtk_text_buffer_insert(imhtml->text_buffer, iter, smiley, -1); | |
8061 | 4087 } |
8890 | 4088 |
4089 g_free(unescaped); | |
8061 | 4090 } |
4091 | |
8962 | 4092 void gtk_imhtml_insert_image_at_iter(GtkIMHtml *imhtml, int id, GtkTextIter *iter) |
4093 { | |
4094 GdkPixbuf *pixbuf = NULL; | |
4095 const char *filename = NULL; | |
4096 gpointer image; | |
4097 GdkRectangle rect; | |
4098 GtkIMHtmlScalable *scalable = NULL; | |
4099 int minus; | |
4100 | |
4101 if (!imhtml->funcs || !imhtml->funcs->image_get || | |
4102 !imhtml->funcs->image_get_size || !imhtml->funcs->image_get_data || | |
4103 !imhtml->funcs->image_get_filename || !imhtml->funcs->image_ref || | |
4104 !imhtml->funcs->image_unref) | |
4105 return; | |
4106 | |
4107 image = imhtml->funcs->image_get(id); | |
4108 | |
4109 if (image) { | |
4110 gpointer data; | |
4111 size_t len; | |
4112 | |
4113 data = imhtml->funcs->image_get_data(image); | |
4114 len = imhtml->funcs->image_get_size(image); | |
4115 | |
4116 if (data && len) { | |
4117 GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); | |
4118 gdk_pixbuf_loader_write(loader, data, len, NULL); | |
4119 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); | |
9337 | 4120 if (pixbuf) |
4121 g_object_ref(G_OBJECT(pixbuf)); | |
8962 | 4122 gdk_pixbuf_loader_close(loader, NULL); |
9337 | 4123 g_object_unref(G_OBJECT(loader)); |
8962 | 4124 } |
4125 | |
4126 } | |
4127 | |
4128 if (pixbuf) { | |
4129 filename = imhtml->funcs->image_get_filename(image); | |
4130 imhtml->funcs->image_ref(id); | |
4131 imhtml->im_images = g_slist_prepend(imhtml->im_images, GINT_TO_POINTER(id)); | |
4132 } else { | |
4133 pixbuf = gtk_widget_render_icon(GTK_WIDGET(imhtml), GTK_STOCK_MISSING_IMAGE, | |
4134 GTK_ICON_SIZE_BUTTON, "gtkimhtml-missing-image"); | |
4135 } | |
4136 | |
4137 scalable = gtk_imhtml_image_new(pixbuf, filename, id); | |
4138 gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(imhtml), &rect); | |
4139 scalable->add_to(scalable, imhtml, iter); | |
4140 minus = gtk_text_view_get_left_margin(GTK_TEXT_VIEW(imhtml)) + | |
4141 gtk_text_view_get_right_margin(GTK_TEXT_VIEW(imhtml)); | |
4142 scalable->scale(scalable, rect.width - minus, rect.height); | |
4143 imhtml->scalables = g_list_append(imhtml->scalables, scalable); | |
4144 | |
4145 g_object_unref(G_OBJECT(pixbuf)); | |
4146 } | |
4147 | |
8677 | 4148 static const gchar *tag_to_html_start(GtkTextTag *tag) |
8061 | 4149 { |
8677 | 4150 const gchar *name; |
4151 static gchar buf[1024]; | |
4152 | |
4153 name = tag->name; | |
4154 g_return_val_if_fail(name != NULL, ""); | |
4155 | |
4156 if (strcmp(name, "BOLD") == 0) { | |
4157 return "<b>"; | |
4158 } else if (strcmp(name, "ITALICS") == 0) { | |
4159 return "<i>"; | |
4160 } else if (strcmp(name, "UNDERLINE") == 0) { | |
4161 return "<u>"; | |
9924 | 4162 } else if (strcmp(name, "STRIKE") == 0) { |
4163 return "<s>"; | |
8677 | 4164 } else if (strncmp(name, "LINK ", 5) == 0) { |
4165 char *tmp = g_object_get_data(G_OBJECT(tag), "link_url"); | |
4166 if (tmp) { | |
4167 g_snprintf(buf, sizeof(buf), "<a href=\"%s\">", tmp); | |
4168 buf[sizeof(buf)-1] = '\0'; | |
4169 return buf; | |
4170 } else { | |
4171 return ""; | |
4172 } | |
4173 } else if (strncmp(name, "FORECOLOR ", 10) == 0) { | |
4174 g_snprintf(buf, sizeof(buf), "<font color=\"%s\">", &name[10]); | |
4175 return buf; | |
4176 } else if (strncmp(name, "BACKCOLOR ", 10) == 0) { | |
4177 g_snprintf(buf, sizeof(buf), "<font back=\"%s\">", &name[10]); | |
4178 return buf; | |
10776 | 4179 } else if (strncmp(name, "BACKGROUND ", 10) == 0) { |
4180 g_snprintf(buf, sizeof(buf), "<body bgcolor=\"%s\">", &name[11]); | |
4181 return buf; | |
8677 | 4182 } else if (strncmp(name, "FONT FACE ", 10) == 0) { |
4183 g_snprintf(buf, sizeof(buf), "<font face=\"%s\">", &name[10]); | |
4184 return buf; | |
4185 } else if (strncmp(name, "FONT SIZE ", 10) == 0) { | |
4186 g_snprintf(buf, sizeof(buf), "<font size=\"%s\">", &name[10]); | |
4187 return buf; | |
4188 } else { | |
4189 return ""; | |
4190 } | |
8061 | 4191 } |
4192 | |
8677 | 4193 static const gchar *tag_to_html_end(GtkTextTag *tag) |
8061 | 4194 { |
8677 | 4195 const gchar *name; |
4196 | |
4197 name = tag->name; | |
4198 g_return_val_if_fail(name != NULL, ""); | |
4199 | |
4200 if (strcmp(name, "BOLD") == 0) { | |
4201 return "</b>"; | |
4202 } else if (strcmp(name, "ITALICS") == 0) { | |
4203 return "</i>"; | |
4204 } else if (strcmp(name, "UNDERLINE") == 0) { | |
4205 return "</u>"; | |
9924 | 4206 } else if (strcmp(name, "STRIKE") == 0) { |
4207 return "</s>"; | |
8677 | 4208 } else if (strncmp(name, "LINK ", 5) == 0) { |
4209 return "</a>"; | |
4210 } else if (strncmp(name, "FORECOLOR ", 10) == 0) { | |
4211 return "</font>"; | |
4212 } else if (strncmp(name, "BACKCOLOR ", 10) == 0) { | |
4213 return "</font>"; | |
10776 | 4214 } else if (strncmp(name, "BACKGROUND ", 10) == 0) { |
4215 return "</body>"; | |
8677 | 4216 } else if (strncmp(name, "FONT FACE ", 10) == 0) { |
4217 return "</font>"; | |
4218 } else if (strncmp(name, "FONT SIZE ", 10) == 0) { | |
4219 return "</font>"; | |
4220 } else { | |
4221 return ""; | |
4222 } | |
4223 } | |
4224 | |
4225 static gboolean tag_ends_here(GtkTextTag *tag, GtkTextIter *iter, GtkTextIter *niter) | |
4226 { | |
4227 return ((gtk_text_iter_has_tag(iter, GTK_TEXT_TAG(tag)) && | |
4228 !gtk_text_iter_has_tag(niter, GTK_TEXT_TAG(tag))) || | |
4229 gtk_text_iter_is_end(niter)); | |
8061 | 4230 } |
4231 | |
4232 /* Basic notion here: traverse through the text buffer one-by-one, non-character elements, such | |
4233 * as smileys and IM images are represented by the Unicode "unknown" character. Handle them. Else | |
8677 | 4234 * check for tags that are toggled on, insert their html form, and push them on the queue. Then insert |
4235 * the actual text. Then check for tags that are toggled off and insert them, after checking the queue. | |
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8729
diff
changeset
|
4236 * Finally, replace <, >, &, and " with their HTML equivalent. |
8677 | 4237 */ |
8061 | 4238 char *gtk_imhtml_get_markup_range(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *end) |
4239 { | |
4240 gunichar c; | |
8677 | 4241 GtkTextIter iter, nextiter; |
8061 | 4242 GString *str = g_string_new(""); |
8677 | 4243 GSList *tags, *sl; |
4244 GQueue *q, *r; | |
4245 GtkTextTag *tag; | |
4246 | |
4247 q = g_queue_new(); | |
4248 r = g_queue_new(); | |
4249 | |
8061 | 4250 |
4251 gtk_text_iter_order(start, end); | |
8677 | 4252 nextiter = iter = *start; |
4253 gtk_text_iter_forward_char(&nextiter); | |
4254 | |
9071 | 4255 /* First add the tags that are already in progress (we don't care about non-printing tags)*/ |
8677 | 4256 tags = gtk_text_iter_get_tags(start); |
4257 | |
4258 for (sl = tags; sl; sl = sl->next) { | |
4259 tag = sl->data; | |
4260 if (!gtk_text_iter_toggles_tag(start, GTK_TEXT_TAG(tag))) { | |
9071 | 4261 if (strlen(tag_to_html_end(tag)) > 0) |
4262 g_string_append(str, tag_to_html_start(tag)); | |
8677 | 4263 g_queue_push_tail(q, tag); |
8061 | 4264 } |
4265 } | |
8677 | 4266 g_slist_free(tags); |
8061 | 4267 |
4268 while ((c = gtk_text_iter_get_char(&iter)) != 0 && !gtk_text_iter_equal(&iter, end)) { | |
8677 | 4269 |
4270 tags = gtk_text_iter_get_tags(&iter); | |
4271 | |
4272 for (sl = tags; sl; sl = sl->next) { | |
4273 tag = sl->data; | |
4274 if (gtk_text_iter_begins_tag(&iter, GTK_TEXT_TAG(tag))) { | |
9071 | 4275 if (strlen(tag_to_html_end(tag)) > 0) |
4276 g_string_append(str, tag_to_html_start(tag)); | |
8677 | 4277 g_queue_push_tail(q, tag); |
4278 } | |
4279 } | |
4280 | |
4281 | |
8061 | 4282 if (c == 0xFFFC) { |
4283 GtkTextChildAnchor* anchor = gtk_text_iter_get_child_anchor(&iter); | |
9071 | 4284 if (anchor) { |
4285 char *text = g_object_get_data(G_OBJECT(anchor), "gtkimhtml_htmltext"); | |
4286 if (text) | |
4287 str = g_string_append(str, text); | |
4288 } | |
8677 | 4289 } else if (c == '<') { |
4290 str = g_string_append(str, "<"); | |
4291 } else if (c == '>') { | |
4292 str = g_string_append(str, ">"); | |
4293 } else if (c == '&') { | |
4294 str = g_string_append(str, "&"); | |
4295 } else if (c == '"') { | |
4296 str = g_string_append(str, """); | |
4297 } else if (c == '\n') { | |
4298 str = g_string_append(str, "<br>"); | |
8061 | 4299 } else { |
8677 | 4300 str = g_string_append_unichar(str, c); |
4301 } | |
4302 | |
4303 tags = g_slist_reverse(tags); | |
4304 for (sl = tags; sl; sl = sl->next) { | |
4305 tag = sl->data; | |
9071 | 4306 /** don't worry about non-printing tags ending */ |
4307 if (tag_ends_here(tag, &iter, &nextiter) && strlen(tag_to_html_end(tag)) > 0) { | |
8677 | 4308 |
4309 GtkTextTag *tmp; | |
4310 | |
4311 while ((tmp = g_queue_pop_tail(q)) != tag) { | |
4312 if (tmp == NULL) | |
4313 break; | |
4314 | |
9071 | 4315 if (!tag_ends_here(tmp, &iter, &nextiter) && strlen(tag_to_html_end(tmp)) > 0) |
8677 | 4316 g_queue_push_tail(r, tmp); |
4317 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tmp))); | |
4318 } | |
4319 | |
4320 if (tmp == NULL) | |
4321 gaim_debug_warning("gtkimhtml", "empty queue, more closing tags than open tags!\n"); | |
4322 else | |
4323 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tag))); | |
4324 | |
4325 while ((tmp = g_queue_pop_head(r))) { | |
4326 g_string_append(str, tag_to_html_start(GTK_TEXT_TAG(tmp))); | |
4327 g_queue_push_tail(q, tmp); | |
8061 | 4328 } |
4329 } | |
4330 } | |
8677 | 4331 |
4332 g_slist_free(tags); | |
8061 | 4333 gtk_text_iter_forward_char(&iter); |
8677 | 4334 gtk_text_iter_forward_char(&nextiter); |
8061 | 4335 } |
8677 | 4336 |
4337 while ((tag = g_queue_pop_tail(q))) | |
4338 g_string_append(str, tag_to_html_end(GTK_TEXT_TAG(tag))); | |
4339 | |
4340 g_queue_free(q); | |
4341 g_queue_free(r); | |
8061 | 4342 return g_string_free(str, FALSE); |
4343 } | |
4344 | |
8698 | 4345 void gtk_imhtml_close_tags(GtkIMHtml *imhtml, GtkTextIter *iter) |
8061 | 4346 { |
4347 if (imhtml->edit.bold) | |
4348 gtk_imhtml_toggle_bold(imhtml); | |
4349 | |
4350 if (imhtml->edit.italic) | |
4351 gtk_imhtml_toggle_italic(imhtml); | |
4352 | |
4353 if (imhtml->edit.underline) | |
4354 gtk_imhtml_toggle_underline(imhtml); | |
4355 | |
9924 | 4356 if (imhtml->edit.strike) |
4357 gtk_imhtml_toggle_strike(imhtml); | |
4358 | |
8061 | 4359 if (imhtml->edit.forecolor) |
4360 gtk_imhtml_toggle_forecolor(imhtml, NULL); | |
4361 | |
4362 if (imhtml->edit.backcolor) | |
4363 gtk_imhtml_toggle_backcolor(imhtml, NULL); | |
4364 | |
4365 if (imhtml->edit.fontface) | |
4366 gtk_imhtml_toggle_fontface(imhtml, NULL); | |
4367 | |
8677 | 4368 imhtml->edit.fontsize = 0; |
4369 | |
8719 | 4370 if (imhtml->edit.link) |
4371 gtk_imhtml_toggle_link(imhtml, NULL); | |
4372 | |
8698 | 4373 gtk_text_buffer_remove_all_tags(imhtml->text_buffer, iter, iter); |
8061 | 4374 |
4375 } | |
4376 | |
4377 char *gtk_imhtml_get_markup(GtkIMHtml *imhtml) | |
4378 { | |
4379 GtkTextIter start, end; | |
4380 | |
4381 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
4382 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
4383 return gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
4384 } | |
4385 | |
8677 | 4386 char **gtk_imhtml_get_markup_lines(GtkIMHtml *imhtml) |
4387 { | |
4388 int i, j, lines; | |
4389 GtkTextIter start, end; | |
4390 char **ret; | |
4391 | |
4392 lines = gtk_text_buffer_get_line_count(imhtml->text_buffer); | |
4393 ret = g_new0(char *, lines + 1); | |
4394 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &start); | |
4395 end = start; | |
4396 gtk_text_iter_forward_to_line_end(&end); | |
4397 | |
4398 for (i = 0, j = 0; i < lines; i++) { | |
9612 | 4399 if (gtk_text_iter_get_char(&start) != '\n') { |
4400 ret[j] = gtk_imhtml_get_markup_range(imhtml, &start, &end); | |
4401 if (ret[j] != NULL) | |
4402 j++; | |
4403 } | |
4404 | |
8677 | 4405 gtk_text_iter_forward_line(&start); |
4406 end = start; | |
4407 gtk_text_iter_forward_to_line_end(&end); | |
4408 } | |
4409 | |
4410 return ret; | |
4411 } | |
4412 | |
4413 char *gtk_imhtml_get_text(GtkIMHtml *imhtml, GtkTextIter *start, GtkTextIter *stop) | |
8061 | 4414 { |
8519 | 4415 GString *str = g_string_new(""); |
4416 GtkTextIter iter, end; | |
4417 gunichar c; | |
4418 | |
8677 | 4419 if (start == NULL) |
4420 gtk_text_buffer_get_start_iter(imhtml->text_buffer, &iter); | |
4421 else | |
4422 iter = *start; | |
4423 | |
4424 if (stop == NULL) | |
4425 gtk_text_buffer_get_end_iter(imhtml->text_buffer, &end); | |
4426 else | |
4427 end = *stop; | |
4428 | |
4429 gtk_text_iter_order(&iter, &end); | |
8519 | 4430 |
4431 while ((c = gtk_text_iter_get_char(&iter)) != 0 && !gtk_text_iter_equal(&iter, &end)) { | |
4432 if (c == 0xFFFC) { | |
8677 | 4433 GtkTextChildAnchor* anchor; |
4434 char *text = NULL; | |
4435 | |
4436 anchor = gtk_text_iter_get_child_anchor(&iter); | |
4437 if (anchor) | |
8698 | 4438 text = g_object_get_data(G_OBJECT(anchor), "gtkimhtml_plaintext"); |
8677 | 4439 if (text) |
4440 str = g_string_append(str, text); | |
8519 | 4441 } else { |
4442 g_string_append_unichar(str, c); | |
4443 } | |
4444 gtk_text_iter_forward_char(&iter); | |
4445 } | |
4446 | |
4447 return g_string_free(str, FALSE); | |
8061 | 4448 } |
8962 | 4449 |
4450 void gtk_imhtml_set_funcs(GtkIMHtml *imhtml, GtkIMHtmlFuncs *f) | |
4451 { | |
4452 g_return_if_fail(imhtml != NULL); | |
4453 imhtml->funcs = f; | |
4454 } |