Chapter 12. Gtk2::TextView


This section borrows heavily from the PyGtk tutorial. It gives the reader a decent introduction to the Gtk2::TextView widget. This widget has a more complex model, and it is necessarily to understand it.

12.1. Gtk2::TextView overview

Gtk2::TextView widgets and their associated objects (Gtk2::TextBuffers, Gtk2::TextMarks, Gtk2::TextIters, Gtk2::TextTags and Gtk2::TextTagTables) provide a powerful framework for multi line text editing.

A Gtk2::TextBuffer (see Section 12.3) contains the text which is displayed by one or more Gtk2::TextView widgets.

Within Gtk+, text is encoded in UTF-8, which means that one character may be encoded as multiple bytes. Within a Gtk2::TextBuffer it is necessary to differentiate between the character counts (called offsets) and the byte counts (called indexes).

Gtk2::TextIters provide a volatile representation of the position in a Gtk2::TextBuffer between two characters. Gtk2::TextIters are valid until the number of characters in the Gtk2::TextBuffer changes; i.e. any time characters are inserted or deleted from a Gtk2::TextBuffer all Gtk2::TextIters will become invalid. Gtk2::TextIters are the primary way to specify locations in a Gtk2::TextBuffer for manipulating text.

Gtk2::TextMarks are provided to allow preservation of Gtk2::TextBuffer positions across buffer modifications. A mark is like a Gtk2::TextIter(see Section 12.4) in that it represents a position between two characters in a Gtk2::TextBuffer, but if the text surrounding the mark is deleted, the mark remains where the deleted text once was. Likewise, if text is inserted at the mark, the mark ends up either to the left or right of the inserted text depending on the gravity of the mark - right gravity leaves the mark to the right of the inserted text while left gravity leaves it to the left. Gtk2::TextMarks (see Section 12.5) may be named or anonymous if not given a name. Each Gtk2::TextBuffer has two predefined named Gtk2::TextMarks (see Section 12.5) called insert and selection_bound. These refer to the insertion point and the boundary of the selection (the selection is between the insert and the selection_bound marks).

Gtk2::TextTags (see Section 12.6) are objects that specify a set of attributes that can be applied to a range of text in a Gtk2::TextBuffer. Each Gtk2::TextBuffer has a Gtk2::TextTagTable(see Section 12.6) which contains the tags that are available in that buffer. Gtk2::TextTagTables can be shared between Gtk2::TextBuffers to provide commonality. Gtk2::TextTags are generally used to change the appearance of a range of text but can also be used to prevent a range of text from being edited.