2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / ControlCollection.cs
1 //
2 // System.Web.UI.ControlCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (c) 2002-2005 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI {
34
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public class ControlCollection : ICollection, IEnumerable
39         {
40                 Control owner;
41                 Control [] controls;
42                 int version;
43                 int count;
44                 bool readOnly;
45                 
46                 public ControlCollection (Control owner)
47                 {
48                         if (owner == null)
49                                 throw new ArgumentException ("owner");
50
51                         this.owner = owner;
52                 }
53
54                 public
55 #if NET_2_0
56                 virtual
57 #endif
58                 int Count {
59                         get { return count; }
60                 }
61
62                 public bool IsReadOnly {
63                         get { return readOnly; }
64                 }
65
66                 public bool  IsSynchronized {
67                         get { return false; }
68                 }
69
70                 public virtual Control this [int index] {
71                         get {
72                                 if (index < 0 || index >= count)
73                                         throw new ArgumentOutOfRangeException ("index");
74
75                                 return controls [index];
76                         }
77                 }
78
79                 protected Control Owner {
80                         get { return owner; }
81                 }
82
83                 public object SyncRoot {
84                         get { return this; }
85                 }
86
87                 void EnsureControls ()
88                 {
89                         if (controls == null) {
90                                 controls = new Control [5];
91                         } else if (controls.Length < count + 1) {
92                                 int n = controls.Length == 5 ? 3 : 2;
93                                 Control [] newControls = new Control [controls.Length * n];
94                                 Array.Copy (controls, 0, newControls, 0, controls.Length);
95                                 controls = newControls;
96                         }
97                 }
98
99                 public virtual void Add (Control child)
100                 {
101                         if (child == null)
102                                 throw new ArgumentNullException ("child");
103
104                         if (readOnly)
105                                 throw new HttpException (Locale.GetText ("Collection is read-only."));
106
107                         if (Object.ReferenceEquals (owner, child))
108                                 throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
109
110                         EnsureControls ();
111                         version++;
112                         controls [count++] = child;
113                         owner.AddedControl (child, count - 1);
114                 }
115
116                 public virtual void AddAt (int index, Control child)
117                 {
118                         if (child == null) // maybe we should check for ! (child is Control)?
119                                 throw new ArgumentNullException ();
120                         
121                         if (index < -1 || index > count)
122                                 throw new ArgumentOutOfRangeException ();
123
124                         if (readOnly)
125                                 throw new HttpException (Locale.GetText ("Collection is read-only."));
126
127                         if (Object.ReferenceEquals (owner, child))
128                                 throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
129
130                         if (index == -1) {
131                                 Add (child);
132                                 return;
133                         }
134
135                         EnsureControls ();
136                         version++;
137                         Array.Copy (controls, index, controls, index + 1, count - index);
138                         count++;
139                         controls [index] = child;
140                         owner.AddedControl (child, index);
141                 }
142
143                 public virtual void Clear ()
144                 {
145                         if (controls == null)
146                                 return;
147
148                         version++;
149                         for (int i = 0; i < count; i++)
150                                 owner.RemovedControl (controls [i]);
151
152                         count = 0;
153                         if (owner != null)
154                                 owner.ResetChildNames ();
155                 }
156
157                 public virtual bool Contains (Control c)
158                 {
159                         return (controls != null && Array.IndexOf (controls, c) != -1);
160                 }
161
162                 public
163 #if NET_2_0
164                 virtual
165 #endif
166                 void CopyTo (Array array, int index)
167                 {
168                         if (controls == null)
169                                 return;
170
171                         // can't use controls.CopyTo (array, index);
172                         // as we do not allocate it based on the true 
173                         // numbers of items we have in the collection
174                         // so we must re-implement Array.CopyTo :(
175
176                         if (array == null)
177                                 throw new ArgumentNullException ("array");
178                         if (index + count > array.GetLowerBound (0) + array.GetLength (0))
179                                 throw new ArgumentException ();
180                         if (array.Rank > 1)
181                                 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
182                         if (index < 0)
183                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText ("Value has to be >= 0."));
184
185                         for (int i=0; i < count; i++)
186                                 array.SetValue (controls [i], i + index);
187                 }
188
189                 public
190 #if NET_2_0
191                 virtual
192 #endif
193                 IEnumerator GetEnumerator ()
194                 {
195                         return new SimpleEnumerator (this);
196                 }
197
198                 public virtual int IndexOf (Control c)
199                 {
200                         if (controls == null || c == null)
201                                 return -1;
202
203                         return Array.IndexOf (controls, c);
204                 }
205
206                 public virtual void Remove (Control value)
207                 {
208                         int idx = IndexOf (value);
209                         if (idx == -1)
210                                 return;
211                         RemoveAt (idx);
212                 }
213
214                 public virtual void RemoveAt (int index)
215                 {
216                         if (readOnly)
217                                 throw new HttpException ();
218
219                         version++;
220                         Control ctrl = controls [index];
221                         count--;
222                         if (count - index > 0)
223                                 Array.Copy (controls, index + 1, controls, index, count - index);
224
225                         controls [count] = null;
226                         owner.RemovedControl (ctrl);
227                 }
228                 
229                 internal void SetReadonly (bool readOnly)
230                 {
231                         this.readOnly = readOnly;
232                 }
233
234                 // Almost the same as in ArrayList
235                 sealed class SimpleEnumerator : IEnumerator
236                 {
237                         ControlCollection coll;
238                         int index;
239                         int version;
240                         object currentElement;
241                                                         
242                         public SimpleEnumerator (ControlCollection coll)
243                         {
244                                 this.coll = coll;
245                                 index = -1;
246                                 version = coll.version;
247                         }
248         
249                         public bool MoveNext ()
250                         {
251                                 if (version != coll.version)
252                                         throw new InvalidOperationException ("List has changed.");
253                                 
254                                 if (index >= -1 && ++index < coll.Count) {
255                                         currentElement = coll [index];
256                                         return true;
257                                 } else {
258                                         index = -2;
259                                         return false;
260                                 }
261                         }
262         
263                         public object Current {
264                                 get {
265                                         if (index < 0)
266                                                 throw new InvalidOperationException (index == -1 ? "Enumerator not started" : "Enumerator ended");
267                                         
268                                         return currentElement;
269                                 }
270                         }
271         
272                         public void Reset ()
273                         {
274                                 if (version != coll.version)
275                                         throw new InvalidOperationException ("List has changed.");
276                                 
277                                 index = -1;
278                         }
279                 }
280         }
281 }
282