Scrolling to a line in GtkTextView



Hi,

In my application, I would like to have a GtkTextView be scrolled to a
particular line after the GtkTextBuffer is loaded.

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


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