New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripItemCollection.cs
1 //\r
2 // ToolStripItemCollection.cs\r
3 //\r
4 // Permission is hereby granted, free of charge, to any person obtaining\r
5 // a copy of this software and associated documentation files (the\r
6 // "Software"), to deal in the Software without restriction, including\r
7 // without limitation the rights to use, copy, modify, merge, publish,\r
8 // distribute, sublicense, and/or sell copies of the Software, and to\r
9 // permit persons to whom the Software is furnished to do so, subject to\r
10 // the following conditions:\r
11 // \r
12 // The above copyright notice and this permission notice shall be\r
13 // included in all copies or substantial portions of the Software.\r
14 // \r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22 //\r
23 // Copyright (c) 2006 Jonathan Pobst\r
24 //\r
25 // Authors:\r
26 //      Jonathan Pobst (monkey@jpobst.com)\r
27 //\r
28 #if NET_2_0\r
29 \r
30 using System.Drawing;\r
31 using System.Collections;\r
32 using System.Windows.Forms.Layout;\r
33 \r
34 namespace System.Windows.Forms\r
35 {\r
36         public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable\r
37         {\r
38                 private ToolStrip owner;\r
39 \r
40                 #region Public Constructor\r
41                 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()\r
42                 {\r
43                         if (owner == null)\r
44                                 throw new ArgumentNullException ("owner");\r
45 \r
46                         this.owner = owner;\r
47 \r
48                         if (value != null)\r
49                                 foreach (ToolStripItem tsi in value)\r
50                                         this.Add (tsi);\r
51                 }\r
52                 #endregion\r
53 \r
54                 #region Public Properties\r
55                 public override bool IsReadOnly { get { return base.IsReadOnly; } }\r
56                 \r
57                 public virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }\r
58                 \r
59                 public virtual ToolStripItem this[string key] {\r
60                         get {\r
61                                 foreach (ToolStripItem tsi in this)\r
62                                         if (tsi.Name == key)\r
63                                                 return tsi;\r
64 \r
65                                 return null;\r
66                         }\r
67                 }\r
68                 #endregion\r
69 \r
70                 #region Public Methods\r
71                 public ToolStripItem Add (Image image)\r
72                 {\r
73                         ToolStripButton tsb = new ToolStripButton (image);\r
74                         this.Add (tsb);\r
75                         return tsb;\r
76                 }\r
77 \r
78                 public ToolStripItem Add (string text)\r
79                 {\r
80                         ToolStripButton tsb = new ToolStripButton (text);\r
81                         this.Add (tsb);\r
82                         return tsb;\r
83                 }\r
84 \r
85                 public int Add (ToolStripItem value)\r
86                 {\r
87                         if (value == null)\r
88                                 throw new ArgumentNullException ("value");\r
89 \r
90                         value.Owner = owner;\r
91                         value.Parent = owner;\r
92                         owner.PerformLayout ();\r
93                         owner.OnItemAdded (new ToolStripItemEventArgs (value));\r
94                         return base.Add (value);\r
95                 }\r
96 \r
97                 public ToolStripItem Add (string text, Image image)\r
98                 {\r
99                         ToolStripButton tsb = new ToolStripButton (text, image);\r
100                         this.Add (tsb);\r
101                         return tsb;\r
102                 }\r
103 \r
104                 public ToolStripItem Add (string text, Image image, EventHandler onClick)\r
105                 {\r
106                         ToolStripButton tsb = new ToolStripButton (text, image, onClick);\r
107                         this.Add (tsb);\r
108                         return tsb;\r
109                 }\r
110 \r
111                 public void AddRange (ToolStripItem[] toolStripItems)\r
112                 {\r
113                         if (toolStripItems == null)\r
114                                 throw new ArgumentNullException ("toolStripItems");\r
115                         if (this.IsReadOnly)\r
116                                 throw new NotSupportedException ("This collection is read-only");\r
117 \r
118                         this.owner.SuspendLayout ();\r
119 \r
120                         foreach (ToolStripItem tsi in toolStripItems)\r
121                                 this.Add (tsi);\r
122 \r
123                         this.owner.ResumeLayout ();\r
124                 }\r
125 \r
126                 public void AddRange (ToolStripItemCollection toolStripItems)\r
127                 {\r
128                         if (toolStripItems == null)\r
129                                 throw new ArgumentNullException ("toolStripItems");\r
130                         if (this.IsReadOnly)\r
131                                 throw new NotSupportedException ("This collection is read-only");\r
132 \r
133                         this.owner.SuspendLayout ();\r
134 \r
135                         foreach (ToolStripItem tsi in toolStripItems)\r
136                                 this.Add (tsi);\r
137 \r
138                         this.owner.ResumeLayout ();\r
139                 }\r
140 \r
141                 public virtual void Clear ()\r
142                 {\r
143                         if (this.IsReadOnly)\r
144                                 throw new NotSupportedException ("This collection is read-only");\r
145 \r
146                         base.Clear ();\r
147                         owner.PerformLayout ();\r
148                 }\r
149 \r
150                 public bool Contains (ToolStripItem value)\r
151                 {\r
152                         return base.Contains (value);\r
153                 }\r
154 \r
155                 public virtual bool ContainsKey (string key)\r
156                 {\r
157                         return this[key] != null;\r
158                 }\r
159 \r
160                 public void CopyTo (ToolStripItem[] array, int index)\r
161                 {\r
162                         base.CopyTo (array, index);\r
163                 }\r
164 \r
165                 [MonoTODO ()]\r
166                 public ToolStripItem[] Find (string key, bool searchAllChildren)\r
167                 {\r
168                         if (key == null)\r
169                                 throw new ArgumentNullException ("key");\r
170 \r
171                         ArrayList al = new ArrayList ();\r
172 \r
173                         foreach (ToolStripItem tsi in this) {\r
174                                 if (tsi.Name == key) {\r
175                                         al.Add (tsi);\r
176 \r
177                                         if (searchAllChildren) {\r
178                                                 // TODO: tsi does not have an items property yet..\r
179                                         }\r
180                                 }\r
181                         }\r
182 \r
183                         return (ToolStripItem[])al.ToArray ();\r
184                 }\r
185 \r
186                 public int IndexOf (ToolStripItem value)\r
187                 {\r
188                         return base.IndexOf (value);\r
189                 }\r
190 \r
191                 public virtual int IndexOfKey (string key)\r
192                 {\r
193                         ToolStripItem tsi = this[key];\r
194 \r
195                         if (tsi == null)\r
196                                 return -1;\r
197 \r
198                         return this.IndexOf (tsi);\r
199                 }\r
200 \r
201                 public void Insert (int index, ToolStripItem value)\r
202                 {\r
203                         if (value == null)\r
204                                 throw new ArgumentNullException ("value");\r
205 \r
206                         base.Insert (index, value);\r
207                         owner.OnItemAdded (new ToolStripItemEventArgs (value));\r
208                         owner.PerformLayout ();\r
209                 }\r
210 \r
211                 public void Remove (ToolStripItem value)\r
212                 {\r
213                         if (this.IsReadOnly)\r
214                                 throw new NotSupportedException ("This collection is read-only");\r
215 \r
216                         base.Remove (value);\r
217                         owner.OnItemRemoved (new ToolStripItemEventArgs (value));\r
218                         owner.PerformLayout ();\r
219                 }\r
220 \r
221                 public void RemoveAt (int index)\r
222                 {\r
223                         if (this.IsReadOnly)\r
224                                 throw new NotSupportedException ("This collection is read-only");\r
225 \r
226                         ToolStripItem tsi = (ToolStripItem)base[index];\r
227                         base.RemoveAt (index);\r
228                         owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));\r
229                         owner.PerformLayout ();\r
230                 }\r
231 \r
232                 public virtual void RemoveByKey (string key)\r
233                 {\r
234                         if (this.IsReadOnly)\r
235                                 throw new NotSupportedException ("This collection is read-only");\r
236 \r
237                         ToolStripItem tsi = this[key];\r
238 \r
239                         if (tsi != null)\r
240                                 this.Remove (tsi);\r
241 \r
242                         return;\r
243                 }\r
244                 #endregion\r
245 \r
246                 #region IList Members\r
247                 int IList.Add (object value)\r
248                 {\r
249                         return this.Add ((ToolStripItem)value);\r
250                 }\r
251 \r
252                 void IList.Clear ()\r
253                 {\r
254                         this.Clear ();\r
255                 }\r
256 \r
257                 bool IList.Contains (object value)\r
258                 {\r
259                         return this.Contains ((ToolStripItem)value);\r
260                 }\r
261 \r
262                 int IList.IndexOf (object value)\r
263                 {\r
264                         return this.IndexOf ((ToolStripItem)value);\r
265                 }\r
266 \r
267                 void IList.Insert (int index, object value)\r
268                 {\r
269                         this.Insert (index, (ToolStripItem)value);\r
270                 }\r
271 \r
272                 bool IList.IsFixedSize {\r
273                         get { return this.IsFixedSize; }\r
274                 }\r
275 \r
276                 bool IList.IsReadOnly {\r
277                         get { return this.IsReadOnly; }\r
278                 }\r
279 \r
280                 void IList.Remove (object value)\r
281                 {\r
282                         this.Remove ((ToolStripItem)value); ;\r
283                 }\r
284 \r
285                 void IList.RemoveAt (int index)\r
286                 {\r
287                         this.RemoveAt (index);\r
288                 }\r
289 \r
290                 object IList.this[int index] {\r
291                         get { return this[index]; }\r
292                         set { throw new NotSupportedException (); }\r
293                 }\r
294                 #endregion\r
295         }\r
296 }\r
297 #endif