1 #include <gtk/gtk.h>
2 
3 
4 static gboolean
fill(gpointer user_data)5 fill (gpointer   user_data)
6 {
7   GtkWidget *progress_bar = user_data;
8 
9   /*Get the current progress*/
10   gdouble fraction;
11   fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));
12 
13   /*Increase the bar by 10% each time this function is called*/
14   fraction += 0.1;
15 
16   /*Fill in the bar with the new fraction*/
17   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
18 
19   /*Ensures that the fraction stays below 1.0*/
20   if (fraction < 1.0)
21     return TRUE;
22 
23   return FALSE;
24 }
25 
26 
27 
28 static void
activate(GtkApplication * app,gpointer user_data)29 activate (GtkApplication *app,
30           gpointer        user_data)
31 {
32   GtkWidget *window;
33   GtkWidget *progress_bar;
34 
35   gdouble fraction = 0.0;
36 
37   /*Create a window with a title, and a default size*/
38   window = gtk_application_window_new (app);
39   gtk_window_set_title (GTK_WINDOW (window), "ProgressBar Example");
40   gtk_window_set_default_size (GTK_WINDOW (window), 220, 20);
41 
42   /*Create a progressbar and add it to the window*/
43   progress_bar = gtk_progress_bar_new ();
44   gtk_container_add (GTK_CONTAINER (window), progress_bar);
45 
46   /*Fill in the given fraction of the bar. Has to be between 0.0-1.0 inclusive*/
47   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
48 
49   /*Use the created fill function every 500 milliseconds*/
50   g_timeout_add (500, fill, GTK_PROGRESS_BAR (progress_bar));
51 
52   gtk_widget_show_all (window);
53 }
54 
55 
56 
57 int
main(int argc,char ** argv)58 main (int argc, char **argv)
59 {
60   GtkApplication *app;
61   int status;
62 
63   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
64   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
65   status = g_application_run (G_APPLICATION (app), argc, argv);
66   g_object_unref (app);
67 
68   return status;
69 }
70