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] {
75 if (index < 0 || index >= Count)
76 throw new ArgumentOutOfRangeException ("index");
80 if (index < 0 || index >= Count)
81 throw new ArgumentOutOfRangeException ("index");
82 TreeNode node = (TreeNode) value;
88 public virtual TreeNode this [int index] {
90 if (index < 0 || index >= Count)
91 throw new ArgumentOutOfRangeException ("index");
95 if (index < 0 || index >= Count)
96 throw new ArgumentOutOfRangeException ("index");
98 nodes [index] = value;
102 public virtual TreeNode Add (string text)
104 TreeNode res = new TreeNode (text);
109 public virtual int Add (TreeNode node)
112 throw new ArgumentNullException("node");
115 TreeView tree_view = null;
117 if (tree_view != null && tree_view.Sorted) {
118 res = AddSorted (node);
120 if (count >= nodes.Length)
122 nodes [count++] = node;
131 public virtual void AddRange (TreeNode [] nodes)
134 throw new ArgumentNullException("node");
136 // We can't just use Array.Copy because the nodes also
137 // need to have some properties set when they are added.
138 for (int i = 0; i < nodes.Length; i++)
142 public virtual void Clear ()
147 Array.Clear (nodes, 0, count);
150 TreeView tree_view = null;
152 tree_view = owner.TreeView;
154 tree_view.top_node = null;
155 if (tree_view != null) {
156 tree_view.UpdateBelow (owner);
157 tree_view.RecalculateVisibleOrder (owner);
158 tree_view.UpdateScrollBars ();
163 public bool Contains (TreeNode node)
165 return (Array.BinarySearch (nodes, node) > 0);
168 public void CopyTo (Array dest, int index)
170 Array.Copy (nodes, index, dest, index, count);
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)
198 throw new NullReferenceException ();
200 int index = IndexOf (node);
205 throw new NullReferenceException ();
209 public virtual void RemoveAt (int index)
211 RemoveAt (index, true);
214 private void RemoveAt (int index, bool update)
216 TreeNode removed = nodes [index];
217 TreeNode prev = GetPrevNode (removed);
218 TreeNode new_selected = null;
219 bool visible = removed.IsVisible;
221 TreeView tree_view = null;
223 tree_view = owner.TreeView;
225 if (tree_view != null) {
226 tree_view.RecalculateVisibleOrder (prev);
227 if (removed == tree_view.top_node) {
229 if (removed.IsRoot) {
230 tree_view.top_node = null;
232 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
233 if (oe.MovePrevious () && oe.MovePrevious ()) {
234 tree_view.top_node = oe.CurrentNode;
236 removed.is_expanded = false;
237 oe = new OpenTreeNodeEnumerator (removed);
238 if (oe.MoveNext () && oe.MoveNext ()) {
239 tree_view.top_node = oe.CurrentNode;
241 tree_view.top_node = null;
246 if (removed == tree_view.selected_node) {
247 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
248 if (oe.MoveNext () && oe.MoveNext ()) {
249 new_selected = oe.CurrentNode;
251 oe = new OpenTreeNodeEnumerator (removed);
253 new_selected = oe.CurrentNode;
258 Array.Copy (nodes, index + 1, nodes, index, count - index);
260 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
263 if (tree_view != null && new_selected != null) {
264 tree_view.SelectedNode = new_selected;
267 TreeNode parent = removed.parent;
268 removed.parent = null;
270 if (update && tree_view != null && visible) {
271 tree_view.RecalculateVisibleOrder (prev);
272 tree_view.UpdateScrollBars ();
273 tree_view.UpdateBelow (parent);
277 private TreeNode GetPrevNode (TreeNode node)
279 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
281 if (one.MovePrevious () && one.MovePrevious ())
282 return one.CurrentNode;
286 private void SetupNode (TreeNode node)
288 // Remove it from any old parents
293 TreeView tree_view = null;
295 tree_view = owner.TreeView;
297 if (tree_view != null) {
298 TreeNode prev = GetPrevNode (node);
300 if (tree_view.top_node == null)
301 tree_view.top_node = node;
304 tree_view.RecalculateVisibleOrder (prev);
305 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
306 tree_view.UpdateScrollBars ();
309 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
310 // tree_view.UpdateBelow (owner);
311 tree_view.UpdateNode (owner);
312 tree_view.UpdateNode (node);
313 } else if (owner != null && tree_view != null) {
314 tree_view.UpdateBelow (owner);
318 int IList.Add (object node)
320 return Add ((TreeNode) node);
323 bool IList.Contains (object node)
325 return Contains ((TreeNode) node);
328 int IList.IndexOf (object node)
330 return IndexOf ((TreeNode) node);
333 void IList.Insert (int index, object node)
335 Insert (index, (TreeNode) node);
338 void IList.Remove (object node)
340 Remove ((TreeNode) node);
343 private int AddSorted (TreeNode node)
345 if (count >= nodes.Length)
348 CompareInfo compare = Application.CurrentCulture.CompareInfo;
351 for (int i = 0; i < count; i++) {
353 int comp = compare.Compare (node.Text, nodes [i].Text);
360 // Stick it at the end
364 // Move the nodes up and adjust their indices
365 for (int i = count - 1; i >= pos; i--) {
366 nodes [i + 1] = nodes [i];
374 // Would be nice to do this without running through the collection twice
375 internal void Sort () {
376 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
378 for (int i = 0; i < count; i++) {
379 nodes [i].Nodes.Sort ();
385 TreeNode [] nn = new TreeNode [nodes.Length + 50];
386 Array.Copy (nodes, nn, nodes.Length);
390 private void Shrink ()
392 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
393 TreeNode [] nn = new TreeNode [len];
394 Array.Copy (nodes, nn, count);
399 internal class TreeNodeEnumerator : IEnumerator {
401 private TreeNodeCollection collection;
402 private int index = -1;
404 public TreeNodeEnumerator (TreeNodeCollection collection)
406 this.collection = collection;
409 public object Current {
413 return collection [index];
417 public bool MoveNext ()
419 if (index + 1 >= collection.Count)
431 private class TreeNodeComparer : IComparer {
433 private CompareInfo compare;
435 public TreeNodeComparer (CompareInfo compare)
437 this.compare = compare;
440 public int Compare (object x, object y)
442 TreeNode l = (TreeNode) x;
443 TreeNode r = (TreeNode) y;
444 int res = compare.Compare (l.Text, r.Text);
446 return (res == 0 ? l.Index - r.Index : res);