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