Blame platform-demos/C/samples/combobox.vala

Packit 1470ea
/* A window in the application */
Packit 1470ea
class MyWindow : Gtk.ApplicationWindow {
Packit 1470ea
Packit 1470ea
	/* An instance array of linux distributions belonging to this window. */
Packit 1470ea
	string[] distros = {"Select distribution", "Fedora", "Mint", "Suse"};
Packit 1470ea
Packit 1470ea
	/* This enum makes the code more readable when we refer to
Packit 1470ea
	 * the column as Column.DISTRO, instead of just 0.
Packit 1470ea
	 */
Packit 1470ea
	enum Column {
Packit 1470ea
		DISTRO
Packit 1470ea
	}
Packit 1470ea
Packit 1470ea
	/* Constructor */
Packit 1470ea
	internal MyWindow (MyApplication app) {
Packit 1470ea
		Object (application: app, title: "Welcome to GNOME");
Packit 1470ea
Packit 1470ea
		this.set_default_size (200, -1);
Packit 1470ea
		this.border_width = 10;
Packit 1470ea
Packit 1470ea
		Gtk.ListStore liststore = new Gtk.ListStore (1, typeof (string));
Packit 1470ea
Packit 1470ea
		for (int i = 0; i < distros.length; i++){
Packit 1470ea
			Gtk.TreeIter iter;
Packit 1470ea
			liststore.append (out iter);
Packit 1470ea
			liststore.set (iter, Column.DISTRO, distros[i]);
Packit 1470ea
		}
Packit 1470ea
Packit 1470ea
		Gtk.ComboBox combobox = new Gtk.ComboBox.with_model (liststore);
Packit 1470ea
		Gtk.CellRendererText cell = new Gtk.CellRendererText ();
Packit 1470ea
		combobox.pack_start (cell, false);
Packit 1470ea
Packit 1470ea
		combobox.set_attributes (cell, "text", Column.DISTRO);
Packit 1470ea
Packit 1470ea
		/* Set the first item in the list to be selected (active). */
Packit 1470ea
		combobox.set_active (0);
Packit 1470ea
Packit 1470ea
		/* Connect the 'changed' signal of the combobox
Packit 1470ea
		 * to the signal handler (aka. callback function).
Packit 1470ea
		 */
Packit 1470ea
		combobox.changed.connect (this.item_changed);
Packit 1470ea
Packit 1470ea
		/* Add the combobox to this window */
Packit 1470ea
		this.add (combobox);
Packit 1470ea
		combobox.show ();
Packit 1470ea
	}
Packit 1470ea
Packit 1470ea
	/* Signal handler for the 'changed' signal of the combobox. */
Packit 1470ea
	void item_changed (Gtk.ComboBox combo) {
Packit 1470ea
		if (combo.get_active () !=0) {
Packit 1470ea
			print ("You chose " + distros [combo.get_active ()] +"\n");
Packit 1470ea
		}
Packit 1470ea
	}
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
/* This is the application */
Packit 1470ea
class MyApplication : Gtk.Application {
Packit 1470ea
Packit 1470ea
	/* Constructor */
Packit 1470ea
	internal MyApplication () {
Packit 1470ea
		Object (application_id: "org.example.MyApplication");
Packit 1470ea
	}
Packit 1470ea
Packit 1470ea
	/* Override the activate signal of GLib.Application,
Packit 1470ea
	 * which is inherited by Gtk.Application.
Packit 1470ea
	 */
Packit 1470ea
	protected override void activate () {
Packit 1470ea
Packit 1470ea
		/* Create the window of this application
Packit 1470ea
		 * and show it.
Packit 1470ea
		 */
Packit 1470ea
		new MyWindow (this).show ();
Packit 1470ea
	}
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
/* main creates and runs the application */
Packit 1470ea
int main (string[] args) {
Packit 1470ea
	return new MyApplication ().run (args);
Packit 1470ea
}