GtkText

GtkText — A simple single-line text entry field

Properties

gboolean activates-default Read / Write
PangoAttrList * attributes Read / Write
GtkEntryBuffer * buffer Read / Write / Construct
gboolean enable-emoji-completion Read / Write
GMenuModel * extra-menu Read / Write
gchar * im-module Read / Write
GtkInputHints input-hints Read / Write
GtkInputPurpose input-purpose Read / Write
guint invisible-char Read / Write
gboolean invisible-char-set Read / Write
gint max-length Read / Write
gboolean overwrite-mode Read / Write
gchar * placeholder-text Read / Write
gboolean propagate-text-width Read / Write
gint scroll-offset Read
PangoTabArray * tabs Read / Write
gboolean truncate-multiline Read / Write
gboolean visibility Read / Write

Signals

void activate Action
void backspace Action
void copy-clipboard Action
void cut-clipboard Action
void delete-from-cursor Action
void insert-at-cursor Action
void insert-emoji Action
void move-cursor Action
void paste-clipboard Action
void preedit-changed Action
void toggle-overwrite Action

Actions

  menu.popup  
  text.redo  
  text.undo  
  misc.toggle-visibility  
  misc.insert-emoji  
  selection.select-all  
  selection.delete  
  clipboard.paste  
  clipboard.copy  
  clipboard.cut  

Types and Values

struct GtkText
struct GtkTextClass

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkText

Implemented Interfaces

GtkText implements AtkImplementorIface, GtkBuildable, GtkConstraintTarget and GtkEditable.

Includes

#include <gtk/gtk.h>

Description

The GtkText widget is a single line text entry widget.

A fairly large set of key bindings are supported by default. If the entered text is longer than the allocation of the widget, the widget will scroll so that the cursor position is visible.

When using an entry for passwords and other sensitive information, it can be put into “password mode” using gtk_text_set_visibility(). In this mode, entered text is displayed using a “invisible” character. By default, GTK picks the best invisible character that is available in the current font, but it can be changed with gtk_text_set_invisible_char().

If you are looking to add icons or progress display in an entry, look at GtkEntry. There other alternatives for more specialized use cases, such as GtkSearchEntry.

If you need multi-line editable text, look at GtkTextView.

CSS nodes

1
2
3
4
5
6
7
text[.read-only]
├── placeholder
├── undershoot.left
├── undershoot.right
├── [selection]
├── [block-cursor]
╰── [window.popup]

GtkText has a main node with the name text. Depending on the properties of the widget, the .read-only style class may appear.

When the entry has a selection, it adds a subnode with the name selection.

When the entry is in overwrite mode, it adds a subnode with the name block-cursor that determines how the block cursor is drawn.

The CSS node for a context menu is added as a subnode below text as well.

The undershoot nodes are used to draw the underflow indication when content is scrolled out of view. These nodes get the .left and .right style classes added depending on where the indication is drawn.

When touch is used and touch selection handles are shown, they are using CSS nodes with name cursor-handle. They get the .top or .bottom style class depending on where they are shown in relation to the selection. If there is just a single handle for the text cursor, it gets the style class .insertion-cursor.

Functions

gtk_text_new ()

GtkWidget *
gtk_text_new (void);

Creates a new self.

Returns

a new GtkText.


gtk_text_new_with_buffer ()

GtkWidget *
gtk_text_new_with_buffer (GtkEntryBuffer *buffer);

Creates a new self with the specified text buffer.

Parameters

buffer

The buffer to use for the new GtkText.

 

Returns

a new GtkText


gtk_text_set_buffer ()

void
gtk_text_set_buffer (GtkText *self,
                     GtkEntryBuffer *buffer);

Set the GtkEntryBuffer object which holds the text for this widget.

Parameters

self

a GtkText

 

buffer

a GtkEntryBuffer

 

gtk_text_get_buffer ()

GtkEntryBuffer *
gtk_text_get_buffer (GtkText *self);

Get the GtkEntryBuffer object which holds the text for this self.

Parameters

self

a GtkText

 

Returns

A GtkEntryBuffer object.

[transfer none]


gtk_text_set_visibility ()

void
gtk_text_set_visibility (GtkText *self,
                         gboolean visible);

