Re: [gtk-list] Re: Q: automatic function on program startup



In article <349AD406.34918B4@redestb.es>
eduperez@redestb.es writes:

> Eduardo Perez wrote:
> 
> Takashi Matsuda wrote:
[...]

> > Your need seems like the credit window which some commercial apprications show
> > at the beginning of execution.
> > Following code can do that. Is this fit your need?
> >
> > [...]
> 
> Many thanks for your interest, but I'm afraid this is not what I'm
> looking for.
>
[...]

Then, the following is it?


                                  -Takashi Matsuda
                                   matsu@arch.comp.kyutech.ac.jp

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

typedef struct
{
  GtkWidget	*window, *progressbar;
  gint		calc_func_tag, update_func_tag, data, limit;
} CalcData;

void quit_func (GtkWidget *widget, gpointer data)
{
  gtk_exit (0);
}

gint destroy_func (gpointer data)
{
  CalcData *cdata;
  GtkWidget *second, *button;

  cdata = (CalcData *) data;

  gtk_widget_destroy (cdata->window);
  g_free (cdata);

  second = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  button = gtk_button_new_with_label ("QUIT");
  gtk_container_add (GTK_CONTAINER(second), button);
  gtk_signal_connect (GTK_OBJECT(button), "clicked",
		      GTK_SIGNAL_FUNC(quit_func), NULL);

  gtk_widget_show (button);
  gtk_widget_show (second);

  return FALSE;
}  

gint periodic_update_func (gpointer data)
{
  CalcData *cdata;

  cdata = (CalcData *) data;

  gtk_progress_bar_update (GTK_PROGRESS_BAR(cdata->progressbar), 
  			   (gfloat)(cdata->data) / cdata->limit);
  return TRUE;
}

gint calculate_func (gpointer data)
{
  CalcData *cdata;

  cdata = (CalcData *) data;
  
  /* this meens calculation */
  cdata->data ++;
  usleep (500*1000);
  
  if (cdata->data >= cdata->limit)
    {
      /* calculation finished */
      if (cdata->update_func_tag)
	gtk_timeout_remove (cdata->update_func_tag);
      cdata->calc_func_tag = 0;
      cdata->update_func_tag = 0;
      periodic_update_func (data);
      gtk_idle_add (destroy_func, data);
      
      return FALSE;
    }

  return TRUE;
}

int main (int argc, char **argv)
{
  GtkWidget *first, *progressbar;
  CalcData *cdata;

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  cdata = g_new (CalcData, 1);
  
  first = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  progressbar = gtk_progress_bar_new ();
  gtk_container_add (GTK_CONTAINER(first), progressbar);

  gtk_widget_show (progressbar);
  gtk_widget_show (first);

  cdata->window = first;
  cdata->progressbar = progressbar;
  cdata->calc_func_tag = gtk_idle_add (calculate_func, cdata);
  cdata->update_func_tag = gtk_timeout_add (500, periodic_update_func, cdata);
  cdata->data = 0;
  cdata->limit = 20;

  periodic_update_func (cdata);
  
  gtk_main ();

  return 0;
}



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