New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TreeNodeCollection.cs
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24
25
26 using System;
27 using System.Collections;
28 using System.ComponentModel;
29 using System.Globalization;
30
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 {
34
35                 private static readonly int OrigSize = 50;
36
37                 private TreeNode owner;
38                 private int count;
39                 private TreeNode [] nodes;
40
41                 private TreeNodeCollection ()
42                 {
43                 }
44
45                 internal TreeNodeCollection (TreeNode owner)
46                 {
47                         this.owner = owner;
48                         nodes = new TreeNode [OrigSize];
49                 }
50
51                 [Browsable(false)]
52                 [EditorBrowsable(EditorBrowsableState.Advanced)]
53                 public int Count {
54                         get { return count; }
55                 }
56
57                 public bool IsReadOnly {
58                         get { return false; }
59                 }
60
61                 bool ICollection.IsSynchronized {
62                         get { return false; }
63                 }
64
65                 object ICollection.SyncRoot {
66                         get { return this; }
67                 }
68
69                 bool IList.IsFixedSize {
70                         get { return false; }
71                 }
72
73                 object IList.this [int index] {
74                         get {
75                                 if (index < 0 || index >= Count)
76                                         throw new ArgumentOutOfRangeException ("index");
77                                 return nodes [index];
78                         }
79                         set {
80                                 if (index < 0 || index >= Count)
81                                         throw new ArgumentOutOfRangeException ("index");
82                                 TreeNode node = (TreeNode) value;
83                                 SetupNode (node);
84                                 nodes [index] = node;
85                         }
86                 }
87
88                 public virtual TreeNode this [int index] {
89                         get {
90                                 if (index < 0 || index >= Count)
91                                         throw new ArgumentOutOfRangeException ("index");
92                                 return nodes [index];
93                         }
94                         set {
95                                 if (index < 0 || index >= Count)
96                                         throw new ArgumentOutOfRangeException ("index");
97                                 SetupNode (value);
98                                 nodes [index] = value;
99                         }
100                 }
101
102                 public virtual TreeNode Add (string text)
103                 {
104                         TreeNode res = new TreeNode (text);
105                         Add (res);
106                         return res;
107                 }
108
109                 public virtual int Add (TreeNode node)
110                 {
111                         if (node == null)
112                                 throw new ArgumentNullException("node");
113
114                         int res;
115                         TreeView tree_view = null;
116
117                         if (tree_view != null && tree_view.Sorted) {
118                                 res = AddSorted (node);
119                         } else {
120                                 if (count >= nodes.Length)
121                                         Grow ();
122                                 nodes [count++] = node;
123                                 res = count;
124                         }
125
126                         SetupNode (node);
127
128                         return res;
129                 }
130
131                 public virtual void AddRange (TreeNode [] nodes)
132                 {
133                         if (nodes == null)
134                                 throw new ArgumentNullException("node");
135
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++)
139                                 Add (nodes [i]);
140                 }
141
142                 public virtual void Clear ()
143                 {
144                         while (count > 0)
145                                 RemoveAt (0, false);
146                         
147                         Array.Clear (nodes, 0, count);
148                         count = 0;
149
150                         TreeView tree_view = null;
151                         if (owner != null) {
152                                 tree_view = owner.TreeView;
153                                 if (owner.IsRoot)
154                                         tree_view.top_node = null;
155                                 if (tree_view != null) {
156                                         tree_view.UpdateBelow (owner);
157                                         tree_view.RecalculateVisibleOrder (owner);
158                                 }
159                         }
160                 }
161
162                 public bool Contains (TreeNode node)
163                 {
164                         return (Array.BinarySearch (nodes, node) > 0);
165                 }
166
167                 public void CopyTo (Array dest, int index)
168                 {
169                         Array.Copy (nodes, index, dest, index, count);
170                 }
171
172                 public IEnumerator GetEnumerator ()
173                 {
174                         return new TreeNodeEnumerator (this);
175                 }
176
177                 public int IndexOf (TreeNode node)
178                 {
179                         return Array.IndexOf (nodes, node);
180                 }
181
182                 public virtual void Insert (int index, TreeNode node)
183                 {
184                         if (count >= nodes.Length)
185                                 Grow ();
186
187                         Array.Copy (nodes, index, nodes, index + 1, count - index);
188                         nodes [index] = node;
189                         count++;
190
191                         SetupNode (node);
192                 }
193
194                 public void Remove (TreeNode node)
195                 {
196                         int index = IndexOf (node);
197                         if (index > 0)
198                                 RemoveAt (index);
199                 }
200
201                 public virtual void RemoveAt (int index)
202                 {
203                         RemoveAt (index, true);
204                 }
205
206                 private void RemoveAt (int index, bool update)
207                 {
208                         TreeNode removed = nodes [index];
209                         TreeNode prev = GetPrevNode (removed);
210                         TreeNode new_selected = null;
211                         bool visible = removed.IsVisible;
212
213                         TreeView tree_view = null;
214                         if (owner != null)
215                                 tree_view = owner.TreeView;
216
217                         if (tree_view != null) {
218                                 tree_view.RecalculateVisibleOrder (prev);
219                                 if (removed == tree_view.top_node) {
220
221                                         if (removed.IsRoot) {
222                                                 tree_view.top_node = null;
223                                         } else {
224                                                 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
225                                                 if (oe.MovePrevious () && oe.MovePrevious ()) {
226                                                         tree_view.top_node = oe.CurrentNode;
227                                                 } else {
228                                                         removed.is_expanded = false;
229                                                         oe = new OpenTreeNodeEnumerator (removed);
230                                                         if (oe.MoveNext () && oe.MoveNext ()) {
231                                                                 tree_view.top_node = oe.CurrentNode;
232                                                         } else {
233                                                                 tree_view.top_node = null;
234                                                         }
235                                                 }
236                                         }
237                                 }
238                                 if (removed == tree_view.selected_node) {
239                                         OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
240                                         if (oe.MoveNext () && oe.MoveNext ()) {
241                                                 new_selected = oe.CurrentNode;
242                                         } else {
243                                                 oe = new OpenTreeNodeEnumerator (removed);
244                                                 oe.MovePrevious ();
245                                                 new_selected = oe.CurrentNode;
246                                         }
247                                 }
248                         }
249
250                         Array.Copy (nodes, index + 1, nodes, index, count - index);
251                         count--;
252                         if (nodes.Length > OrigSize && nodes.Length > (count * 2))
253                                 Shrink ();
254
255                         if (tree_view != null && new_selected != null) {
256                                 tree_view.SelectedNode = new_selected;
257                         }
258
259                         TreeNode parent = removed.parent;
260                         removed.parent = null;
261
262                         if (update && tree_view != null && visible) {
263                                 tree_view.RecalculateVisibleOrder (prev);
264                                 tree_view.UpdateScrollBars ();
265                                 tree_view.UpdateBelow (parent);
266                         }
267                 }
268
269                 private TreeNode GetPrevNode (TreeNode node)
270                 {
271                         OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
272
273                         if (one.MovePrevious () && one.MovePrevious ())
274                                 return one.CurrentNode;
275                         return null;
276                 }
277
278                 private void SetupNode (TreeNode node)
279                 {
280                         // Remove it from any old parents
281                         node.Remove ();
282
283                         node.parent = owner;
284
285                         TreeView tree_view = null;
286                         if (owner != null)
287                                 tree_view = owner.TreeView;
288
289                         if (tree_view != null) {
290                                 TreeNode prev = GetPrevNode (node);
291
292                                 if (tree_view.top_node == null)
293                                         tree_view.top_node = node;
294
295                                 if (node.IsVisible)
296                                         tree_view.RecalculateVisibleOrder (prev);
297                                 tree_view.UpdateScrollBars ();
298                         }
299
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);
306                         }
307                 }
308
309                 int IList.Add (object node)
310                 {
311                         return Add ((TreeNode) node);
312                 }
313
314                 bool IList.Contains (object node)
315                 {
316                         return Contains ((TreeNode) node);
317                 }
318                 
319                 int IList.IndexOf (object node)
320                 {
321                         return IndexOf ((TreeNode) node);
322                 }
323
324                 void IList.Insert (int index, object node)
325                 {
326                         Insert (index, (TreeNode) node);
327                 }
328
329                 void IList.Remove (object node)
330                 {
331                         Remove ((TreeNode) node);
332                 }
333
334                 private int AddSorted (TreeNode node)
335                 {
336                         if (count >= nodes.Length)
337                                 Grow ();
338
339                         CompareInfo compare = Application.CurrentCulture.CompareInfo;
340                         int pos = 0;
341                         bool found = false;
342                         for (int i = 0; i < count; i++) {
343                                 pos = i;
344                                 int comp = compare.Compare (node.Text, nodes [i].Text);
345                                 if (comp < 0) {
346                                         found = true;
347                                         break;
348                                 }
349                         }
350
351                         // Stick it at the end
352                         if (!found)
353                                 pos = count;
354
355                         // Move the nodes up and adjust their indices
356                         for (int i = count - 1; i >= pos; i--) {
357                                 nodes [i + 1] = nodes [i];
358                         }
359                         count++;
360                         nodes [pos] = node;
361
362                         return count;
363                 }
364
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));
368
369                         for (int i = 0; i < count; i++) {
370                                 nodes [i].Nodes.Sort ();
371                         }
372                 }
373
374                 private void Grow ()
375                 {
376                         TreeNode [] nn = new TreeNode [nodes.Length + 50];
377                         Array.Copy (nodes, nn, nodes.Length);
378                         nodes = nn;
379                 }
380
381                 private void Shrink ()
382                 {
383                         int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
384                         TreeNode [] nn = new TreeNode [len];
385                         Array.Copy (nodes, nn, count);
386                         nodes = nn;
387                 }
388
389                 
390                 internal class TreeNodeEnumerator : IEnumerator {
391
392                         private TreeNodeCollection collection;
393                         private int index = -1;
394
395                         public TreeNodeEnumerator (TreeNodeCollection collection)
396                         {
397                                 this.collection = collection;
398                         }
399
400                         public object Current {
401                                 get { return collection [index]; }
402                         }
403
404                         public bool MoveNext ()
405                         {
406                                 if (index + 1 >= collection.Count)
407                                         return false;
408                                 index++;
409                                 return true;
410                         }
411
412                         public void Reset ()
413                         {
414                                 index = 0;
415                         }
416                 }
417
418                 private class TreeNodeComparer : IComparer {
419
420                         private CompareInfo compare;
421                 
422                         public TreeNodeComparer (CompareInfo compare)
423                         {
424                                 this.compare = compare;
425                         }
426                 
427                         public int Compare (object x, object y)
428                         {
429                                 TreeNode l = (TreeNode) x;
430                                 TreeNode r = (TreeNode) y;
431                                 int res = compare.Compare (l.Text, r.Text);
432
433                                 return (res == 0 ? l.Index - r.Index : res);
434                         }
435                 }
436         }
437 }
438