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