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
33 #include "gdk-pixbuf/gdk-pixdata.h"
34 #include "gtktextbufferserialize.h"
35 #include "gtktexttagprivate.h"
41 GString *tag_table_str;
44 GtkTextIter start, end;
49 GHashTable *tag_id_tags;
50 } SerializationContext;
53 serialize_value (GValue *value)
55 if (g_value_type_transformable (value->g_type, G_TYPE_STRING))
57 GValue text_value = { 0 };
60 g_value_init (&text_value, G_TYPE_STRING);
61 g_value_transform (value, &text_value);
63 tmp = g_markup_escape_text (g_value_get_string (&text_value), -1);
64 g_value_unset (&text_value);
68 else if (value->g_type == GDK_TYPE_COLOR)
70 GdkColor *color = g_value_get_boxed (value);
72 return g_strdup_printf ("%x:%x:%x", color->red, color->green, color->blue);
76 g_warning ("Type %s is not serializable\n", g_type_name (value->g_type));
83 deserialize_value (const gchar *str,
86 if (g_value_type_transformable (G_TYPE_STRING, value->g_type))
88 GValue text_value = { 0 };
91 g_value_init (&text_value, G_TYPE_STRING);
92 g_value_set_static_string (&text_value, str);
94 retval = g_value_transform (&text_value, value);
95 g_value_unset (&text_value);
99 else if (value->g_type == G_TYPE_BOOLEAN)
103 v = strcmp (str, "TRUE") == 0;
105 g_value_set_boolean (value, v);
109 else if (value->g_type == G_TYPE_INT)
115 v = g_ascii_strtoll (str, &tmp, 10);
117 if (errno || tmp == NULL || tmp == str)
120 g_value_set_int (value, v);
124 else if (value->g_type == G_TYPE_DOUBLE)
129 v = g_ascii_strtod (str, &tmp);
131 if (tmp == NULL || tmp == str)
134 g_value_set_double (value, v);
138 else if (value->g_type == GDK_TYPE_COLOR)
147 color.red = g_ascii_strtoll (old, &tmp, 16);
149 if (errno || tmp == old)
158 color.green = g_ascii_strtoll (old, &tmp, 16);
159 if (errno || tmp == old)
168 color.blue = g_ascii_strtoll (old, &tmp, 16);
170 if (errno || tmp == old || *tmp != '\0')
173 g_value_set_boxed (value, &color);
177 else if (G_VALUE_HOLDS_ENUM (value))
179 GEnumClass *class = G_ENUM_CLASS (g_type_class_peek (value->g_type));
180 GEnumValue *enum_value;
182 enum_value = g_enum_get_value_by_name (class, str);
186 g_value_set_enum (value, enum_value->value);
194 g_warning ("Type %s can not be deserialized\n", g_type_name (value->g_type));
200 /* Checks if a param is set, or if it's the default value */
202 is_param_set (GObject *object,
206 /* We need to special case some attributes here */
207 if (strcmp (pspec->name, "background-gdk") == 0)
211 g_object_get (object, "background-set", &is_set, NULL);
215 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
217 g_object_get_property (object, pspec->name, value);
224 else if (strcmp (pspec->name, "foreground-gdk") == 0)
228 g_object_get (object, "foreground-set", &is_set, NULL);
232 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
234 g_object_get_property (object, pspec->name, value);
246 is_set_name = g_strdup_printf ("%s-set", pspec->name);
248 if (g_object_class_find_property (G_OBJECT_GET_CLASS (object), is_set_name) == NULL)
250 g_free (is_set_name);
255 g_object_get (object, is_set_name, &is_set, NULL);
259 g_free (is_set_name);
263 g_free (is_set_name);
265 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
267 g_object_get_property (object, pspec->name, value);
269 if (g_param_value_defaults (pspec, value))
271 g_value_unset (value);
281 serialize_tag (gpointer key,
285 SerializationContext *context = user_data;
286 GtkTextTag *tag = data;
293 g_string_append (context->tag_table_str, " <tag ");
295 /* Handle anonymous tags */
298 tag_name = g_markup_escape_text (tag->priv->name, -1);
299 g_string_append_printf (context->tag_table_str, "name=\"%s\"", tag_name);
304 tag_id = GPOINTER_TO_INT (g_hash_table_lookup (context->tag_id_tags, tag));
306 g_string_append_printf (context->tag_table_str, "id=\"%d\"", tag_id);
309 g_string_append_printf (context->tag_table_str, " priority=\"%d\">\n", tag->priv->priority);
311 /* Serialize properties */
312 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (tag), &n_pspecs);
314 for (i = 0; i < n_pspecs; i++)
316 GValue value = { 0 };
319 if (!(pspecs[i]->flags & G_PARAM_READABLE) ||
320 !(pspecs[i]->flags & G_PARAM_WRITABLE))
323 if (!is_param_set (G_OBJECT (tag), pspecs[i], &value))
326 /* Now serialize the attr */
327 tmp2 = serialize_value (&value);
331 tmp = g_markup_escape_text (pspecs[i]->name, -1);
332 g_string_append_printf (context->tag_table_str, " <attr name=\"%s\" ", tmp);
335 tmp = g_markup_escape_text (g_type_name (pspecs[i]->value_type), -1);
336 g_string_append_printf (context->tag_table_str, "type=\"%s\" value=\"%s\" />\n", tmp, tmp2);
342 g_value_unset (&value);
347 g_string_append (context->tag_table_str, " </tag>\n");
351 serialize_tags (SerializationContext *context)
353 g_string_append (context->tag_table_str, " <text_view_markup>\n");
354 g_string_append (context->tag_table_str, " <tags>\n");
355 g_hash_table_foreach (context->tags, serialize_tag, context);
356 g_string_append (context->tag_table_str, " </tags>\n");
361 dump_tag_list (const gchar *str,
364 g_print ("%s: ", str);
372 g_print ("%s ", ((GtkTextTag *)list->data)->name);
382 find_list_delta (GSList *old_list,
388 GList *tmp_added, *tmp_removed;
393 /* Find added tags */
397 if (!g_slist_find (old_list, tmp->data))
398 tmp_added = g_list_prepend (tmp_added, tmp->data);
405 /* Find removed tags */
409 if (!g_slist_find (new_list, tmp->data))
410 tmp_removed = g_list_prepend (tmp_removed, tmp->data);
415 /* We reverse the list here to match the xml semantics */
416 *removed = g_list_reverse (tmp_removed);
420 serialize_section_header (GString *str,
424 g_return_if_fail (strlen (name) == 26);
426 g_string_append (str, name);
428 g_string_append_c (str, length >> 24);
430 g_string_append_c (str, (length >> 16) & 0xff);
431 g_string_append_c (str, (length >> 8) & 0xff);
432 g_string_append_c (str, length & 0xff);
436 serialize_text (GtkTextBuffer *buffer,
437 SerializationContext *context)
439 GtkTextIter iter, old_iter;
440 GSList *tag_list, *new_tag_list;
443 g_string_append (context->text_str, "<text>");
445 iter = context->start;
451 GList *added, *removed;
453 gchar *tmp_text, *escaped_text;
455 new_tag_list = gtk_text_iter_get_tags (&iter);
456 find_list_delta (tag_list, new_tag_list, &added, &removed);
458 /* Handle removed tags */
459 for (tmp = removed; tmp; tmp = tmp->next)
461 GtkTextTag *tag = tmp->data;
463 /* Only close the tag if we didn't close it before (by using
464 * the stack logic in the while() loop below)
466 if (g_slist_find (active_tags, tag))
468 g_string_append (context->text_str, "</apply_tag>");
470 /* Drop all tags that were opened after this one (which are
471 * above this on in the stack)
473 while (active_tags->data != tag)
475 added = g_list_prepend (added, active_tags->data);
476 active_tags = g_slist_remove (active_tags, active_tags->data);
477 g_string_append_printf (context->text_str, "</apply_tag>");
480 active_tags = g_slist_remove (active_tags, active_tags->data);
484 /* Handle added tags */
485 for (tmp = added; tmp; tmp = tmp->next)
487 GtkTextTag *tag = tmp->data;
490 /* Add it to the tag hash table */
491 g_hash_table_insert (context->tags, tag, tag);
495 tag_name = g_markup_escape_text (tag->priv->name, -1);
497 g_string_append_printf (context->text_str, "<apply_tag name=\"%s\">", tag_name);
504 /* We've got an anonymous tag, find out if it's been
506 if (!g_hash_table_lookup_extended (context->tag_id_tags, tag, NULL, &tag_id))
508 tag_id = GINT_TO_POINTER (context->tag_id++);
510 g_hash_table_insert (context->tag_id_tags, tag, tag_id);
513 g_string_append_printf (context->text_str, "<apply_tag id=\"%d\">", GPOINTER_TO_INT (tag_id));
516 active_tags = g_slist_prepend (active_tags, tag);
519 g_slist_free (tag_list);
520 tag_list = new_tag_list;
523 g_list_free (removed);
527 /* Now try to go to either the next tag toggle, or if a pixbuf appears */
530 gunichar ch = gtk_text_iter_get_char (&iter);
534 GdkPixbuf *pixbuf = gtk_text_iter_get_pixbuf (&iter);
538 /* Append the text before the pixbuf */
539 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
540 escaped_text = g_markup_escape_text (tmp_text, -1);
543 /* Forward so we don't get the 0xfffc char */
544 gtk_text_iter_forward_char (&iter);
547 g_string_append (context->text_str, escaped_text);
548 g_free (escaped_text);
550 g_string_append_printf (context->text_str, "<pixbuf index=\"%d\" />", context->n_pixbufs);
552 context->n_pixbufs++;
553 context->pixbufs = g_list_prepend (context->pixbufs, pixbuf);
561 gtk_text_iter_forward_char (&iter);
563 if (gtk_text_iter_toggles_tag (&iter, NULL))
567 /* We might have moved too far */
568 if (gtk_text_iter_compare (&iter, &context->end) > 0)
571 /* Append the text */
572 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
573 escaped_text = g_markup_escape_text (tmp_text, -1);
576 g_string_append (context->text_str, escaped_text);
577 g_free (escaped_text);
579 while (!gtk_text_iter_equal (&iter, &context->end));
581 /* Close any open tags */
582 for (tag_list = active_tags; tag_list; tag_list = tag_list->next)
583 g_string_append (context->text_str, "</apply_tag>");
585 g_slist_free (active_tags);
586 g_string_append (context->text_str, "</text>\n</text_view_markup>\n");
590 serialize_pixbufs (SerializationContext *context,
595 for (list = context->pixbufs; list != NULL; list = list->next)
597 GdkPixbuf *pixbuf = list->data;
602 gdk_pixdata_from_pixbuf (&pixdata, pixbuf, FALSE);
603 tmp = gdk_pixdata_serialize (&pixdata, &len);
605 serialize_section_header (text, "GTKTEXTBUFFERPIXBDATA-0001", len);
606 g_string_append_len (text, (gchar *) tmp, len);
612 _gtk_text_buffer_serialize_rich_text (GtkTextBuffer *register_buffer,
613 GtkTextBuffer *content_buffer,
614 const GtkTextIter *start,
615 const GtkTextIter *end,
619 SerializationContext context;
622 context.tags = g_hash_table_new (NULL, NULL);
623 context.text_str = g_string_new (NULL);
624 context.tag_table_str = g_string_new (NULL);
625 context.start = *start;
627 context.n_pixbufs = 0;
628 context.pixbufs = NULL;
630 context.tag_id_tags = g_hash_table_new (NULL, NULL);
632 /* We need to serialize the text before the tag table so we know
633 what tags are used */
634 serialize_text (content_buffer, &context);
635 serialize_tags (&context);
637 text = g_string_new (NULL);
638 serialize_section_header (text, "GTKTEXTBUFFERCONTENTS-0001",
639 context.tag_table_str->len + context.text_str->len);
641 g_string_append_len (text, context.tag_table_str->str, context.tag_table_str->len);
642 g_string_append_len (text, context.text_str->str, context.text_str->len);
644 context.pixbufs = g_list_reverse (context.pixbufs);
645 serialize_pixbufs (&context, text);
647 g_hash_table_destroy (context.tags);
648 g_list_free (context.pixbufs);
649 g_string_free (context.text_str, TRUE);
650 g_string_free (context.tag_table_str, TRUE);
651 g_hash_table_destroy (context.tag_id_tags);
655 return (guint8 *) g_string_free (text, FALSE);
661 STATE_TEXT_VIEW_MARKUP,
689 GtkTextBuffer *buffer;
691 /* Tags that are defined in <tag> elements */
692 GHashTable *defined_tags;
694 /* Tags that are anonymous */
695 GHashTable *anonymous_tags;
697 /* Tag name substitutions */
698 GHashTable *substitutions;
701 GtkTextTag *current_tag;
703 /* Priority of current tag */
704 gint current_tag_prio;
706 /* Id of current tag */
709 /* Tags and their priorities */
710 GList *tag_priorities;
716 gboolean create_tags;
718 gboolean parsed_text;
719 gboolean parsed_tags;
723 set_error (GError **err,
724 GMarkupParseContext *context,
734 g_markup_parse_context_get_position (context, &line, &ch);
736 va_start (args, format);
737 str = g_strdup_vprintf (format, args);
740 g_set_error (err, error_domain, error_code,
741 ("Line %d character %d: %s"),
748 push_state (ParseInfo *info,
751 info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
755 pop_state (ParseInfo *info)
757 g_return_if_fail (info->states != NULL);
759 info->states = g_slist_remove (info->states, info->states->data);
763 peek_state (ParseInfo *info)
765 g_return_val_if_fail (info->states != NULL, STATE_START);
767 return GPOINTER_TO_INT (info->states->data);
770 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
774 check_id_or_name (GMarkupParseContext *context,
775 const gchar *element_name,
776 const gchar **attribute_names,
777 const gchar **attribute_values,
782 gboolean has_id = FALSE;
783 gboolean has_name = FALSE;
789 for (i = 0; attribute_names[i] != NULL; i++)
791 if (strcmp (attribute_names[i], "name") == 0)
793 *name = attribute_values[i];
797 set_error (error, context,
799 G_MARKUP_ERROR_PARSE,
800 _("Both \"id\" and \"name\" were found on the <%s> element"),
807 set_error (error, context,
809 G_MARKUP_ERROR_PARSE,
810 _("The attribute \"%s\" was found twice on the <%s> element"),
811 "name", element_name);
817 else if (strcmp (attribute_names[i], "id") == 0)
823 set_error (error, context,
825 G_MARKUP_ERROR_PARSE,
826 _("Both \"id\" and \"name\" were found on the <%s> element"),
833 set_error (error, context,
835 G_MARKUP_ERROR_PARSE,
836 _("The attribute \"%s\" was found twice on the <%s> element"),
843 /* Try parsing the integer */
846 *id = g_ascii_strtoll (attribute_values[i], &tmp, 10);
848 if (errno || tmp == attribute_values[i])
850 set_error (error, context,
851 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
852 _("<%s> element has invalid ID \"%s\""), attribute_values[i]);
858 if (!has_id && !has_name)
860 set_error (error, context,
861 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
862 _("<%s> element has neither a \"name\" nor an \"id\" attribute"), element_name);
876 locate_attributes (GMarkupParseContext *context,
877 const char *element_name,
878 const char **attribute_names,
879 const char **attribute_values,
880 gboolean allow_unknown_attrs,
882 const char *first_attribute_name,
883 const char **first_attribute_retloc,
891 LocateAttr attrs[MAX_ATTRS];
895 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
896 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
901 attrs[0].name = first_attribute_name;
902 attrs[0].retloc = first_attribute_retloc;
903 *first_attribute_retloc = NULL;
905 va_start (args, first_attribute_retloc);
907 name = va_arg (args, const char*);
908 retloc = va_arg (args, const char**);
912 g_return_val_if_fail (retloc != NULL, FALSE);
914 g_assert (n_attrs < MAX_ATTRS);
916 attrs[n_attrs].name = name;
917 attrs[n_attrs].retloc = retloc;
921 name = va_arg (args, const char*);
922 retloc = va_arg (args, const char**);
931 while (attribute_names[i])
940 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
942 retloc = attrs[j].retloc;
946 set_error (error, context,
948 G_MARKUP_ERROR_PARSE,
949 _("Attribute \"%s\" repeated twice on the same <%s> element"),
950 attrs[j].name, element_name);
955 *retloc = attribute_values[i];
962 if (!found && !allow_unknown_attrs)
964 set_error (error, context,
966 G_MARKUP_ERROR_PARSE,
967 _("Attribute \"%s\" is invalid on <%s> element in this context"),
968 attribute_names[i], element_name);
981 check_no_attributes (GMarkupParseContext *context,
982 const char *element_name,
983 const char **attribute_names,
984 const char **attribute_values,
987 if (attribute_names[0] != NULL)
989 set_error (error, context,
991 G_MARKUP_ERROR_PARSE,
992 _("Attribute \"%s\" is invalid on <%s> element in this context"),
993 attribute_names[0], element_name);
1001 tag_exists (GMarkupParseContext *context,
1007 GtkTextTagTable *tag_table;
1008 const gchar *real_name;
1010 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1012 if (info->create_tags)
1014 /* If we have an anonymous tag, just return it directly */
1016 return g_hash_table_lookup (info->anonymous_tags,
1017 GINT_TO_POINTER (id));
1019 /* First, try the substitutions */
1020 real_name = g_hash_table_lookup (info->substitutions, name);
1023 return gtk_text_tag_table_lookup (tag_table, real_name);
1025 /* Next, try the list of defined tags */
1026 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1027 return gtk_text_tag_table_lookup (tag_table, name);
1029 set_error (error, context,
1030 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1031 _("Tag \"%s\" has not been defined."), name);
1041 set_error (error, context,
1042 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1043 _("Anonymous tag found and tags can not be created."));
1047 tag = gtk_text_tag_table_lookup (tag_table, name);
1052 set_error (error, context,
1053 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1054 _("Tag \"%s\" does not exist in buffer and tags can not be created."), name);
1068 get_pixbuf_from_headers (GList *headers,
1076 header = g_list_nth_data (headers, id);
1081 if (!gdk_pixdata_deserialize (&pixdata, header->length,
1082 (const guint8 *) header->start, error))
1085 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, TRUE, error);
1091 parse_apply_tag_element (GMarkupParseContext *context,
1092 const gchar *element_name,
1093 const gchar **attribute_names,
1094 const gchar **attribute_values,
1098 const gchar *name, *priority;
1102 g_assert (peek_state (info) == STATE_TEXT ||
1103 peek_state (info) == STATE_APPLY_TAG);
1105 if (ELEMENT_IS ("apply_tag"))
1107 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1108 "priority", &priority, NULL))
1111 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1116 tag = tag_exists (context, name, id, info, error);
1121 info->tag_stack = g_slist_prepend (info->tag_stack, tag);
1123 push_state (info, STATE_APPLY_TAG);
1125 else if (ELEMENT_IS ("pixbuf"))
1130 const gchar *pixbuf_id;
1132 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1133 "index", &pixbuf_id, NULL))
1136 int_id = atoi (pixbuf_id);
1137 pixbuf = get_pixbuf_from_headers (info->headers, int_id, error);
1139 span = g_new0 (TextSpan, 1);
1140 span->pixbuf = pixbuf;
1143 info->spans = g_list_prepend (info->spans, span);
1148 push_state (info, STATE_PIXBUF);
1151 set_error (error, context,
1152 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1153 _("Element <%s> is not allowed below <%s>"),
1154 element_name, peek_state(info) == STATE_TEXT ? "text" : "apply_tag");
1158 parse_attr_element (GMarkupParseContext *context,
1159 const gchar *element_name,
1160 const gchar **attribute_names,
1161 const gchar **attribute_values,
1165 const gchar *name, *type, *value;
1167 GValue gvalue = { 0 };
1170 g_assert (peek_state (info) == STATE_TAG);
1172 if (ELEMENT_IS ("attr"))
1174 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1175 "name", &name, "type", &type, "value", &value, NULL))
1178 gtype = g_type_from_name (type);
1180 if (gtype == G_TYPE_INVALID)
1182 set_error (error, context,
1183 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1184 _("\"%s\" is not a valid attribute type"), type);
1188 if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (info->current_tag), name)))
1190 set_error (error, context,
1191 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1192 _("\"%s\" is not a valid attribute name"), name);
1196 g_value_init (&gvalue, gtype);
1198 if (!deserialize_value (value, &gvalue))
1200 set_error (error, context,
1201 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1202 _("\"%s\" could not be converted to a value of type \"%s\" for attribute \"%s\""),
1207 if (g_param_value_validate (pspec, &gvalue))
1209 set_error (error, context,
1210 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1211 _("\"%s\" is not a valid value for attribute \"%s\""),
1213 g_value_unset (&gvalue);
1217 g_object_set_property (G_OBJECT (info->current_tag),
1220 g_value_unset (&gvalue);
1222 push_state (info, STATE_ATTR);
1226 set_error (error, context,
1227 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1228 _("Element <%s> is not allowed below <%s>"),
1229 element_name, "tag");
1235 get_tag_name (ParseInfo *info,
1236 const gchar *tag_name)
1238 GtkTextTagTable *tag_table;
1242 name = g_strdup (tag_name);
1244 if (!info->create_tags)
1248 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1250 while (gtk_text_tag_table_lookup (tag_table, name) != NULL)
1253 name = g_strdup_printf ("%s-%d", tag_name, ++i);
1258 g_hash_table_insert (info->substitutions, g_strdup (tag_name), g_strdup (name));
1265 parse_tag_element (GMarkupParseContext *context,
1266 const gchar *element_name,
1267 const gchar **attribute_names,
1268 const gchar **attribute_values,
1272 const gchar *name, *priority;
1278 g_assert (peek_state (info) == STATE_TAGS);
1280 if (ELEMENT_IS ("tag"))
1282 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1283 "priority", &priority, NULL))
1286 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1292 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1294 set_error (error, context,
1295 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1296 _("Tag \"%s\" already defined"), name);
1303 prio = g_ascii_strtoll (priority, &tmp, 10);
1305 if (errno || tmp == priority)
1307 set_error (error, context,
1308 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1309 _("Tag \"%s\" has invalid priority \"%s\""), name, priority);
1315 tag_name = get_tag_name (info, name);
1316 info->current_tag = gtk_text_tag_new (tag_name);
1321 info->current_tag = gtk_text_tag_new (NULL);
1322 info->current_tag_id = id;
1325 info->current_tag_prio = prio;
1327 push_state (info, STATE_TAG);
1331 set_error (error, context,
1332 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1333 _("Element <%s> is not allowed below <%s>"),
1334 element_name, "tags");
1339 start_element_handler (GMarkupParseContext *context,
1340 const gchar *element_name,
1341 const gchar **attribute_names,
1342 const gchar **attribute_values,
1346 ParseInfo *info = user_data;
1348 switch (peek_state (info))
1351 if (ELEMENT_IS ("text_view_markup"))
1353 if (!check_no_attributes (context, element_name,
1354 attribute_names, attribute_values, error))
1357 push_state (info, STATE_TEXT_VIEW_MARKUP);
1361 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1362 _("Outermost element in text must be <text_view_markup> not <%s>"),
1365 case STATE_TEXT_VIEW_MARKUP:
1366 if (ELEMENT_IS ("tags"))
1368 if (info->parsed_tags)
1370 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1371 _("A <%s> element has already been specified"), "tags");
1375 if (!check_no_attributes (context, element_name,
1376 attribute_names, attribute_values, error))
1379 push_state (info, STATE_TAGS);
1382 else if (ELEMENT_IS ("text"))
1384 if (info->parsed_text)
1386 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1387 _("A <%s> element has already been specified"), "text");
1390 else if (!info->parsed_tags)
1392 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1393 _("A <text> element can't occur before a <tags> element"));
1397 if (!check_no_attributes (context, element_name,
1398 attribute_names, attribute_values, error))
1401 push_state (info, STATE_TEXT);
1405 set_error (error, context,
1406 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1407 _("Element <%s> is not allowed below <%s>"),
1408 element_name, "text_view_markup");
1411 parse_tag_element (context, element_name,
1412 attribute_names, attribute_values,
1416 parse_attr_element (context, element_name,
1417 attribute_names, attribute_values,
1421 case STATE_APPLY_TAG:
1422 parse_apply_tag_element (context, element_name,
1423 attribute_names, attribute_values,
1427 g_assert_not_reached ();
1433 sort_tag_prio (TextTagPrio *a,
1436 if (a->prio < b->prio)
1438 else if (a->prio > b->prio)
1445 end_element_handler (GMarkupParseContext *context,
1446 const gchar *element_name,
1450 ParseInfo *info = user_data;
1454 switch (peek_state (info))
1458 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1460 info->parsed_tags = TRUE;
1462 /* Sort list and add the tags */
1463 info->tag_priorities = g_list_sort (info->tag_priorities,
1464 (GCompareFunc)sort_tag_prio);
1465 list = info->tag_priorities;
1468 TextTagPrio *prio = list->data;
1470 if (info->create_tags)
1471 gtk_text_tag_table_add (gtk_text_buffer_get_tag_table (info->buffer),
1474 g_object_unref (prio->tag);
1483 g_assert (peek_state (info) == STATE_TAGS);
1485 if (info->current_tag->priv->name)
1487 /* Add tag to defined tags hash */
1488 tmp = g_strdup (info->current_tag->priv->name);
1489 g_hash_table_insert (info->defined_tags,
1494 g_hash_table_insert (info->anonymous_tags,
1495 GINT_TO_POINTER (info->current_tag_id),
1499 if (info->create_tags)
1503 /* add the tag to the list */
1504 prio = g_new0 (TextTagPrio, 1);
1505 prio->prio = info->current_tag_prio;
1506 prio->tag = info->current_tag;
1508 info->tag_priorities = g_list_prepend (info->tag_priorities, prio);
1511 info->current_tag = NULL;
1515 g_assert (peek_state (info) == STATE_TAG);
1517 case STATE_APPLY_TAG:
1519 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1520 peek_state (info) == STATE_TEXT);
1523 info->tag_stack = g_slist_delete_link (info->tag_stack,
1529 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1531 info->spans = g_list_reverse (info->spans);
1532 info->parsed_text = TRUE;
1534 case STATE_TEXT_VIEW_MARKUP:
1536 g_assert (peek_state (info) == STATE_START);
1540 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1541 peek_state (info) == STATE_TEXT);
1544 g_assert_not_reached ();
1550 all_whitespace (const char *text,
1557 end = text + text_len;
1561 if (!g_ascii_isspace (*p))
1564 p = g_utf8_next_char (p);
1571 text_handler (GMarkupParseContext *context,
1577 ParseInfo *info = user_data;
1580 if (all_whitespace (text, text_len) &&
1581 peek_state (info) != STATE_TEXT &&
1582 peek_state (info) != STATE_APPLY_TAG)
1585 switch (peek_state (info))
1588 g_assert_not_reached (); /* gmarkup shouldn't do this */
1591 case STATE_APPLY_TAG:
1595 span = g_new0 (TextSpan, 1);
1596 span->text = g_strndup (text, text_len);
1597 span->tags = g_slist_copy (info->tag_stack);
1599 info->spans = g_list_prepend (info->spans, span);
1602 g_assert_not_reached ();
1608 parse_info_init (ParseInfo *info,
1609 GtkTextBuffer *buffer,
1610 gboolean create_tags,
1613 info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
1615 info->create_tags = create_tags;
1616 info->headers = headers;
1617 info->defined_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1618 info->substitutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1619 info->anonymous_tags = g_hash_table_new_full (NULL, NULL, NULL, NULL);
1620 info->tag_stack = NULL;
1622 info->parsed_text = FALSE;
1623 info->parsed_tags = FALSE;
1624 info->current_tag = NULL;
1625 info->current_tag_prio = -1;
1626 info->tag_priorities = NULL;
1628 info->buffer = buffer;
1632 text_span_free (TextSpan *span)
1634 g_free (span->text);
1635 g_slist_free (span->tags);
1640 parse_info_free (ParseInfo *info)
1644 g_slist_free (info->tag_stack);
1645 g_slist_free (info->states);
1647 g_hash_table_destroy (info->substitutions);
1648 g_hash_table_destroy (info->defined_tags);
1650 if (info->current_tag)
1651 g_object_unref (info->current_tag);
1656 text_span_free (list->data);
1660 g_list_free (info->spans);
1662 list = info->tag_priorities;
1665 TextTagPrio *prio = list->data;
1668 g_object_unref (prio->tag);
1673 g_list_free (info->tag_priorities);
1678 insert_text (ParseInfo *info,
1681 GtkTextIter start_iter;
1688 mark = gtk_text_buffer_create_mark (info->buffer, "deserialize_insert_point",
1694 TextSpan *span = tmp->data;
1697 gtk_text_buffer_insert (info->buffer, iter, span->text, -1);
1700 gtk_text_buffer_insert_pixbuf (info->buffer, iter, span->pixbuf);
1701 g_object_unref (span->pixbuf);
1703 gtk_text_buffer_get_iter_at_mark (info->buffer, &start_iter, mark);
1709 GtkTextTag *tag = tags->data;
1711 gtk_text_buffer_apply_tag (info->buffer, tag,
1717 gtk_text_buffer_move_mark (info->buffer, mark, iter);
1722 gtk_text_buffer_delete_mark (info->buffer, mark);
1728 read_int (const guchar *start)
1742 header_is (Header *header,
1745 return (strncmp (header->id, id, strlen (id)) == 0);
1749 read_headers (const gchar *start,
1756 GList *headers = NULL;
1763 if (strncmp (start + i, "GTKTEXTBUFFERCONTENTS-0001", 26) == 0 ||
1764 strncmp (start + i, "GTKTEXTBUFFERPIXBDATA-0001", 26) == 0)
1766 section_len = read_int ((const guchar *) start + i + 26);
1768 if (i + 30 + section_len > len)
1771 header = g_new0 (Header, 1);
1772 header->id = start + i;
1773 header->length = section_len;
1774 header->start = start + i + 30;
1776 i += 30 + section_len;
1778 headers = g_list_prepend (headers, header);
1784 return g_list_reverse (headers);
1787 g_list_foreach (headers, (GFunc) g_free, NULL);
1788 g_list_free (headers);
1790 g_set_error_literal (error,
1792 G_MARKUP_ERROR_PARSE,
1793 _("Serialized data is malformed"));
1799 deserialize_text (GtkTextBuffer *buffer,
1803 gboolean create_tags,
1807 GMarkupParseContext *context;
1809 gboolean retval = FALSE;
1811 static const GMarkupParser rich_text_parser = {
1812 start_element_handler,
1813 end_element_handler,
1819 parse_info_init (&info, buffer, create_tags, headers);
1821 context = g_markup_parse_context_new (&rich_text_parser,
1824 if (!g_markup_parse_context_parse (context,
1830 if (!g_markup_parse_context_end_parse (context, error))
1835 /* Now insert the text */
1836 insert_text (&info, iter);
1839 parse_info_free (&info);
1841 g_markup_parse_context_free (context);
1847 _gtk_text_buffer_deserialize_rich_text (GtkTextBuffer *register_buffer,
1848 GtkTextBuffer *content_buffer,
1852 gboolean create_tags,
1860 headers = read_headers ((gchar *) text, length, error);
1865 header = headers->data;
1866 if (!header_is (header, "GTKTEXTBUFFERCONTENTS-0001"))
1868 g_set_error_literal (error,
1870 G_MARKUP_ERROR_PARSE,
1871 _("Serialized data is malformed. First section isn't GTKTEXTBUFFERCONTENTS-0001"));
1877 retval = deserialize_text (content_buffer, iter,
1878 header->start, header->length,
1879 create_tags, error, headers->next);
1882 g_list_foreach (headers, (GFunc)g_free, NULL);
1883 g_list_free (headers);