View

class View(**properties: Any)

Superclasses: TextView, Widget, InitiallyUnowned, Object

Subclasses: Map

Implemented Interfaces: 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 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_data (provider,
                                 "textview { font-family: Monospace; font-size: 8pt; }",
                                 -1,
                                 NULL);
gtk_style_context_add_provider (gtk_widget_get_style_context (view),
                                GTK_STYLE_PROVIDER (provider),
                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref (provider);

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 family or scale set so that the font size may be scaled relative to the default font set in CSS.

Constructors

class View
classmethod new() Widget

Creates a new GtkSourceView.

By default, an empty Buffer will be lazily created and can be retrieved with get_buffer.

If you want to specify your own buffer, either override the create_buffer factory method, or use new_with_buffer.

classmethod new_with_buffer(buffer: Buffer) Widget

Creates a new View widget displaying the buffer buffer.

One buffer can be shared among many widgets.

Parameters:

buffer – a Buffer.

Methods

class View
get_auto_indent() bool

Returns whether auto-indentation of text is enabled.

get_background_pattern() BackgroundPatternType

Returns the BackgroundPatternType specifying if and how the background pattern should be displayed for this view.

get_completion() Completion

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.

get_enable_snippets() bool

Gets the 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.

get_gutter(window_type: TextWindowType) Gutter

Returns the Gutter object associated with window_type for view.

Only %GTK_TEXT_WINDOW_LEFT and %GTK_TEXT_WINDOW_RIGHT are supported, respectively corresponding to the left and right gutter. The line numbers and mark category icons are rendered in the left gutter.

Parameters:

window_type – the gutter window type.

get_highlight_current_line() bool

Returns whether the current line is highlighted.

get_hover() Hover

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.

get_indent_on_tab() bool

Returns whether when the tab key is pressed the current selection should get indented instead of replaced with the \t character.

get_indent_width() int

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

See set_indent_width for details.

get_indenter() Indenter | None

Gets the indenter property.

get_insert_spaces_instead_of_tabs() bool

Returns whether when inserting a tabulator character it should be replaced by a group of space characters.

get_mark_attributes(category: str, priority: int) MarkAttributes

Gets attributes and priority for the category.

Parameters:
  • category – the category.

  • priority – place where priority of the category will be stored.

get_right_margin_position() int

Gets the position of the right margin in the given view.

get_show_line_marks() bool

Returns whether line marks are displayed beside the text.

get_show_line_numbers() bool

Returns whether line numbers are displayed beside the text.

get_show_right_margin() bool

Returns whether a right margin is displayed.

get_smart_backspace() bool

Returns True if pressing the Backspace key will try to delete spaces up to the previous tab stop.

get_smart_home_end() SmartHomeEndType

Returns a SmartHomeEndType end value specifying how the cursor will move when HOME and END keys are pressed.

get_space_drawer() SpaceDrawer

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.

get_tab_width() int

Returns the width of tabulation in characters.

get_visual_column(iter: TextIter) int

Determines the visual column at iter taking into consideration the tab_width of view.

Parameters:

iter – a position in view.

indent_lines(start: TextIter, end: TextIter) None

Inserts one indentation level at the beginning of the specified lines. The empty lines are not indented.

Parameters:
  • startTextIter of the first line to indent

  • endTextIter of the last line to indent

push_snippet(snippet: Snippet, location: TextIter | None = None) None

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 Snippet

  • location – a TextIter or None for the cursor position

set_auto_indent(enable: bool) None

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.

set_background_pattern(background_pattern: BackgroundPatternType) None

Set if and how the background pattern should be displayed.

Parameters:

background_pattern – the BackgroundPatternType.

set_enable_snippets(enable_snippets: bool) None

Sets the enable_snippets property.

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

Parameters:

enable_snippets – if snippets should be enabled

set_highlight_current_line(highlight: bool) None

If highlight is True the current line will be highlighted.

Parameters:

highlight – whether to highlight the current line.

set_indent_on_tab(enable: bool) None

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.

set_indent_width(width: int) None

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 tab_width property will be used.

The indent_width interacts with the insert_spaces_instead_of_tabs property and tab_width. An example will be clearer:

If the indent_width is 4 and tab_width is 8 and 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 tab_width is 8). On the other hand, if 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.

set_indenter(indenter: Indenter | None = None) None

Sets the indenter for view to indenter.

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

Parameters:

indenter – a Indenter or None

set_insert_spaces_instead_of_tabs(enable: bool) None

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.

