1 #include <gtk/gtk.h>
2 
3 
4 
5 /*Callback function in which pushes an item onto the statusbar*/
6 static void
push_item(GtkWidget * widget,gpointer data)7 push_item (GtkWidget *widget,
8            gpointer   data)
9 {
10   GtkWidget *status_bar = data;
11 
12   /*Create a context id, which is used to uniquely identify
13    *the source of a message*/
14   guint context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (status_bar),
15                                                    "Statusbar example");
16 
17   /*Count is used to keep track of the amount of items
18   the user is pushing/popping*/
19   static int count = 1;
20   char hold_output[20];
21 
22   /*This is a safer form of the standard sprintf () function. The output is
23   guaranteed in this case to not exceed 20 characters, and the result is stored
24   into the 'hold_output' variable*/
25   g_snprintf (hold_output, 20, "Item %d", count++);
26   gtk_statusbar_push (GTK_STATUSBAR (status_bar),
27                      context_id,
28                      hold_output);
29 }
30 
31 
32 
33 /*Callback function that is used to pop an item off the statusbar*/
34 static void
pop_item(GtkWidget * widget,gpointer data)35 pop_item (GtkWidget *widget,
36           gpointer   data )
37 {
38   GtkWidget *status_bar = data;
39   guint context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (status_bar),
40                                                    "Statusbar example");
41 
42   gtk_statusbar_pop (GTK_STATUSBAR (status_bar), context_id);
43 }
44 
45 
46 
47 static void
activate(GtkApplication * app,gpointer user_data)48 activate (GtkApplication *app,
49           gpointer        user_data)
50 {
51     GtkWidget *grid;
52     GtkWidget *window;
53     GtkWidget *status_bar;
54     GtkWidget *pop_button;
55     GtkWidget *push_button;
56 
57     /*Create a window with a title, border width, and a default size**/
58     window = gtk_application_window_new (app);
59     gtk_window_set_default_size (GTK_WINDOW (window), 220, 100);
60     gtk_window_set_title (GTK_WINDOW (window), "Statusbar Example");
61     gtk_container_set_border_width (GTK_CONTAINER(window), 10);
62 
63     /*Create the status bar, which is held in the global variable*/
64     status_bar = gtk_statusbar_new ();
65 
66     /*Create the buttons with labels*/
67     push_button = gtk_button_new_with_label ("push item");
68     pop_button = gtk_button_new_with_label ("pop last item");
69 
70     /*Create the grid, and attach the buttons/statusbar accordingly*/
71     grid = gtk_grid_new ();
72     gtk_grid_attach (GTK_GRID (grid), push_button, 0,1,1,1);
73     gtk_grid_attach (GTK_GRID (grid), pop_button, 0,2,1,1);
74     gtk_grid_attach (GTK_GRID (grid), status_bar, 0,3,1,1);
75 
76     /*Connecting the clicked signals to the corresponding callback functions*/
77     g_signal_connect (GTK_BUTTON (push_button), "clicked",
78                       G_CALLBACK (push_item), status_bar);
79     g_signal_connect (GTK_BUTTON (pop_button), "clicked",
80                       G_CALLBACK (pop_item), status_bar);
81 
82     /*Attach the grid holding the child widgets onto the window, and show all*/
83     gtk_container_add (GTK_CONTAINER (window), grid);
84     gtk_widget_show_all (window);
85 }
86 
87 
88 
89 int
main(int argc,char ** argv)90 main (int argc, char **argv)
91 {
92   GtkApplication *app;
93   int status;
94 
95   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
96   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
97   status = g_application_run (G_APPLICATION (app), argc, argv);
98   g_object_unref (app);
99 
100   return status;
101 }
102