Skip to content

Gtk.Editable

interface

Interface for single-line 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 Editable.insert-text, an application can convert all entry into a widget into uppercase.

Forcing entry to uppercase.

#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 GtkEditable on your own widget is that you will embed a GtkText inside a complex widget, and want to delegate the editable functionality to that text widget. GtkEditable provides some utility functions to make this easy.

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

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 GtkEditable interface, provide an implementation for the get_delegate vfunc that returns your text widget:

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 Editable.init_delegate:

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 Editable.finish_delegate before destroying your text widget:

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

  ...
  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 GtkEditable that uses a delegate, the low level Editable.insert-text and 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 Editable.insert-text and Editable.delete-text signals, you will need to connect to them on the delegate obtained via Editable.get_delegate.

Methods

delegate_get_accessible_platform_state

def delegate_get_accessible_platform_state(self, state: AccessiblePlatformState | int) -> bool

Retrieves the accessible platform state from the editable delegate.

This is an helper function to retrieve the accessible state for GtkEditable interface implementations using a delegate pattern.

You should call this function in your editable widget implementation of the Accessible.get_platform_state virtual function, for instance:

static void
accessible_interface_init (GtkAccessibleInterface *iface)
{
  iface->get_platform_state = your_editable_get_accessible_platform_state;
}

static gboolean
your_editable_get_accessible_platform_state (GtkAccessible *accessible,
                                             GtkAccessiblePlatformState state)
{
  return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (accessible), state);
}

Note that the widget which is the delegate must be a direct child of this widget, otherwise your implementation of Accessible.get_platform_state might not even be called, as the platform change will originate from the parent of the delegate, and, as a result, will not work properly.

So, if you can't ensure the direct child condition, you should give the delegate the AccessibleRole.TEXT_BOX role, or you can change your tree to allow this function to work.

Parameters:

  • state — what kind of accessible state to retrieve

delete_selection

def delete_selection(self) -> None

Deletes the currently selected text of the editable.

This call doesn’t do anything if there is no selected text.

delete_text

def delete_text(self, start_pos: int, end_pos: int) -> None

Deletes a sequence of characters.

The characters that are deleted are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters deleted are those from start_pos to the end of the text.

Note that the positions are specified in characters, not bytes.

Parameters:

  • start_pos — start position
  • end_pos — end position

finish_delegate

def finish_delegate(self) -> None

Undoes the setup done by Editable.init_delegate.

This is a helper function that should be called from dispose, before removing the delegate object.

get_alignment

def get_alignment(self) -> float

Gets the alignment of the editable.

get_chars

def get_chars(self, start_pos: int, end_pos: int) -> str

Retrieves a sequence of characters.

The characters that are retrieved are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters retrieved are those characters from start_pos to the end of the text.

Note that positions are specified in characters, not bytes.

Parameters:

  • start_pos — start of text
  • end_pos — end of text

get_delegate

def get_delegate(self) -> Editable | None

Gets the GtkEditable that editable is delegating its implementation to.

Typically, the delegate is a Text widget.

get_editable

def get_editable(self) -> bool

Retrieves whether editable is editable.

get_enable_undo

def get_enable_undo(self) -> bool

Gets if undo/redo actions are enabled for editable

get_max_width_chars

def get_max_width_chars(self) -> int

Retrieves the desired maximum width of editable, in characters.

get_position

def get_position(self) -> int

Retrieves the current position of the cursor relative to the start of the content of the editable.

Note that this position is in characters, not in bytes.

get_selection_bounds

def get_selection_bounds(self) -> tuple[bool, int, int]

Retrieves the selection bound of the editable.

start_pos will be filled with the start of the selection and end_pos with end. If no text was selected both will be identical and False will be returned.

Note that positions are specified in characters, not bytes.

get_text

def get_text(self) -> str

Retrieves the contents of editable.

The returned string is owned by GTK and must not be modified or freed.

get_width_chars

def get_width_chars(self) -> int

Gets the number of characters of space reserved for the contents of the editable.

init_delegate

def init_delegate(self) -> None

Sets up a delegate for GtkEditable.

This is assuming that the get_delegate vfunc in the GtkEditable interface has been set up for the editable's type.

