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");
115 // Remove it from any old parents
119 TreeView tree_view = null;
121 tree_view = owner.TreeView;
122 node.indent_level = owner.indent_level + 1;
124 if (tree_view != null && tree_view.Sorted) {
125 res = AddSorted (node);
128 if (count >= nodes.Length)
130 nodes [count++] = node;
134 if (tree_view != null) {
135 TreeNode prev = GetPrevNode (node);
137 if (tree_view.top_node == null)
138 tree_view.top_node = node;
141 tree_view.RecalculateVisibleOrder (prev);
142 tree_view.UpdateScrollBars ();
145 if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
146 // tree_view.UpdateBelow (owner);
147 tree_view.UpdateNode (owner);
148 tree_view.UpdateNode (node);
149 } else if (owner != null && tree_view != null) {
150 tree_view.UpdateBelow (owner);
156 public virtual void AddRange (TreeNode [] nodes)
159 throw new ArgumentNullException("node");
161 // We can't just use Array.Copy because the nodes also
162 // need to have some properties set when they are added.
163 for (int i = 0; i < nodes.Length; i++)
167 public virtual void Clear ()
169 for (int i = 0; i < count; i++)
172 Array.Clear (nodes, 0, count);
175 TreeView tree_view = null;
177 tree_view = owner.TreeView;
179 tree_view.top_node = null;
180 if (tree_view != null)
181 tree_view.UpdateBelow (owner);
185 public bool Contains (TreeNode node)
187 return (Array.BinarySearch (nodes, node) > 0);
190 public void CopyTo (Array dest, int index)
192 nodes.CopyTo (dest, index);
195 public IEnumerator GetEnumerator ()
197 return new TreeNodeEnumerator (this);
200 public int IndexOf (TreeNode node)
202 return Array.IndexOf (nodes, node);
205 public virtual void Insert (int index, TreeNode node)
209 if (count >= nodes.Length)
212 Array.Copy (nodes, index, nodes, index + 1, count - index);
213 nodes [index] = node;
217 public void Remove (TreeNode node)
219 int index = IndexOf (node);
224 public virtual void RemoveAt (int index)
226 RemoveAt (index, true);
229 private void RemoveAt (int index, bool update)
231 TreeNode removed = nodes [index];
232 TreeNode prev = GetPrevNode (removed);
233 TreeNode new_selected = null;
234 bool visible = removed.IsVisible;
236 Array.Copy (nodes, index + 1, nodes, index, count - index);
238 if (nodes.Length > OrigSize && nodes.Length > (count * 2))
241 TreeView tree_view = null;
243 tree_view = owner.TreeView;
244 if (tree_view != null) {
245 if (removed == tree_view.top_node) {
246 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
247 if (oe.MoveNext () && oe.MoveNext ()) {
248 tree_view.top_node = oe.CurrentNode;
250 oe = new OpenTreeNodeEnumerator (removed);
252 tree_view.top_node = oe.CurrentNode;
255 if (removed == tree_view.selected_node) {
256 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
257 if (oe.MoveNext () && oe.MoveNext ()) {
258 new_selected = oe.CurrentNode;
260 oe = new OpenTreeNodeEnumerator (removed);
262 new_selected = oe.CurrentNode;
268 if (tree_view != null && new_selected != null) {
269 tree_view.SelectedNode = new_selected;
272 TreeNode parent = removed.parent;
273 removed.parent = null;
275 if (tree_view != null && visible) {
276 tree_view.RecalculateVisibleOrder (prev);
277 tree_view.UpdateScrollBars ();
278 tree_view.UpdateBelow (parent);
282 private TreeNode GetPrevNode (TreeNode node)
284 OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
286 if (one.MovePrevious () && one.MovePrevious ())
287 return one.CurrentNode;
291 int IList.Add (object node)
293 return Add ((TreeNode) node);
296 bool IList.Contains (object node)
298 return Contains ((TreeNode) node);
301 int IList.IndexOf (object node)
303 return IndexOf ((TreeNode) node);
306 void IList.Insert (int index, object node)
308 Insert (index, (TreeNode) node);
311 void IList.Remove (object node)
313 Remove ((TreeNode) node);
316 private int AddSorted (TreeNode node)
318 if (count >= nodes.Length)
321 CompareInfo compare = Application.CurrentCulture.CompareInfo;
324 for (int i = 0; i < count; i++) {
326 int comp = compare.Compare (node.Text, nodes [i].Text);
333 // Stick it at the end
337 // Move the nodes up and adjust their indices
338 for (int i = count - 1; i >= pos; i--) {
339 nodes [i + 1] = nodes [i];
348 // Would be nice to do this without running through the collection twice
349 internal void Sort () {
351 Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
353 for (int i = 0; i < count; i++) {
354 nodes [i].Nodes.Sort ();
357 // No null checks since sort can only be called from the treeviews root node collection
358 owner.TreeView.RecalculateVisibleOrder (owner);
359 owner.TreeView.UpdateScrollBars ();
364 TreeNode [] nn = new TreeNode [nodes.Length + 50];
365 Array.Copy (nodes, nn, nodes.Length);
369 private void Shrink ()
371 int len = (count > OrigSize ? count : OrigSize);
372 TreeNode [] nn = new TreeNode [len];
373 Array.Copy (nodes, nn, count);
378 internal class TreeNodeEnumerator : IEnumerator {
380 private TreeNodeCollection collection;
381 private int index = -1;
383 public TreeNodeEnumerator (TreeNodeCollection collection)
385 this.collection = collection;
388 public object Current {
389 get { return collection [index]; }
392 public bool MoveNext ()
394 if (index + 1 >= collection.Count)
406 private class TreeNodeComparer : IComparer {
408 private CompareInfo compare;
410 public TreeNodeComparer (CompareInfo compare)
412 this.compare = compare;
415 public int Compare (object x, object y)
417 TreeNode l = (TreeNode) x;
418 TreeNode r = (TreeNode) y;
419 int res = compare.Compare (l.Text, r.Text);
421 return (res == 0 ? l.Index - r.Index : res);