ComboBox

This ComboBox prints to the terminal when you change your selection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* A window in the application */
class MyWindow : Gtk.ApplicationWindow {

	/* An instance array of linux distributions belonging to this window. */
	string[] distros = {"Select distribution", "Fedora", "Mint", "Suse"};

	/* This enum makes the code more readable when we refer to
	 * the column as Column.DISTRO, instead of just 0.
	 */
	enum Column {
		DISTRO
	}

	/* Constructor */
	internal MyWindow (MyApplication app) {
		Object (application: app, title: "Welcome to GNOME");

		this.set_default_size (200, -1);
		this.border_width = 10;

		Gtk.ListStore liststore = new Gtk.ListStore (1, typeof (string));

		for (int i = 0; i < distros.length; i++){
			Gtk.TreeIter iter;
			liststore.append (out iter);
			liststore.set (iter, Column.DISTRO, distros[i]);
		}

		Gtk.ComboBox combobox = new Gtk.ComboBox.with_model (liststore);
		Gtk.CellRendererText cell = new Gtk.CellRendererText ();
		combobox.pack_start (cell, false);

		combobox.set_attributes (cell, "text", Column.DISTRO);

		/* Set the first item in the list to be selected (active). */
		combobox.set_active (0);

		/* Connect the 'changed' signal of the combobox
		 * to the signal handler (aka. callback function).
		 */
		combobox.changed.connect (this.item_changed);

		/* Add the combobox to this window */
		this.add (combobox);
		combobox.show ();
	}

	/* Signal handler for the 'changed' signal of the combobox. */
	void item_changed (Gtk.ComboBox combo) {
		if (combo.get_active () !=0) {
			print ("You chose " + distros [combo.get_active ()] +"\n");
		}
	}
}

/* This is the application */
class MyApplication : Gtk.Application {

	/* Constructor */
	internal MyApplication () {
		Object (application_id: "org.example.MyApplication");
	}

	/* Override the activate signal of GLib.Application,
	 * which is inherited by Gtk.Application.
	 */
	protected override void activate () {

		/* Create the window of this application
		 * and show it.
		 */
		new MyWindow (this).show ();
	}
}

/* main creates and runs the application */
int main (string[] args) {
	return new MyApplication ().run (args);
}

In this sample we used the following: