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-2005 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Globalization;
32 namespace System.Windows.Forms {
33 [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
34 public class TreeNodeCollection : IList, ICollection, IEnumerable {
36 private static readonly int OrigSize = 50;
38 private TreeNode owner;
40 private TreeNode [] nodes;
42 private TreeNodeCollection ()
46 internal TreeNodeCollection (TreeNode owner)
49 nodes = new TreeNode [OrigSize];
53 [EditorBrowsable(EditorBrowsableState.Advanced)]
54 public virtual int Count {
58 public virtual bool IsReadOnly {
62 bool ICollection.IsSynchronized {
66 object ICollection.SyncRoot {
70 bool IList.IsFixedSize {
74 object IList.this [int index] {
76 if (index < 0 || index > Count)
77 throw new ArgumentOutOfRangeException ("index");
81 if (index < 0 || index > Count)
82 throw new ArgumentOutOfRangeException ("index");
83 TreeNode node = (TreeNode) value;
89 public virtual TreeNode this [int index] {
91 if (index < 0 || index > Count)
92 throw new ArgumentOutOfRangeException ("index");
96 if (index < 0 || index > Count)
97 throw new ArgumentOutOfRangeException ("index");
99 nodes [index] = value;
103 public virtual TreeNode Add (string text)
105 TreeNode res = new TreeNode (text);
110 public virtual int Add (TreeNode node)
113 throw new ArgumentNullException("node");
115 if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
116 return AddSorted (node);
118 if (count >= nodes.Length)
120 nodes [count++] = node;
122 if (owner.TreeView != null)
123 owner.TreeView.Refresh ();
127 public virtual void AddRange (TreeNode [] nodes)
130 throw new ArgumentNullException("node");
132 // We can't just use Array.Copy because the nodes also
133 // need to have some properties set when they are added.
134 for (int i = 0; i < nodes.Length; i++)
138 public virtual void Clear ()
140 Array.Clear (nodes, 0, count);
144 public bool Contains (TreeNode node)
146 return (Array.BinarySearch (nodes, node) > 0);
149 public virtual void CopyTo (Array dest, int index)
151 nodes.CopyTo (dest, index);
154 public virtual IEnumerator GetEnumerator ()
156 return new TreeNodeEnumerator (this);
159 public int IndexOf (TreeNode node)
161 return Array.IndexOf (nodes, node);
164 public virtual void Insert (int index, TreeNode node)
167 IList list = (IList) nodes;
168 list.Insert (index, node);
171 public virtual void Remove (TreeNode node)
173 int index = IndexOf (node);
178 public virtual void RemoveAt (int index)
180 Array.Copy (nodes, index, nodes, index - 1, count - index);
182 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
184 if (owner.TreeView != null)
185 owner.TreeView.TotalNodeCount--;
188 int IList.Add (object node)
190 return Add ((TreeNode) node);
193 bool IList.Contains (object node)
195 return Contains ((TreeNode) node);
198 int IList.IndexOf (object node)
200 return IndexOf ((TreeNode) node);
203 void IList.Insert (int index, object node)
205 Insert (index, (TreeNode) node);
208 void IList.Remove (object node)
210 Remove ((TreeNode) node);
213 private int AddSorted (TreeNode node)
216 if (count >= nodes.Length)
219 CompareInfo compare = Application.CurrentCulture.CompareInfo;
222 for (int i = 0; i < count; i++) {
224 int comp = compare.Compare (node.Text, nodes [i].Text);
231 // Stick it at the end
235 // Move the nodes up and adjust their indices
236 for (int i = count - 1; i >= pos; i--) {
237 nodes [i + 1] = nodes [i];
238 nodes [i + 1].SetIndex (i + 1);
247 // Would be nice to do this without running through the collection twice
248 internal void Sort () {
250 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
252 for (int i = 0; i < count; i++) {
253 nodes [i].SetIndex (i);
254 nodes [i].Nodes.Sort ();
258 private void SetData (TreeNode node)
260 SetData (node, count);
263 private void SetData (TreeNode node, int pos)
265 node.SetAddedData ((owner != null ? owner.TreeView : null), owner, pos);
270 TreeNode [] nn = new TreeNode [nodes.Length + 50];
271 Array.Copy (nodes, nn, nodes.Length);
275 private void Shrink ()
277 int len = (count > OrigSize ? count : OrigSize);
278 TreeNode [] nn = new TreeNode [len];
279 Array.Copy (nodes, nn, count);
284 internal class TreeNodeEnumerator : IEnumerator {
286 private TreeNodeCollection collection;
287 private int index = -1;
289 public TreeNodeEnumerator (TreeNodeCollection collection)
291 this.collection = collection;
294 public object Current {
295 get { return collection [index]; }
298 public bool MoveNext ()
300 if (index + 1 >= collection.Count)
312 private class TreeNodeComparer : IComparer {
314 private CompareInfo compare;
316 public TreeNodeComparer (CompareInfo compare)
318 this.compare = compare;
321 public int Compare (object x, object y)
323 TreeNode l = (TreeNode) x;
324 TreeNode r = (TreeNode) y;
325 int res = compare.Compare (l.Text, r.Text);
327 return (res == 0 ? l.Index - r.Index : res);