How can I select text in Gtk::TextView
:
- starting from where the cursor is
n
number of characters backwards
The documentation from developer.gnome.org
doesn't seem to help.
The selection isn't done in the Gtk::TextView
itself but in the associated Gtk::TextBuffer
. While I'm not sure why exactly this design choice was done I'm at least clear about the consequence: Selections may be shared between multiple Gtk::TextView
s when they share the same buffer. This may be desirable or not but it's how “they” have done it.
The buffer of a Gtk::TextView
can be obtained with
Glib::RefPtr< TextBuffer > get_buffer ()
Returns the Gtk::TextBuffer 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.
Then, the Gtk::TextBuffer
provides
void Gtk::TextBuffer::select_range (const iterator& ins, const iterator& bound)
This function moves the “insert” and “selection_bound” marks simultaneously.
If you move them in two steps with
move_mark()
, you will temporarily select a region in between their old and new locations, which can be pretty inefficient since the temporarily-selected region will force stuff to be recalculated. This function moves them as a unit, which can be optimized.
ins
Where to put the “insert” mark.
bound
Where to put the “selection_bound” mark.
The current cursor position can be obtained with
Glib::RefPtr Gtk::TextBuffer::get_insert()
Returns the mark that represents the cursor (insertion point).
Equivalent to calling get_mark() to get the mark named “insert”, but very slightly more efficient, and involves less typing.
The returned Gtk::TextMark
can be “converted” to a Gtk::TextIter
by using
TextIter Gtk::TextMark::get_iter().
Additionally, Gtk::TextBuffer
provides a variety of get_iter_at
functions to get the Gtk::TextBuffer::iterator
s for distinct parameters.
A note in general:
To learn a powerful widget API by the reference manual, is something I would consider as tedious.
In the case of gtkmm, there is a serious alternative:
(which is available in other languages as well).
Chapter 11 is about TextView and might help to get the “big picture”.