To specify that some text in the buffer should have specific formatting, you must define a tag to hold that formatting information, and then apply that tag to the region of text. For instance, to define the tag and it's properties:
Glib::RefPtr<Gtk::TextBuffer::Tag> refTagMatch = Gtk::TextBuffer::Tag::create();
refTagMatch->property_background() = "orange";
You can specify a name for the
Tag when using the
create() method, but it is not necessary.
The Tag class has many other properties.
Reference
Each Gtk::TextBuffer uses a Gtk::TextBuffer::TagTable, which contains the Tags for that buffer. 2 or more TextBuffer may share the same TagTable. When you create Tags you should add them to the TagTable. For instance:
Glib::RefPtr<Gtk::TextBuffer::TagTable> refTagTable = Gtk::TextBuffer::TagTable::create();
refTagTable->add(refTagMatch);
Glib::RefPtr<Gtk::TextBuffer> refBuffer = Gtk::TextBuffer::create(refTagTable); //Hopefully a future version of gtkmm will have a set_tag_table() method, for use after creation of the buffer.
You can also use get_tag_table to get, and maybe modify, the TextBuffer's default TagTable instead of creating one explicitly.
Reference
If you have created a Tag and added it to the TagTable, you may apply that tag to part of the TextBuffer so that some of the text is displayed with that formatting. You define the start and end of the range of text by specifying Gtk::TextBuffer::iterators. For instance:
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);
You can apply more than one Tag to the same text, by using apply_tag() more than once, or by using insert_with_tags(). The Tags might specify different values for the same properties, but you can resolve these conflicts by using Tag::set_priority().
TextBuffer iterators are generally invalidated when the text changes, but you can use a Gtk::TextBuffer::Mark to remember a position in these situations. For instance,
Glib::RefPtr<Gtk::TextBuffer::Mark> refMark = refBuffer->create_mark(iter);
You can then use the get_iter() method later to create an iterator for the Mark's new position.
There are two built-in Marks - insert and select_bound, which you can access with TextBuffer's get_insert() and get_selection_bound() methods.