Blame platform-demos/gl/combobox_multicolumn.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_multicolumn.vala" xml:lang="gl">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">ComboBox (Vala)</title>
Packit 1470ea
    <link type="guide" xref="beginner.vala#menu-combo-toolbar"/>
Packit 1470ea
    <link type="seealso" xref="combobox.vala"/>
Packit 1470ea
    <revision version="0.1" date="2013-06-18" status="review"/>
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>2013</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A widget used to choose from a list of items</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Fran Dieguez</mal:name>
Packit 1470ea
      <mal:email>frandieguez@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2012-2013.</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>ComboBox (two columns)</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/combobox_multicolumn.png"/>
Packit 1470ea
  

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

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Código usado para xerar este exemplo</title>
Packit 1470ea
    class MyWindow : Gtk.ApplicationWindow {
Packit 1470ea
Packit 1470ea
	string[] file = {"Select", "New", "Open", "Save"};
Packit 1470ea
	string[] stock_item = {"","gtk-new", "gtk-open", "gtk-save"};
Packit 1470ea
Packit 1470ea
	enum Column {
Packit 1470ea
		FILE,
Packit 1470ea
		STOCK_ITEM	
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 (2, typeof (string), typeof (string));
Packit 1470ea
Packit 1470ea
		for (int i = 0; i < file.length; i++){
Packit 1470ea
			Gtk.TreeIter iter;
Packit 1470ea
			liststore.append (out iter);
Packit 1470ea
			liststore.set (iter, Column.FILE, file[i]);
Packit 1470ea
			liststore.set (iter, Column.STOCK_ITEM, stock_item[i]);
Packit 1470ea
		}
Packit 1470ea
Packit 1470ea
		Gtk.ComboBox combobox = new Gtk.ComboBox.with_model (liststore);
Packit 1470ea
Packit 1470ea
		/* CellRenderers render the data. */
Packit 1470ea
		Gtk.CellRendererText cell = new Gtk.CellRendererText ();
Packit 1470ea
		Gtk.CellRendererPixbuf cell_pb = new Gtk.CellRendererPixbuf ();
Packit 1470ea
		
Packit 1470ea
                /* we pack the cell into the beginning of the combobox, allocating
Packit 1470ea
		 * no more space than needed;
Packit 1470ea
		 * first the image, then the text;
Packit 1470ea
		 * note that it does not matter in which order they are in the model,
Packit 1470ea
		 * the visualization is decided by the order of the cellrenderers
Packit 1470ea
		 */
Packit 1470ea
		combobox.pack_start (cell_pb, false);
Packit 1470ea
		combobox.pack_start (cell, false);
Packit 1470ea
Packit 1470ea
		/* associate a property of the cellrenderer to a column in the model
Packit 1470ea
		 * used by the combobox
Packit 1470ea
		 */
Packit 1470ea
		combobox.set_attributes (cell_pb, "stock_id", Column.STOCK_ITEM);
Packit 1470ea
		combobox.set_attributes (cell, "text", Column.FILE);
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
	void item_changed (Gtk.ComboBox combo) {
Packit 1470ea
		if (combo.get_active () !=0) {
Packit 1470ea
			print ("You chose " + file [combo.get_active ()] +"\n");
Packit 1470ea
		}
Packit 1470ea
	}
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
class MyApplication : Gtk.Application {
Packit 1470ea
        protected override void activate () {
Packit 1470ea
                new MyWindow (this).show ();
Packit 1470ea
        }
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
int main (string[] args) {
Packit 1470ea
	return new MyApplication ().run (args);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API References</title>
Packit 1470ea
    

Neste exemplo empregaremos o seguinte:

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

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

</item>
Packit 1470ea
      <item>

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

</item>
Packit 1470ea
      <item>

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

</item>
Packit 1470ea
      <item>

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

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">Stock Items</link>

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