Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CustomTreeModel.cs « Xwt.GtkBackend « Xwt.Gtk - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8753ec627abe765529dacd2a93dc281883584ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// 
// CustomTreeModel.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Gtk;
#if XWT_GTK3
using TreeModelImplementor = Gtk.ITreeModelImplementor;
#endif

namespace Xwt.GtkBackend
{
	public class CustomTreeModel: GLib.Object, TreeModelImplementor
	{

		ITreeDataSource source;
		Dictionary<TreePosition,NodeData> handleHash = new Dictionary<TreePosition,NodeData> ();
		Type[] colTypes;
		Gtk.TreeModelAdapter adapter;
		int stamp;

		/// <summary>
		/// Stores information about the index of a noda and the GCHandle
		/// used by the native reference
		/// </summary>
		class NodeData
		{
			public GCHandle Handle;
			public int Index;
		}

		public CustomTreeModel (ITreeDataSource source)
		{
			this.source = source;
			adapter = new Gtk.TreeModelAdapter (this);
			colTypes = source.ColumnTypes;
			
			source.NodeChanged += HandleNodeChanged;
			source.NodeDeleted += HandleNodeDeleted;
			source.NodeInserted += HandleNodeInserted;
			source.NodesReordered += HandleNodesReordered;
		}
		
		public Gtk.TreeModelAdapter Store {
			get { return adapter; }
		}

		public Gtk.TreeIter IterFromNode (TreePosition node, int index = -1)
		{
			// The returned iter will have a reference to the TreePosition
			// through a GCHandle. At the same time we store a NodeData
			// object to a hashtable, which contains the GCHandle and
			// the index of the node

			NodeData data;
			if (!handleHash.TryGetValue (node, out data)) {
				// It is the first time the node is referenced.
				// Store it in handleHash.
				handleHash [node] = data = new NodeData {
					Handle = GCHandle.Alloc (node),
					Index = index
				};
			}

			// If the index of the node changed, update it in its NodeData
			if (index != -1 && index != data.Index)
				data.Index = index;

			Gtk.TreeIter result = Gtk.TreeIter.Zero;
			result.UserData = (IntPtr)data.Handle;
			result.Stamp = stamp;
			return result;
		}

		int GetCachedIndex (TreePosition node)
		{
			// Gets the index of a node, or -1 if that information is not available
			NodeData data;
			if (handleHash.TryGetValue (node, out data))
				return data.Index;
			return -1;
		}
		
		void CacheIndex (TreePosition node, int index)
		{
			// Stores the index of a node
			NodeData data;
			if (handleHash.TryGetValue (node, out data))
				data.Index = index;
		}
		
		public bool NodeFromIter (Gtk.TreeIter iter, out TreePosition pos)
		{
			if (iter.UserData == IntPtr.Zero) {
				pos = null;
				return true;
			}
			if (iter.Stamp != stamp) {
				// The iter has been invalidated
				pos = null;
				return false;
			}
			GCHandle gch = GCHandle.FromIntPtr (iter.UserData);
			pos = (TreePosition) gch.Target;
			return true;
		}

		#region TreeModelImplementor implementation

		void HandleNodesReordered (object sender, TreeNodeOrderEventArgs e)
		{
			// Don't increase the stamp because no nodes have been removed, so all iters are still valid
			// Update the cached indexes for all the children that have chanfed
			for (int n = 0; n < e.ChildrenOrder.Length; n++) {
				if (e.ChildrenOrder [n] != n) {
					var child = source.GetChild (e.Node, n);
					if (child != null)
						CacheIndex (child, n);
				}
			} 
			adapter.EmitRowsReordered (GetPath (e.Node), IterFromNode (e.Node), e.ChildrenOrder);
		}

		void HandleNodeInserted (object sender, TreeNodeEventArgs e)
		{
			// Don't increase the stamp because no nodes have been removed, so all iters are still valid
			// Update the cached indexes
			var parent = source.GetParent (e.Node);
			var count = source.GetChildrenCount (parent);
			for (int n = e.ChildIndex + 1; n < count; n++) {
				var child = source.GetChild (parent, n);
				if (child != null)
					CacheIndex (child, n);
			} 
			var iter = IterFromNode (e.Node, e.ChildIndex);
			adapter.EmitRowInserted (GetPath (e.Node), iter);
		}

		void HandleNodeDeleted (object sender, TreeNodeChildEventArgs e)
		{
			if (e.Node != null && !handleHash.ContainsKey (e.Node))
				return;

			NodeData data;
			if (e.Child != null && handleHash.TryGetValue (e.Child, out data)) {
				// Increase the model stamp since the node is gone and there may
				// be iters referencing that node. Increasing the stamp will
				// invalidate all those nodes
				stamp++;
				data.Handle.Free ();
				handleHash.Remove (e.Child);

				// Update the indexes of the node following the deleted one
				var count = source.GetChildrenCount (e.Node);
				for (int n = e.ChildIndex; n < count; n++) {
					var child = source.GetChild (e.Node, n);
					if (child != null)
						CacheIndex (child, n);
				}
			}

			if (e.Node == null) {
				adapter.EmitRowDeleted (new Gtk.TreePath (new int[] { e.ChildIndex }));
			} else {
				var p = GetPath (e.Node);
				int[] idx = new int [p.Indices.Length + 1];
				p.Indices.CopyTo (idx, 0);
				idx [idx.Length - 1] = e.ChildIndex;
				adapter.EmitRowDeleted (new Gtk.TreePath (idx));
			}
		}

