1 #include <gtk/gtk.h>
2
3
4
5 /*This is the callback function. It is a handler function
6 which reacts to the signal. In this case, it will cause the
7 spinner to start and stop according to how many times the user
8 presses the button.*/
9 static void
button_toggled_cb(GtkWidget * button,gpointer user_data)10 button_toggled_cb (GtkWidget *button,
11 gpointer user_data)
12 {
13 GtkWidget *spinner = user_data;
14
15 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button)))
16 gtk_spinner_start (GTK_SPINNER (spinner));
17 else {
18 gtk_spinner_stop (GTK_SPINNER (spinner));
19 }
20 }
21
22
23
24 static void
activate(GtkApplication * app,gpointer user_data)25 activate (GtkApplication *app,
26 gpointer user_data)
27 {
28 GtkWidget *window;
29 GtkWidget *button;
30 GtkWidget *grid;
31 GtkWidget *spinner;
32
33 /*Create a window with a title, border width and a default size*/
34 window = gtk_application_window_new (app);
35 gtk_window_set_title (GTK_WINDOW (window), "ToggleButton Example");
36 gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
37 gtk_container_set_border_width(GTK_CONTAINER(window), 30);
38
39 /*Create a togglebutton with a label*/
40 button = gtk_toggle_button_new_with_label ("Start/Stop");
41
42 /*Create a spinner, with extra horizontal and vertical space*/
43 spinner = gtk_spinner_new ();
44 gtk_widget_set_hexpand (spinner, TRUE);
45 gtk_widget_set_vexpand (spinner, TRUE);
46
47 /*Create a grid and set the row spacing, attach the togglebutton
48 and spinner onto the grid and position them accordingly*/
49 grid = gtk_grid_new();
50 gtk_grid_set_row_homogeneous (GTK_GRID (grid), FALSE);
51 gtk_grid_set_row_spacing (GTK_GRID (grid), 15);
52 gtk_grid_attach (GTK_GRID (grid), spinner, 0, 0, 1, 1);
53 gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
54
55 gtk_container_add (GTK_CONTAINER (window), grid);
56
57 /*Connecting the toggled signal to the callback*/
58 g_signal_connect (GTK_TOGGLE_BUTTON (button), "toggled",
59 G_CALLBACK (button_toggled_cb), spinner);
60
61 gtk_widget_show_all (window);
62 }
63
64
65
66 int
main(int argc,char ** argv)67 main (int argc, char **argv)
68 {
69 GtkApplication *app;
70 int status;
71
72 app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
73 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
74 status = g_application_run (G_APPLICATION (app), argc, argv);
75 g_object_unref (app);
76
77 return status;
78 }
79