El búfer

Gtk::TextBuffer es el modelo que contiene los datos del Gtk::TextView, al igual que el Gtk::TreeModel usado por Gtk::TreeView. Esto permite a dos o más Gtk::TreeView compartir el mismo TextBuffer, y permite mostrar esos búferes de texto de una manera ligeramente diferente. O bien, puede mantener varios Gtk::TextBuffer y elegir mostrar cada uno en distintas ocasiones en el mismo widget GtK::TextView.

El TextView crea su propio TextBuffer predeterminado, al que puede acceder mediante el método get_buffer().

Referencia

12.1.1. Iteradores

A Gtk::TextBuffer::iterator and a Gtk::TextBuffer::const_iterator represent a position between two characters in the text buffer. Whenever the buffer is modified in a way that affects the number of characters in the buffer, all outstanding iterators become invalid. Because of this, iterators can't be used to preserve positions across buffer modifications. To preserve a position, use Gtk::TextBuffer::Mark.

Referencia

12.1.2. Etiquetas y formateado

12.1.2.1. Etiquetas

Para especificar que algún texto en el búfer deba tener un formato especial, defina una etiqueta que contenta esa información de formato, y luego aplíquela a la región de texto. Por ejemplo, para definir la etiqueta y sus propiedades:

auto refTagMatch = Gtk::TextBuffer::Tag::create();
refTagMatch->property_background() = "orange";

Puede especificar un nombre para la clase Tag cuando use el método create(), pero no es necesario.

La clase Tag tiene muchas otras propiedades.

Referencia

12.1.2.2. TagTable

Cada Gtk::TextBuffer usa una Gtk::TextBuffer::TagTable, que contiene las Tag para ese búfer. Dos o más TextBuffer pueden compartir la misma TagTable. Cuando cree las Tag, añádalas a la TagTable. Por ejemplo:

auto refTagTable = Gtk::TextBuffer::TagTable::create();
refTagTable->add(refTagMatch);
//Hopefully a future version of gtkmm will have a set_tag_table() method,
//for use after creation of the buffer.
auto refBuffer = Gtk::TextBuffer::create(refTagTable);

También puede usar get_tag_table() para obtener, y tal vez modificar, la TagTable predeterminada del TextBuffer en lugar de crear una explícitamente.

Referencia

12.1.2.3. Aplicar etiquetas

Si ha creado una Tag y la ha añadido a la TagTable, podrá aplicarle esa etiqueta a parte del TextBuffer, para que una parte del texto se muestre con ese formato. Puede definir el inicio y el fin del rango de texto especificando Gtk::TextBuffer::iterator. Por ejemplo:

refBuffer->apply_tag(refTagMatch, iterRangeStart, iterRangeStop);

Or you could specify the tag when first inserting the text:

refBuffer->insert_with_tag(iter, "Some text", refTagMatch);

Puede aplicar más de una Tag al mismo texto, usando apply_tag() más de una vez, o usando insert_with_tags(). Las Tag podrían especificar valores diferentes para las mismas propiedades, pero puede resolver estos conflictos usando Tag::set_priority().

12.1.3. Marcas

Generalmente se invalidan los iteradores del TextBuffer cuando el texto cambia, pero puede usar una Gtk::TextBuffer::Mark para recordar una posición en estas situaciones. Por ejemplo,

auto refMark = refBuffer->create_mark(iter);

Puede entonces usar el método get_iter() más tarde para crear un iterador para la posición nueva de la Mark.

Hay dos clases Mark incorporadas: insert y select_bound, a las que puede acceder con los métodos get_insert() and get_selection_bound() de TextBuffer.

Referencia

12.1.4. La vista

Como se mencionó anteriormente, cada TextView tiene un TextBuffer, y uno o más TextView pueden compartir el mismo TextBuffer.

Al igual que con el TreeView, probablemente deba poner su TextView dentro de una ScrolledWindow para permitirle al usuario ver y mover toda el área de texto con barras de desplazamiento.

Referencia

12.1.4.1. Formato predeterminado

Los TextView tienen varios métodos que le permiten cambiar la presentación del búfer de esta vista particular. Algunos de estos pueden anularse por los Gtk::TextTag en el búfer, si especifican las mismas cosas. Por ejemplo, set_left_margin(), set_right_margin(), set_indent(), etc.

12.1.4.2. Desplazamiento

Gtk::TextView has various scroll_to() methods. These allow you to ensure that a particular part of the text buffer is visible. For instance, your application's Find feature might use Gtk::TextView::scroll_to() to show the found text.