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