Class Text

All Implemented Interfaces:
Accessible, AccessibleText, Buildable, ConstraintTarget, Editable, Proxy

@Generated("org.javagi.JavaGI") public class Text extends Widget implements Accessible, AccessibleText, Buildable, ConstraintTarget, Editable

A single-line text entry.

GtkText is the common implementation of single-line text editing that is shared between Entry, PasswordEntry, SpinButton, and other widgets. In all of these, a GtkText instance is used as the delegate for the 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 setVisibility(boolean). 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 setInvisibleChar(int).

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

If you need multi-line editable text, use 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:

  • Gtk.Text::activate
  • Gtk.Text::backspace
  • Gtk.Text::copy-clipboard
  • Gtk.Text::cut-clipboard
  • Gtk.Text::delete-from-cursor
  • Gtk.Text::insert-emoji
  • Gtk.Text::move-cursor
  • Gtk.Text::paste-clipboard
  • Gtk.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 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.

  • Constructor Details

    • Text

      public Text(MemorySegment address)
      Create a Text instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Text

      public Text()
      Create a new Text.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Text class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Text asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class Widget
      Returns:
      the instance as if it were its parent type
    • withBuffer

      public static Text withBuffer(EntryBuffer buffer)
      Creates a new GtkText with the specified buffer.
      Parameters:
      buffer - the buffer to use
      Returns:
      a new GtkText
    • computeCursorExtents

      public void computeCursorExtents(long position, @Nullable Rect strong, @Nullable Rect 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.

      Parameters:
      position - the character position
      strong - location to store the strong cursor position
      weak - location to store the weak cursor position
      Since:
      4.4
    • getActivatesDefault

      public boolean getActivatesDefault()

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

      See setActivatesDefault(boolean).

      Returns:
      true if this Text will activate the default widget
    • getAttributes

      public @Nullable AttrList getAttributes()

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

      See setAttributes(AttrList).

      Returns:
      the attribute list
    • getBuffer

      public EntryBuffer getBuffer()
      Get the entry buffer object which holds the text for this widget.
      Returns:
      the entry buffer object
    • getEnableEmojiCompletion

      public boolean getEnableEmojiCompletion()
      Returns whether Emoji completion is enabled.
      Returns:
      true if Emoji completion is enabled
    • getExtraMenu

      public @Nullable MenuModel getExtraMenu()

      Gets the extra menu model of the text widget.

      See setExtraMenu(MenuModel).

      Returns:
      the menu model
    • getInputHints

      public Set<InputHints> getInputHints()
      Gets the input hints of the text widget.
      Returns:
      the input hints
    • getInputPurpose

      public InputPurpose getInputPurpose()
      Gets the input purpose of the text widget.
      Returns:
      the input purpose
    • getInvisibleChar

      public int getInvisibleChar()

      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 setInvisibleChar(int).

      Returns:
      the current invisible char, or 0, if text does not show invisible text at all
    • getMaxLength

      public int getMaxLength()

      Retrieves the maximum allowed length of the contents.

      See setMaxLength(int).

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

      Returns:
      the maximum allowed number of characters, or 0 if there is no limit
    • getOverwriteMode

      public boolean getOverwriteMode()

      Gets whether text is overwritten when typing.

      See setOverwriteMode(boolean).

      Returns:
      whether text is overwritten when typing
    • getPlaceholderText

      public @Nullable String getPlaceholderText()

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

      See setPlaceholderText(String).

      Returns:
      the placeholder text
    • getPropagateTextWidth

      public boolean getPropagateTextWidth()
      Returns whether the text widget will grow and shrink with the content.
      Returns:
      true if this Text will propagate the text width
    • getTabs

      public @Nullable TabArray getTabs()

      Gets the tab stops for the text widget.

      See setTabs(TabArray).

      Returns:
      the tab stops
    • getTextLength

      public short getTextLength()

      Retrieves the length of the contents.

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

      Returns:
      the length of the contents, in characters
    • getTruncateMultiline

      public boolean getTruncateMultiline()
      Returns whether pasted text will be truncated to the first line.
      Returns:
      true if this Text will truncate pasted multi-line text
    • getVisibility

      public boolean getVisibility()
      Retrieves whether the text is visible.
      Returns:
      true if the text is visible
    • grabFocusWithoutSelecting

      public boolean grabFocusWithoutSelecting()

      Causes the text widget to have the keyboard focus.

      It behaves like Widget.grabFocus(), 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:
      true if focus is now inside this Text
    • setActivatesDefault

      public void setActivatesDefault(boolean activates)

      Sets whether pressing Enter will activate the default widget.

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

      Parameters:
      activates - true to activate window’s default widget on Enter keypress
    • setAttributes

      public void setAttributes(@Nullable AttrList attrs)
      Apply attributes to the contents of the text widget.
      Parameters:
      attrs - a list of style attributes
    • setBuffer

      public void setBuffer(EntryBuffer buffer)
      Set the entry buffer object which holds the text for this widget.
      Parameters:
      buffer - an entry buffer object
    • setEnableEmojiCompletion

      public void setEnableEmojiCompletion(boolean enableEmojiCompletion)

      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.

      Parameters:
      enableEmojiCompletion - true to enable Emoji completion
    • setExtraMenu

      public void setExtraMenu(@Nullable MenuModel model)
      Sets a menu model to add to the context menu of the text widget.
      Parameters:
      model - a menu model
    • setInputHints

      public void setInputHints(Set<InputHints> hints)
      Sets hints that allow input methods to fine-tune their behaviour.
      Parameters:
      hints - input hints
    • setInputHints

      public void setInputHints(InputHints... hints)
      Sets hints that allow input methods to fine-tune their behaviour.
      Parameters:
      hints - input hints
    • setInputPurpose

      public void setInputPurpose(InputPurpose 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.

      Parameters:
      purpose - the input purpose
    • setInvisibleChar

      public void setInvisibleChar(int 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.

      Parameters:
      ch - a Unicode character
    • setMaxLength

      public void setMaxLength(int 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 EntryBuffer.setMaxLength(int) on it.

      Parameters:
      length - 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
    • setOverwriteMode

      public void setOverwriteMode(boolean overwrite)
      Sets whether the text is overwritten when typing.
      Parameters:
      overwrite - new value
    • setPlaceholderText

      public void setPlaceholderText(@Nullable String 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.

      Parameters:
      text - a string to be displayed when this Text is empty and unfocused
    • setPropagateTextWidth

      public void setPropagateTextWidth(boolean propagateTextWidth)
      Sets whether the text widget should grow and shrink with the content.
      Parameters:
      propagateTextWidth - true to propagate the text width
    • setTabs

      public void setTabs(@Nullable TabArray tabs)
      Sets tab stops for the text widget.
      Parameters:
      tabs - tab stops
    • setTruncateMultiline

      public void setTruncateMultiline(boolean truncateMultiline)
      Sets whether pasted text should be truncated to the first line.
      Parameters:
      truncateMultiline - true to truncate multi-line text
    • setVisibility

      public void setVisibility(boolean 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 setInvisibleChar(int).

      Note that you probably want to set Gtk.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.

      Parameters:
      visible - true if the contents of the text widget are displayed as plain text
    • unsetInvisibleChar

      public void unsetInvisibleChar()

      Unsets the invisible char.

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

    • onActivate

      Emitted when the user hits the Enter key.

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

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitActivate

      public void emitActivate()
      Emits the "activate" signal. See onActivate(Text.ActivateCallback).
    • onBackspace

      Emitted when the user asks for it.

      This is a keybinding signal.

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

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitBackspace

      public void emitBackspace()
      Emits the "backspace" signal. See onBackspace(Text.BackspaceCallback).
    • onCopyClipboard

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitCopyClipboard

      public void emitCopyClipboard()
      Emits the "copy-clipboard" signal. See onCopyClipboard(Text.CopyClipboardCallback).
    • onCutClipboard

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitCutClipboard

      public void emitCutClipboard()
      Emits the "cut-clipboard" signal. See onCutClipboard(Text.CutClipboardCallback).
    • onDeleteFromCursor

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitDeleteFromCursor

      public void emitDeleteFromCursor(DeleteType type, int count)
      Emits the "delete-from-cursor" signal. See onDeleteFromCursor(Text.DeleteFromCursorCallback).
    • onInsertAtCursor

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitInsertAtCursor

      public void emitInsertAtCursor(String string)
      Emits the "insert-at-cursor" signal. See onInsertAtCursor(Text.InsertAtCursorCallback).
    • onInsertEmoji

      Emitted to present the Emoji chooser.

      This is a keybinding signal.

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

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitInsertEmoji

      public void emitInsertEmoji()
      Emits the "insert-emoji" signal. See onInsertEmoji(Text.InsertEmojiCallback).
    • onMoveCursor

      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 GObjects#signalEmitByName 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
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitMoveCursor

      public void emitMoveCursor(MovementStep step, int count, boolean extend)
      Emits the "move-cursor" signal. See onMoveCursor(Text.MoveCursorCallback).
    • onPasteClipboard

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPasteClipboard

      public void emitPasteClipboard()
      Emits the "paste-clipboard" signal. See onPasteClipboard(Text.PasteClipboardCallback).
    • onPreeditChanged

      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.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPreeditChanged

      public void emitPreeditChanged(String preedit)
      Emits the "preedit-changed" signal. See onPreeditChanged(Text.PreeditChangedCallback).
    • onToggleOverwrite

      Emitted to toggle the overwrite mode.

      This is a keybinding signal.

      The default bindings for this signal is Insert.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitToggleOverwrite

      public void emitToggleOverwrite()
      Emits the "toggle-overwrite" signal. See onToggleOverwrite(Text.ToggleOverwriteCallback).
    • builder

      public static Text.Builder<? extends Text.Builder> builder()
      A Text.Builder object constructs a Text with the specified properties. Use the various set...() methods to set properties, and finish construction with Text.Builder.build().
      Returns:
      the builder object