1.197 <GtkText>

Class: <GtkText>

It derives from <GtkWidget>.

A single-line text entry.

<GtkText> is the common implementation of single-line text editing that is shared between <GtkEntry>, <GtkPasswordEntry>, <GtkSpinButton>, and other widgets. In all of these, a <GtkText> instance is used as the delegate for the <GtkEditable> implementation.

A large number of key bindings s 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 text:set-visibility. In this mode, entered text is displayed using an “invisible” character. By default, GTK picks the best invisible character that is available in the current font, but it can be changed with text:set-invisible-char.

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

If you need multi-line editable text, use <GtkTextView>.

Shortcuts and Gestures

<GtkText> supports the following keyboard shortcuts:

  • Shift+F10 or Menu opens the context menu.
  • Ctrl+A or Ctrl+&sol; selects all the text.
  • Ctrl+Shift+A or Ctrl+&bsol; unselects all.
  • Ctrl+Z undoes the last modification.
  • Ctrl+Y or Ctrl+Shift+Z redoes the last undone modification.
  • Ctrl+Shift+T toggles the text direction.
  • Clear clears the content.

Additionally, the following signals have default keybindings:

Actions

<GtkText> defines a set of built-in actions:

  • clipboard.copy copies the contents to the clipboard.
  • clipboard.cut copies the contents to the clipboard and deletes it from the widget.
  • clipboard.paste inserts the contents of the clipboard into the widget.
  • menu.popup opens the context menu.
  • misc.insert-emoji opens the Emoji chooser.
  • misc.toggle-visibility toggles the <GtkText>:visibility property.
  • misc.toggle-direction toggles the text direction.
  • selection.delete deletes the current selection.
  • selection.select-all selects all of the widgets content.
  • text.redo redoes the last change to the contents.
  • text.undo undoes the last change to the contents.
  • text.clear removes all content.

CSS nodes

