Class View

All Implemented Interfaces:
Accessible, AccessibleText, Buildable, ConstraintTarget, Scrollable, Proxy
Direct Known Subclasses:
Map

@Generated("org.javagi.JavaGI") public class View extends TextView implements Accessible, AccessibleText, Buildable, ConstraintTarget, Scrollable

Subclass of TextView.

GtkSourceView is the main class of the GtkSourceView library. Use a Buffer to display text with a GtkSourceView.

This class provides:

  • Show the line numbers;
  • Show a right margin;
  • Highlight the current line;
  • Indentation settings;
  • Configuration for the Home and End keyboard keys;
  • Configure and show line marks;
  • And a few other things.

An easy way to test all these features is to use the test-widget mini-program provided in the GtkSourceView repository, in the tests/ directory.

GtkSourceView as GtkBuildable

The GtkSourceView implementation of the Buildable interface exposes the View:completion object with the internal-child "completion".

An example of a UI definition fragment with GtkSourceView:

<object class="GtkSourceView" id="source_view">
  <property name="tab-width">4</property>
  <property name="auto-indent">True</property>
  <child internal-child="completion">
    <object class="GtkSourceCompletion">
      <property name="select-on-show">False</property>
    </object>
  </child>
</object>

Changing the Font

Gtk CSS provides the best way to change the font for a GtkSourceView in a manner that allows for components like Map to scale the desired font.

GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_string (provider,
                                  "textview { font-family: Monospace; font-size: 8pt; }");
