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