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)]
58 public 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");
116 TreeView tree_view = null;
118 if (tree_view != null && tree_view.Sorted) {
119 res = AddSorted (node);
121 if (count >= nodes.Length)
123 nodes [count++] = node;
132 public virtual void AddRange (TreeNode [] nodes)
135 throw new ArgumentNullException("node");
137 // We can't just use Array.Copy because the nodes also
138 // need to have some properties set when they are added.
139 for (int i = 0; i < nodes.Length; i++)
143 public virtual void Clear ()
145 for (int i = 0; i < count; i++)
148 Array.Clear (nodes, 0, count);
151 TreeView tree_view = null;
153 tree_view = owner.TreeView;
155 tree_view.top_node = null;
156 if (tree_view != null) {
157 tree_view.UpdateBelow (owner);
158 tree_view.RecalculateVisibleOrder (owner);
163 public bool Contains (TreeNode node)
165 return (Array.BinarySearch (nodes, node) > 0);
168 public void CopyTo (Array dest, int index)
170 nodes.CopyTo (dest, index);
173 public IEnumerator GetEnumerator ()
175 return new TreeNodeEnumerator (this);
178 public int IndexOf (TreeNode node)
180 return Array.IndexOf (nodes, node);
183 public virtual void Insert (int index, TreeNode node)
185 if (count >= nodes.Length)
188 Array.Copy (nodes, index, nodes, index + 1, count - index);
189 nodes [index] = node;
195 public void Remove (TreeNode node)
197 int index = IndexOf (node);
202 public virtual void RemoveAt (int index)
204 RemoveAt (index, true);
207 private void RemoveAt (int index, bool update)
209 TreeNode removed = nodes [index];
210 TreeNode prev = GetPrevNode (removed);
211 TreeNode new_selected = null;
212 bool visible = removed.IsVisible;
214 TreeView tree_view = null;
216 tree_view = owner.TreeView;
218 if (tree_view != null) {
219 tree_view.RecalculateVisibleOrder (prev);
220 if (removed == tree_view.top_node) {
222 if (removed.IsRoot) {
223 tree_view.top_node = null;
225 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
226 if (oe.MovePrevious () && oe.MovePrevious ()) {
227 tree_view.top_node = oe.CurrentNode;
229 removed.is_expanded = false;
230 oe = new OpenTreeNodeEnumerator (removed);
231 if (oe.MoveNext () && oe.MoveNext ()) {
232 tree_view.top_node = oe.CurrentNode;
234 tree_view.top_node = null;
239 if (removed == tree_view.selected_node) {
240 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
241 if (oe.MoveNext () && oe.MoveNext ()) {
242 new_selected = oe.CurrentNode;
244 oe = new OpenTreeNodeEnumerator (removed);
246 new_selected = oe.CurrentNode;
252 Array.Copy (nodes, index + 1, nodes, index, count - index);
254 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
257 if (tree_view != null && new_selected != null) {
258 tree_view.SelectedNode = new_selected;
261 TreeNode parent = removed.parent;
262 removed.parent = null;
264 if (tree_view != null && visible) {
265 tree_view.RecalculateVisibleOrder (prev);
266 tree_view.UpdateScrollBars ();
267 tree_view.UpdateBelow (parent);
271 private TreeNode GetPrevNode (TreeNode node)
273 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
275 if (one.MovePrevious () && one.MovePrevious ())
276 return one.CurrentNode;
280 private void SetupNode (TreeNode node)
282 // Remove it from any old parents
287 TreeView tree_view = null;
289 tree_view = owner.TreeView;
291 if (tree_view != null) {
292 TreeNode prev = GetPrevNode (node);
294 if (tree_view.top_node == null)
295 tree_view.top_node = node;
298 tree_view.RecalculateVisibleOrder (prev);
299 tree_view.UpdateScrollBars ();
302 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
303 // tree_view.UpdateBelow (owner);
304 tree_view.UpdateNode (owner);
305 tree_view.UpdateNode (node);
306 } else if (owner != null && tree_view != null) {
307 tree_view.UpdateBelow (owner);
311 int IList.Add (object node)
313 return Add ((TreeNode) node);
316 bool IList.Contains (object node)
318 return Contains ((TreeNode) node);
321 int IList.IndexOf (object node)
323 return IndexOf ((TreeNode) node);
326 void IList.Insert (int index, object node)
328 Insert (index, (TreeNode) node);
331 void IList.Remove (object node)
333 Remove ((TreeNode) node);
336 private int AddSorted (TreeNode node)
338 if (count >= nodes.Length)
341 CompareInfo compare = Application.CurrentCulture.CompareInfo;
344 for (int i = 0; i < count; i++) {
346 int comp = compare.Compare (node.Text, nodes [i].Text);
353 // Stick it at the end
357 // Move the nodes up and adjust their indices
358 for (int i = count - 1; i >= pos; i--) {
359 nodes [i + 1] = nodes [i];
367 // Would be nice to do this without running through the collection twice
368 internal void Sort () {
370 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
372 for (int i = 0; i < count; i++) {
373 nodes [i].Nodes.Sort ();
376 // No null checks since sort can only be called from the treeviews root node collection
377 owner.TreeView.RecalculateVisibleOrder (owner);
378 owner.TreeView.UpdateScrollBars ();
383 TreeNode [] nn = new TreeNode [nodes.Length + 50];
384 Array.Copy (nodes, nn, nodes.Length);
388 private void Shrink ()
390 int len = (count > OrigSize ? count : OrigSize);
391 TreeNode [] nn = new TreeNode [len];
392 Array.Copy (nodes, nn, count);
397 internal class TreeNodeEnumerator : IEnumerator {
399 private TreeNodeCollection collection;
400 private int index = -1;
402 public TreeNodeEnumerator (TreeNodeCollection collection)
404 this.collection = collection;
407 public object Current {
408 get { return collection [index]; }
411 public bool MoveNext ()
413 if (index + 1 >= collection.Count)
425 private class TreeNodeComparer : IComparer {
427 private CompareInfo compare;
429 public TreeNodeComparer (CompareInfo compare)
431 this.compare = compare;
434 public int Compare (object x, object y)
436 TreeNode l = (TreeNode) x;
437 TreeNode r = (TreeNode) y;
438 int res = compare.Compare (l.Text, r.Text);
440 return (res == 0 ? l.Index - r.Index : res);