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
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 <[EMAIL PROTECTED]>
+//
+// 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/[EMAIL PROTECTED]'GtkCombo']/[EMAIL PROTECTED]'SetPopdownStrings']" name="hidden">1</attr>
<attr path="/api/namespace/[EMAIL PROTECTED]'GtkComboBox']/[EMAIL PROTECTED]'gtk_combo_box_get_active_iter']/*/[EMAIL PROTECTED]'GtkTreeIter*']" name="pass_as">out</attr>
<add-node path="/api/namespace/[EMAIL PROTECTED]'GtkContainer']"><implements><interface name="IEnumerable" /></implements></add-node>
+ <add-node path="/api/namespace/[EMAIL PROTECTED]'ListStore']"><implements><interface name="IEnumerable" /></implements></add-node>
<attr path="/api/namespace/[EMAIL PROTECTED]'GtkContainer']/[EMAIL PROTECTED]'ChildGetProperty']" name="hidden">1</attr>
<attr path="/api/namespace/[EMAIL PROTECTED]'GtkContainer']/[EMAIL PROTECTED]'Forall']/*/[EMAIL PROTECTED]'callback']" name="scope">call</attr>
<attr path="/api/namespace/[EMAIL PROTECTED]'GtkContainer']/[EMAIL PROTECTED]'Foreach']/*/[EMAIL PROTECTED]'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