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"
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)
118 v = g_ascii_strtoll (str, &tmp, 10);
120 if (errno || tmp == NULL || tmp == str)
123 g_value_set_int (value, v);
127 else if (value->g_type == G_TYPE_DOUBLE)
132 v = g_ascii_strtod (str, &tmp);
134 if (tmp == NULL || tmp == str)
137 g_value_set_double (value, v);
141 else if (value->g_type == GDK_TYPE_COLOR)
150 color.red = g_ascii_strtoll (old, &tmp, 16);
152 if (errno || tmp == old)
161 color.green = g_ascii_strtoll (old, &tmp, 16);
162 if (errno || tmp == old)
171 color.blue = g_ascii_strtoll (old, &tmp, 16);
173 if (errno || tmp == old || *tmp != '\0')
176 g_value_set_boxed (value, &color);
180 else if (G_VALUE_HOLDS_ENUM (value))
182 GEnumClass *class = G_ENUM_CLASS (g_type_class_peek (value->g_type));
183 GEnumValue *enum_value;
185 enum_value = g_enum_get_value_by_name (class, str);
189 g_value_set_enum (value, enum_value->value);
197 g_warning ("Type %s can not be deserialized\n", g_type_name (value->g_type));
203 /* Checks if a param is set, or if it's the default value */
205 is_param_set (GObject *object,
209 /* We need to special case some attributes here */
210 if (strcmp (pspec->name, "background-gdk") == 0)
214 g_object_get (object, "background-set", &is_set, NULL);
218 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
220 g_object_get_property (object, pspec->name, value);
227 else if (strcmp (pspec->name, "foreground-gdk") == 0)
231 g_object_get (object, "foreground-set", &is_set, NULL);
235 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
237 g_object_get_property (object, pspec->name, value);
249 is_set_name = g_strdup_printf ("%s-set", pspec->name);
251 if (g_object_class_find_property (G_OBJECT_GET_CLASS (object), is_set_name) == NULL)
253 g_free (is_set_name);
258 g_object_get (object, is_set_name, &is_set, NULL);
262 g_free (is_set_name);
266 g_free (is_set_name);
268 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
270 g_object_get_property (object, pspec->name, value);
272 if (g_param_value_defaults (pspec, value))
274 g_value_unset (value);
284 serialize_tag (gpointer key,
288 SerializationContext *context = user_data;
289 GtkTextTag *tag = data;
296 g_string_append (context->tag_table_str, " <tag ");
298 /* Handle anonymous tags */
301 tag_name = g_markup_escape_text (tag->name, -1);
302 g_string_append_printf (context->tag_table_str, "name=\"%s\"", tag_name);
307 tag_id = GPOINTER_TO_INT (g_hash_table_lookup (context->tag_id_tags, tag));
309 g_string_append_printf (context->tag_table_str, "id=\"%d\"", tag_id);
312 g_string_append_printf (context->tag_table_str, " priority=\"%d\">\n", tag->priority);
314 /* Serialize properties */
315 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (tag), &n_pspecs);
317 for (i = 0; i < n_pspecs; i++)
319 GValue value = { 0 };
322 if (!(pspecs[i]->flags & G_PARAM_READABLE) ||
323 !(pspecs[i]->flags & G_PARAM_WRITABLE))
326 if (!is_param_set (G_OBJECT (tag), pspecs[i], &value))
329 /* Now serialize the attr */
330 tmp2 = serialize_value (&value);
334 tmp = g_markup_escape_text (pspecs[i]->name, -1);
335 g_string_append_printf (context->tag_table_str, " <attr name=\"%s\" ", tmp);
338 tmp = g_markup_escape_text (g_type_name (pspecs[i]->value_type), -1);
339 g_string_append_printf (context->tag_table_str, "type=\"%s\" value=\"%s\" />\n", tmp, tmp2);
345 g_value_unset (&value);
350 g_string_append (context->tag_table_str, " </tag>\n");
354 serialize_tags (SerializationContext *context)
356 g_string_append (context->tag_table_str, " <text_view_markup>\n");
357 g_string_append (context->tag_table_str, " <tags>\n");
358 g_hash_table_foreach (context->tags, serialize_tag, context);
359 g_string_append (context->tag_table_str, " </tags>\n");
364 dump_tag_list (const gchar *str,
367 g_print ("%s: ", str);
375 g_print ("%s ", ((GtkTextTag *)list->data)->name);
385 find_list_delta (GSList *old_list,
391 GList *tmp_added, *tmp_removed;
396 /* Find added tags */
400 if (!g_slist_find (old_list, tmp->data))
401 tmp_added = g_list_prepend (tmp_added, tmp->data);
408 /* Find removed tags */
412 if (!g_slist_find (new_list, tmp->data))
413 tmp_removed = g_list_prepend (tmp_removed, tmp->data);
418 /* We reverse the list here to match the xml semantics */
419 *removed = g_list_reverse (tmp_removed);
423 serialize_section_header (GString *str,
427 g_return_if_fail (strlen (name) == 26);
429 g_string_append (str, name);
431 g_string_append_c (str, length >> 24);
433 g_string_append_c (str, (length >> 16) & 0xff);
434 g_string_append_c (str, (length >> 8) & 0xff);
435 g_string_append_c (str, length & 0xff);
439 serialize_text (GtkTextBuffer *buffer,
440 SerializationContext *context)
442 GtkTextIter iter, old_iter;
443 GSList *tag_list, *new_tag_list;
446 g_string_append (context->text_str, "<text>");
448 iter = context->start;
454 GList *added, *removed;
456 gchar *tmp_text, *escaped_text;
458 new_tag_list = gtk_text_iter_get_tags (&iter);
459 find_list_delta (tag_list, new_tag_list, &added, &removed);
461 /* Handle removed tags */
462 for (tmp = removed; tmp; tmp = tmp->next)
464 GtkTextTag *tag = tmp->data;
466 /* Only close the tag if we didn't close it before (by using
467 * the stack logic in the while() loop below)
469 if (g_slist_find (active_tags, tag))
471 g_string_append (context->text_str, "</apply_tag>");
473 /* Drop all tags that were opened after this one (which are
474 * above this on in the stack)
476 while (active_tags->data != tag)
478 added = g_list_prepend (added, active_tags->data);
479 active_tags = g_slist_remove (active_tags, active_tags->data);
480 g_string_append_printf (context->text_str, "</apply_tag>");
483 active_tags = g_slist_remove (active_tags, active_tags->data);
487 /* Handle added tags */
488 for (tmp = added; tmp; tmp = tmp->next)
490 GtkTextTag *tag = tmp->data;
493 /* Add it to the tag hash table */
494 g_hash_table_insert (context->tags, tag, tag);
498 tag_name = g_markup_escape_text (tag->name, -1);
500 g_string_append_printf (context->text_str, "<apply_tag name=\"%s\">", tag_name);
507 /* We've got an anonymous tag, find out if it's been
509 if (!g_hash_table_lookup_extended (context->tag_id_tags, tag, NULL, &tag_id))
511 tag_id = GINT_TO_POINTER (context->tag_id++);
513 g_hash_table_insert (context->tag_id_tags, tag, tag_id);
516 g_string_append_printf (context->text_str, "<apply_tag id=\"%d\">", GPOINTER_TO_INT (tag_id));
519 active_tags = g_slist_prepend (active_tags, tag);
522 g_slist_free (tag_list);
523 tag_list = new_tag_list;
526 g_list_free (removed);
530 /* Now try to go to either the next tag toggle, or if a pixbuf appears */
533 gunichar ch = gtk_text_iter_get_char (&iter);
537 GdkPixbuf *pixbuf = gtk_text_iter_get_pixbuf (&iter);
541 /* Append the text before the pixbuf */
542 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
543 escaped_text = g_markup_escape_text (tmp_text, -1);
546 /* Forward so we don't get the 0xfffc char */
547 gtk_text_iter_forward_char (&iter);
550 g_string_append (context->text_str, escaped_text);
551 g_free (escaped_text);
553 g_string_append_printf (context->text_str, "<pixbuf index=\"%d\" />", context->n_pixbufs);
555 context->n_pixbufs++;
556 context->pixbufs = g_list_prepend (context->pixbufs, pixbuf);
564 gtk_text_iter_forward_char (&iter);
566 if (gtk_text_iter_toggles_tag (&iter, NULL))
570 /* We might have moved too far */
571 if (gtk_text_iter_compare (&iter, &context->end) > 0)
574 /* Append the text */
575 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
576 escaped_text = g_markup_escape_text (tmp_text, -1);
579 g_string_append (context->text_str, escaped_text);
580 g_free (escaped_text);
582 while (!gtk_text_iter_equal (&iter, &context->end));
584 /* Close any open tags */
585 for (tag_list = active_tags; tag_list; tag_list = tag_list->next)
586 g_string_append (context->text_str, "</apply_tag>");
588 g_slist_free (active_tags);
589 g_string_append (context->text_str, "</text>\n</text_view_markup>\n");
593 serialize_pixbufs (SerializationContext *context,
598 for (list = context->pixbufs; list != NULL; list = list->next)
600 GdkPixbuf *pixbuf = list->data;
605 gdk_pixdata_from_pixbuf (&pixdata, pixbuf, FALSE);
606 tmp = gdk_pixdata_serialize (&pixdata, &len);
608 serialize_section_header (text, "GTKTEXTBUFFERPIXBDATA-0001", len);
609 g_string_append_len (text, (gchar *) tmp, len);
615 _gtk_text_buffer_serialize_rich_text (GtkTextBuffer *register_buffer,
616 GtkTextBuffer *content_buffer,
617 const GtkTextIter *start,
618 const GtkTextIter *end,
622 SerializationContext context;
625 context.tags = g_hash_table_new (NULL, NULL);
626 context.text_str = g_string_new (NULL);
627 context.tag_table_str = g_string_new (NULL);
628 context.start = *start;
630 context.n_pixbufs = 0;
631 context.pixbufs = NULL;
633 context.tag_id_tags = g_hash_table_new (NULL, NULL);
635 /* We need to serialize the text before the tag table so we know
636 what tags are used */
637 serialize_text (content_buffer, &context);
638 serialize_tags (&context);
640 text = g_string_new (NULL);
641 serialize_section_header (text, "GTKTEXTBUFFERCONTENTS-0001",
642 context.tag_table_str->len + context.text_str->len);
644 g_string_append_len (text, context.tag_table_str->str, context.tag_table_str->len);
645 g_string_append_len (text, context.text_str->str, context.text_str->len);
647 context.pixbufs = g_list_reverse (context.pixbufs);
648 serialize_pixbufs (&context, text);
650 g_hash_table_destroy (context.tags);
651 g_list_free (context.pixbufs);
652 g_string_free (context.text_str, TRUE);
653 g_string_free (context.tag_table_str, TRUE);
654 g_hash_table_destroy (context.tag_id_tags);
658 return (guint8 *) g_string_free (text, FALSE);
664 STATE_TEXT_VIEW_MARKUP,
692 GtkTextBuffer *buffer;
694 /* Tags that are defined in <tag> elements */
695 GHashTable *defined_tags;
697 /* Tags that are anonymous */
698 GHashTable *anonymous_tags;
700 /* Tag name substitutions */
701 GHashTable *substitutions;
704 GtkTextTag *current_tag;
706 /* Priority of current tag */
707 gint current_tag_prio;
709 /* Id of current tag */
712 /* Tags and their priorities */
713 GList *tag_priorities;
719 gboolean create_tags;
721 gboolean parsed_text;
722 gboolean parsed_tags;
726 set_error (GError **err,
727 GMarkupParseContext *context,
737 g_markup_parse_context_get_position (context, &line, &ch);
739 va_start (args, format);
740 str = g_strdup_vprintf (format, args);
743 g_set_error (err, error_domain, error_code,
744 ("Line %d character %d: %s"),
751 push_state (ParseInfo *info,
754 info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
758 pop_state (ParseInfo *info)
760 g_return_if_fail (info->states != NULL);
762 info->states = g_slist_remove (info->states, info->states->data);
766 peek_state (ParseInfo *info)
768 g_return_val_if_fail (info->states != NULL, STATE_START);
770 return GPOINTER_TO_INT (info->states->data);
773 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
777 check_id_or_name (GMarkupParseContext *context,
778 const gchar *element_name,
779 const gchar **attribute_names,
780 const gchar **attribute_values,
785 gboolean has_id = FALSE;
786 gboolean has_name = FALSE;
792 for (i = 0; attribute_names[i] != NULL; i++)
794 if (strcmp (attribute_names[i], "name") == 0)
796 *name = attribute_values[i];
800 set_error (error, context,
802 G_MARKUP_ERROR_PARSE,
803 _("Both \"id\" and \"name\" were found on the <%s> element"),
810 set_error (error, context,
812 G_MARKUP_ERROR_PARSE,
813 _("The attribute \"%s\" was found twice on the <%s> element"),
814 "name", element_name);
820 else if (strcmp (attribute_names[i], "id") == 0)
826 set_error (error, context,
828 G_MARKUP_ERROR_PARSE,
829 _("Both \"id\" and \"name\" were found on the <%s> element"),
836 set_error (error, context,
838 G_MARKUP_ERROR_PARSE,
839 _("The attribute \"%s\" was found twice on the <%s> element"),
846 /* Try parsing the integer */
849 *id = g_ascii_strtoll (attribute_values[i], &tmp, 10);
851 if (errno || tmp == attribute_values[i])
853 set_error (error, context,
854 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
855 _("<%s> element has invalid ID \"%s\""), attribute_values[i]);
861 if (!has_id && !has_name)
863 set_error (error, context,
864 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
865 _("<%s> element has neither a \"name\" nor an \"id\" attribute"), element_name);
879 locate_attributes (GMarkupParseContext *context,
880 const char *element_name,
881 const char **attribute_names,
882 const char **attribute_values,
883 gboolean allow_unknown_attrs,
885 const char *first_attribute_name,
886 const char **first_attribute_retloc,
894 LocateAttr attrs[MAX_ATTRS];
898 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
899 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
904 attrs[0].name = first_attribute_name;
905 attrs[0].retloc = first_attribute_retloc;
906 *first_attribute_retloc = NULL;
908 va_start (args, first_attribute_retloc);
910 name = va_arg (args, const char*);
911 retloc = va_arg (args, const char**);
915 g_return_val_if_fail (retloc != NULL, FALSE);
917 g_assert (n_attrs < MAX_ATTRS);
919 attrs[n_attrs].name = name;
920 attrs[n_attrs].retloc = retloc;
924 name = va_arg (args, const char*);
925 retloc = va_arg (args, const char**);
934 while (attribute_names[i])
943 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
945 retloc = attrs[j].retloc;
949 set_error (error, context,
951 G_MARKUP_ERROR_PARSE,
952 _("Attribute \"%s\" repeated twice on the same <%s> element"),
953 attrs[j].name, element_name);
958 *retloc = attribute_values[i];
965 if (!found && !allow_unknown_attrs)
967 set_error (error, context,
969 G_MARKUP_ERROR_PARSE,
970 _("Attribute \"%s\" is invalid on <%s> element in this context"),
971 attribute_names[i], element_name);
984 check_no_attributes (GMarkupParseContext *context,
985 const char *element_name,
986 const char **attribute_names,
987 const char **attribute_values,
990 if (attribute_names[0] != NULL)
992 set_error (error, context,
994 G_MARKUP_ERROR_PARSE,
995 _("Attribute \"%s\" is invalid on <%s> element in this context"),
996 attribute_names[0], element_name);
1004 tag_exists (GMarkupParseContext *context,
1010 GtkTextTagTable *tag_table;
1011 const gchar *real_name;
1013 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1015 if (info->create_tags)
1017 /* If we have an anonymous tag, just return it directly */
1019 return g_hash_table_lookup (info->anonymous_tags,
1020 GINT_TO_POINTER (id));
1022 /* First, try the substitutions */
1023 real_name = g_hash_table_lookup (info->substitutions, name);
1026 return gtk_text_tag_table_lookup (tag_table, real_name);
1028 /* Next, try the list of defined tags */
1029 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1030 return gtk_text_tag_table_lookup (tag_table, name);
1032 set_error (error, context,
1033 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1034 _("Tag \"%s\" has not been defined."), name);
1044 set_error (error, context,
1045 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1046 _("Anonymous tag found and tags can not be created."));
1050 tag = gtk_text_tag_table_lookup (tag_table, name);
1055 set_error (error, context,
1056 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1057 _("Tag \"%s\" does not exist in buffer and tags can not be created."), name);
1071 get_pixbuf_from_headers (GList *headers,
1079 header = g_list_nth_data (headers, id);
1084 if (!gdk_pixdata_deserialize (&pixdata, header->length,
1085 (const guint8 *) header->start, error))
1088 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, TRUE, error);
1094 parse_apply_tag_element (GMarkupParseContext *context,
1095 const gchar *element_name,
1096 const gchar **attribute_names,
1097 const gchar **attribute_values,
1101 const gchar *name, *priority;
1105 g_assert (peek_state (info) == STATE_TEXT ||
1106 peek_state (info) == STATE_APPLY_TAG);
1108 if (ELEMENT_IS ("apply_tag"))
1110 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1111 "priority", &priority, NULL))
1114 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1119 tag = tag_exists (context, name, id, info, error);
1124 info->tag_stack = g_slist_prepend (info->tag_stack, tag);
1126 push_state (info, STATE_APPLY_TAG);
1128 else if (ELEMENT_IS ("pixbuf"))
1133 const gchar *pixbuf_id;
1135 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1136 "index", &pixbuf_id, NULL))
1139 int_id = atoi (pixbuf_id);
1140 pixbuf = get_pixbuf_from_headers (info->headers, int_id, error);
1142 span = g_new0 (TextSpan, 1);
1143 span->pixbuf = pixbuf;
1146 info->spans = g_list_prepend (info->spans, span);
1151 push_state (info, STATE_PIXBUF);
1154 set_error (error, context,
1155 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1156 _("Element <%s> is not allowed below <%s>"),
1157 element_name, peek_state(info) == STATE_TEXT ? "text" : "apply_tag");
1161 parse_attr_element (GMarkupParseContext *context,
1162 const gchar *element_name,
1163 const gchar **attribute_names,
1164 const gchar **attribute_values,
1168 const gchar *name, *type, *value;
1170 GValue gvalue = { 0 };
1173 g_assert (peek_state (info) == STATE_TAG);
1175 if (ELEMENT_IS ("attr"))
1177 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1178 "name", &name, "type", &type, "value", &value, NULL))
1181 gtype = g_type_from_name (type);
1183 if (gtype == G_TYPE_INVALID)
1185 set_error (error, context,
1186 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1187 _("\"%s\" is not a valid attribute type"), type);
1191 if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (info->current_tag), name)))
1193 set_error (error, context,
1194 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1195 _("\"%s\" is not a valid attribute name"), name);
1199 g_value_init (&gvalue, gtype);
1201 if (!deserialize_value (value, &gvalue))
1203 set_error (error, context,
1204 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1205 _("\"%s\" could not be converted to a value of type \"%s\" for attribute \"%s\""),
1210 if (g_param_value_validate (pspec, &gvalue))
1212 set_error (error, context,
1213 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1214 _("\"%s\" is not a valid value for attribute \"%s\""),
1216 g_value_unset (&gvalue);
1220 g_object_set_property (G_OBJECT (info->current_tag),
1223 g_value_unset (&gvalue);
1225 push_state (info, STATE_ATTR);
1229 set_error (error, context,
1230 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1231 _("Element <%s> is not allowed below <%s>"),
1232 element_name, "tag");
1238 get_tag_name (ParseInfo *info,
1239 const gchar *tag_name)
1241 GtkTextTagTable *tag_table;
1245 name = g_strdup (tag_name);
1247 if (!info->create_tags)
1251 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1253 while (gtk_text_tag_table_lookup (tag_table, name) != NULL)
1256 name = g_strdup_printf ("%s-%d", tag_name, ++i);
1261 g_hash_table_insert (info->substitutions, g_strdup (tag_name), g_strdup (name));
1268 parse_tag_element (GMarkupParseContext *context,
1269 const gchar *element_name,
1270 const gchar **attribute_names,
1271 const gchar **attribute_values,
1275 const gchar *name, *priority;
1281 g_assert (peek_state (info) == STATE_TAGS);
1283 if (ELEMENT_IS ("tag"))
1285 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1286 "priority", &priority, NULL))
1289 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1295 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1297 set_error (error, context,
1298 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1299 _("Tag \"%s\" already defined"), name);
1306 prio = g_ascii_strtoll (priority, &tmp, 10);
1308 if (errno || tmp == priority)
1310 set_error (error, context,
1311 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1312 _("Tag \"%s\" has invalid priority \"%s\""), name, priority);
1318 tag_name = get_tag_name (info, name);
1319 info->current_tag = gtk_text_tag_new (tag_name);
1324 info->current_tag = gtk_text_tag_new (NULL);
1325 info->current_tag_id = id;
1328 info->current_tag_prio = prio;
1330 push_state (info, STATE_TAG);
1334 set_error (error, context,
1335 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1336 _("Element <%s> is not allowed below <%s>"),
1337 element_name, "tags");
1342 start_element_handler (GMarkupParseContext *context,
1343 const gchar *element_name,
1344 const gchar **attribute_names,
1345 const gchar **attribute_values,
1349 ParseInfo *info = user_data;
1351 switch (peek_state (info))
1354 if (ELEMENT_IS ("text_view_markup"))
1356 if (!check_no_attributes (context, element_name,
1357 attribute_names, attribute_values, error))
1360 push_state (info, STATE_TEXT_VIEW_MARKUP);
1364 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1365 _("Outermost element in text must be <text_view_markup> not <%s>"),
1368 case STATE_TEXT_VIEW_MARKUP:
1369 if (ELEMENT_IS ("tags"))
1371 if (info->parsed_tags)
1373 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1374 _("A <%s> element has already been specified"), "tags");
1378 if (!check_no_attributes (context, element_name,
1379 attribute_names, attribute_values, error))
1382 push_state (info, STATE_TAGS);
1385 else if (ELEMENT_IS ("text"))
1387 if (info->parsed_text)
1389 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1390 _("A <%s> element has already been specified"), "text");
1393 else if (!info->parsed_tags)
1395 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1396 _("A <text> element can't occur before a <tags> element"));
1400 if (!check_no_attributes (context, element_name,
1401 attribute_names, attribute_values, error))
1404 push_state (info, STATE_TEXT);
1408 set_error (error, context,
1409 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1410 _("Element <%s> is not allowed below <%s>"),
1411 element_name, "text_view_markup");
1414 parse_tag_element (context, element_name,
1415 attribute_names, attribute_values,
1419 parse_attr_element (context, element_name,
1420 attribute_names, attribute_values,
1424 case STATE_APPLY_TAG:
1425 parse_apply_tag_element (context, element_name,
1426 attribute_names, attribute_values,
1430 g_assert_not_reached ();
1436 sort_tag_prio (TextTagPrio *a,
1439 if (a->prio < b->prio)
1441 else if (a->prio > b->prio)
1448 end_element_handler (GMarkupParseContext *context,
1449 const gchar *element_name,
1453 ParseInfo *info = user_data;
1457 switch (peek_state (info))
1461 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1463 info->parsed_tags = TRUE;
1465 /* Sort list and add the tags */
1466 info->tag_priorities = g_list_sort (info->tag_priorities,
1467 (GCompareFunc)sort_tag_prio);
1468 list = info->tag_priorities;
1471 TextTagPrio *prio = list->data;
1473 if (info->create_tags)
1474 gtk_text_tag_table_add (gtk_text_buffer_get_tag_table (info->buffer),
1477 g_object_unref (prio->tag);
1486 g_assert (peek_state (info) == STATE_TAGS);
1488 if (info->current_tag->name)
1490 /* Add tag to defined tags hash */
1491 tmp = g_strdup (info->current_tag->name);
1492 g_hash_table_insert (info->defined_tags,
1497 g_hash_table_insert (info->anonymous_tags,
1498 GINT_TO_POINTER (info->current_tag_id),
1502 if (info->create_tags)
1506 /* add the tag to the list */
1507 prio = g_new0 (TextTagPrio, 1);
1508 prio->prio = info->current_tag_prio;
1509 prio->tag = info->current_tag;
1511 info->tag_priorities = g_list_prepend (info->tag_priorities, prio);
1514 info->current_tag = NULL;
1518 g_assert (peek_state (info) == STATE_TAG);
1520 case STATE_APPLY_TAG:
1522 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1523 peek_state (info) == STATE_TEXT);
1526 info->tag_stack = g_slist_delete_link (info->tag_stack,
1532 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1534 info->spans = g_list_reverse (info->spans);
1535 info->parsed_text = TRUE;
1537 case STATE_TEXT_VIEW_MARKUP:
1539 g_assert (peek_state (info) == STATE_START);
1543 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1544 peek_state (info) == STATE_TEXT);
1547 g_assert_not_reached ();
1553 all_whitespace (const char *text,
1560 end = text + text_len;
1564 if (!g_ascii_isspace (*p))
1567 p = g_utf8_next_char (p);
1574 text_handler (GMarkupParseContext *context,
1580 ParseInfo *info = user_data;
1583 if (all_whitespace (text, text_len) &&
1584 peek_state (info) != STATE_TEXT &&
1585 peek_state (info) != STATE_APPLY_TAG)
1588 switch (peek_state (info))
1591 g_assert_not_reached (); /* gmarkup shouldn't do this */
1594 case STATE_APPLY_TAG:
1598 span = g_new0 (TextSpan, 1);
1599 span->text = g_strndup (text, text_len);
1600 span->tags = g_slist_copy (info->tag_stack);
1602 info->spans = g_list_prepend (info->spans, span);
1605 g_assert_not_reached ();
1611 parse_info_init (ParseInfo *info,
1612 GtkTextBuffer *buffer,
1613 gboolean create_tags,
1616 info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
1618 info->create_tags = create_tags;
1619 info->headers = headers;
1620 info->defined_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1621 info->substitutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1622 info->anonymous_tags = g_hash_table_new_full (NULL, NULL, NULL, NULL);
1623 info->tag_stack = NULL;
1625 info->parsed_text = FALSE;
1626 info->parsed_tags = FALSE;
1627 info->current_tag = NULL;
1628 info->current_tag_prio = -1;
1629 info->tag_priorities = NULL;
1631 info->buffer = buffer;
1635 text_span_free (TextSpan *span)
1637 g_free (span->text);
1638 g_slist_free (span->tags);
1643 parse_info_free (ParseInfo *info)
1647 g_slist_free (info->tag_stack);
1648 g_slist_free (info->states);
1650 g_hash_table_destroy (info->substitutions);
1651 g_hash_table_destroy (info->defined_tags);
1653 if (info->current_tag)
1654 g_object_unref (info->current_tag);
1659 text_span_free (list->data);
1663 g_list_free (info->spans);
1665 list = info->tag_priorities;
1668 TextTagPrio *prio = list->data;
1671 g_object_unref (prio->tag);
1676 g_list_free (info->tag_priorities);
1681 insert_text (ParseInfo *info,
1684 GtkTextIter start_iter;
1691 mark = gtk_text_buffer_create_mark (info->buffer, "deserialize_insert_point",
1697 TextSpan *span = tmp->data;
1700 gtk_text_buffer_insert (info->buffer, iter, span->text, -1);
1703 gtk_text_buffer_insert_pixbuf (info->buffer, iter, span->pixbuf);
1704 g_object_unref (span->pixbuf);
1706 gtk_text_buffer_get_iter_at_mark (info->buffer, &start_iter, mark);
1712 GtkTextTag *tag = tags->data;
1714 gtk_text_buffer_apply_tag (info->buffer, tag,
1720 gtk_text_buffer_move_mark (info->buffer, mark, iter);
1725 gtk_text_buffer_delete_mark (info->buffer, mark);
1731 read_int (const guchar *start)
1745 header_is (Header *header,
1748 return (strncmp (header->id, id, strlen (id)) == 0);
1752 read_headers (const gchar *start,
1759 GList *headers = NULL;
1766 if (strncmp (start + i, "GTKTEXTBUFFERCONTENTS-0001", 26) == 0 ||
1767 strncmp (start + i, "GTKTEXTBUFFERPIXBDATA-0001", 26) == 0)
1769 section_len = read_int ((const guchar *) start + i + 26);
1771 if (i + 30 + section_len > len)
1774 header = g_new0 (Header, 1);
1775 header->id = start + i;
1776 header->length = section_len;
1777 header->start = start + i + 30;
1779 i += 30 + section_len;
1781 headers = g_list_prepend (headers, header);
1787 return g_list_reverse (headers);
1790 g_list_foreach (headers, (GFunc) g_free, NULL);
1791 g_list_free (headers);
1793 g_set_error_literal (error,
1795 G_MARKUP_ERROR_PARSE,
1796 _("Serialized data is malformed"));
1802 deserialize_text (GtkTextBuffer *buffer,
1806 gboolean create_tags,
1810 GMarkupParseContext *context;
1812 gboolean retval = FALSE;
1814 static const GMarkupParser rich_text_parser = {
1815 start_element_handler,
1816 end_element_handler,
1822 parse_info_init (&info, buffer, create_tags, headers);
1824 context = g_markup_parse_context_new (&rich_text_parser,
1827 if (!g_markup_parse_context_parse (context,
1833 if (!g_markup_parse_context_end_parse (context, error))
1838 /* Now insert the text */
1839 insert_text (&info, iter);
1842 parse_info_free (&info);
1844 g_markup_parse_context_free (context);
1850 _gtk_text_buffer_deserialize_rich_text (GtkTextBuffer *register_buffer,
1851 GtkTextBuffer *content_buffer,
1855 gboolean create_tags,
1863 headers = read_headers ((gchar *) text, length, error);
1868 header = headers->data;
1869 if (!header_is (header, "GTKTEXTBUFFERCONTENTS-0001"))
1871 g_set_error_literal (error,
1873 G_MARKUP_ERROR_PARSE,
1874 _("Serialized data is malformed. First section isn't GTKTEXTBUFFERCONTENTS-0001"));
1880 retval = deserialize_text (content_buffer, iter,
1881 header->start, header->length,
1882 create_tags, error, headers->next);
1885 g_list_foreach (headers, (GFunc)g_free, NULL);
1886 g_list_free (headers);