[gnome-devel-docs] Vala simple TreeView example.



commit 80b949c498a6e31d089dd20a6cd2ad1b8be80cb1
Author: Tiffany Antopolski <tiffany antopolski gmail com>
Date:   Thu May 10 03:02:04 2012 -0400

    Vala simple TreeView example.

 .../C/media/treeview_simple_liststore.png          |  Bin 0 -> 21063 bytes
 .../C/samples/treeview_simple_liststore.vala       |  133 ++++++++++++++++++++
 .../C/treeview_simple_liststore.vala.page          |   33 +++++
 platform-demos/Makefile.am                         |    3 +
 4 files changed, 169 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/media/treeview_simple_liststore.png b/platform-demos/C/media/treeview_simple_liststore.png
new file mode 100644
index 0000000..7e99cc6
Binary files /dev/null and b/platform-demos/C/media/treeview_simple_liststore.png differ
diff --git a/platform-demos/C/samples/treeview_simple_liststore.vala b/platform-demos/C/samples/treeview_simple_liststore.vala
new file mode 100644
index 0000000..baa0f03
--- /dev/null
+++ b/platform-demos/C/samples/treeview_simple_liststore.vala
@@ -0,0 +1,133 @@
+public class PhoneBookEntry {
+	public string firstname;
+	public string lastname;
+	public string phone;
+	
+	public PhoneBookEntry (string f, string l, string p) {
+		this.firstname = f;
+		this.lastname = l;
+		this.phone = p;
+	}
+}
+
+class TreeViewSimpleListStore : Gtk.ApplicationWindow {
+
+	Gtk.TreeSelection selection;
+	Gtk.Label label;
+
+	PhoneBookEntry[] phonebook = {
+		new PhoneBookEntry ("Jurg", "Billeter", "555-0123"),
+		new PhoneBookEntry ("Johannes", "Schmid", "555-1234"),
+		new PhoneBookEntry ("Julita", "Inca", "555-2345"),
+		new PhoneBookEntry ("Javier", "Jardon", "555-3456"),
+		new PhoneBookEntry ("Jason", "Clinton", "555-4567"),
+		new PhoneBookEntry ("Random J.", "Hacker", "555-5678")
+	};
+
+	enum Column {
+		FIRSTNAME, 
+		LASTNAME, 
+		PHONE,
+		WEIGHT
+	}
+
+	internal TreeViewSimpleListStore (MyApplication app) {
+		Object (application: app, title: "My Phone Book");
+
+		this.set_default_size (250, 100);
+		this.border_width = 10;
+
+		var view = new Gtk.TreeView ();
+		this.setup_treeview (view);
+		view.expand = true;
+		
+		label = new Gtk.Label ("");
+
+		var grid = new Gtk.Grid ();
+	
+		grid.attach (view, 0, 0, 1, 1);
+		grid.attach (label, 0, 1, 1, 1);
+		this.add (grid);
+	
+		selection = view.get_selection ();
+		selection.changed.connect (this.on_changed);
+	}
+
+	void setup_treeview (Gtk.TreeView view) {
+        	var listmodel = new Gtk.ListStore (4, 
+                                                   typeof (string), 
+                                                   typeof (string),
+                                                   typeof (string),
+                                                   typeof (int));
+			view.set_model (listmodel);
+
+			var cell = new Gtk.CellRendererText ();
+
+			/* 'weight' refers to font boldness.  
+		 	 *  400 is normal.
+		 	 *  700 is bold.
+		 	 */
+			cell.set ("weight_set", true);
+ 
+			/*columns*/
+			view.insert_column_with_attributes (-1,
+                                                            "First Name",
+                                                            cell, "text",
+                                                            Column.FIRSTNAME,
+                                                            "weight", Column.WEIGHT);
+
+			view.insert_column_with_attributes (-1, 
+                                                            "Last Name",
+                                                            new Gtk.CellRendererText (),
+                                                            "text", Column.LASTNAME);
+
+			view.insert_column_with_attributes (-1, 
+                                                            "Phone Number",
+                                                            new Gtk.CellRendererText (),
+                                                            "text", Column.PHONE);
+
+		/* Insert the phonebook into the ListStore */
+		Gtk.TreeIter iter;	
+		for (int i = 0; i < phonebook.length; i++) {	
+			listmodel.append (out iter);
+			listmodel.set (iter, Column.FIRSTNAME,
+                                       phonebook[i].firstname,
+                                       Column.LASTNAME, phonebook[i].lastname,
+                                       Column.PHONE, phonebook[i].phone,
+                                       Column.WEIGHT, 700);
+		}
+	}
+
+	void on_changed () {
+		Gtk.TreeModel model;
+		Gtk.TreeIter iter;
+		string name;
+		string lastname;
+		string phone;
+
+		if (selection.get_selected (out model, out iter)) {
+			model.get (iter, 
+                                   Column.FIRSTNAME, out name,
+                                   Column.LASTNAME, out lastname,
+                                   Column.PHONE, out phone);
+
+			label.set_text ("\n" + name + " " + lastname + " " + phone);
+		}
+	}
+}
+
+class MyApplication : Gtk.Application {
+	protected override void activate () {
+
+		/* Create new Window and show all the things. */
+		new TreeViewSimpleListStore (this).show_all ();
+	}
+
+	internal MyApplication () {
+		Object (application_id: "example.liststore.simple.treeview");
+	}
+}
+
+int main (string[] args) {
+	return new MyApplication ().run (args);
+}
diff --git a/platform-demos/C/treeview_simple_liststore.vala.page b/platform-demos/C/treeview_simple_liststore.vala.page
new file mode 100644
index 0000000..30feb11
--- /dev/null
+++ b/platform-demos/C/treeview_simple_liststore.vala.page
@@ -0,0 +1,33 @@
+<page xmlns="http://projectmallard.org/1.0/";
+      xmlns:xi="http://www.w3.org/2001/XInclude";
+      type="guide" style="task"
+      id="treeview_simple_liststore.vala">
+  <info>
+    <link type="guide" xref="beginner.vala#treeview"/>
+    <link type="seealso" xref="grid.vala"/>
+    <link type="seealso" xref="label.vala"/>
+    <revision version="0.1" date="2012-05-09" status="draft"/>
+
+    <credit type="author copyright">
+      <name>Tiffany Antopolski</name>
+      <email>tiffany antopolski gmail com</email>
+      <years>2012</years>
+    </credit>
+
+    <desc>A widget can display any TreeModel implementation (lists and trees)</desc>
+  </info>
+
+  <title>Simple Treeview with ListStore</title>
+  <media type="image" mime="image/png" src="media/treeview_simple_liststore.png"/>
+  <p>This TreeView displays a simple ListStore with the Selection "changed" signal connected.</p>
+
+<code mime="text/x-vala" style="numbered"><xi:include href="samples/treeview_simple_liststore.vala" parse="text"><xi:fallback/></xi:include></code>
+<p>
+  In this sample we used the following:
+</p>
+<list>
+  <item><p><link href="http://valadoc.org/#!api=gtk+-3.0/Gtk.TreeView";>Gtk.TreeView</link></p></item>
+  <item><p><link href="http://valadoc.org/#!api=gtk+-3.0/Gtk.ListStore";>Gtk.ListStore</link></p></item>
+  <item><p><link href="http://valadoc.org/#!api=gtk+-3.0/Gtk.TreeSelection";>Gtk.TreeSelection</link></p></item>
+</list>
+</page>
diff --git a/platform-demos/Makefile.am b/platform-demos/Makefile.am
index 2cfd76f..c568290 100644
--- a/platform-demos/Makefile.am
+++ b/platform-demos/Makefile.am
@@ -50,6 +50,7 @@ demo_sources = \
 	samples/switch.vala			\
 	samples/togglebutton.vala		\
 	samples/toolbar.vala			\
+	samples/treeview_simple_liststore.vala 	\
 	samples/window.c			\
 	samples/window.py			\
 	samples/window.vala			\
@@ -92,6 +93,7 @@ DOC_FIGURES = \
 	media/togglebutton.png			\
 	media/toolbar.png			\
 	media/toolbar2.png			\
+	media/treeview_simple_liststore.png	\
 	media/ubuntu.png			\
 	media/weatherAppJs.png			\
 	media/window.png			\
@@ -161,6 +163,7 @@ DOC_PAGES =				\
 	togglebutton.vala.page		\
 	toolbar.js.page			\
 	toolbar.vala.page		\
+	treeview_simple_liststore.vala.page	\
 	translate.page			\
 	vala.page			\
 	weatherApp.js.page		\



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]