Add licensing info
[mono.git] / mcs / class / System.Web / System.Web.UI / ControlCollection.cs
1 //
2 // System.Web.UI.ControlCollection.cs
3 //
4 // Duncan Mak  (duncan@ximian.com)
5 //
6 // (C) Ximian, Inc.
7 //
8
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;
31 using System.Collections;
32
33 namespace System.Web.UI {
34
35         public class ControlCollection : ICollection, IEnumerable
36         {
37                 ArrayList list;
38                 Control owner;
39                 
40                 public ControlCollection (Control owner)
41                 {
42                         if (owner == null)
43                                 throw new ArgumentException ();
44
45                         list = new ArrayList ();
46                         this.owner = owner;
47                 }
48
49                 internal ControlCollection (Control owner, bool shortList)
50                 {
51                         if (owner == null)
52                                 throw new ArgumentException ();
53
54                         list = new ArrayList (shortList ? 1 : 0);
55                         this.owner = owner;
56                 }
57
58                 public int Count {
59                         get { return list.Count; }
60                 }
61
62                 public bool IsReadOnly {
63                         get { return list.IsReadOnly; }
64                 }
65
66                 public bool  IsSynchronized {
67                         get { return list.IsSynchronized; }
68                 }
69
70                 public virtual Control this [int index] {
71                         get { return list [index] as Control; }
72                 }
73
74                 protected Control Owner {
75                         get { return owner; }
76                 }
77
78                 public object SyncRoot {
79                         get { return list.SyncRoot; }
80                 }
81
82                 public virtual void Add (Control child)
83                 {
84                         if (child == null)
85                                 throw new ArgumentNullException ();
86                         if (IsReadOnly)
87                                 throw new HttpException ();
88
89                         list.Add (child);
90                         owner.AddedControl (child, list.Count - 1);
91                 }
92
93                 public virtual void AddAt (int index, Control child)
94                 {
95                         if (child == null) // maybe we should check for ! (child is Control)?
96                                 throw new ArgumentNullException ();
97                         
98                         if ((index < -1) || (index > Count))
99                                 throw new ArgumentOutOfRangeException ();
100
101                         if (IsReadOnly)
102                                 throw new HttpException ();
103
104                         if (index == -1){
105                                 Add (child);
106                         } else {
107                                 list.Insert (index, child);
108                                 owner.AddedControl (child, index);
109                         }
110                 }
111
112                 public virtual void Clear ()
113                 {
114                         list.Clear ();
115                         if (owner != null)
116                                 owner.ResetChildNames ();
117                 }
118
119                 public virtual bool Contains (Control c)
120                 {
121                         return list.Contains (c);
122                 }
123
124                 public void CopyTo (Array array, int index)
125                 {
126                         list.CopyTo (array, index);
127                 }
128
129                 public IEnumerator GetEnumerator ()
130                 {
131                         return list.GetEnumerator ();
132                 }
133
134                 public virtual int IndexOf (Control c)
135                 {
136                         return list.IndexOf (c);
137                 }
138
139                 public virtual void Remove (Control value)
140                 {
141                         list.Remove (value);
142                         owner.RemovedControl (value);
143                 }
144
145                 public virtual void RemoveAt (int index)
146                 {
147                         if (IsReadOnly)
148                                 throw new HttpException ();
149
150                         Control value = (Control) list [index];
151                         list.RemoveAt (index);
152                         owner.RemovedControl (value);
153                 }
154         }
155 }