2.9 <GtkEditable>

Interface: <GtkEditable>

Interface for single-line text editing widgets.

Typical examples of editable widgets are <GtkEntry> and <GtkSpinButton>. 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 Gtk.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.

Properties

Property: editable:cursor-position self

The current position of the insertion cursor in chars.

Its value is a 32-bit integer.

It is read only.

Property: editable:editable self

Whether the entry contents can be edited.

Its value is a boolean.

Set it with (set! (editable:editable self) value).

Property: editable:enable-undo self

If undo/redo should be enabled for the editable.

Its value is a boolean.

Set it with (set! (editable:enable-undo self) value).

Property: editable:max-width-chars self

The desired maximum width of the entry, in characters.

Its value is a 32-bit integer.

Set it with (set! (editable:max-width-chars self) value).

Property: editable:selection-bound self

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

Its value is a 32-bit integer.

It is read only.

Property: editable:text self

The contents of the entry.

Its value is a string, or #f.

Set it with (set! (editable:text self) value).

Property: editable:width-chars self

Number of characters to leave space for in the entry.

Its value is a 32-bit integer.

Set it with (set! (editable:width-chars self) value).

Property: editable:xalign self

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

Reversed for RTL layouts.

Its value is a real number.

Set it with (set! (editable:xalign self) value).

Procedures

Method: editable:delegate-get-accessible-platform-state? self state ⇒ %return
Method: delegate-get-accessible-platform-state? self state ⇒ %return

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:do-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:do-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 text-box role, or you can change your tree to allow this function to work.

self

Passed: a <GtkEditable>.

a <GtkEditable> implementation

state

Passed: a <GtkAccessiblePlatformState>, or one of its symbols.

what kind of accessible state to retrieve

%return

Returned: a boolean.

the accessible platform state of the delegate

Procedure: editable:delegate-get-property? object prop-id value pspec ⇒ %return

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.

object

Passed: a <GObject>.

a <GObject>

prop-id

Passed: an unsigned 32-bit integer.

a property ID

value

Passed: a <GValue>.

value to set

pspec

Passed: a <GParam>.

the <GParam> for the property

%return

Returned: a boolean.

#t if the property was found

Procedure: editable:delegate-set-property? object prop-id value pspec ⇒ %return

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.

object

Passed: a <GObject>.

a <GObject>

prop-id

Passed: an unsigned 32-bit integer.

a property ID

value

Passed: a <GValue>.

value to set

pspec

Passed: a <GParam>.

the <GParam> for the property

%return

Returned: a boolean.

#t if the property was found

Method: editable:delete-selection self
Method: delete-selection self

Deletes the currently selected text of the editable.

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

self

Passed: a <GtkEditable>.

a <GtkEditable>

Method: editable:delete-text self start-pos end-pos
Method: delete-text self start-pos end-pos

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start position

end-pos

Passed: a 32-bit integer.

end position

Method: editable:finish-delegate self
Method: finish-delegate self

Undoes the setup done by editable:init-delegate.

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

self

Passed: a <GtkEditable>.

a <GtkEditable>

Method: editable:get-alignment self ⇒ %return
Method: get-alignment self ⇒ %return

Gets the alignment of the editable.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a real number.

the alignment

Method: editable:get-chars self start-pos end-pos ⇒ %return
Method: get-chars self start-pos end-pos ⇒ %return

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start of text

end-pos

Passed: a 32-bit integer.

end of text

%return

Returned: a string.

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.

Method: editable:get-delegate self ⇒ %return
Method: get-delegate self ⇒ %return

Gets the <GtkEditable> that self is delegating its implementation to.

Typically, the delegate is a <GtkText> widget.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a <GtkEditable>, or #f.

the delegate <GtkEditable>

Method: editable:get-editable? self ⇒ %return
Method: get-editable? self ⇒ %return

Retrieves whether self is editable.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a boolean.

#t if self is editable.

Method: editable:get-enable-undo? self ⇒ %return
Method: get-enable-undo? self ⇒ %return

Gets if undo/redo actions are enabled for self

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a boolean.

#t if undo is enabled

Method: editable:get-max-width-chars self ⇒ %return
Method: get-max-width-chars self ⇒ %return

Retrieves the desired maximum width of self, in characters.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a 32-bit integer.

the maximum width of the entry, in characters

Method: editable:get-position self ⇒ %return
Method: get-position self ⇒ %return

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a 32-bit integer.

the cursor position

Method: editable:get-selection-bounds self ⇒ %return start-pos end-pos
Method: get-selection-bounds self ⇒ %return start-pos end-pos

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 #f will be returned.

Note that positions are specified in characters, not bytes.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a boolean.

#t if there is a non-empty selection, #f otherwise

start-pos

Returned: a 32-bit integer.

location to store the starting position

end-pos

Returned: a 32-bit integer.

location to store the end position

Method: editable:get-text self ⇒ %return
Method: get-text self ⇒ %return

Retrieves the contents of self.

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

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a string.

a pointer to the contents of the editable

Method: editable:get-width-chars self ⇒ %return
Method: get-width-chars self ⇒ %return

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

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a 32-bit integer.

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

Method: editable:init-delegate self
Method: init-delegate self

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

Method: editable:insert-text self text length position ⇒ position
Method: insert-text self text length position ⇒ position

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

text

Passed: a string.

the text to insert

length

Passed: a 32-bit integer.

the length of the text in bytes, or -1

position

Passed, and returned: a 32-bit integer.

location of the position text will be inserted at

Method: editable:select-region self start-pos end-pos
Method: select-region self start-pos end-pos

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start of region

end-pos

Passed: a 32-bit integer.

