Merge branch 'fix-textchild-abi' into 'main'

Restore the GtkTextchild abi

Closes #4531

See merge request GNOME/gtk!4236
This commit is contained in:
Matthias Clasen 2021-12-11 16:32:45 +00:00
commit ae92181d02
6 changed files with 293 additions and 275 deletions

View File

@ -2387,7 +2387,7 @@ copy_segment (GString *string,
{
gboolean copy = TRUE;
if (!include_nonchars &&
g_strcmp0 (_gtk_text_unknown_char_utf8, seg->body.child.obj->chars) == 0)
g_strcmp0 (_gtk_text_unknown_char_utf8, gtk_text_child_anchor_get_replacement (seg->body.child.obj)) == 0)
{
copy = FALSE;
}
@ -2400,7 +2400,7 @@ copy_segment (GString *string,
if (copy)
{
g_string_append_len (string,
seg->body.child.obj->chars,
gtk_text_child_anchor_get_replacement (seg->body.child.obj),
seg->byte_count);
}
}
@ -7142,7 +7142,7 @@ _gtk_text_btree_spew_line_short (GtkTextLine *line, int indent)
}
else if (seg->type == &gtk_text_child_type)
{
char *str = g_strndup (seg->body.child.obj->chars, seg->byte_count);
char *str = g_strndup (gtk_text_child_anchor_get_replacement (seg->body.child.obj), seg->byte_count);
printf ("%s child '%s'...\n", spaces, str);
g_free (str);
}
@ -7250,7 +7250,7 @@ _gtk_text_btree_spew_segment (GtkTextBTree* tree, GtkTextLineSegment * seg)
}
else if (seg->type == &gtk_text_child_type)
{
char *str = g_strndup (seg->body.child.obj->chars, seg->byte_count);
char *str = g_strndup (gtk_text_child_anchor_get_replacement (seg->body.child.obj), seg->byte_count);
printf (" '%s'\n", str);
g_free (str);
}

View File

@ -53,6 +53,12 @@
#include "gtktextlayoutprivate.h"
#include "gtkintl.h"
typedef struct {
char *replacement;
} GtkTextChildAnchorPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (GtkTextChildAnchor, gtk_text_child_anchor, G_TYPE_OBJECT)
#define CHECK_IN_BUFFER(anchor) \
G_STMT_START { \
if ((anchor)->segment == NULL) \
@ -291,6 +297,7 @@ _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
GtkTextLineSegment *seg;
GtkTextChildAnchorPrivate *priv = gtk_text_child_anchor_get_instance_private (anchor);
seg = g_slice_alloc (WIDGET_SEG_SIZE);
@ -298,8 +305,8 @@ _gtk_widget_segment_new (GtkTextChildAnchor *anchor)
seg->next = NULL;
seg->byte_count = strlen (anchor->chars);
seg->char_count = g_utf8_strlen (anchor->chars, seg->byte_count);
seg->byte_count = strlen (priv->replacement);
seg->char_count = g_utf8_strlen (priv->replacement, seg->byte_count);
seg->body.child.obj = anchor;
seg->body.child.obj->segment = seg;
@ -373,8 +380,6 @@ _gtk_anchored_child_set_layout (GtkWidget *child,
static void gtk_text_child_anchor_finalize (GObject *obj);
G_DEFINE_TYPE (GtkTextChildAnchor, gtk_text_child_anchor, G_TYPE_OBJECT)
static void
gtk_text_child_anchor_init (GtkTextChildAnchor *child_anchor)
{
@ -422,23 +427,27 @@ gtk_text_child_anchor_new (void)
GtkTextChildAnchor *
gtk_text_child_anchor_new_with_replacement (const char *replacement_character)
{
GtkTextChildAnchor *anchor;
GtkTextChildAnchorPrivate *priv;
/* only a single character can be set as replacement */
g_return_val_if_fail (g_utf8_strlen (replacement_character, -1) == 1, NULL);
GtkTextChildAnchor *anchor = g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR, NULL);
anchor->chars = g_strdup (replacement_character);
anchor = g_object_new (GTK_TYPE_TEXT_CHILD_ANCHOR, NULL);
priv = gtk_text_child_anchor_get_instance_private (anchor);
priv->replacement = g_strdup (replacement_character);
return anchor;
}
static void
gtk_text_child_anchor_finalize (GObject *obj)
{
GtkTextChildAnchor *anchor;
GtkTextLineSegment *seg;
anchor = GTK_TEXT_CHILD_ANCHOR (obj);
seg = anchor->segment;
GtkTextChildAnchor *anchor = GTK_TEXT_CHILD_ANCHOR (obj);
GtkTextChildAnchorPrivate *priv = gtk_text_child_anchor_get_instance_private (anchor);
GtkTextLineSegment *seg = anchor->segment;
if (seg)
{
@ -455,7 +464,7 @@ gtk_text_child_anchor_finalize (GObject *obj)
g_slice_free1 (WIDGET_SEG_SIZE, seg);
}
anchor->segment = NULL;
g_free (priv->replacement);
G_OBJECT_CLASS (gtk_text_child_anchor_parent_class)->finalize (obj);
}
@ -607,3 +616,11 @@ gtk_text_anchored_child_set_layout (GtkWidget *child,
_gtk_anchored_child_set_layout (child, layout);
}
const char *
gtk_text_child_anchor_get_replacement (GtkTextChildAnchor *anchor)
{
GtkTextChildAnchorPrivate *priv = gtk_text_child_anchor_get_instance_private (anchor);
return priv->replacement;
}

View File

@ -60,7 +60,6 @@ struct _GtkTextChildAnchor
/*< private >*/
gpointer segment;
char *chars; /* replacement character */
};
struct _GtkTextChildAnchorClass

View File

@ -85,6 +85,8 @@ void _gtk_widget_segment_unref (GtkTextLineSegment *widget_seg
GtkTextLayout* _gtk_anchored_child_get_layout (GtkWidget *child);
const char * gtk_text_child_anchor_get_replacement (GtkTextChildAnchor *anchor);
G_END_DECLS
#endif

View File

@ -894,7 +894,7 @@ gtk_text_iter_get_char (const GtkTextIter *iter)
}
else if (real->segment->type == &gtk_text_child_type)
{
return g_utf8_get_char (real->segment->body.child.obj->chars);
return g_utf8_get_char (gtk_text_child_anchor_get_replacement (real->segment->body.child.obj));
}
else
{

View File

@ -2470,7 +2470,7 @@ gtk_text_layout_create_display (GtkTextLayout *layout,
size_only, FALSE);
add_child_attrs (layout, display, style,
seg, attrs, layout_byte_offset);
memcpy (text + layout_byte_offset, seg->body.child.obj->chars,
memcpy (text + layout_byte_offset, gtk_text_child_anchor_get_replacement (seg->body.child.obj),
seg->byte_count);
layout_byte_offset += seg->byte_count;
buffer_byte_offset += seg->byte_count;