Mark tests as not working under TARGET_JVM
[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 new 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                         
93                         int index = base.Add (value);
94                         
95                         owner.OnItemAdded (new ToolStripItemEventArgs (value));
96                         return index;
97                 }
98
99                 public ToolStripItem Add (string text, Image image)
100                 {
101                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
102                         this.Add (tsb);
103                         return tsb;
104                 }
105
106                 public ToolStripItem Add (string text, Image image, EventHandler onClick)
107                 {
108                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
109                         this.Add (tsb);
110                         return tsb;
111                 }
112
113                 public void AddRange (ToolStripItem[] toolStripItems)
114                 {
115                         if (toolStripItems == null)
116                                 throw new ArgumentNullException ("toolStripItems");
117                         if (this.IsReadOnly)
118                                 throw new NotSupportedException ("This collection is read-only");
119
120                         this.owner.SuspendLayout ();
121
122                         foreach (ToolStripItem tsi in toolStripItems)
123                                 this.Add (tsi);
124
125                         this.owner.ResumeLayout ();
126                 }
127
128                 public void AddRange (ToolStripItemCollection toolStripItems)
129                 {
130                         if (toolStripItems == null)
131                                 throw new ArgumentNullException ("toolStripItems");
132                         if (this.IsReadOnly)
133                                 throw new NotSupportedException ("This collection is read-only");
134
135                         this.owner.SuspendLayout ();
136
137                         foreach (ToolStripItem tsi in toolStripItems)
138                                 this.Add (tsi);
139
140                         this.owner.ResumeLayout ();
141                 }
142
143                 public new virtual void Clear ()
144                 {
145                         if (this.IsReadOnly)
146                                 throw new NotSupportedException ("This collection is read-only");
147
148                         base.Clear ();
149                         owner.PerformLayout ();
150                 }
151
152                 public bool Contains (ToolStripItem value)
153                 {
154                         return base.Contains (value);
155                 }
156
157                 public virtual bool ContainsKey (string key)
158                 {
159                         return this[key] != null;
160                 }
161
162                 public void CopyTo (ToolStripItem[] array, int index)
163                 {
164                         base.CopyTo (array, index);
165                 }
166
167                 [MonoTODO ()]
168                 public ToolStripItem[] Find (string key, bool searchAllChildren)
169                 {
170                         if (key == null)
171                                 throw new ArgumentNullException ("key");
172
173                         ArrayList al = new ArrayList ();
174
175                         foreach (ToolStripItem tsi in this) {
176                                 if (tsi.Name == key) {
177                                         al.Add (tsi);
178
179                                         if (searchAllChildren) {
180                                                 // TODO: tsi does not have an items property yet..
181                                         }
182                                 }
183                         }
184
185                         return (ToolStripItem[])al.ToArray ();
186                 }
187
188                 public int IndexOf (ToolStripItem value)
189                 {
190                         return base.IndexOf (value);
191                 }
192
193                 public virtual int IndexOfKey (string key)
194                 {
195                         ToolStripItem tsi = this[key];
196
197                         if (tsi == null)
198                                 return -1;
199
200                         return this.IndexOf (tsi);
201                 }
202
203                 public void Insert (int index, ToolStripItem value)
204                 {
205                         if (value == null)
206                                 throw new ArgumentNullException ("value");
207
208                         base.Insert (index, value);
209                         owner.OnItemAdded (new ToolStripItemEventArgs (value));
210                         owner.PerformLayout ();
211                 }
212
213                 public void Remove (ToolStripItem value)
214                 {
215                         if (this.IsReadOnly)
216                                 throw new NotSupportedException ("This collection is read-only");
217
218                         base.Remove (value);
219                         owner.OnItemRemoved (new ToolStripItemEventArgs (value));
220                         owner.PerformLayout ();
221                 }
222
223                 public new void RemoveAt (int index)
224                 {
225                         if (this.IsReadOnly)
226                                 throw new NotSupportedException ("This collection is read-only");
227
228                         ToolStripItem tsi = (ToolStripItem)base[index];
229                         base.RemoveAt (index);
230                         owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
231                         owner.PerformLayout ();
232                 }
233
234                 public virtual void RemoveByKey (string key)
235                 {
236                         if (this.IsReadOnly)
237                                 throw new NotSupportedException ("This collection is read-only");
238
239                         ToolStripItem tsi = this[key];
240
241                         if (tsi != null)
242                                 this.Remove (tsi);
243
244                         return;
245                 }
246                 #endregion
247
248                 #region Internal Methods
249                 // When we create DisplayedItems, we don't want to modify the item's
250                 // parent or trigger a layout.
251                 internal int AddNoOwnerOrLayout (ToolStripItem value)
252                 {
253                         if (value == null)
254                                 throw new ArgumentNullException ("value");
255
256                         int index = base.Add (value);
257                         return index;
258                 }
259                 #endregion
260                 
261                 #region IList Members
262                 int IList.Add (object value)
263                 {
264                         return this.Add ((ToolStripItem)value);
265                 }
266
267                 void IList.Clear ()
268                 {
269                         this.Clear ();
270                 }
271
272                 bool IList.Contains (object value)
273                 {
274                         return this.Contains ((ToolStripItem)value);
275                 }
276
277                 int IList.IndexOf (object value)
278                 {
279                         return this.IndexOf ((ToolStripItem)value);
280                 }
281
282                 void IList.Insert (int index, object value)
283                 {
284                         this.Insert (index, (ToolStripItem)value);
285                 }
286
287                 bool IList.IsFixedSize {
288                         get { return this.IsFixedSize; }
289                 }
290
291                 bool IList.IsReadOnly {
292                         get { return this.IsReadOnly; }
293                 }
294
295                 void IList.Remove (object value)
296                 {
297                         this.Remove ((ToolStripItem)value); ;
298                 }
299
300                 void IList.RemoveAt (int index)
301                 {
302                         this.RemoveAt (index);
303                 }
304
305                 object IList.this[int index] {
306                         get { return this[index]; }
307                         set { throw new NotSupportedException (); }
308                 }
309                 #endregion
310         }
311 }
312 #endif