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