This is a helper function that should be called in instance init, after creating the delegate object.

insert_text

def insert_text(self, text: str, length: int, position: int) -> int

Inserts length bytes of text into the contents of the widget, at position position.

Note that the position is in characters, not in bytes. The function updates position to point after the newly inserted text.

Parameters:

  • text — the text to insert
  • length — the length of the text in bytes, or -1
  • position — location of the position text will be inserted at

select_region

def select_region(self, start_pos: int, end_pos: int) -> None

Selects a region of text.

The characters that are selected are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters selected are those characters from start_pos to the end of the text.

Note that positions are specified in characters, not bytes.

Parameters:

  • start_pos — start of region
  • end_pos — end of region

set_alignment

def set_alignment(self, xalign: float) -> None

Sets the alignment for the contents of the editable.

This controls the horizontal positioning of the contents when the displayed text is shorter than the width of the editable.

Parameters:

  • xalign — The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts

set_editable

def set_editable(self, is_editable: bool) -> None

Determines if the user can edit the text in the editable widget.

Parameters:

  • is_editableTrue if the user is allowed to edit the text in the widget

set_enable_undo

def set_enable_undo(self, enable_undo: bool) -> None

If enabled, changes to editable will be saved for undo/redo actions.

This results in an additional copy of text changes and are not stored in secure memory. As such, undo is forcefully disabled when Text.visibility is set to False.

Parameters:

  • enable_undo — if undo/redo should be enabled

set_max_width_chars

def set_max_width_chars(self, n_chars: int) -> None

Sets the desired maximum width in characters of editable.

Parameters:

  • n_chars — the new desired maximum width, in characters

set_position

def set_position(self, position: int) -> None

Sets the cursor position in the editable to the given value.

The cursor is displayed before the character with the given (base 0) index in the contents of the editable. The value must be less than or equal to the number of characters in the editable. A value of -1 indicates that the position should be set after the last character of the editable. Note that position is in characters, not in bytes.

Parameters:

  • position — the position of the cursor

set_text

def set_text(self, text: str) -> None

Sets the text in the editable to the given value.

This is replacing the current contents.

Parameters:

  • text — the text to set

set_width_chars

def set_width_chars(self, n_chars: int) -> None

Changes the size request of the editable to be about the right size for n_chars characters.

Note that it changes the size request, the size can still be affected by how you pack the widget into containers. If n_chars is -1, the size reverts to the default size.

Parameters:

  • n_chars — width in chars

Static functions

delegate_get_property

@staticmethod
def delegate_get_property(object: GObject.Object, prop_id: int, value: GObject.Value, pspec: GObject.ParamSpec) -> bool

Gets a property of the GtkEditable delegate for object.

This is helper function that should be called in the get_property function of your GtkEditable implementation, before handling your own properties.

Parameters:

  • object — a GObject
  • prop_id — a property ID
  • value — value to set
  • pspec — the GParamSpec for the property

delegate_set_property

@staticmethod
def delegate_set_property(object: GObject.Object, prop_id: int, value: GObject.Value, pspec: GObject.ParamSpec) -> bool

Sets a property on the GtkEditable delegate for object.

This is a helper function that should be called in the set_property function of your GtkEditable implementation, before handling your own properties.

Parameters:

  • object — a GObject
  • prop_id — a property ID
  • value — value to set
  • pspec — the GParamSpec for the property

install_properties

@staticmethod
def install_properties(object_class: GObject.ObjectClass, first_prop: int) -> int

Overrides the GtkEditable properties for class.

This is a helper function that should be called in class_init, after installing your own properties.

Note that your class must have "text", "cursor-position", "selection-bound", "editable", "width-chars", "max-width-chars", "xalign" and "enable-undo" properties for this function to work.

To handle the properties in your set_property and get_property functions, you can either use Editable.delegate_set_property and Editable.delegate_get_property (if you are using a delegate), or remember the first_prop offset and add it to the values in the EditableProperties enumeration to get the property IDs for these properties.

Parameters:

  • object_class — a GObjectClass
  • first_prop — property ID to use for the first property

Virtual methods

do_changed

