gtk.editable

Module for [Editable] interface

Types 2

interfaceEditable

[gtk.editable.Editable] is an interface for text editing widgets.

Typical examples of editable widgets are [gtk.entry.Entry] and [gtk.spin_button.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.

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

In your class_init function, call [gtk.editable.Editable.installProperties], 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 [gtk.editable.Editable] 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 [gtk.editable.Editable.initDelegate]:

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.Editable.finishDelegate] 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 [gtk.editable.Editable.delegateSetProperty] 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 [gtk.editable.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.Editable.getDelegate].

Methods
int cursorPosition() @propertyGet `cursorPosition` property. Returns: The current position of the insertion cursor in chars.
bool editable() @propertyGet `editable` property. Returns: Whether the entry contents can be edited.
void editable(bool propval) @propertySet `editable` property. Params: propval = Whether the entry contents can be edited.
bool enableUndo() @propertyGet `enableUndo` property. Returns: If undo/redo should be enabled for the editable.
void enableUndo(bool propval) @propertySet `enableUndo` property. Params: propval = If undo/redo should be enabled for the editable.
int maxWidthChars() @propertyGet `maxWidthChars` property. Returns: The desired maximum width of the entry, in characters.
void maxWidthChars(int propval) @propertySet `maxWidthChars` property. Params: propval = The desired maximum width of the entry, in characters.
int selectionBound() @propertyGet `selectionBound` property. Returns: The position of the opposite end of the selection from the cursor in chars.
string text() @propertyGet `text` property. Returns: The contents of the entry.
void text(string propval) @propertySet `text` property. Params: propval = The contents of the entry.
int widthChars() @propertyGet `widthChars` property. Returns: Number of characters to leave space for in the entry.
void widthChars(int propval) @propertySet `widthChars` property. Params: propval = Number of characters to leave space for in the entry.
float xalign() @propertyGet `xalign` property. Returns: The horizontal alignment, from 0 (left) to 1 (right).
void xalign(float propval) @propertySet `xalign` property. Params: propval = The horizontal alignment, from 0 (left) to 1 (right).
bool delegateGetProperty(gobject.object.ObjectWrap object, uint propId, gobject.value.Value value, gobject.param_spec.ParamSpec pspec)Gets a property of the [gtk.editable.Editable] delegate for object.
bool delegateSetProperty(gobject.object.ObjectWrap object, uint propId, gobject.value.Value value, gobject.param_spec.ParamSpec pspec)Sets a property on the [gtk.editable.Editable] delegate for object.
bool delegateGetAccessiblePlatformState(gtk.types.AccessiblePlatformState state)Retrieves the accessible platform state from the editable delegate.
void deleteSelection()Deletes the currently selected text of the editable.
void deleteText(int startPos, int endPos)Deletes a sequence of characters.
void finishDelegate()Undoes the setup done by [gtk.editable.Editable.initDelegate].
float getAlignment()Gets the alignment of the editable. Returns: the alignment
string getChars(int startPos, int endPos)Retrieves a sequence of characters.
gtk.editable.Editable getDelegate()Gets the [gtk.editable.Editable] that editable is delegating its implementation to.
bool getEditable()Retrieves whether editable is editable. Returns: true if editable is editable.
bool getEnableUndo()Gets if undo/redo actions are enabled for editable Returns: true if undo is enabled
int getMaxWidthChars()Retrieves the desired maximum width of editable, in characters. Returns: the maximum width of the entry, in characters
int getPosition()Retrieves the current position of the cursor relative to the start of the content of the editable.
bool getSelectionBounds(out int startPos, out int endPos)Retrieves the selection bound of the editable.
string getText()Retrieves the contents of editable.
int getWidthChars()Gets the number of characters of space reserved for the contents of the editable. Returns: number of chars to request space for, or negative if unset
void initDelegate()Sets up a delegate for [gtk.editable.Editable].
void insertText(string text, ref int position)Inserts length bytes of text into the contents of the widget, at position position.
void selectRegion(int startPos, int endPos)Selects a region of text.
void setAlignment(float xalign)Sets the alignment for the contents of the editable.
void setEditable(bool isEditable)Determines if the user can edit the text in the editable widget.
void setEnableUndo(bool enableUndo)If enabled, changes to editable will be saved for undo/redo actions.
void setMaxWidthChars(int nChars)Sets the desired maximum width in characters of editable.
void setPosition(int position)Sets the cursor position in the editable to the given value.
void setText(string text)Sets the text in the editable to the given value.
void setWidthChars(int nChars)Changes the size request of the editable to be about the right size for n_chars characters.
gulong connectChanged(T)(T callback, Flag!"After" after = No.After)Connect to `Changed` signal.
gulong connectDeleteText(T)(T callback, Flag!"After" after = No.After)Connect to `DeleteText` signal.
gulong connectInsertText(T)(T callback, Flag!"After" after = No.After)Connect to `InsertText` signal.
Methods
T editable(bool propval)Set `editable` property. Params: propval = Whether the entry contents can be edited. Returns: Builder instance for fluent chaining
T enableUndo(bool propval)Set `enableUndo` property. Params: propval = If undo/redo should be enabled for the editable. Returns: Builder instance for fluent chaining
T maxWidthChars(int propval)Set `maxWidthChars` property. Params: propval = The desired maximum width of the entry, in characters. Returns: Builder instance for fluent chaining
T text(string propval)Set `text` property. Params: propval = The contents of the entry. Returns: Builder instance for fluent chaining
T widthChars(int propval)Set `widthChars` property. Params: propval = Number of characters to leave space for in the entry. Returns: Builder instance for fluent chaining
T xalign(float propval)Set `xalign` property. Params: propval = The horizontal alignment, from 0 (left) to 1 (right).