Getting GtkTextView line number

1.6k Views Asked by At

How can I get a line number from GtkTextBuffer based on the current cursor position? Let us assume that wrap-mode is set and I am in some where in middle of the 3rd line. But how can I get the program to tell me that I am in the 3rd line ?

2

There are 2 best solutions below

0
ptomato On

You can't, because the text buffer doesn't know how big the user has resized the window. The best you can do is to use gtk_text_view_get_iter_location() which gives you a rectangle of the pixel coordinates of an iterator.

You can divide the y coordinate by the height of the rectangle to estimate the line number, but of course that only works if all your lines are the same height.

0
user2109788 On

I know this post is very old, still I'm answering to this. To get the line number you can try the following self.textview.get_iter_at_location(x, y).get_line() But I haven't tried this with wrap-mode.