Gtk.Editable

g GObject.GInterface GObject.GInterface Gtk.Editable Gtk.Editable GObject.GInterface->Gtk.Editable

Implementations:

Gtk.EditableLabel, Gtk.Entry, Gtk.PasswordEntry, Gtk.SearchEntry, Gtk.SpinButton, Gtk.Text

Methods

class

delegate_get_property (object, prop_id, value, pspec)

class

delegate_set_property (object, prop_id, value, pspec)

class

install_properties (object_class, first_prop)

delegate_get_accessible_platform_state (state)

delete_selection ()

delete_text (start_pos, end_pos)

finish_delegate ()

get_alignment ()

get_chars (start_pos, end_pos)

get_delegate ()

get_editable ()

get_enable_undo ()

get_max_width_chars ()

get_position ()

get_selection_bounds ()

get_text ()

get_width_chars ()

init_delegate ()

insert_text (self, text, position)

select_region (start_pos, end_pos)

set_alignment (xalign)

set_editable (is_editable)

set_enable_undo (enable_undo)

set_max_width_chars (n_chars)

set_position (position)

set_text (text)

set_width_chars (n_chars)

Virtual Methods

do_changed ()

do_delete_text (start_pos, end_pos)

do_do_delete_text (start_pos, end_pos)

do_do_insert_text (text, length, position)

do_get_delegate ()

do_get_selection_bounds ()

do_get_text ()

do_insert_text (text, length, position)

do_set_selection_bounds (start_pos, end_pos)

Properties

Name

Type

Flags

Short Description

cursor-position

int

r

editable

bool

r/w/en

enable-undo

bool

r/w/en

max-width-chars

int

r/w/en

selection-bound

int

r

text

str

r/w/en

width-chars

int

r/w/en

xalign

float

r/w/en

Signals

Name

Short Description

changed

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

delete-text

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

insert-text

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

Fields

None

Class Details

class Gtk.Editable
Bases:

GObject.GInterface

Structure:

Gtk.EditableInterface

GtkEditable is an interface for text editing widgets.

