1 /* gtktextbufferserialize.c
3 * Copyright (C) 2001 Havoc Pennington
4 * Copyright (C) 2004 Nokia Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 /* FIXME: We should use other error codes for the
23 * parts that deal with the format errors
32 #include "gdk-pixbuf/gdk-pixdata.h"
33 #include "gtktextbufferserialize.h"
40 GString *tag_table_str;
43 GtkTextIter start, end;
48 GHashTable *tag_id_tags;
49 } SerializationContext;
52 serialize_value (GValue *value)
54 if (g_value_type_transformable (value->g_type, G_TYPE_STRING))
56 GValue text_value = { 0 };
59 g_value_init (&text_value, G_TYPE_STRING);
60 g_value_transform (value, &text_value);
62 tmp = g_markup_escape_text (g_value_get_string (&text_value), -1);
63 g_value_unset (&text_value);
67 else if (value->g_type == GDK_TYPE_COLOR)
69 GdkColor *color = g_value_get_boxed (value);
71 return g_strdup_printf ("%x:%x:%x", color->red, color->green, color->blue);
73 else if (g_type_is_a (value->g_type, GDK_TYPE_DRAWABLE))
75 /* Don't do anything */
79 g_warning ("Type %s is not serializable\n", g_type_name (value->g_type));
86 deserialize_value (const gchar *str,
89 if (g_value_type_transformable (G_TYPE_STRING, value->g_type))
91 GValue text_value = { 0 };
94 g_value_init (&text_value, G_TYPE_STRING);
95 g_value_set_static_string (&text_value, str);
97 retval = g_value_transform (&text_value, value);
98 g_value_unset (&text_value);
102 else if (value->g_type == G_TYPE_BOOLEAN)
106 v = strcmp (str, "TRUE") == 0;
108 g_value_set_boolean (value, v);
112 else if (value->g_type == G_TYPE_INT)
117 v = strtol (str, &tmp, 10);
119 if (tmp == NULL || tmp == str)
122 g_value_set_int (value, v);
126 else if (value->g_type == G_TYPE_DOUBLE)
131 v = g_ascii_strtod (str, &tmp);
133 if (tmp == NULL || tmp == str)
136 g_value_set_double (value, v);
140 else if (value->g_type == GDK_TYPE_COLOR)
147 color.red = strtol (old, &tmp, 16);
149 if (tmp == NULL || tmp == old)
156 color.green = strtol (old, &tmp, 16);
157 if (tmp == NULL || tmp == old)
164 color.blue = strtol (old, &tmp, 16);
166 if (tmp == NULL || tmp == old || *tmp != '\0')
169 g_value_set_boxed (value, &color);
173 else if (G_VALUE_HOLDS_ENUM (value))
175 GEnumClass *class = G_ENUM_CLASS (g_type_class_peek (value->g_type));
176 GEnumValue *enum_value;
178 enum_value = g_enum_get_value_by_name (class, str);
182 g_value_set_enum (value, enum_value->value);
190 g_warning ("Type %s can not be deserialized\n", g_type_name (value->g_type));
196 /* Checks if a param is set, or if it's the default value */
198 is_param_set (GObject *object,
202 /* We need to special case some attributes here */
203 if (strcmp (pspec->name, "background-gdk") == 0)
207 g_object_get (object, "background-set", &is_set, NULL);
211 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
213 g_object_get_property (object, pspec->name, value);
220 else if (strcmp (pspec->name, "foreground-gdk") == 0)
224 g_object_get (object, "foreground-set", &is_set, NULL);
228 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
230 g_object_get_property (object, pspec->name, value);
242 is_set_name = g_strdup_printf ("%s-set", pspec->name);
244 if (g_object_class_find_property (G_OBJECT_GET_CLASS (object), is_set_name) == NULL)
246 g_free (is_set_name);
251 g_object_get (object, is_set_name, &is_set, NULL);
255 g_free (is_set_name);
259 g_free (is_set_name);
261 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
263 g_object_get_property (object, pspec->name, value);
265 if (g_param_value_defaults (pspec, value))
267 g_value_unset (value);
277 serialize_tag (gpointer key,
281 SerializationContext *context = user_data;
282 GtkTextTag *tag = data;
289 g_string_append (context->tag_table_str, " <tag ");
291 /* Handle anonymous tags */
294 tag_name = g_markup_escape_text (tag->name, -1);
295 g_string_append_printf (context->tag_table_str, "name=\"%s\"", tag_name);
300 tag_id = GPOINTER_TO_INT (g_hash_table_lookup (context->tag_id_tags, tag));
302 g_string_append_printf (context->tag_table_str, "id=\"%d\"", tag_id);
305 g_string_append_printf (context->tag_table_str, " priority=\"%d\">\n", tag->priority);
307 /* Serialize properties */
308 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (tag), &n_pspecs);
310 for (i = 0; i < n_pspecs; i++)
312 GValue value = { 0 };
315 if (!(pspecs[i]->flags & G_PARAM_READABLE) ||
316 !(pspecs[i]->flags & G_PARAM_WRITABLE))
319 if (!is_param_set (G_OBJECT (tag), pspecs[i], &value))
322 /* Now serialize the attr */
323 tmp2 = serialize_value (&value);
327 tmp = g_markup_escape_text (pspecs[i]->name, -1);
328 g_string_append_printf (context->tag_table_str, " <attr name=\"%s\" ", tmp);
331 tmp = g_markup_escape_text (g_type_name (pspecs[i]->value_type), -1);
332 g_string_append_printf (context->tag_table_str, "type=\"%s\" value=\"%s\" />\n", tmp, tmp2);
338 g_value_unset (&value);
343 g_string_append (context->tag_table_str, " </tag>\n");
347 serialize_tags (SerializationContext *context)
349 g_string_append (context->tag_table_str, " <text_view_markup>\n");
350 g_string_append (context->tag_table_str, " <tags>\n");
351 g_hash_table_foreach (context->tags, serialize_tag, context);
352 g_string_append (context->tag_table_str, " </tags>\n");
357 dump_tag_list (const gchar *str,
360 g_print ("%s: ", str);
368 g_print ("%s ", ((GtkTextTag *)list->data)->name);
378 find_list_delta (GSList *old_list,
384 GList *tmp_added, *tmp_removed;
389 /* Find added tags */
393 if (!g_slist_find (old_list, tmp->data))
394 tmp_added = g_list_prepend (tmp_added, tmp->data);
401 /* Find removed tags */
405 if (!g_slist_find (new_list, tmp->data))
406 tmp_removed = g_list_prepend (tmp_removed, tmp->data);
411 /* We reverse the list here to match the xml semantics */
412 *removed = g_list_reverse (tmp_removed);
416 serialize_section_header (GString *str,
420 g_return_if_fail (strlen (name) == 26);
422 g_string_append (str, name);
424 g_string_append_c (str, length >> 24);
426 g_string_append_c (str, (length >> 16) & 0xff);
427 g_string_append_c (str, (length >> 8) & 0xff);
428 g_string_append_c (str, length & 0xff);
432 serialize_text (GtkTextBuffer *buffer,
433 SerializationContext *context)
435 GtkTextIter iter, old_iter;
436 GSList *tag_list, *new_tag_list;
439 g_string_append (context->text_str, "<text>");
441 iter = context->start;
447 GList *added, *removed;
449 gchar *tmp_text, *escaped_text;
451 new_tag_list = gtk_text_iter_get_tags (&iter);
452 find_list_delta (tag_list, new_tag_list, &added, &removed);
454 /* Handle removed tags */
455 for (tmp = removed; tmp; tmp = tmp->next)
457 GtkTextTag *tag = tmp->data;
459 /* Only close the tag if we didn't close it before (by using
460 * the stack logic in the while() loop below)
462 if (g_slist_find (active_tags, tag))
464 g_string_append (context->text_str, "</apply_tag>");
466 /* Drop all tags that were opened after this one (which are
467 * above this on in the stack)
469 while (active_tags->data != tag)
471 added = g_list_prepend (added, active_tags->data);
472 active_tags = g_slist_remove (active_tags, active_tags->data);
473 g_string_append_printf (context->text_str, "</apply_tag>");
476 active_tags = g_slist_remove (active_tags, active_tags->data);
480 /* Handle added tags */
481 for (tmp = added; tmp; tmp = tmp->next)
483 GtkTextTag *tag = tmp->data;
486 /* Add it to the tag hash table */
487 g_hash_table_insert (context->tags, tag, tag);
491 tag_name = g_markup_escape_text (tag->name, -1);
493 g_string_append_printf (context->text_str, "<apply_tag name=\"%s\">", tag_name);
500 /* We've got an anonymous tag, find out if it's been
502 if (!g_hash_table_lookup_extended (context->tag_id_tags, tag, NULL, &tag_id))
504 tag_id = GINT_TO_POINTER (context->tag_id++);
506 g_hash_table_insert (context->tag_id_tags, tag, tag_id);
509 g_string_append_printf (context->text_str, "<apply_tag id=\"%d\">", GPOINTER_TO_INT (tag_id));
512 active_tags = g_slist_prepend (active_tags, tag);
515 g_slist_free (tag_list);
516 tag_list = new_tag_list;
519 g_list_free (removed);
523 /* Now try to go to either the next tag toggle, or if a pixbuf appears */
526 gunichar ch = gtk_text_iter_get_char (&iter);
530 GdkPixbuf *pixbuf = gtk_text_iter_get_pixbuf (&iter);
534 /* Append the text before the pixbuf */
535 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
536 escaped_text = g_markup_escape_text (tmp_text, -1);
539 /* Forward so we don't get the 0xfffc char */
540 gtk_text_iter_forward_char (&iter);
543 g_string_append (context->text_str, escaped_text);
544 g_free (escaped_text);
546 g_string_append_printf (context->text_str, "<pixbuf index=\"%d\" />", context->n_pixbufs);
548 context->n_pixbufs++;
549 context->pixbufs = g_list_prepend (context->pixbufs, pixbuf);
557 gtk_text_iter_forward_char (&iter);
559 if (gtk_text_iter_toggles_tag (&iter, NULL))
563 /* We might have moved too far */
564 if (gtk_text_iter_compare (&iter, &context->end) > 0)
567 /* Append the text */
568 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
569 escaped_text = g_markup_escape_text (tmp_text, -1);
572 g_string_append (context->text_str, escaped_text);
573 g_free (escaped_text);
575 while (!gtk_text_iter_equal (&iter, &context->end));
577 /* Close any open tags */
578 for (tag_list = active_tags; tag_list; tag_list = tag_list->next)
579 g_string_append (context->text_str, "</apply_tag>");
581 g_slist_free (active_tags);
582 g_string_append (context->text_str, "</text>\n</text_view_markup>\n");
586 serialize_pixbufs (SerializationContext *context,
591 for (list = context->pixbufs; list != NULL; list = list->next)
593 GdkPixbuf *pixbuf = list->data;
598 gdk_pixdata_from_pixbuf (&pixdata, pixbuf, FALSE);
599 tmp = gdk_pixdata_serialize (&pixdata, &len);
601 serialize_section_header (text, "GTKTEXTBUFFERPIXBDATA-0001", len);
602 g_string_append_len (text, (gchar *) tmp, len);
608 _gtk_text_buffer_serialize_rich_text (GtkTextBuffer *register_buffer,
609 GtkTextBuffer *content_buffer,
610 const GtkTextIter *start,
611 const GtkTextIter *end,
615 SerializationContext context;
618 context.tags = g_hash_table_new (NULL, NULL);
619 context.text_str = g_string_new (NULL);
620 context.tag_table_str = g_string_new (NULL);
621 context.start = *start;
623 context.n_pixbufs = 0;
624 context.pixbufs = NULL;
626 context.tag_id_tags = g_hash_table_new (NULL, NULL);
628 /* We need to serialize the text before the tag table so we know
629 what tags are used */
630 serialize_text (content_buffer, &context);
631 serialize_tags (&context);
633 text = g_string_new (NULL);
634 serialize_section_header (text, "GTKTEXTBUFFERCONTENTS-0001",
635 context.tag_table_str->len + context.text_str->len);
637 g_string_append_len (text, context.tag_table_str->str, context.tag_table_str->len);
638 g_string_append_len (text, context.text_str->str, context.text_str->len);
640 context.pixbufs = g_list_reverse (context.pixbufs);
641 serialize_pixbufs (&context, text);
643 g_hash_table_destroy (context.tags);
644 g_list_free (context.pixbufs);
645 g_string_free (context.text_str, TRUE);
646 g_string_free (context.tag_table_str, TRUE);
647 g_hash_table_destroy (context.tag_id_tags);
651 return (guint8 *) g_string_free (text, FALSE);
657 STATE_TEXT_VIEW_MARKUP,
685 GtkTextBuffer *buffer;
687 /* Tags that are defined in <tag> elements */
688 GHashTable *defined_tags;
690 /* Tags that are anonymous */
691 GHashTable *anonymous_tags;
693 /* Tag name substitutions */
694 GHashTable *substitutions;
697 GtkTextTag *current_tag;
699 /* Priority of current tag */
700 gint current_tag_prio;
702 /* Id of current tag */
705 /* Tags and their priorities */
706 GList *tag_priorities;
712 gboolean create_tags;
714 gboolean parsed_text;
715 gboolean parsed_tags;
719 set_error (GError **err,
720 GMarkupParseContext *context,
730 g_markup_parse_context_get_position (context, &line, &ch);
732 va_start (args, format);
733 str = g_strdup_vprintf (format, args);
736 g_set_error (err, error_domain, error_code,
737 ("Line %d character %d: %s"),
744 push_state (ParseInfo *info,
747 info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
751 pop_state (ParseInfo *info)
753 g_return_if_fail (info->states != NULL);
755 info->states = g_slist_remove (info->states, info->states->data);
759 peek_state (ParseInfo *info)
761 g_return_val_if_fail (info->states != NULL, STATE_START);
763 return GPOINTER_TO_INT (info->states->data);
766 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
770 check_id_or_name (GMarkupParseContext *context,
771 const gchar *element_name,
772 const gchar **attribute_names,
773 const gchar **attribute_values,
778 gboolean has_id = FALSE;
779 gboolean has_name = FALSE;
785 for (i = 0; attribute_names[i] != NULL; i++)
787 if (strcmp (attribute_names[i], "name") == 0)
789 *name = attribute_values[i];
793 set_error (error, context,
795 G_MARKUP_ERROR_PARSE,
796 _("Both \"id\" and \"name\" were found on the <%s> element"),
803 set_error (error, context,
805 G_MARKUP_ERROR_PARSE,
806 _("The attribute \"%s\" was found twice on the <%s> element"),
807 "name", element_name);
813 else if (strcmp (attribute_names[i], "id") == 0)
819 set_error (error, context,
821 G_MARKUP_ERROR_PARSE,
822 _("Both \"id\" and \"name\" were found on the <%s> element"),
829 set_error (error, context,
831 G_MARKUP_ERROR_PARSE,
832 _("The attribute \"%s\" was found twice on the <%s> element"),
839 /* Try parsing the integer */
840 *id = strtol (attribute_values[i], &tmp, 10);
842 if (tmp == NULL || tmp == attribute_values[i])
844 set_error (error, context,
845 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
846 _("<%s> element has invalid id \"%s\""), attribute_values[i]);
852 if (!has_id && !has_name)
854 set_error (error, context,
855 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
856 _("<%s> element has neither a \"name\" nor an \"id\" attribute"), element_name);
870 locate_attributes (GMarkupParseContext *context,
871 const char *element_name,
872 const char **attribute_names,
873 const char **attribute_values,
874 gboolean allow_unknown_attrs,
876 const char *first_attribute_name,
877 const char **first_attribute_retloc,
885 LocateAttr attrs[MAX_ATTRS];
889 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
890 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
895 attrs[0].name = first_attribute_name;
896 attrs[0].retloc = first_attribute_retloc;
897 *first_attribute_retloc = NULL;
899 va_start (args, first_attribute_retloc);
901 name = va_arg (args, const char*);
902 retloc = va_arg (args, const char**);
906 g_return_val_if_fail (retloc != NULL, FALSE);
908 g_assert (n_attrs < MAX_ATTRS);
910 attrs[n_attrs].name = name;
911 attrs[n_attrs].retloc = retloc;
915 name = va_arg (args, const char*);
916 retloc = va_arg (args, const char**);
925 while (attribute_names[i])
934 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
936 retloc = attrs[j].retloc;
940 set_error (error, context,
942 G_MARKUP_ERROR_PARSE,
943 _("Attribute \"%s\" repeated twice on the same <%s> element"),
944 attrs[j].name, element_name);
949 *retloc = attribute_values[i];
956 if (!found && !allow_unknown_attrs)
958 set_error (error, context,
960 G_MARKUP_ERROR_PARSE,
961 _("Attribute \"%s\" is invalid on <%s> element in this context"),
962 attribute_names[i], element_name);
975 check_no_attributes (GMarkupParseContext *context,
976 const char *element_name,
977 const char **attribute_names,
978 const char **attribute_values,
981 if (attribute_names[0] != NULL)
983 set_error (error, context,
985 G_MARKUP_ERROR_PARSE,
986 _("Attribute \"%s\" is invalid on <%s> element in this context"),
987 attribute_names[0], element_name);
995 tag_exists (GMarkupParseContext *context,
1001 const gchar *real_name;
1003 if (info->create_tags)
1005 /* If we have an anonymous tag, just return it directly */
1007 return g_hash_table_lookup (info->anonymous_tags,
1008 GINT_TO_POINTER (id));
1010 /* First, try the substitutions */
1011 real_name = g_hash_table_lookup (info->substitutions, name);
1014 return gtk_text_tag_table_lookup (info->buffer->tag_table, real_name);
1016 /* Next, try the list of defined tags */
1017 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1018 return gtk_text_tag_table_lookup (info->buffer->tag_table, name);
1020 set_error (error, context,
1021 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1022 _("Tag \"%s\" has not been defined."), name);
1032 set_error (error, context,
1033 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1034 _("Anonymous tag found and tags can not be created."));
1038 tag = gtk_text_tag_table_lookup (info->buffer->tag_table, name);
1043 set_error (error, context,
1044 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1045 _("Tag \"%s\" does not exist in buffer and tags can not be created."), name);
1059 get_pixbuf_from_headers (GList *headers,
1067 header = g_list_nth_data (headers, id);
1072 if (!gdk_pixdata_deserialize (&pixdata, header->length,
1073 (const guint8 *) header->start, error))
1076 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, TRUE, error);
1082 parse_apply_tag_element (GMarkupParseContext *context,
1083 const gchar *element_name,
1084 const gchar **attribute_names,
1085 const gchar **attribute_values,
1089 const gchar *name, *priority;
1093 g_assert (peek_state (info) == STATE_TEXT ||
1094 peek_state (info) == STATE_APPLY_TAG);
1096 if (ELEMENT_IS ("apply_tag"))
1098 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1099 "priority", &priority, NULL))
1102 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1107 tag = tag_exists (context, name, id, info, error);
1112 info->tag_stack = g_slist_prepend (info->tag_stack, tag);
1114 push_state (info, STATE_APPLY_TAG);
1116 else if (ELEMENT_IS ("pixbuf"))
1121 const gchar *pixbuf_id;
1123 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1124 "index", &pixbuf_id, NULL))
1127 int_id = atoi (pixbuf_id);
1128 pixbuf = get_pixbuf_from_headers (info->headers, int_id, error);
1130 span = g_new0 (TextSpan, 1);
1131 span->pixbuf = pixbuf;
1134 info->spans = g_list_prepend (info->spans, span);
1139 push_state (info, STATE_PIXBUF);
1142 set_error (error, context,
1143 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1144 _("Element <%s> is not allowed below <%s>"),
1145 element_name, peek_state(info) == STATE_TEXT ? "text" : "apply_tag");
1149 parse_attr_element (GMarkupParseContext *context,
1150 const gchar *element_name,
1151 const gchar **attribute_names,
1152 const gchar **attribute_values,
1156 const gchar *name, *type, *value;
1158 GValue gvalue = { 0 };
1161 g_assert (peek_state (info) == STATE_TAG);
1163 if (ELEMENT_IS ("attr"))
1165 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1166 "name", &name, "type", &type, "value", &value, NULL))
1169 gtype = g_type_from_name (type);
1171 if (gtype == G_TYPE_INVALID)
1173 set_error (error, context,
1174 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1175 _("\"%s\" is not a valid attribute type"), type);
1179 if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (info->current_tag), name)))
1181 set_error (error, context,
1182 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1183 _("\"%s\" is not a valid attribute name"), name);
1187 g_value_init (&gvalue, gtype);
1189 if (!deserialize_value (value, &gvalue))
1191 set_error (error, context,
1192 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1193 _("\"%s\" could not be converted to a value of type \"%s\" for attribute \"%s\""),
1198 if (g_param_value_validate (pspec, &gvalue))
1200 set_error (error, context,
1201 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1202 _("\"%s\" is not a valid value for attribute \"%s\""),
1204 g_value_unset (&gvalue);
1208 g_object_set_property (G_OBJECT (info->current_tag),
1211 g_value_unset (&gvalue);
1213 push_state (info, STATE_ATTR);
1217 set_error (error, context,
1218 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1219 _("Element <%s> is not allowed below <%s>"),
1220 element_name, "tag");
1226 get_tag_name (ParseInfo *info,
1227 const gchar *tag_name)
1232 name = g_strdup (tag_name);
1234 if (!info->create_tags)
1239 while (gtk_text_tag_table_lookup (info->buffer->tag_table, name) != NULL)
1242 name = g_strdup_printf ("%s-%d", tag_name, ++i);
1247 g_hash_table_insert (info->substitutions, g_strdup (tag_name), g_strdup (name));
1254 parse_tag_element (GMarkupParseContext *context,
1255 const gchar *element_name,
1256 const gchar **attribute_names,
1257 const gchar **attribute_values,
1261 const gchar *name, *priority;
1267 g_assert (peek_state (info) == STATE_TAGS);
1269 if (ELEMENT_IS ("tag"))
1271 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1272 "priority", &priority, NULL))
1275 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1281 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1283 set_error (error, context,
1284 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1285 _("Tag \"%s\" already defined"), name);
1290 prio = strtol (priority, &tmp, 10);
1292 if (tmp == NULL || tmp == priority)
1294 set_error (error, context,
1295 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1296 _("Tag \"%s\" has invalid priority \"%s\""), name, priority);
1302 tag_name = get_tag_name (info, name);
1303 info->current_tag = gtk_text_tag_new (tag_name);
1308 info->current_tag = gtk_text_tag_new (NULL);
1309 info->current_tag_id = id;
1312 info->current_tag_prio = prio;
1314 push_state (info, STATE_TAG);
1318 set_error (error, context,
1319 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1320 _("Element <%s> is not allowed below <%s>"),
1321 element_name, "tags");
1326 start_element_handler (GMarkupParseContext *context,
1327 const gchar *element_name,
1328 const gchar **attribute_names,
1329 const gchar **attribute_values,
1333 ParseInfo *info = user_data;
1335 switch (peek_state (info))
1338 if (ELEMENT_IS ("text_view_markup"))
1340 if (!check_no_attributes (context, element_name,
1341 attribute_names, attribute_values, error))
1344 push_state (info, STATE_TEXT_VIEW_MARKUP);
1348 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1349 _("Outermost element in text must be <text_view_markup> not <%s>"),
1352 case STATE_TEXT_VIEW_MARKUP:
1353 if (ELEMENT_IS ("tags"))
1355 if (info->parsed_tags)
1357 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1358 _("A <%s> element has already been specified"), "tags");
1362 if (!check_no_attributes (context, element_name,
1363 attribute_names, attribute_values, error))
1366 push_state (info, STATE_TAGS);
1369 else if (ELEMENT_IS ("text"))
1371 if (info->parsed_text)
1373 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1374 _("A <%s> element has already been specified"), "text");
1377 else if (!info->parsed_tags)
1379 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1380 _("A <text> element can't occur before a <tags> element"));
1384 if (!check_no_attributes (context, element_name,
1385 attribute_names, attribute_values, error))
1388 push_state (info, STATE_TEXT);
1392 set_error (error, context,
1393 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1394 _("Element <%s> is not allowed below <%s>"),
1395 element_name, "text_view_markup");
1398 parse_tag_element (context, element_name,
1399 attribute_names, attribute_values,
1403 parse_attr_element (context, element_name,
1404 attribute_names, attribute_values,
1408 case STATE_APPLY_TAG:
1409 parse_apply_tag_element (context, element_name,
1410 attribute_names, attribute_values,
1414 g_assert_not_reached ();
1420 sort_tag_prio (TextTagPrio *a,
1423 if (a->prio < b->prio)
1425 else if (a->prio > b->prio)
1432 end_element_handler (GMarkupParseContext *context,
1433 const gchar *element_name,
1437 ParseInfo *info = user_data;
1441 switch (peek_state (info))
1445 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1447 info->parsed_tags = TRUE;
1449 /* Sort list and add the tags */
1450 info->tag_priorities = g_list_sort (info->tag_priorities,
1451 (GCompareFunc)sort_tag_prio);
1452 list = info->tag_priorities;
1455 TextTagPrio *prio = list->data;
1457 if (info->create_tags)
1458 gtk_text_tag_table_add (info->buffer->tag_table, prio->tag);
1460 g_object_unref (prio->tag);
1469 g_assert (peek_state (info) == STATE_TAGS);
1471 if (info->current_tag->name)
1473 /* Add tag to defined tags hash */
1474 tmp = g_strdup (info->current_tag->name);
1475 g_hash_table_insert (info->defined_tags,
1480 g_hash_table_insert (info->anonymous_tags,
1481 GINT_TO_POINTER (info->current_tag_id),
1485 if (info->create_tags)
1489 /* add the tag to the list */
1490 prio = g_new0 (TextTagPrio, 1);
1491 prio->prio = info->current_tag_prio;
1492 prio->tag = info->current_tag;
1494 info->tag_priorities = g_list_prepend (info->tag_priorities, prio);
1497 info->current_tag = NULL;
1501 g_assert (peek_state (info) == STATE_TAG);
1503 case STATE_APPLY_TAG:
1505 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1506 peek_state (info) == STATE_TEXT);
1509 info->tag_stack = g_slist_delete_link (info->tag_stack,
1515 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1517 info->spans = g_list_reverse (info->spans);
1518 info->parsed_text = TRUE;
1520 case STATE_TEXT_VIEW_MARKUP:
1522 g_assert (peek_state (info) == STATE_START);
1526 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1527 peek_state (info) == STATE_TEXT);
1530 g_assert_not_reached ();
1536 all_whitespace (const char *text,
1543 end = text + text_len;
1547 if (!g_ascii_isspace (*p))
1550 p = g_utf8_next_char (p);
1557 text_handler (GMarkupParseContext *context,
1563 ParseInfo *info = user_data;
1566 if (all_whitespace (text, text_len) &&
1567 peek_state (info) != STATE_TEXT &&
1568 peek_state (info) != STATE_APPLY_TAG)
1571 switch (peek_state (info))
1574 g_assert_not_reached (); /* gmarkup shouldn't do this */
1577 case STATE_APPLY_TAG:
1581 span = g_new0 (TextSpan, 1);
1582 span->text = g_strndup (text, text_len);
1583 span->tags = g_slist_copy (info->tag_stack);
1585 info->spans = g_list_prepend (info->spans, span);
1588 g_assert_not_reached ();
1594 parse_info_init (ParseInfo *info,
1595 GtkTextBuffer *buffer,
1596 gboolean create_tags,
1599 info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
1601 info->create_tags = create_tags;
1602 info->headers = headers;
1603 info->defined_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1604 info->substitutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1605 info->anonymous_tags = g_hash_table_new_full (NULL, NULL, NULL, NULL);
1606 info->tag_stack = NULL;
1608 info->parsed_text = FALSE;
1609 info->parsed_tags = FALSE;
1610 info->current_tag = NULL;
1611 info->current_tag_prio = -1;
1612 info->tag_priorities = NULL;
1614 info->buffer = buffer;
1618 text_span_free (TextSpan *span)
1620 g_free (span->text);
1621 g_slist_free (span->tags);
1626 parse_info_free (ParseInfo *info)
1630 g_slist_free (info->tag_stack);
1631 g_slist_free (info->states);
1633 g_hash_table_destroy (info->substitutions);
1634 g_hash_table_destroy (info->defined_tags);
1636 if (info->current_tag)
1637 g_object_unref (info->current_tag);
1642 text_span_free (list->data);
1646 g_list_free (info->spans);
1648 list = info->tag_priorities;
1651 TextTagPrio *prio = list->data;
1654 g_object_unref (prio->tag);
1659 g_list_free (info->tag_priorities);
1664 insert_text (ParseInfo *info,
1667 GtkTextIter start_iter;
1674 mark = gtk_text_buffer_create_mark (info->buffer, "deserialize_insert_point",
1680 TextSpan *span = tmp->data;
1683 gtk_text_buffer_insert (info->buffer, iter, span->text, -1);
1686 gtk_text_buffer_insert_pixbuf (info->buffer, iter, span->pixbuf);
1687 g_object_unref (span->pixbuf);
1689 gtk_text_buffer_get_iter_at_mark (info->buffer, &start_iter, mark);
1695 GtkTextTag *tag = tags->data;
1697 gtk_text_buffer_apply_tag (info->buffer, tag,
1703 gtk_text_buffer_move_mark (info->buffer, mark, iter);
1708 gtk_text_buffer_delete_mark (info->buffer, mark);
1714 read_int (const guchar *start)
1728 header_is (Header *header,
1731 return (strncmp (header->id, id, strlen (id)) == 0);
1735 read_headers (const gchar *start,
1742 GList *headers = NULL;
1749 if (strncmp (start + i, "GTKTEXTBUFFERCONTENTS-0001", 26) == 0 ||
1750 strncmp (start + i, "GTKTEXTBUFFERPIXBDATA-0001", 26) == 0)
1752 section_len = read_int ((const guchar *) start + i + 26);
1754 if (i + 30 + section_len > len)
1757 header = g_new0 (Header, 1);
1758 header->id = start + i;
1759 header->length = section_len;
1760 header->start = start + i + 30;
1762 i += 30 + section_len;
1764 headers = g_list_prepend (headers, header);
1770 return g_list_reverse (headers);
1773 g_list_foreach (headers, (GFunc) g_free, NULL);
1774 g_list_free (headers);
1776 g_set_error_literal (error,
1778 G_MARKUP_ERROR_PARSE,
1779 _("Serialized data is malformed"));
1785 deserialize_text (GtkTextBuffer *buffer,
1789 gboolean create_tags,
1793 GMarkupParseContext *context;
1795 gboolean retval = FALSE;
1797 static const GMarkupParser rich_text_parser = {
1798 start_element_handler,
1799 end_element_handler,
1805 parse_info_init (&info, buffer, create_tags, headers);
1807 context = g_markup_parse_context_new (&rich_text_parser,
1810 if (!g_markup_parse_context_parse (context,
1816 if (!g_markup_parse_context_end_parse (context, error))
1821 /* Now insert the text */
1822 insert_text (&info, iter);
1825 parse_info_free (&info);
1827 g_markup_parse_context_free (context);
1833 _gtk_text_buffer_deserialize_rich_text (GtkTextBuffer *register_buffer,
1834 GtkTextBuffer *content_buffer,
1838 gboolean create_tags,
1846 headers = read_headers ((gchar *) text, length, error);
1851 header = headers->data;
1852 if (!header_is (header, "GTKTEXTBUFFERCONTENTS-0001"))
1854 g_set_error_literal (error,
1856 G_MARKUP_ERROR_PARSE,
1857 _("Serialized data is malformed. First section isn't GTKTEXTBUFFERCONTENTS-0001"));
1863 retval = deserialize_text (content_buffer, iter,
1864 header->start, header->length,
1865 create_tags, error, headers->next);
1868 g_list_foreach (headers, (GFunc)g_free, NULL);
1869 g_list_free (headers);