1 #include <gtk/gtk.h>
2
3
4
5 /*Signal handler for the "toggled" signal of the RadioButton*/
6 static void
button_toggled_cb(GtkWidget * button,gpointer user_data)7 button_toggled_cb (GtkWidget *button,
8 gpointer user_data)
9 {
10 char *b_state;
11 const char *button_label;
12
13 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
14 b_state = "on";
15 else {
16 b_state = "off";
17 g_print ("\n");
18 }
19
20 button_label = gtk_button_get_label (GTK_BUTTON (button));
21
22 g_print ("%s was turned %s\n", button_label, b_state);
23 }
24
25
26
27 static void
activate(GtkApplication * app,gpointer user_data)28 activate (GtkApplication *app,
29 gpointer user_data)
30 {
31 GtkWidget *grid;
32 GtkWidget *window;
33
34 GtkWidget *button1;
35 GtkWidget *button2;
36 GtkWidget *button3;
37
38 /*Create a window with a set title and default size.
39 Also, set a border width for the amount of space to leave
40 inside the window*/
41 window = gtk_application_window_new (app);
42 gtk_window_set_title (GTK_WINDOW (window), "RadioButton Example");
43 gtk_window_set_default_size (GTK_WINDOW (window), 250, 100);
44 gtk_container_set_border_width (GTK_CONTAINER(window), 20);
45
46
47 /*Create an initial radio button*/
48 button1 = gtk_radio_button_new_with_label (NULL, "Button 1");
49
50 /*Create a second radio button, and add it to the same group as Button 1*/
51 button2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button1),
52 "Button 2");
53
54 /*Create a third button, and add it to the same group as Button 1*/
55 button3 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button1),
56 "Button 3");
57
58
59 /*Create a grid, attach the buttons, and position them accordingly*/
60 grid = gtk_grid_new ();
61 gtk_grid_attach (GTK_GRID (grid), button1, 0, 0, 1, 1);
62 gtk_grid_attach (GTK_GRID (grid), button2, 0, 1, 1, 1);
63 gtk_grid_attach (GTK_GRID (grid), button3, 0, 2, 1, 1);
64
65 /*Be sure to set the initial state of each button*/
66 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button2), TRUE);
67 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button1), FALSE);
68 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button3), FALSE);
69
70 /*Connect the signal handlers (aka Callback functions) to the buttons*/
71 g_signal_connect (GTK_TOGGLE_BUTTON (button1), "toggled",
72 G_CALLBACK (button_toggled_cb), window);
73 g_signal_connect (GTK_TOGGLE_BUTTON (button2), "toggled",
74 G_CALLBACK (button_toggled_cb), window);
75 g_signal_connect (GTK_TOGGLE_BUTTON (button3), "toggled",
76 G_CALLBACK (button_toggled_cb), window);
77
78 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
79
80 gtk_widget_show_all (window);
81 }
82
83
84
85 int
main(int argc,char ** argv)86 main (int argc, char **argv)
87 {
88 GtkApplication *app;
89 int status;
90
91 app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
92 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
93 status = g_application_run (G_APPLICATION (app), argc, argv);
94 g_object_unref (app);
95
96 return status;
97 }
98