2008-12-08 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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.ComponentModel;
33 using System.Windows.Forms.Layout;
34
35 namespace System.Windows.Forms
36 {
37         [ListBindable (false)]
38         [Editor ("System.Windows.Forms.Design.ToolStripCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
39         public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable
40         {
41                 private ToolStrip owner;
42                 private bool internal_created;
43                 
44                 #region Public Constructor
45                 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
46                 {
47                         if (owner == null)
48                                 throw new ArgumentNullException ("owner");
49
50                         if (value == null)
51                                 throw new ArgumentNullException ("toolStripItems");
52
53                         this.owner = owner;
54
55                         foreach (ToolStripItem tsi in value)
56                                 this.AddNoOwnerOrLayout (tsi);
57                 }
58
59                 internal ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value, bool internalcreated) : base ()
60                 {
61                         if (owner == null)
62                                 throw new ArgumentNullException ("owner");
63
64                         this.internal_created = internalcreated;
65                         this.owner = owner;
66                         
67                         if (value != null)
68                                 foreach (ToolStripItem tsi in value)
69                                         this.AddNoOwnerOrLayout (tsi);
70                 }
71                 #endregion
72
73                 #region Public Properties
74                 public override bool IsReadOnly { get { return base.IsReadOnly; } }
75                 
76                 public new virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }
77                 
78                 public virtual ToolStripItem this[string key] {
79                         get {
80                                 foreach (ToolStripItem tsi in this)
81                                         if (tsi.Name == key)
82                                                 return tsi;
83
84                                 return null;
85                         }
86                 }
87                 #endregion
88
89                 #region Public Methods
90                 public ToolStripItem Add (Image image)
91                 {
92                         ToolStripItem tsb = owner.CreateDefaultItem (string.Empty, image, null);
93                         this.Add (tsb);
94                         return tsb;
95                 }
96
97                 public ToolStripItem Add (string text)
98                 {
99                         ToolStripItem tsb = owner.CreateDefaultItem (text, null, null);
100                         this.Add (tsb);
101                         return tsb;
102                 }
103
104                 public int Add (ToolStripItem value)
105                 {
106                         if (value == null)
107                                 throw new ArgumentNullException ("value");
108
109                         value.InternalOwner = owner;
110                                 
111                         if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
112                                 ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
113                                 
114                         int index = base.Add (value);
115                         
116                         if (this.internal_created)
117                                 owner.OnItemAdded (new ToolStripItemEventArgs (value));
118                                 
119                         return index;
120                 }
121
122                 public ToolStripItem Add (string text, Image image)
123                 {
124                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
125                         this.Add (tsb);
126                         return tsb;
127                 }
128
129                 public ToolStripItem Add (string text, Image image, EventHandler onClick)
130                 {
131                         ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
132                         this.Add (tsb);
133                         return tsb;
134                 }
135
136                 public void AddRange (ToolStripItem[] toolStripItems)
137                 {
138                         if (toolStripItems == null)
139                                 throw new ArgumentNullException ("toolStripItems");
140                         if (this.IsReadOnly)
141                                 throw new NotSupportedException ("This collection is read-only");
142
143                         this.owner.SuspendLayout ();
144
145                         foreach (ToolStripItem tsi in toolStripItems)
146                                 this.Add (tsi);
147
148                         this.owner.ResumeLayout ();
149                 }
150
151                 public void AddRange (ToolStripItemCollection toolStripItems)
152                 {
153                         if (toolStripItems == null)
154                                 throw new ArgumentNullException ("toolStripItems");
155                         if (this.IsReadOnly)
156                                 throw new NotSupportedException ("This collection is read-only");
157
158                         this.owner.SuspendLayout ();
159
160                         foreach (ToolStripItem tsi in toolStripItems)
161                                 this.Add (tsi);
162
163                         this.owner.ResumeLayout ();
164                 }
165
166                 public new virtual void Clear ()
167                 {
168                         if (this.IsReadOnly)
169                                 throw new NotSupportedException ("This collection is read-only");
170
171                         base.Clear ();
172                         owner.PerformLayout ();
173                 }
174
175                 public bool Contains (ToolStripItem value)
176                 {
177                         return base.Contains (value);
178                 }
179
180                 public virtual bool ContainsKey (string key)
181                 {
182                         return this[key] != null;
183                 }
184
185                 public void CopyTo (ToolStripItem[] array, int index)
186                 {
187                         base.CopyTo (array, index);
188                 }
189
190                 [MonoTODO ()]
191                 public ToolStripItem[] Find (string key, bool searchAllChildren)
192                 {
193                         if (key == null)
194                                 throw new ArgumentNullException ("key");
195
196                         ArrayList al = new ArrayList ();
197
198                         foreach (ToolStripItem tsi in this) {
199                                 if (tsi.Name == key) {
200                                         al.Add (tsi);
201
202                                         if (searchAllChildren) {
203                                                 // TODO: tsi does not have an items property yet..
204                                         }
205                                 }
206                         }
207
208                         return (ToolStripItem[])al.ToArray ();
209                 }
210
211                 public int IndexOf (ToolStripItem value)
212                 {
213                         return base.IndexOf (value);
214                 }
215
216                 public virtual int IndexOfKey (string key)
217                 {
218                         ToolStripItem tsi = this[key];
219
220                         if (tsi == null)
221                                 return -1;
222
223                         return this.IndexOf (tsi);
224                 }
225
226                 public void Insert (int index, ToolStripItem value)
227                 {
228                         if (value == null)
229                                 throw new ArgumentNullException ("value");
230
231                         if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
232                                 ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
233
234                         if (value.Owner != null)
235                                 value.Owner.Items.Remove (value);
236                                 
237                         base.Insert (index, value);
238                         
239                         if (internal_created) {
240                                 value.InternalOwner = owner;
241                                 owner.OnItemAdded (new ToolStripItemEventArgs (value));
242                         }
243                         
244                         if (owner.Created)
245                                 owner.PerformLayout ();
246                 }
247
248                 public void Remove (ToolStripItem value)
249                 {
250                         if (this.IsReadOnly)
251                                 throw new NotSupportedException ("This collection is read-only");
252
253                         base.Remove (value);
254                         
255                         if (value != null && internal_created) {
256                                 value.InternalOwner = null;
257                                 value.Parent = null;
258                         }
259                         
260                         if (internal_created)
261                                 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
262                         
263                         if (owner.Created)      
264                                 owner.PerformLayout ();
265                 }
266
267                 public void RemoveAt (int index)
268                 {
269                         if (this.IsReadOnly)
270                                 throw new NotSupportedException ("This collection is read-only");
271
272                         ToolStripItem tsi = (ToolStripItem)base[index];
273                         this.Remove (tsi);
274                 }
275
276                 public virtual void RemoveByKey (string key)
277                 {
278                         if (this.IsReadOnly)
279                                 throw new NotSupportedException ("This collection is read-only");
280
281                         ToolStripItem tsi = this[key];
282
283                         if (tsi != null)
284                                 this.Remove (tsi);
285
286                         return;
287                 }
288                 #endregion
289
290                 #region Internal Methods
291                 // When we create DisplayedItems, we don't want to modify the item's
292                 // parent or trigger a layout.
293                 internal int AddNoOwnerOrLayout (ToolStripItem value)
294                 {
295                         if (value == null)
296                                 throw new ArgumentNullException ("value");
297
298                         int index = base.Add (value);
299                         return index;
300                 }
301
302                 internal void InsertNoOwnerOrLayout (int index, ToolStripItem value)
303                 {
304                         if (value == null)
305                                 throw new ArgumentNullException ("value");
306
307                         if (index > Count)
308                                 base.Add (value);
309                         else
310                                 base.Insert (index, value);
311                 }
312
313                 internal void RemoveNoOwnerOrLayout (ToolStripItem value)
314                 {
315                         if (value == null)
316                                 throw new ArgumentNullException ("value");
317
318                         base.Remove (value);
319                 }
320                 #endregion
321                 
322                 #region IList Members
323                 int IList.Add (object value)
324                 {
325                         return this.Add ((ToolStripItem)value);
326                 }
327
328                 void IList.Clear ()
329                 {
330                         this.Clear ();
331                 }
332
333                 bool IList.Contains (object value)
334                 {
335                         return this.Contains ((ToolStripItem)value);
336                 }
337
338                 int IList.IndexOf (object value)
339                 {
340                         return this.IndexOf ((ToolStripItem)value);
341                 }
342
343                 void IList.Insert (int index, object value)
344                 {
345                         this.Insert (index, (ToolStripItem)value);
346                 }
347
348                 bool IList.IsFixedSize {
349                         get { return this.IsFixedSize; }
350                 }
351
352                 void IList.Remove (object value)
353                 {
354                         this.Remove ((ToolStripItem)value); ;
355                 }
356
357                 void IList.RemoveAt (int index)
358                 {
359                         this.RemoveAt (index);
360                 }
361
362                 object IList.this[int index] {
363                         get { return this[index]; }
364                         set { throw new NotSupportedException (); }
365                 }
366                 #endregion
367         }
368 }
369 #endif