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