* TreeView.cs: Don't draw the selected node when we lose
[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 virtual int Count {
55                         get { return count; }
56                 }
57
58                 public virtual 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                                 SetData (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                                 SetData (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                         if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
116                                 return AddSorted (node);
117                         SetData (node);
118                         if (count >= nodes.Length)
119                                 Grow ();
120                         nodes [count++] = node;
121
122                         if (owner.TreeView != null && (owner.IsExpanded || owner.IsRoot)) {
123                                 // XXX: Need to ensure the boxes for the nodes have been created
124                                 owner.TreeView.UpdateNode (owner);
125                         } else if (owner.TreeView != null) {
126                                 owner.TreeView.UpdateNodePlusMinus (owner);
127                         }
128
129                         return count;
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                         Array.Clear (nodes, 0, count);
146                         count = 0;
147                 }
148
149                 public bool Contains (TreeNode node)
150                 {
151                         return (Array.BinarySearch (nodes, node) > 0);
152                 }
153
154                 public virtual void CopyTo (Array dest, int index)
155                 {
156                         nodes.CopyTo (dest, index);
157                 }
158
159                 public virtual IEnumerator GetEnumerator ()
160                 {
161                         return new TreeNodeEnumerator (this);
162                 }
163
164                 public int IndexOf (TreeNode node)
165                 {
166                         return Array.IndexOf (nodes, node);
167                 }
168
169                 public virtual void Insert (int index, TreeNode node)
170                 {
171                         SetData (node);
172
173                         if (count >= nodes.Length)
174                                 Grow ();
175
176                         Array.Copy (nodes, index, nodes, index + 1, count - index);
177                         nodes [index] = node;
178                         count++;
179                 }
180
181                 public virtual void Remove (TreeNode node)
182                 {
183                         int index = IndexOf (node);
184                         if (index > 0)
185                                 RemoveAt (index);
186                 }
187
188                 public virtual void RemoveAt (int index)
189                 {
190                         TreeNode removed = nodes [index];
191                         Array.Copy (nodes, index + 1, nodes, index, count - index);
192                         count--;
193                         if (nodes.Length > OrigSize && nodes.Length > (count * 2))
194                                 Shrink ();
195                         if (owner.TreeView != null)
196                                 owner.TreeView.UpdateNode (removed);
197                 }
198
199                 int IList.Add (object node)
200                 {
201                         return Add ((TreeNode) node);
202                 }
203
204                 bool IList.Contains (object node)
205                 {
206                         return Contains ((TreeNode) node);
207                 }
208                 
209                 int IList.IndexOf (object node)
210                 {
211                         return IndexOf ((TreeNode) node);
212                 }
213
214                 void IList.Insert (int index, object node)
215                 {
216                         Insert (index, (TreeNode) node);
217                 }
218
219                 void IList.Remove (object node)
220                 {
221                         Remove ((TreeNode) node);
222                 }
223
224                 private int AddSorted (TreeNode node)
225                 {
226                         
227                         if (count >= nodes.Length)
228                                 Grow ();
229
230                         CompareInfo compare = Application.CurrentCulture.CompareInfo;
231                         int pos = 0;
232                         bool found = false;
233                         for (int i = 0; i < count; i++) {
234                                 pos = i;
235                                 int comp = compare.Compare (node.Text, nodes [i].Text);
236                                 if (comp < 0) {
237                                         found = true;
238                                         break;
239                                 }
240                         }
241
242                         // Stick it at the end
243                         if (!found)
244                                 pos = count;
245
246                         // Move the nodes up and adjust their indices
247                         for (int i = count - 1; i >= pos; i--) {
248                                 nodes [i + 1] = nodes [i];
249                                 nodes [i + 1].SetIndex (i + 1);
250                         }
251                         count++;
252                         nodes [pos] = node;
253
254                         SetData (node, pos);
255                         return count;
256                 }
257
258                 // Would be nice to do this without running through the collection twice
259                 internal void Sort () {
260
261                         Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
262
263                         for (int i = 0; i < count; i++) {
264                                 nodes [i].SetIndex (i);
265                                 nodes [i].Nodes.Sort ();
266                         }
267                 }
268
269                 private void SetData (TreeNode node)
270                 {
271                         SetData (node, count);
272                 }
273
274                 private void SetData (TreeNode node, int pos)
275                 {
276                         node.SetAddedData ((owner != null ? owner.TreeView : null), owner, pos);
277                 }
278
279                 private void Grow ()
280                 {
281                         TreeNode [] nn = new TreeNode [nodes.Length + 50];
282                         Array.Copy (nodes, nn, nodes.Length);
283                         nodes = nn;
284                 }
285
286                 private void Shrink ()
287                 {
288                         int len = (count > OrigSize ? count : OrigSize);
289                         TreeNode [] nn = new TreeNode [len];
290                         Array.Copy (nodes, nn, count);
291                         nodes = nn;
292                 }
293
294                 
295                 internal class TreeNodeEnumerator : IEnumerator {
296
297                         private TreeNodeCollection collection;
298                         private int index = -1;
299
300                         public TreeNodeEnumerator (TreeNodeCollection collection)
301                         {
302                                 this.collection = collection;
303                         }
304
305                         public object Current {
306                                 get { return collection [index]; }
307                         }
308
309                         public bool MoveNext ()
310                         {
311                                 if (index + 1 >= collection.Count)
312                                         return false;
313                                 index++;
314                                 return true;
315                         }
316
317                         public void Reset ()
318                         {
319                                 index = 0;
320                         }
321                 }
322
323                 private class TreeNodeComparer : IComparer {
324
325                         private CompareInfo compare;
326                 
327                         public TreeNodeComparer (CompareInfo compare)
328                         {
329                                 this.compare = compare;
330                         }
331                 
332                         public int Compare (object x, object y)
333                         {
334                                 TreeNode l = (TreeNode) x;
335                                 TreeNode r = (TreeNode) y;
336                                 int res = compare.Compare (l.Text, r.Text);
337
338                                 return (res == 0 ? l.Index - r.Index : res);
339                         }
340                 }
341         }
342 }
343