end of region

Method: editable:set-alignment self xalign
Method: set-alignment self xalign

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

xalign

Passed: a real number.

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

Method: editable:set-editable self is-editable
Method: set-editable self is-editable

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

self

Passed: a <GtkEditable>.

a <GtkEditable>

is-editable

Passed: a boolean.

#t if the user is allowed to edit the text in the widget

Method: editable:set-enable-undo self enable-undo
Method: set-enable-undo self enable-undo

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 text:visibility is set to #f.

self

Passed: a <GtkEditable>.

a <GtkEditable>

enable-undo

Passed: a boolean.

if undo/redo should be enabled

Method: editable:set-max-width-chars self n-chars
Method: set-max-width-chars self n-chars

Sets the desired maximum width in characters of self.

self

Passed: a <GtkEditable>.

a <GtkEditable>

n-chars

Passed: a 32-bit integer.

the new desired maximum width, in characters

Method: editable:set-position self position
Method: set-position self position

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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

position

Passed: a 32-bit integer.

the position of the cursor

Method: editable:set-text self text
Method: set-text self text

Sets the text in the editable to the given value.

This is replacing the current contents.

self

Passed: a <GtkEditable>.

a <GtkEditable>

text

Passed: a string.

the text to set

Method: editable:set-width-chars self n-chars
Method: set-width-chars self n-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.

self

Passed: a <GtkEditable>.

a <GtkEditable>

n-chars

Passed: a 32-bit integer.

width in chars

Signals

Signal: editable:changed

Connect a handler with (connect object editable:changed handler). It is called as (handler self).

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

self

Passed: a <GtkEditable>.

Signal: editable:delete-text

Connect a handler with (connect object editable:delete-text handler). It is called as (handler self start-pos end-pos).

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

self

Passed: a <GtkEditable>.

start-pos

Passed: a 32-bit integer.

the starting position

end-pos

Passed: a 32-bit integer.

the end position

Signal: editable:insert-text

Connect a handler with (connect object editable:insert-text handler). It is called as (handler self text length position) ⇒ position.

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 signal-stop-emission, it is possible to modify the inserted text, or prevent it from being inserted entirely.

self

Passed: a <GtkEditable>.

text

Passed: a string.

the new text to insert

length

Passed: a 32-bit integer.

the length of the new text, in bytes, or -1 if new_text is nul-terminated

position

Passed, and returned: a 32-bit integer.

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.

Virtual methods

Virtual method: editable:do-changed implementor self

To override it in a new class, give register-type #:vfuncs `((changed . ,procedure)). The procedure is called as (procedure self).

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

Virtual method: editable:do-delete-text implementor self start-pos end-pos

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.

To override it in a new class, give register-type #:vfuncs `((delete-text . ,procedure)). The procedure is called as (procedure self start-pos end-pos).

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start position

end-pos

Passed: a 32-bit integer.

end position

Virtual method: editable:do-do-delete-text implementor self start-pos end-pos

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.

To override it in a new class, give register-type #:vfuncs `((do-delete-text . ,procedure)). The procedure is called as (procedure self start-pos end-pos).

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start position

end-pos

Passed: a 32-bit integer.

end position

Virtual method: editable:do-do-insert-text implementor self text length position ⇒ position

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.

To override it in a new class, give register-type #:vfuncs `((do-insert-text . ,procedure)). The procedure is called as (procedure self text length position) ⇒ position.

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

text

Passed: a string.

the text to insert

length

Passed: a 32-bit integer.

the length of the text in bytes, or -1

position

Passed, and returned: a 32-bit integer.

location of the position text will be inserted at

Virtual method: editable:do-get-delegate implementor self ⇒ %return

Gets the <GtkEditable> that self is delegating its implementation to.

Typically, the delegate is a <GtkText> widget.

To override it in a new class, give register-type #:vfuncs `((get-delegate . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a <GtkEditable>, or #f.

the delegate <GtkEditable>

Virtual method: editable:do-get-selection-bounds implementor self ⇒ %return start-pos end-pos

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 #f will be returned.

Note that positions are specified in characters, not bytes.

To override it in a new class, give register-type #:vfuncs `((get-selection-bounds . ,procedure)). The procedure is called as (procedure self) ⇒ %return start-pos end-pos.

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a boolean.

#t if there is a non-empty selection, #f otherwise

start-pos

Returned: a 32-bit integer.

location to store the starting position

end-pos

Returned: a 32-bit integer.

location to store the end position

Virtual method: editable:do-get-text implementor self ⇒ %return

Retrieves the contents of self.

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

To override it in a new class, give register-type #:vfuncs `((get-text . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

%return

Returned: a string.

a pointer to the contents of the editable

Virtual method: editable:do-insert-text implementor self text length position ⇒ position

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.

To override it in a new class, give register-type #:vfuncs `((insert-text . ,procedure)). The procedure is called as (procedure self text length position) ⇒ position.

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

text

Passed: a string.

the text to insert

length

Passed: a 32-bit integer.

the length of the text in bytes, or -1

position

Passed, and returned: a 32-bit integer.

location of the position text will be inserted at

Virtual method: editable:do-set-selection-bounds implementor self start-pos end-pos

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.

To override it in a new class, give register-type #:vfuncs `((set-selection-bounds . ,procedure)). The procedure is called as (procedure self start-pos end-pos).

implementor

Passed: the class whose implementation to call: a class that implements <GtkEditable>, or its GType number.

self

Passed: a <GtkEditable>.

a <GtkEditable>

start-pos

Passed: a 32-bit integer.

start of region

end-pos

Passed: a 32-bit integer.

end of region