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