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

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
}