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