View#
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_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 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
Bufferwill be lazily created and can be retrieved withget_buffer.If you want to specify your own buffer, either override the
create_bufferfactory method, or usenew_with_buffer.
Methods#
- class View
- do_line_mark_activated(self, iter: TextIter, button: int, state: ModifierType, n_presses: int) None#
- Parameters:
iter
button
state
n_presses
- do_push_snippet(self, snippet: Snippet, location: TextIter | None = None) None#
- Parameters:
snippet
location
- get_annotations() Annotations#
Gets the
Annotationsassociated withview.The returned object is guaranteed to be the same for the lifetime of
view. EachViewobject has a differentAnnotations.Added in version 5.18.
- get_background_pattern() BackgroundPatternType#
Returns the
BackgroundPatternTypespecifying if and how the background pattern should be displayed for thisview.
- get_completion() Completion#
Gets the
Completionassociated withview.The returned object is guaranteed to be the same for the lifetime of
view. EachGtkSourceViewobject has a differentCompletion.
- get_enable_snippets() bool#
Gets the
enable_snippetsproperty.If
True, matching snippets found in theSnippetManagermay be expanded when the user presses Tab after a word in theView.
- get_gutter(window_type: TextWindowType) Gutter#
Returns the
Gutterobject associated withwindow_typeforview.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_hover() Hover#
Gets the
Hoverassociated withview.The returned object is guaranteed to be the same for the lifetime of
view. EachViewobject has a differentHover.
- get_indent_on_tab() bool#
Returns whether when the tab key is pressed the current selection should get indented instead of replaced with the
\tcharacter.
- get_indent_width() int#
Returns the number of spaces to use for each step of indent.
See
set_indent_widthfor details.
- 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_smart_backspace() bool#
Returns
Trueif pressing the Backspace key will try to delete spaces up to the previous tab stop.
- get_smart_home_end() SmartHomeEndType#
Returns a
SmartHomeEndTypeend value specifying how the cursor will move when HOME and END keys are pressed.
- get_space_drawer() SpaceDrawer#
Gets the
SpaceDrawerassociated withview.The returned object is guaranteed to be the same for the lifetime of
view. EachViewobject has a differentSpaceDrawer.
- get_visual_column(iter: TextIter) int#
Determines the visual column at
itertaking into consideration thetab_widthofview.- 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.
- push_snippet(snippet: Snippet, location: TextIter | None = None) None#
Inserts a new snippet at
locationIf another snippet was already active, it will be paused and the new snippet will become active. Once the focus positions of
snippethave been exhausted, editing will return to the previous snippet.
- set_auto_indent(enable: bool) None#
If
Trueauto-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_snippetsproperty.If
enable_snippetsisTrue, matching snippets found in theSnippetManagermay be expanded when the user presses Tab after a word in theView.- Parameters:
enable_snippets – if snippets should be enabled
- set_highlight_current_line(highlight: bool) None#
If
highlightisTruethe 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\tcharacter. 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
\tcharacter.- 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
widthis -1, the value of thetab_widthproperty will be used.The
indent_widthinteracts with theinsert_spaces_instead_of_tabsproperty andtab_width. An example will be clearer:If the
indent_widthis 4 andtab_widthis 8 andinsert_spaces_instead_of_tabsisFalse, 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\tcharacter instead (sincetab_widthis 8). On the other hand, ifinsert_spaces_instead_of_tabsisTrue, the second tab key pressed will insert 4 more spaces for a total of 8 spaces in theTextBuffer.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
viewtoindenter.Note that the indenter will not be used unless
View:auto-indent has been set toTrue.- Parameters:
indenter – a
IndenterorNone
- set_insert_spaces_instead_of_tabs(enable: bool) None#
If
Truea tab key pressed is replaced by a group of space characters.Of course it is still possible to insert a real
\tprogrammatically with theTextBufferAPI.- 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
Trueline marks will be displayed beside the text.- Parameters:
show – whether line marks should be displayed.
- set_show_line_numbers(show: bool) None#
If
Trueline numbers will be displayed beside the text.- Parameters:
show – whether line numbers should be displayed.
- set_show_right_margin(show: bool) None#
If
Truea 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
TextBufferstill contains\tcharacters, but they can take a different visual width in aViewwidget.- Parameters:
width – width of tab in characters.
Properties#
- class View
- props.annotations: Annotations#
The
Annotationsobject associated with the view.Added in version 5.18.
- 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.indenter: Indenter#
The property is a
Indenterto use to indent as the user types into theView.
- props.smart_home_end: SmartHomeEndType#
Set the behavior of the HOME and END keys.
- props.space_drawer: SpaceDrawer#
The
SpaceDrawerobject associated with the view.
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
- 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
iterto determine on which line the activation took place.- Parameters:
iter – a
TextIterbutton – 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:
down –
Trueto move down,Falseto move up.
- move_to_matching_bracket(extend_selection: bool) None#
Keybinding signal to move the cursor to the matching bracket.
- Parameters:
extend_selection –
Trueif 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, location: TextIter) 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
snippethave been exhausted.locationwill be updated to point at the end of the 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
CompletionContextwith the activation type asUSER_REQUESTED.Applications should not connect to it, but may emit it with
signal_emit_by_nameif 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_cursoraction. 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
TextItercount – 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_push_snippet(snippet: Snippet, location: TextIter | None = None) None#
Inserts a new snippet at
locationIf another snippet was already active, it will be paused and the new snippet will become active. Once the focus positions of
snippethave been exhausted, editing will return to the previous snippet.
Fields#
- class View
- parent_instance#