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);
75 g_warning ("Type %s is not serializable\n", g_type_name (value->g_type));
82 deserialize_value (const gchar *str,
85 if (g_value_type_transformable (G_TYPE_STRING, value->g_type))
87 GValue text_value = { 0 };
90 g_value_init (&text_value, G_TYPE_STRING);
91 g_value_set_static_string (&text_value, str);
93 retval = g_value_transform (&text_value, value);
94 g_value_unset (&text_value);
98 else if (value->g_type == G_TYPE_BOOLEAN)
102 v = strcmp (str, "TRUE") == 0;
104 g_value_set_boolean (value, v);
108 else if (value->g_type == G_TYPE_INT)
114 v = g_ascii_strtoll (str, &tmp, 10);
116 if (errno || tmp == NULL || tmp == str)
119 g_value_set_int (value, v);
123 else if (value->g_type == G_TYPE_DOUBLE)
128 v = g_ascii_strtod (str, &tmp);
130 if (tmp == NULL || tmp == str)
133 g_value_set_double (value, v);
137 else if (value->g_type == GDK_TYPE_COLOR)
146 color.red = g_ascii_strtoll (old, &tmp, 16);
148 if (errno || tmp == old)
157 color.green = g_ascii_strtoll (old, &tmp, 16);
158 if (errno || tmp == old)
167 color.blue = g_ascii_strtoll (old, &tmp, 16);
169 if (errno || tmp == old || *tmp != '\0')
172 g_value_set_boxed (value, &color);
176 else if (G_VALUE_HOLDS_ENUM (value))
178 GEnumClass *class = G_ENUM_CLASS (g_type_class_peek (value->g_type));
179 GEnumValue *enum_value;
181 enum_value = g_enum_get_value_by_name (class, str);
185 g_value_set_enum (value, enum_value->value);
193 g_warning ("Type %s can not be deserialized\n", g_type_name (value->g_type));
199 /* Checks if a param is set, or if it's the default value */
201 is_param_set (GObject *object,
205 /* We need to special case some attributes here */
206 if (strcmp (pspec->name, "background-gdk") == 0)
210 g_object_get (object, "background-set", &is_set, NULL);
214 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
216 g_object_get_property (object, pspec->name, value);
223 else if (strcmp (pspec->name, "foreground-gdk") == 0)
227 g_object_get (object, "foreground-set", &is_set, NULL);
231 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
233 g_object_get_property (object, pspec->name, value);
245 is_set_name = g_strdup_printf ("%s-set", pspec->name);
247 if (g_object_class_find_property (G_OBJECT_GET_CLASS (object), is_set_name) == NULL)
249 g_free (is_set_name);
254 g_object_get (object, is_set_name, &is_set, NULL);
258 g_free (is_set_name);
262 g_free (is_set_name);
264 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
266 g_object_get_property (object, pspec->name, value);
268 if (g_param_value_defaults (pspec, value))
270 g_value_unset (value);
280 serialize_tag (gpointer key,
284 SerializationContext *context = user_data;
285 GtkTextTag *tag = data;
292 g_string_append (context->tag_table_str, " <tag ");
294 /* Handle anonymous tags */
297 tag_name = g_markup_escape_text (tag->name, -1);
298 g_string_append_printf (context->tag_table_str, "name=\"%s\"", tag_name);
303 tag_id = GPOINTER_TO_INT (g_hash_table_lookup (context->tag_id_tags, tag));
305 g_string_append_printf (context->tag_table_str, "id=\"%d\"", tag_id);
308 g_string_append_printf (context->tag_table_str, " priority=\"%d\">\n", tag->priority);
310 /* Serialize properties */
311 pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (tag), &n_pspecs);
313 for (i = 0; i < n_pspecs; i++)
315 GValue value = { 0 };
318 if (!(pspecs[i]->flags & G_PARAM_READABLE) ||
319 !(pspecs[i]->flags & G_PARAM_WRITABLE))
322 if (!is_param_set (G_OBJECT (tag), pspecs[i], &value))
325 /* Now serialize the attr */
326 tmp2 = serialize_value (&value);
330 tmp = g_markup_escape_text (pspecs[i]->name, -1);
331 g_string_append_printf (context->tag_table_str, " <attr name=\"%s\" ", tmp);
334 tmp = g_markup_escape_text (g_type_name (pspecs[i]->value_type), -1);
335 g_string_append_printf (context->tag_table_str, "type=\"%s\" value=\"%s\" />\n", tmp, tmp2);
341 g_value_unset (&value);
346 g_string_append (context->tag_table_str, " </tag>\n");
350 serialize_tags (SerializationContext *context)
352 g_string_append (context->tag_table_str, " <text_view_markup>\n");
353 g_string_append (context->tag_table_str, " <tags>\n");
354 g_hash_table_foreach (context->tags, serialize_tag, context);
355 g_string_append (context->tag_table_str, " </tags>\n");
360 dump_tag_list (const gchar *str,
363 g_print ("%s: ", str);
371 g_print ("%s ", ((GtkTextTag *)list->data)->name);
381 find_list_delta (GSList *old_list,
387 GList *tmp_added, *tmp_removed;
392 /* Find added tags */
396 if (!g_slist_find (old_list, tmp->data))
397 tmp_added = g_list_prepend (tmp_added, tmp->data);
404 /* Find removed tags */
408 if (!g_slist_find (new_list, tmp->data))
409 tmp_removed = g_list_prepend (tmp_removed, tmp->data);
414 /* We reverse the list here to match the xml semantics */
415 *removed = g_list_reverse (tmp_removed);
419 serialize_section_header (GString *str,
423 g_return_if_fail (strlen (name) == 26);
425 g_string_append (str, name);
427 g_string_append_c (str, length >> 24);
429 g_string_append_c (str, (length >> 16) & 0xff);
430 g_string_append_c (str, (length >> 8) & 0xff);
431 g_string_append_c (str, length & 0xff);
435 serialize_text (GtkTextBuffer *buffer,
436 SerializationContext *context)
438 GtkTextIter iter, old_iter;
439 GSList *tag_list, *new_tag_list;
442 g_string_append (context->text_str, "<text>");
444 iter = context->start;
450 GList *added, *removed;
452 gchar *tmp_text, *escaped_text;
454 new_tag_list = gtk_text_iter_get_tags (&iter);
455 find_list_delta (tag_list, new_tag_list, &added, &removed);
457 /* Handle removed tags */
458 for (tmp = removed; tmp; tmp = tmp->next)
460 GtkTextTag *tag = tmp->data;
462 /* Only close the tag if we didn't close it before (by using
463 * the stack logic in the while() loop below)
465 if (g_slist_find (active_tags, tag))
467 g_string_append (context->text_str, "</apply_tag>");
469 /* Drop all tags that were opened after this one (which are
470 * above this on in the stack)
472 while (active_tags->data != tag)
474 added = g_list_prepend (added, active_tags->data);
475 active_tags = g_slist_remove (active_tags, active_tags->data);
476 g_string_append_printf (context->text_str, "</apply_tag>");
479 active_tags = g_slist_remove (active_tags, active_tags->data);
483 /* Handle added tags */
484 for (tmp = added; tmp; tmp = tmp->next)
486 GtkTextTag *tag = tmp->data;
489 /* Add it to the tag hash table */
490 g_hash_table_insert (context->tags, tag, tag);
494 tag_name = g_markup_escape_text (tag->name, -1);
496 g_string_append_printf (context->text_str, "<apply_tag name=\"%s\">", tag_name);
503 /* We've got an anonymous tag, find out if it's been
505 if (!g_hash_table_lookup_extended (context->tag_id_tags, tag, NULL, &tag_id))
507 tag_id = GINT_TO_POINTER (context->tag_id++);
509 g_hash_table_insert (context->tag_id_tags, tag, tag_id);
512 g_string_append_printf (context->text_str, "<apply_tag id=\"%d\">", GPOINTER_TO_INT (tag_id));
515 active_tags = g_slist_prepend (active_tags, tag);
518 g_slist_free (tag_list);
519 tag_list = new_tag_list;
522 g_list_free (removed);
526 /* Now try to go to either the next tag toggle, or if a pixbuf appears */
529 gunichar ch = gtk_text_iter_get_char (&iter);
533 GdkPixbuf *pixbuf = gtk_text_iter_get_pixbuf (&iter);
537 /* Append the text before the pixbuf */
538 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
539 escaped_text = g_markup_escape_text (tmp_text, -1);
542 /* Forward so we don't get the 0xfffc char */
543 gtk_text_iter_forward_char (&iter);
546 g_string_append (context->text_str, escaped_text);
547 g_free (escaped_text);
549 g_string_append_printf (context->text_str, "<pixbuf index=\"%d\" />", context->n_pixbufs);
551 context->n_pixbufs++;
552 context->pixbufs = g_list_prepend (context->pixbufs, pixbuf);
560 gtk_text_iter_forward_char (&iter);
562 if (gtk_text_iter_toggles_tag (&iter, NULL))
566 /* We might have moved too far */
567 if (gtk_text_iter_compare (&iter, &context->end) > 0)
570 /* Append the text */
571 tmp_text = gtk_text_iter_get_slice (&old_iter, &iter);
572 escaped_text = g_markup_escape_text (tmp_text, -1);
575 g_string_append (context->text_str, escaped_text);
576 g_free (escaped_text);
578 while (!gtk_text_iter_equal (&iter, &context->end));
580 /* Close any open tags */
581 for (tag_list = active_tags; tag_list; tag_list = tag_list->next)
582 g_string_append (context->text_str, "</apply_tag>");
584 g_slist_free (active_tags);
585 g_string_append (context->text_str, "</text>\n</text_view_markup>\n");
589 serialize_pixbufs (SerializationContext *context,
594 for (list = context->pixbufs; list != NULL; list = list->next)
596 GdkPixbuf *pixbuf = list->data;
601 gdk_pixdata_from_pixbuf (&pixdata, pixbuf, FALSE);
602 tmp = gdk_pixdata_serialize (&pixdata, &len);
604 serialize_section_header (text, "GTKTEXTBUFFERPIXBDATA-0001", len);
605 g_string_append_len (text, (gchar *) tmp, len);
611 _gtk_text_buffer_serialize_rich_text (GtkTextBuffer *register_buffer,
612 GtkTextBuffer *content_buffer,
613 const GtkTextIter *start,
614 const GtkTextIter *end,
618 SerializationContext context;
621 context.tags = g_hash_table_new (NULL, NULL);
622 context.text_str = g_string_new (NULL);
623 context.tag_table_str = g_string_new (NULL);
624 context.start = *start;
626 context.n_pixbufs = 0;
627 context.pixbufs = NULL;
629 context.tag_id_tags = g_hash_table_new (NULL, NULL);
631 /* We need to serialize the text before the tag table so we know
632 what tags are used */
633 serialize_text (content_buffer, &context);
634 serialize_tags (&context);
636 text = g_string_new (NULL);
637 serialize_section_header (text, "GTKTEXTBUFFERCONTENTS-0001",
638 context.tag_table_str->len + context.text_str->len);
640 g_string_append_len (text, context.tag_table_str->str, context.tag_table_str->len);
641 g_string_append_len (text, context.text_str->str, context.text_str->len);
643 context.pixbufs = g_list_reverse (context.pixbufs);
644 serialize_pixbufs (&context, text);
646 g_hash_table_destroy (context.tags);
647 g_list_free (context.pixbufs);
648 g_string_free (context.text_str, TRUE);
649 g_string_free (context.tag_table_str, TRUE);
650 g_hash_table_destroy (context.tag_id_tags);
654 return (guint8 *) g_string_free (text, FALSE);
660 STATE_TEXT_VIEW_MARKUP,
688 GtkTextBuffer *buffer;
690 /* Tags that are defined in <tag> elements */
691 GHashTable *defined_tags;
693 /* Tags that are anonymous */
694 GHashTable *anonymous_tags;
696 /* Tag name substitutions */
697 GHashTable *substitutions;
700 GtkTextTag *current_tag;
702 /* Priority of current tag */
703 gint current_tag_prio;
705 /* Id of current tag */
708 /* Tags and their priorities */
709 GList *tag_priorities;
715 gboolean create_tags;
717 gboolean parsed_text;
718 gboolean parsed_tags;
722 set_error (GError **err,
723 GMarkupParseContext *context,
733 g_markup_parse_context_get_position (context, &line, &ch);
735 va_start (args, format);
736 str = g_strdup_vprintf (format, args);
739 g_set_error (err, error_domain, error_code,
740 ("Line %d character %d: %s"),
747 push_state (ParseInfo *info,
750 info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
754 pop_state (ParseInfo *info)
756 g_return_if_fail (info->states != NULL);
758 info->states = g_slist_remove (info->states, info->states->data);
762 peek_state (ParseInfo *info)
764 g_return_val_if_fail (info->states != NULL, STATE_START);
766 return GPOINTER_TO_INT (info->states->data);
769 #define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
773 check_id_or_name (GMarkupParseContext *context,
774 const gchar *element_name,
775 const gchar **attribute_names,
776 const gchar **attribute_values,
781 gboolean has_id = FALSE;
782 gboolean has_name = FALSE;
788 for (i = 0; attribute_names[i] != NULL; i++)
790 if (strcmp (attribute_names[i], "name") == 0)
792 *name = attribute_values[i];
796 set_error (error, context,
798 G_MARKUP_ERROR_PARSE,
799 _("Both \"id\" and \"name\" were found on the <%s> element"),
806 set_error (error, context,
808 G_MARKUP_ERROR_PARSE,
809 _("The attribute \"%s\" was found twice on the <%s> element"),
810 "name", element_name);
816 else if (strcmp (attribute_names[i], "id") == 0)
822 set_error (error, context,
824 G_MARKUP_ERROR_PARSE,
825 _("Both \"id\" and \"name\" were found on the <%s> element"),
832 set_error (error, context,
834 G_MARKUP_ERROR_PARSE,
835 _("The attribute \"%s\" was found twice on the <%s> element"),
842 /* Try parsing the integer */
845 *id = g_ascii_strtoll (attribute_values[i], &tmp, 10);
847 if (errno || tmp == attribute_values[i])
849 set_error (error, context,
850 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
851 _("<%s> element has invalid ID \"%s\""), attribute_values[i]);
857 if (!has_id && !has_name)
859 set_error (error, context,
860 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
861 _("<%s> element has neither a \"name\" nor an \"id\" attribute"), element_name);
875 locate_attributes (GMarkupParseContext *context,
876 const char *element_name,
877 const char **attribute_names,
878 const char **attribute_values,
879 gboolean allow_unknown_attrs,
881 const char *first_attribute_name,
882 const char **first_attribute_retloc,
890 LocateAttr attrs[MAX_ATTRS];
894 g_return_val_if_fail (first_attribute_name != NULL, FALSE);
895 g_return_val_if_fail (first_attribute_retloc != NULL, FALSE);
900 attrs[0].name = first_attribute_name;
901 attrs[0].retloc = first_attribute_retloc;
902 *first_attribute_retloc = NULL;
904 va_start (args, first_attribute_retloc);
906 name = va_arg (args, const char*);
907 retloc = va_arg (args, const char**);
911 g_return_val_if_fail (retloc != NULL, FALSE);
913 g_assert (n_attrs < MAX_ATTRS);
915 attrs[n_attrs].name = name;
916 attrs[n_attrs].retloc = retloc;
920 name = va_arg (args, const char*);
921 retloc = va_arg (args, const char**);
930 while (attribute_names[i])
939 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
941 retloc = attrs[j].retloc;
945 set_error (error, context,
947 G_MARKUP_ERROR_PARSE,
948 _("Attribute \"%s\" repeated twice on the same <%s> element"),
949 attrs[j].name, element_name);
954 *retloc = attribute_values[i];
961 if (!found && !allow_unknown_attrs)
963 set_error (error, context,
965 G_MARKUP_ERROR_PARSE,
966 _("Attribute \"%s\" is invalid on <%s> element in this context"),
967 attribute_names[i], element_name);
980 check_no_attributes (GMarkupParseContext *context,
981 const char *element_name,
982 const char **attribute_names,
983 const char **attribute_values,
986 if (attribute_names[0] != NULL)
988 set_error (error, context,
990 G_MARKUP_ERROR_PARSE,
991 _("Attribute \"%s\" is invalid on <%s> element in this context"),
992 attribute_names[0], element_name);
1000 tag_exists (GMarkupParseContext *context,
1006 GtkTextTagTable *tag_table;
1007 const gchar *real_name;
1009 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1011 if (info->create_tags)
1013 /* If we have an anonymous tag, just return it directly */
1015 return g_hash_table_lookup (info->anonymous_tags,
1016 GINT_TO_POINTER (id));
1018 /* First, try the substitutions */
1019 real_name = g_hash_table_lookup (info->substitutions, name);
1022 return gtk_text_tag_table_lookup (tag_table, real_name);
1024 /* Next, try the list of defined tags */
1025 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1026 return gtk_text_tag_table_lookup (tag_table, name);
1028 set_error (error, context,
1029 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1030 _("Tag \"%s\" has not been defined."), name);
1040 set_error (error, context,
1041 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1042 _("Anonymous tag found and tags can not be created."));
1046 tag = gtk_text_tag_table_lookup (tag_table, name);
1051 set_error (error, context,
1052 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1053 _("Tag \"%s\" does not exist in buffer and tags can not be created."), name);
1067 get_pixbuf_from_headers (GList *headers,
1075 header = g_list_nth_data (headers, id);
1080 if (!gdk_pixdata_deserialize (&pixdata, header->length,
1081 (const guint8 *) header->start, error))
1084 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, TRUE, error);
1090 parse_apply_tag_element (GMarkupParseContext *context,
1091 const gchar *element_name,
1092 const gchar **attribute_names,
1093 const gchar **attribute_values,
1097 const gchar *name, *priority;
1101 g_assert (peek_state (info) == STATE_TEXT ||
1102 peek_state (info) == STATE_APPLY_TAG);
1104 if (ELEMENT_IS ("apply_tag"))
1106 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1107 "priority", &priority, NULL))
1110 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1115 tag = tag_exists (context, name, id, info, error);
1120 info->tag_stack = g_slist_prepend (info->tag_stack, tag);
1122 push_state (info, STATE_APPLY_TAG);
1124 else if (ELEMENT_IS ("pixbuf"))
1129 const gchar *pixbuf_id;
1131 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1132 "index", &pixbuf_id, NULL))
1135 int_id = atoi (pixbuf_id);
1136 pixbuf = get_pixbuf_from_headers (info->headers, int_id, error);
1138 span = g_new0 (TextSpan, 1);
1139 span->pixbuf = pixbuf;
1142 info->spans = g_list_prepend (info->spans, span);
1147 push_state (info, STATE_PIXBUF);
1150 set_error (error, context,
1151 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1152 _("Element <%s> is not allowed below <%s>"),
1153 element_name, peek_state(info) == STATE_TEXT ? "text" : "apply_tag");
1157 parse_attr_element (GMarkupParseContext *context,
1158 const gchar *element_name,
1159 const gchar **attribute_names,
1160 const gchar **attribute_values,
1164 const gchar *name, *type, *value;
1166 GValue gvalue = { 0 };
1169 g_assert (peek_state (info) == STATE_TAG);
1171 if (ELEMENT_IS ("attr"))
1173 if (!locate_attributes (context, element_name, attribute_names, attribute_values, FALSE, error,
1174 "name", &name, "type", &type, "value", &value, NULL))
1177 gtype = g_type_from_name (type);
1179 if (gtype == G_TYPE_INVALID)
1181 set_error (error, context,
1182 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1183 _("\"%s\" is not a valid attribute type"), type);
1187 if (!(pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (info->current_tag), name)))
1189 set_error (error, context,
1190 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1191 _("\"%s\" is not a valid attribute name"), name);
1195 g_value_init (&gvalue, gtype);
1197 if (!deserialize_value (value, &gvalue))
1199 set_error (error, context,
1200 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1201 _("\"%s\" could not be converted to a value of type \"%s\" for attribute \"%s\""),
1206 if (g_param_value_validate (pspec, &gvalue))
1208 set_error (error, context,
1209 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1210 _("\"%s\" is not a valid value for attribute \"%s\""),
1212 g_value_unset (&gvalue);
1216 g_object_set_property (G_OBJECT (info->current_tag),
1219 g_value_unset (&gvalue);
1221 push_state (info, STATE_ATTR);
1225 set_error (error, context,
1226 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1227 _("Element <%s> is not allowed below <%s>"),
1228 element_name, "tag");
1234 get_tag_name (ParseInfo *info,
1235 const gchar *tag_name)
1237 GtkTextTagTable *tag_table;
1241 name = g_strdup (tag_name);
1243 if (!info->create_tags)
1247 tag_table = gtk_text_buffer_get_tag_table (info->buffer);
1249 while (gtk_text_tag_table_lookup (tag_table, name) != NULL)
1252 name = g_strdup_printf ("%s-%d", tag_name, ++i);
1257 g_hash_table_insert (info->substitutions, g_strdup (tag_name), g_strdup (name));
1264 parse_tag_element (GMarkupParseContext *context,
1265 const gchar *element_name,
1266 const gchar **attribute_names,
1267 const gchar **attribute_values,
1271 const gchar *name, *priority;
1277 g_assert (peek_state (info) == STATE_TAGS);
1279 if (ELEMENT_IS ("tag"))
1281 if (!locate_attributes (context, element_name, attribute_names, attribute_values, TRUE, error,
1282 "priority", &priority, NULL))
1285 if (!check_id_or_name (context, element_name, attribute_names, attribute_values,
1291 if (g_hash_table_lookup (info->defined_tags, name) != NULL)
1293 set_error (error, context,
1294 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1295 _("Tag \"%s\" already defined"), name);
1302 prio = g_ascii_strtoll (priority, &tmp, 10);
1304 if (errno || tmp == priority)
1306 set_error (error, context,
1307 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1308 _("Tag \"%s\" has invalid priority \"%s\""), name, priority);
1314 tag_name = get_tag_name (info, name);
1315 info->current_tag = gtk_text_tag_new (tag_name);
1320 info->current_tag = gtk_text_tag_new (NULL);
1321 info->current_tag_id = id;
1324 info->current_tag_prio = prio;
1326 push_state (info, STATE_TAG);
1330 set_error (error, context,
1331 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1332 _("Element <%s> is not allowed below <%s>"),
1333 element_name, "tags");
1338 start_element_handler (GMarkupParseContext *context,
1339 const gchar *element_name,
1340 const gchar **attribute_names,
1341 const gchar **attribute_values,
1345 ParseInfo *info = user_data;
1347 switch (peek_state (info))
1350 if (ELEMENT_IS ("text_view_markup"))
1352 if (!check_no_attributes (context, element_name,
1353 attribute_names, attribute_values, error))
1356 push_state (info, STATE_TEXT_VIEW_MARKUP);
1360 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1361 _("Outermost element in text must be <text_view_markup> not <%s>"),
1364 case STATE_TEXT_VIEW_MARKUP:
1365 if (ELEMENT_IS ("tags"))
1367 if (info->parsed_tags)
1369 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1370 _("A <%s> element has already been specified"), "tags");
1374 if (!check_no_attributes (context, element_name,
1375 attribute_names, attribute_values, error))
1378 push_state (info, STATE_TAGS);
1381 else if (ELEMENT_IS ("text"))
1383 if (info->parsed_text)
1385 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1386 _("A <%s> element has already been specified"), "text");
1389 else if (!info->parsed_tags)
1391 set_error (error, context, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1392 _("A <text> element can't occur before a <tags> element"));
1396 if (!check_no_attributes (context, element_name,
1397 attribute_names, attribute_values, error))
1400 push_state (info, STATE_TEXT);
1404 set_error (error, context,
1405 G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE,
1406 _("Element <%s> is not allowed below <%s>"),
1407 element_name, "text_view_markup");
1410 parse_tag_element (context, element_name,
1411 attribute_names, attribute_values,
1415 parse_attr_element (context, element_name,
1416 attribute_names, attribute_values,
1420 case STATE_APPLY_TAG:
1421 parse_apply_tag_element (context, element_name,
1422 attribute_names, attribute_values,
1426 g_assert_not_reached ();
1432 sort_tag_prio (TextTagPrio *a,
1435 if (a->prio < b->prio)
1437 else if (a->prio > b->prio)
1444 end_element_handler (GMarkupParseContext *context,
1445 const gchar *element_name,
1449 ParseInfo *info = user_data;
1453 switch (peek_state (info))
1457 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1459 info->parsed_tags = TRUE;
1461 /* Sort list and add the tags */
1462 info->tag_priorities = g_list_sort (info->tag_priorities,
1463 (GCompareFunc)sort_tag_prio);
1464 list = info->tag_priorities;
1467 TextTagPrio *prio = list->data;
1469 if (info->create_tags)
1470 gtk_text_tag_table_add (gtk_text_buffer_get_tag_table (info->buffer),
1473 g_object_unref (prio->tag);
1482 g_assert (peek_state (info) == STATE_TAGS);
1484 if (info->current_tag->name)
1486 /* Add tag to defined tags hash */
1487 tmp = g_strdup (info->current_tag->name);
1488 g_hash_table_insert (info->defined_tags,
1493 g_hash_table_insert (info->anonymous_tags,
1494 GINT_TO_POINTER (info->current_tag_id),
1498 if (info->create_tags)
1502 /* add the tag to the list */
1503 prio = g_new0 (TextTagPrio, 1);
1504 prio->prio = info->current_tag_prio;
1505 prio->tag = info->current_tag;
1507 info->tag_priorities = g_list_prepend (info->tag_priorities, prio);
1510 info->current_tag = NULL;
1514 g_assert (peek_state (info) == STATE_TAG);
1516 case STATE_APPLY_TAG:
1518 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1519 peek_state (info) == STATE_TEXT);
1522 info->tag_stack = g_slist_delete_link (info->tag_stack,
1528 g_assert (peek_state (info) == STATE_TEXT_VIEW_MARKUP);
1530 info->spans = g_list_reverse (info->spans);
1531 info->parsed_text = TRUE;
1533 case STATE_TEXT_VIEW_MARKUP:
1535 g_assert (peek_state (info) == STATE_START);
1539 g_assert (peek_state (info) == STATE_APPLY_TAG ||
1540 peek_state (info) == STATE_TEXT);
1543 g_assert_not_reached ();
1549 all_whitespace (const char *text,
1556 end = text + text_len;
1560 if (!g_ascii_isspace (*p))
1563 p = g_utf8_next_char (p);
1570 text_handler (GMarkupParseContext *context,
1576 ParseInfo *info = user_data;
1579 if (all_whitespace (text, text_len) &&
1580 peek_state (info) != STATE_TEXT &&
1581 peek_state (info) != STATE_APPLY_TAG)
1584 switch (peek_state (info))
1587 g_assert_not_reached (); /* gmarkup shouldn't do this */
1590 case STATE_APPLY_TAG:
1594 span = g_new0 (TextSpan, 1);
1595 span->text = g_strndup (text, text_len);
1596 span->tags = g_slist_copy (info->tag_stack);
1598 info->spans = g_list_prepend (info->spans, span);
1601 g_assert_not_reached ();
1607 parse_info_init (ParseInfo *info,
1608 GtkTextBuffer *buffer,
1609 gboolean create_tags,
1612 info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
1614 info->create_tags = create_tags;
1615 info->headers = headers;
1616 info->defined_tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1617 info->substitutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1618 info->anonymous_tags = g_hash_table_new_full (NULL, NULL, NULL, NULL);
1619 info->tag_stack = NULL;
1621 info->parsed_text = FALSE;
1622 info->parsed_tags = FALSE;
1623 info->current_tag = NULL;
1624 info->current_tag_prio = -1;
1625 info->tag_priorities = NULL;
1627 info->buffer = buffer;
1631 text_span_free (TextSpan *span)
1633 g_free (span->text);
1634 g_slist_free (span->tags);
1639 parse_info_free (ParseInfo *info)
1643 g_slist_free (info->tag_stack);
1644 g_slist_free (info->states);
1646 g_hash_table_destroy (info->substitutions);
1647 g_hash_table_destroy (info->defined_tags);
1649 if (info->current_tag)
1650 g_object_unref (info->current_tag);
1655 text_span_free (list->data);
1659 g_list_free (info->spans);
1661 list = info->tag_priorities;
1664 TextTagPrio *prio = list->data;
1667 g_object_unref (prio->tag);
1672 g_list_free (info->tag_priorities);
1677 insert_text (ParseInfo *info,
1680 GtkTextIter start_iter;
1687 mark = gtk_text_buffer_create_mark (info->buffer, "deserialize_insert_point",
1693 TextSpan *span = tmp->data;
1696 gtk_text_buffer_insert (info->buffer, iter, span->text, -1);
1699 gtk_text_buffer_insert_pixbuf (info->buffer, iter, span->pixbuf);
1700 g_object_unref (span->pixbuf);
1702 gtk_text_buffer_get_iter_at_mark (info->buffer, &start_iter, mark);
1708 GtkTextTag *tag = tags->data;
1710 gtk_text_buffer_apply_tag (info->buffer, tag,
1716 gtk_text_buffer_move_mark (info->buffer, mark, iter);
1721 gtk_text_buffer_delete_mark (info->buffer, mark);
1727 read_int (const guchar *start)
1741 header_is (Header *header,
1744 return (strncmp (header->id, id, strlen (id)) == 0);
1748 read_headers (const gchar *start,
1755 GList *headers = NULL;
1762 if (strncmp (start + i, "GTKTEXTBUFFERCONTENTS-0001", 26) == 0 ||
1763 strncmp (start + i, "GTKTEXTBUFFERPIXBDATA-0001", 26) == 0)
1765 section_len = read_int ((const guchar *) start + i + 26);
1767 if (i + 30 + section_len > len)
1770 header = g_new0 (Header, 1);
1771 header->id = start + i;
1772 header->length = section_len;
1773 header->start = start + i + 30;
1775 i += 30 + section_len;
1777 headers = g_list_prepend (headers, header);
1783 return g_list_reverse (headers);
1786 g_list_foreach (headers, (GFunc) g_free, NULL);
1787 g_list_free (headers);
1789 g_set_error_literal (error,
1791 G_MARKUP_ERROR_PARSE,
1792 _("Serialized data is malformed"));
1798 deserialize_text (GtkTextBuffer *buffer,
1802 gboolean create_tags,
1806 GMarkupParseContext *context;
1808 gboolean retval = FALSE;
1810 static const GMarkupParser rich_text_parser = {
1811 start_element_handler,
1812 end_element_handler,
1818 parse_info_init (&info, buffer, create_tags, headers);
1820 context = g_markup_parse_context_new (&rich_text_parser,
1823 if (!g_markup_parse_context_parse (context,
1829 if (!g_markup_parse_context_end_parse (context, error))
1834 /* Now insert the text */
1835 insert_text (&info, iter);
1838 parse_info_free (&info);
1840 g_markup_parse_context_free (context);
1846 _gtk_text_buffer_deserialize_rich_text (GtkTextBuffer *register_buffer,
1847 GtkTextBuffer *content_buffer,
1851 gboolean create_tags,
1859 headers = read_headers ((gchar *) text, length, error);
1864 header = headers->data;
1865 if (!header_is (header, "GTKTEXTBUFFERCONTENTS-0001"))
1867 g_set_error_literal (error,
1869 G_MARKUP_ERROR_PARSE,
1870 _("Serialized data is malformed. First section isn't GTKTEXTBUFFERCONTENTS-0001"));
1876 retval = deserialize_text (content_buffer, iter,
1877 header->start, header->length,
1878 create_tags, error, headers->next);
1881 g_list_foreach (headers, (GFunc)g_free, NULL);
1882 g_list_free (headers);