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;
131 if (tree_view != null && tree_view.Sorted) {
132 res = AddSorted (node);
134 if (count >= nodes.Length)
136 nodes [count++] = node;
146 public virtual TreeNode Add (string key, string text)
148 TreeNode node = new TreeNode (text);
154 public virtual TreeNode Add (string key, string text, int imageIndex)
156 TreeNode node = Add (key, text);
157 node.ImageIndex = imageIndex;
161 public virtual TreeNode Add (string key, string text, string imageKey)
163 TreeNode node = Add (key, text);
164 node.ImageKey = imageKey;
169 public virtual TreeNode Add (string key, string text, int imageIndex, int selectedImageIndex)
171 TreeNode node = Add (key, text);
172 node.ImageIndex = imageIndex;
173 node.SelectedImageIndex = selectedImageIndex;
177 public virtual TreeNode Add (string key, string text, string imageKey, string selectedImageKey)
179 TreeNode node = Add (key, text);
180 node.ImageKey = imageKey;
181 node.SelectedImageKey = selectedImageKey;
188 public virtual void AddRange (TreeNode [] nodes)
191 throw new ArgumentNullException("nodes");
193 // We can't just use Array.Copy because the nodes also
194 // need to have some properties set when they are added.
195 for (int i = 0; i < nodes.Length; i++)
199 public virtual void Clear ()
204 Array.Clear (nodes, 0, count);
207 TreeView tree_view = null;
209 tree_view = owner.TreeView;
210 if (tree_view != null) {
211 tree_view.highlighted_node = null;
212 tree_view.selected_node = null;
213 tree_view.UpdateBelow (owner);
214 tree_view.RecalculateVisibleOrder (owner);
215 tree_view.UpdateScrollBars (false);
220 public bool Contains (TreeNode node)
222 return (Array.BinarySearch (nodes, node) > 0);
225 public virtual bool ContainsKey (string key)
227 for (int i = 0; i < count; i++) {
228 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
235 public void CopyTo (Array dest, int index)
237 Array.Copy (nodes, index, dest, index, count);
240 public IEnumerator GetEnumerator ()
242 return new TreeNodeEnumerator (this);
245 public int IndexOf (TreeNode node)
247 return Array.IndexOf (nodes, node);
251 public virtual int IndexOfKey (string key)
253 for (int i = 0; i < count; i++) {
254 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
260 public virtual TreeNode Insert (int index, string text)
262 TreeNode node = new TreeNode (text);
263 Insert (index, node);
268 public virtual void Insert (int index, TreeNode node)
270 if (count >= nodes.Length)
273 Array.Copy (nodes, index, nodes, index + 1, count - index);
274 nodes [index] = node;
281 public virtual TreeNode Insert (int index, string key, string text)
283 TreeNode node = new TreeNode (text);
285 Insert (index, node);
289 public virtual TreeNode Insert (int index, string key, string text, int imageIndex)
291 TreeNode node = new TreeNode (text);
293 node.ImageIndex = imageIndex;
294 Insert (index, node);
298 public virtual TreeNode Insert (int index, string key, string text, string imageKey)
300 TreeNode node = new TreeNode (text);
302 node.ImageKey = imageKey;
303 Insert (index, node);
307 public virtual TreeNode Insert (int index, string key, string text, int imageIndex, int selectedImageIndex)
309 TreeNode node = new TreeNode (text, imageIndex, selectedImageIndex);
311 Insert (index, node);
315 public virtual TreeNode Insert (int index, string key, string text, string imageKey, string selectedImageKey)
317 TreeNode node = new TreeNode (text);
319 node.ImageKey = imageKey;
320 node.SelectedImageKey = selectedImageKey;
321 Insert (index, node);
326 public void Remove (TreeNode node)
329 throw new NullReferenceException ();
331 int index = IndexOf (node);
336 throw new NullReferenceException ();
340 public virtual void RemoveAt (int index)
342 RemoveAt (index, true);
345 private void RemoveAt (int index, bool update)
347 TreeNode removed = nodes [index];
348 TreeNode prev = GetPrevNode (removed);
349 TreeNode new_selected = null;
350 bool visible = removed.IsVisible;
352 TreeView tree_view = null;
354 tree_view = owner.TreeView;
356 if (tree_view != null) {
357 tree_view.RecalculateVisibleOrder (prev);
359 if (removed == tree_view.SelectedNode) {
360 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
361 if (oe.MoveNext () && oe.MoveNext ()) {
362 new_selected = oe.CurrentNode;
364 oe = new OpenTreeNodeEnumerator (removed);
366 new_selected = oe.CurrentNode;
371 Array.Copy (nodes, index + 1, nodes, index, count - index);
373 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
376 if (tree_view != null && new_selected != null) {
377 tree_view.SelectedNode = new_selected;
380 TreeNode parent = removed.parent;
381 removed.parent = null;
383 if (update && tree_view != null && visible) {
384 tree_view.RecalculateVisibleOrder (prev);
385 tree_view.UpdateScrollBars (false);
386 tree_view.UpdateBelow (parent);
391 public virtual void RemoveByKey (string key)
393 TreeNode node = this[key];
400 private TreeNode GetPrevNode (TreeNode node)
402 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
404 if (one.MovePrevious () && one.MovePrevious ())
405 return one.CurrentNode;
409 private void SetupNode (TreeNode node)
411 // Remove it from any old parents
416 TreeView tree_view = null;
418 tree_view = owner.TreeView;
420 if (tree_view != null) {
421 TreeNode prev = GetPrevNode (node);
423 if (tree_view.IsHandleCreated && node.ArePreviousNodesExpanded)
424 tree_view.RecalculateVisibleOrder (prev);
425 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
426 tree_view.UpdateScrollBars (false);
429 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
430 // tree_view.UpdateBelow (owner);
431 tree_view.UpdateNode (owner);
432 tree_view.UpdateNode (node);
433 } else if (owner != null && tree_view != null) {
434 tree_view.UpdateBelow (owner);
438 int IList.Add (object node)
440 return Add ((TreeNode) node);
443 bool IList.Contains (object node)
445 return Contains ((TreeNode) node);
448 int IList.IndexOf (object node)
450 return IndexOf ((TreeNode) node);
453 void IList.Insert (int index, object node)
455 Insert (index, (TreeNode) node);
458 void IList.Remove (object node)
460 Remove ((TreeNode) node);
463 private int AddSorted (TreeNode node)
465 if (count >= nodes.Length)
468 CompareInfo compare = Application.CurrentCulture.CompareInfo;
471 for (int i = 0; i < count; i++) {
473 int comp = compare.Compare (node.Text, nodes [i].Text);
480 // Stick it at the end
484 // Move the nodes up and adjust their indices
485 for (int i = count - 1; i >= pos; i--) {
486 nodes [i + 1] = nodes [i];
494 // Would be nice to do this without running through the collection twice
495 internal void Sort (IComparer sorter) {
496 Array.Sort (nodes, 0, count, sorter == null ? new TreeNodeComparer (Application.CurrentCulture.CompareInfo) : sorter);
498 for (int i = 0; i < count; i++) {
499 nodes [i].Nodes.Sort (sorter);
505 TreeNode [] nn = new TreeNode [nodes.Length + 50];
506 Array.Copy (nodes, nn, nodes.Length);
510 private void Shrink ()
512 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
513 TreeNode [] nn = new TreeNode [len];
514 Array.Copy (nodes, nn, count);
519 public TreeNode[] Find (string key, bool searchAllChildren)
521 List<TreeNode> results = new List<TreeNode> (0);
522 Find (key, searchAllChildren, this, results);
524 return results.ToArray ();
527 private static void Find (string key, bool searchAllChildren, TreeNodeCollection nodes, List<TreeNode> results)
529 for (int i = 0; i < nodes.Count; i++) {
530 TreeNode thisNode = nodes [i];
532 if (string.Compare (thisNode.Name, key, true, CultureInfo.InvariantCulture) == 0)
533 results.Add (thisNode);
536 // Need to match the Microsoft order.
538 if (searchAllChildren){
539 for (int i = 0; i < nodes.Count; i++){
540 TreeNodeCollection childNodes = nodes [i].Nodes;
541 if (childNodes.Count > 0) {
542 Find (key, searchAllChildren, childNodes, results);
548 internal class TreeNodeEnumerator : IEnumerator {
550 private TreeNodeCollection collection;
551 private int index = -1;
553 public TreeNodeEnumerator (TreeNodeCollection collection)
555 this.collection = collection;
558 public object Current {
562 return collection [index];
566 public bool MoveNext ()
568 if (index + 1 >= collection.Count)
580 private class TreeNodeComparer : IComparer {
582 private CompareInfo compare;
584 public TreeNodeComparer (CompareInfo compare)
586 this.compare = compare;
589 public int Compare (object x, object y)
591 TreeNode l = (TreeNode) x;
592 TreeNode r = (TreeNode) y;
593 int res = compare.Compare (l.Text, r.Text);
595 return (res == 0 ? l.Index - r.Index : res);