		void HandleNodeChanged (object sender, TreeNodeEventArgs e)
		{
			if (handleHash.ContainsKey (e.Node))
				adapter.EmitRowChanged (GetPath (e.Node), IterFromNode (e.Node));
		}
		
		public GLib.GType GetColumnType (int index)
		{
			return (GLib.GType) colTypes [index];
		}

		public bool GetIter (out Gtk.TreeIter iter, Gtk.TreePath path)
        {
            iter = Gtk.TreeIter.Zero;
			TreePosition pos = null;
			int[] indices = path.Indices;
			if (indices.Length == 0)
				return false;
			for (int n=0; n<indices.Length; n++) {
				pos = source.GetChild (pos, indices [n]);
				if (pos == null)
					return false;
			}
			iter = IterFromNode (pos, indices[indices.Length - 1]);
			return true;
		}

		public Gtk.TreePath GetPath (Gtk.TreeIter iter)
		{
			TreePosition pos;
			if (NodeFromIter (iter, out pos))
				return GetPath (pos);
			return Gtk.TreePath.NewFirst ();
		}
		
		public Gtk.TreePath GetPath (TreePosition pos)
		{
			List<int> idx = new List<int> ();
			while (pos != null) {
				var parent = source.GetParent (pos);
				idx.Add (GetIndex (parent, pos));
				pos = parent;
			}
			idx.Reverse ();
			return new Gtk.TreePath (idx.ToArray ());
		}
		
		int GetIndex (TreePosition parent, TreePosition pos)
		{
			var res = GetCachedIndex (pos);
			if (res != -1)
				return res;

			int n = 0;
			do {
				var c = source.GetChild (parent, n);
				if (c == null)
					return -1;
				if (c == pos || c.Equals (pos)) {
					CacheIndex (pos, n);
					return n;
				}
				n++;
			} while (true);
		}

		public void GetValue (Gtk.TreeIter iter, int column, ref GLib.Value value)
		{
			TreePosition pos;
			if (!NodeFromIter (iter, out pos)) {
				value = GLib.Value.Empty;
				return;
			}
			var v = source.GetValue (pos, column);
			if (v == null)
				value = GLib.Value.Empty;
			else
				value = new GLib.Value (v);
		}

		public bool IterNext (ref Gtk.TreeIter iter)
		{
			TreePosition pos;
			if (!NodeFromIter (iter, out pos))
				return false;
			TreePosition parent = source.GetParent (pos);
			int i = GetIndex (parent, pos);
			if (i == -1)
				return false;
			pos = source.GetChild (parent, i + 1);
			if (pos != null) {
				iter = IterFromNode (pos, i + 1);
				return true;
			} else
				return false;
		}

		#if XWT_GTK3
		public bool IterPrevious (ref Gtk.TreeIter iter)
		{
			TreePosition pos;
			if (!NodeFromIter (iter, out pos))
				return false;
			TreePosition parent = source.GetParent (pos);
			int i = GetIndex (parent, pos);
			pos = source.GetChild (parent, i - 1);
			if (pos != null) {
				iter = IterFromNode (pos, i - 1);
				return true;
			} else
				return false;
		}
		#endif

		public bool IterChildren (out Gtk.TreeIter iter, Gtk.TreeIter parent)
        {
			iter = parent;
			TreePosition pos;
			if (!NodeFromIter (parent, out pos))
				return false;
			pos = source.GetChild (pos, 0);
			if (pos != null) {
				iter = IterFromNode (pos, 0);
				return true;
			} else
				return false;
		}

		public bool IterHasChild (Gtk.TreeIter iter)
		{
			TreePosition pos;
			if (!NodeFromIter (iter, out pos))
				return false;
			return source.GetChildrenCount (pos) != 0;
		}

		public int IterNChildren (Gtk.TreeIter iter)
		{
			TreePosition pos;
			if (!NodeFromIter (iter, out pos))
				return 0;
			return source.GetChildrenCount (pos);
		}

		public bool IterNthChild (out Gtk.TreeIter iter, Gtk.TreeIter parent, int n)
        {
			iter = parent;
			TreePosition pos;
			if (!NodeFromIter (parent, out pos))
				return false;
			pos = source.GetChild (pos, n);
			if (pos != null) {
				iter = IterFromNode (pos, n);
				return true;
			} else
				return false;
		}

		public bool IterParent (out Gtk.TreeIter iter, Gtk.TreeIter child)
        {
			iter = child;
			TreePosition pos;
			if (!NodeFromIter (iter, out pos))
				return false;
			if (pos != null)
				pos = source.GetParent (pos);
			if (pos != null) {
				iter = IterFromNode (pos);
				return true;
			} else
				return false;
		}

		public void RefNode (Gtk.TreeIter iter)
		{
		}

		public void UnrefNode (Gtk.TreeIter iter)
		{
		}

		public Gtk.TreeModelFlags Flags {
			get {
				return Gtk.TreeModelFlags.ItersPersist;
			}
		}

		public int NColumns {
			get {
				return colTypes.Length;
			}
		}
		#endregion
	}
}