Sets whether the contents of the self are visible or not. When visibility is set to FALSE, characters are displayed as the invisible char, and will also appear that way when the text in the self widget is copied to the clipboard.

By default, GTK picks the best invisible character available in the current font, but it can be changed with gtk_text_set_invisible_char().

Note that you probably want to set “input-purpose” to GTK_INPUT_PURPOSE_PASSWORD or GTK_INPUT_PURPOSE_PIN to inform input methods about the purpose of this self, in addition to setting visibility to FALSE.

Parameters

self

a GtkText

 

visible

TRUE if the contents of the self are displayed as plaintext

 

gtk_text_get_visibility ()

gboolean
gtk_text_get_visibility (GtkText *self);

Retrieves whether the text in self is visible. See gtk_text_set_visibility().

Parameters

self

a GtkText

 

Returns

TRUE if the text is currently visible


gtk_text_set_invisible_char ()

void
gtk_text_set_invisible_char (GtkText *self,
                             gunichar ch);

Sets the character to use in place of the actual text when gtk_text_set_visibility() has been called to set text visibility to FALSE. i.e. this is the character used in “password mode” to show the user how many characters have been typed.

By default, GTK picks the best invisible char available in the current font. If you set the invisible char to 0, then the user will get no feedback at all; there will be no text on the screen as they type.

Parameters

self

a GtkText

 

ch

a Unicode character

 

gtk_text_get_invisible_char ()

gunichar
gtk_text_get_invisible_char (GtkText *self);

Retrieves the character displayed in place of the real characters for entries with visibility set to false. See gtk_text_set_invisible_char().

Parameters

self

a GtkText

 

Returns

the current invisible char, or 0, if the self does not show invisible text at all.


gtk_text_unset_invisible_char ()

void
gtk_text_unset_invisible_char (GtkText *self);

Unsets the invisible char previously set with gtk_text_set_invisible_char(). So that the default invisible char is used again.

Parameters

self

a GtkText

 

gtk_text_set_overwrite_mode ()

void
gtk_text_set_overwrite_mode (GtkText *self,
                             gboolean overwrite);

Sets whether the text is overwritten when typing in the GtkText.

Parameters

self

a GtkText

 

overwrite

new value

 

gtk_text_get_overwrite_mode ()

gboolean
gtk_text_get_overwrite_mode (GtkText *self);

Gets the value set by gtk_text_set_overwrite_mode().

Parameters

self

a GtkText

 

Returns

whether the text is overwritten when typing.


gtk_text_set_max_length ()

void
gtk_text_set_max_length (GtkText *self,
                         int length);

Sets the maximum allowed length of the contents of the widget.

If the current contents are longer than the given length, then they will be truncated to fit.

This is equivalent to getting self 's GtkEntryBuffer and calling gtk_entry_buffer_set_max_length() on it. ]|

Parameters

self

a GtkText

 

length

the maximum length of the self, or 0 for no maximum. (other than the maximum length of entries.) The value passed in will be clamped to the range 0-65536.

 

gtk_text_get_max_length ()

gint
gtk_text_get_max_length (GtkText *self);

Retrieves the maximum allowed length of the text in self . See gtk_text_set_max_length().

This is equivalent to getting self 's GtkEntryBuffer and calling gtk_entry_buffer_get_max_length() on it.

Parameters

self

a GtkText

 

Returns

the maximum allowed number of characters in GtkText, or 0 if there is no maximum.


gtk_text_get_text_length ()

guint16
gtk_text_get_text_length (GtkText *self);

Retrieves the current length of the text in self .

This is equivalent to getting self 's GtkEntryBuffer and calling gtk_entry_buffer_get_length() on it.

Parameters

self

a GtkText

 

Returns

the current number of characters in GtkText, or 0 if there are none.


gtk_text_set_activates_default ()

void
gtk_text_set_activates_default (GtkText *self,
                                gboolean activates);

If activates is TRUE, pressing Enter in the self will activate the default widget for the window containing the self. This usually means that the dialog box containing the self will be closed, since the default widget is usually one of the dialog buttons.

Parameters

self

a GtkText

 

activates

TRUE to activate window’s default widget on Enter keypress

 

gtk_text_get_activates_default ()

gboolean
gtk_text_get_activates_default (GtkText *self);

Retrieves the value set by gtk_text_set_activates_default().

