module Gtk::Editable

Overview

Gtk::Editable is an interface for text editing widgets.

Typical examples of editable widgets are Gtk::Entry and Gtk::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@Gtk.Editable::insert-text], an application can convert all entry into a widget into uppercase.

Forcing entry to uppercase.

WARNING ⚠️ The following code is in c ⚠️

#include <ctype.h>

void
insert_text_handler (Gtk::Editable *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 Gtk::Editable

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

In your class_init function, call Gtk::Editable#install_properties, passing the first available property ID:

WARNING ⚠️ 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 Gtk::Editable interface, provide an implementation for the get_delegate vfunc that returns your text widget:

WARNING ⚠️ The following code is in c ⚠️

Gtk::Editable *
get_editable_delegate (Gtk::Editable *editable)
{
  return GTK_EDITABLE (MY_WIDGET (editable)->text_widget);
}

static void
my_editable_init (Gtk::EditableInterface *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 Gtk::EditableInterface.get_delegate() vfunc returns.

In your instance_init function, create your text widget, and then call Gtk::Editable#init_delegate:

WARNING ⚠️ 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 Gtk::Editable#finish_delegate before destroying your text widget:

WARNING ⚠️ 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 Gtk::Editable#delegate_set_property in your set_property function (and similar for get_property), to set the editable properties:

WARNING ⚠️ 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 Gtk::Editable that uses a delegate, the low level [signal@Gtk.Editable::insert-text] and [signal@Gtk.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@Gtk.Editable::insert-text] and [signal@Gtk.Editable::delete-text] signals, you will need to connect to them on the delegate obtained via Gtk::Editable#delegate.

Direct including types

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/editable.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.cast(obj : GObject::Object) : self #

Cast a GObject::Object to self, throws a TypeCastError if the cast can't be made.


[View source]

Class Method Detail

def self.cast?(obj : GObject::Object) : self | Nil #

[View source]
def self.delegate_get_property(object : GObject::Object, prop_id : UInt32, value : _, pspec : GObject::ParamSpec) : Bool #

[View source]
def self.delegate_set_property(object : GObject::Object, prop_id : UInt32, value : _, pspec : GObject::ParamSpec) : Bool #

[View source]
def self.g_type : UInt64 #

[View source]
def self.install_properties(object_class : GObject::ObjectClass, first_prop : UInt32) : UInt32 #

[View source]

Instance Method Detail

def alignment : Float32 #

[View source]
def alignment=(xalign : Float32) : Nil #

[View source]
def changed_signal #

[View source]
def chars(start_pos : Int32, end_pos : Int32) : String #

[View source]
def cursor_position : Int32 #

[View source]
def delegate : Gtk::Editable | Nil #

[View source]
def delete_selection : Nil #

[View source]
def delete_text(start_pos : Int32, end_pos : Int32) : Nil #

[View source]
def delete_text_signal #

[View source]
def editable : Bool #

[View source]
def editable=(is_editable : Bool) : Nil #

[View source]
def editable? : Bool #

[View source]
def enable_undo : Bool #

[View source]
def enable_undo=(enable_undo : Bool) : Nil #

[View source]
def enable_undo? : Bool #

[View source]
def finish_delegate : Nil #

[View source]
def init_delegate : Nil #

[View source]
def insert_text(text : String, length : Int32, position : Int32) : Nil #

[View source]
def insert_text_signal #

[View source]
def max_width_chars : Int32 #

[View source]
def max_width_chars=(n_chars : Int32) : Nil #

[View source]
def position : Int32 #

[View source]
def position=(position : Int32) : Nil #

[View source]
def select_region(start_pos : Int32, end_pos : Int32) : Nil #

[View source]
def selection_bound : Int32 #

[View source]
def selection_bounds : Bool #

[View source]
def text : String #

[View source]
def text=(text : String) : Nil #

[View source]
abstract def to_unsafe #

[View source]
def width_chars : Int32 #

[View source]
def width_chars=(n_chars : Int32) : Nil #

[View source]
def xalign : Float32 #

[View source]
def xalign=(value : Float32) : Float32 #

[View source]