1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Globalization;
32 using System.Collections.Generic;
35 namespace System.Windows.Forms {
36 [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
37 public class TreeNodeCollection : IList, ICollection, IEnumerable {
39 private static readonly int OrigSize = 50;
41 private TreeNode owner;
43 private TreeNode [] nodes;
45 private TreeNodeCollection ()
49 internal TreeNodeCollection (TreeNode owner)
52 nodes = new TreeNode [OrigSize];
56 [EditorBrowsable(EditorBrowsableState.Advanced)]
63 public bool IsReadOnly {
67 bool ICollection.IsSynchronized {
71 object ICollection.SyncRoot {
75 bool IList.IsFixedSize {
79 object IList.this [int index] {
84 if (!(value is TreeNode))
85 throw new ArgumentException ("Parameter must be of type TreeNode.", "value");
86 this [index] = (TreeNode) value;
90 public virtual TreeNode this [int index] {
92 if (index < 0 || index >= Count)
93 throw new ArgumentOutOfRangeException ("index");
97 if (index < 0 || index >= Count)
98 throw new ArgumentOutOfRangeException ("index");
100 nodes [index] = value;
105 public virtual TreeNode this [string key] {
107 for (int i = 0; i < count; i++)
108 if (string.Compare (key, nodes[i].Name, true) == 0)
116 public virtual TreeNode Add (string text)
118 TreeNode res = new TreeNode (text);
123 public virtual int Add (TreeNode node)
126 throw new ArgumentNullException("node");
129 TreeView tree_view = null;
132 tree_view = owner.TreeView;
134 if (tree_view != null && tree_view.Sorted) {
135 res = AddSorted (node);
137 if (count >= nodes.Length)
150 public virtual TreeNode Add (string key, string text)
152 TreeNode node = new TreeNode (text);
158 public virtual TreeNode Add (string key, string text, int imageIndex)
160 TreeNode node = Add (key, text);
161 node.ImageIndex = imageIndex;
165 public virtual TreeNode Add (string key, string text, string imageKey)
167 TreeNode node = Add (key, text);
168 node.ImageKey = imageKey;
173 public virtual TreeNode Add (string key, string text, int imageIndex, int selectedImageIndex)
175 TreeNode node = Add (key, text);
176 node.ImageIndex = imageIndex;
177 node.SelectedImageIndex = selectedImageIndex;
181 public virtual TreeNode Add (string key, string text, string imageKey, string selectedImageKey)
183 TreeNode node = Add (key, text);
184 node.ImageKey = imageKey;
185 node.SelectedImageKey = selectedImageKey;
192 public virtual void AddRange (TreeNode [] nodes)
195 throw new ArgumentNullException("nodes");
197 // We can't just use Array.Copy because the nodes also
198 // need to have some properties set when they are added.
199 for (int i = 0; i < nodes.Length; i++)
203 public virtual void Clear ()
208 Array.Clear (nodes, 0, count);
211 TreeView tree_view = null;
213 tree_view = owner.TreeView;
214 if (tree_view != null) {
215 tree_view.highlighted_node = null;
216 tree_view.selected_node = null;
217 tree_view.UpdateBelow (owner);
218 tree_view.RecalculateVisibleOrder (owner);
219 tree_view.UpdateScrollBars (false);
224 public bool Contains (TreeNode node)
226 return (Array.BinarySearch (nodes, node) > 0);
229 public virtual bool ContainsKey (string key)
231 for (int i = 0; i < count; i++) {
232 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
239 public void CopyTo (Array dest, int index)
241 Array.Copy (nodes, index, dest, index, count);
244 public IEnumerator GetEnumerator ()
246 return new TreeNodeEnumerator (this);
249 public int IndexOf (TreeNode node)
251 return Array.IndexOf (nodes, node);
255 public virtual int IndexOfKey (string key)
257 for (int i = 0; i < count; i++) {
258 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
264 public virtual TreeNode Insert (int index, string text)
266 TreeNode node = new TreeNode (text);
267 Insert (index, node);
272 public virtual void Insert (int index, TreeNode node)
274 if (count >= nodes.Length)
277 Array.Copy (nodes, index, nodes, index + 1, count - index);
278 nodes [index] = node;
285 public virtual TreeNode Insert (int index, string key, string text)
287 TreeNode node = new TreeNode (text);
289 Insert (index, node);
293 public virtual TreeNode Insert (int index, string key, string text, int imageIndex)
295 TreeNode node = new TreeNode (text);
297 node.ImageIndex = imageIndex;
298 Insert (index, node);
302 public virtual TreeNode Insert (int index, string key, string text, string imageKey)
304 TreeNode node = new TreeNode (text);
306 node.ImageKey = imageKey;
307 Insert (index, node);
311 public virtual TreeNode Insert (int index, string key, string text, int imageIndex, int selectedImageIndex)
313 TreeNode node = new TreeNode (text, imageIndex, selectedImageIndex);
315 Insert (index, node);
319 public virtual TreeNode Insert (int index, string key, string text, string imageKey, string selectedImageKey)
321 TreeNode node = new TreeNode (text);
323 node.ImageKey = imageKey;
324 node.SelectedImageKey = selectedImageKey;
325 Insert (index, node);
330 public void Remove (TreeNode node)
333 throw new NullReferenceException ();
335 int index = IndexOf (node);
340 throw new NullReferenceException ();
344 public virtual void RemoveAt (int index)
346 RemoveAt (index, true);
349 private void RemoveAt (int index, bool update)
351 TreeNode removed = nodes [index];
352 TreeNode prev = GetPrevNode (removed);
353 TreeNode new_selected = null;
354 bool visible = removed.IsVisible;
356 TreeView tree_view = null;
358 tree_view = owner.TreeView;
360 if (tree_view != null) {
361 tree_view.RecalculateVisibleOrder (prev);
363 if (removed == tree_view.SelectedNode) {
364 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
365 if (oe.MoveNext () && oe.MoveNext ()) {
366 new_selected = oe.CurrentNode;
368 oe = new OpenTreeNodeEnumerator (removed);
370 new_selected = oe.CurrentNode;
375 Array.Copy (nodes, index + 1, nodes, index, count - index - 1);
380 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
383 if (tree_view != null && new_selected != null) {
384 tree_view.SelectedNode = new_selected;
387 TreeNode parent = removed.parent;
388 removed.parent = null;
390 if (update && tree_view != null && visible) {
391 tree_view.RecalculateVisibleOrder (prev);
392 tree_view.UpdateScrollBars (false);
393 tree_view.UpdateBelow (parent);
398 public virtual void RemoveByKey (string key)
400 TreeNode node = this[key];
407 private TreeNode GetPrevNode (TreeNode node)
409 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
411 if (one.MovePrevious () && one.MovePrevious ())
412 return one.CurrentNode;
416 private void SetupNode (TreeNode node)
418 // Remove it from any old parents
423 TreeView tree_view = null;
425 tree_view = owner.TreeView;
427 if (tree_view != null) {
428 TreeNode prev = GetPrevNode (node);
430 if (tree_view.IsHandleCreated && node.ArePreviousNodesExpanded)
431 tree_view.RecalculateVisibleOrder (prev);
432 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
433 tree_view.UpdateScrollBars (false);
436 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
437 // tree_view.UpdateBelow (owner);
438 tree_view.UpdateNode (owner);
439 tree_view.UpdateNode (node);
440 } else if (owner != null && tree_view != null) {
441 tree_view.UpdateBelow (owner);
445 int IList.Add (object node)
447 return Add ((TreeNode) node);
450 bool IList.Contains (object node)
452 return Contains ((TreeNode) node);
455 int IList.IndexOf (object node)
457 return IndexOf ((TreeNode) node);
460 void IList.Insert (int index, object node)
462 Insert (index, (TreeNode) node);
465 void IList.Remove (object node)
467 Remove ((TreeNode) node);
470 private int AddSorted (TreeNode node)
472 if (count >= nodes.Length)
475 CompareInfo compare = Application.CurrentCulture.CompareInfo;
478 for (int i = 0; i < count; i++) {
480 int comp = compare.Compare (node.Text, nodes [i].Text);
487 // Stick it at the end
491 // Move the nodes up and adjust their indices
492 for (int i = count - 1; i >= pos; i--) {
493 nodes [i + 1] = nodes [i];
501 // Would be nice to do this without running through the collection twice
502 internal void Sort (IComparer sorter) {
503 Array.Sort (nodes, 0, count, sorter == null ? new TreeNodeComparer (Application.CurrentCulture.CompareInfo) : sorter);
505 for (int i = 0; i < count; i++) {
506 nodes [i].Nodes.Sort (sorter);
512 TreeNode [] nn = new TreeNode [nodes.Length + 50];
513 Array.Copy (nodes, nn, nodes.Length);
517 private void Shrink ()
519 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
520 TreeNode [] nn = new TreeNode [len];
521 Array.Copy (nodes, nn, count);
526 public TreeNode[] Find (string key, bool searchAllChildren)
528 List<TreeNode> results = new List<TreeNode> (0);
529 Find (key, searchAllChildren, this, results);
531 return results.ToArray ();
534 private static void Find (string key, bool searchAllChildren, TreeNodeCollection nodes, List<TreeNode> results)
536 for (int i = 0; i < nodes.Count; i++) {
537 TreeNode thisNode = nodes [i];
539 if (string.Compare (thisNode.Name, key, true, CultureInfo.InvariantCulture) == 0)
540 results.Add (thisNode);
543 // Need to match the Microsoft order.
545 if (searchAllChildren){
546 for (int i = 0; i < nodes.Count; i++){
547 TreeNodeCollection childNodes = nodes [i].Nodes;
548 if (childNodes.Count > 0) {
549 Find (key, searchAllChildren, childNodes, results);
555 internal class TreeNodeEnumerator : IEnumerator {
557 private TreeNodeCollection collection;
558 private int index = -1;
560 public TreeNodeEnumerator (TreeNodeCollection collection)
562 this.collection = collection;
565 public object Current {
569 return collection [index];
573 public bool MoveNext ()
575 if (index + 1 >= collection.Count)
587 private class TreeNodeComparer : IComparer {
589 private CompareInfo compare;
591 public TreeNodeComparer (CompareInfo compare)
593 this.compare = compare;
596 public int Compare (object x, object y)
598 TreeNode l = (TreeNode) x;
599 TreeNode r = (TreeNode) y;
600 int res = compare.Compare (l.Text, r.Text);
602 return (res == 0 ? l.Index - r.Index : res);