updating a progress bar in a loop



Hello, I'm pretty new to gtk and I'm trying to add a progress bar widget 
to a loop that monitors a file descriptor.
I've read some gtk code but still haven't been able to get this to work.
I run gtk_init, create the progress bar, add a timeout that starts my 
loop, within the loop I call gtk_progress_bar_update but the display doesn't 
seem to change. The widget appears, but doesn't fully draw and then it 
shows a 100% status and stops. BTW, my timeout function returns false so 
it should only run once. Also, my pointer to the progress bar widget is 
global (for now at least) and I pass arguments needed by the loop thru 
the timeout.
Here are some snippets:

This is the loop that gets called, it just loops, reads from a file 
descriptor, and prints status messages as it goes along. All that part 
works, only the progress_bar stuff doesn't seem to work.

  while (xferring) {
    FD_ZERO(&fdset);
    FD_SET(fd, &fdset);
    t.tv_sec =  200;
    t.tv_usec = 0;
    select_rv = select(fd+1, &fdset, NULL, NULL, &t);
    /*    printf("select_rv is %d\n", select_rv); */
    if (select_rv > 0) {
      if((read_rv = read(fd, &bread, sizeof(int))) > 0) {
	if(bread == -2) { printf("I'm done...\n"); break; }
	bread_tot += bread;
	printf("bread is %d, bread_tot is %ld\n", bread, bread_tot);
	percent = (float) bread_tot / fsize;
	printf("%f = %ld / %ld\n", percent, bread_tot, fsize);
		gtk_progress_bar_update 
	  (GTK_PROGRESS_BAR (pbar), (gfloat) percent); 
      } else if (read_rv <= 0) { 
	xferring = 0; 
	printf("done transferring\n");
      }
    } else if (select_rv <= 0) { 
      xferring = 0;
      printf("select_rv is ltz, %d\n", select_rv);
    }
  }

here is the declaration of the timeout function:

gint progress_timeout (gpointer data) {
  do_loop(data);
  printf("I did some stuff\n");
  return FALSE;
}

here the data is a pointer to a struct that holds info necessary for the loop 
to perform its stuff.

This is an editted version of the main stuff:

GtkWidget *pbar;

int main(int argc, char *argv[]) {
  unsigned long int fsize = 0;
  request_struct *request;
  request = malloc(sizeof(*request));
  strcpy(request->host, argv[1]);
  strcpy(request->path, argv[2]);
  strcpy(request->file, argv[3]);
  gtk_init(&argc, &argv);
  pbar = create_progress_bar();
  gtk_timeout_add(100, (GtkFunction) progress_timeout, request);
  gtk_main();

  return 0;
}

create_progress_bar is almost a direct copy of the one found in 
testgtk.c.

Thanks for any insight,
Chris



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