~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/TreeStoreBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TreeStoreBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Xwt.Backends;
 
29
using Xwt.Drawing;
 
30
using Xwt.Engine;
 
31
 
 
32
namespace Xwt.GtkBackend
 
33
{
 
34
        class IterPos: TreePosition
 
35
        {
 
36
                public IterPos ()
 
37
                {
 
38
                }
 
39
                
 
40
                public IterPos (int treeVersion, Gtk.TreeIter iter)
 
41
                {
 
42
                        this.Iter = iter;
 
43
                        this.Version = treeVersion;
 
44
                }
 
45
                
 
46
                public Gtk.TreeIter Iter;
 
47
                public Gtk.TreeIter LastChildIter;
 
48
                public int LastChildIndex = -1;
 
49
                public int ChildrenCount = -1;
 
50
                public int Version;
 
51
        }
 
52
        
 
53
        public class TreeStoreBackend: TableStoreBackend, ITreeStoreBackend
 
54
        {
 
55
                Type[] columnTypes;
 
56
                int version;
 
57
                
 
58
                public Gtk.TreeStore Tree {
 
59
                        get { return (Gtk.TreeStore) Store; }
 
60
                }
 
61
                
 
62
                public override Gtk.TreeModel InitializeModel (Type[] columnTypes)
 
63
                {
 
64
                        this.columnTypes = columnTypes;
 
65
                        return new Gtk.TreeStore (columnTypes);
 
66
                }
 
67
                
 
68
                public event EventHandler<TreeNodeEventArgs> NodeInserted;
 
69
                public event EventHandler<TreeNodeChildEventArgs> NodeDeleted;
 
70
                public event EventHandler<TreeNodeEventArgs> NodeChanged;
 
71
                public event EventHandler<TreeNodeOrderEventArgs> NodesReordered;
 
72
                
 
73
                IterPos GetIterPos (TreePosition pos)
 
74
                {
 
75
                        IterPos tpos = (IterPos) pos;
 
76
                        if (tpos != null && tpos.Version != version) {
 
77
                                tpos.LastChildIndex = -1;
 
78
                                tpos.ChildrenCount = -1;
 
79
                        }
 
80
                        return tpos;
 
81
                }
 
82
                
 
83
                public void Clear ()
 
84
                {
 
85
                        version++;
 
86
                        Tree.Clear ();
 
87
                }
 
88
 
 
89
                public TreePosition GetChild (TreePosition pos, int index)
 
90
                {
 
91
                        IterPos tpos = GetIterPos (pos);
 
92
                        if (tpos != null && tpos.LastChildIndex == index)
 
93
                                return new IterPos (version, tpos.LastChildIter);
 
94
                        if (index == 0) {
 
95
                                if (tpos != null) {
 
96
                                        Gtk.TreeIter it;
 
97
                                        if (Tree.IterChildren (out it, tpos.Iter)) {
 
98
                                                tpos.LastChildIter = it;
 
99
                                                tpos.LastChildIndex = 0;
 
100
                                                return new IterPos (version, it);
 
101
                                        }
 
102
                                } else {
 
103
                                        Gtk.TreeIter it;
 
104
                                        if (Tree.GetIterFirst (out it))
 
105
                                                return new IterPos (version, it);
 
106
                                }
 
107
                                return null;
 
108
                        }
 
109
                        
 
110
                        if (tpos == null) {
 
111
                                Gtk.TreeIter it;
 
112
                                if (Tree.IterNthChild (out it, index))
 
113
                                        return new IterPos (version, it);
 
114
                                else
 
115
                                        return null;
 
116
                        }
 
117
                        
 
118
                        if (tpos.LastChildIndex == -1 || tpos.LastChildIndex > index) {
 
119
                                Gtk.TreeIter it;
 
120
                                if (Tree.IterNthChild (out it, tpos.Iter, index)) {
 
121
                                        tpos.LastChildIter = it;
 
122
                                        tpos.LastChildIndex = index;
 
123
                                        return new IterPos (version, it);
 
124
                                } else
 
125
                                        return null;
 
126
                        }
 
127
                        
 
128
                        // tpos.LastChildIndex < index
 
129
                        
 
130
                        Gtk.TreeIter iter = tpos.LastChildIter;
 
131
                        for (int n = tpos.LastChildIndex; n < index; n++) {
 
132
                                if (!Tree.IterNext (ref iter))
 
133
                                        return null;
 
134
                        }
 
135
                        tpos.LastChildIter = iter;
 
136
                        tpos.LastChildIndex = index;
 
137
                        return new IterPos (version, iter);
 
138
                }
 
139
                
 
140
                public int GetChildrenCount (TreePosition pos)
 
141
                {
 
142
                        if (pos == null)
 
143
                                return Tree.IterNChildren ();
 
144
                        
 
145
                        IterPos tpos = GetIterPos (pos);
 
146
                        
 
147
                        if (tpos.ChildrenCount != -1)
 
148
                                return tpos.ChildrenCount;
 
149
                        
 
150
                        return tpos.ChildrenCount = Tree.IterNChildren (tpos.Iter);
 
151
                }
 
152
 
 
153
                public void SetValue (TreePosition pos, int column, object value)
 
154
                {
 
155
                        IterPos tpos = GetIterPos (pos);
 
156
                        SetValue (tpos.Iter, column, value);
 
157
                }
 
158
 
 
159
                public object GetValue (TreePosition pos, int column)
 
160
                {
 
161
                        IterPos tpos = GetIterPos (pos);
 
162
                        return GetValue (tpos.Iter, column);
 
163
                }
 
164
 
 
165
                public TreePosition InsertBefore (TreePosition pos)
 
166
                {
 
167
                        version++;
 
168
                        IterPos tpos = GetIterPos (pos);
 
169
                        var p = Tree.InsertNodeBefore (tpos.Iter);
 
170
                        return new IterPos (version, p);
 
171
                }
 
172
 
 
173
                public TreePosition InsertAfter (TreePosition pos)
 
174
                {
 
175
                        version++;
 
176
                        IterPos tpos = GetIterPos (pos);
 
177
                        var p = Tree.InsertNodeAfter (tpos.Iter);
 
178
                        return new IterPos (version, p);
 
179
                }
 
180
 
 
181
                public TreePosition AddChild (TreePosition pos)
 
182
                {
 
183
                        version++;
 
184
                        IterPos tpos = GetIterPos (pos);
 
185
                        Gtk.TreeIter it;
 
186
                        if (pos == null)
 
187
                                it = Tree.AppendNode ();
 
188
                        else
 
189
                                it = Tree.AppendNode (tpos.Iter);
 
190
                        return new IterPos (version, it);
 
191
                }
 
192
                
 
193
                public void Remove (TreePosition pos)
 
194
                {
 
195
                        version++;
 
196
                        IterPos tpos = GetIterPos (pos);
 
197
                        Gtk.TreeIter it = tpos.Iter;
 
198
                        Tree.Remove (ref it);
 
199
                }
 
200
 
 
201
                public TreePosition GetNext (TreePosition pos)
 
202
                {
 
203
                        IterPos tpos = GetIterPos (pos);
 
204
                        Gtk.TreeIter it = tpos.Iter;
 
205
                        if (!Tree.IterNext (ref it))
 
206
                                return null;
 
207
                        return new IterPos (version, it);
 
208
                }
 
209
 
 
210
                public TreePosition GetPrevious (TreePosition pos)
 
211
                {
 
212
                        throw new NotImplementedException ();
 
213
                }
 
214
 
 
215
                public TreePosition GetParent (TreePosition pos)
 
216
                {
 
217
                        IterPos tpos = GetIterPos (pos);
 
218
                        Gtk.TreeIter it;
 
219
                        if (!Tree.IterParent (out it, tpos.Iter))
 
220
                                return null;
 
221
                        return new IterPos (version, it);
 
222
                }
 
223
                
 
224
                public Type[] ColumnTypes {
 
225
                        get {
 
226
                                return columnTypes;
 
227
                        }
 
228
                }
 
229
                
 
230
                public void EnableEvent (object eventId)
 
231
                {
 
232
                }
 
233
                
 
234
                public void DisableEvent (object eventId)
 
235
                {
 
236
                }
 
237
        }
 
238
}
 
239