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;
29 using System.Globalization;
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.Refresh ();
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);
210 private int AddSorted (TreeNode node)
213 if (count >= nodes.Length)
216 CompareInfo compare = Application.CurrentCulture.CompareInfo;
219 for (int i = 0; i < count; i++) {
221 int comp = compare.Compare (node.Text, nodes [i].Text);
228 // Stick it at the end
232 // Move the nodes up and adjust their indices
233 for (int i = count - 1; i >= pos; i--) {
234 nodes [i + 1] = nodes [i];
235 nodes [i + 1].SetIndex (i + 1);
244 // Would be nice to do this without running through the collection twice
245 internal void Sort () {
247 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
249 for (int i = 0; i < count; i++) {
250 nodes [i].SetIndex (i);
251 nodes [i].Nodes.Sort ();
255 private void SetData (TreeNode node)
257 SetData (node, count);
260 private void SetData (TreeNode node, int pos)
262 node.SetAddedData ((owner != null ? owner.TreeView : null), owner, pos);
267 TreeNode [] nn = new TreeNode [nodes.Length + 50];
268 Array.Copy (nodes, nn, nodes.Length);
272 private void Shrink ()
274 int len = (count > OrigSize ? count : OrigSize);
275 TreeNode [] nn = new TreeNode [len];
276 Array.Copy (nodes, nn, count);
281 internal class TreeNodeEnumerator : IEnumerator {
283 private TreeNodeCollection collection;
284 private int index = -1;
286 public TreeNodeEnumerator (TreeNodeCollection collection)
288 this.collection = collection;
291 public object Current {
292 get { return collection [index]; }
295 public bool MoveNext ()
297 if (index + 1 >= collection.Count)
309 private class TreeNodeComparer : IComparer {
311 private CompareInfo compare;
313 public TreeNodeComparer (CompareInfo compare)
315 this.compare = compare;
318 public int Compare (object x, object y)
320 TreeNode l = (TreeNode) x;
321 TreeNode r = (TreeNode) y;
322 int res = compare.Compare (l.Text, r.Text);
324 return (res == 0 ? l.Index - r.Index : res);