| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Text View |
| 4 |
========= |
| 5 |
|
| 6 |
The :class:`Gtk.TextView` widget can be used to display and edit large amounts |
| 7 |
of formatted text. Like the :class:`Gtk.ListView`, it has a model/view |
| 8 |
design. |
| 9 |
In this case the :class:`Gtk.TextBuffer` is the model which represents the text |
| 10 |
being edited. This allows two or more :class:`Gtk.TextView` widgets to share the |
| 11 |
same :class:`Gtk.TextBuffer`, and allows those text buffers to be displayed |
| 12 |
slightly differently. Or you could maintain several text buffers and choose to |
| 13 |
display each one at different times in the same :class:`Gtk.TextView` widget. |
| 14 |
|
| 15 |
.. seealso:: `Text Widget Overview`_ in the GTK documentation. |
| 16 |
|
| 17 |
The View |
| 18 |
-------- |
| 19 |
|
| 20 |
The :class:`Gtk.TextView` is the frontend with which the user can add, edit and |
| 21 |
delete textual data. They are commonly used to edit multiple lines of text. |
| 22 |
When creating a :class:`Gtk.TextView` it contains its own default |
| 23 |
:class:`Gtk.TextBuffer`, which is kept in the :attr:`Gtk.TextView.props.buffer` |
| 24 |
property. |
| 25 |
|
| 26 |
By default, text can be added, edited and removed from the :class:`Gtk.TextView`. |
| 27 |
You can disable this by changing :attr:`Gtk.TextView.props.editable`. |
| 28 |
If the text is not editable, you usually want to hide the text cursor with |
| 29 |
:attr:`Gtk.TextView.props.cursor_visible` as well. |
| 30 |
In some cases it may be useful to set the justification of the text with |
| 31 |
:attr:`Gtk.TextView.props.justification`. |
| 32 |
The text can be displayed at the left edge, (:attr:`Gtk.Justification.LEFT`), |
| 33 |
at the right edge (:attr:`Gtk.Justification.RIGHT`), centered |
| 34 |
(:attr:`Gtk.Justification.CENTER`), or distributed across the complete |
| 35 |
width (:attr:`Gtk.Justification.FILL`). |
| 36 |
|
| 37 |
Another default setting of the :class:`Gtk.TextView` widget is long lines of |
| 38 |
text will continue horizontally until a break is entered. To wrap the text and |
| 39 |
prevent it going off the edges of the screen set |
| 40 |
:attr:`Gtk.TextView.props.wrap_mode` similar to :doc:`/tutorials/gtk4/display-widgets/label`. |
| 41 |
|
| 42 |
The Model |
| 43 |
--------- |
| 44 |
|
| 45 |
The :class:`Gtk.TextBuffer` is the core of the :class:`Gtk.TextView` widget, and |
| 46 |
is used to hold whatever text is being displayed in the :class:`Gtk.TextView`. |
| 47 |
Setting and retrieving the contents is possible with |
| 48 |
:attr:`Gtk.TextBuffer.props.text`. |
| 49 |
However, most text manipulation is accomplished with *iterators*, represented by |
| 50 |
a :class:`Gtk.TextIter`. An iterator represents a position between two |
| 51 |
characters in the text buffer. |
| 52 |
Iterators are not valid indefinitely; whenever the buffer is modified in a way |
| 53 |
that affects the contents of the buffer, all outstanding iterators become |
| 54 |
invalid. |
| 55 |
|
| 56 |
Because of this, iterators can't be used to preserve positions across buffer |
| 57 |
modifications. To preserve a position, use :class:`Gtk.TextMark`. |
| 58 |
A text buffer contains two built-in marks; an "insert" mark (which is the |
| 59 |
position of the cursor) and the "selection_bound" mark. Both of them can be |
| 60 |
retrieved using :meth:`Gtk.TextBuffer.get_insert` and |
| 61 |
:meth:`Gtk.TextBuffer.get_selection_bound`, respectively. |
| 62 |
By default, the location of a :class:`Gtk.TextMark` is not shown. |
| 63 |
This can be changed by calling :meth:`Gtk.TextMark.set_visible`. |
| 64 |
|
| 65 |
Many methods exist to retrieve a :class:`Gtk.TextIter`. For instance, |
| 66 |
:meth:`Gtk.TextBuffer.get_start_iter` returns an iterator pointing to the first |
| 67 |
position in the text buffer, whereas :meth:`Gtk.TextBuffer.get_end_iter` returns |
| 68 |
an iterator pointing past the last valid character. Retrieving the bounds of |
| 69 |
the selected text can be achieved by calling |
| 70 |
:meth:`Gtk.TextBuffer.get_selection_bounds`. |
| 71 |
|
| 72 |
To insert text at a specific position use :meth:`Gtk.TextBuffer.insert`. |
| 73 |
Another useful method is :meth:`Gtk.TextBuffer.insert_at_cursor` which inserts |
| 74 |
text wherever the cursor may be currently positioned. To remove portions of |
| 75 |
the text buffer use :meth:`Gtk.TextBuffer.delete`. |
| 76 |
|
| 77 |
In addition, :class:`Gtk.TextIter` can be used to locate textual matches in the |
| 78 |
buffer using :meth:`Gtk.TextIter.forward_search` and |
| 79 |
:meth:`Gtk.TextIter.backward_search`. |
| 80 |
The start and end iters are used as the starting point of the search and move |
| 81 |
forwards/backwards depending on requirements. |
| 82 |
|
| 83 |
Tags |
| 84 |
---- |
| 85 |
|
| 86 |
Text in a buffer can be marked with tags. |
| 87 |
A tag is an attribute that can be applied to some range of text. For example, a |
| 88 |
tag might be called "bold" and make the text inside the tag bold. |
| 89 |
However, the tag concept is more general than that; tags don't have to affect |
| 90 |
appearance. |
| 91 |
They can instead affect the behavior of mouse and key presses, "lock" a range of |
| 92 |
text so the user can't edit it, or countless other things. A tag is represented |
| 93 |
by a :class:`Gtk.TextTag` object. |
| 94 |
One :class:`Gtk.TextTag` can be applied to any number of text ranges in any |
| 95 |
number of buffers. |
| 96 |
|
| 97 |
Each tag is stored in a :class:`Gtk.TextTagTable`. |
| 98 |
A tag table defines a set of tags that can be used together. |
| 99 |
Each buffer has one tag table associated with it; only tags from that tag table |
| 100 |
can be used with the buffer. |
| 101 |
A single tag table can be shared between multiple buffers, however. |
| 102 |
|
| 103 |
To specify that some text in the buffer should have specific formatting, you |
| 104 |
must define a tag to hold that formatting information, and then apply that tag |
| 105 |
to the region of text using :meth:`Gtk.TextBuffer.create_tag` and |
| 106 |
:meth:`Gtk.TextBuffer.apply_tag`: |
| 107 |
|
| 108 |
.. code:: python |
| 109 |
|
| 110 |
tag = textbuffer.create_tag('orange_bg', background='orange') |
| 111 |
textbuffer.apply_tag(tag, start_iter, end_iter) |
| 112 |
|
| 113 |
The following are some of the common styles applied to text: |
| 114 |
|
| 115 |
* Background colour ("background" property) |
| 116 |
* Foreground colour ("foreground" property) |
| 117 |
* Underline ("underline" property) |
| 118 |
* Bold ("weight" property) |
| 119 |
* Italics ("style" property) |
| 120 |
* Strikethrough ("strikethrough" property) |
| 121 |
* Justification ("justification" property) |
| 122 |
* Size ("size" and "size-points" properties) |
| 123 |
* Text wrapping ("wrap-mode" property) |
| 124 |
|
| 125 |
You can also delete particular tags later using :meth:`Gtk.TextBuffer.remove_tag` |
| 126 |
or delete all tags in a given region by calling :meth:`Gtk.TextBuffer.remove_all_tags`. |
| 127 |
|
| 128 |
Example |
| 129 |
------- |
| 130 |
|
| 131 |
.. image:: images/textview.png |
| 132 |
|
| 133 |
.. literalinclude:: examples/textview.py |
| 134 |
:linenos: |
| 135 |
|
| 136 |
|
| 137 |
.. _Text Widget Overview: https://docs.gtk.org/gtk4/section-text-widget.html |