Merge pull request #2102 from AdamBurgess/master
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ToolStripComboBox.cs
1 //
2 // ToolStripComboBox.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
29 using System.Drawing;
30 using System.ComponentModel;
31 using System.Windows.Forms.Design;
32
33 namespace System.Windows.Forms
34 {
35         [DefaultProperty ("Items")]
36         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
37         public class ToolStripComboBox : ToolStripControlHost
38         {
39                 #region Public Constructors
40                 public ToolStripComboBox () : base (new ToolStripComboBoxControl ())
41                 {
42                         // The default size of a new ToolStripComboBox doesn't seem
43                         // to be DefaultSize.
44                         Size = new Size (121, 21);
45                 }
46
47                 [EditorBrowsable (EditorBrowsableState.Never)]
48                 public ToolStripComboBox (Control c) : base (c)
49                 {
50                         throw new NotSupportedException ();
51                 }
52
53                 public ToolStripComboBox (string name) : this ()
54                 {
55                         base.Name = name;
56                 }
57                 #endregion
58
59                 #region Public Properties
60                 [Browsable (true)]
61                 [Localizable (true)]
62                 [EditorBrowsable (EditorBrowsableState.Always)]
63                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
64                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
65                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
66                 public AutoCompleteStringCollection AutoCompleteCustomSource {
67                         get { return ComboBox.AutoCompleteCustomSource; }
68                         set { ComboBox.AutoCompleteCustomSource = value; }
69                 }
70
71                 [Browsable (true)]
72                 [DefaultValue (AutoCompleteMode.None)]
73                 [EditorBrowsable (EditorBrowsableState.Always)]
74                 public AutoCompleteMode AutoCompleteMode {
75                         get { return ComboBox.AutoCompleteMode; }
76                         set { ComboBox.AutoCompleteMode = value; }
77                 }
78
79                 [Browsable (true)]
80                 [DefaultValue (AutoCompleteSource.None)]
81                 [EditorBrowsable (EditorBrowsableState.Always)]
82                 public AutoCompleteSource AutoCompleteSource {
83                         get { return ComboBox.AutoCompleteSource; }
84                         set { ComboBox.AutoCompleteSource = value; }
85                 }
86
87                 [Browsable (false)]
88                 [EditorBrowsable (EditorBrowsableState.Never)]
89                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90                 public override Image BackgroundImage {
91                         get { return base.BackgroundImage; }
92                         set { base.BackgroundImage = value; }
93                 }
94
95                 [Browsable (false)]
96                 [EditorBrowsable (EditorBrowsableState.Never)]
97                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
98                 public override ImageLayout BackgroundImageLayout {
99                         get { return base.BackgroundImageLayout; }
100                         set { base.BackgroundImageLayout = value; }
101                 }
102                 
103                 [Browsable (false)]
104                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
105                 public ComboBox ComboBox {
106                         get { return (ComboBox)base.Control; }
107                 }
108
109                 [Browsable (true)]
110                 [DefaultValue (106)]
111                 [EditorBrowsable (EditorBrowsableState.Always)]
112                 public int DropDownHeight {
113                         get { return this.ComboBox.DropDownHeight; }
114                         set { this.ComboBox.DropDownHeight = value; }
115                 }
116                 
117                 [DefaultValue (ComboBoxStyle.DropDown)]
118                 [RefreshProperties (RefreshProperties.Repaint)]
119                 public ComboBoxStyle DropDownStyle {
120                         get { return this.ComboBox.DropDownStyle; }
121                         set { this.ComboBox.DropDownStyle = value; }
122                 }
123
124                 public int DropDownWidth {
125                         get { return this.ComboBox.DropDownWidth; }
126                         set { this.ComboBox.DropDownWidth = value; }
127                 }
128
129                 [Browsable (false)]
130                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131                 public bool DroppedDown {
132                         get { return this.ComboBox.DroppedDown; }
133                         set { this.ComboBox.DroppedDown = value; }
134                 }
135
136                 [LocalizableAttribute (true)]
137                 [DefaultValue (FlatStyle.Popup)]
138                 public FlatStyle FlatStyle {
139                         get { return ComboBox.FlatStyle; }
140                         set { ComboBox.FlatStyle = value; }
141                 }
142
143                 [Localizable (true)]
144                 [DefaultValue (true)]
145                 public bool IntegralHeight {
146                         get { return this.ComboBox.IntegralHeight; }
147                         set { this.ComboBox.IntegralHeight = value; }
148                 }
149
150                 [Localizable (true)]
151                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
152                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
153                 public ComboBox.ObjectCollection Items {
154                         get { return this.ComboBox.Items; }
155                 }
156
157                 [Localizable (true)]
158                 [DefaultValue (8)]
159                 public int MaxDropDownItems {
160                         get { return this.ComboBox.MaxDropDownItems; }
161                         set { this.ComboBox.MaxDropDownItems = value; }
162                 }
163
164                 [Localizable (true)]
165                 [DefaultValue (0)]
166                 public int MaxLength {
167                         get { return this.ComboBox.MaxLength; }
168                         set { this.ComboBox.MaxLength = value; }
169                 }
170
171                 [Browsable (false)]
172                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173                 public int SelectedIndex {
174                         get { return this.ComboBox.SelectedIndex; }
175                         set {
176                                 this.ComboBox.SelectedIndex = value;
177                                 
178                                 if (this.ComboBox.SelectedIndex >= 0)
179                                         Text = Items [value].ToString ();
180                         }
181                 }
182
183                 [Bindable (true)]
184                 [Browsable (false)]
185                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
186                 public Object SelectedItem {
187                         get { return this.ComboBox.SelectedItem; }
188                         set { this.ComboBox.SelectedItem = value; }
189                 }
190
191                 [Browsable (false)]
192                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
193                 public string SelectedText {
194                         get { return this.ComboBox.SelectedText; }
195                         set { this.ComboBox.SelectedText = value; }
196                 }
197
198                 [Browsable (false)]
199                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
200                 public int SelectionLength {
201                         get { return this.ComboBox.SelectionLength; }
202                         set { this.ComboBox.SelectionLength = value; }
203                 }
204
205                 [Browsable (false)]
206                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
207                 public int SelectionStart {
208                         get { return this.ComboBox.SelectionStart; }
209                         set { this.ComboBox.SelectionStart = value; }
210                 }
211
212                 [DefaultValue (false)]
213                 public bool Sorted {
214                         get { return this.ComboBox.Sorted; }
215                         set { this.ComboBox.Sorted = value; }
216                 }
217                 #endregion
218
219                 #region Protected Properties
220                 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
221                 protected override Size DefaultSize { get { return new Size (100, 22); } }
222                 #endregion
223
224                 #region Public Methods
225                 public void BeginUpdate ()
226                 {
227                         this.ComboBox.BeginUpdate ();
228                 }
229
230                 public void EndUpdate ()
231                 {
232                         this.ComboBox.EndUpdate ();
233                 }
234
235                 public int FindString (string s)
236                 {
237                         return this.ComboBox.FindString (s);
238                 }
239
240                 public int FindString (string s, int startIndex)
241                 {
242                         return this.ComboBox.FindString (s, startIndex);
243                 }
244
245                 public int FindStringExact (string s)
246                 {
247                         return this.ComboBox.FindStringExact (s);
248                 }
249
250                 public int FindStringExact (string s, int startIndex)
251                 {
252                         return this.ComboBox.FindStringExact (s, startIndex);
253                 }
254
255                 public int GetItemHeight (int index)
256                 {
257                         return this.ComboBox.GetItemHeight (index);
258                 }
259
260                 public override Size GetPreferredSize (Size constrainingSize)
261                 {
262                         return base.GetPreferredSize (constrainingSize);
263                 }
264
265                 public void Select (int start, int length)
266                 {
267                         this.ComboBox.Select (start, length);
268                 }
269
270                 public void SelectAll ()
271                 {
272                         this.ComboBox.SelectAll ();
273                 }
274
275                 public override string ToString ()
276                 {
277                         return this.ComboBox.ToString ();
278                 }
279                 #endregion
280
281                 #region Protected Methods
282                 protected virtual void OnDropDown (EventArgs e)
283                 {
284                         EventHandler eh = (EventHandler)(Events [DropDownEvent]);
285                         if (eh != null)
286                                 eh (this, e);
287                 }
288
289                 protected virtual void OnDropDownClosed (EventArgs e)
290                 {
291                         EventHandler eh = (EventHandler)(Events [DropDownClosedEvent]);
292                         if (eh != null)
293                                 eh (this, e);
294                 }
295
296                 protected virtual void OnDropDownStyleChanged (EventArgs e)
297                 {
298                         EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
299                         if (eh != null)
300                                 eh (this, e);
301                 }
302
303                 protected virtual void OnSelectedIndexChanged (EventArgs e)
304                 {
305                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
306                         if (eh != null)
307                                 eh (this, e);
308                 }
309
310                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
311                 {
312                 }
313
314                 protected override void OnSubscribeControlEvents (Control control)
315                 {
316                         base.OnSubscribeControlEvents (control);
317
318                         this.ComboBox.DropDown += new EventHandler (HandleDropDown);
319                         this.ComboBox.DropDownClosed += new EventHandler(HandleDropDownClosed);
320                         this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
321                         this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
322                         this.ComboBox.TextChanged += new EventHandler (HandleTextChanged);
323                         this.ComboBox.TextUpdate += new EventHandler (HandleTextUpdate);
324                 }
325
326                 protected virtual void OnTextUpdate (EventArgs e)
327                 {
328                         EventHandler eh = (EventHandler)(Events [TextUpdateEvent]);
329                         if (eh != null)
330                                 eh (this, e);
331                 }
332
333                 protected override void OnUnsubscribeControlEvents (Control control)
334                 {
335                         base.OnUnsubscribeControlEvents (control);
336                 }
337                 #endregion
338
339                 #region Public Events
340                 static object DropDownEvent = new object ();
341                 static object DropDownClosedEvent = new object ();
342                 static object DropDownStyleChangedEvent = new object ();
343                 static object SelectedIndexChangedEvent = new object ();
344                 static object TextUpdateEvent = new object ();
345
346                 [Browsable (false)]
347                 [EditorBrowsable (EditorBrowsableState.Never)]
348                 public new event EventHandler DoubleClick {
349                         add { base.DoubleClick += value; }
350                         remove { base.DoubleClick -= value; }
351                 }
352
353                 public event EventHandler DropDown {
354                         add { Events.AddHandler (DropDownEvent, value); }
355                         remove { Events.RemoveHandler (DropDownEvent, value); }
356                 }
357
358                 public event EventHandler DropDownClosed {
359                         add { Events.AddHandler (DropDownClosedEvent, value); }
360                         remove { Events.RemoveHandler (DropDownClosedEvent, value); }
361                 }
362
363                 public event EventHandler DropDownStyleChanged {
364                         add { Events.AddHandler (DropDownStyleChangedEvent, value); }
365                         remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
366                 }
367
368                 public event EventHandler SelectedIndexChanged {
369                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
370                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
371                 }
372
373                 public event EventHandler TextUpdate {
374                         add { Events.AddHandler (TextUpdateEvent, value); }
375                         remove { Events.RemoveHandler (TextUpdateEvent, value); }
376                 }
377                 #endregion
378
379                 #region Private Methods
380                 private void HandleDropDown (object sender, EventArgs e)
381                 {
382                         OnDropDown (e);
383                 }
384
385                 private void HandleDropDownClosed (object sender, EventArgs e)
386                 {
387                         OnDropDownClosed (e);
388                 }
389                 
390                 private void HandleDropDownStyleChanged (object sender, EventArgs e)
391                 {
392                         OnDropDownStyleChanged (e);
393                 }
394
395                 private void HandleSelectedIndexChanged (object sender, EventArgs e)
396                 {
397                         OnSelectedIndexChanged (e);
398                 }
399
400                 private void HandleTextChanged (object sender, EventArgs e)
401                 {
402                         OnTextChanged (e);
403                 }
404                 
405                 private void HandleTextUpdate (object sender, EventArgs e)
406                 {
407                         OnTextUpdate (e);
408                 }
409                 #endregion
410
411                 private class ToolStripComboBoxControl : ComboBox
412                 {
413                         public ToolStripComboBoxControl () : base ()
414                         {
415                                 this.border_style = BorderStyle.None;
416                                 this.FlatStyle = FlatStyle.Popup;
417                         }
418                 }
419         }
420 }