1 #include <gtk/gtk.h>
2
3 /* This is the callback function. It is a handler function which reacts to the
4 * signal. In this case, if the row selected is not the first one of the
5 * ComboBox, we write its value in the terminal for the user.
6 */
7 static void
on_changed(GtkComboBox * widget,gpointer user_data)8 on_changed (GtkComboBox *widget,
9 gpointer user_data)
10 {
11 GtkComboBox *combo_box = widget;
12
13 if (gtk_combo_box_get_active (combo_box) != 0) {
14 gchar *distro = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT(combo_box));
15 g_print ("You chose %s\n", distro);
16 g_free (distro);
17 }
18
19 }
20
21
22 static void
activate(GtkApplication * app,gpointer user_data)23 activate (GtkApplication *app,
24 gpointer user_data)
25 {
26 gint i;
27 GtkWidget *view;
28 GtkWidget *window;
29 GtkWidget *combo_box;
30
31 /* Create a window with a title, border width, and a default size. Setting the
32 * size to -1 means to use the "natural" default size.
33 * (the size request of the window)
34 */
35 window = gtk_application_window_new (app);
36 gtk_window_set_title (GTK_WINDOW (window), "Welcome to GNOME");
37 gtk_window_set_default_size (GTK_WINDOW (window), 200, -1);
38 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
39
40
41 /* Create the combo box and append your string values to it. */
42 combo_box = gtk_combo_box_text_new ();
43 const char *distros[] = {"Select distribution", "Fedora", "Mint", "Suse"};
44
45
46 /* G_N_ELEMENTS is a macro which determines the number of elements in an array.*/
47 for (i = 0; i < G_N_ELEMENTS (distros); i++){
48 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), distros[i]);
49 }
50
51 /* Choose to set the first row as the active one by default, from the beginning */
52 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
53
54 /* Connect the signal emitted when a row is selected to the appropriate
55 * callback function.
56 */
57 g_signal_connect (combo_box,
58 "changed",
59 G_CALLBACK (on_changed),
60 NULL);
61
62 /* Add it to the window */
63 gtk_container_add (GTK_CONTAINER (window), combo_box);
64
65 gtk_widget_show_all (window);
66 }
67
68
69 int
main(int argc,char ** argv)70 main (int argc, char **argv)
71 {
72 GtkApplication *app;
73 int status;
74
75 app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
76 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
77 status = g_application_run (G_APPLICATION (app), argc, argv);
78 g_object_unref (app);
79
80 return status;
81 }
82