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;
30 using System.Collections.Generic;
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];
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 ("Parameter must be of type TreeNode.", "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 this [string key] {
100 for (int i = 0; i < count; i++)
101 if (string.Compare (key, nodes[i].Name, true) == 0)
110 TreeView tv = owner == null ? null : owner.TreeView;
111 return tv != null && (tv.Sorted || tv.TreeViewNodeSorter != null);
115 public virtual TreeNode Add (string text)
117 TreeNode res = new TreeNode (text);
122 public virtual int Add (TreeNode node)
125 throw new ArgumentNullException("node");
128 TreeView tree_view = null;
131 tree_view = owner.TreeView;
133 if (tree_view != null && UsingSorting) {
134 index = AddSorted (node);
136 if (count >= nodes.Length)
145 // UIA Framework Event: Collection Changed
146 if (tree_view != null)
147 tree_view.OnUIACollectionChanged (owner, new CollectionChangeEventArgs (CollectionChangeAction.Add, node));
151 public virtual TreeNode Add (string key, string text)
153 TreeNode node = new TreeNode (text);
159 public virtual TreeNode Add (string key, string text, int imageIndex)
161 TreeNode node = Add (key, text);
162 node.ImageIndex = imageIndex;
166 public virtual TreeNode Add (string key, string text, string imageKey)
168 TreeNode node = Add (key, text);
169 node.ImageKey = imageKey;
174 public virtual TreeNode Add (string key, string text, int imageIndex, int selectedImageIndex)
176 TreeNode node = Add (key, text);
177 node.ImageIndex = imageIndex;
178 node.SelectedImageIndex = selectedImageIndex;
182 public virtual TreeNode Add (string key, string text, string imageKey, string selectedImageKey)
184 TreeNode node = Add (key, text);
185 node.ImageKey = imageKey;
186 node.SelectedImageKey = selectedImageKey;
190 public virtual void AddRange (TreeNode [] nodes)
193 throw new ArgumentNullException("nodes");
195 // We can't just use Array.Copy because the nodes also
196 // need to have some properties set when they are added.
197 for (int i = 0; i < nodes.Length; i++)
201 public virtual void Clear ()
206 Array.Clear (nodes, 0, count);
209 TreeView tree_view = null;
211 tree_view = owner.TreeView;
212 if (tree_view != 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.IndexOf (nodes, node, 0, count) != -1;
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)
234 public void CopyTo (Array dest, int index)
236 Array.Copy (nodes, index, dest, index, count);
239 public IEnumerator GetEnumerator ()
241 return new TreeNodeEnumerator (this);
244 public int IndexOf (TreeNode node)
246 return Array.IndexOf (nodes, node);
249 public virtual int IndexOfKey (string key)
251 for (int i = 0; i < count; i++) {
252 if (string.Compare (nodes [i].Name, key, true, CultureInfo.InvariantCulture) == 0)
258 public virtual TreeNode Insert (int index, string text)
260 TreeNode node = new TreeNode (text);
261 Insert (index, node);
265 public virtual void Insert (int index, TreeNode node)
267 if (count >= nodes.Length)
270 Array.Copy (nodes, index, nodes, index + 1, count - index);
271 nodes [index] = node;
274 // If we can use sorting, it means we have an owner *and* a TreeView
276 Sort (owner.TreeView.TreeViewNodeSorter);
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);
325 public void Remove (TreeNode node)
328 throw new NullReferenceException ();
330 int index = IndexOf (node);
335 public virtual void RemoveAt (int index)
337 RemoveAt (index, true);
340 private void RemoveAt (int index, bool update)
342 TreeNode removed = nodes [index];
343 TreeNode prev = GetPrevNode (removed);
344 TreeNode new_selected = null;
345 bool re_set_selected = false;
346 bool visible = removed.IsVisible;
348 TreeView tree_view = null;
350 tree_view = owner.TreeView;
352 if (tree_view != null) {
353 tree_view.RecalculateVisibleOrder (prev);
355 if (removed == tree_view.SelectedNode) {
356 re_set_selected = true;
357 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
358 if (oe.MoveNext () && oe.MoveNext ()) {
359 new_selected = oe.CurrentNode;
361 oe = new OpenTreeNodeEnumerator (removed);
363 new_selected = oe.CurrentNode == removed ? null : oe.CurrentNode;
368 Array.Copy (nodes, index + 1, nodes, index, count - index - 1);
373 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
376 if (tree_view != null && re_set_selected) {
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);
389 // UIA Framework Event: Collection Changed
390 if (tree_view != null)
391 tree_view.OnUIACollectionChanged (owner, new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
394 public virtual void RemoveByKey (string key)
396 TreeNode node = this[key];
402 private TreeNode GetPrevNode (TreeNode node)
404 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
406 if (one.MovePrevious () && one.MovePrevious ())
407 return one.CurrentNode;
411 private void SetupNode (TreeNode node)
413 // We used to remove this from the previous parent, but .Net
414 // skips this step (even if setting the owner field).
419 TreeView tree_view = null;
421 tree_view = owner.TreeView;
423 if (tree_view != null) {
424 // We may need to invalidate this entire node collection if sorted.
425 TreeNode prev = UsingSorting ? owner : GetPrevNode (node);
427 if (tree_view.IsHandleCreated && node.ArePreviousNodesExpanded)
428 tree_view.RecalculateVisibleOrder (prev);
429 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
430 tree_view.UpdateScrollBars (false);
432 tree_view.UpdateBelow (owner);
436 int IList.Add (object node)
438 return Add ((TreeNode) node);
441 bool IList.Contains (object node)
443 return Contains ((TreeNode) node);
446 int IList.IndexOf (object node)
448 return IndexOf ((TreeNode) node);
451 void IList.Insert (int index, object node)
453 Insert (index, (TreeNode) node);
456 void IList.Remove (object node)
458 Remove ((TreeNode) node);
461 private int AddSorted (TreeNode node)
463 if (count >= nodes.Length)
466 TreeView tree_view = owner.TreeView;
467 if (tree_view.TreeViewNodeSorter != null) { // Custom sorting
468 nodes [count++] = node;
469 Sort (tree_view.TreeViewNodeSorter);
473 CompareInfo compare = Application.CurrentCulture.CompareInfo;
476 for (int i = 0; i < count; i++) {
478 int comp = compare.Compare (node.Text, nodes [i].Text);
485 // Stick it at the end
489 // Move the nodes up and adjust their indices
490 for (int i = count - 1; i >= index; i--) {
491 nodes [i + 1] = nodes [i];
494 nodes [index] = node;
499 // Would be nice to do this without running through the collection twice
500 internal void Sort (IComparer sorter) {
501 Array.Sort (nodes, 0, count, sorter == null ? new TreeNodeComparer (Application.CurrentCulture.CompareInfo) : sorter);
503 for (int i = 0; i < count; i++) {
504 nodes [i].Nodes.Sort (sorter);
507 // Sorted may have been set to false even if TreeViewNodeSorter is being used.
508 TreeView tv = owner == null ? null : owner.TreeView;
515 TreeNode [] nn = new TreeNode [nodes.Length + 50];
516 Array.Copy (nodes, nn, nodes.Length);
520 private void Shrink ()
522 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
523 TreeNode [] nn = new TreeNode [len];
524 Array.Copy (nodes, nn, count);
528 public TreeNode[] Find (string key, bool searchAllChildren)
530 List<TreeNode> results = new List<TreeNode> (0);
531 Find (key, searchAllChildren, this, results);
533 return results.ToArray ();
536 private static void Find (string key, bool searchAllChildren, TreeNodeCollection nodes, List<TreeNode> results)
538 for (int i = 0; i < nodes.Count; i++) {
539 TreeNode thisNode = nodes [i];
541 if (string.Compare (thisNode.Name, key, true, CultureInfo.InvariantCulture) == 0)
542 results.Add (thisNode);
545 // Need to match the Microsoft order.
547 if (searchAllChildren){
548 for (int i = 0; i < nodes.Count; i++){
549 TreeNodeCollection childNodes = nodes [i].Nodes;
550 if (childNodes.Count > 0) {
551 Find (key, searchAllChildren, childNodes, results);
557 internal class TreeNodeEnumerator : IEnumerator {
559 private TreeNodeCollection collection;
560 private int index = -1;
562 public TreeNodeEnumerator (TreeNodeCollection collection)
564 this.collection = collection;
567 public object Current {
571 return collection [index];
575 public bool MoveNext ()
577 if (index + 1 >= collection.Count)
589 private class TreeNodeComparer : IComparer {
591 private CompareInfo compare;
593 public TreeNodeComparer (CompareInfo compare)
595 this.compare = compare;
598 public int Compare (object x, object y)
600 TreeNode l = (TreeNode) x;
601 TreeNode r = (TreeNode) y;
602 int res = compare.Compare (l.Text, r.Text);
604 return (res == 0 ? l.Index - r.Index : res);