Struct gtk4::Editable[][src]

pub struct Editable(_);
Expand description

Editable is an interface for text editing widgets.

Typical examples of editable widgets are Entry and SpinButton. It contains functions for generically manipulating an editable widget, a large number of action signals used for key bindings, and several signals that an application can connect to modify the behavior of a widget.

As an example of the latter usage, by connecting the following handler to signal::Editable::insert-text, an application can convert all entry into a widget into uppercase.

Forcing entry to uppercase.

⚠️ The following code is in c ⚠️

#include <ctype.h>

void
insert_text_handler (GtkEditable *editable,
                     const char  *text,
                     int          length,
                     int         *position,
                     gpointer     data)
{
  char *result = g_utf8_strup (text, length);

  g_signal_handlers_block_by_func (editable,
                               (gpointer) insert_text_handler, data);
  gtk_editable_insert_text (editable, result, length, position);
  g_signal_handlers_unblock_by_func (editable,
                                     (gpointer) insert_text_handler, data);

  g_signal_stop_emission_by_name (editable, "insert_text");

  g_free (result);
}

Implementing GtkEditable

The most likely scenario for implementing Editable on your own widget is that you will embed a Text inside a complex widget, and want to delegate the editable functionality to that text widget. Editable provides some utility functions to make this easy.

In your class_init function, call [install_properties()][Self::install_properties()], passing the first available property ID:

⚠️ The following code is in c ⚠️

static void
my_class_init (MyClass *class)
{
  ...
  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);
  gtk_editable_install_properties (object_clas, NUM_PROPERTIES);
  ...
}

In your interface_init function for the Editable interface, provide an implementation for the get_delegate vfunc that returns your text widget:

⚠️ The following code is in c ⚠️

GtkEditable *
get_editable_delegate (GtkEditable *editable)
{
  return GTK_EDITABLE (MY_WIDGET (editable)->text_widget);
}

static void
my_editable_init (GtkEditableInterface *iface)
{
  iface->get_delegate = get_editable_delegate;
}

You don’t need to provide any other vfuncs. The default implementations work by forwarding to the delegate that the GtkEditableInterface.get_delegate() vfunc returns.

In your instance_init function, create your text widget, and then call EditableExt::init_delegate():

⚠️ The following code is in c ⚠️

static void
my_widget_init (MyWidget *self)
{
  ...
  self->text_widget = gtk_text_new ();
  gtk_editable_init_delegate (GTK_EDITABLE (self));
  ...
}

In your dispose function, call EditableExt::finish_delegate() before destroying your text widget:

⚠️ The following code is in c ⚠️

static void
my_widget_dispose (GObject *object)
{
  ...
  gtk_editable_finish_delegate (GTK_EDITABLE (self));
  g_clear_pointer (&self->text_widget, gtk_widget_unparent);
  ...
}

Finally, use [delegate_set_property()][Self::delegate_set_property()] in your set_property function (and similar for get_property), to set the editable properties:

⚠️ The following code is in c ⚠️

  ...
  if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
    return;

  switch (prop_id)
  ...

It is important to note that if you create a Editable that uses a delegate, the low level signal::Editable::insert-text and signal::Editable::delete-text signals will be propagated from the “wrapper” editable to the delegate, but they will not be propagated from the delegate to the “wrapper” editable, as they would cause an infinite recursion. If you wish to connect to the signal::Editable::insert-text and signal::Editable::delete-text signals, you will need to connect to them on the delegate obtained via EditableExt::delegate().

Implements

EditableExt, WidgetExt, glib::ObjectExt, AccessibleExt, BuildableExt, ConstraintTargetExt, EditableExtManual, WidgetExtManual, AccessibleExtManual

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Override the virtual methods of this interface for the given subclass and do other interface initialization. Read more

Instance specific initialization. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Returns the type identifier of Self.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Upcasts an object to a superclass or interface T. Read more

Upcasts an object to a reference of its superclass or interface T. Read more

Tries to downcast to a subclass or interface implementor T. Read more

Tries to downcast to a reference of its subclass or interface implementor T. Read more

Tries to cast to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more

Tries to cast to reference to an object of type T. This handles upcasting, downcasting and casting between interface and interface implementors. All checks are performed at runtime, while downcast and upcast will do many checks at compile-time already. Read more

Casts to T unconditionally. Read more

Casts to &T unconditionally. Read more

Performs the conversion.

Performs the conversion.

Returns true if the object is an instance of (can be cast to) T.

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Same as connect but takes a SignalId instead of a signal name.

Same as connect_local but takes a SignalId instead of a signal name.

Same as connect_unsafe but takes a SignalId instead of a signal name.

Emit signal by signal id.

Same as emit but takes Value for the arguments.

Emit signal by its name.

Same as emit_by_name but takes Value for the arguments.

Emit signal with details by signal id.

Same as emit_with_details but takes Value for the arguments.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Returns a SendValue clone of self.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.