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

TreeNodeNavigator.cs « MonoDevelop.Ide.Gui.Components « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b69fa16661b43b43410953c0d65a0d095523c33d (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// TreeNodeNavigator.cs
//
// Author:
//   Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// 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;

namespace MonoDevelop.Ide.Gui.Components
{
	public partial class ExtensibleTreeView
	{
		class TreeNodeNavigator: ITreeNavigator
		{
			protected ExtensibleTreeView pad;
			protected Gtk.TreeView tree;
			protected Gtk.TreeStore store;
			Gtk.TreeIter currentNavIter;
			object dataItem;
			
			public TreeNodeNavigator (ExtensibleTreeView pad): this (pad, Gtk.TreeIter.Zero)
			{
			}
			
			public TreeNodeNavigator (ExtensibleTreeView pad, Gtk.TreeIter iter)
			{
				this.pad = pad;
				tree = pad.Tree;
				store = pad.Store;
				MoveToIter (iter);
			}
			
			protected Gtk.TreeIter currentIter {
				get { return currentNavIter; }
			}
			
			public ITreeNavigator Clone ()
			{
				return new TreeNodeNavigator (pad, currentIter);
			}
			
			void AssertIsValid ()
			{
				if (!pad.sorting && !currentIter.Equals (Gtk.TreeIter.Zero) && !store.IterIsValid (currentIter)) {
					if (dataItem == null || !MoveToObject (dataItem))
						throw new InvalidOperationException ("Tree iterator has been invalidated.");
				}
			}
			
			protected void InitIter (Gtk.TreeIter it, object dataObject)
			{
				currentNavIter = it;
				dataItem = dataObject;
			}
	
			
			public object DataItem {
				get {
					return dataItem;
				}
			}
			
			public TypeNodeBuilder TypeNodeBuilder {
				get {
					return pad.GetTypeNodeBuilder (CurrentPosition._iter);
				}
			}
			
			internal NodeBuilder[] NodeBuilderChain {
				get {
					AssertIsValid ();
					NodeBuilder[] chain = (NodeBuilder[]) GetStoreValue (ExtensibleTreeView.BuilderChainColumn);
					if (chain != null)
						return chain;
					else
						return new NodeBuilder [0];
				}
			}
			
			public bool Selected {
				get {
					AssertIsValid ();
					return tree.Selection.IterIsSelected (currentIter);
				}
				set {
					AssertIsValid ();
					if (value != Selected) {
						ExpandToNode ();
						try {
							tree.Selection.SelectIter (currentIter);
							tree.SetCursor (store.GetPath (currentIter), pad.CompleteColumn, false);
						} catch (Exception) {}
	//						tree.ScrollToCell (store.GetPath (currentIter), null, false, 0, 0);
					}
				}
			}
			
			public NodePosition CurrentPosition {
				get {
					AssertIsValid ();
					NodePosition pos = new NodePosition ();
					pos._iter = currentIter;
					return pos;
				}
			}
			
			public bool MoveToPosition (NodePosition position)
			{
				MoveToIter ((Gtk.TreeIter) position._iter);
				return true;
			}
			
			internal void MoveToIter (Gtk.TreeIter iter)
			{
				currentNavIter = iter;
				if (!iter.Equals (Gtk.TreeIter.Zero))
					dataItem = GetStoreValue (ExtensibleTreeView.DataItemColumn);
				else
					dataItem = null;
			}

			public void ScrollToNode ()
			{
				tree.ScrollToCell (store.GetPath (currentIter), pad.CompleteColumn, true, 0, 0);
			}

			public bool MoveToRoot ()
			{
				AssertIsValid ();
				Gtk.TreeIter it;
				if (!store.GetIterFirst (out it))
					return false;
				
				MoveToIter (it);
				return true;
			}
		
			public bool MoveToObject (object dataObject)
			{
				Gtk.TreeIter iter;
				if (!pad.GetFirstNode (dataObject, out iter)) return false;
				MoveToIter (iter);
				return true;
			}
		
			public bool MoveToNextObject ()
			{
				object dataItem = DataItem;
				if (dataItem == null)
					return false;
				if (currentIter.Equals (Gtk.TreeIter.Zero))
					return false;
				Gtk.TreeIter it = currentIter;
				if (!pad.GetNextNode (dataItem, ref it))
					return false;
				
				MoveToIter (it);
				return true;
			}
		
			public bool MoveToParent ()
			{
				AssertIsValid ();
				Gtk.TreeIter it;
				if (store.IterParent (out it, currentIter)) {
					MoveToIter (it);
					return true;
				} else
					return false;
			}
			
			public bool MoveToParent (Type dataType)
			{
				AssertIsValid ();
				Gtk.TreeIter newIter = currentIter;
				while (store.IterParent (out newIter, newIter)) {
					object data = store.GetValue (newIter, ExtensibleTreeView.DataItemColumn);
					if (dataType.IsInstanceOfType (data)) {
						MoveToIter (newIter);
						return true;
					}
				}
				return false;
			}
			
			public bool MoveToFirstChild ()
			{
				EnsureFilled ();
				Gtk.TreeIter it;
				if (!store.IterChildren (out it, currentIter))
					return false;
				
				MoveToIter (it);
				return true;
			}
			
			public bool MoveNext ()
			{
				AssertIsValid ();
				Gtk.TreeIter it = currentIter;
				if (!store.IterNext (ref it))
					return false;
				MoveToIter (it);
				return true;
			}
			
			public bool HasChild (string name, Type dataType)
			{
				if (MoveToChild (name, dataType)) {
					MoveToParent ();
					return true;
				} else
					return false;
			}
			
			public bool HasChildren ()
			{
				EnsureFilled ();
				Gtk.TreeIter it;
				return store.IterChildren (out it, currentIter);
			}
		
			public bool FindChild (object dataObject)
			{
				return FindChild (dataObject, false);
			}
			
			public bool FindChild (object dataObject, bool recursive)
			{
				AssertIsValid ();
				object it;
				
				if (!pad.NodeHash.TryGetValue (dataObject, out it))
					return false;
				else if (it is Gtk.TreeIter) {
					if (IsChildIter (currentIter, (Gtk.TreeIter)it, recursive)) {
						MoveToIter ((Gtk.TreeIter)it);
						return true;
					} else
						return false;
				} else {
					foreach (Gtk.TreeIter cit in (Gtk.TreeIter[])it) {
						if (IsChildIter (currentIter, cit, recursive)) {
							MoveToIter ((Gtk.TreeIter)cit);
							return true;
						}
					}
					return false;
				}
			}
			
			bool IsChildIter (Gtk.TreeIter pit, Gtk.TreeIter cit, bool recursive)
			{
				Gtk.TreePath pitPath = tree.Model.GetPath (pit);
				Gtk.TreePath citPath = tree.Model.GetPath (cit);

				if (!citPath.Up ())
					return false;

				if (citPath.Equals(pitPath))
					return true;

				return recursive && pitPath.IsAncestor (citPath);
			}
			
			public bool MoveToChild (string name, Type dataType)
			{
				EnsureFilled ();
				Gtk.TreeIter oldIter = currentIter;
	
				if (!MoveToFirstChild ()) {
					MoveToIter (oldIter);
					return false;
				}
	
				do {
					if (name == NodeName)
						return true;
				} while (MoveNext ());
	
				MoveToIter (oldIter);
				return false;
			}
			
			public bool Expanded {
				get {
					AssertIsValid ();
					return tree.GetRowExpanded (store.GetPath (currentIter));
				}
				set {
					AssertIsValid ();
					if (value && !Expanded) {
						Gtk.TreePath path = store.GetPath (currentIter);
						tree.ExpandRow (path, false);
					}
					else if (!value && Expanded) {
						Gtk.TreePath path = store.GetPath (currentIter);
						tree.CollapseRow (path);
					}
				}
			}
	
			public ITreeOptions Options {
				get { return pad.globalOptions; }
			}
			
			public NodeState SaveState ()
			{
				AssertIsValid ();
				return NodeState.SaveState (pad, this);
			}
			
			public void RestoreState (NodeState state)
			{
				AssertIsValid ();
				NodeState.RestoreState (pad, this, state);
			}
		
			public void Refresh ()
			{
				ITreeBuilder builder = new TreeBuilder (pad, currentIter);
				builder.UpdateAll ();
			}
			
			public void ExpandToNode ()
			{
				Gtk.TreeIter it;
				AssertIsValid ();
				if (store.IterParent (out it, currentIter)) {
					Gtk.TreePath path = store.GetPath (it);
					tree.ExpandToPath (path);
				}
			}
			
			public string NodeName {
				get {
					object data = DataItem;
					NodeBuilder[] chain = BuilderChain;
					if (chain != null && chain.Length > 0) return ((TypeNodeBuilder)chain[0]).GetNodeName (this, data);
					else return GetStoreNodeInfo ().Label;
				}
			}
			
			public NodeBuilder[] BuilderChain {
				get {
					AssertIsValid ();
					return (NodeBuilder[]) GetStoreValue (ExtensibleTreeView.BuilderChainColumn);
				}
			}
			
			public T GetParentDataItem<T> (bool includeCurrent)
			{
				return (T)GetParentDataItem (typeof(T), includeCurrent);
			}

			public object GetParentDataItem (Type type, bool includeCurrent)
			{
				if (includeCurrent && type.IsInstanceOfType (DataItem))
					return DataItem;
	
				Gtk.TreeIter it = currentIter;
				while (store.IterParent (out it, it)) {
					object data = store.GetValue (it, ExtensibleTreeView.DataItemColumn);
					if (type.IsInstanceOfType (data))
						return data;
				}
				return null;
			}
		
			public virtual void EnsureFilled ()
			{
				AssertIsValid ();
				if (!(bool) GetStoreValue (ExtensibleTreeView.FilledColumn))
					new TreeBuilder (pad, currentIter).FillNode ();
			}
			
			public bool Filled {
				get {
					AssertIsValid ();
					return (bool) GetStoreValue (ExtensibleTreeView.FilledColumn);
				}
			}

			object GetStoreValue (int column)
			{
				return store.GetValue (currentIter, column);
			}

			NodeInfo GetStoreNodeInfo ()
			{
				return (NodeInfo) store.GetValue (currentIter, 0);
			}
		}
	}
}