Parameters

self

a GtkText

 

Returns

TRUE if the self will activate the default widget


gtk_text_set_placeholder_text ()

void
gtk_text_set_placeholder_text (GtkText *self,
                               const char *text);

Sets text to be displayed in self when it is empty.

This can be used to give a visual hint of the expected contents of the self.

Parameters

self

a GtkText

 

text

a string to be displayed when self is empty and unfocused, or NULL.

[nullable]

gtk_text_get_placeholder_text ()

const char *
gtk_text_get_placeholder_text (GtkText *self);

Retrieves the text that will be displayed when self is empty and unfocused

Parameters

self

a GtkText

 

Returns

a pointer to the placeholder text as a string. This string points to internally allocated storage in the widget and must not be freed, modified or stored. If no placeholder text has been set, NULL will be returned.

[nullable][transfer none]


gtk_text_set_input_purpose ()

void
gtk_text_set_input_purpose (GtkText *self,
                            GtkInputPurpose purpose);

Sets the “input-purpose” property which can be used by on-screen keyboards and other input methods to adjust their behaviour.

Parameters

self

a GtkText

 

purpose

the purpose

 

gtk_text_get_input_purpose ()

GtkInputPurpose
gtk_text_get_input_purpose (GtkText *self);

Gets the value of the “input-purpose” property.

Parameters

self

a GtkText

 

gtk_text_set_input_hints ()

void
gtk_text_set_input_hints (GtkText *self,
                          GtkInputHints hints);

Sets the “input-hints” property, which allows input methods to fine-tune their behaviour.

Parameters

self

a GtkText

 

hints

the hints

 

gtk_text_get_input_hints ()

GtkInputHints
gtk_text_get_input_hints (GtkText *self);

Gets the value of the “input-hints” property.

Parameters

self

a GtkText

 

gtk_text_set_attributes ()

void
gtk_text_set_attributes (GtkText *self,
                         PangoAttrList *attrs);

Sets a PangoAttrList; the attributes in the list are applied to the text.

Parameters

self

a GtkText

 

attrs

a PangoAttrList or NULL to unset.

[nullable]

gtk_text_get_attributes ()

PangoAttrList *
gtk_text_get_attributes (GtkText *self);

Gets the attribute list that was set on the self using gtk_text_set_attributes(), if any.

Parameters

self

a GtkText

 

Returns

the attribute list, or NULL if none was set.

[transfer none][nullable]


gtk_text_set_tabs ()

void
gtk_text_set_tabs (GtkText *self,
                   PangoTabArray *tabs);

Sets a PangoTabArray; the tabstops in the array are applied to the self text.

Parameters

self

a GtkText

 

tabs

a PangoTabArray.

[nullable]

gtk_text_get_tabs ()

PangoTabArray *
gtk_text_get_tabs (GtkText *self);

Gets the tabstops that were set on the self using gtk_text_set_tabs(), if any.

Parameters

self

a GtkText

 

Returns

the tabstops, or NULL if none was set.

[nullable][transfer none]


gtk_text_grab_focus_without_selecting ()

gboolean
gtk_text_grab_focus_without_selecting (GtkText *self);

Causes self to have keyboard focus.

It behaves like gtk_widget_grab_focus(), except that it doesn't select the contents of the self. You only want to call this on some special entries which the user usually doesn't want to replace all text in, such as search-as-you-type entries.

Parameters

self

a GtkText

 

Returns

TRUE if focus is now inside self


gtk_text_set_extra_menu ()

void
gtk_text_set_extra_menu (GtkText *self,
                         GMenuModel *model);

Sets a menu model to add when constructing the context menu for self .

Parameters

self

a GtkText

 

model

a GMenuModel.

[allow-none]

gtk_text_get_extra_menu ()

GMenuModel *
gtk_text_get_extra_menu (GtkText *self);

Gets the menu model set with gtk_text_set_extra_menu().

Parameters

self

a GtkText

 

Returns

(nullable): the menu model.

[transfer none]

Types and Values

struct GtkText

struct GtkText;

struct GtkTextClass

struct GtkTextClass {
  GtkWidgetClass parent_class;