Typical examples of editable widgets are [class`Gtk`.Entry] and [class`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.

```c #include <ctype.h>

void insert_text_handler (Gtk.Editable *editable, const str *text, int length, int *position, object data) { str *result = GLib.utf8_strup (text, length);

g_signal_handlers_block_by_func (editable, (object) insert_text_handler, data); Gtk.Editable.insert_text (editable, result, length, position); g_signal_handlers_unblock_by_func (editable, (object) insert_text_handler, data);

GObject.signal_stop_emission_by_name (editable, “insert_text”);

GLib.free (result); } ```

Implementing Gtk.Editable

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 [func`Gtk`.Editable.install_properties], passing the first available property ID:

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

```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.Editable.do_get_delegate() vfunc returns.

In your instance_init function, create your text widget, and then call [method`Gtk`.Editable.init_delegate]:

``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 [method`Gtk`.Editable.finish_delegate] before destroying your text widget:

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

```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 GtkEditable 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 [method`Gtk`.Editable.get_delegate].

classmethod delegate_get_property(object, prop_id, value, pspec)[source]
Parameters:
Returns:

True if the property was found

Return type:

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.

classmethod delegate_set_property(object, prop_id, value, pspec)[source]
Parameters:
Returns:

True if the property was found

Return type:

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.

classmethod install_properties(object_class, first_prop)[source]
Parameters:
  • object_class (GObject.ObjectClass) – a GObjectClass

  • first_prop (int) – property ID to use for the first property

Returns:

the number of properties that were installed

Return type:

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 [func`Gtk`.Editable.delegate_set_property] and [func`Gtk`.Editable.delegate_get_property] (if you are using a delegate), or remember the first_prop offset and add it to the values in the [enum`Gtk`.EditableProperties] enumeration to get the property IDs for these properties.

delegate_get_accessible_platform_state(state)[source]
Parameters:

state (Gtk.AccessiblePlatformState) – what kind of accessible state to retrieve

Return type:

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 [vfunc`Gtk`.Accessible.get_platform_state] virtual function, for instance:

```c static void accessible_interface_init (Gtk.AccessibleInterface *iface) { iface->get_platform_state = your_editable_get_accessible_platform_state; }

static bool your_editable_get_accessible_platform_state (Gtk.Accessible *accessible, Gtk.AccessiblePlatformState state) { return Gtk.Editable.delegate_get_accessible_platform_state (GTK_EDITABLE (accessible), state); } ```

New in version 4.10.

delete_selection()[source]

Deletes the currently selected text of the editable.

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

delete_text(start_pos, end_pos)[source]
Parameters:
  • start_pos (int) – start position

  • end_pos (int) – end position

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.

finish_delegate()[source]

Undoes the setup done by [method`Gtk`.Editable.init_delegate].

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

get_alignment()[source]
Returns:

the alignment

Return type:

float

Gets the alignment of the editable.

get_chars(start_pos, end_pos)[source]
Parameters:
  • start_pos (int) – start of text

  • end_pos (int) – end of text

Returns:

a pointer to the contents of the widget as a string. This string is allocated by the GtkEditable implementation and should be freed by the caller.

Return type:

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.

get_delegate()[source]
Returns:

the delegate GtkEditable

Return type:

Gtk.Editable or None

Gets the GtkEditable that self is delegating its implementation to.

Typically, the delegate is a [class`Gtk`.Text] widget.

get_editable()[source]
Returns:

True if self is editable.

Return type:

bool

Retrieves whether self is editable.

get_enable_undo()[source]
Returns:

True if undo is enabled

Return type:

bool

Gets if undo/redo actions are enabled for self

get_max_width_chars()[source]
Returns:

the maximum width of the entry, in characters

Return type:

int

Retrieves the desired maximum width of self, in characters.

get_position()[source]
Returns:

the cursor position

Return type:

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()[source]
Returns:

An empty tuple if no area is selected or a tuple containing:

start_pos:

the starting position

end_pos:

the end position

Return type:

(start_pos: int, end_pos: int) or ()

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 an empty tuple will be returned.

Note that positions are specified in characters, not bytes.

get_text()[source]
Returns:

a pointer to the contents of the editable

Return type:

str

Retrieves the contents of self.

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

get_width_chars()[source]
Returns:

number of chars to request space for, or negative if unset

Return type:

int

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

init_delegate()[source]

Sets up a delegate for GtkEditable.

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

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

insert_text(self, text, position)[source]
Parameters:
  • new_text (str) – the text to append

  • position (int) – location of the position text will be inserted at

Returns:

location of the position text will be inserted at

Return type:

int

Inserts new_text into the contents of the widget, at position position.

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

select_region(start_pos, end_pos)[source]
Parameters:
  • start_pos (int) – start of region

  • end_pos (int) – end of region

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.

set_alignment(xalign)[source]
Parameters:

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

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.

set_editable(is_editable)[source]
Parameters:

is_editable (bool) – True if the user is allowed to edit the text in the widget

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

set_enable_undo(enable_undo)[source]
Parameters:

enable_undo (bool) – if undo/redo should be enabled

If enabled, changes to self 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 [property`Gtk`.Text:visibility] is set to False.

set_max_width_chars(n_chars)[source]
Parameters:

n_chars (int) – the new desired maximum width, in characters

Sets the desired maximum width in characters of self.

set_position(position)[source]
Parameters:

position (int) – the position of the cursor

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.

set_text(text)[source]
Parameters:

text (str) – the text to set

Sets the text in the editable to the given value.

This is replacing the current contents.

set_width_chars(n_chars)[source]
Parameters:

n_chars (int) – width in chars

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.

do_changed() virtual
do_delete_text(start_pos, end_pos) virtual
Parameters:
  • start_pos (int) – start position

  • end_pos (int) – end position

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.

do_do_delete_text(start_pos, end_pos) virtual
Parameters:
  • start_pos (int) – start position

  • end_pos (int) – end position

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.

do_do_insert_text(text, length, position) virtual
Parameters:
  • text (str) – the text to insert

  • length (int) – the length of the text in bytes, or -1

  • position (int) – location of the position text will be inserted at

Returns:

location of the position text will be inserted at

Return type:

position: 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.

do_get_delegate() virtual
Returns:

the delegate GtkEditable

Return type:

Gtk.Editable or None

Gets the GtkEditable that editable is delegating its implementation to.

Typically, the delegate is a [class`Gtk`.Text] widget.

do_get_selection_bounds() virtual
Returns:

True if there is a non-empty selection, False otherwise

start_pos:

location to store the starting position

end_pos:

location to store the end position

Return type:

(bool, start_pos: int, end_pos: 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() virtual
Returns:

a pointer to the contents of the editable

Return type:

str

Retrieves the contents of editable.

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

do_insert_text(text, length, position) virtual
Parameters:
  • text (str) – the text to insert

  • length (int) – the length of the text in bytes, or -1

  • position (int) – location of the position text will be inserted at

Returns:

location of the position text will be inserted at

Return type:

position: 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.

do_set_selection_bounds(start_pos, end_pos) virtual
Parameters:
  • start_pos (int) – start of region

  • end_pos (int) – end of region

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.

Signal Details

Gtk.Editable.signals.changed(editable)
Signal Name:

changed

Flags:

RUN_LAST

Parameters:

editable (Gtk.Editable) – The object which received the signal

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).

Gtk.Editable.signals.delete_text(editable, start_pos, end_pos)
Signal Name:

delete-text

Flags:

RUN_LAST

Parameters:
  • editable (Gtk.Editable) – The object which received the signal

  • start_pos (int) – the starting position

  • end_pos (int) – the end position

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 [method`Gtk`.Editable.delete_text].

Gtk.Editable.signals.insert_text(editable, text, length, position)
Signal Name:

insert-text

Flags:

RUN_LAST

Parameters:
  • editable (Gtk.Editable) – The object which received the signal

  • text (str) – the new text to insert

  • length (int) – the length of the new text, in bytes, or -1 if new_text is nul-terminated

  • position (int) – the position, in characters, at which to insert the new text. this is an in-out parameter. After the signal emission is finished, it should point after the newly inserted text.

Returns:

the position, in characters, at which to insert the new text. this is an in-out parameter. After the signal emission is finished, it should point after the newly inserted text.

Return type:

position: int

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.

Property Details

Gtk.Editable.props.cursor_position
Name:

cursor-position

Type:

int

Default Value:

0

Flags:

READABLE

The current position of the insertion cursor in chars.

Gtk.Editable.props.editable
Name:

editable

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the entry contents can be edited.

Gtk.Editable.props.enable_undo
Name:

enable-undo

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

If undo/redo should be enabled for the editable.

Gtk.Editable.props.max_width_chars
Name:

max-width-chars

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The desired maximum width of the entry, in characters.

Gtk.Editable.props.selection_bound
Name:

selection-bound

Type:

int

Default Value:

0

Flags:

READABLE

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

Gtk.Editable.props.text
Name:

text

Type:

str

Default Value:

''

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The contents of the entry.

Gtk.Editable.props.width_chars
Name:

width-chars

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Number of characters to leave space for in the entry.

Gtk.Editable.props.xalign
Name:

xalign

Type:

float

Default Value:

0.0

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Reversed for RTL layouts.