1public class PhoneBookEntry {
2	public string firstname;
3	public string lastname;
4	public string phone;
5
6	public PhoneBookEntry (string f, string l, string p) {
7		this.firstname = f;
8		this.lastname = l;
9		this.phone = p;
10	}
11}
12
13class TreeViewSimpleListStore : Gtk.ApplicationWindow {
14
15	Gtk.Label label;
16
17	PhoneBookEntry[] phonebook = {
18		new PhoneBookEntry ("Jurg", "Billeter", "555-0123"),
19		new PhoneBookEntry ("Johannes", "Schmid", "555-1234"),
20		new PhoneBookEntry ("Julita", "Inca", "555-2345"),
21		new PhoneBookEntry ("Javier", "Jardon", "555-3456"),
22		new PhoneBookEntry ("Jason", "Clinton", "555-4567"),
23		new PhoneBookEntry ("Random J.", "Hacker", "555-5678")
24	};
25
26	enum Column {
27		FIRSTNAME,
28		LASTNAME,
29		PHONE
30	}
31
32	internal TreeViewSimpleListStore (MyApplication app) {
33		Object (application: app, title: "My Phone Book");
34
35		this.set_default_size (250, 100);
36		this.border_width = 10;
37
38		var view = new Gtk.TreeView ();
39		this.setup_treeview (view);
40		view.expand = true;
41
42		label = new Gtk.Label ("");
43
44		var grid = new Gtk.Grid ();
45
46		grid.attach (view, 0, 0, 1, 1);
47		grid.attach (label, 0, 1, 1, 1);
48		this.add (grid);
49
50		var selection = view.get_selection ();
51		selection.changed.connect (this.on_changed);
52	}
53
54	void setup_treeview (Gtk.TreeView view) {
55		var listmodel = new Gtk.ListStore (3, typeof (string),
56                                              typeof (string),
57                                              typeof (string));
58		view.set_model (listmodel);
59
60		var cell = new Gtk.CellRendererText ();
61
62		/* 'weight' refers to font boldness.
63		 *  400 is normal.
64		 *  700 is bold.
65		 */
66		cell.set ("weight_set", true);
67		cell.set ("weight", 700);
68
69		/*columns*/
70		view.insert_column_with_attributes (-1, "First Name",
71                                                cell, "text",
72                                                Column.FIRSTNAME);
73
74		view.insert_column_with_attributes (-1, "Last Name",
75                                                new Gtk.CellRendererText (),
76                                                "text", Column.LASTNAME);
77
78		view.insert_column_with_attributes (-1, "Phone Number",
79                                                new Gtk.CellRendererText (),
80                                                "text", Column.PHONE);
81
82		/* Insert the phonebook into the ListStore */
83		Gtk.TreeIter iter;
84		for (int i = 0; i < phonebook.length; i++) {
85			listmodel.append (out iter);
86			listmodel.set (iter, Column.FIRSTNAME,
87                                 phonebook[i].firstname,
88                                 Column.LASTNAME, phonebook[i].lastname,
89                                 Column.PHONE, phonebook[i].phone);
90		}
91	}
92
93	void on_changed (Gtk.TreeSelection selection) {
94		Gtk.TreeModel model;
95		Gtk.TreeIter iter;
96		string name;
97		string lastname;
98		string phone;
99
100		if (selection.get_selected (out model, out iter)) {
101			model.get (iter,
102                                   Column.FIRSTNAME, out name,
103                                   Column.LASTNAME, out lastname,
104                                   Column.PHONE, out phone);
105
106			label.set_text ("\n" + name + " " + lastname + " " + phone);
107		}
108	}
109}
110
111class MyApplication : Gtk.Application {
112	protected override void activate () {
113
114		/* Create new Window and show all the things. */
115		new TreeViewSimpleListStore (this).show_all ();
116	}
117
118	internal MyApplication () {
119		Object (application_id: "example.liststore.simple.treeview");
120	}
121}
122
123int main (string[] args) {
124	return new MyApplication ().run (args);
125}
126