1// TreeEnumerator.cs - .NET-style Enumerator for ITreeModel classes
2//
3// Author: Eric Butler <eric@extremeboredom.net>
4//
5// Copyright (c) 2005 Eric Butler
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of version 2 of the Lesser GNU General
9// Public License as published by the Free Software Foundation.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public
17// License along with this program; if not, write to the
18// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19// Boston, MA 02111-1307, USA.
20
21
22using System;
23using System.Collections;
24
25namespace Gtk {
26 internal class TreeEnumerator : IEnumerator {
27 private Gtk.TreeIter iter;
28 private Gtk.ITreeModel model;
29 private bool reset = true;
30 private bool changed = false;
31
32 public TreeEnumerator(ITreeModel model) {
33 this.model = model;
34
35 model.RowChanged += new RowChangedHandler(row_changed);
36 model.RowDeleted += new RowDeletedHandler(row_deleted);
37 model.RowInserted += new RowInsertedHandler(row_inserted);
38 model.RowsReordered += new RowsReorderedHandler(rows_reordered);
39 }
40
41 public object Current {
42 get {
43 if (reset == false) {
44 object[] row = new object[model.NColumns];
45 for (int x = 0; x < model.NColumns; x++) {
46 row[x] = model.GetValue(iter, x);
47 }
48 return row;
49 } else {
50 throw new InvalidOperationException("Enumerator not started.");
51 }
52 }
53 }
54
55 public bool MoveNext() {
56 if (changed == false) {
57 if (reset == true) {
58 reset = false;
59 return model.GetIterFirst(out iter);
60 } else {
61 return model.IterNext(ref iter);
62 }
63 } else {
64 throw new InvalidOperationException("List has changed.");
65 }
66 }
67
68 public void Reset() {
69 reset = true;
70 changed = false;
71 }
72
73 private void row_changed(object o, RowChangedArgs args) {
74 changed = true;
75 }
76
77 private void row_deleted(object o, RowDeletedArgs args) {
78 changed = true;
79 }
80
81 private void row_inserted(object o, RowInsertedArgs args) {
82 changed = true;
83 }
84
85 private void rows_reordered(object o, RowsReorderedArgs args) {
86 changed = true;
87 }
88 }
89}