Class: Gtk::Text

Inherits:
Widget
  • Object
show all
Defined in:
(unknown)

Overview

A single-line text entry.

GtkText is the common implementation of single-line text editing
that is shared between [classGtk.Entry], [classGtk.PasswordEntry],
[classGtk.SpinButton], and other widgets. In all of these, a GtkText
instance is used as the delegate for the [ifaceGtk.Editable] 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 [methodGtk.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
[methodGtk.Text.set_invisible_char].

If you want to add icons or progress display in an entry, look at
[classGtk.Entry]. There are other alternatives for more specialized
use cases, such as [classGtk.SearchEntry].

If you need multi-line editable text, use [classGtk.TextView].

Shortcuts and Gestures

GtkText supports the following keyboard shortcuts:

  • Shift+F10 or Menu opens the context menu.
  • Ctrl+A or Ctrl+/
    selects all the text.
  • Ctrl+Shift+A or
    Ctrl+\ 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:

  • [signalGtk.Text::activate]
  • [signalGtk.Text::backspace]
  • [signalGtk.Text::copy-clipboard]
  • [signalGtk.Text::cut-clipboard]
  • [signalGtk.Text::delete-from-cursor]
  • [signalGtk.Text::insert-emoji]
  • [signalGtk.Text::move-cursor]
  • [signalGtk.Text::paste-clipboard]
  • [signalGtk.Text::toggle-overwrite]

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

Instance Method Summary collapse

Methods inherited from Widget

#accessible_role, #accessible_role=, #action_set_enabled, #activate, #activate_action, #activate_action_variant, #activate_default, #activate_signal, #activate_signal=, #activate_signal_from_name=, #add_binding, #add_binding_action, #add_binding_signal, #add_controller, #add_css_class, #add_mnemonic_label, #add_shortcut, #add_tick_callback, #allocate, #allocated_baseline, #allocated_height, #allocated_width, #baseline, #bind_template_callback_full, #bind_template_child_full, #can_focus, #can_focus=, #can_focus?, #can_target, #can_target=, #can_target?, #child_focus, #child_visible, #child_visible=, #clipboard, #compute_bounds, #compute_expand, #compute_point, #compute_transform, #contains, #create_pango_context, #create_pango_layout, #css_classes, #css_classes=, #css_name, #css_name=, #cursor, #cursor=, #cursor_from_name=, default_direction, default_direction=, #direction, #direction=, #display, #dispose_template, #drag_check_threshold, #error_bell, #first_child, #focus_child, #focus_child=, #focus_on_click, #focus_on_click=, #focus_on_click?, #focusable, #focusable=, #focusable?, #font_map, #font_map=, #font_options, #font_options=, #frame_clock, #get_allocation, #get_ancestor, #get_color, #get_preferred_size, #get_size, #get_size_request, #get_template_child, #grab_focus, #halign, #halign=, #has_css_class, #has_default, #has_default=, #has_default?, #has_focus, #has_focus=, #has_focus?, #has_tooltip, #has_tooltip=, #has_tooltip?, #has_visible_focus, #height, #height_request, #height_request=, #hexpand, #hexpand=, #hexpand?, #hexpand_set, #hexpand_set=, #hexpand_set?, #hide, #in_destruction, #init_template, #insert_action_group, #insert_after, #insert_before, #install_action, #install_property_action, #is_ancestor, #is_drawable, #is_focus, #is_sensitive, #is_visible, #keynav_failed, #last_child, #layout_manager, #layout_manager=, #layout_manager_type, #layout_manager_type=, #limit_events, #limit_events=, #limit_events?, #list_mnemonic_labels, #map, #mapped, #margin_bottom, #margin_bottom=, #margin_end, #margin_end=, #margin_start, #margin_start=, #margin_top, #margin_top=, #measure, #mnemonic_activate, #name, #name=, #native, #next_sibling, #observe_children, #observe_controllers, #opacity, #opacity=, #overflow, #overflow=, #pango_context, #parent, #parent=, #pick, #prev_sibling, #primary_clipboard, #query_action, #queue_allocate, #queue_draw, #queue_resize, #realize, #realized, #receives_default, #receives_default=, #receives_default?, #remove_controller, #remove_css_class, #remove_mnemonic_label, #remove_tick_callback, #request_mode, #root, #root=, #scale_factor, #scale_factor=, #sensitive, #sensitive=, #sensitive?, #set_size_request, #set_state_flags, #settings, #should_layout, #show, #size_allocate, #snapshot_child, #state_flags, #style_context, #template=, #template_from_resource=, #template_scope=, #tooltip_markup, #tooltip_markup=, #tooltip_text, #tooltip_text=, #translate_coordinates, #trigger_tooltip_query, #unmap, #unparent, #unrealize, #unset_state_flags, #valign, #valign=, #vexpand, #vexpand=, #vexpand?, #vexpand_set, #vexpand_set=, #vexpand_set?, #visible, #visible=, #visible?, #width, #width_request, #width_request=

Constructor Details

#initialize(buffer) ⇒ Gtk::Widget

Creates a new GtkText with the specified buffer.

Parameters:

Instance Method Details

#activates_default ⇒ Boolean

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

See [methodGtk.Text.set_activates_default].

Returns:

  • (Boolean) —

    true if self will activate the default widget

#activates_default=(activates_default) ⇒ Boolean

Whether to activate the default widget when Enter is pressed.

Parameters:

  • activates_default (Boolean)

Returns:

  • (Boolean) —

    activates-default

  • (Boolean) —

    activates-default

#activates_default? ⇒ Boolean

Whether to activate the default widget when Enter is pressed.

Returns:

  • (Boolean) —

    activates-default

#attributes ⇒ Pango::AttrList

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.

Returns:

  • (Pango::AttrList) —

    attributes

#attributes=(attributes) ⇒ Pango::AttrList

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.

Parameters:

  • attributes (Pango::AttrList)

Returns:

  • (Pango::AttrList) —

    attributes

  • (Pango::AttrList) —

    attributes

#buffer ⇒ Gtk::EntryBuffer

The GtkEntryBuffer object which stores the text.

Returns:

#buffer=(buffer) ⇒ Gtk::EntryBuffer

The GtkEntryBuffer object which stores the text.

Parameters:

Returns:

#compute_cursor_extents(position, strong, weak) ⇒ nil

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.

Parameters:

  • position (Integer) —

    the character position

  • strong (Graphene::Rect) —

    location to store the strong cursor position

  • weak (Graphene::Rect) —

    location to store the weak cursor position

Returns:

  • (nil)

#enable_emoji_completion ⇒ Boolean

Returns whether Emoji completion is enabled.

Returns:

  • (Boolean) —

    true if Emoji completion is enabled

#enable_emoji_completion=(enable_emoji_completion) ⇒ Boolean

Whether to suggest Emoji replacements.

Parameters:

  • enable_emoji_completion (Boolean)

Returns:

  • (Boolean) —

    enable-emoji-completion

  • (Boolean) —

    enable-emoji-completion

#enable_emoji_completion? ⇒ Boolean

Whether to suggest Emoji replacements.

Returns:

  • (Boolean) —

    enable-emoji-completion

#extra_menu ⇒ Gio::MenuModel

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

Returns:

  • (Gio::MenuModel) —

    extra-menu

#extra_menu=(extra_menu) ⇒ Gio::MenuModel

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

Parameters:

  • extra_menu (Gio::MenuModel)

Returns:

  • (Gio::MenuModel) —

    extra-menu

  • (Gio::MenuModel) —

    extra-menu

#grab_focus_without_selecting ⇒ Boolean

Causes the text widget to have the keyboard focus.

It behaves like [methodGtk.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.

Returns:

  • (Boolean) —

    true if focus is now inside self

#im_module ⇒ String

Which input method module should be used.

See [classGtk.IMMulticontext].

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

Returns:

  • (String) —

    im-module

#im_module=(im_module) ⇒ String

Which input method module should be used.

See [classGtk.IMMulticontext].

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

Parameters:

  • im_module (String)

Returns:

  • (String) —

    im-module

  • (String) —

    im-module

#input_hints ⇒ Gtk::InputHints

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

Returns:

#input_hints=(input_hints) ⇒ Gtk::InputHints

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

Parameters:

Returns:

#input_purpose ⇒ Gtk::InputPurpose

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 [enumGtk.InputPurpose.password]
or [enumGtk.InputPurpose.pin] is independent from setting
[propertyGtk.Text:visibility].

Returns:

#input_purpose=(input_purpose) ⇒ Gtk::InputPurpose

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 [enumGtk.InputPurpose.password]
or [enumGtk.InputPurpose.pin] is independent from setting
[propertyGtk.Text:visibility].

Parameters:

Returns:

#invisible_char ⇒ Integer

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

Returns:

  • (Integer) —

    invisible-char

#invisible_char=(invisible_char) ⇒ Integer

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

Parameters:

  • invisible_char (Integer)

Returns:

  • (Integer) —

    invisible-char

  • (Integer) —

    invisible-char

#invisible_char_set=(invisible_char_set) ⇒ Boolean

Whether the invisible char has been set.

Parameters:

  • invisible_char_set (Boolean)

Returns:

  • (Boolean) —

    invisible-char-set

  • (Boolean) —

    invisible-char-set

#invisible_char_set? ⇒ Boolean

Whether the invisible char has been set.

Returns:

  • (Boolean) —

    invisible-char-set

#max_length ⇒ Integer

Maximum number of characters that are allowed.

Zero indicates no limit.

Returns:

  • (Integer) —

    max-length

#max_length=(max_length) ⇒ Integer

Maximum number of characters that are allowed.

Zero indicates no limit.

Parameters:

  • max_length (Integer)

Returns:

  • (Integer) —

    max-length

  • (Integer) —

    max-length

#overwrite_mode ⇒ Boolean

Gets whether text is overwritten when typing.

See [methodGtk.Text.set_overwrite_mode].

Returns:

  • (Boolean) —

    whether text is overwritten when typing

#overwrite_mode=(overwrite_mode) ⇒ Boolean

If text is overwritten when typing.

Parameters:

  • overwrite_mode (Boolean)

Returns:

  • (Boolean) —

    overwrite-mode

  • (Boolean) —

    overwrite-mode

#overwrite_mode? ⇒ Boolean

If text is overwritten when typing.

Returns:

  • (Boolean) —

    overwrite-mode

#placeholder_text ⇒ String

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

Returns:

  • (String) —

    placeholder-text

#placeholder_text=(placeholder_text) ⇒ String

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

Parameters:

  • placeholder_text (String)

Returns:

  • (String) —

    placeholder-text

  • (String) —

    placeholder-text

#propagate_text_width ⇒ Boolean

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

Returns:

  • (Boolean) —

    true if self will propagate the text width

#propagate_text_width=(propagate_text_width) ⇒ Boolean

Whether the widget should grow and shrink with the content.

Parameters:

  • propagate_text_width (Boolean)

Returns:

  • (Boolean) —

    propagate-text-width

  • (Boolean) —

    propagate-text-width

#propagate_text_width? ⇒ Boolean

Whether the widget should grow and shrink with the content.

Returns:

  • (Boolean) —

    propagate-text-width

#scroll_offset ⇒ Integer

Number of pixels scrolled of the screen to the left.

Returns:

  • (Integer) —

    scroll-offset

#scroll_offset=(scroll_offset) ⇒ Integer

Number of pixels scrolled of the screen to the left.

Parameters:

  • scroll_offset (Integer)

Returns:

  • (Integer) —

    scroll-offset

  • (Integer) —

    scroll-offset

#tabs ⇒ Pango::TabArray

Custom tabs for this text widget.

Returns:

  • (Pango::TabArray) —

    tabs

#tabs=(tabs) ⇒ Pango::TabArray

Custom tabs for this text widget.

Parameters:

  • tabs (Pango::TabArray)

Returns:

  • (Pango::TabArray) —

    tabs

  • (Pango::TabArray) —

    tabs

#text_length ⇒ Integer

Retrieves the length of the contents.

This is equivalent to getting self's GtkEntryBuffer
and calling [methodGtk.EntryBuffer.get_length] on it.

Returns:

  • (Integer) —

    the length of the contents, in characters

#truncate_multiline ⇒ Boolean

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

Returns:

  • (Boolean) —

    true if self will truncate pasted multi-line text

#truncate_multiline=(truncate_multiline) ⇒ Boolean

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

Parameters:

  • truncate_multiline (Boolean)

Returns:

  • (Boolean) —

    truncate-multiline

  • (Boolean) —

    truncate-multiline

#truncate_multiline? ⇒ Boolean

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

Returns:

  • (Boolean) —

    truncate-multiline

#unset_invisible_char ⇒ nil

Unsets the invisible char.

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

Returns:

  • (nil)

#visibility ⇒ Boolean

Retrieves whether the text is visible.

Returns:

  • (Boolean) —

    true if the text is visible

#visibility=(visibility) ⇒ Boolean

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

Parameters:

  • visibility (Boolean)

Returns:

  • (Boolean) —

    visibility

  • (Boolean) —

    visibility

#visibility? ⇒ Boolean

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

Returns:

  • (Boolean) —

    visibility