Merge branch 'marek'
[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-2010 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 virtual int Count {
55                         get { return count; }
56                 }
57
58                 public bool IsReadOnly {
59                         get { return readOnly; }
60                 }
61
62                 public bool  IsSynchronized {
63                         get { return false; }
64                 }
65
66                 public virtual Control this [int index] {
67                         get {
68                                 if (index < 0 || index >= count)
69                                         throw new ArgumentOutOfRangeException ("index");
70
71                                 return controls [index];
72                         }
73                 }
74
75                 protected Control Owner {
76                         get { return owner; }
77                 }
78
79                 public object SyncRoot {
80                         get { return this; }
81                 }
82
83                 void EnsureControls ()
84                 {
85                         if (controls == null) {
86                                 controls = new Control [5];
87                         } else if (controls.Length < count + 1) {
88                                 int n = controls.Length == 5 ? 3 : 2;
89                                 Control [] newControls = new Control [controls.Length * n];
90                                 Array.Copy (controls, 0, newControls, 0, controls.Length);
91                                 controls = newControls;
92                         }
93                 }
94
95                 public virtual void Add (Control child)
96                 {
97                         if (child == null)
98                                 throw new ArgumentNullException ("child");
99
100                         if (readOnly)
101                                 throw new HttpException (Locale.GetText ("Collection is read-only."));
102
103                         if (Object.ReferenceEquals (owner, child))
104                                 throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
105
106                         EnsureControls ();
107                         version++;
108                         controls [count++] = child;
109                         owner.AddedControl (child, count - 1);
110                 }
111
112                 public virtual void AddAt (int index, Control child)
113                 {
114                         if (child == null) // maybe we should check for ! (child is Control)?
115                                 throw new ArgumentNullException ();
116                         
117                         if (index < -1 || index > count)
118                                 throw new ArgumentOutOfRangeException ();
119
120                         if (readOnly)
121                                 throw new HttpException (Locale.GetText ("Collection is read-only."));
122
123                         if (Object.ReferenceEquals (owner, child))
124                                 throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
125
126                         if (index == -1) {
127                                 Add (child);
128                                 return;
129                         }
130
131                         EnsureControls ();
132                         version++;
133                         Array.Copy (controls, index, controls, index + 1, count - index);
134                         count++;
135                         controls [index] = child;
136                         owner.AddedControl (child, index);
137                 }
138
139                 public virtual void Clear ()
140                 {
141                         if (controls == null)
142                                 return;
143
144                         version++;
145                         for (int i = 0; i < count; i++)
146                                 owner.RemovedControl (controls [i]);
147
148                         count = 0;
149                         if (owner != null)
150                                 owner.ResetChildNames ();
151                 }
152
153                 public virtual bool Contains (Control c)
154                 {
155                         return (controls != null && Array.IndexOf (controls, c) != -1);
156                 }
157
158                 public virtual void CopyTo (Array array, int index)
159                 {
160                         if (controls == null)
161                                 return;
162
163                         // can't use controls.CopyTo (array, index);
164                         // as we do not allocate it based on the true 
165                         // numbers of items we have in the collection
166                         // so we must re-implement Array.CopyTo :(
167
168                         if (array == null)
169                                 throw new ArgumentNullException ("array");
170                         if (index + count > array.GetLowerBound (0) + array.GetLength (0))
171                                 throw new ArgumentException ();
172                         if (array.Rank > 1)
173                                 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
174                         if (index < 0)
175                                 throw new ArgumentOutOfRangeException ("index", Locale.GetText ("Value has to be >= 0."));
176
177                         for (int i=0; i < count; i++)
178                                 array.SetValue (controls [i], i + index);
179                 }
180
181                 public virtual IEnumerator GetEnumerator ()
182                 {
183                         return new SimpleEnumerator (this);
184                 }
185
186                 public virtual int IndexOf (Control c)
187                 {
188                         if (controls == null || c == null)
189                                 return -1;
190
191                         return Array.IndexOf (controls, c);
192                 }
193
194                 public virtual void Remove (Control value)
195                 {
196                         int idx = IndexOf (value);
197                         if (idx == -1)
198                                 return;
199                         RemoveAt (idx);
200                 }
201
202                 public virtual void RemoveAt (int index)
203                 {
204                         if (readOnly)
205                                 throw new HttpException ();
206
207                         version++;
208                         Control ctrl = controls [index];
209                         count--;
210                         if (count - index > 0)
211                                 Array.Copy (controls, index + 1, controls, index, count - index);
212
213                         controls [count] = null;
214                         owner.RemovedControl (ctrl);
215                 }
216                 
217                 internal void SetReadonly (bool readOnly)
218                 {
219                         this.readOnly = readOnly;
220                 }
221
222                 // Almost the same as in ArrayList
223                 sealed class SimpleEnumerator : IEnumerator
224                 {
225                         ControlCollection coll;
226                         int index;
227                         int version;
228                         object currentElement;
229                                                         
230                         public SimpleEnumerator (ControlCollection coll)
231                         {
232                                 this.coll = coll;
233                                 index = -1;
234                                 version = coll.version;
235                         }
236         
237                         public bool MoveNext ()
238                         {
239                                 if (version != coll.version)
240                                         throw new InvalidOperationException ("List has changed.");
241                                 
242                                 if (index >= -1 && ++index < coll.Count) {
243                                         currentElement = coll [index];
244                                         return true;
245                                 } else {
246                                         index = -2;
247                                         return false;
248                                 }
249                         }
250         
251                         public object Current {
252                                 get {
253                                         if (index < 0)
254                                                 throw new InvalidOperationException (index == -1 ? "Enumerator not started" : "Enumerator ended");
255                                         
256                                         return currentElement;
257                                 }
258                         }
259         
260                         public void Reset ()
261                         {
262                                 if (version != coll.version)
263                                         throw new InvalidOperationException ("List has changed.");
264                                 
265                                 index = -1;
266                         }
267                 }
268         }
269 }
270