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 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
28 using System.Collections;
31 namespace System.Windows.Forms {
33 public class TreeNodeCollection : IList, ICollection, IEnumerable {
35 private static readonly int OrigSize = 50;
37 private TreeNode owner;
39 private TreeNode [] nodes;
41 private TreeNodeCollection ()
45 internal TreeNodeCollection (TreeNode owner)
48 nodes = new TreeNode [OrigSize];
51 public virtual int Count {
55 public virtual bool IsReadOnly {
59 bool ICollection.IsSynchronized {
63 object ICollection.SyncRoot {
67 bool IList.IsFixedSize {
71 object IList.this [int index] {
73 if (index < 0 || index > Count)
74 throw new ArgumentOutOfRangeException ("index");
78 if (index < 0 || index > Count)
79 throw new ArgumentOutOfRangeException ("index");
80 TreeNode node = (TreeNode) value;
86 public virtual TreeNode this [int index] {
88 if (index < 0 || index > Count)
89 throw new ArgumentOutOfRangeException ("index");
93 if (index < 0 || index > Count)
94 throw new ArgumentOutOfRangeException ("index");
96 nodes [index] = value;
100 public virtual TreeNode Add (string text)
102 TreeNode res = new TreeNode (text);
107 public virtual int Add (TreeNode node)
110 throw new ArgumentNullException("node");
112 if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
113 return AddSorted (node);
115 if (count >= nodes.Length)
117 nodes [count++] = node;
119 if (owner.TreeView != null)
120 owner.TreeView.TotalNodeCount++;
124 public virtual void AddRange (TreeNode [] nodes)
127 throw new ArgumentNullException("node");
129 // We can't just use Array.Copy because the nodes also
130 // need to have some properties set when they are added.
131 for (int i = 0; i < nodes.Length; i++)
135 public virtual void Clear ()
137 Array.Clear (nodes, 0, count);
141 public bool Contains (TreeNode node)
143 return (Array.BinarySearch (nodes, node) > 0);
146 public virtual void CopyTo (Array dest, int index)
148 nodes.CopyTo (dest, index);
151 public virtual IEnumerator GetEnumerator ()
153 return new TreeNodeEnumerator (this);
156 public int IndexOf (TreeNode node)
158 return Array.IndexOf (nodes, node);
161 public virtual void Insert (int index, TreeNode node)
164 IList list = (IList) nodes;
165 list.Insert (index, node);
168 public virtual void Remove (TreeNode node)
170 int index = IndexOf (node);
175 public virtual void RemoveAt (int index)
177 Array.Copy (nodes, index, nodes, index - 1, count - index);
179 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
181 if (owner.TreeView != null)
182 owner.TreeView.TotalNodeCount--;
185 int IList.Add (object node)
187 return Add ((TreeNode) node);
190 bool IList.Contains (object node)
192 return Contains ((TreeNode) node);
195 int IList.IndexOf (object node)
197 return IndexOf ((TreeNode) node);
200 void IList.Insert (int index, object node)
202 Insert (index, (TreeNode) node);
205 void IList.Remove (object node)
207 Remove ((TreeNode) node);
211 private int AddSorted (TreeNode node)
214 if (count >= nodes.Length)
216 nodes [count++] = node;
221 internal void Sort ()
226 private void SetData (TreeNode node)
228 node.SetAddedData ((owner != null ? owner.TreeView : null), owner, count);
233 TreeNode [] nn = new TreeNode [nodes.Length + 50];
234 Array.Copy (nodes, nn, nodes.Length);
238 private void Shrink ()
240 int len = (count > OrigSize ? count : OrigSize);
241 TreeNode [] nn = new TreeNode [len];
242 Array.Copy (nodes, nn, count);
247 internal class TreeNodeEnumerator : IEnumerator {
249 private TreeNodeCollection collection;
250 private int index = -1;
252 public TreeNodeEnumerator (TreeNodeCollection collection)
254 this.collection = collection;
257 public object Current {
258 get { return collection [index]; }
261 public bool MoveNext ()
263 if (index + 1 >= collection.Count)