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"
39 GString *tag_table_str;
42 GtkTextIter start, end;
47 GHashTable *tag_id_tags;
48 } SerializationContext;
51 serialize_value (GValue *value)
53 if (g_value_type_transformable (value->g_type, G_TYPE_STRING))
55 GValue text_value = { 0 };
58 g_value_init (&text_value, G_TYPE_STRING);
59 g_value_transform (value, &text_value);
61 tmp = g_markup_escape_text (g_value_get_string (&text_value), -1);
62 g_value_unset (&text_value);
66 else if (value->g_type == GDK_TYPE_COLOR)
68 GdkColor *color = g_value_get_boxed (value);
70 return g_strdup_printf ("%x:%x:%x", color->red, color->green, color->blue);
72 else if (g_type_is_a (value->g_type, GDK_TYPE_DRAWABLE))
74 /* Don't do anything */
78 g_warning ("Type %s is not serializable\n", g_type_name (value->g_type));
85 deserialize_value (const gchar *str,
88 if (g_value_type_transformable (G_TYPE_STRING, value->g_type))
90 GValue text_value = { 0 };
93 g_value_init (&text_value, G_TYPE_STRING);
94 g_value_set_static_string (&text_value, str);
96 retval = g_value_transform (&text_value, value);
97 g_value_unset (&text_value);
101 else if (value->g_type == G_TYPE_BOOLEAN)
105 v = strcmp (str, "TRUE") == 0;
107 g_value_set_boolean (value, v);
111 else if (value->g_type == G_TYPE_INT)
116 v = strtol (str, &tmp, 10);
118 if (tmp == NULL || tmp == str)
121 g_value_set_int (value, v);
125 else if (value->g_type == G_TYPE_DOUBLE)
130 v = g_ascii_strtod (str, &tmp);
132 if (tmp == NULL || tmp == str)
135 g_value_set_double (value, v);
139 else if (value->g_type == GDK_TYPE_COLOR)
146 color.red = strtol (old, &tmp, 16);
148 if (tmp == NULL || tmp == old)
155 color.green = strtol (old, &tmp, 16);
156 if (tmp == NULL || tmp == old)
163 color.blue = strtol (old, &tmp, 16);
165 if (tmp == NULL || tmp == old || *tmp != '\0')
168 g_value_set_boxed (value, &color);
172 else if (G_VALUE_HOLDS_ENUM (value))
174 GEnumClass *class = G_ENUM_CLASS (g_type_class_peek (value->g_type));
175 GEnumValue *enum_value;
177 enum_value = g_enum_get_value_by_name (class, str);
181 g_value_set_enum (value, enum_value->value);
189 g_warning ("Type %s can not be deserialized\n", g_type_name (value->g_type));
195 /* Checks if a param is set, or if it's the default value */
197 is_param_set (GObject *object,
201 /* We need to special case some attributes here */
202 if (strcmp (pspec->name, "background-gdk") == 0)
206 g_object_get (object, "background-set", &is_set, NULL);
210 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
212 g_object_get_property (object, pspec->name, value);
219 else if (strcmp (pspec->name, "foreground-gdk") == 0)
223 g_object_get (object, "foreground-set", &is_set, NULL);
227 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
229 g_object_get_property (object, pspec->name, value);
241 is_set_name = g_strdup_printf ("%s-set", pspec->name);
243 if (g_object_class_find_property (G_OBJECT_GET_CLASS (object), is_set_name) == NULL)
245 g_free (is_set_name);
250 g_object_get (object, is_set_name, &is_set, NULL);
254 g_free (is_set_name);
258 g_free (is_set_name);
260 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
262 g_object_get_property (object, pspec->name, value);
264 if (g_param_value_defaults (pspec, value))
266 g_value_unset (value);
276 serialize_tag (gpointer key,
280 SerializationContext *context = user_data;
281 GtkTextTag *tag = data;
288 g_string_append (context->tag_table_str, " <tag ");
290 /* Handle anonymous tags */
293 tag_name = g_markup_escape_text (tag->name, -1);
294 g_string_append_printf (context->tag_table_str, "name=\"%s\"", tag_name);
299 tag_id = GPOINTER_TO_INT (g_hash_table_lookup (context->tag_id_tags, tag));
301 g_string_append_printf (context->tag_table_str, "id=\"%d\"", tag_id);
304 g_string_append_printf (context->tag_table_str, " priority=\"%d\">\n", tag->priority);
306 /* Serialize properties */
307 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (tag), &n_pspecs);
309 for (i = 0; i < n_pspecs; i++)
311 GValue value = { 0 };
314 if (!(pspecs[i]->flags & G_PARAM_READABLE) ||
315 !(pspecs[i]->flags & G_PARAM_WRITABLE))
318 if (!is_param_set (G_OBJECT (tag), pspecs[i], &value))
321 /* Now serialize the attr */
322 tmp2 = serialize_value (&value);
326 tmp = g_markup_escape_text (pspecs[i]->name, -1);
327 g_string_append_printf (context->tag_table_str, " <attr name=\"%s\" ", tmp);
330 tmp = g_markup_escape_text (g_type_name (pspecs[i]->value_type), -1);
331 g_string_append_printf (context->tag_table_str, "type=\"%s\" value=\"%s\" />\n", tmp, tmp2);
337 g_value_unset (&value);
342 g_string_append (context->tag_table_str, " </tag>\n");
346 serialize_tags (SerializationContext *context)
348 g_string_append (context->tag_table_str, " <text_view_markup>\n");
349 g_string_append (context->tag_table_str, " <tags>\n");
350 g_hash_table_foreach (context->tags, serialize_tag, context);
351 g_string_append (context->tag_table_str, " </tags>\n");
356 dump_tag_list (const gchar *str,
359 g_print ("%s: ", str);
367 g_print ("%s ", ((GtkTextTag *)list->data)->name);
377 find_list_delta (GSList *old_list,
383 GList *tmp_added, *tmp_removed;
388 /* Find added tags */
392 if (!g_slist_find (old_list, tmp->data))
393 tmp_added = g_list_prepend (tmp_added, tmp->data);
400 /* Find removed tags */
404 if (!g_slist_find (new_list, tmp->data))
405 tmp_removed = g_list_prepend (tmp_removed, tmp->data);
410 /* We reverse the list here to match the xml semantics */
411 *removed = g_list_reverse (tmp_removed);
415 serialize_section_header (GString *str,
419 g_return_if_fail (strlen (name) == 26);
421 g_string_append (str, name);
423 g_string_append_c (str, length >> 24);
425 g_string_append_c (str, (length >> 16) & 0xff);
426 g_string_append_c (str, (length >> 8) & 0xff);
427 g_string_append_c (str, length & 0xff);
431 serialize_text (GtkTextBuffer *buffer,
432 SerializationContext *context)
434 GtkTextIter iter, old_iter;
435 GSList *tag_list, *new_tag_list;
438 g_string_append (context->text_str, "<text>");
440 iter = context->start;
446 GList *added, *removed;
448 gchar *tmp_text, *escaped_text;
450 new_tag_list = gtk_text_iter_get_tags (&iter);
451 find_list_delta (tag_list, new_tag_list, &added, &removed);
453 /* Handle removed tags */
454 for (tmp = removed; tmp; tmp = tmp->next)
456 GtkTextTag *tag = tmp->data;
458 /* Only close the tag if we didn't close it before (by using
459 * the stack logic in the while() loop below)
461 if (g_slist_find (active_tags, tag))
463 g_string_append (context->text_str, "</apply_tag>");
465 /* Drop all tags that were opened after this one (which are
466 * above this on in the stack)
468 while (active_tags->data != tag)
470 added = g_list_prepend (added, active_tags->data);
471 active_tags = g_slist_remove (active_tags, active_tags->data);
472 g_string_append_printf (context->text_str, "</apply_tag>");
475 active_tags = g_slist_remove (active_tags, active_tags->data);
479 /* Handle added tags */
480 for (tmp = added; tmp; tmp = tmp->next)
482 GtkTextTag *tag = tmp->data;
485 /* Add it to the tag hash table */
486 g_hash_table_insert (context->tags, tag, tag);
490 tag_name = g_markup_escape_text (tag->name, -1);
492 g_string_append_printf (context->text_str, "<apply_tag name=\"%s\">", tag_name);
499 /* We've got an anonymous tag, find out if it's been
501 if (!g_hash_table_lookup_extended (context->tag_id_tags, tag, NULL, &tag_id))
503 tag_id = GINT_TO_POINTER (context->tag_id++);
505 g_hash_table_insert (context->tag_id_tags, tag, tag_id);
508 g_string_append_printf (context->text_str, "<apply_tag id=\"%d\">", GPOINTER_TO_INT (tag_id));
511 active_tags = g_slist_prepend (active_tags, tag);
514 g_slist_free (tag_list);
515 tag_list = new_tag_list;
518 g_list_free (removed);
522 /* Now try to go to either the next tag toggle, or if a pixbuf appears */
525 gunichar ch = gtk_text_iter_get_char (&iter);
529 GdkPixbuf *pixbuf = gtk_text_iter_get_pixbuf (&iter);
533 /* Append the text before the pixbuf */
534 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
535 escaped_text = g_markup_escape_text (tmp_text, -1);
538 /* Forward so we don't get the 0xfffc char */
539 gtk_text_iter_forward_char (&iter);
542 g_string_append (context->text_str, escaped_text);
543 g_free (escaped_text);
545 g_string_append_printf (context->text_str, "<pixbuf index=\"%d\" />", context->n_pixbufs);
547 context->n_pixbufs++;
548 context->pixbufs = g_list_prepend (context->pixbufs, pixbuf);
556 gtk_text_iter_forward_char (&iter);
558 if (gtk_text_iter_toggles_tag (&iter, NULL))
562 /* We might have moved too far */
563 if (gtk_text_iter_compare (&iter, &context->end) > 0)
566 /* Append the text */
567 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
568 escaped_text = g_markup_escape_text (tmp_text, -1);
571 g_string_append (context->text_str, escaped_text);
572 g_free (escaped_text);
574 while (!gtk_text_iter_equal (&iter, &context->end));
576 /* Close any open tags */
577 for (tag_list = active_tags; tag_list; tag_list = tag_list->next)
578 g_string_append (context->text_str, "</apply_tag>");
580 g_slist_free (active_tags);
581 g_string_append (context->text_str, "</text>\n</text_view_markup>\n");
585 serialize_pixbufs (SerializationContext *context,
590 for (list = context->pixbufs; list != NULL; list = list->next)
592 GdkPixbuf *pixbuf = list->data;
597 gdk_pixdata_from_pixbuf (&pixdata, pixbuf, FALSE);
598 tmp = gdk_pixdata_serialize (&pixdata, &len);
600 serialize_section_header (text, "GTKTEXTBUFFERPIXBDATA-0001", len);
601 g_string_append_len (text, (gchar *) tmp, len);
607 _gtk_text_buffer_serialize_rich_text (GtkTextBuffer *register_buffer,
608 GtkTextBuffer *content_buffer,
609 const GtkTextIter *start,
610 const GtkTextIter *end,
614 SerializationContext context;
617 context.tags = g_hash_table_new (NULL, NULL);
618 context.text_str = g_string_new (NULL);
619 context.tag_table_str = g_string_new (NULL);
620 context.start = *start;
622 context.n_pixbufs = 0;
623 context.pixbufs = NULL;
625 context.tag_id_tags = g_hash_table_new (NULL, NULL);
627 /* We need to serialize the text before the tag table so we know
628 what tags are used */
629 serialize_text (content_buffer, &context);
630 serialize_tags (&context);
632 text = g_string_new (NULL);
633 serialize_section_header (text, "GTKTEXTBUFFERCONTENTS-0001",
634 context.tag_table_str->len + context.text_str->len);
636 g_string_append_len (text, context.tag_table_str->str, context.tag_table_str->len);
637 g_string_append_len (text, context.text_str->str, context.text_str->len);
639 context.pixbufs = g_list_reverse (context.pixbufs);
640 serialize_pixbufs (&context, text);
642 g_hash_table_destroy (context.tags);
643 g_list_free (context.pixbufs);
644 g_string_free (context.text_str, TRUE);
645 g_string_free (context.tag_table_str, TRUE);
646 g_hash_table_destroy (context.tag_id_tags);
650 return (guint8 *) g_string_free (text, FALSE);
656 STATE_TEXT_VIEW_MARKUP,
684 GtkTextBuffer *buffer;
686 /* Tags that are defined in <tag> elements */
687 GHashTable *defined_tags;
689 /* Tags that are anonymous */
690 GHashTable *anonymous_tags;
692 /* Tag name substitutions */
693 GHashTable *substitutions;
696 GtkTextTag *current_tag;
698 /* Priority of current tag */
699 gint current_tag_prio;
701 /* Id of current tag */
704 /* Tags and their priorities */
705 GList *tag_priorities;
711 gboolean create_tags;
713 gboolean parsed_text;
714 gboolean parsed_tags;
718 set_error (GError **err,
719 GMarkupParseContext *context,
729 g_markup_parse_context_get_position (context, &line, &ch);
731 va_start (args, format);
732 str = g_strdup_vprintf (format, args);
735 g_set_error (err, error_domain, error_code,
736 ("Line %d character %d: %s"),
743 push_state (ParseInfo *info,
746 info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
750 pop_state (ParseInfo *info)
752 g_return_if_fail (info->states != NULL);
754 info->states = g_slist_remove (info->states, info->states->data);
758 peek_state (ParseInfo *info)
760 g_return_val_if_fail (info->states != NULL, STATE_START);
762 return GPOINTER_TO_INT (info->states->data);
765 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
769 check_id_or_name (GMarkupParseContext *context,
770 const gchar *element_name,
771 const gchar **attribute_names,
772 const gchar **attribute_values,
777 gboolean has_id = FALSE;
778 gboolean has_name = FALSE;
784 for (i = 0; attribute_names[i] != NULL; i++)
786 if (strcmp (attribute_names[i], "name") == 0)
788 *name = attribute_values[i];
792 set_error (error, context,
794 G_MARKUP_ERROR_PARSE,
795 _("Both \"id\" and \"name\" were found on the <%s> element"),
802 set_error (error, context,
804 G_MARKUP_ERROR_PARSE,
805 _("The attribute \"%s\" was found twice on the <%s> element"),
806 "name", element_name);
812 else if (strcmp (attribute_names[i], "id") == 0)
818 set_error (error, context,
820 G_MARKUP_ERROR_PARSE,
821 _("Both \"id\" and \"name\" were found on the <%s> element"),
828 set_error (error, context,
830 G_MARKUP_ERROR_PARSE,
831 _("The attribute \"%s\" was found twice on the <%s> element"),
838 /* Try parsing the integer */
839 *id = strtol (attribute_values[i], &tmp, 10);
841 if (tmp == NULL || tmp == attribute_values[i])
843 set_error (error, context,
844 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
845 _("<%s> element has invalid id \"%s\""), attribute_values[i]);
851 if (!has_id && !has_name)
853 set_error (error, context,
854 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
855 _("<%s> element has neither a \"name\" nor an \"id\" attribute"), element_name);
869 locate_attributes (GMarkupParseContext *context,
870 const char *element_name,
871 const char **attribute_names,
872 const char **attribute_values,
873 gboolean allow_unknown_attrs,
875 const char *first_attribute_name,
876 const char **first_attribute_retloc,
884 LocateAttr attrs[MAX_ATTRS];
888 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
889 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
894 attrs[0].name = first_attribute_name;
895 attrs[0].retloc = first_attribute_retloc;
896 *first_attribute_retloc = NULL;
898 va_start (args, first_attribute_retloc);
900 name = va_arg (args, const char*);
901 retloc = va_arg (args, const char**);
905 g_return_val_if_fail (retloc != NULL, FALSE);
907 g_assert (n_attrs < MAX_ATTRS);
909 attrs[n_attrs].name = name;
910 attrs[n_attrs].retloc = retloc;
914 name = va_arg (args, const char*);
915 retloc = va_arg (args, const char**);
924 while (attribute_names[i])
933 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
935 retloc = attrs[j].retloc;
939 set_error (error, context,
941 G_MARKUP_ERROR_PARSE,
942 _("Attribute \"%s\" repeated twice on the same <%s> element"),
943 attrs[j].name, element_name);
948 *retloc = attribute_values[i];
955 if (!found && !allow_unknown_attrs)
957 set_error (error, context,
959 G_MARKUP_ERROR_PARSE,
960 _("Attribute \"%s\" is invalid on <%s> element in this context"),
961 attribute_names[i], element_name);
974 check_no_attributes (GMarkupParseContext *context,
975 const char *element_name,
976 const char **attribute_names,
977 const char **attribute_values,
980 if (attribute_names[0] != NULL)
982 set_error (error, context,
984 G_MARKUP_ERROR_PARSE,
985 _("Attribute \"%s\" is invalid on <%s> element in this context"),
986 attribute_names[0], element_name);
994 tag_exists (GMarkupParseContext *context,
1000 GtkTextTagTable *tag_table;
1001 const gchar *real_name;
1003 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1005 if (info->create_tags)
1007 /* If we have an anonymous tag, just return it directly */
1009 return g_hash_table_lookup (info->anonymous_tags,
1010 GINT_TO_POINTER (id));
1012 /* First, try the substitutions */
1013 real_name = g_hash_table_lookup (info->substitutions, name);
1016 return gtk_text_tag_table_lookup (tag_table, real_name);
1018 /* Next, try the list of defined tags */
1019 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1020 return gtk_text_tag_table_lookup (tag_table, name);
1022 set_error (error, context,
1023 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1024 _("Tag \"%s\" has not been defined."), name);
1034 set_error (error, context,
1035 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1036 _("Anonymous tag found and tags can not be created."));
1040 tag = gtk_text_tag_table_lookup (tag_table, name);
1045 set_error (error, context,
1046 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1047 _("Tag \"%s\" does not exist in buffer and tags can not be created."), name);
1061 get_pixbuf_from_headers (GList *headers,
1069 header = g_list_nth_data (headers, id);
1074 if (!gdk_pixdata_deserialize (&pixdata, header->length,
1075 (const guint8 *) header->start, error))
1078 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, TRUE, error);
1084 parse_apply_tag_element (GMarkupParseContext *context,
1085 const gchar *element_name,
1086 const gchar **attribute_names,
1087 const gchar **attribute_values,
1091 const gchar *name, *priority;
1095 g_assert (peek_state (info) == STATE_TEXT ||
1096 peek_state (info) == STATE_APPLY_TAG);
1098 if (ELEMENT_IS ("apply_tag"))
1100 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1101 "priority", &priority, NULL))
1104 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1109 tag = tag_exists (context, name, id, info, error);
1114 info->tag_stack = g_slist_prepend (info->tag_stack, tag);
1116 push_state (info, STATE_APPLY_TAG);
1118 else if (ELEMENT_IS ("pixbuf"))
1123 const gchar *pixbuf_id;
1125 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1126 "index", &pixbuf_id, NULL))
1129 int_id = atoi (pixbuf_id);
1130 pixbuf = get_pixbuf_from_headers (info->headers, int_id, error);
1132 span = g_new0 (TextSpan, 1);
1133 span->pixbuf = pixbuf;
1136 info->spans = g_list_prepend (info->spans, span);
1141 push_state (info, STATE_PIXBUF);
1144 set_error (error, context,
1145 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1146 _("Element <%s> is not allowed below <%s>"),
1147 element_name, peek_state(info) == STATE_TEXT ? "text" : "apply_tag");
1151 parse_attr_element (GMarkupParseContext *context,
1152 const gchar *element_name,
1153 const gchar **attribute_names,
1154 const gchar **attribute_values,
1158 const gchar *name, *type, *value;
1160 GValue gvalue = { 0 };
1163 g_assert (peek_state (info) == STATE_TAG);
1165 if (ELEMENT_IS ("attr"))
1167 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1168 "name", &name, "type", &type, "value", &value, NULL))
1171 gtype = g_type_from_name (type);
1173 if (gtype == G_TYPE_INVALID)
1175 set_error (error, context,
1176 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1177 _("\"%s\" is not a valid attribute type"), type);
1181 if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (info->current_tag), name)))
1183 set_error (error, context,
1184 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1185 _("\"%s\" is not a valid attribute name"), name);
1189 g_value_init (&gvalue, gtype);
1191 if (!deserialize_value (value, &gvalue))
1193 set_error (error, context,
1194 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1195 _("\"%s\" could not be converted to a value of type \"%s\" for attribute \"%s\""),
1200 if (g_param_value_validate (pspec, &gvalue))
1202 set_error (error, context,
1203 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1204 _("\"%s\" is not a valid value for attribute \"%s\""),
1206 g_value_unset (&gvalue);
1210 g_object_set_property (G_OBJECT (info->current_tag),
1213 g_value_unset (&gvalue);
1215 push_state (info, STATE_ATTR);
1219 set_error (error, context,
1220 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1221 _("Element <%s> is not allowed below <%s>"),
1222 element_name, "tag");
1228 get_tag_name (ParseInfo *info,
1229 const gchar *tag_name)
1231 GtkTextTagTable *tag_table;
1235 name = g_strdup (tag_name);
1237 if (!info->create_tags)
1241 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1243 while (gtk_text_tag_table_lookup (tag_table, name) != NULL)
1246 name = g_strdup_printf ("%s-%d", tag_name, ++i);
1251 g_hash_table_insert (info->substitutions, g_strdup (tag_name), g_strdup (name));
1258 parse_tag_element (GMarkupParseContext *context,
1259 const gchar *element_name,
1260 const gchar **attribute_names,
1261 const gchar **attribute_values,
1265 const gchar *name, *priority;
1271 g_assert (peek_state (info) == STATE_TAGS);
1273 if (ELEMENT_IS ("tag"))
1275 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1276 "priority", &priority, NULL))
1279 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1285 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1287 set_error (error, context,
1288 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1289 _("Tag \"%s\" already defined"), name);
1294 prio = strtol (priority, &tmp, 10);
1296 if (tmp == NULL || tmp == priority)
1298 set_error (error, context,
1299 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1300 _("Tag \"%s\" has invalid priority \"%s\""), name, priority);
1306 tag_name = get_tag_name (info, name);
1307 info->current_tag = gtk_text_tag_new (tag_name);
1312 info->current_tag = gtk_text_tag_new (NULL);
1313 info->current_tag_id = id;
1316 info->current_tag_prio = prio;
1318 push_state (info, STATE_TAG);
1322 set_error (error, context,
1323 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1324 _("Element <%s> is not allowed below <%s>"),
1325 element_name, "tags");
1330 start_element_handler (GMarkupParseContext *context,
1331 const gchar *element_name,
1332 const gchar **attribute_names,
1333 const gchar **attribute_values,
1337 ParseInfo *info = user_data;
1339 switch (peek_state (info))
1342 if (ELEMENT_IS ("text_view_markup"))
1344 if (!check_no_attributes (context, element_name,
1345 attribute_names, attribute_values, error))
1348 push_state (info, STATE_TEXT_VIEW_MARKUP);
1352 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1353 _("Outermost element in text must be <text_view_markup> not <%s>"),
1356 case STATE_TEXT_VIEW_MARKUP:
1357 if (ELEMENT_IS ("tags"))
1359 if (info->parsed_tags)
1361 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1362 _("A <%s> element has already been specified"), "tags");
1366 if (!check_no_attributes (context, element_name,
1367 attribute_names, attribute_values, error))
1370 push_state (info, STATE_TAGS);
1373 else if (ELEMENT_IS ("text"))
1375 if (info->parsed_text)
1377 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1378 _("A <%s> element has already been specified"), "text");
1381 else if (!info->parsed_tags)
1383 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1384 _("A <text> element can't occur before a <tags> element"));
1388 if (!check_no_attributes (context, element_name,
1389 attribute_names, attribute_values, error))
1392 push_state (info, STATE_TEXT);
1396 set_error (error, context,
1397 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1398 _("Element <%s> is not allowed below <%s>"),
1399 element_name, "text_view_markup");
1402 parse_tag_element (context, element_name,
1403 attribute_names, attribute_values,
1407 parse_attr_element (context, element_name,
1408 attribute_names, attribute_values,
1412 case STATE_APPLY_TAG:
1413 parse_apply_tag_element (context, element_name,
1414 attribute_names, attribute_values,
1418 g_assert_not_reached ();
1424 sort_tag_prio (TextTagPrio *a,
1427 if (a->prio < b->prio)
1429 else if (a->prio > b->prio)
1436 end_element_handler (GMarkupParseContext *context,
1437 const gchar *element_name,
1441 ParseInfo *info = user_data;
1445 switch (peek_state (info))
1449 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1451 info->parsed_tags = TRUE;
1453 /* Sort list and add the tags */
1454 info->tag_priorities = g_list_sort (info->tag_priorities,
1455 (GCompareFunc)sort_tag_prio);
1456 list = info->tag_priorities;
1459 TextTagPrio *prio = list->data;
1461 if (info->create_tags)
1462 gtk_text_tag_table_add (gtk_text_buffer_get_tag_table (info->buffer),
1465 g_object_unref (prio->tag);
1474 g_assert (peek_state (info) == STATE_TAGS);
1476 if (info->current_tag->name)
1478 /* Add tag to defined tags hash */
1479 tmp = g_strdup (info->current_tag->name);
1480 g_hash_table_insert (info->defined_tags,
1485 g_hash_table_insert (info->anonymous_tags,
1486 GINT_TO_POINTER (info->current_tag_id),
1490 if (info->create_tags)
1494 /* add the tag to the list */
1495 prio = g_new0 (TextTagPrio, 1);
1496 prio->prio = info->current_tag_prio;
1497 prio->tag = info->current_tag;
1499 info->tag_priorities = g_list_prepend (info->tag_priorities, prio);
1502 info->current_tag = NULL;
1506 g_assert (peek_state (info) == STATE_TAG);
1508 case STATE_APPLY_TAG:
1510 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1511 peek_state (info) == STATE_TEXT);
1514 info->tag_stack = g_slist_delete_link (info->tag_stack,
1520 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1522 info->spans = g_list_reverse (info->spans);
1523 info->parsed_text = TRUE;
1525 case STATE_TEXT_VIEW_MARKUP:
1527 g_assert (peek_state (info) == STATE_START);
1531 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1532 peek_state (info) == STATE_TEXT);
1535 g_assert_not_reached ();
1541 all_whitespace (const char *text,
1548 end = text + text_len;
1552 if (!g_ascii_isspace (*p))
1555 p = g_utf8_next_char (p);
1562 text_handler (GMarkupParseContext *context,
1568 ParseInfo *info = user_data;
1571 if (all_whitespace (text, text_len) &&
1572 peek_state (info) != STATE_TEXT &&
1573 peek_state (info) != STATE_APPLY_TAG)
1576 switch (peek_state (info))
1579 g_assert_not_reached (); /* gmarkup shouldn't do this */
1582 case STATE_APPLY_TAG:
1586 span = g_new0 (TextSpan, 1);
1587 span->text = g_strndup (text, text_len);
1588 span->tags = g_slist_copy (info->tag_stack);
1590 info->spans = g_list_prepend (info->spans, span);
1593 g_assert_not_reached ();
1599 parse_info_init (ParseInfo *info,
1600 GtkTextBuffer *buffer,
1601 gboolean create_tags,
1604 info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
1606 info->create_tags = create_tags;
1607 info->headers = headers;
1608 info->defined_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1609 info->substitutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1610 info->anonymous_tags = g_hash_table_new_full (NULL, NULL, NULL, NULL);
1611 info->tag_stack = NULL;
1613 info->parsed_text = FALSE;
1614 info->parsed_tags = FALSE;
1615 info->current_tag = NULL;
1616 info->current_tag_prio = -1;
1617 info->tag_priorities = NULL;
1619 info->buffer = buffer;
1623 text_span_free (TextSpan *span)
1625 g_free (span->text);
1626 g_slist_free (span->tags);
1631 parse_info_free (ParseInfo *info)
1635 g_slist_free (info->tag_stack);
1636 g_slist_free (info->states);
1638 g_hash_table_destroy (info->substitutions);
1639 g_hash_table_destroy (info->defined_tags);
1641 if (info->current_tag)
1642 g_object_unref (info->current_tag);
1647 text_span_free (list->data);
1651 g_list_free (info->spans);
1653 list = info->tag_priorities;
1656 TextTagPrio *prio = list->data;
1659 g_object_unref (prio->tag);
1664 g_list_free (info->tag_priorities);
1669 insert_text (ParseInfo *info,
1672 GtkTextIter start_iter;
1679 mark = gtk_text_buffer_create_mark (info->buffer, "deserialize_insert_point",
1685 TextSpan *span = tmp->data;
1688 gtk_text_buffer_insert (info->buffer, iter, span->text, -1);
1691 gtk_text_buffer_insert_pixbuf (info->buffer, iter, span->pixbuf);
1692 g_object_unref (span->pixbuf);
1694 gtk_text_buffer_get_iter_at_mark (info->buffer, &start_iter, mark);
1700 GtkTextTag *tag = tags->data;
1702 gtk_text_buffer_apply_tag (info->buffer, tag,
1708 gtk_text_buffer_move_mark (info->buffer, mark, iter);
1713 gtk_text_buffer_delete_mark (info->buffer, mark);
1719 read_int (const guchar *start)
1733 header_is (Header *header,
1736 return (strncmp (header->id, id, strlen (id)) == 0);
1740 read_headers (const gchar *start,
1747 GList *headers = NULL;
1754 if (strncmp (start + i, "GTKTEXTBUFFERCONTENTS-0001", 26) == 0 ||
1755 strncmp (start + i, "GTKTEXTBUFFERPIXBDATA-0001", 26) == 0)
1757 section_len = read_int ((const guchar *) start + i + 26);
1759 if (i + 30 + section_len > len)
1762 header = g_new0 (Header, 1);
1763 header->id = start + i;
1764 header->length = section_len;
1765 header->start = start + i + 30;
1767 i += 30 + section_len;
1769 headers = g_list_prepend (headers, header);
1775 return g_list_reverse (headers);
1778 g_list_foreach (headers, (GFunc) g_free, NULL);
1779 g_list_free (headers);
1781 g_set_error_literal (error,
1783 G_MARKUP_ERROR_PARSE,
1784 _("Serialized data is malformed"));
1790 deserialize_text (GtkTextBuffer *buffer,
1794 gboolean create_tags,
1798 GMarkupParseContext *context;
1800 gboolean retval = FALSE;
1802 static const GMarkupParser rich_text_parser = {
1803 start_element_handler,
1804 end_element_handler,
1810 parse_info_init (&info, buffer, create_tags, headers);
1812 context = g_markup_parse_context_new (&rich_text_parser,
1815 if (!g_markup_parse_context_parse (context,
1821 if (!g_markup_parse_context_end_parse (context, error))
1826 /* Now insert the text */
1827 insert_text (&info, iter);
1830 parse_info_free (&info);
1832 g_markup_parse_context_free (context);
1838 _gtk_text_buffer_deserialize_rich_text (GtkTextBuffer *register_buffer,
1839 GtkTextBuffer *content_buffer,
1843 gboolean create_tags,
1851 headers = read_headers ((gchar *) text, length, error);
1856 header = headers->data;
1857 if (!header_is (header, "GTKTEXTBUFFERCONTENTS-0001"))
1859 g_set_error_literal (error,
1861 G_MARKUP_ERROR_PARSE,
1862 _("Serialized data is malformed. First section isn't GTKTEXTBUFFERCONTENTS-0001"));
1868 retval = deserialize_text (content_buffer, iter,
1869 header->start, header->length,
1870 create_tags, error, headers->next);
1873 g_list_foreach (headers, (GFunc)g_free, NULL);
1874 g_list_free (headers);