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 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
298 tree_view.UpdateScrollBars ();
301 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
302 // tree_view.UpdateBelow (owner);
303 tree_view.UpdateNode (owner);
304 tree_view.UpdateNode (node);
305 } else if (owner != null && tree_view != null) {
306 tree_view.UpdateBelow (owner);
310 int IList.Add (object node)
312 return Add ((TreeNode) node);
315 bool IList.Contains (object node)
317 return Contains ((TreeNode) node);
320 int IList.IndexOf (object node)
322 return IndexOf ((TreeNode) node);
325 void IList.Insert (int index, object node)
327 Insert (index, (TreeNode) node);
330 void IList.Remove (object node)
332 Remove ((TreeNode) node);
335 private int AddSorted (TreeNode node)
337 if (count >= nodes.Length)
340 CompareInfo compare = Application.CurrentCulture.CompareInfo;
343 for (int i = 0; i < count; i++) {
345 int comp = compare.Compare (node.Text, nodes [i].Text);
352 // Stick it at the end
356 // Move the nodes up and adjust their indices
357 for (int i = count - 1; i >= pos; i--) {
358 nodes [i + 1] = nodes [i];
366 // Would be nice to do this without running through the collection twice
367 internal void Sort () {
368 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
370 for (int i = 0; i < count; i++) {
371 nodes [i].Nodes.Sort ();
377 TreeNode [] nn = new TreeNode [nodes.Length + 50];
378 Array.Copy (nodes, nn, nodes.Length);
382 private void Shrink ()
384 int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
385 TreeNode [] nn = new TreeNode [len];
386 Array.Copy (nodes, nn, count);
391 internal class TreeNodeEnumerator : IEnumerator {
393 private TreeNodeCollection collection;
394 private int index = -1;
396 public TreeNodeEnumerator (TreeNodeCollection collection)
398 this.collection = collection;
401 public object Current {
402 get { return collection [index]; }
405 public bool MoveNext ()
407 if (index + 1 >= collection.Count)
419 private class TreeNodeComparer : IComparer {
421 private CompareInfo compare;
423 public TreeNodeComparer (CompareInfo compare)
425 this.compare = compare;
428 public int Compare (object x, object y)
430 TreeNode l = (TreeNode) x;
431 TreeNode r = (TreeNode) y;
432 int res = compare.Compare (l.Text, r.Text);
434 return (res == 0 ? l.Index - r.Index : res);