2008-11-20 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ComboBox.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //      Mike Kestner  <mkestner@novell.com>
25 //      Daniel Nauck    (dna(at)mono-project(dot)de)
26
27 using System;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
32 using System.Drawing;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
36
37 namespace System.Windows.Forms
38 {
39         [DefaultProperty("Items")]
40         [DefaultEvent("SelectedIndexChanged")]
41         [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 #if NET_2_0
43         [DefaultBindingProperty ("Text")]
44         [ClassInterface (ClassInterfaceType.AutoDispatch)]
45         [ComVisible(true)]
46 #endif
47         public class ComboBox : ListControl
48         {
49                 private DrawMode draw_mode = DrawMode.Normal;
50                 private ComboBoxStyle dropdown_style;
51                 private int dropdown_width = -1;
52                 private int selected_index = -1;
53                 private ObjectCollection items;
54                 private bool suspend_ctrlupdate;
55                 private int maxdrop_items = 8;
56                 private bool integral_height = true;
57                 private bool sorted;
58                 private int max_length;
59                 private ComboListBox listbox_ctrl;
60                 private ComboTextBox textbox_ctrl;
61                 private bool process_textchanged_event = true;
62                 private bool item_height_specified;
63                 private int item_height;
64                 private int requested_height = -1;
65                 private Hashtable item_heights;
66                 private bool show_dropdown_button;
67                 private ButtonState button_state = ButtonState.Normal;
68                 private bool dropped_down;
69                 private Rectangle text_area;
70                 private Rectangle button_area;
71                 private Rectangle listbox_area;
72                 private const int button_width = 16;
73                 bool drop_down_button_entered;
74 #if NET_2_0
75                 private AutoCompleteStringCollection auto_complete_custom_source = null;
76                 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
77                 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
78                 private int drop_down_height;
79                 private FlatStyle flat_style;
80 #endif
81
82                 [ComVisible(true)]
83                 public class ChildAccessibleObject : AccessibleObject {
84
85                         public ChildAccessibleObject (ComboBox owner, IntPtr handle)
86                                 : base (owner)
87                         {
88                         }
89
90                         public override string Name {
91                                 get {
92                                         return base.Name;
93                                 }
94                         }
95                 }
96
97                 public ComboBox ()
98                 {
99                         items = new ObjectCollection (this);
100                         DropDownStyle = ComboBoxStyle.DropDown;
101                         item_height = FontHeight + 2;
102                         background_color = ThemeEngine.Current.ColorWindow;
103                         border_style = BorderStyle.None;
104
105 #if NET_2_0
106                         drop_down_height = 106;
107                         flat_style = FlatStyle.Standard;
108 #endif
109
110                         /* Events */
111                         MouseDown += new MouseEventHandler (OnMouseDownCB);
112                         MouseUp += new MouseEventHandler (OnMouseUpCB);
113                         MouseMove += new MouseEventHandler (OnMouseMoveCB);
114                         MouseWheel += new MouseEventHandler (OnMouseWheelCB);
115                         MouseEnter += new EventHandler (OnMouseEnter);
116                         MouseLeave += new EventHandler (OnMouseLeave);
117                         KeyDown +=new KeyEventHandler(OnKeyDownCB);
118                 }
119
120                 #region events
121                 
122                 [Browsable (false)]
123                 [EditorBrowsable (EditorBrowsableState.Never)]
124                 public new event EventHandler BackgroundImageChanged {
125                         add { base.BackgroundImageChanged += value; }
126                         remove { base.BackgroundImageChanged -= value; }
127                 }
128                 
129 #if NET_2_0
130
131                 [Browsable (false)]
132                 [EditorBrowsable (EditorBrowsableState.Never)]
133                 public new event EventHandler BackgroundImageLayoutChanged
134                 {
135                         add { base.BackgroundImageLayoutChanged += value; }
136                         remove { base.BackgroundImageLayoutChanged -= value; }
137                 }
138
139                 [Browsable (false)]
140                 [EditorBrowsable (EditorBrowsableState.Never)]
141                 public new event EventHandler DoubleClick
142                 {
143                         add { base.DoubleClick += value; }
144                         remove { base.DoubleClick -= value; }
145                 }
146 #endif
147
148                 static object DrawItemEvent = new object ();
149                 static object DropDownEvent = new object ();
150                 static object DropDownStyleChangedEvent = new object ();
151                 static object MeasureItemEvent = new object ();
152                 static object SelectedIndexChangedEvent = new object ();
153                 static object SelectionChangeCommittedEvent = new object ();
154 #if NET_2_0
155                 static object DropDownClosedEvent = new object ();
156                 static object TextUpdateEvent = new object ();
157 #endif
158
159                 public event DrawItemEventHandler DrawItem {
160                         add { Events.AddHandler (DrawItemEvent, value); }
161                         remove { Events.RemoveHandler (DrawItemEvent, value); }
162                 }
163
164                 public event EventHandler DropDown {
165                         add { Events.AddHandler (DropDownEvent, value); }
166                         remove { Events.RemoveHandler (DropDownEvent, value); }
167                 }
168 #if NET_2_0
169                 public event EventHandler DropDownClosed
170                 {
171                         add { Events.AddHandler (DropDownClosedEvent, value); }
172                         remove { Events.RemoveHandler (DropDownClosedEvent, value); }
173                 }
174 #endif
175
176                 public event EventHandler DropDownStyleChanged {
177                         add { Events.AddHandler (DropDownStyleChangedEvent, value); }
178                         remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
179                 }
180
181                 public event MeasureItemEventHandler MeasureItem {
182                         add { Events.AddHandler (MeasureItemEvent, value); }
183                         remove { Events.RemoveHandler (MeasureItemEvent, value); }
184                 }
185 #if NET_2_0
186                 [Browsable (false)]
187                 [EditorBrowsable (EditorBrowsableState.Never)]
188                 public new event EventHandler PaddingChanged
189                 {
190                         add { base.PaddingChanged += value; }
191                         remove { base.PaddingChanged -= value; }
192                 }
193 #endif
194                 
195                 [Browsable (false)]
196                 [EditorBrowsable (EditorBrowsableState.Never)]
197                 public new event PaintEventHandler Paint {
198                         add { base.Paint += value; }
199                         remove { base.Paint -= value; }
200                 }
201                 
202                 public event EventHandler SelectedIndexChanged {
203                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
204                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
205                 }
206
207                 public event EventHandler SelectionChangeCommitted {
208                         add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
209                         remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
210                 }
211 #if NET_2_0
212                 public event EventHandler TextUpdate
213                 {
214                         add { Events.AddHandler (TextUpdateEvent, value); }
215                         remove { Events.RemoveHandler (TextUpdateEvent, value); }
216                 }
217 #endif
218
219                 #endregion Events
220
221                 #region Public Properties
222 #if NET_2_0
223                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
224                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
225                 [Browsable (true)]
226                 [EditorBrowsable (EditorBrowsableState.Always)]
227                 [Localizable (true)]
228                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
229                          "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
230                 public AutoCompleteStringCollection AutoCompleteCustomSource { 
231                         get {
232                                 if(auto_complete_custom_source == null) {
233                                         auto_complete_custom_source = new AutoCompleteStringCollection ();
234                                         auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
235                                 }
236                                 return auto_complete_custom_source;
237                         }
238                         set {
239                                 if(auto_complete_custom_source == value)
240                                         return;
241
242                                 if(auto_complete_custom_source != null) //remove eventhandler from old collection
243                                         auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
244
245                                 auto_complete_custom_source = value;
246
247                                 if(auto_complete_custom_source != null)
248                                         auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
249
250                                 SetTextBoxAutoCompleteData ();
251                         }
252                 }
253
254                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
255                 [Browsable (true)]
256                 [EditorBrowsable (EditorBrowsableState.Always)]
257                 [DefaultValue (AutoCompleteMode.None)]
258                 public AutoCompleteMode AutoCompleteMode {
259                         get { return auto_complete_mode; }
260                         set {
261                                 if(auto_complete_mode == value)
262                                         return;
263
264                                 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
265                                         throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
266
267                                 auto_complete_mode = value;
268                                 SetTextBoxAutoCompleteData ();
269                         }
270                 }
271
272                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
273                 [Browsable (true)]
274                 [EditorBrowsable (EditorBrowsableState.Always)]
275                 [DefaultValue (AutoCompleteSource.None)]
276                 public AutoCompleteSource AutoCompleteSource {
277                         get { return auto_complete_source; }
278                         set {
279                                 if(auto_complete_source == value)
280                                         return;
281
282                                 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
283                                         throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
284
285                                 auto_complete_source = value;
286                                 SetTextBoxAutoCompleteData ();
287                         }
288                 }
289
290                 void SetTextBoxAutoCompleteData ()
291                 {
292                         if (textbox_ctrl == null)
293                                 return;
294
295                         textbox_ctrl.AutoCompleteMode = auto_complete_mode;
296
297                         if (auto_complete_source == AutoCompleteSource.ListItems) {
298                                 textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
299                                 textbox_ctrl.AutoCompleteCustomSource = null;
300                                 textbox_ctrl.AutoCompleteInternalSource = this;
301                         } else {
302                                 textbox_ctrl.AutoCompleteSource = auto_complete_source;
303                                 textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
304                                 textbox_ctrl.AutoCompleteInternalSource = null;
305                         }
306                 }
307 #endif
308                 public override Color BackColor {
309                         get { return base.BackColor; }
310                         set {
311                                 if (base.BackColor == value)
312                                         return;
313                                 base.BackColor = value;
314                                 Refresh ();
315                         }
316                 }
317
318                 [Browsable (false)]
319                 [EditorBrowsable (EditorBrowsableState.Never)]
320                 public override Image BackgroundImage {
321                         get { return base.BackgroundImage; }
322                         set {
323                                 if (base.BackgroundImage == value)
324                                         return;
325                                 base.BackgroundImage = value;
326                                 Refresh ();
327                         }
328                 }
329
330 #if NET_2_0
331                 [Browsable (false)]
332                 [EditorBrowsable (EditorBrowsableState.Never)]
333                 public override ImageLayout BackgroundImageLayout {
334                         get { return base.BackgroundImageLayout; }
335                         set { base.BackgroundImageLayout = value; }
336                 }
337 #endif
338
339                 protected override CreateParams CreateParams {
340                         get { return base.CreateParams;}
341                 }
342
343 #if NET_2_0
344                 [DefaultValue ((string)null)]
345                 [AttributeProvider (typeof (IListSource))]
346                 [RefreshProperties (RefreshProperties.Repaint)]
347                 [MWFCategory("Data")]
348                 public new object DataSource {
349                         get { return base.DataSource; }
350                         set { base.DataSource = value; }
351                 }
352 #endif
353
354                 protected override Size DefaultSize {
355                         get { return new Size (121, 21); }
356                 }
357
358                 [RefreshProperties(RefreshProperties.Repaint)]
359                 [DefaultValue (DrawMode.Normal)]
360                 [MWFCategory("Behavior")]
361                 public DrawMode DrawMode {
362                         get { return draw_mode; }
363                         set {
364                                 if (!Enum.IsDefined (typeof (DrawMode), value))
365                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
366
367                                 if (draw_mode == value)
368                                         return;
369
370                                 if (draw_mode == DrawMode.OwnerDrawVariable)
371                                         item_heights = null;
372                                 draw_mode = value;
373                                 if (draw_mode == DrawMode.OwnerDrawVariable)
374                                         item_heights = new Hashtable ();
375                                 Refresh ();
376                         }
377                 }
378
379 #if NET_2_0
380                 [Browsable (true)]
381                 [DefaultValue (106)]
382                 [EditorBrowsable (EditorBrowsableState.Always)]
383                 [MWFCategory("Behavior")]
384                 public int DropDownHeight {
385                         get {
386                                 return drop_down_height;
387                         }
388                         set {
389                                 if (value < 1)
390                                         throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
391                                         
392                                 drop_down_height = value;
393                                 IntegralHeight = false;
394                         }
395                 }
396 #endif
397
398                 [DefaultValue (ComboBoxStyle.DropDown)]
399                 [RefreshProperties(RefreshProperties.Repaint)]
400                 [MWFCategory("Appearance")]
401                 public ComboBoxStyle DropDownStyle {
402                         get { return dropdown_style; }
403                         set {
404                                 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
405                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
406
407                                 if (dropdown_style == value)
408                                         return;
409
410                                 SuspendLayout ();
411
412                                 if (dropdown_style == ComboBoxStyle.Simple) {
413                                         if (listbox_ctrl != null) {
414                                                 Controls.RemoveImplicit (listbox_ctrl);
415                                                 listbox_ctrl.Dispose ();
416                                                 listbox_ctrl = null;
417                                         }
418                                 }
419
420                                 dropdown_style = value;
421                                 
422                                 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
423                                         Controls.RemoveImplicit (textbox_ctrl);
424                                         textbox_ctrl.Dispose ();
425                                         textbox_ctrl = null;
426                                 }
427
428                                 if (dropdown_style == ComboBoxStyle.Simple) {
429                                         show_dropdown_button = false;
430                                         
431                                         CreateComboListBox ();
432                                         Controls.AddImplicit (listbox_ctrl);
433                                         listbox_ctrl.Visible = true;
434
435                                         // This should give us a 150 default height
436                                         // for Simple mode if size hasn't been set
437                                         // (DefaultSize doesn't work for us in this case)
438                                         if (requested_height == -1)
439                                                 requested_height = 150;
440                                 } else {
441                                         show_dropdown_button = true;
442                                         button_state = ButtonState.Normal;
443                                 }
444         
445                                 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
446                                         textbox_ctrl = new ComboTextBox (this);
447                                         object selected_item = SelectedItem;
448                                         if (selected_item != null)
449                                                 textbox_ctrl.Text = GetItemText (selected_item);
450                                         textbox_ctrl.BorderStyle = BorderStyle.None;
451                                         textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
452                                         textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
453                                         textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
454                                         textbox_ctrl.ContextMenu = ContextMenu;
455
456                                         if (IsHandleCreated == true)
457                                                 Controls.AddImplicit (textbox_ctrl);
458 #if NET_2_0
459                                         SetTextBoxAutoCompleteData ();
460 #endif
461                                 }
462                                 
463                                 ResumeLayout ();
464                                 OnDropDownStyleChanged (EventArgs.Empty);
465                                 
466                                 LayoutComboBox ();
467                                 UpdateComboBoxBounds ();
468                                 Refresh ();
469                         }
470                 }
471
472                 [MWFCategory("Behavior")]
473                 public int DropDownWidth {
474                         get { 
475                                 if (dropdown_width == -1)
476                                         return Width;
477                                         
478                                 return dropdown_width; 
479                         }
480                         set {
481                                 if (dropdown_width == value)
482                                         return;
483                                         
484                                 if (value < 1)
485 #if NET_2_0
486                                         throw new ArgumentOutOfRangeException ("DropDownWidth",
487                                                 "The DropDownWidth value is less than one.");
488 #else
489                                         throw new ArgumentException ("The DropDownWidth value is less than one.");
490 #endif
491
492                                 dropdown_width = value;
493                         }
494                 }
495                 
496                 [Browsable (false)]
497                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
498                 public bool DroppedDown {
499                         get { 
500                                 if (dropdown_style == ComboBoxStyle.Simple)
501                                         return true;
502                                 
503                                 return dropped_down;
504                         }
505                         set {
506                                 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
507                                         return;
508                                         
509                                 if (value) 
510                                         DropDownListBox ();
511                                 else
512                                         listbox_ctrl.HideWindow ();
513                         }
514                 }
515
516 #if NET_2_0
517                 [DefaultValue (FlatStyle.Standard)]
518                 [Localizable (true)]
519                 [MWFCategory("Appearance")]
520                 public FlatStyle FlatStyle {
521                         get { return flat_style; }
522                         set {
523                                 if (!Enum.IsDefined (typeof (FlatStyle), value))
524                                         throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
525                                 
526                                 flat_style = value;
527                                 LayoutComboBox ();
528                                 Invalidate ();
529                         }
530                 }
531 #endif
532
533                 public override bool Focused {
534                         get { return base.Focused; }
535                 }
536
537                 public override Color ForeColor {
538                         get { return base.ForeColor; }
539                         set {
540                                 if (base.ForeColor == value)
541                                         return;
542                                 base.ForeColor = value;
543                                 Refresh ();
544                         }
545                 }
546
547                 [DefaultValue (true)]
548                 [Localizable (true)]
549                 [MWFCategory("Behavior")]
550                 public bool IntegralHeight {
551                         get { return integral_height; }
552                         set {
553                                 if (integral_height == value)
554                                         return;
555                                 integral_height = value;
556                                 UpdateComboBoxBounds ();
557                                 Refresh ();
558                         }
559                 }
560
561                 [Localizable (true)]
562                 [MWFCategory("Behavior")]
563                 public int ItemHeight {
564                         get {
565                                 if (item_height == -1) {
566                                         SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
567                                         item_height = (int) sz.Height;
568                                 }
569                                 return item_height;
570                         }
571                         set {
572                                 if (value < 1)
573 #if NET_2_0
574                                         throw new ArgumentOutOfRangeException ("ItemHeight",
575                                                 "The item height value is less than one.");
576 #else
577                                         throw new ArgumentException ("The item height value is less than one.");
578 #endif
579
580                                 item_height_specified = true;
581                                 item_height = value;
582                                 if (IntegralHeight)
583                                         UpdateComboBoxBounds ();
584                                 LayoutComboBox ();
585                                 Refresh ();
586                         }
587                 }
588
589                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
590                 [Localizable (true)]
591                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
592 #if NET_2_0
593                 [MergableProperty (false)]
594 #endif
595                 [MWFCategory("Data")]
596                 public ComboBox.ObjectCollection Items {
597                         get { return items; }
598                 }
599
600                 [DefaultValue (8)]
601                 [Localizable (true)]
602                 [MWFCategory("Behavior")]
603                 public int MaxDropDownItems {
604                         get { return maxdrop_items; }
605                         set {
606                                 if (maxdrop_items == value)
607                                         return;
608                                 maxdrop_items = value;
609                         }
610                 }
611
612 #if NET_2_0
613                 public override Size MaximumSize {
614                         get { return base.MaximumSize; }
615                         set {
616                                 base.MaximumSize = new Size (value.Width, 0);
617                         }
618                 }
619 #endif
620
621                 [DefaultValue (0)]
622                 [Localizable (true)]
623                 [MWFCategory("Behavior")]
624                 public int MaxLength {
625                         get { return max_length; }
626                         set {
627                                 if (max_length == value)
628                                         return;
629
630                                 max_length = value;
631                                 
632                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
633                                         if (value < 0) {
634                                                 value = 0;
635                                         }
636                                         textbox_ctrl.MaxLength = value;
637                                 }
638                         }
639                 }
640
641 #if NET_2_0
642                 public override Size MinimumSize {
643                         get { return base.MinimumSize; }
644                         set {
645                                 base.MinimumSize = new Size (value.Width, 0);
646                         }
647                 }
648                 
649                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
650                 [EditorBrowsable (EditorBrowsableState.Never)]
651                 [Browsable (false)]
652                 public new Padding Padding  {
653                         get { return base.Padding; }
654                         set { base.Padding = value; }
655                 }
656 #endif
657
658                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
659                 [Browsable (false)]
660                 public int PreferredHeight {
661                         get { return Font.Height + 8; }
662                 }
663
664                 [Browsable (false)]
665                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
666                 public override int SelectedIndex {
667                         get { return selected_index; }
668                         set {
669                                 if (selected_index == value)
670                                         return;
671
672                                 if (value <= -2 || value >= Items.Count)
673                                         throw new ArgumentOutOfRangeException ("SelectedIndex");
674                                 selected_index = value;
675
676                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
677                                         if (value == -1)
678                                                 SetControlText (string.Empty, false);
679                                         else
680                                                 SetControlText (GetItemText (Items [value]), false);
681                                 }
682
683                                 if (DropDownStyle == ComboBoxStyle.DropDownList)
684                                         Invalidate ();
685
686                                 if (listbox_ctrl != null)
687                                         listbox_ctrl.HighlightedIndex = value;
688
689                                 OnSelectedValueChanged (new EventArgs ());
690                                 OnSelectedIndexChanged (new EventArgs ());
691                                 OnSelectedItemChanged (new EventArgs ());
692                         }
693                 }
694
695                 [Browsable (false)]
696                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
697                 [Bindable(true)]
698                 public object SelectedItem {
699                         get { return selected_index == -1 ? null : Items [selected_index]; }
700                         set {
701                                 object item = selected_index == -1 ? null : Items [selected_index];
702                                 if (item == value)
703                                         return;
704
705                                 if (value == null)
706                                         SelectedIndex = -1;
707                                 else
708                                         SelectedIndex = Items.IndexOf (value);
709                         }
710                 }
711                 
712                 [Browsable (false)]
713                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
714                 public string SelectedText {
715                         get {
716                                 if (dropdown_style == ComboBoxStyle.DropDownList)
717                                         return string.Empty;
718                                         
719                                 string retval = textbox_ctrl.SelectedText;
720                                 
721 #if ONLY_1_1
722                                 // On 1.1, the textbox will return null, combobox returns ""
723                                 if (retval == null && !textbox_ctrl.IsHandleCreated)
724                                         return string.Empty;
725 #endif                                  
726                                 return retval;
727                         }
728                         set {
729                                 if (dropdown_style == ComboBoxStyle.DropDownList)
730                                         return;
731                                 textbox_ctrl.SelectedText = value;
732                         }
733                 }
734
735                 [Browsable (false)]
736                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
737                 public int SelectionLength {
738                         get {
739                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
740                                         return 0;
741                                 
742                                 int result = textbox_ctrl.SelectionLength;
743                                 return result == -1 ? 0 : result;
744                         }
745                         set {
746                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
747                                         return;
748                                 if (textbox_ctrl.SelectionLength == value)
749                                         return;
750                                 textbox_ctrl.SelectionLength = value;
751                         }
752                 }
753
754                 [Browsable (false)]
755                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
756                 public int SelectionStart {
757                         get { 
758                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
759                                         return 0;
760                                 return textbox_ctrl.SelectionStart;
761                         }
762                         set {
763                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
764                                         return;
765                                 if (textbox_ctrl.SelectionStart == value)
766                                         return;
767                                 textbox_ctrl.SelectionStart = value;
768                         }
769                 }
770
771                 [DefaultValue (false)]
772                 [MWFCategory("Behavior")]
773                 public bool Sorted {
774                         get { return sorted; }
775                         set {
776                                 if (sorted == value)
777                                         return;
778                                 sorted = value;
779                                 SelectedIndex = -1;
780                                 if (sorted) {
781                                         Items.Sort ();
782                                         LayoutComboBox ();
783                                 }
784                         }
785                 }
786
787                 [Bindable (true)]
788                 [Localizable (true)]
789                 public override string Text {
790                         get {
791                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
792                                         if (textbox_ctrl != null) {
793                                                 return textbox_ctrl.Text;
794                                         }
795                                 }
796                                 
797                                 if (SelectedItem != null)
798                                         return GetItemText (SelectedItem);
799                                 
800                                 return base.Text;
801                         }
802                         set {
803                                 if (value == null) {
804                                         if (SelectedIndex == -1) {
805                                                 if (dropdown_style != ComboBoxStyle.DropDownList)
806                                                         SetControlText (string.Empty, false);
807                                         } else {
808                                                 SelectedIndex = -1;
809                                         }
810                                         return;
811                                 }
812
813                                 // do nothing if value exactly matches text of selected item
814                                 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
815                                         return;
816
817                                 // find exact match using case-sensitive comparison, and if does
818                                 // not result in any match then use case-insensitive comparison
819                                 int index = FindStringExact (value, -1, false);
820                                 if (index == -1) {
821                                         index = FindStringExact (value, -1, true);
822                                 }
823                                 if (index != -1) {
824                                         SelectedIndex = index;
825                                         return;
826                                 }
827
828                                 if (dropdown_style != ComboBoxStyle.DropDownList)
829                                         textbox_ctrl.Text = GetItemText (value);
830                         }
831                 }
832
833                 #endregion Public Properties
834
835                 #region Internal Properties
836                 internal Rectangle ButtonArea {
837                         get { return button_area; }
838                 }
839
840                 internal Rectangle TextArea {
841                         get { return text_area; }
842                 }
843                 #endregion
844
845                 #region UIA Framework Properties
846
847                 internal TextBox UIATextBox {
848                         get { return textbox_ctrl; }
849                 }
850
851                 #endregion UIA Framework Properties
852
853                 #region Public Methods
854 #if NET_2_0
855                 [Obsolete ("This method has been deprecated")]
856 #endif
857                 protected virtual void AddItemsCore (object[] value)
858                 {
859                         
860                 }
861
862                 public void BeginUpdate ()
863                 {
864                         suspend_ctrlupdate = true;
865                 }
866
867 #if NET_2_0
868                 protected override AccessibleObject CreateAccessibilityInstance ()
869                 {
870                         return base.CreateAccessibilityInstance ();
871                 }
872                 
873                 protected override void CreateHandle ()
874                 {
875                         base.CreateHandle ();
876                 }
877 #endif
878
879                 protected override void Dispose (bool disposing)
880                 {
881                         if (disposing) {
882                                 if (listbox_ctrl != null) {
883                                         listbox_ctrl.Dispose ();
884                                         Controls.RemoveImplicit (listbox_ctrl);
885                                         listbox_ctrl = null;
886                                 }
887                         
888                                 if (textbox_ctrl != null) {
889                                         Controls.RemoveImplicit (textbox_ctrl);
890                                         textbox_ctrl.Dispose ();
891                                         textbox_ctrl = null;
892                                 }
893                         }
894                         
895                         base.Dispose (disposing);
896                 }
897
898                 public void EndUpdate ()
899                 {
900                         suspend_ctrlupdate = false;
901                         UpdatedItems ();
902                         Refresh ();
903                 }
904
905                 public int FindString (string s)
906                 {
907                         return FindString (s, -1);
908                 }
909
910                 public int FindString (string s, int startIndex)
911                 {
912                         if (s == null || Items.Count == 0) 
913                                 return -1;
914
915 #if NET_2_0
916                         if (startIndex < -1 || startIndex >= Items.Count)
917 #else
918                         if (startIndex < -1 || startIndex >= Items.Count - 1)
919 #endif
920                                 throw new ArgumentOutOfRangeException ("startIndex");
921
922                         int i = startIndex;
923 #if NET_2_0
924                         if (i == (Items.Count - 1))
925                                 i = -1;
926 #endif
927                         do {
928                                 i++;
929                                 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
930                                         return i;
931                                 if (i == (Items.Count - 1))
932                                         i = -1;
933                         } while (i != startIndex);
934
935                         return -1;
936                 }
937
938                 public int FindStringExact (string s)
939                 {
940                         return FindStringExact (s, -1);
941                 }
942
943                 public int FindStringExact (string s, int startIndex)
944                 {
945                         return FindStringExact (s, startIndex, true);
946                 }
947
948                 private int FindStringExact (string s, int startIndex, bool ignoreCase)
949                 {
950                         if (s == null || Items.Count == 0) 
951                                 return -1;
952
953 #if NET_2_0
954                         if (startIndex < -1 || startIndex >= Items.Count)
955 #else
956                         if (startIndex < -1 || startIndex >= Items.Count - 1)
957 #endif
958                                 throw new ArgumentOutOfRangeException ("startIndex");
959
960                         int i = startIndex;
961 #if NET_2_0
962                         if (i == (Items.Count - 1))
963                                 i = -1;
964 #endif
965                         do {
966                                 i++;
967                                 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
968                                         return i;
969                                 if (i == (Items.Count - 1))
970                                         i = -1;
971                         } while (i != startIndex);
972
973                         return -1;
974                 }
975
976                 public int GetItemHeight (int index)
977                 {
978                         if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
979
980                                 if (index < 0 || index >= Items.Count )
981                                         throw new ArgumentOutOfRangeException ("The item height value is less than zero");
982                                 
983                                 object item = Items [index];
984                                 if (item_heights.Contains (item))
985                                         return (int) item_heights [item];
986                                 
987                                 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
988                                 OnMeasureItem (args);
989                                 item_heights [item] = args.ItemHeight;
990                                 return args.ItemHeight;
991                         }
992
993                         return ItemHeight;
994                 }
995
996                 protected override bool IsInputKey (Keys keyData)
997                 {
998                         switch (keyData & ~Keys.Modifiers) {
999                         case Keys.Up:
1000                         case Keys.Down:
1001                         case Keys.Left:
1002                         case Keys.Right:
1003                         case Keys.PageUp:
1004                         case Keys.PageDown:
1005                         case Keys.Home:
1006                         case Keys.End:
1007                                 return true;
1008                         
1009                         default:
1010                                 return false;
1011                         }
1012                 }
1013
1014                 protected override void OnBackColorChanged (EventArgs e)
1015                 {
1016                         base.OnBackColorChanged (e);
1017
1018                         if (textbox_ctrl != null)
1019                                 textbox_ctrl.BackColor = BackColor;
1020                 }
1021
1022                 protected override void OnDataSourceChanged (EventArgs e)
1023                 {
1024                         base.OnDataSourceChanged (e);
1025                         BindDataItems ();
1026                         
1027                         if (DataSource == null || DataManager == null) {
1028                                 SelectedIndex = -1;
1029                         } 
1030                         else {
1031                                 SelectedIndex = DataManager.Position;
1032                         }
1033                 }
1034
1035                 protected override void OnDisplayMemberChanged (EventArgs e)
1036                 {
1037                         base.OnDisplayMemberChanged (e);
1038
1039                         if (DataManager == null)
1040                                 return;
1041
1042                         if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1043                                 SetControlText (GetItemText (Items [selected_index]), true);
1044
1045                         if (!IsHandleCreated)
1046                                 return;
1047
1048                         SelectedIndex = DataManager.Position;
1049                         Invalidate ();
1050                 }
1051
1052                 protected virtual void OnDrawItem (DrawItemEventArgs e)
1053                 {
1054                         DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1055                         if (eh != null)
1056                                 eh (this, e);
1057                 }
1058
1059                 internal void HandleDrawItem (DrawItemEventArgs e)
1060                 {
1061                         // Only raise OnDrawItem if we are in an OwnerDraw mode
1062                         switch (DrawMode) {
1063                                 case DrawMode.OwnerDrawFixed:
1064                                 case DrawMode.OwnerDrawVariable:
1065                                         OnDrawItem (e);
1066                                         break;
1067                                 default:
1068                                         ThemeEngine.Current.DrawComboBoxItem (this, e);
1069                                         break;
1070                         }
1071                 }
1072
1073                 protected virtual void OnDropDown (EventArgs e)
1074                 {
1075                         EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1076                         if (eh != null)
1077                                 eh (this, e);
1078                 }
1079
1080 #if NET_2_0
1081                 protected virtual void OnDropDownClosed (EventArgs e)
1082                 {
1083                         EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1084                         if (eh != null)
1085                                 eh (this, e);
1086                 }
1087 #endif
1088
1089                 protected virtual void OnDropDownStyleChanged (EventArgs e)
1090                 {
1091                         EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1092                         if (eh != null)
1093                                 eh (this, e);
1094                 }
1095
1096                 protected override void OnFontChanged (EventArgs e)
1097                 {
1098                         base.OnFontChanged (e);
1099
1100                         if (textbox_ctrl != null)
1101                                 textbox_ctrl.Font = Font;
1102                         
1103                         if (!item_height_specified)
1104                                 item_height = Font.Height + 2;
1105
1106                         if (IntegralHeight)
1107                                 UpdateComboBoxBounds ();
1108
1109                         LayoutComboBox ();
1110                 }
1111
1112                 protected override void OnForeColorChanged (EventArgs e)
1113                 {
1114                         base.OnForeColorChanged (e);
1115                         if (textbox_ctrl != null)
1116                                 textbox_ctrl.ForeColor = ForeColor;
1117                 }
1118
1119                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1120                 protected override void OnGotFocus (EventArgs e)
1121                 {
1122                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1123                                 // We draw DDL styles manually, so they require a
1124                                 // refresh to have their selection drawn
1125                                 Invalidate ();
1126                         }
1127                         
1128                         if (textbox_ctrl != null) {
1129                                 textbox_ctrl.SetSelectable (false);
1130                                 textbox_ctrl.ShowSelection = true;
1131                                 textbox_ctrl.ActivateCaret (true);
1132                                 textbox_ctrl.SelectAll ();
1133                         }
1134
1135                         base.OnGotFocus (e);
1136                 }
1137
1138                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1139                 protected override void OnLostFocus (EventArgs e)
1140                 {
1141                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1142                                 // We draw DDL styles manually, so they require a
1143                                 // refresh to have their selection drawn
1144                                 Invalidate ();
1145                         }
1146
1147                         if (listbox_ctrl != null && dropped_down) {
1148                                 listbox_ctrl.HideWindow ();
1149                         }
1150
1151                         if (textbox_ctrl != null) {
1152                                 textbox_ctrl.SetSelectable (true);
1153                                 textbox_ctrl.ActivateCaret (false);
1154                                 textbox_ctrl.ShowSelection = false;
1155                                 textbox_ctrl.SelectionLength = 0;
1156                         }
1157
1158                         base.OnLostFocus (e);
1159                 }
1160
1161                 protected override void OnHandleCreated (EventArgs e)
1162                 {
1163                         base.OnHandleCreated (e);
1164
1165                         SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1166
1167                         if (textbox_ctrl != null)
1168                                 Controls.AddImplicit (textbox_ctrl);
1169
1170                         LayoutComboBox ();
1171                         UpdateComboBoxBounds ();
1172                 }
1173
1174                 protected override void OnHandleDestroyed (EventArgs e)
1175                 {
1176                         base.OnHandleDestroyed (e);
1177                 }
1178
1179                 protected override void OnKeyPress (KeyPressEventArgs e)
1180                 {
1181                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1182                                 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1183                                 if (index != -1) {
1184                                         SelectedIndex = index;
1185                                         if (DroppedDown) { //Scroll into view
1186                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1187                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1188                                                 // Or, selecting an item earlier in the list.
1189                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1190                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1191                                         }
1192                                 }
1193                         }
1194
1195                         base.OnKeyPress (e);
1196                 }
1197
1198                 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1199                 {
1200                         MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1201                         if (eh != null)
1202                                 eh (this, e);
1203                 }
1204
1205                 protected override void OnParentBackColorChanged (EventArgs e)
1206                 {
1207                         base.OnParentBackColorChanged (e);
1208                 }
1209
1210                 protected override void OnResize (EventArgs e)
1211                 {
1212                         LayoutComboBox ();
1213                         if (listbox_ctrl != null)
1214                                 listbox_ctrl.CalcListBoxArea ();
1215                 }
1216
1217                 protected override void OnSelectedIndexChanged (EventArgs e)
1218                 {
1219                         base.OnSelectedIndexChanged (e);
1220
1221                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1222                         if (eh != null)
1223                                 eh (this, e);
1224                 }
1225
1226                 protected virtual void OnSelectedItemChanged (EventArgs e)
1227                 {
1228                 }
1229
1230                 protected override void OnSelectedValueChanged (EventArgs e)
1231                 {
1232                         base.OnSelectedValueChanged (e);
1233                 }
1234
1235                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1236                 {
1237                         EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1238                         if (eh != null)
1239                                 eh (this, e);
1240                 }
1241
1242                 protected override void RefreshItem (int index)
1243                 {
1244                         if (index < 0 || index >= Items.Count)
1245                                 throw new ArgumentOutOfRangeException ("index");
1246                                 
1247                         if (draw_mode == DrawMode.OwnerDrawVariable)
1248                                 item_heights.Remove (Items [index]);
1249                 }
1250
1251 #if NET_2_0
1252                 protected override void RefreshItems ()
1253                 {
1254                         for (int i = 0; i < Items.Count; i++) {
1255                                 RefreshItem (i);
1256                         }
1257
1258                         LayoutComboBox ();
1259                         Refresh ();
1260
1261                         if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1262                                 SetControlText (GetItemText (Items [selected_index]), false);
1263                 }
1264
1265                 public override void ResetText ()
1266                 {
1267                         Text = String.Empty;
1268                 }
1269                 
1270                 protected override bool ProcessKeyEventArgs (ref Message m)
1271                 {
1272                         return base.ProcessKeyEventArgs (ref m);
1273                 }
1274
1275                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1276                 protected override void OnKeyDown (KeyEventArgs e)
1277                 {
1278                         base.OnKeyDown (e);
1279                 }
1280
1281                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1282                 protected override void OnValidating (CancelEventArgs e)
1283                 {
1284                         base.OnValidating (e);
1285                 }
1286
1287                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1288                 protected override void OnTextChanged (EventArgs e)
1289                 {
1290                         base.OnTextChanged (e);
1291                 }
1292
1293 #if NET_2_0
1294                 protected virtual void OnTextUpdate (EventArgs e)
1295                 {
1296                         EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1297                         if (eh != null)
1298                                 eh (this, e);
1299                 }
1300 #endif
1301                 protected override void OnMouseLeave (EventArgs e)
1302                 {
1303 #if NET_2_0
1304                         if (flat_style == FlatStyle.Popup)
1305                                 Invalidate ();
1306 #endif
1307                         base.OnMouseLeave (e);
1308                 }
1309                 
1310                 protected override void OnMouseEnter (EventArgs e)
1311                 {
1312 #if NET_2_0
1313                         if (flat_style == FlatStyle.Popup)
1314                                 Invalidate ();
1315 #endif
1316                         base.OnMouseEnter (e);
1317                 }
1318 #endif
1319
1320 #if NET_2_0
1321                 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1322                 {
1323                         base.ScaleControl (factor, specified);
1324                 }
1325 #endif
1326
1327                 public void Select (int start, int length)
1328                 {
1329                         if (start < 0)
1330                                 throw new ArgumentException ("Start cannot be less than zero");
1331                                 
1332                         if (length < 0)
1333                                 throw new ArgumentException ("length cannot be less than zero");
1334                                 
1335                         if (dropdown_style == ComboBoxStyle.DropDownList)
1336                                 return;
1337
1338                         textbox_ctrl.Select (start, length);
1339                 }
1340
1341                 public void SelectAll ()
1342                 {
1343                         if (dropdown_style == ComboBoxStyle.DropDownList)
1344                                 return;
1345
1346                         if (textbox_ctrl != null) {
1347                                 textbox_ctrl.ShowSelection = true;
1348                                 textbox_ctrl.SelectAll ();
1349                         }
1350                 }
1351
1352                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1353                 {
1354                         bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1355                         bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1356
1357                         if ((specified & BoundsSpecified.Height) != 0 ||
1358                                 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1359
1360                                 requested_height = height;
1361                                 height = SnapHeight (height);
1362                         }
1363
1364                         base.SetBoundsCore (x, y, width, height, specified);
1365                 }
1366
1367                 protected override void SetItemCore (int index, object value)
1368                 {
1369                         if (index < 0 || index >= Items.Count)
1370                                 return;
1371
1372                         Items[index] = value;
1373                 }
1374
1375                 protected override void SetItemsCore (IList value)
1376                 {
1377                         BeginUpdate ();
1378                         try {
1379                                 Items.Clear ();
1380                                 Items.AddRange (value);
1381                         } finally {
1382                                 EndUpdate ();
1383                         }
1384                 }
1385
1386                 public override string ToString ()
1387                 {
1388                         return base.ToString () + ", Items.Count:" + Items.Count;
1389                 }
1390
1391                 protected override void WndProc (ref Message m)
1392                 {
1393                         switch ((Msg) m.Msg) {
1394                         case Msg.WM_KEYUP:
1395                         case Msg.WM_KEYDOWN:
1396                                 Keys keys = (Keys) m.WParam.ToInt32 ();
1397 #if NET_2_0
1398                                 // Don't pass the message to base if auto complete is being used and available.
1399                                 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1400                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1401                                         return;
1402                                 }
1403 #endif
1404                                 if (keys == Keys.Up || keys == Keys.Down)
1405                                         break;
1406                                 goto case Msg.WM_CHAR;
1407                         case Msg.WM_CHAR:
1408                                 if (textbox_ctrl != null)
1409                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1410                                 break;
1411                         case Msg.WM_MOUSELEAVE:
1412                                 Point location = PointToClient (Control.MousePosition);
1413                                 if (ClientRectangle.Contains (location))
1414                                         return;
1415                                 break;
1416                         default:
1417                                 break;
1418                         }
1419                         base.WndProc (ref m);
1420                 }
1421
1422                 #endregion Public Methods
1423
1424                 #region Private Methods
1425 #if NET_2_0
1426                 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1427                         if(auto_complete_source == AutoCompleteSource.CustomSource) {
1428                                 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1429                         }
1430                 }
1431 #endif
1432
1433                 internal override bool InternalCapture {
1434                         get { return Capture; }
1435                         set {}
1436                 }
1437                 
1438                 void LayoutComboBox ()
1439                 {
1440                         int border = ThemeEngine.Current.Border3DSize.Width;
1441
1442                         text_area = ClientRectangle;
1443                         text_area.Height = PreferredHeight;
1444                         
1445                         listbox_area = ClientRectangle;
1446                         listbox_area.Y = text_area.Bottom + 3;
1447                         listbox_area.Height -= (text_area.Height + 2);
1448
1449                         Rectangle prev_button_area = button_area;
1450
1451                         if (DropDownStyle == ComboBoxStyle.Simple)
1452                                 button_area = Rectangle.Empty;
1453                         else {
1454                                 button_area = text_area;
1455                                 button_area.X = text_area.Right - button_width - border;
1456                                 button_area.Y = text_area.Y + border;
1457                                 button_area.Width = button_width;
1458                                 button_area.Height = text_area.Height - 2 * border;
1459 #if NET_2_0
1460                                 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1461                                         button_area.Inflate (1, 1);
1462                                         button_area.X += 2;
1463                                         button_area.Width -= 2;
1464                                 }
1465 #endif
1466                         }
1467
1468                         if (button_area != prev_button_area) {
1469                                 prev_button_area.Y -= border;
1470                                 prev_button_area.Width += border;
1471                                 prev_button_area.Height += 2 * border;
1472                                 Invalidate (prev_button_area);
1473                                 Invalidate (button_area);
1474                         }
1475
1476                         if (textbox_ctrl != null) {
1477                                 int text_border = border + 1;
1478                                 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1479                                 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1480                                 textbox_ctrl.Height = text_area.Height - text_border * 2;
1481                         }
1482
1483                         if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1484                                 listbox_ctrl.Location = listbox_area.Location;
1485                                 listbox_ctrl.CalcListBoxArea ();
1486                         }
1487                 }
1488
1489                 private void CreateComboListBox ()
1490                 {
1491                         listbox_ctrl = new ComboListBox (this);
1492                         listbox_ctrl.HighlightedIndex = SelectedIndex;
1493                 }
1494                 
1495                 internal void Draw (Rectangle clip, Graphics dc)
1496                 {
1497                         Theme theme = ThemeEngine.Current;
1498                         FlatStyle style = FlatStyle.Standard;
1499                         bool is_flat = false;
1500
1501 #if NET_2_0
1502                         style = this.FlatStyle;
1503                         is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1504 #endif
1505
1506                         theme.ComboBoxDrawBackground (this, dc, clip, style);
1507
1508                         int border = theme.Border3DSize.Width;
1509
1510                         // No edit control, we paint the edit ourselves
1511                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1512                                 DrawItemState state = DrawItemState.None;
1513                                 Rectangle item_rect = text_area;
1514                                 item_rect.X += border;
1515                                 item_rect.Y += border;
1516                                 item_rect.Width -= (button_area.Width + 2 * border);
1517                                 item_rect.Height -= 2 * border;
1518                                 
1519                                 if (Focused) {
1520                                         state = DrawItemState.Selected;
1521                                         state |= DrawItemState.Focus;
1522                                 }
1523                                 
1524                                 state |= DrawItemState.ComboBoxEdit;
1525                                 HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1526                         }
1527                         
1528                         if (show_dropdown_button) {
1529                                 ButtonState current_state;
1530                                 if (is_enabled)
1531                                         current_state = button_state;
1532                                 else
1533                                         current_state = ButtonState.Inactive;
1534
1535                                 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1536                                         dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1537
1538                                 if (is_flat) {
1539                                         theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1540                                 } else {
1541                                         theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state); 
1542                                 }
1543                         }
1544                 }
1545
1546                 internal bool DropDownButtonEntered {
1547                         get { return drop_down_button_entered; }
1548                         private set {
1549                                 if (drop_down_button_entered == value)
1550                                         return;
1551                                 drop_down_button_entered = value;
1552                                 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1553                                         Invalidate (button_area);
1554                         }
1555                 }
1556
1557                 internal void DropDownListBox ()
1558                 {
1559                         DropDownButtonEntered = false;
1560
1561                         if (DropDownStyle == ComboBoxStyle.Simple)
1562                                 return;
1563                         
1564                         if (listbox_ctrl == null)
1565                                 CreateComboListBox ();
1566
1567                         listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1568
1569                         FindMatchOrSetIndex(SelectedIndex);
1570
1571 #if NET_2_0
1572                         if (textbox_ctrl != null)
1573                                 textbox_ctrl.HideAutoCompleteList ();
1574 #endif
1575
1576                         if (listbox_ctrl.ShowWindow ())
1577                                 dropped_down = true;
1578
1579                         button_state = ButtonState.Pushed;
1580                         if (dropdown_style == ComboBoxStyle.DropDownList)
1581                                 Invalidate (text_area);
1582                 }
1583                 
1584                 internal void DropDownListBoxFinished ()
1585                 {
1586                         if (DropDownStyle == ComboBoxStyle.Simple)
1587                                 return;
1588                                 
1589                         FindMatchOrSetIndex (SelectedIndex);
1590                         button_state = ButtonState.Normal;
1591                         Invalidate (button_area);
1592                         dropped_down = false;
1593 #if NET_2_0
1594                         OnDropDownClosed (EventArgs.Empty);
1595 #endif
1596                         /*
1597                          * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1598                          * this causes the popup to appear under the main form.  This is horrible but necessary
1599                          */
1600                          
1601                          // If the user opens a new form in an event, it will close our dropdown,
1602                          // so we need a null check here
1603                          if (listbox_ctrl != null) {
1604                                 listbox_ctrl.Dispose ();
1605                                 listbox_ctrl = null;
1606                         }
1607                 }
1608                 
1609                 private int FindStringCaseInsensitive (string search)
1610                 {
1611                         if (search.Length == 0) {
1612                                 return -1;
1613                         }
1614                         
1615                         for (int i = 0; i < Items.Count; i++) 
1616                         {
1617                                 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1618                                         return i;
1619                         }
1620
1621                         return -1;
1622                 }
1623
1624                 // Search in the list for the substring, starting the search at the list 
1625                 // position specified, the search wraps thus covering all the list.
1626                 internal int FindStringCaseInsensitive (string search, int start_index)
1627                 {
1628                         if (search.Length == 0) {
1629                                 return -1;
1630                         }
1631                         // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1632                         if (start_index < 0 || start_index > Items.Count)
1633                                 throw new ArgumentOutOfRangeException("start_index");
1634
1635                         for (int i = 0; i < Items.Count; i++) {
1636                                 int index = (i + start_index) % Items.Count;
1637                                 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1638                                         return index;
1639                         }
1640
1641                         return -1;
1642                 }
1643
1644                 internal override bool IsInputCharInternal (char charCode)
1645                 {
1646                         return true;
1647                 }
1648
1649                 internal override ContextMenu ContextMenuInternal {
1650                         get {
1651                                 return base.ContextMenuInternal;
1652                         }
1653                         set {
1654                                 base.ContextMenuInternal = value;
1655                                 if (textbox_ctrl != null) {
1656                                         textbox_ctrl.ContextMenu = value;
1657                                 }
1658                         }
1659                 }
1660
1661                 internal void RestoreContextMenu ()
1662                 {
1663                         textbox_ctrl.RestoreContextMenu ();
1664                 }
1665
1666                 private void OnKeyDownCB(object sender, KeyEventArgs e)
1667                 {
1668                         if (Items.Count == 0)
1669                                 return;
1670
1671                         int offset;
1672                         switch (e.KeyCode) 
1673                         {
1674                                 case Keys.Up:
1675                                         FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1676
1677                                         if (DroppedDown)
1678                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1679                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1680                                         break;
1681         
1682                                 case Keys.Down:
1683                                         if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1684                                                 DropDownListBox ();
1685                                         else
1686                                                 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1687                                                 
1688                                         if (DroppedDown)
1689                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1690                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1691                                         break;
1692                                 
1693                                 case Keys.PageUp:
1694                                         offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1695                                         if (offset < 1)
1696                                                 offset = 1;
1697
1698                                         SelectedIndex = Math.Max (SelectedIndex - offset, 0);
1699
1700                                         if (DroppedDown)
1701                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1702                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1703                                         break;
1704         
1705                                 case Keys.PageDown:
1706                                         if (SelectedIndex == -1) {
1707                                                 SelectedIndex = 0;
1708                                                 if (dropdown_style != ComboBoxStyle.Simple)
1709                                                         return;
1710                                         }
1711
1712                                         offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1713                                         if (offset < 1)
1714                                                 offset = 1;
1715
1716                                         SelectedIndex = Math.Min (SelectedIndex + offset, Items.Count - 1);
1717
1718                                         if (DroppedDown)
1719                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1720                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1721                                         break;
1722                                 
1723                                 case Keys.Enter:        
1724                                 case Keys.Escape:
1725                                         DropDownListBoxFinished ();
1726                                         break;
1727                                         
1728                                 case Keys.Home:
1729                                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1730                                                 SelectedIndex = 0;
1731
1732                                                 if (DroppedDown)
1733                                                         if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1734                                                                 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1735                                         }
1736                                         
1737                                         break;
1738                                 case Keys.End:
1739                                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1740                                                 SelectedIndex = Items.Count - 1;
1741
1742                                                 if (DroppedDown)
1743                                                         if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1744                                                                 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1745                                         }
1746                                         
1747                                         break;
1748                                 default:
1749                                         break;
1750                         }
1751                 }
1752
1753                 // If no item is currently selected, and an item is found matching the text 
1754                 // in the textbox, then selected that item.  Otherwise the item at the given 
1755                 // index is selected.
1756                 private void FindMatchOrSetIndex(int index)
1757                 {
1758                         int match = -1;
1759                         if (SelectedIndex == -1 && Text.Length != 0)
1760                                 match = FindStringCaseInsensitive(Text);
1761                         if (match != -1)
1762                                 SelectedIndex = match;
1763                         else
1764                                 SelectedIndex = index;
1765                 }
1766                 
1767                 void OnMouseDownCB (object sender, MouseEventArgs e)
1768                 {
1769                         Rectangle area;
1770                         if (DropDownStyle == ComboBoxStyle.DropDownList)
1771                                 area = ClientRectangle;
1772                         else
1773                                 area = button_area;
1774
1775                         if (area.Contains (e.X, e.Y)) {
1776                                 if (Items.Count > 0)
1777                                         DropDownListBox ();
1778                                 else {
1779                                         button_state = ButtonState.Pushed;
1780                                         OnDropDown (EventArgs.Empty);
1781                                 }
1782                                 
1783                                 Invalidate (button_area);
1784                                 Update ();
1785                         }
1786                         Capture = true;
1787                 }
1788
1789                 void OnMouseEnter (object sender, EventArgs e)
1790                 {
1791                         if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1792                                 Invalidate ();
1793                 }
1794
1795                 void OnMouseLeave (object sender, EventArgs e)
1796                 {
1797                         if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1798                                 drop_down_button_entered = false;
1799                                 Invalidate ();
1800                         } else {
1801                                 if (show_dropdown_button)
1802                                         DropDownButtonEntered = false;
1803                         }
1804                 }
1805
1806                 void OnMouseMoveCB (object sender, MouseEventArgs e)
1807                 {
1808                         if (show_dropdown_button && !dropped_down)
1809                                 DropDownButtonEntered = button_area.Contains (e.Location);
1810
1811                         if (DropDownStyle == ComboBoxStyle.Simple)
1812                                 return;
1813
1814                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1815                                 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1816                                 if (listbox_ctrl.ClientRectangle.Contains (location))
1817                                         listbox_ctrl.Capture = true;
1818                         }
1819                 }
1820
1821                 void OnMouseUpCB (object sender, MouseEventArgs e)
1822                 {
1823                         Capture = false;
1824                         
1825                         button_state = ButtonState.Normal;
1826                         Invalidate (button_area);
1827
1828                         OnClick (EventArgs.Empty);
1829
1830                         if (dropped_down)
1831                                 listbox_ctrl.Capture = true;
1832                 }
1833
1834                 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1835                 {
1836                         if (Items.Count == 0)
1837                                 return;
1838
1839                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1840                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1841                                 listbox_ctrl.Scroll (-lines);
1842                         } else {
1843                                 int lines = me.Delta / 120;
1844                                 int index = SelectedIndex - lines;
1845                                 if (index < 0)
1846                                         index = 0;
1847                                 else if (index >= Items.Count)
1848                                         index = Items.Count - 1;
1849                                 SelectedIndex = index;
1850                         }
1851                 }
1852
1853                 internal override void OnPaintInternal (PaintEventArgs pevent)
1854                 {
1855                         if (suspend_ctrlupdate)
1856                                 return;
1857
1858                         Draw (ClientRectangle, pevent.Graphics);
1859                 }
1860                 
1861                 private void OnTextBoxClick (object sender, EventArgs e)
1862                 {
1863                         OnClick (e);
1864                 }
1865
1866                 private void OnTextChangedEdit (object sender, EventArgs e)
1867                 {
1868                         if (process_textchanged_event == false)
1869                                 return; 
1870
1871                         int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1872                         
1873                         if (item == -1) {
1874                                 // Setting base.Text below will raise this event
1875                                 // if we found something
1876                                 OnTextChanged (EventArgs.Empty);
1877                                 return;
1878                         }
1879                         
1880                         // TODO:  THIS IS BROKEN-ISH
1881                         // I don't think we should hilight, and setting the top item does weirdness
1882                         // when there is no scrollbar
1883                         
1884                         if (listbox_ctrl != null) {
1885                                 listbox_ctrl.SetTopItem (item);
1886                                 listbox_ctrl.HighlightedIndex = item;
1887                         }
1888
1889                         base.Text = textbox_ctrl.Text;
1890                 }
1891
1892                 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1893                 {
1894                         selected_index = -1;
1895                 }
1896
1897                 internal void SetControlText (string s, bool suppressTextChanged)
1898                 {
1899                         if (suppressTextChanged)
1900                                 process_textchanged_event = false;
1901                                 
1902                         textbox_ctrl.Text = s;
1903                         textbox_ctrl.SelectAll ();
1904                         process_textchanged_event = true;
1905                 }
1906                 
1907                 void UpdateComboBoxBounds ()
1908                 {
1909                         if (requested_height == -1)
1910                                 return;
1911
1912                         // Save the requested height since set bounds can destroy it
1913                         int save_height = requested_height;
1914                         SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1915                                 BoundsSpecified.Height);
1916                         requested_height = save_height;
1917                 }
1918
1919                 int SnapHeight (int height)
1920                 {
1921                         if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1922                                 if (IntegralHeight) {
1923                                         int border = ThemeEngine.Current.Border3DSize.Height;
1924                                         int lb_height = (height - PreferredHeight - 2) - border * 2;
1925                                         if (lb_height > ItemHeight) {
1926                                                 int partial = (lb_height) % ItemHeight;
1927                                                 height -= partial;
1928                                         } else if (lb_height < ItemHeight)
1929                                                 height = PreferredHeight;
1930                                 }
1931                         } else
1932                                 height = PreferredHeight;
1933
1934                         return height;
1935                 }
1936
1937                 private void UpdatedItems ()
1938                 {
1939                         if (listbox_ctrl != null) {
1940                                 listbox_ctrl.UpdateLastVisibleItem ();
1941                                 listbox_ctrl.CalcListBoxArea ();
1942                                 listbox_ctrl.Refresh ();
1943                         }
1944                 }
1945
1946                 #endregion Private Methods
1947
1948                 [ListBindableAttribute (false)]
1949                 public class ObjectCollection : IList, ICollection, IEnumerable
1950                 {
1951
1952                         private ComboBox owner;
1953                         internal ArrayList object_items = new ArrayList ();
1954                         
1955                         #region UIA Framework Events
1956
1957 #if NET_2_0
1958                         //NOTE:
1959                         //      We are using Reflection to add/remove internal events.
1960                         //      Class ListProvider uses the events.
1961                         //
1962                         //Event used to generate UIA StructureChangedEvent
1963                         static object UIACollectionChangedEvent = new object ();
1964
1965                         internal event CollectionChangeEventHandler UIACollectionChanged {
1966                                 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1967                                 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1968                         }
1969                         
1970                         internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1971                         {
1972                                 CollectionChangeEventHandler eh
1973                                         = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1974                                 if (eh != null)
1975                                         eh (owner, args);
1976                         }
1977 #endif
1978
1979                         #endregion UIA Framework Events
1980
1981                         public ObjectCollection (ComboBox owner)
1982                         {
1983                                 this.owner = owner;
1984                         }
1985
1986                         #region Public Properties
1987                         public int Count {
1988                                 get { return object_items.Count; }
1989                         }
1990
1991                         public bool IsReadOnly {
1992                                 get { return false; }
1993                         }
1994
1995                         [Browsable (false)]
1996                         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1997                         public virtual object this [int index] {
1998                                 get {
1999                                         if (index < 0 || index >= Count)
2000                                                 throw new ArgumentOutOfRangeException ("index");
2001
2002                                         return object_items[index];
2003                                 }
2004                                 set {
2005                                         if (index < 0 || index >= Count)
2006                                                 throw new ArgumentOutOfRangeException ("index");
2007                                         if (value == null)
2008                                                 throw new ArgumentNullException ("value");
2009
2010 #if NET_2_0
2011                                         //UIA Framework event: Item Removed
2012                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2013 #endif
2014
2015                                         object_items[index] = value;
2016                                         
2017 #if NET_2_0
2018                                         //UIA Framework event: Item Added
2019                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2020 #endif
2021
2022                                         if (owner.listbox_ctrl != null)
2023                                                 owner.listbox_ctrl.InvalidateItem (index);
2024                                         if (index == owner.SelectedIndex) {
2025                                                 if (owner.textbox_ctrl == null)
2026                                                         owner.Refresh ();
2027                                                 else
2028                                                         owner.textbox_ctrl.SelectedText = value.ToString ();
2029                                         }
2030                                 }
2031                         }
2032
2033                         bool ICollection.IsSynchronized {
2034                                 get { return false; }
2035                         }
2036
2037                         object ICollection.SyncRoot {
2038                                 get { return this; }
2039                         }
2040
2041                         bool IList.IsFixedSize {
2042                                 get { return false; }
2043                         }
2044
2045                         #endregion Public Properties
2046                         
2047                         #region Public Methods
2048                         public int Add (object item)
2049                         {
2050                                 int idx;
2051
2052                                 idx = AddItem (item);
2053                                 owner.UpdatedItems ();
2054                                 return idx;
2055                         }
2056
2057                         public void AddRange (object[] items)
2058                         {
2059                                 if (items == null)
2060                                         throw new ArgumentNullException ("items");
2061
2062                                 foreach (object mi in items)
2063                                         AddItem (mi);
2064                                         
2065                                 owner.UpdatedItems ();
2066                         }
2067
2068                         public void Clear ()
2069                         {
2070                                 owner.selected_index = -1;
2071                                 object_items.Clear ();
2072                                 owner.UpdatedItems ();
2073                                 owner.Refresh ();
2074                                 
2075 #if NET_2_0
2076                                 //UIA Framework event: Items list cleared
2077                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2078 #endif
2079                         }
2080                         
2081                         public bool Contains (object value)
2082                         {
2083                                 if (value == null)
2084                                         throw new ArgumentNullException ("value");
2085
2086                                 return object_items.Contains (value);
2087                         }
2088
2089 #if NET_2_0
2090                         public void CopyTo (object [] destination, int arrayIndex)
2091                         {
2092                                 object_items.CopyTo (destination, arrayIndex);
2093                         }
2094
2095                         void ICollection.CopyTo (Array destination, int index)
2096                         {
2097                                 object_items.CopyTo (destination, index);
2098                         }
2099 #else
2100                         public void CopyTo (object [] dest, int arrayIndex)
2101                         {
2102                                 object_items.CopyTo (dest, arrayIndex);
2103                         }
2104
2105                         void ICollection.CopyTo (Array dest, int index)
2106                         {
2107                                 object_items.CopyTo (dest, index);
2108                         }
2109 #endif
2110
2111                         public IEnumerator GetEnumerator ()
2112                         {
2113                                 return object_items.GetEnumerator ();
2114                         }
2115
2116                         int IList.Add (object item)
2117                         {
2118                                 return Add (item);
2119                         }
2120
2121                         public int IndexOf (object value)
2122                         {
2123                                 if (value == null)
2124                                         throw new ArgumentNullException ("value");
2125
2126                                 return object_items.IndexOf (value);
2127                         }
2128
2129                         public void Insert (int index,  object item)
2130                         {
2131                                 if (index < 0 || index > Count)
2132                                         throw new ArgumentOutOfRangeException ("index");
2133                                 if (item == null)
2134                                         throw new ArgumentNullException ("item");
2135                                 
2136                                 owner.BeginUpdate ();
2137                                 
2138                                 if (owner.Sorted)
2139                                         AddItem (item);
2140                                 else {
2141                                         object_items.Insert (index, item);
2142 #if NET_2_0
2143                                         //UIA Framework event: Item added
2144                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2145 #endif                                  
2146                                 }
2147                                 
2148                                 owner.EndUpdate ();     // Calls UpdatedItems
2149                         }
2150
2151                         public void Remove (object value)
2152                         {
2153                                 if (value == null)
2154                                         return;
2155
2156                                 if (IndexOf (value) == owner.SelectedIndex)
2157                                         owner.SelectedIndex = -1;
2158                                 
2159                                 RemoveAt (IndexOf (value));
2160                         }
2161
2162                         public void RemoveAt (int index)
2163                         {
2164                                 if (index < 0 || index >= Count)
2165                                         throw new ArgumentOutOfRangeException ("index");
2166                                         
2167                                 if (index == owner.SelectedIndex)
2168                                         owner.SelectedIndex = -1;
2169
2170 #if NET_2_0
2171                                 object removed = object_items [index];
2172 #endif
2173
2174
2175                                 object_items.RemoveAt (index);
2176                                 owner.UpdatedItems ();
2177                                 
2178 #if NET_2_0
2179                                 //UIA Framework event: Item removed
2180                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2181 #endif
2182                         }
2183                         #endregion Public Methods
2184
2185                         #region Private Methods
2186                         private int AddItem (object item)
2187                         {
2188                                 if (item == null)
2189                                         throw new ArgumentNullException ("item");
2190
2191                                 if (owner.Sorted) {
2192                                         int index = 0;
2193                                         foreach (object o in object_items) {
2194                                                 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2195                                                         object_items.Insert (index, item);
2196                                                         
2197                                                         // If we added the new item before the selectedindex
2198                                                         // bump the selectedindex by one, behavior differs if
2199                                                         // Handle has not been created.
2200                                                         if (index <= owner.selected_index && owner.IsHandleCreated)
2201                                                                 owner.selected_index++;
2202                                                                 
2203 #if NET_2_0
2204                                                         //UIA Framework event: Item added
2205                                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2206 #endif
2207
2208                                                         return index;
2209                                                 }
2210                                                 index++;
2211                                         }
2212                                 }
2213                                 object_items.Add (item);
2214                                 
2215 #if NET_2_0
2216                                 //UIA Framework event: Item added
2217                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2218 #endif
2219                                 
2220                                 return object_items.Count - 1;
2221                         }
2222                         
2223                         internal void AddRange (IList items)
2224                         {
2225                                 foreach (object mi in items)
2226                                         AddItem (mi);
2227                                 
2228                                 owner.UpdatedItems ();
2229                         }
2230
2231                         internal void Sort ()
2232                         {
2233                                 // If the objects the user put here don't have their own comparer,
2234                                 // use one that compares based on the object's ToString
2235                                 if (object_items.Count > 0 && object_items[0] is IComparer)
2236                                         object_items.Sort ();
2237                                 else
2238                                         object_items.Sort (new ObjectComparer (owner));
2239                         }
2240
2241                         private class ObjectComparer : IComparer
2242                         {
2243                                 private ListControl owner;
2244                                 
2245                                 public ObjectComparer (ListControl owner)
2246                                 {
2247                                         this.owner = owner;
2248                                 }
2249                                 
2250                                 #region IComparer Members
2251                                 public int Compare (object x, object y)
2252                                 {
2253                                         return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2254                                 }
2255                                 #endregion
2256                         }
2257                         #endregion Private Methods
2258                 }
2259
2260                 internal class ComboTextBox : TextBox {
2261
2262                         private ComboBox owner;
2263
2264                         public ComboTextBox (ComboBox owner)
2265                         {
2266                                 this.owner = owner;
2267                                 ShowSelection = false;
2268                                 HideSelection = false;
2269                         }
2270
2271                         internal void SetSelectable (bool selectable)
2272                         {
2273                                 SetStyle (ControlStyles.Selectable, selectable);
2274                         }
2275
2276                         internal void ActivateCaret (bool active)
2277                         {
2278                                 if (active)
2279                                         document.CaretHasFocus ();
2280                                 else
2281                                         document.CaretLostFocus ();
2282                         }
2283                         
2284 #if NET_2_0
2285                         internal override void OnTextUpdate ()
2286                         {
2287                                 base.OnTextUpdate ();
2288                                 owner.OnTextUpdate (EventArgs.Empty);
2289                         }
2290 #endif
2291                         
2292                         protected override void OnGotFocus (EventArgs e)
2293                         {
2294                                 owner.Select (false, true);
2295                         }
2296
2297                         protected override void OnLostFocus (EventArgs e)
2298                         {
2299                                 owner.Select (false, true);
2300                         }
2301
2302                         public override bool Focused {
2303                                 get {
2304                                         return owner.Focused;
2305                                 }
2306                         }
2307                         
2308                         internal override bool ActivateOnShow { get { return false; } }
2309                 }
2310
2311                 internal class ComboListBox : Control
2312                 {
2313                         private ComboBox owner;
2314                         private VScrollBarLB vscrollbar_ctrl;
2315                         private int top_item;                   /* First item that we show the in the current page */
2316                         private int last_item;                  /* Last visible item */
2317                         internal int page_size;                 /* Number of listbox items per page */
2318                         private Rectangle textarea_drawable;    /* Rectangle of the drawable text area */
2319                         
2320                         internal enum ItemNavigation
2321                         {
2322                                 First,
2323                                 Last,
2324                                 Next,
2325                                 Previous,
2326                                 NextPage,
2327                                 PreviousPage,
2328                         }
2329                         
2330                         class VScrollBarLB : VScrollBar
2331                         {
2332                                 public VScrollBarLB ()
2333                                 {
2334                                 }
2335                                 
2336                                 internal override bool InternalCapture {
2337                                         get { return Capture; }
2338                                         set { }
2339                                 }
2340
2341                                 public void FireMouseDown (MouseEventArgs e) 
2342                                 {
2343                                         if (!Visible) 
2344                                                 return;
2345
2346                                         e = TranslateEvent (e);
2347                                         OnMouseDown (e);
2348                                 }
2349                                 
2350                                 public void FireMouseUp (MouseEventArgs e) 
2351                                 {
2352                                         if (!Visible)
2353                                                 return;
2354
2355                                         e = TranslateEvent (e);
2356                                         OnMouseUp (e);
2357                                 }
2358                                 
2359                                 public void FireMouseMove (MouseEventArgs e) 
2360                                 {
2361                                         if (!Visible)
2362                                                 return;
2363
2364                                         e = TranslateEvent (e);
2365                                         OnMouseMove (e);
2366                                 }
2367                                 
2368                                 MouseEventArgs TranslateEvent (MouseEventArgs e)
2369                                 {
2370                                         Point loc = PointToClient (Control.MousePosition);
2371                                         return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2372                                 }
2373                         }
2374
2375                         public ComboListBox (ComboBox owner)
2376                         {
2377                                 this.owner = owner;
2378                                 top_item = 0;
2379                                 last_item = 0;
2380                                 page_size = 0;
2381
2382                                 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2383
2384                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2385                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2386
2387                                 this.is_visible = false;
2388
2389                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2390                                         InternalBorderStyle = BorderStyle.Fixed3D;
2391                                 else
2392                                         InternalBorderStyle = BorderStyle.FixedSingle;
2393                         }
2394
2395                         protected override CreateParams CreateParams
2396                         {
2397                                 get {
2398                                         CreateParams cp = base.CreateParams;
2399                                         if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2400                                                 return cp;
2401
2402                                         cp.Style ^= (int)WindowStyles.WS_CHILD;
2403                                         cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2404                                         cp.Style |= (int)WindowStyles.WS_POPUP;
2405                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2406                                         return cp;
2407                                 }
2408                         }
2409
2410                         internal override bool InternalCapture {
2411                                 get {
2412                                         return Capture;
2413                                 }
2414
2415                                 set {
2416                                 }
2417                         }
2418
2419                         internal override bool ActivateOnShow { get { return false; } }
2420                         #region Private Methods
2421
2422                         // Calcs the listbox area
2423                         internal void CalcListBoxArea ()
2424                         {
2425                                 int width, height;
2426                                 bool show_scrollbar = false;
2427
2428                                 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2429                                         Rectangle area = owner.listbox_area;
2430                                         width = area.Width;
2431                                         height = area.Height;
2432
2433                                         // No calculation needed
2434                                         if (height <= 0 || width <= 0)
2435                                                 return;
2436                                 }
2437                                 else { // DropDown or DropDownList
2438                                         
2439                                         width = owner.DropDownWidth;
2440                                         int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2441                                         
2442                                         if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2443                                                 height = 0;
2444                                                 for (int i = 0; i < count; i++) {
2445                                                         height += owner.GetItemHeight (i);
2446                                                 }
2447                                                 
2448                                         } else  {
2449 #if NET_2_0
2450                                                 height = owner.DropDownHeight;
2451 #else           
2452                                                 height = owner.ItemHeight * count;
2453 #endif
2454                                         }
2455                                 }
2456                                 
2457                                 page_size = Math.Max (height / owner.ItemHeight, 1);
2458
2459                                 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2460                                 if ((dropdown_style != ComboBoxStyle.Simple && owner.Items.Count <= owner.MaxDropDownItems)
2461                                         || (dropdown_style == ComboBoxStyle.Simple && owner.Items.Count * owner.ItemHeight < height)) {
2462
2463                                         if (vscrollbar_ctrl != null)
2464                                                 vscrollbar_ctrl.Visible = false;
2465                                         if (dropdown_style != ComboBoxStyle.Simple)
2466                                                 height = owner.ItemHeight * owner.items.Count;
2467                                 } else {
2468                                         /* Need vertical scrollbar */
2469                                         if (vscrollbar_ctrl == null) {
2470                                                 vscrollbar_ctrl = new VScrollBarLB ();
2471                                                 vscrollbar_ctrl.Minimum = 0;
2472                                                 vscrollbar_ctrl.SmallChange = 1;
2473                                                 vscrollbar_ctrl.LargeChange = 1;
2474                                                 vscrollbar_ctrl.Maximum = 0;
2475                                                 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2476                                                 Controls.AddImplicit (vscrollbar_ctrl);
2477                                         }
2478                                         
2479                                         vscrollbar_ctrl.Dock = DockStyle.Right;
2480
2481                                         vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2482 #if NET_2_0
2483                                         int large = page_size;
2484 #else
2485                                         int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2486 #endif
2487                                         if (large < 1)
2488                                                 large = 1;
2489                                         vscrollbar_ctrl.LargeChange = large;
2490                                         show_scrollbar = vscrollbar_ctrl.Visible = true;
2491
2492                                         int hli = HighlightedIndex;
2493                                         if (hli > 0) {
2494                                                 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2495                                                 vscrollbar_ctrl.Value = hli;
2496                                         }
2497                                 }
2498                                 
2499                                 Size = new Size (width, height);
2500                                 textarea_drawable = ClientRectangle;
2501                                 textarea_drawable.Width = width;
2502                                 textarea_drawable.Height = height;
2503                                 
2504                                 if (vscrollbar_ctrl != null && show_scrollbar)
2505                                         textarea_drawable.Width -= vscrollbar_ctrl.Width;
2506
2507                                 last_item = LastVisibleItem ();
2508                         }
2509
2510                         private void Draw (Rectangle clip, Graphics dc)
2511                         {
2512                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2513
2514                                 if (owner.Items.Count > 0) {
2515                                         
2516                                         for (int i = top_item; i <= last_item; i++) {
2517                                                 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2518
2519                                                 if (!clip.IntersectsWith (item_rect))
2520                                                         continue;
2521
2522                                                 DrawItemState state = DrawItemState.None;
2523
2524                                                 if (i == HighlightedIndex) {
2525                                                         state |= DrawItemState.Selected;
2526                                                         
2527                                                         if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2528                                                                 state |= DrawItemState.Focus;
2529                                                         }
2530                                                 }
2531
2532                                                 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2533                                                         i, state, owner.ForeColor, owner.BackColor));
2534                                         }
2535                                 }
2536                         }
2537
2538                         int highlighted_index = -1;
2539
2540                         public int HighlightedIndex {
2541                                 get { return highlighted_index; }
2542                                 set { 
2543                                         if (highlighted_index == value)
2544                                                 return;
2545
2546                                         if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2547                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2548                                         highlighted_index = value;
2549                                         if (highlighted_index != -1)
2550                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2551                                 }
2552                         }
2553
2554                         private Rectangle GetItemDisplayRectangle (int index, int top_index)
2555                         {
2556                                 if (index < 0 || index >= owner.Items.Count)
2557                                         throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2558
2559                                 Rectangle item_rect = new Rectangle ();
2560                                 int height = owner.GetItemHeight (index);
2561
2562                                 item_rect.X = 0;
2563                                 item_rect.Width = textarea_drawable.Width;
2564                                 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2565                                         item_rect.Y = 0;
2566                                         for (int i = top_index; i < index; i++)
2567                                                 item_rect.Y += owner.GetItemHeight (i);
2568                                 } else
2569                                         item_rect.Y = height * (index - top_index);
2570
2571                                 item_rect.Height = height;
2572                                 return item_rect;
2573                         }
2574
2575                         public void HideWindow ()
2576                         {
2577                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2578                                         return;
2579                                         
2580                                 Capture = false;
2581                                 Hide ();
2582                                 owner.DropDownListBoxFinished ();
2583                         }
2584
2585                         private int IndexFromPointDisplayRectangle (int x, int y)
2586                         {
2587                                 for (int i = top_item; i <= last_item; i++) {
2588                                         if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2589                                                 return i;
2590                                 }
2591
2592                                 return -1;
2593                         }
2594
2595                         public void InvalidateItem (int index)
2596                         {
2597                                 if (Visible)
2598                                         Invalidate (GetItemDisplayRectangle (index, top_item));
2599                         }
2600
2601                         public int LastVisibleItem ()
2602                         {
2603                                 Rectangle item_rect;
2604                                 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2605                                 int i = 0;
2606                                 
2607                                 for (i = top_item; i < owner.Items.Count; i++) {
2608                                         item_rect = GetItemDisplayRectangle (i, top_item);
2609                                         if (item_rect.Y + item_rect.Height > top_y) {
2610                                                 return i;
2611                                         }
2612                                 }
2613                                 return i - 1;
2614                         }
2615
2616                         public void SetTopItem (int item)
2617                         {
2618                                 if (top_item == item)
2619                                         return;
2620                                 top_item = item;
2621                                 UpdateLastVisibleItem ();
2622                                 Invalidate ();
2623                         }
2624
2625                         public int FirstVisibleItem ()
2626                         {
2627                                 return top_item;
2628                         }
2629                         
2630                         bool scrollbar_grabbed = false;
2631
2632                         bool InScrollBar {
2633                                 get {
2634                                         if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2635                                                 return false;
2636
2637                                         return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2638                                 }
2639                         }
2640
2641                         protected override void OnMouseDown (MouseEventArgs e)
2642                         {
2643                                 if (InScrollBar) {
2644                                         vscrollbar_ctrl.FireMouseDown (e);
2645                                         scrollbar_grabbed = true;
2646                                 }
2647                         }
2648
2649                         protected override void OnMouseMove (MouseEventArgs e)
2650                         {
2651                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2652                                         return;
2653
2654                                 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2655                                         vscrollbar_ctrl.FireMouseMove (e);
2656                                         return;
2657                                 }
2658
2659                                 Point pt = PointToClient (Control.MousePosition);
2660                                 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2661
2662                                 if (index != -1)
2663                                         HighlightedIndex = index;
2664                         }
2665                         
2666                         protected override void OnMouseUp (MouseEventArgs e)
2667                         {
2668                                 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2669
2670                                 if (scrollbar_grabbed) {
2671                                         vscrollbar_ctrl.FireMouseUp (e);
2672                                         scrollbar_grabbed = false;
2673                                         if (index != -1)
2674                                                 HighlightedIndex = index;
2675                                         return;
2676                                 }
2677
2678                                 if (index == -1) {
2679                                         HideWindow ();
2680                                         return;
2681                                 }
2682
2683                                 bool is_change = owner.SelectedIndex != index;
2684                                 
2685                                 owner.SelectedIndex = index;
2686                                 owner.OnSelectionChangeCommitted (new EventArgs ());
2687                                 
2688                                 // If the user selected the already selected item, SelectedIndex
2689                                 // won't fire these events, but .Net does, so we do it here
2690                                 if (!is_change) {
2691                                         owner.OnSelectedValueChanged (EventArgs.Empty);
2692                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
2693                                 }
2694                                 
2695                                 HideWindow ();
2696                         }
2697
2698                         internal override void OnPaintInternal (PaintEventArgs pevent)
2699                         {
2700                                 Draw (pevent.ClipRectangle,pevent.Graphics);
2701                         }
2702
2703                         public bool ShowWindow ()
2704                         {
2705                                 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2706                                         return false;
2707
2708                                 HighlightedIndex = owner.SelectedIndex;
2709
2710                                 CalcListBoxArea ();
2711                                 Show ();
2712
2713                                 Refresh ();
2714                                 owner.OnDropDown (EventArgs.Empty);
2715                                 return true;
2716                         }
2717                         
2718                         public void UpdateLastVisibleItem ()
2719                         {
2720                                 last_item = LastVisibleItem ();
2721                         }
2722
2723                         public void Scroll (int delta)
2724                         {
2725                                 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2726                                         return;
2727
2728                                 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2729
2730                                 int val = vscrollbar_ctrl.Value + delta;
2731                                 if (val > max)
2732                                         val = max;
2733                                 else if (val < vscrollbar_ctrl.Minimum)
2734                                         val = vscrollbar_ctrl.Minimum;
2735                                 vscrollbar_ctrl.Value = val;
2736                         }
2737
2738                         private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2739                         {
2740                                 if (owner.Items.Count == 0)
2741                                         return;
2742
2743                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2744                                 Scroll (-lines);
2745                         }
2746
2747                         // Value Changed
2748                         private void VerticalScrollEvent (object sender, EventArgs e)
2749                         {
2750                                 if (top_item == vscrollbar_ctrl.Value)
2751                                         return;
2752
2753                                 top_item =  vscrollbar_ctrl.Value;
2754                                 UpdateLastVisibleItem ();
2755                                 Invalidate ();
2756                         }
2757                         
2758                         protected override void WndProc(ref Message m) {
2759                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2760                                         owner.Select (false, true);
2761                                 }
2762                                 base.WndProc (ref m);
2763                         }
2764
2765                         #endregion Private Methods
2766                 }
2767         }
2768 }