Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward_display_line doesn't work

Tags:

gtk

vala

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

TextView with wrapping

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?

like image 308
Aleksandar Stefanović Avatar asked Jan 18 '26 17:01

Aleksandar Stefanović


1 Answers

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
like image 179
José Fonte Avatar answered Jan 21 '26 07:01

José Fonte