set_mark_attributes(category: str, attributes: MarkAttributes, priority: int) None

Sets attributes and priority for the category.

Parameters:
  • category – the category.

  • attributes – mark attributes.

  • priority – priority of the category.

set_right_margin_position(pos: int) None

Sets the position of the right margin in the given view.

Parameters:

pos – the width in characters where to position the right margin.

set_show_line_marks(show: bool) None

If True line marks will be displayed beside the text.

Parameters:

show – whether line marks should be displayed.

set_show_line_numbers(show: bool) None

If True line numbers will be displayed beside the text.

Parameters:

show – whether line numbers should be displayed.

set_show_right_margin(show: bool) None

If True a right margin is displayed.

Parameters:

show – whether to show a right margin.

set_smart_backspace(smart_backspace: bool) None

When set to True, pressing the Backspace key will try to delete spaces up to the previous tab stop.

Parameters:

smart_backspace – whether to enable smart Backspace handling.

set_smart_home_end(smart_home_end: SmartHomeEndType) None

Set the desired movement of the cursor when HOME and END keys are pressed.

Parameters:

smart_home_end – the desired behavior among SmartHomeEndType.

set_tab_width(width: int) None

Sets the width of tabulation in characters.

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

Parameters:

width – width of tab in characters.

unindent_lines(start: TextIter, end: TextIter) None

Removes one indentation level at the beginning of the specified lines.

Parameters:
  • startTextIter of the first line to indent

  • endTextIter of the last line to indent

Properties

class View
props.auto_indent: bool
props.background_pattern: BackgroundPatternType

Draw a specific background pattern on the view.

props.completion: Completion

The completion object associated with the view

props.enable_snippets: bool

The property denotes if snippets should be expanded when the user presses Tab after having typed a word matching the snippets found in SnippetManager.

The user may tab through focus-positions of the snippet if any are available by pressing Tab repeatedly until the desired focus position is selected.

props.highlight_current_line: bool
props.indent_on_tab: bool
props.indent_width: int

Width of an indentation step expressed in number of spaces.

props.indenter: Indenter

The property is a Indenter to use to indent as the user types into the View.

props.insert_spaces_instead_of_tabs: bool
props.right_margin_position: int

Position of the right margin.

props.show_line_marks: bool

Whether to display line mark pixbufs

props.show_line_numbers: bool

Whether to display line numbers

props.show_right_margin: bool

Whether to display the right margin.

props.smart_backspace: bool

Whether smart Backspace should be used.

props.smart_home_end: SmartHomeEndType

Set the behavior of the HOME and END keys.

props.space_drawer: SpaceDrawer

The SpaceDrawer object associated with the view.4

props.tab_width: int

Width of a tab character expressed in number of spaces.

Signals

class View.signals
change_case(case_type: ChangeCaseType) None

Keybinding signal to change case of the text at the current cursor position.

Parameters:

case_type – the case to use

change_number(count: int) None

Keybinding signal to edit a number at the current cursor position.

Parameters:

count – the number to add to the number at the current position

join_lines() None

Keybinding signal to join the lines currently selected.

line_mark_activated(iter: TextIter, button: int, state: ModifierType, n_presses: int) None

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:
  • iter – a TextIter

  • button – the button that was pressed

  • state – the modifier state, if any

  • n_presses – the number of presses

move_lines(down: bool) None

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:

downTrue to move down, False to move up.

move_to_matching_bracket(extend_selection: bool) None

Keybinding signal to move the cursor to the matching bracket.

Parameters:

extend_selectionTrue if the move should extend the selection

move_words(count: int) None

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:

count – the number of words to move over

push_snippet(snippet: Snippet) TextIter

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:

snippet – a Snippet

show_completion() None

The signal is a key binding signal which gets emitted when the user requests a completion, by pressing <keycombo><keycap>Control</keycap><keycap>space</keycap></keycombo>.

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

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

smart_home_end(iter: TextIter, count: int) None

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 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:
  • iter – a TextIter

  • count – the count

Virtual Methods

class View
do_line_mark_activated(iter: TextIter, button: int, state: ModifierType, n_presses: int) None
Parameters:
  • iter

  • button

  • state

  • n_presses

do_move_lines(down: bool) None
Parameters:

down

do_move_words(step: int) None
Parameters:

step

do_push_snippet(snippet: Snippet, location: TextIter | None = None) None

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 Snippet

  • location – a TextIter or None for the cursor position

do_show_completion() None

Fields

class View
parent_instance