scrolling logs in GtkTextView to the end (Re: scrolling windows)



On Wednesday 05 March 2003 18:21, Owen Taylor wrote:

A) This is another big FAQ ... apparently tailing logs is something
   everyone wants to do...

   Would also be good if someone wanted to write this up for
   submission.

B) I don't think there is any particular reason to get the
   scrolled window involved.

    gtk_text_buffer_insert()

   followed by:

    gtk_text_view_scroll_to_iter()

   should do the trick. (Maybe Havoc wants to chime in if
   this is the "canonically right" way of doing things.)

I had the same problem: I had a status log window that I always wanted to 
scroll to the end automatically.

I searched gtk-app-devel-list, and I found exactly the suggestion you make (in 
a mail either by you or Havoc, can't remember).

However, this didn't work properly for me. The text view would always scrolled 
only to the second-last line.

It turns out that the solution that works is to hook up an idle timeout that 
is called once and does the scrolling to the end after an insertion. I am not 
sure if that is the right thing to do, but it's the only thing that works 
(for me at least). It also kind-of makes sense given that the docs say this 
about gtk_text_view_scroll_to_iter(): 

"Note that line heights are computed in an idle handler; so this function may 
not have the desired effect if it's called before the height computations."


Here's some code snippets:



/******************************************************************************
 *
 *   status_page_scroll_to_end
 *
 ***/

static gboolean
status_page_scroll_to_end (gpointer data)
{
        GtkTextIter iter;

        gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (viewbuf), &iter);
        gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW(view), &iter, 0, TRUE, 0.0, 1.0);

        return FALSE;
}

/******************************************************************************
 *
 *   status_message
 *
 ***/

static void
status_message (const gchar *msg)
{
   ....
  gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (viewbuf), &end);
  gtk_text_buffer_insert( ... );
  ...

  g_idle_add ( (GSourceFunc) status_page_scroll_to_end, NULL );
}


Cheers
-Tim










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