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.

Hi, ALL,
I got an answer here, but unfortunately the code is using non-existing function.

Moreover I think that I will get an incorrect results if my text will be smaller than the height of the control.

Any idea on how can I get an accurate results for number of logical lines in GtkTextView?

TIA!

As before, it’s not clear what “logical line” means to you. To count the number of display lines, you should be able to use something like this (untested):

GtkTextBuffer *buffer;
GtkTextIter iter;
int n_display_lines = 1;

buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
gtk_text_buffer_get_start_iter (buffer, &iter);

while (gtk_text_view_forward_display_line (GTK_TEXT_VIEW (text_view), &iter))
    n_display_lines++;

I would consider that to be “physical” lines, and gtk_text_buffer_get_line_count() to be “logical” lines.

That’s backwards to how most people use those words. We say that two “physical” lines are separated by a newline character – in a buffer. Two “logical” lines are what we see on two rows – on a display. Sometimes, we use the word “virtual” similarly to how we use “logical”.

So … he is saying that his “physical” is buffer lines; and, his “logical” is screen lines.