  /* Action signals
   */
  void (* activate)           (GtkText         *self);
  void (* move_cursor)        (GtkText         *self,
                               GtkMovementStep  step,
                               gint             count,
                               gboolean         extend);
  void (* insert_at_cursor)   (GtkText         *self,
                               const gchar     *str);
  void (* delete_from_cursor) (GtkText         *self,
                               GtkDeleteType    type,
                               gint             count);
  void (* backspace)          (GtkText         *self);
  void (* cut_clipboard)      (GtkText         *self);
  void (* copy_clipboard)     (GtkText         *self);
  void (* paste_clipboard)    (GtkText         *self);
  void (* toggle_overwrite)   (GtkText         *self);
  void (* insert_emoji)       (GtkText         *self);
  void (* undo)               (GtkText         *self);
  void (* redo)               (GtkText         *self);
};

Property Details

The “activates-default” property

  “activates-default”        gboolean

Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “attributes” property

  “attributes”               PangoAttrList *

A list of Pango attributes to apply to the text of the self.

This is mainly useful to change the size or weight of the text.

The PangoAttribute's start_index and end_index must refer to the GtkEntryBuffer text, i.e. without the preedit string.

Owner: GtkText

Flags: Read / Write


The “buffer” property

  “buffer”                   GtkEntryBuffer *

Text buffer object which actually stores self text.

Owner: GtkText

Flags: Read / Write / Construct


The “enable-emoji-completion” property

  “enable-emoji-completion”  gboolean

Whether to suggest Emoji replacements.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “extra-menu” property

  “extra-menu”               GMenuModel *

A menu model whose contents will be appended to the context menu.

Owner: GtkText

Flags: Read / Write


The “im-module” property

  “im-module”                gchar *

Which IM (input method) module should be used for this self. See GtkIMContext.

Setting this to a non-NULL value overrides the system-wide IM module setting. See the GtkSettings “gtk-im-module” property.

Owner: GtkText

Flags: Read / Write

Default value: NULL


The “input-hints” property

  “input-hints”              GtkInputHints

Additional hints (beyond “input-purpose”) that allow input methods to fine-tune their behaviour.

Owner: GtkText

Flags: Read / Write


The “input-purpose” property

  “input-purpose”            GtkInputPurpose

The purpose of this text field.

This property can be used by on-screen keyboards and other input methods to adjust their behaviour.

Note that setting the purpose to GTK_INPUT_PURPOSE_PASSWORD or GTK_INPUT_PURPOSE_PIN is independent from setting “visibility”.

Owner: GtkText

Flags: Read / Write

Default value: GTK_INPUT_PURPOSE_FREE_FORM


The “invisible-char” property

  “invisible-char”           guint

The character to use when masking self contents (in “password mode”).

Owner: GtkText

Flags: Read / Write

Default value: '*'


The “invisible-char-set” property

  “invisible-char-set”       gboolean

Whether the invisible char has been set for the GtkText.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “max-length” property

  “max-length”               gint

Maximum number of characters for this self. Zero if no maximum.

Owner: GtkText

Flags: Read / Write

Allowed values: [0,65535]

Default value: 0


The “overwrite-mode” property

  “overwrite-mode”           gboolean

If text is overwritten when typing in the GtkText.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “placeholder-text” property

  “placeholder-text”         gchar *

The text that will be displayed in the GtkText when it is empty and unfocused.

Owner: GtkText

Flags: Read / Write

Default value: NULL


The “propagate-text-width” property

  “propagate-text-width”     gboolean

Whether the entry should grow and shrink with the content.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “scroll-offset” property

  “scroll-offset”            gint

Number of pixels of the self scrolled off the screen to the left.

Owner: GtkText

Flags: Read

Allowed values: >= 0

Default value: 0


The “tabs” property

  “tabs”                     PangoTabArray *

A list of tabstops to apply to the text of the self.

Owner: GtkText

Flags: Read / Write


The “truncate-multiline” property

  “truncate-multiline”       gboolean

When TRUE, pasted multi-line text is truncated to the first line.

Owner: GtkText

Flags: Read / Write

Default value: FALSE


The “visibility” property

  “visibility”               gboolean

FALSE displays the “invisible char” instead of the actual text (password mode).

Owner: GtkText

Flags: Read / Write

Default value: TRUE

Signal Details

The “activate” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::activate signal is emitted when the user hits the Enter key.

The default bindings for this signal are all forms of the Enter key.

