New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripItemCollection.cs
1 //
2 // ToolStripItemCollection.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29
30 using System.Drawing;
31 using System.Collections;
32 using System.Windows.Forms.Layout;
33
34 namespace System.Windows.Forms
35 {
36         public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable
37         {
38                 private ToolStrip owner;
39
40                 #region Public Constructor
41                 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
42                 {
43                         if (owner == null)
44                                 throw new ArgumentNullException ("owner");
45
46                         this.owner = owner;
47
48                         if (value != null)
49                                 foreach (ToolStripItem tsi in value)
50                                         this.Add (tsi);
51                 }
52                 #endregion
53
54                 #region Public Properties
55                 public override bool IsReadOnly { get { return base.IsReadOnly; } }
56                 
57                 public virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }
58                 
59                 public virtual ToolStripItem this[string key] {
60                         get {
61                                 foreach (ToolStripItem tsi in this)
62                                         if (tsi.Name == key)
63                                                 return tsi;
64
65                                 return null;
66                         }
67                 }
68                 #endregion
69
70                 #region Public Methods
71                 public ToolStripItem Add (Image image)
72                 {
73                         ToolStripItem tsb = owner.CreateDefaultItem (string.Empty, image, null);
74                         this.Add (tsb);
75                         return tsb;
76                 }
77
78                 public ToolStripItem Add (string text)
79                 {
80                         ToolStripItem tsb = owner.CreateDefaultItem (text, null, null);
81                         this.Add (tsb);
82                         return tsb;
83                 }
84
85                 public int Add (ToolStripItem value)
86                 {
87                         if (value == null)
88                                 throw new ArgumentNullException ("value");
89
90                         value.Owner = owner;
91                         value.Parent = owner;
92                         owner.PerformLayout ();
93                         owner.OnItemAdded (new ToolStripItemEventArgs (value));
94                         return base.Add (value);
95                 }
96
97                 public ToolStripItem Add (string text, Image image)
98                 {
99                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
100                         this.Add (tsb);
101                         return tsb;
102                 }
103
104                 public ToolStripItem Add (string text, Image image, EventHandler onClick)
105                 {
106                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
107                         this.Add (tsb);
108                         return tsb;
109                 }
110
111                 public void AddRange (ToolStripItem[] toolStripItems)
112                 {
113                         if (toolStripItems == null)
114                                 throw new ArgumentNullException ("toolStripItems");
115                         if (this.IsReadOnly)
116                                 throw new NotSupportedException ("This collection is read-only");
117
118                         this.owner.SuspendLayout ();
119
120                         foreach (ToolStripItem tsi in toolStripItems)
121                                 this.Add (tsi);
122
123                         this.owner.ResumeLayout ();
124                 }
125
126                 public void AddRange (ToolStripItemCollection toolStripItems)
127                 {
128                         if (toolStripItems == null)
129                                 throw new ArgumentNullException ("toolStripItems");
130                         if (this.IsReadOnly)
131                                 throw new NotSupportedException ("This collection is read-only");
132
133                         this.owner.SuspendLayout ();
134
135                         foreach (ToolStripItem tsi in toolStripItems)
136                                 this.Add (tsi);
137
138                         this.owner.ResumeLayout ();
139                 }
140
141                 public virtual void Clear ()
142                 {
143                         if (this.IsReadOnly)
144                                 throw new NotSupportedException ("This collection is read-only");
145
146                         base.Clear ();
147                         owner.PerformLayout ();
148                 }
149
150                 public bool Contains (ToolStripItem value)
151                 {
152                         return base.Contains (value);
153                 }
154
155                 public virtual bool ContainsKey (string key)
156                 {
157                         return this[key] != null;
158                 }
159
160                 public void CopyTo (ToolStripItem[] array, int index)
161                 {
162                         base.CopyTo (array, index);
163                 }
164
165                 [MonoTODO ()]
166                 public ToolStripItem[] Find (string key, bool searchAllChildren)
167                 {
168                         if (key == null)
169                                 throw new ArgumentNullException ("key");
170
171                         ArrayList al = new ArrayList ();
172
173                         foreach (ToolStripItem tsi in this) {
174                                 if (tsi.Name == key) {
175                                         al.Add (tsi);
176
177                                         if (searchAllChildren) {
178                                                 // TODO: tsi does not have an items property yet..
179                                         }
180                                 }
181                         }
182
183                         return (ToolStripItem[])al.ToArray ();
184                 }
185
186                 public int IndexOf (ToolStripItem value)
187                 {
188                         return base.IndexOf (value);
189                 }
190
191                 public virtual int IndexOfKey (string key)
192                 {
193                         ToolStripItem tsi = this[key];
194
195                         if (tsi == null)
196                                 return -1;
197
198                         return this.IndexOf (tsi);
199                 }
200
201                 public void Insert (int index, ToolStripItem value)
202                 {
203                         if (value == null)
204                                 throw new ArgumentNullException ("value");
205
206                         base.Insert (index, value);
207                         owner.OnItemAdded (new ToolStripItemEventArgs (value));
208                         owner.PerformLayout ();
209                 }
210
211                 public void Remove (ToolStripItem value)
212                 {
213                         if (this.IsReadOnly)
214                                 throw new NotSupportedException ("This collection is read-only");
215
216                         base.Remove (value);
217                         owner.OnItemRemoved (new ToolStripItemEventArgs (value));
218                         owner.PerformLayout ();
219                 }
220
221                 public void RemoveAt (int index)
222                 {
223                         if (this.IsReadOnly)
224                                 throw new NotSupportedException ("This collection is read-only");
225
226                         ToolStripItem tsi = (ToolStripItem)base[index];
227                         base.RemoveAt (index);
228                         owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
229                         owner.PerformLayout ();
230                 }
231
232                 public virtual void RemoveByKey (string key)
233                 {
234                         if (this.IsReadOnly)
235                                 throw new NotSupportedException ("This collection is read-only");
236
237                         ToolStripItem tsi = this[key];
238
239                         if (tsi != null)
240                                 this.Remove (tsi);
241
242                         return;
243                 }
244                 #endregion
245
246                 #region IList Members
247                 int IList.Add (object value)
248                 {
249                         return this.Add ((ToolStripItem)value);
250                 }
251
252                 void IList.Clear ()
253                 {
254                         this.Clear ();
255                 }
256
257                 bool IList.Contains (object value)
258                 {
259                         return this.Contains ((ToolStripItem)value);
260                 }
261
262                 int IList.IndexOf (object value)
263                 {
264                         return this.IndexOf ((ToolStripItem)value);
265                 }
266
267                 void IList.Insert (int index, object value)
268                 {
269                         this.Insert (index, (ToolStripItem)value);
270                 }
271
272                 bool IList.IsFixedSize {
273                         get { return this.IsFixedSize; }
274                 }
275
276                 bool IList.IsReadOnly {
277                         get { return this.IsReadOnly; }
278                 }
279
280                 void IList.Remove (object value)
281                 {
282                         this.Remove ((ToolStripItem)value); ;
283                 }
284
285                 void IList.RemoveAt (int index)
286                 {
287                         this.RemoveAt (index);
288                 }
289
290                 object IList.this[int index] {
291                         get { return this[index]; }
292                         set { throw new NotSupportedException (); }
293                 }
294                 #endregion
295         }
296 }
297 #endif