Is it possible to get a number of physical lines from GtkTextView

Hi, ALL,
Is there a function which will give me this information? I am hoping there is some kind of internal counter that I can query…

Or I have to go and manually count all ‘\n’ characters?

TIA!

GtkTextBuffer *buffer;
GtkTextIter end;
guint n_lines;

buffer = gtk_text_view_get_buffer (text_view);
gtk_text_buffer_get_end_iter (buffer, &end);
n_lines = gtk_text_iter_get_line (&end) + 1;

g_print ("There are %u lines.\n", n_lines);

Hi,
Is this going to count physical or logical lines?
Unfortunately documentation is not clear about that.

Thank you.

What do you consider a “logical line”?

I assume you’re asking whether a wrapped line counts as more than one. If so, the answer is no. A GtkTextBuffer has no idea how wide the view is or whether line wrapping is enabled, and it could even be shared by multiple views.

Also, there is gtk_text_buffer_get_line_count().

Hi, Chris,
I presume those 2 way gives the same results.

And thank you for one shot call. Even though the documentation says the result is cached, I hope that cache is updated when I add the new line, right?

Hi, Chris,
So if I want logical lines (count wrapped lines) - what should I do?

Also, you said that the value is cached. So I presume that this cache will be invalidated every time the user will add new line when (s)he press Enter?

Thank you.

Hi,
Issue filed as suggested.

Thank you.

Hi, to account for wrapped lines is very simple, you just need to get the widget (TextView) allocated height and divide it by the line height. It can be done like this (vala code):

Gtk.TextIter it;
int line_height;

view.buffer.get_start_iter(out it);              // Get iter at first line
view.get_line_yrange(it, null, out line_height); // Get line height

var view_height = view.get_allocated_height();   // Get the textview height
int lines = view_height / line_height;           // result

Beware the result might not be accurate if you specified vertical margins for your TextView

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.