Parameters

self

The self on which the signal is emitted

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “backspace” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::backspace signal is a keybinding signal which gets emitted when the user asks for it.

The default bindings for this signal are Backspace and Shift-Backspace.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “copy-clipboard” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::copy-clipboard signal is a keybinding signal which gets emitted to copy the selection to the clipboard.

The default bindings for this signal are Ctrl-c and Ctrl-Insert.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “cut-clipboard” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::cut-clipboard signal is a keybinding signal which gets emitted to cut the selection to the clipboard.

The default bindings for this signal are Ctrl-x and Shift-Delete.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “delete-from-cursor” signal

void
user_function (GtkText      *self,
               GtkDeleteType type,
               gint          count,
               gpointer      user_data)

The ::delete-from-cursor signal is a keybinding signal which gets emitted when the user initiates a text deletion.

If the type is GTK_DELETE_CHARS, GTK deletes the selection if there is one, otherwise it deletes the requested number of characters.

The default bindings for this signal are Delete for deleting a character and Ctrl-Delete for deleting a word.

Parameters

self

the object which received the signal

 

type

the granularity of the deletion, as a GtkDeleteType

 

count

the number of type units to delete

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “insert-at-cursor” signal

void
user_function (GtkText *self,
               gchar   *string,
               gpointer user_data)

The ::insert-at-cursor signal is a keybinding signal which gets emitted when the user initiates the insertion of a fixed string at the cursor.

This signal has no default bindings.

Parameters

self

the object which received the signal

 

string

the string to insert

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “insert-emoji” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::insert-emoji signal is a keybinding signal which gets emitted to present the Emoji chooser for the self .

The default bindings for this signal are Ctrl-. and Ctrl-;

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “move-cursor” signal

void
user_function (GtkText        *self,
               GtkMovementStep step,
               gint            count,
               gboolean        extend,
               gpointer        user_data)

The ::move-cursor signal is a keybinding signal which gets emitted when the user initiates a cursor movement. If the cursor is not visible in self , this signal causes the viewport to be moved instead.

Applications should not connect to it, but may emit it with g_signal_emit_by_name() if they need to control the cursor programmatically.

The default bindings for this signal come in two variants, the variant with the Shift modifier extends the selection, the variant without the Shift modifer does not. There are too many key combinations to list them all here.

  • Arrow keys move by individual characters/lines

  • Ctrl-arrow key combinations move by words/paragraphs

  • Home/End keys move to the ends of the buffer

Parameters

self

the object which received the signal

 

step

the granularity of the move, as a GtkMovementStep

 

count

the number of step units to move

 

extend

TRUE if the move should extend the selection

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “paste-clipboard” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::paste-clipboard signal is a keybinding signal which gets emitted to paste the contents of the clipboard into the text view.

The default bindings for this signal are Ctrl-v and Shift-Insert.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “preedit-changed” signal

void
user_function (GtkText *self,
               gchar   *preedit,
               gpointer user_data)

If an input method is used, the typed text will not immediately be committed to the buffer. So if you are interested in the text, connect to this signal.

Parameters

self

the object which received the signal

 

preedit

the current preedit string

 

user_data

user data set when the signal handler was connected.

 

Flags: Action


The “toggle-overwrite” signal

void
user_function (GtkText *self,
               gpointer user_data)

The ::toggle-overwrite signal is a keybinding signal which gets emitted to toggle the overwrite mode of the self.

The default bindings for this signal is Insert.

Parameters

self

the object which received the signal

 

user_data

user data set when the signal handler was connected.

 

Flags: Action

Action Details

The “menu.popup” action

Opens the context menu.


The “text.redo” action

Redoes the last change to the contents.


The “text.undo” action

Undoes the last change to the contents.


The “misc.toggle-visibility” action

Toggles the “visibility” property.


The “misc.insert-emoji” action

Opens the Emoji chooser.


The “selection.select-all” action

Selects all of the widgets content.


The “selection.delete” action

Deletes the current selection.


The “clipboard.paste” action

Inserts the contents of the clipboard into the widget.


The “clipboard.copy” action

Copies the contents to the clipboard.


The “clipboard.cut” action

Copies the contents to the clipboard and deletes it from the widget.

See Also

GtkEntry, GtkTextView