ControlCollection.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // System.Web.UI.ControlCollection.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  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. using System.Collections;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI {
  32. // CAS
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. public class ControlCollection : ICollection, IEnumerable
  36. {
  37. Control owner;
  38. Control [] controls;
  39. int version;
  40. int count;
  41. bool readOnly;
  42. public ControlCollection (Control owner)
  43. {
  44. if (owner == null)
  45. throw new ArgumentException ("owner");
  46. this.owner = owner;
  47. }
  48. public int Count {
  49. get { return count; }
  50. }
  51. public bool IsReadOnly {
  52. get { return readOnly; }
  53. }
  54. public bool IsSynchronized {
  55. get { return false; }
  56. }
  57. public virtual Control this [int index] {
  58. get {
  59. if (index < 0 || index >= count)
  60. throw new ArgumentOutOfRangeException ("index");
  61. return controls [index];
  62. }
  63. }
  64. protected Control Owner {
  65. get { return owner; }
  66. }
  67. public object SyncRoot {
  68. get { return this; }
  69. }
  70. void EnsureControls ()
  71. {
  72. if (controls == null) {
  73. controls = new Control [5];
  74. } else if (controls.Length < count + 1) {
  75. int n = controls.Length == 5 ? 3 : 2;
  76. Control [] newControls = new Control [controls.Length * n];
  77. Array.Copy (controls, 0, newControls, 0, controls.Length);
  78. controls = newControls;
  79. }
  80. }
  81. public virtual void Add (Control child)
  82. {
  83. if (child == null)
  84. throw new ArgumentNullException ();
  85. if (readOnly)
  86. throw new HttpException (Locale.GetText ("Collection is read-only."));
  87. if (Object.ReferenceEquals (owner, child))
  88. throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
  89. EnsureControls ();
  90. version++;
  91. controls [count++] = child;
  92. owner.AddedControl (child, count - 1);
  93. }
  94. public virtual void AddAt (int index, Control child)
  95. {
  96. if (child == null) // maybe we should check for ! (child is Control)?
  97. throw new ArgumentNullException ();
  98. if (index < -1 || index > count)
  99. throw new ArgumentOutOfRangeException ();
  100. if (readOnly)
  101. throw new HttpException (Locale.GetText ("Collection is read-only."));
  102. if (Object.ReferenceEquals (owner, child))
  103. throw new HttpException (Locale.GetText ("Cannot add collection's owner."));
  104. if (index == -1) {
  105. Add (child);
  106. return;
  107. }
  108. EnsureControls ();
  109. version++;
  110. Array.Copy (controls, index, controls, index + 1, count - index);
  111. count++;
  112. controls [index] = child;
  113. owner.AddedControl (child, index);
  114. }
  115. public virtual void Clear ()
  116. {
  117. if (controls == null)
  118. return;
  119. version++;
  120. for (int i = 0; i < count; i++)
  121. owner.RemovedControl (controls [i]);
  122. count = 0;
  123. if (owner != null)
  124. owner.ResetChildNames ();
  125. }
  126. public virtual bool Contains (Control c)
  127. {
  128. return (controls != null && Array.IndexOf (controls, c) != -1);
  129. }
  130. public void CopyTo (Array array, int index)
  131. {
  132. if (controls == null)
  133. return;
  134. // can't use controls.CopyTo (array, index);
  135. // as we do not allocate it based on the true
  136. // numbers of items we have in the collection
  137. // so we must re-implement Array.CopyTo :(
  138. if (array == null)
  139. throw new ArgumentNullException ("array");
  140. if (index + count > array.GetLowerBound (0) + array.GetLength (0))
  141. throw new ArgumentException ();
  142. if (array.Rank > 1)
  143. throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
  144. if (index < 0)
  145. throw new ArgumentOutOfRangeException ("index", Locale.GetText ("Value has to be >= 0."));
  146. for (int i=0; i < count; i++)
  147. array.SetValue (controls [i], i);
  148. }
  149. public IEnumerator GetEnumerator ()
  150. {
  151. return new SimpleEnumerator (this);
  152. }
  153. public virtual int IndexOf (Control c)
  154. {
  155. if (controls == null)
  156. return -1;
  157. return Array.IndexOf (controls, c);
  158. }
  159. public virtual void Remove (Control value)
  160. {
  161. int idx = IndexOf (value);
  162. if (idx == -1)
  163. return;
  164. RemoveAt (idx);
  165. }
  166. public virtual void RemoveAt (int index)
  167. {
  168. if (readOnly)
  169. throw new HttpException ();
  170. version++;
  171. Control ctrl = controls [index];
  172. count--;
  173. if (count - index > 0)
  174. Array.Copy (controls, index + 1, controls, index, count - index);
  175. controls [count] = null;
  176. owner.RemovedControl (ctrl);
  177. }
  178. internal void SetReadonly (bool readOnly)
  179. {
  180. this.readOnly = readOnly;
  181. }
  182. // Almost the same as in ArrayList
  183. sealed class SimpleEnumerator : IEnumerator
  184. {
  185. ControlCollection coll;
  186. int index;
  187. int version;
  188. object currentElement;
  189. public SimpleEnumerator (ControlCollection coll)
  190. {
  191. this.coll = coll;
  192. index = -1;
  193. version = coll.version;
  194. }
  195. public bool MoveNext ()
  196. {
  197. if (version != coll.version)
  198. throw new InvalidOperationException ("List has changed.");
  199. if (index >= -1 && ++index < coll.Count) {
  200. currentElement = coll [index];
  201. return true;
  202. } else {
  203. index = -2;
  204. return false;
  205. }
  206. }
  207. public object Current {
  208. get {
  209. if (index < 0)
  210. throw new InvalidOperationException (index == -1 ? "Enumerator not started" : "Enumerator ended");
  211. return currentElement;
  212. }
  213. }
  214. public void Reset ()
  215. {
  216. if (version != coll.version)
  217. throw new InvalidOperationException ("List has changed.");
  218. index = -1;
  219. }
  220. }
  221. }
  222. }