Title: How do I implement Gtk.Editable in a custom Vala widget without conflicting getters?

I’m writing a custom GTK4 widget in Vala that extends Gtk.Widget and implements Gtk.Editable. The widget contains an internal Gtk.Text instance, and I want to expose its editable functionality through my custom widget.

I’m having trouble implementing the text property and the get_text() method.

If I implement the text property:

public string text {
    get { return text_widget.text; }
    set { text_widget.text = value; }
}

the compiler reports:

`MyProject.TestWidget' does not implement interface method `Gtk.Editable.get_text'

If I add get_text() manually:

public string get_text() {
    return text_widget.get_text();
}

I get a C compilation error:

error: internal: Redefinition of `my_project_test_widget_real_get_text'

If I remove the text property, the compiler instead reports:

`MyProject.TestWidget' does not implement interface property `Gtk.Editable.text'

My questions are:

  1. Why does Vala require both the Gtk.Editable.text property and the Gtk.Editable.get_text() method to be implemented?

  2. How can I implement both without causing a name collision in the generated C code?

  3. Is there a recommended way to delegate the Gtk.Editable interface to an internal Gtk.Text widget, rather than manually forwarding every property and method?

I’m using GTK4 and Vala.

Thanks in advance!

Unfortunately, Gtk.Editable is hard to implement from Vala, as you’ve discovered. I’m not sure how other languages fare here. Some improvements could clearly be made on the Vala side here; but for now it would be easiest for you to implement this one class in C (and then keep using it from Vala). You could even implement a base class in C that you would then derive from in Vala.

From what I remember, try adding

    [CCode (cname = "an alternative function name")]

Otherwise use

String text { get; set; default }

Or something similar with ccode hints, something will work.. eventually

bit tricker that i though - interesting challenge

base.vapi

[CCode (cheader_filename = "base.h", type_id = "mini_get_type ()")]
public class Mini : Gtk.Box, Gtk.Editable {
	public virtual unowned string get_text ();
	public Gtk.Widget view { get; }
	[CCode (has_construct_function = false)]
	public Mini ();
}

base.c - this is class that can be extened and implements the editable interface

#include "base.h"

typedef struct {
	GtkWidget *view;
	char *text;
} MiniPrivate;

static void mini_editable_init (GtkEditableInterface *iface);

G_DEFINE_TYPE_WITH_CODE (Mini, mini, GTK_TYPE_BOX,
	G_ADD_PRIVATE (Mini)
	G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, mini_editable_init))

static void
mini_sync (GtkTextBuffer *buffer, Mini *self)
{
	MiniPrivate *data = mini_get_instance_private (self);
	GtkTextIter start;
	GtkTextIter end;

	gtk_text_buffer_get_bounds (buffer, &start, &end);
	g_free (data->text);
	data->text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
}

static const char *
mini_real_get_text (Mini *self)
{
	return ((MiniPrivate *) mini_get_instance_private (self))->text;
}

static const char *
mini_editable_get_text (GtkEditable *editable)
{
	Mini *self = ATTEMPT_MINI (editable);

	return ATTEMPT_MINI_GET_CLASS (self)->get_text (self);
}

static void
mini_editable_init (GtkEditableInterface *iface)
{
	iface->get_text = mini_editable_get_text;
}

static void
mini_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	if (prop_id == 1) {
		g_value_set_string (value, ATTEMPT_MINI_GET_CLASS (object)->get_text (ATTEMPT_MINI (object)));
		return;
	}
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}

static void
mini_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	MiniPrivate *data;

	if (prop_id == 1) {
		data = mini_get_instance_private (ATTEMPT_MINI (object));
		gtk_text_buffer_set_text (
			gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->view)),
			g_value_get_string (value), -1);
		return;
	}
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}

static void
mini_init (Mini *self)
{
	MiniPrivate *data = mini_get_instance_private (self);

	data->text = g_strdup ("");
	data->view = gtk_text_view_new ();
	gtk_box_append (GTK_BOX (self), data->view);
	g_signal_connect (gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->view)),
		"changed", G_CALLBACK (mini_sync), self);
}

static void
mini_class_init (MiniClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	klass->get_text = mini_real_get_text;
	object_class->get_property = mini_get_property;
	object_class->set_property = mini_set_property;
	gtk_editable_install_properties (object_class, 1);
}

Mini *
mini_new (void)
{
	return g_object_new (MINI_TYPE, NULL);
}

GtkWidget *
mini_get_view (Mini *self)
{
	return ((MiniPrivate *) mini_get_instance_private (self))->view;
}

header file needed

#pragma once
#include <gtk/gtk.h>

#define MINI_TYPE (mini_get_type ())
G_DECLARE_DERIVABLE_TYPE (Mini, mini, ATTEMPT, MINI, GtkBox)

struct _MiniClass {
	GtkBoxClass parent_class;
	const char *(*get_text) (Mini *self);
};

Mini *mini_new (void);
GtkWidget *mini_get_view (Mini *self);

and the vala class extends the base c class

/* Vala subclass of Mini. This class does not implement Gtk.Editable.
 * Overriding get_text is what Gtk.Editable.get_text() returns.
 */
public class Child : Mini {
	private string cached = "from vala";

	public override unowned string get_text () {
		return this.cached;
	}
}

public static int main (string[] args) {
	Gtk.init ();
	stdout.printf ("%s\n", ((Gtk.Editable) new Child ()).get_text ());
	return 0;
}