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