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