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;
31 namespace System.Windows.Forms {
32 [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
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];
52 [EditorBrowsable(EditorBrowsableState.Advanced)]
57 public bool IsReadOnly {
61 bool ICollection.IsSynchronized {
65 object ICollection.SyncRoot {
69 bool IList.IsFixedSize {
73 object IList.this [int index] {
78 if (!(value is TreeNode))
79 throw new ArgumentException ("value");
80 this [index] = (TreeNode) value;
84 public virtual TreeNode this [int index] {
86 if (index < 0 || index >= Count)
87 throw new ArgumentOutOfRangeException ("index");
91 if (index < 0 || index >= Count)
92 throw new ArgumentOutOfRangeException ("index");
94 nodes [index] = value;
98 public virtual TreeNode Add (string text)
100 TreeNode res = new TreeNode (text);
105 public virtual int Add (TreeNode node)
108 throw new ArgumentNullException("node");
111 TreeView tree_view = null;
113 if (tree_view != null && tree_view.Sorted) {
114 res = AddSorted (node);
116 if (count >= nodes.Length)
118 nodes [count++] = node;
128 public virtual TreeNode Add (string key, string text)
130 TreeNode node = new TreeNode (text);
136 public virtual TreeNode Add (string key, string text, int imageIndex)
138 TreeNode node = Add (key, text);
139 node.ImageIndex = imageIndex;
143 public virtual TreeNode Add (string key, string text, string imageKey)
145 TreeNode node = Add (key, text);
146 node.ImageKey = imageKey;
151 public virtual TreeNode Add (string key, string text, int imageIndex, int selectedImageIndex)
153 TreeNode node = Add (key, text);
154 node.ImageIndex = imageIndex;
155 node.SelectedImageIndex = selectedImageIndex;
159 public virtual TreeNode Add (string key, string text, string imageKey, string selectedImageKey)
161 TreeNode node = Add (key, text);
162 node.ImageKey = imageKey;
163 node.SelectedImageKey = selectedImageKey;
170 public virtual void AddRange (TreeNode [] nodes)
173 throw new ArgumentNullException("node");
175 // We can't just use Array.Copy because the nodes also
176 // need to have some properties set when they are added.
177 for (int i = 0; i < nodes.Length; i++)
181 public virtual void Clear ()
186 Array.Clear (nodes, 0, count);
189 TreeView tree_view = null;
191 tree_view = owner.TreeView;
192 if (tree_view != null) {
193 tree_view.UpdateBelow (owner);
194 tree_view.RecalculateVisibleOrder (owner);
195 tree_view.UpdateScrollBars (false);
200 public bool Contains (TreeNode node)
202 return (Array.BinarySearch (nodes, node) > 0);
205 public bool ContainsKey (string key)
207 for (int i = 0; i < count; i++) {
208 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
215 public void CopyTo (Array dest, int index)
217 Array.Copy (nodes, index, dest, index, count);
220 public IEnumerator GetEnumerator ()
222 return new TreeNodeEnumerator (this);
225 public int IndexOf (TreeNode node)
227 return Array.IndexOf (nodes, node);
231 public int IndexOfKey (string key)
233 for (int i = 0; i < count; i++) {
234 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
241 public virtual void Insert (int index, TreeNode node)
243 if (count >= nodes.Length)
246 Array.Copy (nodes, index, nodes, index + 1, count - index);
247 nodes [index] = node;
253 public void Remove (TreeNode node)
256 throw new NullReferenceException ();
258 int index = IndexOf (node);
263 throw new NullReferenceException ();
267 public virtual void RemoveAt (int index)
269 RemoveAt (index, true);
272 private void RemoveAt (int index, bool update)
274 TreeNode removed = nodes [index];
275 TreeNode prev = GetPrevNode (removed);
276 TreeNode new_selected = null;
277 bool visible = removed.IsVisible;
279 TreeView tree_view = null;
281 tree_view = owner.TreeView;
283 if (tree_view != null) {
284 tree_view.RecalculateVisibleOrder (prev);
286 if (removed == tree_view.SelectedNode) {
287 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
288 if (oe.MoveNext () && oe.MoveNext ()) {
289 new_selected = oe.CurrentNode;
291 oe = new OpenTreeNodeEnumerator (removed);
293 new_selected = oe.CurrentNode;
298 Array.Copy (nodes, index + 1, nodes, index, count - index);
300 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
303 if (tree_view != null && new_selected != null) {
304 tree_view.SelectedNode = new_selected;
307 TreeNode parent = removed.parent;
308 removed.parent = null;
310 if (update && tree_view != null && visible) {
311 tree_view.RecalculateVisibleOrder (prev);
312 tree_view.UpdateScrollBars (false);
313 tree_view.UpdateBelow (parent);
317 private TreeNode GetPrevNode (TreeNode node)
319 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
321 if (one.MovePrevious () && one.MovePrevious ())
322 return one.CurrentNode;
326 private void SetupNode (TreeNode node)
328 // Remove it from any old parents
333 TreeView tree_view = null;
335 tree_view = owner.TreeView;
337 if (tree_view != null) {
338 TreeNode prev = GetPrevNode (node);
341 tree_view.RecalculateVisibleOrder (prev);
342 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
343 tree_view.UpdateScrollBars (false);
346 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
347 // tree_view.UpdateBelow (owner);
348 tree_view.UpdateNode (owner);
349 tree_view.UpdateNode (node);
350 } else if (owner != null && tree_view != null) {
351 tree_view.UpdateBelow (owner);
355 int IList.Add (object node)
357 return Add ((TreeNode) node);
360 bool IList.Contains (object node)
362 return Contains ((TreeNode) node);
365 int IList.IndexOf (object node)
367 return IndexOf ((TreeNode) node);
370 void IList.Insert (int index, object node)
372 Insert (index, (TreeNode) node);
375 void IList.Remove (object node)
377 Remove ((TreeNode) node);
380 private int AddSorted (TreeNode node)
382 if (count >= nodes.Length)
385 CompareInfo compare = Application.CurrentCulture.CompareInfo;
388 for (int i = 0; i < count; i++) {
390 int comp = compare.Compare (node.Text, nodes [i].Text);
397 // Stick it at the end
401 // Move the nodes up and adjust their indices
402 for (int i = count - 1; i >= pos; i--) {
403 nodes [i + 1] = nodes [i];
411 // Would be nice to do this without running through the collection twice
412 internal void Sort (IComparer sorter) {
413 Array.Sort (nodes, 0, count, sorter == null ? new TreeNodeComparer (Application.CurrentCulture.CompareInfo) : sorter);
415 for (int i = 0; i < count; i++) {
416 nodes [i].Nodes.Sort (sorter);
422 TreeNode [] nn = new TreeNode [nodes.Length + 50];
423 Array.Copy (nodes, nn, nodes.Length);
427 private void Shrink ()
429 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
430 TreeNode [] nn = new TreeNode [len];
431 Array.Copy (nodes, nn, count);
436 internal class TreeNodeEnumerator : IEnumerator {
438 private TreeNodeCollection collection;
439 private int index = -1;
441 public TreeNodeEnumerator (TreeNodeCollection collection)
443 this.collection = collection;
446 public object Current {
450 return collection [index];
454 public bool MoveNext ()
456 if (index + 1 >= collection.Count)
468 private class TreeNodeComparer : IComparer {
470 private CompareInfo compare;
472 public TreeNodeComparer (CompareInfo compare)
474 this.compare = compare;
477 public int Compare (object x, object y)
479 TreeNode l = (TreeNode) x;
480 TreeNode r = (TreeNode) y;
481 int res = compare.Compare (l.Text, r.Text);
483 return (res == 0 ? l.Index - r.Index : res);