GtkAdjustment question



I am working on porting an app from gtk 1.2 over to 2.0. I am using the 
TextView widget to display log messages to the user, and as new messages 
appear, I would automatically like the window to scroll. But, if the user goes 
to scroll up in the text view, I don't want it to scroll down automatically.

The problem that I ran into if I display more than a page full of text in the
text view before I enter the main loop, it won't automatically scroll. I've
included a test program that inserts text on startup, and then every 1.5
seconds, it will insert more text.

#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>

GtkAdjustment * logwdw_vadj;
GtkWidget * logwdw;

static void log 				( char * message );
static gint insert_text ( gpointer data );

int
main (int argc, char **argv)
{
  GtkWidget * window, * sw;
  char buf[20];
  int i;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
  gtk_widget_set_size_request (window, 400, 100);

  logwdw = gtk_text_view_new ();
  gtk_text_view_set_editable (GTK_TEXT_VIEW (logwdw), FALSE);
  gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (logwdw), FALSE);
  gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (logwdw), GTK_WRAP_WORD);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                 GTK_POLICY_AUTOMATIC,
                                 GTK_POLICY_AUTOMATIC);
  logwdw_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
  gtk_container_add (GTK_CONTAINER (sw), logwdw);

  gtk_container_add (GTK_CONTAINER (window), sw);

  gtk_widget_show_all (window);

  /* Change this to only 1 or 2 iterations, and it will work properly */
  for (i=0; i<20; i++)
    {
      g_snprintf (buf, sizeof (buf), "Message %d\n", i);
      log (buf);
    }

  gtk_timeout_add (1500, insert_text, NULL);
  gtk_main ();
  return (0);
}


static void
log (char * message)
{
  GtkTextBuffer * textbuf;
  GtkTextIter iter;
  guint len;
  int upd;

  upd = logwdw_vadj->upper - logwdw_vadj->page_size == logwdw_vadj->value;
  
  printf ("upd %d, lower %.2f, upper %.2f, value %.2f, step_increment %.2f, page_increment %.2f, page_size %.2f\n", upd, logwdw_vadj->lower, logwdw_vadj->upper, logwdw_vadj->value, logwdw_vadj->step_increment, logwdw_vadj->page_increment, logwdw_vadj->page_size);

  textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (logwdw));
  len = gtk_text_buffer_get_char_count (textbuf);
  gtk_text_buffer_get_iter_at_offset (textbuf, &iter, len);
  gtk_text_buffer_insert (textbuf, &iter, message, -1);

  if (upd)
    gtk_adjustment_set_value (logwdw_vadj, logwdw_vadj->upper);
}


static gint
insert_text (gpointer data)
{
  char buf[20];
  int i;

  for (i=0; i<5; i++)
    {
      g_snprintf (buf, sizeof (buf), "Message %d\n", i);
      log (buf);
    }

  gtk_timeout_add (1500, insert_text, NULL);
  return (0);
}





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