[Gtk-sharp-list] [Patch] Make Gtk.ListStore implement IEnumerable

Eric Butler eric@extremeboredom.net
Tue, 03 May 2005 11:22:52 -0700


--=-5qCEcls8oH2nhWq/pPFN
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hey,

As inspired by the Improving Gtk# thread here is a patch that makes
ListStore implement IEnumerable. This allows you to do something like:

foreach (object[] row in trustedNodesListStore) {
Console.WriteLine(row[0].ToString());
}

Which, for a simple list, is much more friendly than trying to deal with
TreeIter.

I look forward to hear feedback.

Regards,
Eric

--=-5qCEcls8oH2nhWq/pPFN
Content-Disposition: attachment; filename=TreeEnumerator.patch
Content-Type: text/x-patch; name=TreeEnumerator.patch; charset=utf-8
Content-Transfer-Encoding: 7bit

Index: TreeEnumerator.cs
===================================================================
--- TreeEnumerator.cs	(revision 0)
+++ TreeEnumerator.cs	(revision 0)
@@ -0,0 +1,65 @@
+// TreeEnumerator.cs - .NET-style Enumerator for TreeModel classes
+//
+// Author: Eric Butler <eric@extremeboredom.net>
+//
+// Copyright (c) 2005 Eric Butler
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of version 2 of the Lesser GNU General
+// Public License as published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program; if not, write to the
+// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+
+using System;
+using System.Collections;
+
+namespace Gtk
+{
+	public class TreeEnumerator : IEnumerator
+	{
+		Gtk.TreeIter iter;
+		Gtk.TreeModel model;
+		bool reset = true;
+		
+		public TreeEnumerator (TreeModel model)
+		{
+			this.model = model;
+		}
+		
+		public object Current
+		{
+			get {
+				object[] row = new object[model.NColumns];
+				for (int x = 0; x < model.NColumns; x++) {
+					row[x] = model.GetValue(iter, x);
+				}
+				return row;
+			}
+		}
+		
+		public bool MoveNext()
+		{
+			if (reset == true) {
+				reset = false;
+				return model.GetIterFirst(out iter);
+			} else {
+				return model.IterNext(ref iter);
+			}
+		}
+		
+		public void Reset()
+		{
+			reset = true;
+		}
+	}
+}
+
Index: ListStore.custom
===================================================================
--- ListStore.custom	(revision 43951)
+++ ListStore.custom	(working copy)
@@ -198,3 +198,8 @@
 			gtk_tree_sortable_set_default_sort_func(Handle, sort_func_wrapper.NativeDelegate, (IntPtr) gch, DestroyHelper.NotifyHandler);
 		}
 
+		public IEnumerator GetEnumerator()
+		{
+			return new TreeEnumerator(this);
+		}
+
Index: Gtk.metadata
===================================================================
--- Gtk.metadata	(revision 43951)
+++ Gtk.metadata	(working copy)
@@ -179,6 +179,7 @@
   <attr path="/api/namespace/object[@cname='GtkCombo']/method[@name='SetPopdownStrings']" name="hidden">1</attr>
   <attr path="/api/namespace/object[@cname='GtkComboBox']/method[@cname='gtk_combo_box_get_active_iter']/*/*[@type='GtkTreeIter*']" name="pass_as">out</attr> 
   <add-node path="/api/namespace/object[@cname='GtkContainer']"><implements><interface name="IEnumerable" /></implements></add-node>
+  <add-node path="/api/namespace/object[@name='ListStore']"><implements><interface name="IEnumerable" /></implements></add-node>
   <attr path="/api/namespace/object[@cname='GtkContainer']/method[@name='ChildGetProperty']" name="hidden">1</attr>
   <attr path="/api/namespace/object[@cname='GtkContainer']/method[@name='Forall']/*/*[@name='callback']" name="scope">call</attr>
   <attr path="/api/namespace/object[@cname='GtkContainer']/method[@name='Foreach']/*/*[@name='callback']" name="scope">call</attr>
Index: Makefile.am
===================================================================
--- Makefile.am	(revision 43951)
+++ Makefile.am	(working copy)
@@ -27,7 +27,8 @@
 	Timeout.cs			\
 	TreeNodeAttribute.cs		\
 	TreeNode.cs			\
-	TreeNodeValueAttribute.cs
+	TreeNodeValueAttribute.cs	\
+	TreeEnumerator.cs

--=-5qCEcls8oH2nhWq/pPFN--