gtk_style_context_add_provider (gtk_widget_get_style_context (view),
                                GTK_STYLE_PROVIDER (provider),
                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref (provider);
provider = Gtk.CssProvider()
provider.load_from_string("textview { font-family: Monospace; font-size: 8pt; }")
style_context = view.get_style_context()
style_context.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

If you need to adjust the font or size of font within a portion of the document only, you should use a TextTag with the Gtk.TextTag:family or Gtk.TextTag:scale set so that the font size may be scaled relative to the default font set in CSS.

  • Constructor Details

    • View

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

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

    • getType

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

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

      protected View 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 TextView
      Returns:
      the instance as if it were its parent type
    • withBuffer

      public static View withBuffer(Buffer buffer)

      Creates a new GtkSourceView widget displaying the buffer buffer.

      One buffer can be shared among many widgets.

      Parameters:
      buffer - a GtkSourceBuffer.
      Returns:
      a new GtkSourceView.
    • getAnnotations

      public Annotations getAnnotations()

      Gets the Annotations associated with view.

      The returned object is guaranteed to be the same for the lifetime of view. Each View object has a different Annotations.

      Returns:
      the GtkSourceAnnotations associated with view.
      Since:
      5.18
    • getAutoIndent

      public boolean getAutoIndent()
      Returns whether auto-indentation of text is enabled.
      Returns:
      true if auto indentation is enabled.
    • getBackgroundPattern

      public BackgroundPatternType getBackgroundPattern()
      Returns the GtkSourceBackgroundPatternType specifying if and how the background pattern should be displayed for this view.
      Returns:
      the GtkSourceBackgroundPatternType.
    • getCompletion

      public Completion getCompletion()

      Gets the Completion associated with view.

      The returned object is guaranteed to be the same for the lifetime of view. Each GtkSourceView object has a different Completion.

      Returns:
      the GtkSourceCompletion associated with view.
    • getEnableSnippets

      public boolean getEnableSnippets()

      Gets the View:enable-snippets property.

      If true, matching snippets found in the SnippetManager may be expanded when the user presses Tab after a word in the View.

      Returns:
      true if enabled
    • getGutter

      public Gutter getGutter(TextWindowType windowType)

      Returns the Gutter object associated with windowType for view.

      Only TextWindowType.LEFT and TextWindowType.RIGHT are supported, respectively corresponding to the left and right gutter. The line numbers and mark category icons are rendered in the left gutter.

      Overrides:
      getGutter in class TextView
      Parameters:
      windowType - the gutter window type.
      Returns:
      the GtkSourceGutter.
    • getHighlightCurrentLine

      public boolean getHighlightCurrentLine()
      Returns whether the current line is highlighted.
      Returns:
      true if the current line is highlighted.
    • getHover

      public Hover getHover()

      Gets the Hover associated with view.

      The returned object is guaranteed to be the same for the lifetime of view. Each View object has a different Hover.

      Returns:
      a GtkSourceHover associated with view.
    • getIndentOnTab

      public boolean getIndentOnTab()
      Returns whether when the tab key is pressed the current selection should get indented instead of replaced with the \\t character.
      Returns:
      true if the selection is indented when tab is pressed.
    • getIndentWidth

      public int getIndentWidth()

      Returns the number of spaces to use for each step of indent.

      See setIndentWidth(int) for details.

      Returns:
      indent width.
    • getIndenter

      public @Nullable Indenter getIndenter()
      Gets the View:indenter property.
      Returns:
      a GtkSourceIndenter or null
    • getInsertSpacesInsteadOfTabs

      public boolean getInsertSpacesInsteadOfTabs()
      Returns whether when inserting a tabulator character it should be replaced by a group of space characters.
      Returns:
      true if spaces are inserted instead of tabs.
    • getMarkAttributes

      public MarkAttributes getMarkAttributes(String category, MemorySegment priority)
      Gets attributes and priority for the category.
      Parameters:
      category - the category.
      priority - place where priority of the category will be stored.
      Returns:
      GtkSourceMarkAttributes for the category. The object belongs to view, so it must not be unreffed.
    • getRightMarginPosition

      public int getRightMarginPosition()
      Gets the position of the right margin in the given view.
      Returns:
      the position of the right margin.
    • getShowLineMarks

      public boolean getShowLineMarks()
      Returns whether line marks are displayed beside the text.
      Returns:
      true if the line marks are displayed.
    • getShowLineNumbers

      public boolean getShowLineNumbers()
      Returns whether line numbers are displayed beside the text.
      Returns:
      true if the line numbers are displayed.
    • getShowRightMargin

      public boolean getShowRightMargin()
      Returns whether a right margin is displayed.
      Returns:
      true if the right margin is shown.
    • getSmartBackspace

      public boolean getSmartBackspace()
      Returns true if pressing the Backspace key will try to delete spaces up to the previous tab stop.
      Returns:
      true if smart Backspace handling is enabled.
    • getSmartHomeEnd

      public SmartHomeEndType getSmartHomeEnd()
      Returns a SmartHomeEndType end value specifying how the cursor will move when HOME and END keys are pressed.
      Returns:
      a GtkSourceSmartHomeEndType value.
    • getSpaceDrawer

      public SpaceDrawer getSpaceDrawer()

      Gets the SpaceDrawer associated with view.

      The returned object is guaranteed to be the same for the lifetime of view. Each View object has a different SpaceDrawer.

      Returns:
      the GtkSourceSpaceDrawer associated with view.
    • getTabWidth

      public int getTabWidth()
      Returns the width of tabulation in characters.
      Returns:
      width of tab.
    • getVisualColumn

      public int getVisualColumn(TextIter iter)
      Determines the visual column at iter taking into consideration the View:tab-width of view.
      Parameters:
      iter - a position in view.
      Returns:
      the visual column at iter.
    • indentLines

      public void indentLines(TextIter start, TextIter end)
      Inserts one indentation level at the beginning of the specified lines. The empty lines are not indented.
      Parameters:
      start - GtkTextIter of the first line to indent
      end - GtkTextIter of the last line to indent
    • pushSnippet

      public void pushSnippet(Snippet snippet, @Nullable TextIter location)

      Inserts a new snippet at location

      If another snippet was already active, it will be paused and the new snippet will become active. Once the focus positions of snippet have been exhausted, editing will return to the previous snippet.

      Parameters:
      snippet - a GtkSourceSnippet
      location - a GtkTextIter or null for the cursor position
    • setAutoIndent

      public void setAutoIndent(boolean enable)

      If true auto-indentation of text is enabled.

      When Enter is pressed to create a new line, the auto-indentation inserts the same indentation as the previous line. This is not a "smart indentation" where an indentation level is added or removed depending on the context.

      Parameters:
      enable - whether to enable auto indentation.
    • setBackgroundPattern

      public void setBackgroundPattern(BackgroundPatternType backgroundPattern)
      Set if and how the background pattern should be displayed.
      Parameters:
      backgroundPattern - the GtkSourceBackgroundPatternType.
    • setEnableSnippets

      public void setEnableSnippets(boolean enableSnippets)

      Sets the View:enable-snippets property.

      If enableSnippets is true, matching snippets found in the SnippetManager may be expanded when the user presses Tab after a word in the View.

      Parameters:
      enableSnippets - if snippets should be enabled
    • setHighlightCurrentLine

      public void setHighlightCurrentLine(boolean highlight)
      If highlight is true the current line will be highlighted.
      Parameters:
      highlight - whether to highlight the current line.
    • setIndentOnTab

      public void setIndentOnTab(boolean enable)

      If true, when the tab key is pressed when several lines are selected, the selected lines are indented of one level instead of being replaced with a \\t character. Shift+Tab unindents the selection.

      If the first or last line is not selected completely, it is also indented or unindented.

      When the selection doesn't span several lines, the tab key always replaces the selection with a normal \\t character.

      Parameters:
      enable - whether to indent a block when tab is pressed.
    • setIndentWidth

      public void setIndentWidth(int width)

      Sets the number of spaces to use for each step of indent when the tab key is pressed.

      If width is -1, the value of the View:tab-width property will be used.

      The View:indent-width interacts with the View:insert-spaces-instead-of-tabs property and View:tab-width. An example will be clearer:

      If the View:indent-width is 4 and View:tab-width is 8 and View:insert-spaces-instead-of-tabs is false, then pressing the tab key at the beginning of a line will insert 4 spaces. So far so good. Pressing the tab key a second time will remove the 4 spaces and insert a \\t character instead (since View:tab-width is 8). On the other hand, if View:insert-spaces-instead-of-tabs is true, the second tab key pressed will insert 4 more spaces for a total of 8 spaces in the TextBuffer.

      The test-widget program (available in the GtkSourceView repository) may be useful to better understand the indentation settings (enable the space drawing!).

      Parameters:
      width - indent width in characters.
    • setIndenter

      public void setIndenter(@Nullable Indenter indenter)

      Sets the indenter for this View to indenter.

      Note that the indenter will not be used unless GtkSourceView:auto-indent has been set to true.

      Parameters:
      indenter - a GtkSourceIndenter or null
    • setInsertSpacesInsteadOfTabs

      public void setInsertSpacesInsteadOfTabs(boolean enable)

      If true a tab key pressed is replaced by a group of space characters.

      Of course it is still possible to insert a real \\t programmatically with the TextBuffer API.

      Parameters:
      enable - whether to insert spaces instead of tabs.
    • setMarkAttributes

      public void setMarkAttributes(String category, MarkAttributes attributes, int priority)
      Sets attributes and priority for the category.
      Parameters:
      category - the category.
      attributes - mark attributes.
      priority - priority of the category.
    • setRightMarginPosition

      public void setRightMarginPosition(int pos)
      Sets the position of the right margin in the given view.
      Parameters:
      pos - the width in characters where to position the right margin.
    • setShowLineMarks

      public void setShowLineMarks(boolean show)
      If true line marks will be displayed beside the text.
      Parameters:
      show - whether line marks should be displayed.
    • setShowLineNumbers

      public void setShowLineNumbers(boolean show)
      If true line numbers will be displayed beside the text.
      Parameters:
      show - whether line numbers should be displayed.
    • setShowRightMargin

      public void setShowRightMargin(boolean show)
      If true a right margin is displayed.
      Parameters:
      show - whether to show a right margin.
    • setSmartBackspace

      public void setSmartBackspace(boolean smartBackspace)
      When set to true, pressing the Backspace key will try to delete spaces up to the previous tab stop.
      Parameters:
      smartBackspace - whether to enable smart Backspace handling.
    • setSmartHomeEnd

      public void setSmartHomeEnd(SmartHomeEndType smartHomeEnd)
      Set the desired movement of the cursor when HOME and END keys are pressed.
      Parameters:
      smartHomeEnd - the desired behavior among GtkSourceSmartHomeEndType.
    • setTabWidth

      public void setTabWidth(int width)

      Sets the width of tabulation in characters.

      The GtkTextBuffer still contains \\t characters, but they can take a different visual width in a View widget.

      Parameters:
      width - width of tab in characters.
    • unindentLines

      public void unindentLines(TextIter start, TextIter end)
      Removes one indentation level at the beginning of the specified lines.
      Parameters:
      start - GtkTextIter of the first line to indent
      end - GtkTextIter of the last line to indent
    • lineMarkActivated

      protected void lineMarkActivated(TextIter iter, int button, Set<ModifierType> state, int nPresses)
    • moveLines

      protected void moveLines(boolean down)
    • moveWords

      protected void moveWords(int step)
    • showCompletion

      protected void showCompletion()
    • onChangeCase

      Keybinding signal to change case of the text at the current cursor position.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitChangeCase

      public void emitChangeCase(ChangeCaseType caseType)
      Emits the "change-case" signal. See onChangeCase(View.ChangeCaseCallback).
    • onChangeNumber

      Keybinding signal to edit a number at the current cursor position.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitChangeNumber

      public void emitChangeNumber(int count)
      Emits the "change-number" signal. See onChangeNumber(View.ChangeNumberCallback).
    • onJoinLines

      Keybinding signal to join the lines currently selected.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitJoinLines

      public void emitJoinLines()
      Emits the "join-lines" signal. See onJoinLines(View.JoinLinesCallback).
    • onLineMarkActivated

      Emitted when a line mark has been activated (for instance when there was a button press in the line marks gutter).

      You can use iter to determine on which line the activation took place.

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

      public void emitLineMarkActivated(@Nullable TextIter iter, int button, Set<ModifierType> state, int nPresses)
      Emits the "line-mark-activated" signal. See onLineMarkActivated(View.LineMarkActivatedCallback).
    • onMoveLines

      The signal is a keybinding which gets emitted when the user initiates moving a line.

      The default binding key is Alt+Up/Down arrow. And moves the currently selected lines, or the current line up or down by one line.

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

      public void emitMoveLines(boolean down)
      Emits the "move-lines" signal. See onMoveLines(View.MoveLinesCallback).
    • onMoveToMatchingBracket

      Keybinding signal to move the cursor to the matching bracket.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitMoveToMatchingBracket

      public void emitMoveToMatchingBracket(boolean extendSelection)
      Emits the "move-to-matching-bracket" signal. See onMoveToMatchingBracket(View.MoveToMatchingBracketCallback).
    • onMoveWords

      The signal is a keybinding which gets emitted when the user initiates moving a word.

      The default binding key is Alt+Left/Right Arrow and moves the current selection, or the current word by one word.

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

      public void emitMoveWords(int count)
      Emits the "move-words" signal. See onMoveWords(View.MoveWordsCallback).
    • onPushSnippet

      The signal is emitted to insert a new snippet into the view.

      If another snippet was active, it will be paused until all focus positions of snippet have been exhausted.

      location will be updated to point at the end of the snippet.

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

      public void emitPushSnippet(@Nullable Snippet snippet, @Nullable TextIter location)
      Emits the "push-snippet" signal. See onPushSnippet(View.PushSnippetCallback).
    • onShowCompletion

      The signal is a key binding signal which gets emitted when the user requests a completion, by pressing Controlspace.

      This will create a CompletionContext with the activation type as CompletionActivation.USER_REQUESTED.

      Applications should not connect to it, but may emit it with GObjects#signalEmitByName if they need to activate the completion by another means, for example with another key binding or a menu entry.

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

      public void emitShowCompletion()
      Emits the "show-completion" signal. See onShowCompletion(View.ShowCompletionCallback).
    • onSmartHomeEnd

      Emitted when a the cursor was moved according to the smart home end setting.

      The signal is emitted after the cursor is moved, but during the Gtk.TextView::move-cursor action. This can be used to find out whether the cursor was moved by a normal home/end or by a smart home/end.

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

      public void emitSmartHomeEnd(@Nullable TextIter iter, int count)
      Emits the "smart-home-end" signal. See onSmartHomeEnd(View.SmartHomeEndCallback).
    • builder

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