Re: Scrolling to a line in GtkTextView





On 02/12/2019 12:47 PM, Mitko Haralanov via gtk-app-devel-list wrote:
Hi,

In my application, I would like to have a GtkTextView be scrolled to a
particular line after the GtkTextBuffer is loaded.
This was already discussed https://stackoverflow.com/questions/48934458/gtk-sourceview-scroll-to-mark-not-working.

Basically, the GUI calculations are not done and the lines aren't calculated, therefore you need to scroll after the GUI rendering is finished. Use idle_add or timeout_add.

According to the documentation, I can use gtk_text_view_scroll_to_mark() in
order to reliably scroll to the desired line after the line height
computation has been completed. In practice that doesn't work reliably.
Below is the code snippet that I am using:


while (delay++ < 100)
g_main_context_iteration(NULL, FALSE);
debug("Scrolling to line: %lu", lineno);
gtk_text_buffer_get_iter_at_line(GTK_TEXT_BUFFER(priv->sourceBuf),
&iter, (gint)(lineno - 1));
debug("Line: %d, offset: %d", gtk_text_iter_get_line(&iter),
       gtk_text_iter_get_offset(&iter));
gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(priv->sourceBuf), &iter);
mark = gtk_text_buffer_get_mark(GTK_TEXT_BUFFER(priv->sourceBuf),
"result-view-search");
gtk_text_buffer_move_mark(GTK_TEXT_BUFFER(priv->sourceBuf), mark,
   &iter);
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(priv->sourceView), mark, 0.0,
      TRUE, 0.0, 0.5);

What I am noticing is that the reliability of this highly depends on the
line number to which I want to scroll and the length of the busy loop in
the beginning - the higher the line number, the longer the busy loop needs
to be in order to reliably scroll to that line.

If the busy loop is not long enough, the text view does scroll but not the
correct line.

What is the proper way to correctly and reliably scroll a GtkTextView to a
certain line?

Thank you,
Mitko
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]