c5d83057dbdc9e2c77ebda445cc8e4d427b58fc0
[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                         if (node == null)
197                                 throw new NullReferenceException ();
198
199                         int index = IndexOf (node);
200                         if (index != -1)
201                                 RemoveAt (index);
202 #if ONLY_1_1
203                         else
204                                 throw new NullReferenceException ();
205 #endif
206                 }
207
208                 public virtual void RemoveAt (int index)
209                 {
210                         RemoveAt (index, true);
211                 }
212
213                 private void RemoveAt (int index, bool update)
214                 {
215                         TreeNode removed = nodes [index];
216                         TreeNode prev = GetPrevNode (removed);
217                         TreeNode new_selected = null;
218                         bool visible = removed.IsVisible;
219
220                         TreeView tree_view = null;
221                         if (owner != null)
222                                 tree_view = owner.TreeView;
223
224                         if (tree_view != null) {
225                                 tree_view.RecalculateVisibleOrder (prev);
226                                 if (removed == tree_view.top_node) {
227
228                                         if (removed.IsRoot) {
229                                                 tree_view.top_node = null;
230                                         } else {
231                                                 OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
232                                                 if (oe.MovePrevious () && oe.MovePrevious ()) {
233                                                         tree_view.top_node = oe.CurrentNode;
234                                                 } else {
235                                                         removed.is_expanded = false;
236                                                         oe = new OpenTreeNodeEnumerator (removed);
237                                                         if (oe.MoveNext () && oe.MoveNext ()) {
238                                                                 tree_view.top_node = oe.CurrentNode;
239                                                         } else {
240                                                                 tree_view.top_node = null;
241                                                         }
242                                                 }
243                                         }
244                                 }
245                                 if (removed == tree_view.selected_node) {
246                                         OpenTreeNodeEnumerator oe = new OpenTreeNodeEnumerator (removed);
247                                         if (oe.MoveNext () && oe.MoveNext ()) {
248                                                 new_selected = oe.CurrentNode;
249                                         } else {
250                                                 oe = new OpenTreeNodeEnumerator (removed);
251                                                 oe.MovePrevious ();
252                                                 new_selected = oe.CurrentNode;
253                                         }
254                                 }
255                         }
256
257                         Array.Copy (nodes, index + 1, nodes, index, count - index);
258                         count--;
259                         if (nodes.Length > OrigSize && nodes.Length > (count * 2))
260                                 Shrink ();
261
262                         if (tree_view != null && new_selected != null) {
263                                 tree_view.SelectedNode = new_selected;
264                         }
265
266                         TreeNode parent = removed.parent;
267                         removed.parent = null;
268
269                         if (update && tree_view != null && visible) {
270                                 tree_view.RecalculateVisibleOrder (prev);
271                                 tree_view.UpdateScrollBars ();
272                                 tree_view.UpdateBelow (parent);
273                         }
274                 }
275
276                 private TreeNode GetPrevNode (TreeNode node)
277                 {
278                         OpenTreeNodeEnumerator one = new OpenTreeNodeEnumerator (node);
279
280                         if (one.MovePrevious () && one.MovePrevious ())
281                                 return one.CurrentNode;
282                         return null;
283                 }
284
285                 private void SetupNode (TreeNode node)
286                 {
287                         // Remove it from any old parents
288                         node.Remove ();
289
290                         node.parent = owner;
291
292                         TreeView tree_view = null;
293                         if (owner != null)
294                                 tree_view = owner.TreeView;
295
296                         if (tree_view != null) {
297                                 TreeNode prev = GetPrevNode (node);
298
299                                 if (tree_view.top_node == null)
300                                         tree_view.top_node = node;
301
302                                 if (node.IsVisible)
303                                         tree_view.RecalculateVisibleOrder (prev);
304                                 if (owner == tree_view.root_node || node.Parent.IsVisible && node.Parent.IsExpanded)
305                                         tree_view.UpdateScrollBars ();
306                         }
307
308                         if (owner != null && tree_view != null && (owner.IsExpanded || owner.IsRoot)) {
309                                 // tree_view.UpdateBelow (owner);
310                                 tree_view.UpdateNode (owner);
311                                 tree_view.UpdateNode (node);
312                         } else if (owner != null && tree_view != null) {
313                                 tree_view.UpdateBelow (owner);
314                         }
315                 }
316
317                 int IList.Add (object node)
318                 {
319                         return Add ((TreeNode) node);
320                 }
321
322                 bool IList.Contains (object node)
323                 {
324                         return Contains ((TreeNode) node);
325                 }
326                 
327                 int IList.IndexOf (object node)
328                 {
329                         return IndexOf ((TreeNode) node);
330                 }
331
332                 void IList.Insert (int index, object node)
333                 {
334                         Insert (index, (TreeNode) node);
335                 }
336
337                 void IList.Remove (object node)
338                 {
339                         Remove ((TreeNode) node);
340                 }
341
342                 private int AddSorted (TreeNode node)
343                 {
344                         if (count >= nodes.Length)
345                                 Grow ();
346
347                         CompareInfo compare = Application.CurrentCulture.CompareInfo;
348                         int pos = 0;
349                         bool found = false;
350                         for (int i = 0; i < count; i++) {
351                                 pos = i;
352                                 int comp = compare.Compare (node.Text, nodes [i].Text);
353                                 if (comp < 0) {
354                                         found = true;
355                                         break;
356                                 }
357                         }
358
359                         // Stick it at the end
360                         if (!found)
361                                 pos = count;
362
363                         // Move the nodes up and adjust their indices
364                         for (int i = count - 1; i >= pos; i--) {
365                                 nodes [i + 1] = nodes [i];
366                         }
367                         count++;
368                         nodes [pos] = node;
369
370                         return count;
371                 }
372
373                 // Would be nice to do this without running through the collection twice
374                 internal void Sort () {
375                         Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
376
377                         for (int i = 0; i < count; i++) {
378                                 nodes [i].Nodes.Sort ();
379                         }
380                 }
381
382                 private void Grow ()
383                 {
384                         TreeNode [] nn = new TreeNode [nodes.Length + 50];
385                         Array.Copy (nodes, nn, nodes.Length);
386                         nodes = nn;
387                 }
388
389                 private void Shrink ()
390                 {
391                         int len = (count + 1 > OrigSize ? count + 1 : OrigSize);
392                         TreeNode [] nn = new TreeNode [len];
393                         Array.Copy (nodes, nn, count);
394                         nodes = nn;
395                 }
396
397                 
398                 internal class TreeNodeEnumerator : IEnumerator {
399
400                         private TreeNodeCollection collection;
401                         private int index = -1;
402
403                         public TreeNodeEnumerator (TreeNodeCollection collection)
404                         {
405                                 this.collection = collection;
406                         }
407
408                         public object Current {
409                                 get {
410                                         if (index == -1)
411                                                 return null;
412                                         return collection [index];
413                                 }
414                         }
415
416                         public bool MoveNext ()
417                         {
418                                 if (index + 1 >= collection.Count)
419                                         return false;
420                                 index++;
421                                 return true;
422                         }
423
424                         public void Reset ()
425                         {
426                                 index = -1;
427                         }
428                 }
429
430                 private class TreeNodeComparer : IComparer {
431
432                         private CompareInfo compare;
433                 
434                         public TreeNodeComparer (CompareInfo compare)
435                         {
436                                 this.compare = compare;
437                         }
438                 
439                         public int Compare (object x, object y)
440                         {
441                                 TreeNode l = (TreeNode) x;
442                                 TreeNode r = (TreeNode) y;
443                                 int res = compare.Compare (l.Text, r.Text);
444
445                                 return (res == 0 ? l.Index - r.Index : res);
446                         }
447                 }
448         }
449 }
450