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);
162 public bool Contains (TreeNode node)
164 return (Array.BinarySearch (nodes, node) > 0);
167 public void CopyTo (Array dest, int index)
169 Array.Copy (nodes, index, dest, index, count);
172 public IEnumerator GetEnumerator ()
174 return new TreeNodeEnumerator (this);
177 public int IndexOf (TreeNode node)
179 return Array.IndexOf (nodes, node);
182 public virtual void Insert (int index, TreeNode node)
184 if (count >= nodes.Length)
187 Array.Copy (nodes, index, nodes, index + 1, count - index);
188 nodes [index] = node;
194 public void Remove (TreeNode node)
196 int index = IndexOf (node);
201 public virtual void RemoveAt (int index)
203 RemoveAt (index, true);
206 private void RemoveAt (int index, bool update)
208 TreeNode removed = nodes [index];
209 TreeNode prev = GetPrevNode (removed);
210 TreeNode new_selected = null;
211 bool visible = removed.IsVisible;
213 TreeView tree_view = null;
215 tree_view = owner.TreeView;
217 if (tree_view != null) {
218 tree_view.RecalculateVisibleOrder (prev);
219 if (removed == tree_view.top_node) {
221 if (removed.IsRoot) {
222 tree_view.top_node = null;
224 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
225 if (oe.MovePrevious () && oe.MovePrevious ()) {
226 tree_view.top_node = oe.CurrentNode;
228 removed.is_expanded = false;
229 oe = new OpenTreeNodeEnumerator (removed);
230 if (oe.MoveNext () && oe.MoveNext ()) {
231 tree_view.top_node = oe.CurrentNode;
233 tree_view.top_node = null;
238 if (removed == tree_view.selected_node) {
239 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
240 if (oe.MoveNext () && oe.MoveNext ()) {
241 new_selected = oe.CurrentNode;
243 oe = new OpenTreeNodeEnumerator (removed);
245 new_selected = oe.CurrentNode;
250 Array.Copy (nodes, index + 1, nodes, index, count - index);
252 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
255 if (tree_view != null && new_selected != null) {
256 tree_view.SelectedNode = new_selected;
259 TreeNode parent = removed.parent;
260 removed.parent = null;
262 if (update && tree_view != null && visible) {
263 tree_view.RecalculateVisibleOrder (prev);
264 tree_view.UpdateScrollBars ();
265 tree_view.UpdateBelow (parent);
269 private TreeNode GetPrevNode (TreeNode node)
271 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
273 if (one.MovePrevious () && one.MovePrevious ())
274 return one.CurrentNode;
278 private void SetupNode (TreeNode node)
280 // Remove it from any old parents
285 TreeView tree_view = null;
287 tree_view = owner.TreeView;
289 if (tree_view != null) {
290 TreeNode prev = GetPrevNode (node);
292 if (tree_view.top_node == null)
293 tree_view.top_node = node;
296 tree_view.RecalculateVisibleOrder (prev);
297 tree_view.UpdateScrollBars ();
300 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
301 // tree_view.UpdateBelow (owner);
302 tree_view.UpdateNode (owner);
303 tree_view.UpdateNode (node);
304 } else if (owner != null && tree_view != null) {
305 tree_view.UpdateBelow (owner);
309 int IList.Add (object node)
311 return Add ((TreeNode) node);
314 bool IList.Contains (object node)
316 return Contains ((TreeNode) node);
319 int IList.IndexOf (object node)
321 return IndexOf ((TreeNode) node);
324 void IList.Insert (int index, object node)
326 Insert (index, (TreeNode) node);
329 void IList.Remove (object node)
331 Remove ((TreeNode) node);
334 private int AddSorted (TreeNode node)
336 if (count >= nodes.Length)
339 CompareInfo compare = Application.CurrentCulture.CompareInfo;
342 for (int i = 0; i < count; i++) {
344 int comp = compare.Compare (node.Text, nodes [i].Text);
351 // Stick it at the end
355 // Move the nodes up and adjust their indices
356 for (int i = count - 1; i >= pos; i--) {
357 nodes [i + 1] = nodes [i];
365 // Would be nice to do this without running through the collection twice
366 internal void Sort () {
367 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
369 for (int i = 0; i < count; i++) {
370 nodes [i].Nodes.Sort ();
376 TreeNode [] nn = new TreeNode [nodes.Length + 50];
377 Array.Copy (nodes, nn, nodes.Length);
381 private void Shrink ()
383 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
384 TreeNode [] nn = new TreeNode [len];
385 Array.Copy (nodes, nn, count);
390 internal class TreeNodeEnumerator : IEnumerator {
392 private TreeNodeCollection collection;
393 private int index = -1;
395 public TreeNodeEnumerator (TreeNodeCollection collection)
397 this.collection = collection;
400 public object Current {
401 get { return collection [index]; }
404 public bool MoveNext ()
406 if (index + 1 >= collection.Count)
418 private class TreeNodeComparer : IComparer {
420 private CompareInfo compare;
422 public TreeNodeComparer (CompareInfo compare)
424 this.compare = compare;
427 public int Compare (object x, object y)
429 TreeNode l = (TreeNode) x;
430 TreeNode r = (TreeNode) y;
431 int res = compare.Compare (l.Text, r.Text);
433 return (res == 0 ? l.Index - r.Index : res);