text[.read-only]
├── placeholder
├── undershoot.left
├── undershoot.right
├── [selection]
├── [cursor-handle[.top]
├── [cursor-handle.bottom]
├── [block-cursor]
├── [cursor-handle[.top/.bottom][.insertion-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 with the name popup.

The undershoot nodes are used to draw the underflow indication when content is scrolled out of view. These nodes get the .left or .right style class 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.

Accessibility

<GtkText> uses the Gtk.AccessibleRole.none role, which causes it to be skipped for accessibility. This is because <GtkText> is expected to be used as a delegate for a <GtkEditable> implementation that will be represented to accessibility.

Properties

Property: text:activates-default self

Whether to activate the default widget when Enter is pressed.

Its value is a boolean.

Set it with (set! (text:activates-default self) value).

Property: text:attributes self

A list of Pango attributes to apply to the text.

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.

Its value is a <PangoAttrList>, or #f.

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

Property: text:buffer self

The <GtkEntryBuffer> object which stores the text.

Its value is a <GtkEntryBuffer>, or #f.

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

Property: text:enable-emoji-completion self

Whether to suggest Emoji replacements.

Its value is a boolean.

Set it with (set! (text:enable-emoji-completion self) value).

Property: text:extra-menu self

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

Its value is a <GMenuModel>, or #f.

Set it with (set! (text:extra-menu self) value).

Property: text:im-module self

Which input method module should be used.

See <GtkIMMulticontext>.

Setting this to a non-NULL value overrides the system-wide input method. See the settings:gtk-im-module setting.

Its value is a string, or #f.

Set it with (set! (text:im-module self) value).

Property: text:input-hints self

Additional hints that allow input methods to fine-tune their behaviour.

Its value is a <GtkInputHints> (or, to set it, a list of its symbols, or one of them).

Set it with (set! (text:input-hints self) value).

Property: text:input-purpose self

The purpose of this text field.

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

Note that setting the purpose to Gtk.InputPurpose.password or Gtk.InputPurpose.pin is independent from setting text:visibility.

Its value is a <GtkInputPurpose> (or, to set it, one of its symbols).

Set it with (set! (text:input-purpose self) value).

Property: text:invisible-char self

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

Its value is an unsigned 32-bit integer.

Set it with (set! (text:invisible-char self) value).

Property: text:invisible-char-set self

Whether the invisible char has been set.

Its value is a boolean.

Set it with (set! (text:invisible-char-set self) value).

Property: text:max-length self

Maximum number of characters that are allowed.

Zero indicates no limit.

Its value is a 32-bit integer.

Set it with (set! (text:max-length self) value).

Property: text:overwrite-mode self

If text is overwritten when typing.

Its value is a boolean.

Set it with (set! (text:overwrite-mode self) value).

Property: text:placeholder-text self

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

Its value is a string, or #f.

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

Property: text:propagate-text-width self

Whether the widget should grow and shrink with the content.

Its value is a boolean.

Set it with (set! (text:propagate-text-width self) value).

Property: text:scroll-offset self

Number of pixels scrolled of the screen to the left.

Its value is a 32-bit integer.

It is read only.

Property: text:tabs self

Custom tabs for this text widget.

Its value is a <PangoTabArray>, or #f.

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

Property: text:truncate-multiline self

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

Its value is a boolean.

Set it with (set! (text:truncate-multiline self) value).

Property: text:visibility self

If false, the text is masked with the “invisible char”.

Its value is a boolean.

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

Procedures

Method: text:compute-cursor-extents! self position strong weak ⇒ strong weak
Method: compute-cursor-extents! self position strong weak ⇒ strong weak

Determines the positions of the strong and weak cursors for a given character position.

The position of each cursor is stored as a zero-width rectangle. The strong cursor location is the location where characters of the directionality equal to the base direction are inserted. The weak cursor location is the location where characters of the directionality opposite to the base direction are inserted.

The rectangle positions are in widget coordinates.

self

Passed: a <GtkText>.

a text widget

position

Passed: an unsigned 64-bit integer.

the character position

strong

Passed, filled in, and returned: a <GrapheneRect>.

location to store the strong cursor position

weak

Passed, filled in, and returned: a <GrapheneRect>.

location to store the weak cursor position

Method: text:get-activates-default? self ⇒ %return
Method: get-activates-default? self ⇒ %return

Returns whether pressing Enter will activate the default widget for the window containing the widget.

See text:set-activates-default.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if self will activate the default widget

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

Gets the attribute list that was set on the text widget.

See text:set-attributes.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <PangoAttrList>, or #f.

the attribute list

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

Get the entry buffer object which holds the text for this widget.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <GtkEntryBuffer>.

the entry buffer object

Method: text:get-enable-emoji-completion? self ⇒ %return
Method: get-enable-emoji-completion? self ⇒ %return

Returns whether Emoji completion is enabled.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if Emoji completion is enabled

Method: text:get-extra-menu self ⇒ %return
Method: get-extra-menu self ⇒ %return

Gets the extra menu model of the text widget.

See text:set-extra-menu.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <GMenuModel>, or #f.

the menu model

Method: text:get-input-hints self ⇒ %return
Method: get-input-hints self ⇒ %return

Gets the input hints of the text widget.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <GtkInputHints>.

the input hints

Method: text:get-input-purpose self ⇒ %return
Method: get-input-purpose self ⇒ %return

Gets the input purpose of the text widget.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <GtkInputPurpose>.

the input purpose

Method: text:get-invisible-char self ⇒ %return
Method: get-invisible-char self ⇒ %return

Retrieves the character displayed when visibility is set to false.

Note that GTK does not compute this value unless it needs it, so the value returned by this function is not very useful unless it has been explicitly set with text:set-invisible-char.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a character.

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

Method: text:get-max-length self ⇒ %return
Method: get-max-length self ⇒ %return

Retrieves the maximum allowed length of the contents.

See text:set-max-length.

This is equivalent to getting self’s <GtkEntryBuffer> and calling entry-buffer:get-max-length on it.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a 32-bit integer.

the maximum allowed number of characters, or 0 if there is no limit

Method: text:get-overwrite-mode? self ⇒ %return
Method: get-overwrite-mode? self ⇒ %return

Gets whether text is overwritten when typing.

See text:set-overwrite-mode.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

whether text is overwritten when typing

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

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

See text:set-placeholder-text.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a string, or #f.

the placeholder text

Method: text:get-propagate-text-width? self ⇒ %return
Method: get-propagate-text-width? self ⇒ %return

Returns whether the text widget will grow and shrink with the content.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if self will propagate the text width

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

Gets the tab stops for the text widget.

See text:set-tabs.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a <PangoTabArray>, or #f.

the tab stops

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

Retrieves the length of the contents.

This is equivalent to getting self’s <GtkEntryBuffer> and calling entry-buffer:get-length on it.

self

Passed: a <GtkText>.

a text widget

%return

Returned: an unsigned 16-bit integer.

the length of the contents, in characters

Method: text:get-truncate-multiline? self ⇒ %return
Method: get-truncate-multiline? self ⇒ %return

Returns whether pasted text will be truncated to the first line.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if self will truncate pasted multi-line text

Method: text:get-visibility? self ⇒ %return
Method: get-visibility? self ⇒ %return

Retrieves whether the text is visible.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if the text is visible

Method: text:grab-focus-without-selecting? self ⇒ %return
Method: grab-focus-without-selecting? self ⇒ %return

Causes the text widget to have the keyboard focus.

It behaves like widget:grab-focus?, except that it does not select the contents of 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.

self

Passed: a <GtkText>.

a text widget

%return

Returned: a boolean.

true if focus is now inside self

Procedure: text:new ⇒ %return

Creates a new <GtkText>.

%return

Returned: a <GtkWidget>.

the new <GtkText>

Procedure: text:new-with-buffer buffer ⇒ %return

Creates a new <GtkText> with the specified buffer.

buffer

Passed: a <GtkEntryBuffer>.

the buffer to use

%return

Returned: a <GtkWidget>.

a new <GtkText>

Method: text:set-activates-default self activates
Method: set-activates-default self activates

Sets whether pressing Enter will activate the default widget.

This usually means that the dialog containing self will be closed, since the default widget is usually one of the dialog buttons.

self

Passed: a <GtkText>.

a text widget

activates

Passed: a boolean.

true to activate window’s default widget on Enter keypress

Method: text:set-attributes self [attrs]
Method: set-attributes self [attrs]

Apply attributes to the contents of the text widget.

self

Passed: a <GtkText>.

a text widget

attrs

Passed, or left out: a <PangoAttrList>, or #f.

a list of style attributes

Method: text:set-buffer self buffer
Method: set-buffer self buffer

Set the entry buffer object which holds the text for this widget.

self

Passed: a <GtkText>.

a text widget

buffer

Passed: a <GtkEntryBuffer>.

an entry buffer object

Method: text:set-enable-emoji-completion self enable-emoji-completion
Method: set-enable-emoji-completion self enable-emoji-completion

Sets whether Emoji completion is enabled.

If it is, typing ’:’, followed by a recognized keyword, will pop up a window with suggested Emojis matching the keyword.

self

Passed: a <GtkText>.

a text widget

enable-emoji-completion

Passed: a boolean.

true to enable Emoji completion

Method: text:set-extra-menu self [model]
Method: set-extra-menu self [model]

Sets a menu model to add to the context menu of the text widget.

self

Passed: a <GtkText>.

a text widget

model

Passed, or left out: a <GMenuModel>, or #f.

a menu model

Method: text:set-input-hints self hints
Method: set-input-hints self hints

Sets hints that allow input methods to fine-tune their behaviour.

self

Passed: a <GtkText>.

a text widget

hints

Passed: a <GtkInputHints>, or a list of its symbols, or one of them.

input hints

Method: text:set-input-purpose self purpose
Method: set-input-purpose self purpose

Sets the input purpose of the text widget.

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

self

Passed: a <GtkText>.

a text widget

purpose

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

the input purpose

Method: text:set-invisible-char self ch
Method: set-invisible-char self ch

Sets the character to use when in “password mode”.

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.

self

Passed: a <GtkText>.

a text widget

ch

Passed: a character.

a Unicode character

Method: text:set-max-length self length
Method: set-max-length self length

Sets the maximum allowed length of the contents.

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

This is equivalent to getting self’s <GtkEntryBuffer> and calling entry-buffer:set-max-length on it.

self

Passed: a <GtkText>.

a text widget

length

Passed: a 32-bit integer.

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

Method: text:set-overwrite-mode self overwrite
Method: set-overwrite-mode self overwrite

Sets whether the text is overwritten when typing.

self

Passed: a <GtkText>.

a text widget

overwrite

Passed: a boolean.

new value

Method: text:set-placeholder-text self [text]
Method: set-placeholder-text self [text]

Sets the text to be displayed when the text widget is empty and unfocused.

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

self

Passed: a <GtkText>.

a text widget

text

Passed, or left out: a string, or #f.

a string to be displayed when self is empty and unfocused

Method: text:set-propagate-text-width self propagate-text-width
Method: set-propagate-text-width self propagate-text-width

Sets whether the text widget should grow and shrink with the content.

self

Passed: a <GtkText>.

a text widget

propagate-text-width

Passed: a boolean.

true to propagate the text width

Method: text:set-tabs self [tabs]
Method: set-tabs self [tabs]

Sets tab stops for the text widget.

self

Passed: a <GtkText>.

a text widget

tabs

Passed, or left out: a <PangoTabArray>, or #f.

tab stops

Method: text:set-truncate-multiline self truncate-multiline
Method: set-truncate-multiline self truncate-multiline

Sets whether pasted text should be truncated to the first line.

self

Passed: a <GtkText>.

a text widget

truncate-multiline

Passed: a boolean.

true to truncate multi-line text

Method: text:set-visibility self visible
Method: set-visibility self visible

Sets whether the contents of the text widget are visible or not.

When visibility is set to false, characters are displayed as the invisible char, and it will also appear that way when the text in the 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 text:set-invisible-char.

Note that you probably want to set text:input-purpose to Gtk.InputPurpose.password or Gtk.InputPurpose.pin to inform input methods about the purpose of this widget, in addition to setting visibility to false.

self

Passed: a <GtkText>.

a text widget

visible

Passed: a boolean.

true if the contents of the text widget are displayed as plain text

Method: text:unset-invisible-char self
Method: unset-invisible-char self

Unsets the invisible char.

After calling this, the default invisible char is used again.

self

Passed: a <GtkText>.

a text widget

Signals

Signal: text:activate

Connect a handler with (connect object text:activate handler). It is called as (handler self).

Emitted when the user hits the Enter key.

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

self

Passed: a <GtkText>.

Signal: text:backspace

Connect a handler with (connect object text:backspace handler). It is called as (handler self).

Emitted when the user asks for it.

This is a keybinding signal.

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

self

Passed: a <GtkText>.

Signal: text:copy-clipboard

Connect a handler with (connect object text:copy-clipboard handler). It is called as (handler self).

Emitted to copy the selection to the clipboard.

This is a keybinding signal.

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

self

Passed: a <GtkText>.

Signal: text:cut-clipboard

Connect a handler with (connect object text:cut-clipboard handler). It is called as (handler self).

Emitted to cut the selection to the clipboard.

This is a keybinding signal.

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

self

Passed: a <GtkText>.

Signal: text:delete-from-cursor

Connect a handler with (connect object text:delete-from-cursor handler). It is called as (handler self type count).

Emitted when the user initiates a text deletion.

This is a keybinding signal.

If the type is Gtk.DeleteType.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.

self

Passed: a <GtkText>.

type

Passed: a <GtkDeleteType>.

the granularity of the deletion

count

Passed: a 32-bit integer.

the number of type units to delete

Signal: text:insert-at-cursor

Connect a handler with (connect object text:insert-at-cursor handler). It is called as (handler self string).

Emitted when the user initiates the insertion of a fixed string at the cursor.

This is a keybinding signal.

This signal has no default bindings.

self

Passed: a <GtkText>.

string

Passed: a string.

the string to insert

Signal: text:insert-emoji

Connect a handler with (connect object text:insert-emoji handler). It is called as (handler self).

Emitted to present the Emoji chooser.

This is a keybinding signal.

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

self

Passed: a <GtkText>.

Signal: text:move-cursor

Connect a handler with (connect object text:move-cursor handler). It is called as (handler self step count extend).

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.

This is a keybinding signal.

Applications should not connect to it, but may emit it with GObject.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 it does not. There are too many key combinations to list them all here.

  • ←, →, ↑, ↓ move by individual characters/lines
  • Ctrl+←, etc. move by words/paragraphs
  • Home and End move to the ends of the buffer
self

Passed: a <GtkText>.

step

Passed: a <GtkMovementStep>.

the granularity of the move

count

Passed: a 32-bit integer.

the number of step units to move

extend

Passed: a boolean.

true if the move should extend the selection

Signal: text:paste-clipboard

Connect a handler with (connect object text:paste-clipboard handler). It is called as (handler self).

Emitted to paste the contents of the clipboard.

This is a keybinding signal.

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

self

Passed: a <GtkText>.

Signal: text:preedit-changed

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

Emitted when the preedit text changes.

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.

self

Passed: a <GtkText>.

preedit

Passed: a string.

the current preedit string

Signal: text:toggle-overwrite

Connect a handler with (connect object text:toggle-overwrite handler). It is called as (handler self).

Emitted to toggle the overwrite mode.

This is a keybinding signal.

The default bindings for this signal is Insert.

self

Passed: a <GtkText>.