* Makefile.am: Build `docs` after `runtime`, so that it can depend
[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.Hide ();
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                         switch (DrawMode) {
1055                         case DrawMode.OwnerDrawFixed:
1056                         case DrawMode.OwnerDrawVariable:
1057                                 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1058                                 if (eh != null)
1059                                         eh (this, e);
1060                                 break;
1061                         default:
1062                                 ThemeEngine.Current.DrawComboBoxItem (this, e);
1063                                 break;
1064                         }
1065                 }
1066
1067                 protected virtual void OnDropDown (EventArgs e)
1068                 {
1069                         EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1070                         if (eh != null)
1071                                 eh (this, e);
1072                 }
1073
1074 #if NET_2_0
1075                 protected virtual void OnDropDownClosed (EventArgs e)
1076                 {
1077                         EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1078                         if (eh != null)
1079                                 eh (this, e);
1080                 }
1081 #endif
1082
1083                 protected virtual void OnDropDownStyleChanged (EventArgs e)
1084                 {
1085                         EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1086                         if (eh != null)
1087                                 eh (this, e);
1088                 }
1089
1090                 protected override void OnFontChanged (EventArgs e)
1091                 {
1092                         base.OnFontChanged (e);
1093
1094                         if (textbox_ctrl != null)
1095                                 textbox_ctrl.Font = Font;
1096                         
1097                         if (!item_height_specified)
1098                                 item_height = Font.Height + 2;
1099
1100                         if (IntegralHeight)
1101                                 UpdateComboBoxBounds ();
1102
1103                         LayoutComboBox ();
1104                 }
1105
1106                 protected override void OnForeColorChanged (EventArgs e)
1107                 {
1108                         base.OnForeColorChanged (e);
1109                         if (textbox_ctrl != null)
1110                                 textbox_ctrl.ForeColor = ForeColor;
1111                 }
1112
1113                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1114                 protected override void OnGotFocus (EventArgs e)
1115                 {
1116                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1117                                 // We draw DDL styles manually, so they require a
1118                                 // refresh to have their selection drawn
1119                                 Invalidate ();
1120                         }
1121                         
1122                         if (textbox_ctrl != null) {
1123                                 textbox_ctrl.SetSelectable (false);
1124                                 textbox_ctrl.ShowSelection = true;
1125                                 textbox_ctrl.ActivateCaret (true);
1126                                 textbox_ctrl.SelectAll ();
1127                         }
1128
1129                         base.OnGotFocus (e);
1130                 }
1131
1132                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1133                 protected override void OnLostFocus (EventArgs e)
1134                 {
1135                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1136                                 // We draw DDL styles manually, so they require a
1137                                 // refresh to have their selection drawn
1138                                 Invalidate ();
1139                         }
1140
1141                         if (listbox_ctrl != null && dropped_down) {
1142                                 listbox_ctrl.HideWindow ();
1143                         }
1144
1145                         if (textbox_ctrl != null) {
1146                                 textbox_ctrl.SetSelectable (true);
1147                                 textbox_ctrl.ActivateCaret (false);
1148                                 textbox_ctrl.ShowSelection = false;
1149                                 textbox_ctrl.SelectionLength = 0;
1150                         }
1151
1152                         base.OnLostFocus (e);
1153                 }
1154
1155                 protected override void OnHandleCreated (EventArgs e)
1156                 {
1157                         base.OnHandleCreated (e);
1158
1159                         SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1160
1161                         if (textbox_ctrl != null)
1162                                 Controls.AddImplicit (textbox_ctrl);
1163
1164                         LayoutComboBox ();
1165                         UpdateComboBoxBounds ();
1166                 }
1167
1168                 protected override void OnHandleDestroyed (EventArgs e)
1169                 {
1170                         base.OnHandleDestroyed (e);
1171                 }
1172
1173                 protected override void OnKeyPress (KeyPressEventArgs e)
1174                 {
1175                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1176                                 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1177                                 if (index != -1) {
1178                                         SelectedIndex = index;
1179                                         if (DroppedDown) { //Scroll into view
1180                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1181                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1182                                                 // Or, selecting an item earlier in the list.
1183                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1184                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1185                                         }
1186                                 }
1187                         }
1188
1189                         base.OnKeyPress (e);
1190                 }
1191
1192                 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1193                 {
1194                         MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1195                         if (eh != null)
1196                                 eh (this, e);
1197                 }
1198
1199                 protected override void OnParentBackColorChanged (EventArgs e)
1200                 {
1201                         base.OnParentBackColorChanged (e);
1202                 }
1203
1204                 protected override void OnResize (EventArgs e)
1205                 {
1206                         LayoutComboBox ();
1207                         if (listbox_ctrl != null)
1208                                 listbox_ctrl.CalcListBoxArea ();
1209                 }
1210
1211                 protected override void OnSelectedIndexChanged (EventArgs e)
1212                 {
1213                         base.OnSelectedIndexChanged (e);
1214
1215                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1216                         if (eh != null)
1217                                 eh (this, e);
1218                 }
1219
1220                 protected virtual void OnSelectedItemChanged (EventArgs e)
1221                 {
1222                 }
1223
1224                 protected override void OnSelectedValueChanged (EventArgs e)
1225                 {
1226                         base.OnSelectedValueChanged (e);
1227                 }
1228
1229                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1230                 {
1231                         EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1232                         if (eh != null)
1233                                 eh (this, e);
1234                 }
1235
1236                 protected override void RefreshItem (int index)
1237                 {
1238                         if (index < 0 || index >= Items.Count)
1239                                 throw new ArgumentOutOfRangeException ("index");
1240                                 
1241                         if (draw_mode == DrawMode.OwnerDrawVariable)
1242                                 item_heights.Remove (Items [index]);
1243                 }
1244
1245 #if NET_2_0
1246                 protected override void RefreshItems ()
1247                 {
1248                         for (int i = 0; i < Items.Count; i++) {
1249                                 RefreshItem (i);
1250                         }
1251
1252                         LayoutComboBox ();
1253                         Refresh ();
1254
1255                         if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1256                                 SetControlText (GetItemText (Items [selected_index]), false);
1257                 }
1258
1259                 public override void ResetText ()
1260                 {
1261                         Text = String.Empty;
1262                 }
1263                 
1264                 protected override bool ProcessKeyEventArgs (ref Message m)
1265                 {
1266                         return base.ProcessKeyEventArgs (ref m);
1267                 }
1268
1269                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1270                 protected override void OnKeyDown (KeyEventArgs e)
1271                 {
1272                         base.OnKeyDown (e);
1273                 }
1274
1275                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1276                 protected override void OnValidating (CancelEventArgs e)
1277                 {
1278                         base.OnValidating (e);
1279                 }
1280
1281                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1282                 protected override void OnTextChanged (EventArgs e)
1283                 {
1284                         base.OnTextChanged (e);
1285                 }
1286
1287 #if NET_2_0
1288                 protected virtual void OnTextUpdate (EventArgs e)
1289                 {
1290                         EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1291                         if (eh != null)
1292                                 eh (this, e);
1293                 }
1294 #endif
1295                 protected override void OnMouseLeave (EventArgs e)
1296                 {
1297 #if NET_2_0
1298                         if (flat_style == FlatStyle.Popup)
1299                                 Invalidate ();
1300 #endif
1301                         base.OnMouseLeave (e);
1302                 }
1303                 
1304                 protected override void OnMouseEnter (EventArgs e)
1305                 {
1306 #if NET_2_0
1307                         if (flat_style == FlatStyle.Popup)
1308                                 Invalidate ();
1309 #endif
1310                         base.OnMouseEnter (e);
1311                 }
1312 #endif
1313
1314 #if NET_2_0
1315                 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1316                 {
1317                         base.ScaleControl (factor, specified);
1318                 }
1319 #endif
1320
1321                 public void Select (int start, int length)
1322                 {
1323                         if (start < 0)
1324                                 throw new ArgumentException ("Start cannot be less than zero");
1325                                 
1326                         if (length < 0)
1327                                 throw new ArgumentException ("length cannot be less than zero");
1328                                 
1329                         if (dropdown_style == ComboBoxStyle.DropDownList)
1330                                 return;
1331
1332                         textbox_ctrl.Select (start, length);
1333                 }
1334
1335                 public void SelectAll ()
1336                 {
1337                         if (dropdown_style == ComboBoxStyle.DropDownList)
1338                                 return;
1339
1340                         if (textbox_ctrl != null) {
1341                                 textbox_ctrl.ShowSelection = true;
1342                                 textbox_ctrl.SelectAll ();
1343                         }
1344                 }
1345
1346                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1347                 {
1348                         bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1349                         bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1350
1351                         if ((specified & BoundsSpecified.Height) != 0 ||
1352                                 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1353
1354                                 requested_height = height;
1355                                 height = SnapHeight (height);
1356                         }
1357
1358                         base.SetBoundsCore (x, y, width, height, specified);
1359                 }
1360
1361                 protected override void SetItemCore (int index, object value)
1362                 {
1363                         if (index < 0 || index >= Items.Count)
1364                                 return;
1365
1366                         Items[index] = value;
1367                 }
1368
1369                 protected override void SetItemsCore (IList value)
1370                 {
1371                         BeginUpdate ();
1372                         try {
1373                                 Items.Clear ();
1374                                 Items.AddRange (value);
1375                         } finally {
1376                                 EndUpdate ();
1377                         }
1378                 }
1379
1380                 public override string ToString ()
1381                 {
1382                         return base.ToString () + ", Items.Count:" + Items.Count;
1383                 }
1384
1385                 protected override void WndProc (ref Message m)
1386                 {
1387                         switch ((Msg) m.Msg) {
1388                         case Msg.WM_KEYUP:
1389                         case Msg.WM_KEYDOWN:
1390                                 Keys keys = (Keys) m.WParam.ToInt32 ();
1391 #if NET_2_0
1392                                 // Don't pass the message to base if auto complete is being used and available.
1393                                 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1394                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1395                                         return;
1396                                 }
1397 #endif
1398                                 if (keys == Keys.Up || keys == Keys.Down)
1399                                         break;
1400                                 goto case Msg.WM_CHAR;
1401                         case Msg.WM_CHAR:
1402                                 if (textbox_ctrl != null)
1403                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1404                                 break;
1405                         case Msg.WM_MOUSELEAVE:
1406                                 Point location = PointToClient (Control.MousePosition);
1407                                 if (ClientRectangle.Contains (location))
1408                                         return;
1409                                 break;
1410                         default:
1411                                 break;
1412                         }
1413                         base.WndProc (ref m);
1414                 }
1415
1416                 #endregion Public Methods
1417
1418                 #region Private Methods
1419 #if NET_2_0
1420                 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1421                         if(auto_complete_source == AutoCompleteSource.CustomSource) {
1422                                 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1423                         }
1424                 }
1425 #endif
1426
1427                 internal override bool InternalCapture {
1428                         get { return Capture; }
1429                         set {}
1430                 }
1431                 
1432                 void LayoutComboBox ()
1433                 {
1434                         int border = ThemeEngine.Current.Border3DSize.Width;
1435
1436                         text_area = ClientRectangle;
1437                         text_area.Height = PreferredHeight;
1438                         
1439                         listbox_area = ClientRectangle;
1440                         listbox_area.Y = text_area.Bottom + 3;
1441                         listbox_area.Height -= (text_area.Height + 2);
1442
1443                         Rectangle prev_button_area = button_area;
1444
1445                         if (DropDownStyle == ComboBoxStyle.Simple)
1446                                 button_area = Rectangle.Empty;
1447                         else {
1448                                 button_area = text_area;
1449                                 button_area.X = text_area.Right - button_width - border;
1450                                 button_area.Y = text_area.Y + border;
1451                                 button_area.Width = button_width;
1452                                 button_area.Height = text_area.Height - 2 * border;
1453 #if NET_2_0
1454                                 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1455                                         button_area.Inflate (1, 1);
1456                                         button_area.X += 2;
1457                                         button_area.Width -= 2;
1458                                 }
1459 #endif
1460                         }
1461
1462                         if (button_area != prev_button_area) {
1463                                 prev_button_area.Y -= border;
1464                                 prev_button_area.Width += border;
1465                                 prev_button_area.Height += 2 * border;
1466                                 Invalidate (prev_button_area);
1467                                 Invalidate (button_area);
1468                         }
1469
1470                         if (textbox_ctrl != null) {
1471                                 int text_border = border + 1;
1472                                 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1473                                 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1474                                 textbox_ctrl.Height = text_area.Height - text_border * 2;
1475                         }
1476
1477                         if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1478                                 listbox_ctrl.Location = listbox_area.Location;
1479                                 listbox_ctrl.CalcListBoxArea ();
1480                         }
1481                 }
1482
1483                 private void CreateComboListBox ()
1484                 {
1485                         listbox_ctrl = new ComboListBox (this);
1486                         listbox_ctrl.HighlightedIndex = SelectedIndex;
1487                 }
1488                 
1489                 internal void Draw (Rectangle clip, Graphics dc)
1490                 {
1491                         Theme theme = ThemeEngine.Current;
1492                         FlatStyle style = FlatStyle.Standard;
1493                         bool is_flat = false;
1494
1495 #if NET_2_0
1496                         style = this.FlatStyle;
1497                         is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1498 #endif
1499
1500                         theme.ComboBoxDrawBackground (this, dc, clip, style);
1501
1502                         int border = theme.Border3DSize.Width;
1503
1504                         // No edit control, we paint the edit ourselves
1505                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1506                                 DrawItemState state = DrawItemState.None;
1507                                 Rectangle item_rect = text_area;
1508                                 item_rect.X += border;
1509                                 item_rect.Y += border;
1510                                 item_rect.Width -= (button_area.Width + 2 * border);
1511                                 item_rect.Height -= 2 * border;
1512                                 
1513                                 if (Focused) {
1514                                         state = DrawItemState.Selected;
1515                                         state |= DrawItemState.Focus;
1516                                 }
1517                                 
1518                                 state |= DrawItemState.ComboBoxEdit;
1519                                 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1520                         }
1521                         
1522                         if (show_dropdown_button) {
1523                                 ButtonState current_state;
1524                                 if (is_enabled)
1525                                         current_state = button_state;
1526                                 else
1527                                         current_state = ButtonState.Inactive;
1528
1529                                 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1530                                         dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1531
1532                                 if (is_flat) {
1533                                         theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1534                                 } else {
1535                                         theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state); 
1536                                 }
1537                         }
1538                 }
1539
1540                 internal bool DropDownButtonEntered {
1541                         get { return drop_down_button_entered; }
1542                         private set {
1543                                 if (drop_down_button_entered == value)
1544                                         return;
1545                                 drop_down_button_entered = value;
1546                                 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1547                                         Invalidate (button_area);
1548                         }
1549                 }
1550
1551                 internal void DropDownListBox ()
1552                 {
1553                         DropDownButtonEntered = false;
1554
1555                         if (DropDownStyle == ComboBoxStyle.Simple)
1556                                 return;
1557                         
1558                         if (listbox_ctrl == null)
1559                                 CreateComboListBox ();
1560
1561                         listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1562
1563                         FindMatchOrSetIndex(SelectedIndex);
1564
1565 #if NET_2_0
1566                         if (textbox_ctrl != null)
1567                                 textbox_ctrl.HideAutoCompleteList ();
1568 #endif
1569
1570                         if (listbox_ctrl.ShowWindow ())
1571                                 dropped_down = true;
1572
1573                         button_state = ButtonState.Pushed;
1574                         if (dropdown_style == ComboBoxStyle.DropDownList)
1575                                 Invalidate (text_area);
1576                 }
1577                 
1578                 internal void DropDownListBoxFinished ()
1579                 {
1580                         if (DropDownStyle == ComboBoxStyle.Simple)
1581                                 return;
1582                                 
1583                         FindMatchOrSetIndex (SelectedIndex);
1584                         button_state = ButtonState.Normal;
1585                         Invalidate (button_area);
1586                         dropped_down = false;
1587 #if NET_2_0
1588                         OnDropDownClosed (EventArgs.Empty);
1589 #endif
1590                         /*
1591                          * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1592                          * this causes the popup to appear under the main form.  This is horrible but necessary
1593                          */
1594                          
1595                          // If the user opens a new form in an event, it will close our dropdown,
1596                          // so we need a null check here
1597                          if (listbox_ctrl != null) {
1598                                 listbox_ctrl.Dispose ();
1599                                 listbox_ctrl = null;
1600                         }
1601                 }
1602                 
1603                 private int FindStringCaseInsensitive (string search)
1604                 {
1605                         if (search.Length == 0) {
1606                                 return -1;
1607                         }
1608                         
1609                         for (int i = 0; i < Items.Count; i++) 
1610                         {
1611                                 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1612                                         return i;
1613                         }
1614
1615                         return -1;
1616                 }
1617
1618                 // Search in the list for the substring, starting the search at the list 
1619                 // position specified, the search wraps thus covering all the list.
1620                 internal int FindStringCaseInsensitive (string search, int start_index)
1621                 {
1622                         if (search.Length == 0) {
1623                                 return -1;
1624                         }
1625                         // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1626                         if (start_index < 0 || start_index > Items.Count)
1627                                 throw new ArgumentOutOfRangeException("start_index");
1628
1629                         for (int i = 0; i < Items.Count; i++) {
1630                                 int index = (i + start_index) % Items.Count;
1631                                 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1632                                         return index;
1633                         }
1634
1635                         return -1;
1636                 }
1637
1638                 internal override bool IsInputCharInternal (char charCode)
1639                 {
1640                         return true;
1641                 }
1642
1643                 internal override ContextMenu ContextMenuInternal {
1644                         get {
1645                                 return base.ContextMenuInternal;
1646                         }
1647                         set {
1648                                 base.ContextMenuInternal = value;
1649                                 if (textbox_ctrl != null) {
1650                                         textbox_ctrl.ContextMenu = value;
1651                                 }
1652                         }
1653                 }
1654
1655                 internal void RestoreContextMenu ()
1656                 {
1657                         textbox_ctrl.RestoreContextMenu ();
1658                 }
1659
1660                 private void OnKeyDownCB(object sender, KeyEventArgs e)
1661                 {
1662                         if (Items.Count == 0)
1663                                 return;
1664
1665                         int offset;
1666                         switch (e.KeyCode) 
1667                         {
1668                                 case Keys.Up:
1669                                         FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1670
1671                                         if (DroppedDown)
1672                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1673                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1674                                         break;
1675         
1676                                 case Keys.Down:
1677                                         if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1678                                                 DropDownListBox ();
1679                                         else
1680                                                 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1681                                                 
1682                                         if (DroppedDown)
1683                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1684                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1685                                         break;
1686                                 
1687                                 case Keys.PageUp:
1688                                         offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1689                                         if (offset < 1)
1690                                                 offset = 1;
1691
1692                                         SelectedIndex = Math.Max (SelectedIndex - offset, 0);
1693
1694                                         if (DroppedDown)
1695                                                 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1696                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1697                                         break;
1698         
1699                                 case Keys.PageDown:
1700                                         if (SelectedIndex == -1) {
1701                                                 SelectedIndex = 0;
1702                                                 if (dropdown_style != ComboBoxStyle.Simple)
1703                                                         return;
1704                                         }
1705
1706                                         offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1707                                         if (offset < 1)
1708                                                 offset = 1;
1709
1710                                         SelectedIndex = Math.Min (SelectedIndex + offset, Items.Count - 1);
1711
1712                                         if (DroppedDown)
1713                                                 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1714                                                         listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1715                                         break;
1716                                 
1717                                 case Keys.Enter:        
1718                                 case Keys.Escape:
1719                                         DropDownListBoxFinished ();
1720                                         break;
1721                                         
1722                                 case Keys.Home:
1723                                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1724                                                 SelectedIndex = 0;
1725
1726                                                 if (DroppedDown)
1727                                                         if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1728                                                                 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1729                                         }
1730                                         
1731                                         break;
1732                                 case Keys.End:
1733                                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1734                                                 SelectedIndex = Items.Count - 1;
1735
1736                                                 if (DroppedDown)
1737                                                         if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1738                                                                 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1739                                         }
1740                                         
1741                                         break;
1742                                 default:
1743                                         break;
1744                         }
1745                 }
1746
1747                 // If no item is currently selected, and an item is found matching the text 
1748                 // in the textbox, then selected that item.  Otherwise the item at the given 
1749                 // index is selected.
1750                 private void FindMatchOrSetIndex(int index)
1751                 {
1752                         int match = -1;
1753                         if (SelectedIndex == -1 && Text.Length != 0)
1754                                 match = FindStringCaseInsensitive(Text);
1755                         if (match != -1)
1756                                 SelectedIndex = match;
1757                         else
1758                                 SelectedIndex = index;
1759                 }
1760                 
1761                 void OnMouseDownCB (object sender, MouseEventArgs e)
1762                 {
1763                         Rectangle area;
1764                         if (DropDownStyle == ComboBoxStyle.DropDownList)
1765                                 area = ClientRectangle;
1766                         else
1767                                 area = button_area;
1768
1769                         if (area.Contains (e.X, e.Y)) {
1770                                 if (Items.Count > 0)
1771                                         DropDownListBox ();
1772                                 else {
1773                                         button_state = ButtonState.Pushed;
1774                                         OnDropDown (EventArgs.Empty);
1775                                 }
1776                                 
1777                                 Invalidate (button_area);
1778                                 Update ();
1779                         }
1780                         Capture = true;
1781                 }
1782
1783                 void OnMouseEnter (object sender, EventArgs e)
1784                 {
1785                         if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1786                                 Invalidate ();
1787                 }
1788
1789                 void OnMouseLeave (object sender, EventArgs e)
1790                 {
1791                         if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1792                                 drop_down_button_entered = false;
1793                                 Invalidate ();
1794                         } else {
1795                                 if (show_dropdown_button)
1796                                         DropDownButtonEntered = false;
1797                         }
1798                 }
1799
1800                 void OnMouseMoveCB (object sender, MouseEventArgs e)
1801                 {
1802                         if (show_dropdown_button && !dropped_down)
1803                                 DropDownButtonEntered = button_area.Contains (e.Location);
1804
1805                         if (DropDownStyle == ComboBoxStyle.Simple)
1806                                 return;
1807
1808                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1809                                 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1810                                 if (listbox_ctrl.ClientRectangle.Contains (location))
1811                                         listbox_ctrl.Capture = true;
1812                         }
1813                 }
1814
1815                 void OnMouseUpCB (object sender, MouseEventArgs e)
1816                 {
1817                         Capture = false;
1818                         
1819                         button_state = ButtonState.Normal;
1820                         Invalidate (button_area);
1821
1822                         OnClick (EventArgs.Empty);
1823
1824                         if (dropped_down)
1825                                 listbox_ctrl.Capture = true;
1826                 }
1827
1828                 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1829                 {
1830                         if (Items.Count == 0)
1831                                 return;
1832
1833                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1834                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1835                                 listbox_ctrl.Scroll (-lines);
1836                         } else {
1837                                 int lines = me.Delta / 120;
1838                                 int index = SelectedIndex - lines;
1839                                 if (index < 0)
1840                                         index = 0;
1841                                 else if (index >= Items.Count)
1842                                         index = Items.Count - 1;
1843                                 SelectedIndex = index;
1844                         }
1845                 }
1846
1847                 internal override void OnPaintInternal (PaintEventArgs pevent)
1848                 {
1849                         if (suspend_ctrlupdate)
1850                                 return;
1851
1852                         Draw (ClientRectangle, pevent.Graphics);
1853                 }
1854                 
1855                 private void OnTextBoxClick (object sender, EventArgs e)
1856                 {
1857                         OnClick (e);
1858                 }
1859
1860                 private void OnTextChangedEdit (object sender, EventArgs e)
1861                 {
1862                         if (process_textchanged_event == false)
1863                                 return; 
1864
1865                         int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1866                         
1867                         if (item == -1) {
1868                                 // Setting base.Text below will raise this event
1869                                 // if we found something
1870                                 OnTextChanged (EventArgs.Empty);
1871                                 return;
1872                         }
1873                         
1874                         // TODO:  THIS IS BROKEN-ISH
1875                         // I don't think we should hilight, and setting the top item does weirdness
1876                         // when there is no scrollbar
1877                         
1878                         if (listbox_ctrl != null) {
1879                                 listbox_ctrl.SetTopItem (item);
1880                                 listbox_ctrl.HighlightedIndex = item;
1881                         }
1882
1883                         base.Text = textbox_ctrl.Text;
1884                 }
1885
1886                 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1887                 {
1888                         selected_index = -1;
1889                 }
1890
1891                 internal void SetControlText (string s, bool suppressTextChanged)
1892                 {
1893                         if (suppressTextChanged)
1894                                 process_textchanged_event = false;
1895                                 
1896                         textbox_ctrl.Text = s;
1897                         textbox_ctrl.SelectAll ();
1898                         process_textchanged_event = true;
1899                 }
1900                 
1901                 void UpdateComboBoxBounds ()
1902                 {
1903                         if (requested_height == -1)
1904                                 return;
1905
1906                         // Save the requested height since set bounds can destroy it
1907                         int save_height = requested_height;
1908                         SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1909                                 BoundsSpecified.Height);
1910                         requested_height = save_height;
1911                 }
1912
1913                 int SnapHeight (int height)
1914                 {
1915                         if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1916                                 if (IntegralHeight) {
1917                                         int border = ThemeEngine.Current.Border3DSize.Height;
1918                                         int lb_height = (height - PreferredHeight - 2) - border * 2;
1919                                         if (lb_height > ItemHeight) {
1920                                                 int partial = (lb_height) % ItemHeight;
1921                                                 height -= partial;
1922                                         } else if (lb_height < ItemHeight)
1923                                                 height = PreferredHeight;
1924                                 }
1925                         } else
1926                                 height = PreferredHeight;
1927
1928                         return height;
1929                 }
1930
1931                 private void UpdatedItems ()
1932                 {
1933                         if (listbox_ctrl != null) {
1934                                 listbox_ctrl.UpdateLastVisibleItem ();
1935                                 listbox_ctrl.CalcListBoxArea ();
1936                                 listbox_ctrl.Refresh ();
1937                         }
1938                 }
1939
1940                 #endregion Private Methods
1941
1942                 [ListBindableAttribute (false)]
1943                 public class ObjectCollection : IList, ICollection, IEnumerable
1944                 {
1945
1946                         private ComboBox owner;
1947                         internal ArrayList object_items = new ArrayList ();
1948                         
1949                         #region UIA Framework Events
1950
1951 #if NET_2_0
1952                         //NOTE:
1953                         //      We are using Reflection to add/remove internal events.
1954                         //      Class ListProvider uses the events.
1955                         //
1956                         //Event used to generate UIA StructureChangedEvent
1957                         static object UIACollectionChangedEvent = new object ();
1958
1959                         internal event CollectionChangeEventHandler UIACollectionChanged {
1960                                 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1961                                 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1962                         }
1963                         
1964                         internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1965                         {
1966                                 CollectionChangeEventHandler eh
1967                                         = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1968                                 if (eh != null)
1969                                         eh (owner, args);
1970                         }
1971 #endif
1972
1973                         #endregion UIA Framework Events
1974
1975                         public ObjectCollection (ComboBox owner)
1976                         {
1977                                 this.owner = owner;
1978                         }
1979
1980                         #region Public Properties
1981                         public int Count {
1982                                 get { return object_items.Count; }
1983                         }
1984
1985                         public bool IsReadOnly {
1986                                 get { return false; }
1987                         }
1988
1989                         [Browsable (false)]
1990                         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1991                         public virtual object this [int index] {
1992                                 get {
1993                                         if (index < 0 || index >= Count)
1994                                                 throw new ArgumentOutOfRangeException ("index");
1995
1996                                         return object_items[index];
1997                                 }
1998                                 set {
1999                                         if (index < 0 || index >= Count)
2000                                                 throw new ArgumentOutOfRangeException ("index");
2001                                         if (value == null)
2002                                                 throw new ArgumentNullException ("value");
2003
2004 #if NET_2_0
2005                                         //UIA Framework event: Item Removed
2006                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2007 #endif
2008
2009                                         object_items[index] = value;
2010                                         
2011 #if NET_2_0
2012                                         //UIA Framework event: Item Added
2013                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2014 #endif
2015
2016                                         if (owner.listbox_ctrl != null)
2017                                                 owner.listbox_ctrl.InvalidateItem (index);
2018                                         if (index == owner.SelectedIndex) {
2019                                                 if (owner.textbox_ctrl == null)
2020                                                         owner.Refresh ();
2021                                                 else
2022                                                         owner.textbox_ctrl.SelectedText = value.ToString ();
2023                                         }
2024                                 }
2025                         }
2026
2027                         bool ICollection.IsSynchronized {
2028                                 get { return false; }
2029                         }
2030
2031                         object ICollection.SyncRoot {
2032                                 get { return this; }
2033                         }
2034
2035                         bool IList.IsFixedSize {
2036                                 get { return false; }
2037                         }
2038
2039                         #endregion Public Properties
2040                         
2041                         #region Public Methods
2042                         public int Add (object item)
2043                         {
2044                                 int idx;
2045
2046                                 idx = AddItem (item);
2047                                 owner.UpdatedItems ();
2048                                 return idx;
2049                         }
2050
2051                         public void AddRange (object[] items)
2052                         {
2053                                 if (items == null)
2054                                         throw new ArgumentNullException ("items");
2055
2056                                 foreach (object mi in items)
2057                                         AddItem (mi);
2058                                         
2059                                 owner.UpdatedItems ();
2060                         }
2061
2062                         public void Clear ()
2063                         {
2064                                 owner.selected_index = -1;
2065                                 object_items.Clear ();
2066                                 owner.UpdatedItems ();
2067                                 owner.Refresh ();
2068                                 
2069 #if NET_2_0
2070                                 //UIA Framework event: Items list cleared
2071                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2072 #endif
2073                         }
2074                         
2075                         public bool Contains (object value)
2076                         {
2077                                 if (value == null)
2078                                         throw new ArgumentNullException ("value");
2079
2080                                 return object_items.Contains (value);
2081                         }
2082
2083 #if NET_2_0
2084                         public void CopyTo (object [] destination, int arrayIndex)
2085                         {
2086                                 object_items.CopyTo (destination, arrayIndex);
2087                         }
2088
2089                         void ICollection.CopyTo (Array destination, int index)
2090                         {
2091                                 object_items.CopyTo (destination, index);
2092                         }
2093 #else
2094                         public void CopyTo (object [] dest, int arrayIndex)
2095                         {
2096                                 object_items.CopyTo (dest, arrayIndex);
2097                         }
2098
2099                         void ICollection.CopyTo (Array dest, int index)
2100                         {
2101                                 object_items.CopyTo (dest, index);
2102                         }
2103 #endif
2104
2105                         public IEnumerator GetEnumerator ()
2106                         {
2107                                 return object_items.GetEnumerator ();
2108                         }
2109
2110                         int IList.Add (object item)
2111                         {
2112                                 return Add (item);
2113                         }
2114
2115                         public int IndexOf (object value)
2116                         {
2117                                 if (value == null)
2118                                         throw new ArgumentNullException ("value");
2119
2120                                 return object_items.IndexOf (value);
2121                         }
2122
2123                         public void Insert (int index,  object item)
2124                         {
2125                                 if (index < 0 || index > Count)
2126                                         throw new ArgumentOutOfRangeException ("index");
2127                                 if (item == null)
2128                                         throw new ArgumentNullException ("item");
2129                                 
2130                                 owner.BeginUpdate ();
2131                                 
2132                                 if (owner.Sorted)
2133                                         AddItem (item);
2134                                 else {
2135                                         object_items.Insert (index, item);
2136 #if NET_2_0
2137                                         //UIA Framework event: Item added
2138                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2139 #endif                                  
2140                                 }
2141                                 
2142                                 owner.EndUpdate ();     // Calls UpdatedItems
2143                         }
2144
2145                         public void Remove (object value)
2146                         {
2147                                 if (value == null)
2148                                         return;
2149
2150                                 if (IndexOf (value) == owner.SelectedIndex)
2151                                         owner.SelectedIndex = -1;
2152                                 
2153                                 RemoveAt (IndexOf (value));
2154                         }
2155
2156                         public void RemoveAt (int index)
2157                         {
2158                                 if (index < 0 || index >= Count)
2159                                         throw new ArgumentOutOfRangeException ("index");
2160                                         
2161                                 if (index == owner.SelectedIndex)
2162                                         owner.SelectedIndex = -1;
2163
2164 #if NET_2_0
2165                                 object removed = object_items [index];
2166 #endif
2167
2168
2169                                 object_items.RemoveAt (index);
2170                                 owner.UpdatedItems ();
2171                                 
2172 #if NET_2_0
2173                                 //UIA Framework event: Item removed
2174                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2175 #endif
2176                         }
2177                         #endregion Public Methods
2178
2179                         #region Private Methods
2180                         private int AddItem (object item)
2181                         {
2182                                 if (item == null)
2183                                         throw new ArgumentNullException ("item");
2184
2185                                 if (owner.Sorted) {
2186                                         int index = 0;
2187                                         foreach (object o in object_items) {
2188                                                 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2189                                                         object_items.Insert (index, item);
2190                                                         
2191                                                         // If we added the new item before the selectedindex
2192                                                         // bump the selectedindex by one, behavior differs if
2193                                                         // Handle has not been created.
2194                                                         if (index <= owner.selected_index && owner.IsHandleCreated)
2195                                                                 owner.selected_index++;
2196                                                                 
2197 #if NET_2_0
2198                                                         //UIA Framework event: Item added
2199                                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2200 #endif
2201
2202                                                         return index;
2203                                                 }
2204                                                 index++;
2205                                         }
2206                                 }
2207                                 object_items.Add (item);
2208                                 
2209 #if NET_2_0
2210                                 //UIA Framework event: Item added
2211                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2212 #endif
2213                                 
2214                                 return object_items.Count - 1;
2215                         }
2216                         
2217                         internal void AddRange (IList items)
2218                         {
2219                                 foreach (object mi in items)
2220                                         AddItem (mi);
2221                                 
2222                                 owner.UpdatedItems ();
2223                         }
2224
2225                         internal void Sort ()
2226                         {
2227                                 // If the objects the user put here don't have their own comparer,
2228                                 // use one that compares based on the object's ToString
2229                                 if (object_items.Count > 0 && object_items[0] is IComparer)
2230                                         object_items.Sort ();
2231                                 else
2232                                         object_items.Sort (new ObjectComparer (owner));
2233                         }
2234
2235                         private class ObjectComparer : IComparer
2236                         {
2237                                 private ListControl owner;
2238                                 
2239                                 public ObjectComparer (ListControl owner)
2240                                 {
2241                                         this.owner = owner;
2242                                 }
2243                                 
2244                                 #region IComparer Members
2245                                 public int Compare (object x, object y)
2246                                 {
2247                                         return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2248                                 }
2249                                 #endregion
2250                         }
2251                         #endregion Private Methods
2252                 }
2253
2254                 internal class ComboTextBox : TextBox {
2255
2256                         private ComboBox owner;
2257
2258                         public ComboTextBox (ComboBox owner)
2259                         {
2260                                 this.owner = owner;
2261                                 ShowSelection = false;
2262                                 HideSelection = false;
2263                         }
2264
2265                         internal void SetSelectable (bool selectable)
2266                         {
2267                                 SetStyle (ControlStyles.Selectable, selectable);
2268                         }
2269
2270                         internal void ActivateCaret (bool active)
2271                         {
2272                                 if (active)
2273                                         document.CaretHasFocus ();
2274                                 else
2275                                         document.CaretLostFocus ();
2276                         }
2277                         
2278 #if NET_2_0
2279                         internal override void OnTextUpdate ()
2280                         {
2281                                 base.OnTextUpdate ();
2282                                 owner.OnTextUpdate (EventArgs.Empty);
2283                         }
2284 #endif
2285                         
2286                         protected override void OnGotFocus (EventArgs e)
2287                         {
2288                                 owner.Select (false, true);
2289                         }
2290
2291                         protected override void OnLostFocus (EventArgs e)
2292                         {
2293                                 owner.Select (false, true);
2294                         }
2295
2296                         public override bool Focused {
2297                                 get {
2298                                         return owner.Focused;
2299                                 }
2300                         }
2301                         
2302                         internal override bool ActivateOnShow { get { return false; } }
2303                 }
2304
2305                 internal class ComboListBox : Control
2306                 {
2307                         private ComboBox owner;
2308                         private VScrollBarLB vscrollbar_ctrl;
2309                         private int top_item;                   /* First item that we show the in the current page */
2310                         private int last_item;                  /* Last visible item */
2311                         internal int page_size;                 /* Number of listbox items per page */
2312                         private Rectangle textarea_drawable;    /* Rectangle of the drawable text area */
2313                         
2314                         internal enum ItemNavigation
2315                         {
2316                                 First,
2317                                 Last,
2318                                 Next,
2319                                 Previous,
2320                                 NextPage,
2321                                 PreviousPage,
2322                         }
2323                         
2324                         class VScrollBarLB : VScrollBar
2325                         {
2326                                 public VScrollBarLB ()
2327                                 {
2328                                 }
2329                                 
2330                                 internal override bool InternalCapture {
2331                                         get { return Capture; }
2332                                         set { }
2333                                 }
2334
2335                                 public void FireMouseDown (MouseEventArgs e) 
2336                                 {
2337                                         if (!Visible) 
2338                                                 return;
2339
2340                                         e = TranslateEvent (e);
2341                                         OnMouseDown (e);
2342                                 }
2343                                 
2344                                 public void FireMouseUp (MouseEventArgs e) 
2345                                 {
2346                                         if (!Visible)
2347                                                 return;
2348
2349                                         e = TranslateEvent (e);
2350                                         OnMouseUp (e);
2351                                 }
2352                                 
2353                                 public void FireMouseMove (MouseEventArgs e) 
2354                                 {
2355                                         if (!Visible)
2356                                                 return;
2357
2358                                         e = TranslateEvent (e);
2359                                         OnMouseMove (e);
2360                                 }
2361                                 
2362                                 MouseEventArgs TranslateEvent (MouseEventArgs e)
2363                                 {
2364                                         Point loc = PointToClient (Control.MousePosition);
2365                                         return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2366                                 }
2367                         }
2368
2369                         public ComboListBox (ComboBox owner)
2370                         {
2371                                 this.owner = owner;
2372                                 top_item = 0;
2373                                 last_item = 0;
2374                                 page_size = 0;
2375
2376                                 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2377
2378                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2379                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2380
2381                                 this.is_visible = false;
2382
2383                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2384                                         InternalBorderStyle = BorderStyle.Fixed3D;
2385                                 else
2386                                         InternalBorderStyle = BorderStyle.FixedSingle;
2387                         }
2388
2389                         protected override CreateParams CreateParams
2390                         {
2391                                 get {
2392                                         CreateParams cp = base.CreateParams;
2393                                         if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2394                                                 return cp;
2395
2396                                         cp.Style ^= (int)WindowStyles.WS_CHILD;
2397                                         cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2398                                         cp.Style |= (int)WindowStyles.WS_POPUP;
2399                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2400                                         return cp;
2401                                 }
2402                         }
2403
2404                         internal override bool InternalCapture {
2405                                 get {
2406                                         return Capture;
2407                                 }
2408
2409                                 set {
2410                                 }
2411                         }
2412
2413                         internal override bool ActivateOnShow { get { return false; } }
2414                         #region Private Methods
2415
2416                         // Calcs the listbox area
2417                         internal void CalcListBoxArea ()
2418                         {
2419                                 int width, height;
2420                                 bool show_scrollbar = false;
2421
2422                                 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2423                                         Rectangle area = owner.listbox_area;
2424                                         width = area.Width;
2425                                         height = area.Height;
2426
2427                                         // No calculation needed
2428                                         if (height <= 0 || width <= 0)
2429                                                 return;
2430                                 }
2431                                 else { // DropDown or DropDownList
2432                                         
2433                                         width = owner.DropDownWidth;
2434                                         int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2435                                         
2436                                         if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2437                                                 height = 0;
2438                                                 for (int i = 0; i < count; i++) {
2439                                                         height += owner.GetItemHeight (i);
2440                                                 }
2441                                                 
2442                                         } else  {
2443 #if NET_2_0
2444                                                 height = owner.DropDownHeight;
2445 #else           
2446                                                 height = owner.ItemHeight * count;
2447 #endif
2448                                         }
2449                                 }
2450                                 
2451                                 page_size = Math.Max (height / owner.ItemHeight, 1);
2452
2453                                 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2454                                 if ((dropdown_style != ComboBoxStyle.Simple && owner.Items.Count <= owner.MaxDropDownItems)
2455                                         || (dropdown_style == ComboBoxStyle.Simple && owner.Items.Count * owner.ItemHeight < height)) {
2456
2457                                         if (vscrollbar_ctrl != null)
2458                                                 vscrollbar_ctrl.Visible = false;
2459                                         if (dropdown_style != ComboBoxStyle.Simple)
2460                                                 height = owner.ItemHeight * owner.items.Count;
2461                                 } else {
2462                                         /* Need vertical scrollbar */
2463                                         if (vscrollbar_ctrl == null) {
2464                                                 vscrollbar_ctrl = new VScrollBarLB ();
2465                                                 vscrollbar_ctrl.Minimum = 0;
2466                                                 vscrollbar_ctrl.SmallChange = 1;
2467                                                 vscrollbar_ctrl.LargeChange = 1;
2468                                                 vscrollbar_ctrl.Maximum = 0;
2469                                                 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2470                                                 Controls.AddImplicit (vscrollbar_ctrl);
2471                                         }
2472                                         
2473                                         vscrollbar_ctrl.Dock = DockStyle.Right;
2474
2475                                         vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2476 #if NET_2_0
2477                                         int large = page_size;
2478 #else
2479                                         int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2480 #endif
2481                                         if (large < 1)
2482                                                 large = 1;
2483                                         vscrollbar_ctrl.LargeChange = large;
2484                                         show_scrollbar = vscrollbar_ctrl.Visible = true;
2485
2486                                         int hli = HighlightedIndex;
2487                                         if (hli > 0) {
2488                                                 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2489                                                 vscrollbar_ctrl.Value = hli;
2490                                         }
2491                                 }
2492                                 
2493                                 Size = new Size (width, height);
2494                                 textarea_drawable = ClientRectangle;
2495                                 textarea_drawable.Width = width;
2496                                 textarea_drawable.Height = height;
2497                                 
2498                                 if (vscrollbar_ctrl != null && show_scrollbar)
2499                                         textarea_drawable.Width -= vscrollbar_ctrl.Width;
2500
2501                                 last_item = LastVisibleItem ();
2502                         }
2503
2504                         private void Draw (Rectangle clip, Graphics dc)
2505                         {
2506                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2507
2508                                 if (owner.Items.Count > 0) {
2509                                         
2510                                         for (int i = top_item; i <= last_item; i++) {
2511                                                 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2512
2513                                                 if (!clip.IntersectsWith (item_rect))
2514                                                         continue;
2515
2516                                                 DrawItemState state = DrawItemState.None;
2517
2518                                                 if (i == HighlightedIndex) {
2519                                                         state |= DrawItemState.Selected;
2520                                                         
2521                                                         if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2522                                                                 state |= DrawItemState.Focus;
2523                                                         }
2524                                                 }
2525                                                 
2526                                                 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2527                                                         i, state, owner.ForeColor, owner.BackColor));
2528                                         }
2529                                 }
2530                         }
2531
2532                         int highlighted_index = -1;
2533
2534                         public int HighlightedIndex {
2535                                 get { return highlighted_index; }
2536                                 set { 
2537                                         if (highlighted_index == value)
2538                                                 return;
2539
2540                                         if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2541                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2542                                         highlighted_index = value;
2543                                         if (highlighted_index != -1)
2544                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2545                                 }
2546                         }
2547
2548                         private Rectangle GetItemDisplayRectangle (int index, int top_index)
2549                         {
2550                                 if (index < 0 || index >= owner.Items.Count)
2551                                         throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2552
2553                                 Rectangle item_rect = new Rectangle ();
2554                                 int height = owner.GetItemHeight (index);
2555
2556                                 item_rect.X = 0;
2557                                 item_rect.Width = textarea_drawable.Width;
2558                                 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2559                                         item_rect.Y = 0;
2560                                         for (int i = top_index; i < index; i++)
2561                                                 item_rect.Y += owner.GetItemHeight (i);
2562                                 } else
2563                                         item_rect.Y = height * (index - top_index);
2564
2565                                 item_rect.Height = height;
2566                                 return item_rect;
2567                         }
2568
2569                         public void HideWindow ()
2570                         {
2571                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2572                                         return;
2573                                         
2574                                 Capture = false;
2575                                 Hide ();
2576                                 owner.DropDownListBoxFinished ();
2577                         }
2578
2579                         private int IndexFromPointDisplayRectangle (int x, int y)
2580                         {
2581                                 for (int i = top_item; i <= last_item; i++) {
2582                                         if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2583                                                 return i;
2584                                 }
2585
2586                                 return -1;
2587                         }
2588
2589                         public void InvalidateItem (int index)
2590                         {
2591                                 if (Visible)
2592                                         Invalidate (GetItemDisplayRectangle (index, top_item));
2593                         }
2594
2595                         public int LastVisibleItem ()
2596                         {
2597                                 Rectangle item_rect;
2598                                 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2599                                 int i = 0;
2600                                 
2601                                 for (i = top_item; i < owner.Items.Count; i++) {
2602                                         item_rect = GetItemDisplayRectangle (i, top_item);
2603                                         if (item_rect.Y + item_rect.Height > top_y) {
2604                                                 return i;
2605                                         }
2606                                 }
2607                                 return i - 1;
2608                         }
2609
2610                         public void SetTopItem (int item)
2611                         {
2612                                 if (top_item == item)
2613                                         return;
2614                                 top_item = item;
2615                                 UpdateLastVisibleItem ();
2616                                 Invalidate ();
2617                         }
2618
2619                         public int FirstVisibleItem ()
2620                         {
2621                                 return top_item;
2622                         }
2623                         
2624                         bool scrollbar_grabbed = false;
2625
2626                         bool InScrollBar {
2627                                 get {
2628                                         if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2629                                                 return false;
2630
2631                                         return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2632                                 }
2633                         }
2634
2635                         protected override void OnMouseDown (MouseEventArgs e)
2636                         {
2637                                 if (InScrollBar) {
2638                                         vscrollbar_ctrl.FireMouseDown (e);
2639                                         scrollbar_grabbed = true;
2640                                 }
2641                         }
2642
2643                         protected override void OnMouseMove (MouseEventArgs e)
2644                         {
2645                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2646                                         return;
2647
2648                                 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2649                                         vscrollbar_ctrl.FireMouseMove (e);
2650                                         return;
2651                                 }
2652
2653                                 Point pt = PointToClient (Control.MousePosition);
2654                                 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2655
2656                                 if (index != -1)
2657                                         HighlightedIndex = index;
2658                         }
2659                         
2660                         protected override void OnMouseUp (MouseEventArgs e)
2661                         {
2662                                 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2663
2664                                 if (scrollbar_grabbed) {
2665                                         vscrollbar_ctrl.FireMouseUp (e);
2666                                         scrollbar_grabbed = false;
2667                                         if (index != -1)
2668                                                 HighlightedIndex = index;
2669                                         return;
2670                                 }
2671
2672                                 if (index == -1) {
2673                                         HideWindow ();
2674                                         return;
2675                                 }
2676
2677                                 bool is_change = owner.SelectedIndex != index;
2678                                 
2679                                 owner.SelectedIndex = index;
2680                                 owner.OnSelectionChangeCommitted (new EventArgs ());
2681                                 
2682                                 // If the user selected the already selected item, SelectedIndex
2683                                 // won't fire these events, but .Net does, so we do it here
2684                                 if (!is_change) {
2685                                         owner.OnSelectedValueChanged (EventArgs.Empty);
2686                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
2687                                 }
2688                                 
2689                                 HideWindow ();
2690                         }
2691
2692                         internal override void OnPaintInternal (PaintEventArgs pevent)
2693                         {
2694                                 Draw (pevent.ClipRectangle,pevent.Graphics);
2695                         }
2696
2697                         public bool ShowWindow ()
2698                         {
2699                                 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2700                                         return false;
2701
2702                                 HighlightedIndex = owner.SelectedIndex;
2703
2704                                 CalcListBoxArea ();
2705                                 Show ();
2706
2707                                 Refresh ();
2708                                 owner.OnDropDown (EventArgs.Empty);
2709                                 return true;
2710                         }
2711                         
2712                         public void UpdateLastVisibleItem ()
2713                         {
2714                                 last_item = LastVisibleItem ();
2715                         }
2716
2717                         public void Scroll (int delta)
2718                         {
2719                                 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2720                                         return;
2721
2722                                 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2723
2724                                 int val = vscrollbar_ctrl.Value + delta;
2725                                 if (val > max)
2726                                         val = max;
2727                                 else if (val < vscrollbar_ctrl.Minimum)
2728                                         val = vscrollbar_ctrl.Minimum;
2729                                 vscrollbar_ctrl.Value = val;
2730                         }
2731
2732                         private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2733                         {
2734                                 if (owner.Items.Count == 0)
2735                                         return;
2736
2737                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2738                                 Scroll (-lines);
2739                         }
2740
2741                         // Value Changed
2742                         private void VerticalScrollEvent (object sender, EventArgs e)
2743                         {
2744                                 if (top_item == vscrollbar_ctrl.Value)
2745                                         return;
2746
2747                                 top_item =  vscrollbar_ctrl.Value;
2748                                 UpdateLastVisibleItem ();
2749                                 Invalidate ();
2750                         }
2751                         
2752                         protected override void WndProc(ref Message m) {
2753                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2754                                         owner.Select (false, true);
2755                                 }
2756                                 base.WndProc (ref m);
2757                         }
2758
2759                         #endregion Private Methods
2760                 }
2761         }
2762 }