def do_changed(self) -> None

do_delete_text

def do_delete_text(self, start_pos: int, end_pos: int) -> None

Deletes a sequence of characters.

The characters that are deleted are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters deleted are those from start_pos to the end of the text.

Note that the positions are specified in characters, not bytes.

Parameters:

  • start_pos — start position
  • end_pos — end position

do_do_delete_text

def do_do_delete_text(self, start_pos: int, end_pos: int) -> None

Deletes a sequence of characters.

The characters that are deleted are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters deleted are those from start_pos to the end of the text.

Note that the positions are specified in characters, not bytes.

Parameters:

  • start_pos — start position
  • end_pos — end position

do_do_insert_text

def do_do_insert_text(self, text: str, length: int, position: int) -> int

Inserts length bytes of text into the contents of the widget, at position position.

Note that the position is in characters, not in bytes. The function updates position to point after the newly inserted text.

Parameters:

  • text — the text to insert
  • length — the length of the text in bytes, or -1
  • position — location of the position text will be inserted at

do_get_delegate

def do_get_delegate(self) -> Editable | None

Gets the GtkEditable that editable is delegating its implementation to.

Typically, the delegate is a Text widget.

do_get_selection_bounds

def do_get_selection_bounds(self) -> tuple[bool, int, int]

Retrieves the selection bound of the editable.

start_pos will be filled with the start of the selection and end_pos with end. If no text was selected both will be identical and False will be returned.

Note that positions are specified in characters, not bytes.

do_get_text

def do_get_text(self) -> str

Retrieves the contents of editable.

The returned string is owned by GTK and must not be modified or freed.

do_insert_text

def do_insert_text(self, text: str, length: int, position: int) -> int

Inserts length bytes of text into the contents of the widget, at position position.

Note that the position is in characters, not in bytes. The function updates position to point after the newly inserted text.

Parameters:

  • text — the text to insert
  • length — the length of the text in bytes, or -1
  • position — location of the position text will be inserted at

do_set_selection_bounds

def do_set_selection_bounds(self, start_pos: int, end_pos: int) -> None

Selects a region of text.

The characters that are selected are those characters at positions from start_pos up to, but not including end_pos. If end_pos is negative, then the characters selected are those characters from start_pos to the end of the text.

Note that positions are specified in characters, not bytes.

Parameters:

  • start_pos — start of region
  • end_pos — end of region

Properties

cursor_position

cursor_position: int  # read-only

The current position of the insertion cursor in chars.

editable

editable: bool  # read/write

Whether the entry contents can be edited.

enable_undo

enable_undo: bool  # read/write

If undo/redo should be enabled for the editable.

max_width_chars

max_width_chars: int  # read/write

The desired maximum width of the entry, in characters.

selection_bound

selection_bound: int  # read-only

The position of the opposite end of the selection from the cursor in chars.

text

text: str  # read/write

The contents of the entry.

width_chars

width_chars: int  # read/write

Number of characters to leave space for in the entry.

xalign

xalign: float  # read/write

The horizontal alignment, from 0 (left) to 1 (right).

Reversed for RTL layouts.

Signals

changed

def on_changed(self) -> None: ...

Emitted at the end of a single user-visible operation on the contents.

E.g., a paste operation that replaces the contents of the selection will cause only one signal emission (even though it is implemented by first deleting the selection, then inserting the new content, and may cause multiple ::notify::text signals to be emitted).

delete-text

def on_delete_text(self, start_pos: int, end_pos: int) -> None: ...

Emitted when text is deleted from the widget by the user.

The default handler for this signal will normally be responsible for deleting the text, so by connecting to this signal and then stopping the signal with GObject.signal_stop_emission, it is possible to modify the range of deleted text, or prevent it from being deleted entirely.

The start_pos and end_pos parameters are interpreted as for Editable.delete_text.

insert-text

def on_insert_text(self, text: str, length: int, position: int) -> None: ...

Emitted when text is inserted into the widget by the user.

The default handler for this signal will normally be responsible for inserting the text, so by connecting to this signal and then stopping the signal with GObject.signal_stop_emission, it is possible to modify the inserted text, or prevent it from being inserted entirely.