TextView#
Superclasses: Widget
, InitiallyUnowned
, Object
Implemented Interfaces: Accessible
, AccessibleText
, Buildable
, ConstraintTarget
, Scrollable
A widget that displays the contents of a TextBuffer
.
You may wish to begin by reading the conceptual overview, which gives an overview of all the objects and data types related to the text widget and how they work together.
Shortcuts and Gestures#
GtkTextView
supports the following keyboard shortcuts:
Shift`+:kbd:`F10 or Menu opens the context menu.
Ctrl`+:kbd:`Z undoes the last modification.
Ctrl`+:kbd:`Y or Ctrl`+:kbd:`Shift`+:kbd:`Z redoes the last undone modification.
Additionally, the following signals have default keybindings:
Actions#
GtkTextView
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.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.
CSS nodes#
textview.view
├── border.top
├── border.left
├── text
│ ╰── [selection]
├── border.right
├── border.bottom
╰── [window.popup]
GtkTextView
has a main css node with name textview and style class .view,
and subnodes for each of the border windows, and the main text area,
with names border and text, respectively. The border nodes each get
one of the style classes .left, .right, .top or .bottom.
A node representing the selection will appear below the text node.
If a context menu is opened, the window node will appear as a subnode of the main node.
Accessibility#
GtkTextView
uses the TEXT_BOX
role.
Constructors#
- class TextView
- classmethod new() Widget #
Creates a new
GtkTextView
.If you don’t call
set_buffer
before using the text view, an empty default buffer will be created for you. Get the buffer withget_buffer
. If you want to specify your own buffer, considernew_with_buffer
.
- classmethod new_with_buffer(buffer: TextBuffer) Widget #
Creates a new
GtkTextView
widget displaying the bufferbuffer
.One buffer can be shared among many widgets.
buffer
may beNone
to create a default buffer, in which case this function is equivalent tonew
. The text view adds its own reference count to the buffer; it does not take over an existing reference.- Parameters:
buffer – a
GtkTextBuffer
Methods#
- class TextView
- add_child_at_anchor(child: Widget, anchor: TextChildAnchor) None #
Adds a child widget in the text buffer, at the given
anchor
.- Parameters:
child – a
GtkWidget
anchor – a
GtkTextChildAnchor
in theGtkTextBuffer
fortext_view
- add_overlay(child: Widget, xpos: int, ypos: int) None #
Adds
child
at a fixed coordinate in theGtkTextView
’s text window.The
xpos
andypos
must be in buffer coordinates (seeget_iter_location
to convert to buffer coordinates).child
will scroll with the text view.If instead you want a widget that will not move with the
GtkTextView
contents seeGtkOverlay
.- Parameters:
child – a
GtkWidget
xpos – X position of child in window coordinates
ypos – Y position of child in window coordinates
- backward_display_line(iter: TextIter) bool #
Moves the given
iter
backward by one display (wrapped) line.A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the
GtkTextBuffer
.- Parameters:
iter – a
GtkTextIter
- backward_display_line_start(iter: TextIter) bool #
Moves the given
iter
backward to the next display line start.A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the
GtkTextBuffer
.- Parameters:
iter – a
GtkTextIter
- buffer_to_window_coords(win: TextWindowType, buffer_x: int, buffer_y: int) tuple[int, int] #
Converts buffer coordinates to window coordinates.
- Parameters:
win – a
GtkTextWindowType
buffer_x – buffer x coordinate
buffer_y – buffer y coordinate
- forward_display_line(iter: TextIter) bool #
Moves the given
iter
forward by one display (wrapped) line.A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the
GtkTextBuffer
.- Parameters:
iter – a
GtkTextIter
- forward_display_line_end(iter: TextIter) bool #
Moves the given
iter
forward to the next display line end.A display line is different from a paragraph. Paragraphs are separated by newlines or other paragraph separator characters. Display lines are created by line-wrapping a paragraph. If wrapping is turned off, display lines and paragraphs will be the same. Display lines are divided differently for each view, since they depend on the view’s width; paragraphs are the same in all views, since they depend on the contents of the
GtkTextBuffer
.- Parameters:
iter – a
GtkTextIter
- get_accepts_tab() bool #
Returns whether pressing the Tab key inserts a tab characters.
See
set_accepts_tab
.
- get_buffer() TextBuffer #
Returns the
GtkTextBuffer
being displayed by this text view.The reference count on the buffer is not incremented; the caller of this function won’t own a new reference.
- get_cursor_locations(iter: TextIter | None = None) tuple[Rectangle, Rectangle] #
Determine the positions of the strong and weak cursors if the insertion point is at
iter
.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 of the paragraph are inserted. The weak cursor location is the location where characters of the directionality opposite to the base direction of the paragraph are inserted.
If
iter
isNone
, the actual cursor position is used.Note that if
iter
happens to be the actual cursor position, and there is currently an IM preedit sequence being entered, the returned locations will be adjusted to account for the preedit cursor’s offset within the preedit sequence.The rectangle position is in buffer coordinates; use
buffer_to_window_coords
to convert these coordinates to coordinates for one of the windows in the text view.- Parameters:
iter – a
GtkTextIter
- get_editable() bool #
Returns the default editability of the
GtkTextView
.Tags in the buffer may override this setting for some ranges of text.
Gets the menu model that gets added to the context menu or
None
if none has been set.
- get_gutter(win: TextWindowType) Widget | None #
Gets a
GtkWidget
that has previously been set as gutter.See
set_gutter
.win
must be one ofLEFT
,RIGHT
,TOP
, orBOTTOM
.- Parameters:
win – a
GtkTextWindowType
- get_indent() int #
Gets the default indentation of paragraphs in
text_view
.Tags in the view’s buffer may override the default. The indentation may be negative.
- get_input_hints() InputHints #
Gets the
input-hints
of theGtkTextView
.
- get_input_purpose() InputPurpose #
Gets the
input-purpose
of theGtkTextView
.
- get_iter_at_location(x: int, y: int) tuple[bool, TextIter] #
Retrieves the iterator at buffer coordinates
x
andy
.Buffer coordinates are coordinates for the entire buffer, not just the currently-displayed portion. If you have coordinates from an event, you have to convert those to buffer coordinates with
window_to_buffer_coords
.- Parameters:
x – x position, in buffer coordinates
y – y position, in buffer coordinates
- get_iter_at_position(x: int, y: int) tuple[bool, TextIter, int] #
Retrieves the iterator pointing to the character at buffer coordinates
x
andy
.Buffer coordinates are coordinates for the entire buffer, not just the currently-displayed portion. If you have coordinates from an event, you have to convert those to buffer coordinates with
window_to_buffer_coords
.Note that this is different from
get_iter_at_location
, which returns cursor locations, i.e. positions between characters.- Parameters:
x – x position, in buffer coordinates
y – y position, in buffer coordinates
- get_iter_location(iter: TextIter) Rectangle #
Gets a rectangle which roughly contains the character at
iter
.The rectangle position is in buffer coordinates; use
buffer_to_window_coords
to convert these coordinates to coordinates for one of the windows in the text view.- Parameters:
iter – a
GtkTextIter
- get_justification() Justification #
Gets the default justification of paragraphs in
text_view
.Tags in the buffer may override the default.
- get_left_margin() int #
Gets the default left margin size of paragraphs in the
text_view
.Tags in the buffer may override the default.
- get_line_at_y(y: int) tuple[TextIter, int] #
Gets the
GtkTextIter
at the start of the line containing the coordinatey
.y
is in buffer coordinates, convert from window coordinates withwindow_to_buffer_coords
. If non-None
,line_top
will be filled with the coordinate of the top edge of the line.- Parameters:
y – a y coordinate
- get_line_yrange(iter: TextIter) tuple[int, int] #
Gets the y coordinate of the top of the line containing
iter
, and the height of the line.The coordinate is a buffer coordinate; convert to window coordinates with
buffer_to_window_coords
.- Parameters:
iter – a
GtkTextIter
- get_ltr_context() Context #
Gets the
PangoContext
that is used for rendering LTR directed text layouts.The context may be replaced when CSS changes occur.
Added in version 4.4.
- get_pixels_above_lines() int #
Gets the default number of pixels to put above paragraphs.
Adding this function with
get_pixels_below_lines
is equal to the line space between each paragraph.
- get_pixels_below_lines() int #
Gets the default number of pixels to put below paragraphs.
The line space is the sum of the value returned by this function and the value returned by
get_pixels_above_lines
.
- get_pixels_inside_wrap() int #
Gets the default number of pixels to put between wrapped lines inside a paragraph.
- get_right_margin() int #
Gets the default right margin for text in
text_view
.Tags in the buffer may override the default.
- get_rtl_context() Context #
Gets the
PangoContext
that is used for rendering RTL directed text layouts.The context may be replaced when CSS changes occur.
Added in version 4.4.
- get_tabs() TabArray | None #
Gets the default tabs for
text_view
.Tags in the buffer may override the defaults. The returned array will be
None
if “standard” (8-space) tabs are used. Free the return value withfree
.
- get_visible_rect() Rectangle #
Fills
visible_rect
with the currently-visible region of the buffer, in buffer coordinates.Convert to window coordinates with
buffer_to_window_coords
.
- im_context_filter_keypress(event: Event) bool #
Allow the
GtkTextView
input method to internally handle key press and release events.If this function returns
True
, then no further processing should be done for this key event. Seefilter_keypress
.Note that you are expected to call this function from your handler when overriding key event handling. This is needed in the case when you need to insert your own key handling between the input method and the default key event handling of the
GtkTextView
.static gboolean gtk_foo_bar_key_press_event (GtkWidget *widget, GdkEvent *event) { guint keyval; gdk_event_get_keyval ((GdkEvent*)event, &keyval); if (keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter) { if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (widget), event)) return TRUE; } // Do some stuff return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event); }
- Parameters:
event – the key event
- move_mark_onscreen(mark: TextMark) bool #
Moves a mark within the buffer so that it’s located within the currently-visible text area.
- Parameters:
mark – a
GtkTextMark
- move_overlay(child: Widget, xpos: int, ypos: int) None #
Updates the position of a child.
See
add_overlay
.- Parameters:
child – a widget already added with
add_overlay
xpos – new X position in buffer coordinates
ypos – new Y position in buffer coordinates
- move_visually(iter: TextIter, count: int) bool #
Move the iterator a given number of characters visually, treating it as the strong cursor position.
If
count
is positive, then the new strong cursor position will becount
positions to the right of the old cursor position. Ifcount
is negative then the new strong cursor position will becount
positions to the left of the old cursor position.In the presence of bi-directional text, the correspondence between logical and visual order will depend on the direction of the current run, and there may be jumps when the cursor is moved off of the end of a run.
- Parameters:
iter – a
GtkTextIter
count – number of characters to move (negative moves left, positive moves right)
- remove(child: Widget) None #
Removes a child widget from
text_view
.- Parameters:
child – the child to remove
- reset_cursor_blink() None #
Ensures that the cursor is shown.
This also resets the time that it will stay blinking (or visible, in case blinking is disabled).
This function should be called in response to user input (e.g. from derived classes that override the textview’s event handlers).
- reset_im_context() None #
Reset the input method context of the text view if needed.
This can be necessary in the case where modifying the buffer would confuse on-going input method behavior.
- scroll_mark_onscreen(mark: TextMark) None #
Scrolls
text_view
the minimum distance such thatmark
is contained within the visible area of the widget.- Parameters:
mark – a mark in the buffer for
text_view
- scroll_to_iter(iter: TextIter, within_margin: float, use_align: bool, xalign: float, yalign: float) bool #
Scrolls
text_view
so thatiter
is on the screen in the position indicated byxalign
andyalign
.An alignment of 0.0 indicates left or top, 1.0 indicates right or bottom, 0.5 means center. If
use_align
isFalse
, the text scrolls the minimal distance to get the mark onscreen, possibly not scrolling at all. The effective screen for purposes of this function is reduced by a margin of sizewithin_margin
.Note that this function uses the currently-computed height of the lines in the text buffer. Line heights are computed in an idle handler; so this function may not have the desired effect if it’s called before the height computations. To avoid oddness, consider using
scroll_to_mark
which saves a point to be scrolled to after line validation.- Parameters:
iter – a
GtkTextIter
within_margin – margin as a [0.0,0.5) fraction of screen size
use_align – whether to use alignment arguments (if
False
, just get the mark onscreen)xalign – horizontal alignment of mark within visible area
yalign – vertical alignment of mark within visible area
- scroll_to_mark(mark: TextMark, within_margin: float, use_align: bool, xalign: float, yalign: float) None #
Scrolls
text_view
so thatmark
is on the screen in the position indicated byxalign
andyalign
.An alignment of 0.0 indicates left or top, 1.0 indicates right or bottom, 0.5 means center. If
use_align
isFalse
, the text scrolls the minimal distance to get the mark onscreen, possibly not scrolling at all. The effective screen for purposes of this function is reduced by a margin of sizewithin_margin
.- Parameters:
mark – a
GtkTextMark
within_margin – margin as a [0.0,0.5) fraction of screen size
use_align – whether to use alignment arguments (if
False
, just get the mark onscreen)xalign – horizontal alignment of mark within visible area
yalign – vertical alignment of mark within visible area
- set_accepts_tab(accepts_tab: bool) None #
Sets the behavior of the text widget when the Tab key is pressed.
If
accepts_tab
isTrue
, a tab character is inserted. Ifaccepts_tab
isFalse
the keyboard focus is moved to the next widget in the focus chain.Focus can always be moved using Ctrl`+:kbd:`Tab.
- Parameters:
accepts_tab –
True
if pressing the Tab key should insert a tab character,False
, if pressing the Tab key should move the keyboard focus.
- set_bottom_margin(bottom_margin: int) None #
Sets the bottom margin for text in
text_view
.Note that this function is confusingly named. In CSS terms, the value set here is padding.
- Parameters:
bottom_margin – bottom margin in pixels
- set_buffer(buffer: TextBuffer | None = None) None #
Sets
buffer
as the buffer being displayed bytext_view
.The previous buffer displayed by the text view is unreferenced, and a reference is added to
buffer
. If you owned a reference tobuffer
before passing it to this function, you must remove that reference yourself;GtkTextView
will not “adopt” it.- Parameters:
buffer – a
GtkTextBuffer
- set_cursor_visible(setting: bool) None #
Toggles whether the insertion point should be displayed.
A buffer with no editable text probably shouldn’t have a visible cursor, so you may want to turn the cursor off.
Note that this property may be overridden by the
gtk_keynav_use_caret
setting.- Parameters:
setting – whether to show the insertion cursor
- set_editable(setting: bool) None #
Sets the default editability of the
GtkTextView
.You can override this default setting with tags in the buffer, using the “editable” attribute of tags.
- Parameters:
setting – whether it’s editable
Sets a menu model to add when constructing the context menu for
text_view
.You can pass
None
to remove a previously set extra menu.- Parameters:
model – a
GMenuModel
- set_gutter(win: TextWindowType, widget: Widget | None = None) None #
Places
widget
into the gutter specified bywin
.win
must be one ofLEFT
,RIGHT
,TOP
, orBOTTOM
.- Parameters:
win – a
GtkTextWindowType
widget – a
GtkWidget
- set_indent(indent: int) None #
Sets the default indentation for paragraphs in
text_view
.Tags in the buffer may override the default.
- Parameters:
indent – indentation in pixels
- set_input_hints(hints: InputHints) None #
Sets the
input-hints
of theGtkTextView
.The
input-hints
allow input methods to fine-tune their behaviour.- Parameters:
hints – the hints
- set_input_purpose(purpose: InputPurpose) None #
Sets the
input-purpose
of theGtkTextView
.The
input-purpose
can be used by on-screen keyboards and other input methods to adjust their behaviour.- Parameters:
purpose – the purpose
- set_justification(justification: Justification) None #
Sets the default justification of text in
text_view
.Tags in the view’s buffer may override the default.
- Parameters:
justification – justification
- set_left_margin(left_margin: int) None #
Sets the default left margin for text in
text_view
.Tags in the buffer may override the default.
Note that this function is confusingly named. In CSS terms, the value set here is padding.
- Parameters:
left_margin – left margin in pixels
- set_monospace(monospace: bool) None #
Sets whether the
GtkTextView
should display text in monospace styling.- Parameters:
monospace –
True
to request monospace styling
- set_overwrite(overwrite: bool) None #
Changes the
GtkTextView
overwrite mode.- Parameters:
overwrite –
True
to turn on overwrite mode,False
to turn it off
- set_pixels_above_lines(pixels_above_lines: int) None #
Sets the default number of blank pixels above paragraphs in
text_view
.Tags in the buffer for
text_view
may override the defaults.- Parameters:
pixels_above_lines – pixels above paragraphs
- set_pixels_below_lines(pixels_below_lines: int) None #
Sets the default number of pixels of blank space to put below paragraphs in
text_view
.May be overridden by tags applied to
text_view
’s buffer.- Parameters:
pixels_below_lines – pixels below paragraphs
- set_pixels_inside_wrap(pixels_inside_wrap: int) None #
Sets the default number of pixels of blank space to leave between display/wrapped lines within a paragraph.
May be overridden by tags in
text_view
’s buffer.- Parameters:
pixels_inside_wrap – default number of pixels between wrapped lines
- set_right_margin(right_margin: int) None #
Sets the default right margin for text in the text view.
Tags in the buffer may override the default.
Note that this function is confusingly named. In CSS terms, the value set here is padding.
- Parameters:
right_margin – right margin in pixels
- set_tabs(tabs: TabArray) None #
Sets the default tab stops for paragraphs in
text_view
.Tags in the buffer may override the default.
- Parameters:
tabs – tabs as a
PangoTabArray
- set_top_margin(top_margin: int) None #
Sets the top margin for text in
text_view
.Note that this function is confusingly named. In CSS terms, the value set here is padding.
- Parameters:
top_margin – top margin in pixels
- set_wrap_mode(wrap_mode: WrapMode) None #
Sets the line wrapping for the view.
- Parameters:
wrap_mode – a
GtkWrapMode
- starts_display_line(iter: TextIter) bool #
Determines whether
iter
is at the start of a display line.See
forward_display_line
for an explanation of display lines vs. paragraphs.- Parameters:
iter – a
GtkTextIter
Properties#
- class TextView
-
- props.buffer: TextBuffer#
The type of the None singleton.
The type of the None singleton.
- props.input_hints: InputHints#
The type of the None singleton.
- props.input_purpose: InputPurpose#
The type of the None singleton.
- props.justification: Justification#
The type of the None singleton.
Signals#
- class TextView.signals
-
- delete_from_cursor(type: DeleteType, count: int) None #
The type of the None singleton.
- Parameters:
type – the granularity of the deletion, as a
GtkDeleteType
count – the number of
type
units to delete
- extend_selection(granularity: TextExtendSelection, location: TextIter, start: TextIter, end: TextIter) bool #
The type of the None singleton.
- Parameters:
granularity – the granularity type
location – the location where to extend the selection
start – where the selection should start
end – where the selection should end
- insert_at_cursor(string: str) None #
The type of the None singleton.
- Parameters:
string – the string to insert
- move_cursor(step: MovementStep, count: int, extend_selection: bool) None #
The type of the None singleton.
- Parameters:
step – the granularity of the move, as a
GtkMovementStep
count – the number of
step
units to moveextend_selection –
True
if the move should extend the selection
- move_viewport(step: ScrollStep, count: int) None #
The type of the None singleton.
- Parameters:
step – the granularity of the movement, as a
GtkScrollStep
count – the number of
step
units to move
- preedit_changed(preedit: str) None #
The type of the None singleton.
- Parameters:
preedit – the current preedit string
Virtual Methods#
- class TextView
-
- do_delete_from_cursor(type: DeleteType, count: int) None #
The type of the None singleton.
- Parameters:
type
count
- do_extend_selection(granularity: TextExtendSelection, location: TextIter, start: TextIter, end: TextIter) bool #
The type of the None singleton.
- Parameters:
granularity
location
start
end
- do_move_cursor(step: MovementStep, count: int, extend_selection: bool) None #
The type of the None singleton.
- Parameters:
step
count
extend_selection
- do_snapshot_layer(layer: TextViewLayer, snapshot: Snapshot) None #
The type of the None singleton.
- Parameters:
layer
snapshot