I'm using Gtk.TextView to display text, like this:

My goal is to programmatically move the cursor to the start of the next display line (not the start of a buffer line, but instead the first character in the next TextView line).
It is using Gtk.WrapMode.WORD_CHAR as a wrapping preference. Moving TextIter objects works fine when accessed from the TextBuffer:
/* This works as expected */
Gtk.TextIter start_iter;
buffer.get_start_iter (out start_iter);
Or when working directly with the TextIter:
/* This also works as expected */
Gtk.TextIter iter;
...
iter.forward_line ();
But when I want to move the TextIter using TextView, e.g.:
Gtk.TextView text_view;
Gtk.TextIter iter;
...
text_view.forward_display_line (iter);
It doesn't move the iterator, despite forward_display_line returning true (signaling that the iterator has moved).
This is the code I'm using (inside of a class derived from TextView):
Gtk.TextIter line_end;
/* Initializing TextIter, placing it at the start of the buffer. */
this.buffer.get_start_iter (out line_end);
print ("Offset before forwarding: %i\n", line_end.get_offset ()); //returns 0
this.forward_display_line (line_end);
print ("Offset after forwarding: %i\n", line_end.get_offset ()); // still returns 0
Why doesn't this code work? Are TextIters initialized by TextBuffer not appropriate for TextView methods?
In your code you invoke the Gtk.TextBuffer forward_display_line() method which "consumes" a Gtk.TextIter but does not output this same "Iter" with the changes performed by the method. Both calls to Gtk.TextIter get_offset () should return the same value.
Instead, if you use the Gtk.TextIter forward_line (), then the changes should and would be reflected in the second print, e.g.:
Gtk.TextIter line_end;
/* Initializing TextIter, placing it at the start of the buffer. */
this.buffer.get_start_iter (out line_end);
print ("Offset before forwarding: %i\n", line_end.get_offset ()); //returns 0
line_end.forward_line ();
print ("Offset after forwarding: %i\n", line_end.get_offset ()); // should be different from 0 if buffer has some text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With