Blame platform-demos/ca/combobox.vala.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="combobox.vala" xml:lang="ca">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">ComboBox (Vala)</title>
Packit 1470ea
    <link type="guide" xref="beginner.vala#menu-combo-toolbar"/>
Packit 1470ea
    <revision version="0.1" date="2012-05-07" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Tiffany Antopolski</name>
Packit 1470ea
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A widget used to choose from a list of items</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>ComboBox</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/combobox.png"/>
Packit 1470ea
  

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

Packit 1470ea
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
}
Packit 1470ea
Packit 1470ea

Packit 1470ea
  In this sample we used the following:
Packit 1470ea

Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://www.valadoc.org/gtk+-3.0/Gtk.ListStore.html">Gtk.ListStore</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.valadoc.org/gtk+-3.0/Gtk.ComboBox.html">Gtk.ComboBox</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.valadoc.org/gtk+-3.0/Gtk.CellRendererText.html">Gtk.CellRendererText</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.valadoc.org/gtk+-3.0/Gtk.CellLayout.set_attributes.html">set_attributes</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
</page>