2 // ToolStripItemCollection.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Windows.Forms.Layout;
35 namespace System.Windows.Forms
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
41 private ToolStrip owner;
42 private bool internal_created;
44 #region Public Constructor
45 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
48 throw new ArgumentNullException ("owner");
51 throw new ArgumentNullException ("toolStripItems");
55 foreach (ToolStripItem tsi in value)
56 this.AddNoOwnerOrLayout (tsi);
59 internal ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value, bool internalcreated) : base ()
62 throw new ArgumentNullException ("owner");
64 this.internal_created = internalcreated;
68 foreach (ToolStripItem tsi in value)
69 this.AddNoOwnerOrLayout (tsi);
73 #region Public Properties
74 public override bool IsReadOnly { get { return base.IsReadOnly; } }
76 public new virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }
78 public virtual ToolStripItem this[string key] {
80 foreach (ToolStripItem tsi in this)
89 #region Public Methods
90 public ToolStripItem Add (Image image)
92 ToolStripItem tsb = owner.CreateDefaultItem (string.Empty, image, null);
97 public ToolStripItem Add (string text)
99 ToolStripItem tsb = owner.CreateDefaultItem (text, null, null);
104 public int Add (ToolStripItem value)
107 throw new ArgumentNullException ("value");
109 value.InternalOwner = owner;
111 if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
112 ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
114 int index = base.Add (value);
116 if (this.internal_created)
117 owner.OnItemAdded (new ToolStripItemEventArgs (value));
122 public ToolStripItem Add (string text, Image image)
124 ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
129 public ToolStripItem Add (string text, Image image, EventHandler onClick)
131 ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
136 public void AddRange (ToolStripItem[] toolStripItems)
138 if (toolStripItems == null)
139 throw new ArgumentNullException ("toolStripItems");
141 throw new NotSupportedException ("This collection is read-only");
143 this.owner.SuspendLayout ();
145 foreach (ToolStripItem tsi in toolStripItems)
148 this.owner.ResumeLayout ();
151 public void AddRange (ToolStripItemCollection toolStripItems)
153 if (toolStripItems == null)
154 throw new ArgumentNullException ("toolStripItems");
156 throw new NotSupportedException ("This collection is read-only");
158 this.owner.SuspendLayout ();
160 foreach (ToolStripItem tsi in toolStripItems)
163 this.owner.ResumeLayout ();
166 public new virtual void Clear ()
169 throw new NotSupportedException ("This collection is read-only");
172 owner.PerformLayout ();
175 public bool Contains (ToolStripItem value)
177 return base.Contains (value);
180 public virtual bool ContainsKey (string key)
182 return this[key] != null;
185 public void CopyTo (ToolStripItem[] array, int index)
187 base.CopyTo (array, index);
191 public ToolStripItem[] Find (string key, bool searchAllChildren)
194 throw new ArgumentNullException ("key");
196 ArrayList al = new ArrayList ();
198 foreach (ToolStripItem tsi in this) {
199 if (tsi.Name == key) {
202 if (searchAllChildren) {
203 // TODO: tsi does not have an items property yet..
208 return (ToolStripItem[])al.ToArray ();
211 public int IndexOf (ToolStripItem value)
213 return base.IndexOf (value);
216 public virtual int IndexOfKey (string key)
218 ToolStripItem tsi = this[key];
223 return this.IndexOf (tsi);
226 public void Insert (int index, ToolStripItem value)
229 throw new ArgumentNullException ("value");
231 if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
232 ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
234 if (value.Owner != null)
235 value.Owner.Items.Remove (value);
237 base.Insert (index, value);
239 if (internal_created) {
240 value.InternalOwner = owner;
241 owner.OnItemAdded (new ToolStripItemEventArgs (value));
245 owner.PerformLayout ();
248 public void Remove (ToolStripItem value)
251 throw new NotSupportedException ("This collection is read-only");
255 if (value != null && internal_created) {
256 value.InternalOwner = null;
260 if (internal_created)
261 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
264 owner.PerformLayout ();
267 public void RemoveAt (int index)
270 throw new NotSupportedException ("This collection is read-only");
272 ToolStripItem tsi = (ToolStripItem)base[index];
276 public virtual void RemoveByKey (string key)
279 throw new NotSupportedException ("This collection is read-only");
281 ToolStripItem tsi = this[key];
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)
296 throw new ArgumentNullException ("value");
298 int index = base.Add (value);
302 internal void InsertNoOwnerOrLayout (int index, ToolStripItem value)
305 throw new ArgumentNullException ("value");
310 base.Insert (index, value);
313 internal void RemoveNoOwnerOrLayout (ToolStripItem value)
316 throw new ArgumentNullException ("value");
322 #region IList Members
323 int IList.Add (object value)
325 return this.Add ((ToolStripItem)value);
333 bool IList.Contains (object value)
335 return this.Contains ((ToolStripItem)value);
338 int IList.IndexOf (object value)
340 return this.IndexOf ((ToolStripItem)value);
343 void IList.Insert (int index, object value)
345 this.Insert (index, (ToolStripItem)value);
348 bool IList.IsFixedSize {
349 get { return this.IsFixedSize; }
352 void IList.Remove (object value)
354 this.Remove ((ToolStripItem)value); ;
357 void IList.RemoveAt (int index)
359 this.RemoveAt (index);
362 object IList.this[int index] {
363 get { return this[index]; }
364 set { throw new NotSupportedException (); }