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");
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 base.Insert (index, value);
236 if (internal_created) {
238 owner.OnItemAdded (new ToolStripItemEventArgs (value));
242 owner.PerformLayout ();
245 public void Remove (ToolStripItem value)
248 throw new NotSupportedException ("This collection is read-only");
252 if (value != null && internal_created) {
257 if (internal_created)
258 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
261 owner.PerformLayout ();
264 public new void RemoveAt (int index)
267 throw new NotSupportedException ("This collection is read-only");
269 ToolStripItem tsi = (ToolStripItem)base[index];
273 public virtual void RemoveByKey (string key)
276 throw new NotSupportedException ("This collection is read-only");
278 ToolStripItem tsi = this[key];
287 #region Internal Methods
288 // When we create DisplayedItems, we don't want to modify the item's
289 // parent or trigger a layout.
290 internal int AddNoOwnerOrLayout (ToolStripItem value)
293 throw new ArgumentNullException ("value");
295 int index = base.Add (value);
299 internal void InsertNoOwnerOrLayout (int index, ToolStripItem value)
302 throw new ArgumentNullException ("value");
304 base.Insert (index, value);
307 internal void RemoveNoOwnerOrLayout (ToolStripItem value)
310 throw new ArgumentNullException ("value");
316 #region IList Members
317 int IList.Add (object value)
319 return this.Add ((ToolStripItem)value);
327 bool IList.Contains (object value)
329 return this.Contains ((ToolStripItem)value);
332 int IList.IndexOf (object value)
334 return this.IndexOf ((ToolStripItem)value);
337 void IList.Insert (int index, object value)
339 this.Insert (index, (ToolStripItem)value);
342 bool IList.IsFixedSize {
343 get { return this.IsFixedSize; }
346 bool IList.IsReadOnly {
347 get { return this.IsReadOnly; }
350 void IList.Remove (object value)
352 this.Remove ((ToolStripItem)value); ;
355 void IList.RemoveAt (int index)
357 this.RemoveAt (index);
360 object IList.this[int index] {
361 get { return this[index]; }
362 set { throw new NotSupportedException (); }