- Implement 2.0 image key feature.
[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
422                                         if (IsHandleCreated == true)
423                                                 Controls.AddImplicit (textbox_ctrl);
424                                 }
425                                 
426                                 ResumeLayout ();
427                                 OnDropDownStyleChanged (EventArgs.Empty);
428                                 
429                                 LayoutComboBox ();
430                                 UpdateComboBoxBounds ();
431                                 Refresh ();
432                         }
433                 }
434
435                 public int DropDownWidth {
436                         get { 
437                                 if (dropdown_width == -1)
438                                         return Width;
439                                         
440                                 return dropdown_width; 
441                         }
442                         set {
443                                 if (dropdown_width == value)
444                                         return;
445                                         
446                                 if (value < 1)
447 #if NET_2_0
448                                         throw new ArgumentOutOfRangeException ("DropDownWidth",
449                                                 "The DropDownWidth value is less than one.");
450 #else
451                                         throw new ArgumentException ("The DropDownWidth value is less than one.");
452 #endif
453
454                                 dropdown_width = value;
455                         }
456                 }
457                 
458                 [Browsable (false)]
459                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
460                 public bool DroppedDown {
461                         get { 
462                                 if (dropdown_style == ComboBoxStyle.Simple)
463                                         return true;
464                                 
465                                 return dropped_down;
466                         }
467                         set {
468                                 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
469                                         return;
470                                         
471                                 if (value) 
472                                         DropDownListBox ();
473                                 else
474                                         listbox_ctrl.Hide ();
475                         }
476                 }
477
478 #if NET_2_0
479                 [DefaultValue (FlatStyle.Standard)]
480                 [Localizable (true)]
481                 public FlatStyle FlatStyle {
482                         get { return flat_style; }
483                         set {
484                                 if (!Enum.IsDefined (typeof (FlatStyle), value))
485                                         throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
486                                 
487                                 flat_style = value;
488                                 LayoutComboBox ();
489                                 Invalidate ();
490                         }
491                 }
492 #endif
493
494                 public override bool Focused {
495                         get { return base.Focused; }
496                 }
497
498                 public override Color ForeColor {
499                         get { return base.ForeColor; }
500                         set {
501                                 if (base.ForeColor == value)
502                                         return;
503                                 base.ForeColor = value;
504                                 Refresh ();
505                         }
506                 }
507
508                 [DefaultValue (true)]
509                 [Localizable (true)]
510                 public bool IntegralHeight {
511                         get { return integral_height; }
512                         set {
513                                 if (integral_height == value)
514                                         return;
515                                 integral_height = value;
516                                 UpdateComboBoxBounds ();
517                                 Refresh ();
518                         }
519                 }
520
521                 [Localizable (true)]
522                 public int ItemHeight {
523                         get {
524                                 if (item_height == -1) {
525                                         SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
526                                         item_height = (int) sz.Height;
527                                 }
528                                 return item_height;
529                         }
530                         set {
531                                 if (value < 1)
532 #if NET_2_0
533                                         throw new ArgumentOutOfRangeException ("ItemHeight",
534                                                 "The item height value is less than one.");
535 #else
536                                         throw new ArgumentException ("The item height value is less than one.");
537 #endif
538
539                                 item_height_specified = true;
540                                 item_height = value;
541                                 if (IntegralHeight)
542                                         UpdateComboBoxBounds ();
543                                 LayoutComboBox ();
544                                 Refresh ();
545                         }
546                 }
547
548                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
549                 [Localizable (true)]
550                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
551 #if NET_2_0
552                 [MergableProperty (false)]
553 #endif
554                 public ComboBox.ObjectCollection Items {
555                         get { return items; }
556                 }
557
558                 [DefaultValue (8)]
559                 [Localizable (true)]
560                 public int MaxDropDownItems {
561                         get { return maxdrop_items; }
562                         set {
563                                 if (maxdrop_items == value)
564                                         return;
565                                 maxdrop_items = value;
566                         }
567                 }
568
569 #if NET_2_0
570                 public override Size MaximumSize {
571                         get { return base.MaximumSize; }
572                         set {
573                                 base.MaximumSize = new Size (value.Width, 0);
574                         }
575                 }
576 #endif
577
578                 [DefaultValue (0)]
579                 [Localizable (true)]
580                 public int MaxLength {
581                         get { return max_length; }
582                         set {
583                                 if (max_length == value)
584                                         return;
585
586                                 max_length = value;
587                                 
588                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
589                                         if (value < 0) {
590                                                 value = 0;
591                                         }
592                                         textbox_ctrl.MaxLength = value;
593                                 }
594                         }
595                 }
596
597 #if NET_2_0
598                 public override Size MinimumSize {
599                         get { return base.MinimumSize; }
600                         set {
601                                 base.MinimumSize = new Size (value.Width, 0);
602                         }
603                 }
604                 
605                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
606                 [EditorBrowsable (EditorBrowsableState.Never)]
607                 [Browsable (false)]
608                 public new Padding Padding  {
609                         get { return base.Padding; }
610                         set { base.Padding = value; }
611                 }
612 #endif
613
614                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
615                 [Browsable (false)]
616                 public int PreferredHeight {
617                         get {
618                                 return ItemHeight + 5;
619                         }
620                 }
621
622                 [Browsable (false)]
623                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
624                 public override int SelectedIndex {
625                         get { return selected_index; }
626                         set {
627                                 if (selected_index == value)
628                                         return;
629
630                                 if (value <= -2 || value >= Items.Count)
631                                         throw new ArgumentOutOfRangeException ("Index of out range");
632                                 selected_index = value;
633
634                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
635                                         if (value == -1)
636                                                 SetControlText("");
637                                         else
638                                                 SetControlText (GetItemText (Items [value]));
639                                 }
640
641                                 if (DropDownStyle == ComboBoxStyle.DropDownList)
642                                         Invalidate ();
643
644                                 if (listbox_ctrl != null)
645                                         listbox_ctrl.HighlightedIndex = value;
646
647                                 OnSelectedValueChanged (new EventArgs ());
648                                 OnSelectedIndexChanged (new EventArgs ());
649                                 OnSelectedItemChanged (new EventArgs ());
650                         }
651                 }
652
653                 [Browsable (false)]
654                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
655                 [Bindable(true)]
656                 public object SelectedItem {
657                         get { return selected_index == -1 ? null : Items [selected_index]; }
658                         set {
659                                 object item = selected_index == -1 ? null : Items [selected_index];
660                                 if (item == value)
661                                         return;
662                                 SelectedIndex = Items.IndexOf (value);
663                         }
664                 }
665                 
666                 [Browsable (false)]
667                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
668                 public string SelectedText {
669                         get {
670                                 if (dropdown_style == ComboBoxStyle.DropDownList)
671                                         return "";
672                                         
673                                 return textbox_ctrl.SelectedText;
674                         }
675                         set {
676                                 if (dropdown_style == ComboBoxStyle.DropDownList)
677                                         return;
678                                 textbox_ctrl.SelectedText = value;
679                         }
680                 }
681
682                 [Browsable (false)]
683                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
684                 public int SelectionLength {
685                         get {
686                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
687                                         return 0;
688                                 
689                                 int result = textbox_ctrl.SelectionLength;
690                                 return result == -1 ? 0 : result;
691                         }
692                         set {
693                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
694                                         return;
695                                 if (textbox_ctrl.SelectionLength == value)
696                                         return;
697                                 textbox_ctrl.SelectionLength = value;
698                         }
699                 }
700
701                 [Browsable (false)]
702                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
703                 public int SelectionStart {
704                         get { 
705                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
706                                         return 0;
707                                 return textbox_ctrl.SelectionStart;
708                         }
709                         set {
710                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
711                                         return;
712                                 if (textbox_ctrl.SelectionStart == value)
713                                         return;
714                                 textbox_ctrl.SelectionStart = value;
715                         }
716                 }
717
718                 [DefaultValue (false)]
719                 public bool Sorted {
720                         get { return sorted; }
721                         set {
722                                 if (sorted == value)
723                                         return;
724                                 sorted = value;
725                                 SelectedIndex = -1;
726                                 if (sorted) {
727                                         Items.Sort ();
728                                         LayoutComboBox ();
729                                 }
730                         }
731                 }
732
733                 [Bindable (true)]
734                 [Localizable (true)]
735                 public override string Text {
736                         get {
737                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
738                                         if (textbox_ctrl != null) {
739                                                 return textbox_ctrl.Text;
740                                         }
741                                 }
742                                 
743                                 if (SelectedItem != null)
744                                         return GetItemText (SelectedItem);
745                                 
746                                 return base.Text;
747                         }
748                         set {
749                                 if (value == null) {
750                                         if (SelectedIndex == -1) {
751                                                 if (dropdown_style != ComboBoxStyle.DropDownList)
752                                                         SetControlText ("");
753                                         } else {
754                                                 SelectedIndex = -1;
755                                         }
756                                         return;
757                                 }
758
759                                 // do nothing if value exactly matches text of selected item
760                                 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
761                                         return;
762
763                                 // find exact match using case-sensitive comparison, and if does
764                                 // not result in any match then use case-insensitive comparison
765                                 int index = FindStringExact (value, -1, false);
766                                 if (index == -1) {
767                                         index = FindStringExact (value, -1, true);
768                                 }
769                                 if (index != -1) {
770                                         SelectedIndex = index;
771                                         return;
772                                 }
773
774                                 if (dropdown_style != ComboBoxStyle.DropDownList)
775                                         textbox_ctrl.Text = GetItemText (value);
776                         }
777                 }
778
779                 #endregion Public Properties
780
781                 #region Public Methods
782 #if NET_2_0
783                 [Obsolete ("This method has been deprecated")]
784 #endif
785                 protected virtual void AddItemsCore (object[] value)
786                 {
787                         
788                 }
789
790                 public void BeginUpdate ()
791                 {
792                         suspend_ctrlupdate = true;
793                 }
794
795 #if NET_2_0
796                 protected override AccessibleObject CreateAccessibilityInstance ()
797                 {
798                         return base.CreateAccessibilityInstance ();
799                 }
800                 
801                 protected override void CreateHandle ()
802                 {
803                         base.CreateHandle ();
804                 }
805 #endif
806
807                 protected override void Dispose (bool disposing)
808                 {
809                         if (disposing) {
810                                 if (listbox_ctrl != null) {
811                                         listbox_ctrl.Dispose ();
812                                         Controls.RemoveImplicit (listbox_ctrl);
813                                         listbox_ctrl = null;
814                                 }
815                         
816                                 if (textbox_ctrl != null) {
817                                         Controls.RemoveImplicit (textbox_ctrl);
818                                         textbox_ctrl.Dispose ();
819                                         textbox_ctrl = null;
820                                 }
821                         }
822                         
823                         base.Dispose (disposing);
824                 }
825
826                 public void EndUpdate ()
827                 {
828                         suspend_ctrlupdate = false;
829                         UpdatedItems ();
830                         Refresh ();
831                 }
832
833                 public int FindString (string s)
834                 {
835                         return FindString (s, -1);
836                 }
837
838                 public int FindString (string s, int startIndex)
839                 {
840                         if (s == null || Items.Count == 0) 
841                                 return -1;
842
843 #if NET_2_0
844                         if (startIndex < -1 || startIndex >= Items.Count)
845 #else
846                         if (startIndex < -1 || startIndex >= Items.Count - 1)
847 #endif
848                                 throw new ArgumentOutOfRangeException ("startIndex");
849
850                         int i = startIndex;
851 #if NET_2_0
852                         if (i == (Items.Count - 1))
853                                 i = -1;
854 #endif
855                         do {
856                                 i++;
857                                 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
858                                         return i;
859                                 if (i == (Items.Count - 1))
860                                         i = -1;
861                         } while (i != startIndex);
862
863                         return -1;
864                 }
865
866                 public int FindStringExact (string s)
867                 {
868                         return FindStringExact (s, -1);
869                 }
870
871                 public int FindStringExact (string s, int startIndex)
872                 {
873                         return FindStringExact (s, startIndex, true);
874                 }
875
876                 private int FindStringExact (string s, int startIndex, bool ignoreCase)
877                 {
878                         if (s == null || Items.Count == 0) 
879                                 return -1;
880
881 #if NET_2_0
882                         if (startIndex < -1 || startIndex >= Items.Count)
883 #else
884                         if (startIndex < -1 || startIndex >= Items.Count - 1)
885 #endif
886                                 throw new ArgumentOutOfRangeException ("startIndex");
887
888                         int i = startIndex;
889 #if NET_2_0
890                         if (i == (Items.Count - 1))
891                                 i = -1;
892 #endif
893                         do {
894                                 i++;
895                                 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
896                                         return i;
897                                 if (i == (Items.Count - 1))
898                                         i = -1;
899                         } while (i != startIndex);
900
901                         return -1;
902                 }
903
904                 public int GetItemHeight (int index)
905                 {
906                         if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
907
908                                 if (index < 0 || index >= Items.Count )
909                                         throw new ArgumentOutOfRangeException ("The item height value is less than zero");
910                                 
911                                 object item = Items [index];
912                                 if (item_heights.Contains (item))
913                                         return (int) item_heights [item];
914                                 
915                                 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
916                                 OnMeasureItem (args);
917                                 item_heights [item] = args.ItemHeight;
918                                 return args.ItemHeight;
919                         }
920
921                         return ItemHeight;
922                 }
923
924                 protected override bool IsInputKey (Keys keyData)
925                 {
926                         switch (keyData) {
927                         case Keys.Up:
928                         case Keys.Down:
929                         case Keys.Left:
930                         case Keys.Right:
931                         case Keys.PageUp:
932                         case Keys.PageDown:
933                                 return true;
934                         
935                         default:
936                                 return false;
937                         }
938                 }
939
940                 protected override void OnBackColorChanged (EventArgs e)
941                 {
942                         base.OnBackColorChanged (e);
943                 }
944
945                 protected override void OnDataSourceChanged (EventArgs e)
946                 {
947                         base.OnDataSourceChanged (e);
948                         BindDataItems ();
949                         
950                         if (DataSource == null || DataManager == null) {
951                                 SelectedIndex = -1;
952                         } 
953                         else {
954                                 SelectedIndex = DataManager.Position;
955                         }
956                 }
957
958                 protected override void OnDisplayMemberChanged (EventArgs e)
959                 {
960                         base.OnDisplayMemberChanged (e);
961
962                         if (DataManager == null || !IsHandleCreated)
963                                 return;
964
965                         BindDataItems ();
966                         SelectedIndex = DataManager.Position;
967                 }
968
969                 protected virtual void OnDrawItem (DrawItemEventArgs e)
970                 {
971                         switch (DrawMode) {
972                         case DrawMode.OwnerDrawFixed:
973                         case DrawMode.OwnerDrawVariable:
974                                 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
975                                 if (eh != null)
976                                         eh (this, e);
977                                 break;
978                         default:
979                                 ThemeEngine.Current.DrawComboBoxItem (this, e);
980                                 break;
981                         }
982                 }
983
984                 protected virtual void OnDropDown (EventArgs e)
985                 {
986                         EventHandler eh = (EventHandler)(Events [DropDownEvent]);
987                         if (eh != null)
988                                 eh (this, e);
989                 }
990
991 #if NET_2_0
992                 protected virtual void OnDropDownClosed (EventArgs e)
993                 {
994                         EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
995                         if (eh != null)
996                                 eh (this, e);
997                 }
998 #endif
999
1000                 protected virtual void OnDropDownStyleChanged (EventArgs e)
1001                 {
1002                         EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1003                         if (eh != null)
1004                                 eh (this, e);
1005                 }
1006
1007                 protected override void OnFontChanged (EventArgs e)
1008                 {
1009                         base.OnFontChanged (e);
1010
1011                         if (textbox_ctrl != null)
1012                                 textbox_ctrl.Font = Font;
1013                         
1014                         if (!item_height_specified) {
1015                                 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
1016                                 item_height = (int) sz.Height;
1017                         }
1018
1019                         if (IntegralHeight)
1020                                 UpdateComboBoxBounds ();
1021
1022                         LayoutComboBox ();
1023                 }
1024
1025                 protected override void OnForeColorChanged (EventArgs e)
1026                 {
1027                         base.OnForeColorChanged (e);
1028                 }
1029
1030                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1031                 protected override void OnGotFocus (EventArgs e)
1032                 {
1033                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1034                                 // We draw DDL styles manually, so they require a
1035                                 // refresh to have their selection drawn
1036                                 Invalidate ();
1037                         }
1038                         
1039                         if (textbox_ctrl != null) {
1040                                 textbox_ctrl.SetSelectable (false);
1041                                 textbox_ctrl.ShowSelection = true;
1042                                 textbox_ctrl.ActivateCaret (true);
1043                                 textbox_ctrl.SelectAll ();
1044                         }
1045
1046                         base.OnGotFocus (e);
1047                 }
1048
1049                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1050                 protected override void OnLostFocus (EventArgs e)
1051                 {
1052                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1053                                 // We draw DDL styles manually, so they require a
1054                                 // refresh to have their selection drawn
1055                                 Invalidate ();
1056                         }
1057
1058                         if (listbox_ctrl != null && dropped_down) {
1059                                 listbox_ctrl.HideWindow ();
1060                         }
1061
1062                         if (textbox_ctrl != null) {
1063                                 textbox_ctrl.SetSelectable (true);
1064                                 textbox_ctrl.ActivateCaret (false);
1065                                 textbox_ctrl.ShowSelection = false;
1066                         }
1067
1068                         base.OnLostFocus (e);
1069                 }
1070
1071                 protected override void OnHandleCreated (EventArgs e)
1072                 {
1073                         base.OnHandleCreated (e);
1074
1075                         if (listbox_ctrl != null) {
1076                                 Controls.AddImplicit (listbox_ctrl);
1077                                 listbox_ctrl.Visible = true;
1078                         }
1079
1080                         if (textbox_ctrl != null)
1081                                 Controls.AddImplicit (textbox_ctrl);
1082
1083                         LayoutComboBox ();
1084                 }
1085
1086                 protected override void OnHandleDestroyed (EventArgs e)
1087                 {
1088                         base.OnHandleDestroyed (e);
1089                 }
1090
1091                 protected override void OnKeyPress (KeyPressEventArgs e)
1092                 {
1093                         // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
1094                         //if (index != -1)
1095                         //      SelectedIndex = index;
1096
1097                         base.OnKeyPress (e);
1098                 }
1099
1100                 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1101                 {
1102                         MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1103                         if (eh != null)
1104                                 eh (this, e);
1105                 }
1106
1107                 protected override void OnParentBackColorChanged (EventArgs e)
1108                 {
1109                         base.OnParentBackColorChanged (e);
1110                 }
1111
1112                 protected override void OnResize (EventArgs e)
1113                 {
1114                         LayoutComboBox ();
1115                         if (listbox_ctrl != null)
1116                                 listbox_ctrl.CalcListBoxArea ();
1117                 }
1118
1119                 protected override void OnSelectedIndexChanged (EventArgs e)
1120                 {
1121                         base.OnSelectedIndexChanged (e);
1122
1123                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1124                         if (eh != null)
1125                                 eh (this, e);
1126                 }
1127
1128                 protected virtual void OnSelectedItemChanged (EventArgs e)
1129                 {
1130                 }
1131
1132                 protected override void OnSelectedValueChanged (EventArgs e)
1133                 {
1134                         base.OnSelectedValueChanged (e);
1135                 }
1136
1137                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1138                 {
1139                         EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1140                         if (eh != null)
1141                                 eh (this, e);
1142                 }
1143
1144                 protected override void RefreshItem (int index)
1145                 {
1146                         if (index < 0 || index >= Items.Count)
1147                                 throw new ArgumentOutOfRangeException ("Index of out range");
1148                                 
1149                         if (draw_mode == DrawMode.OwnerDrawVariable)
1150                                 item_heights.Remove (Items [index]);
1151                 }
1152
1153 #if NET_2_0
1154                 public override void ResetText ()
1155                 {
1156                         Text = String.Empty;
1157                 }
1158                 
1159                 protected override bool ProcessKeyEventArgs (ref Message m)
1160                 {
1161                         return base.ProcessKeyEventArgs (ref m);
1162                 }
1163
1164                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1165                 protected override void OnKeyDown (KeyEventArgs e)
1166                 {
1167                         base.OnKeyDown (e);
1168                 }
1169
1170                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1171                 protected override void OnValidating (CancelEventArgs e)
1172                 {
1173                         base.OnValidating (e);
1174                 }
1175
1176                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1177                 protected override void OnTextChanged (EventArgs e)
1178                 {
1179                         base.OnTextChanged (e);
1180                 }
1181
1182 #if NET_2_0
1183                 protected virtual void OnTextUpdate (EventArgs e)
1184                 {
1185                         EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1186                         if (eh != null)
1187                                 eh (this, e);
1188                 }
1189 #endif
1190                 protected override void OnMouseLeave (EventArgs e)
1191                 {
1192 #if NET_2_0
1193                         if (flat_style == FlatStyle.Popup)
1194                                 Invalidate ();
1195 #endif
1196                         base.OnMouseLeave (e);
1197                 }
1198                 
1199                 protected override void OnMouseEnter (EventArgs e)
1200                 {
1201 #if NET_2_0
1202                         if (flat_style == FlatStyle.Popup)
1203                                 Invalidate ();
1204 #endif
1205                         base.OnMouseEnter (e);
1206                 }
1207 #endif
1208
1209                 public void Select (int start, int length)
1210                 {
1211                         if (start < 0)
1212                                 throw new ArgumentException ("Start cannot be less than zero");
1213                                 
1214                         if (length < 0)
1215                                 throw new ArgumentException ("length cannot be less than zero");
1216                                 
1217                         if (dropdown_style == ComboBoxStyle.DropDownList)
1218                                 return;
1219
1220                         textbox_ctrl.Select (start, length);
1221                 }
1222
1223                 public void SelectAll ()
1224                 {
1225                         if (dropdown_style == ComboBoxStyle.DropDownList)
1226                                 return;
1227
1228                         if (textbox_ctrl != null) {
1229                                 textbox_ctrl.ShowSelection = true;
1230                                 textbox_ctrl.SelectAll ();
1231                         }
1232                 }
1233
1234                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1235                 {
1236                         if ((specified & BoundsSpecified.Height) != 0) {
1237                                 requested_height = height;
1238
1239                                 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1240                                         if (IntegralHeight) {
1241                                                 int border = ThemeEngine.Current.Border3DSize.Height;
1242                                                 int lb_height = height - PreferredHeight - 2;
1243                                                 if (lb_height - 2 * border > ItemHeight) {
1244                                                         int partial = (lb_height - 2 * border) % ItemHeight;
1245                                                         height -= partial;
1246                                                 } else
1247                                                         height = PreferredHeight;
1248                                         }
1249                                 } else
1250                                         height = PreferredHeight;
1251                         }
1252
1253                         base.SetBoundsCore (x, y, width, height, specified);
1254                 }
1255
1256                 protected override void SetItemCore (int index, object value)
1257                 {
1258                         if (index < 0 || index >= Items.Count)
1259                                 return;
1260
1261                         Items[index] = value;
1262                 }
1263
1264                 protected override void SetItemsCore (IList value)
1265                 {
1266                         BeginUpdate ();
1267                         try {
1268                                 Items.Clear ();
1269                                 Items.AddRange (value);
1270                         } finally {
1271                                 EndUpdate ();
1272                         }
1273                 }
1274
1275                 public override string ToString ()
1276                 {
1277                         return base.ToString () + ", Items.Count:" + Items.Count;
1278                 }
1279
1280                 protected override void WndProc (ref Message m)
1281                 {
1282                         switch ((Msg) m.Msg) {
1283                         case Msg.WM_KEYUP:
1284                         case Msg.WM_KEYDOWN:
1285                                 Keys keys = (Keys) m.WParam.ToInt32 ();
1286                                 if (keys == Keys.Up || keys == Keys.Down)
1287                                         break;
1288                                 goto case Msg.WM_CHAR;
1289                         case Msg.WM_CHAR:
1290                                 if (textbox_ctrl != null)
1291                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1292                                 break;
1293                         case Msg.WM_MOUSE_LEAVE:
1294                                 Point location = PointToClient (Control.MousePosition);
1295                                 if (ClientRectangle.Contains (location))
1296                                         return;
1297                                 break;
1298                         default:
1299                                 break;
1300                         }
1301                         base.WndProc (ref m);
1302                 }
1303
1304                 #endregion Public Methods
1305
1306                 #region Private Methods
1307 #if NET_2_0
1308                 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1309                         if(auto_complete_source == AutoCompleteSource.CustomSource) {
1310                                 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1311                         }
1312                 }
1313 #endif
1314
1315                 internal override bool InternalCapture {
1316                         get { return Capture; }
1317                         set {}
1318                 }
1319                 
1320                 void LayoutComboBox ()
1321                 {
1322                         int border = ThemeEngine.Current.Border3DSize.Width;
1323
1324                         text_area = ClientRectangle;
1325                         text_area.Height = PreferredHeight;
1326                         
1327                         listbox_area = ClientRectangle;
1328                         listbox_area.Y = text_area.Bottom + 3;
1329                         listbox_area.Height -= (text_area.Height + 2);
1330
1331                         Rectangle prev_button_area = button_area;
1332
1333                         if (DropDownStyle == ComboBoxStyle.Simple)
1334                                 button_area = Rectangle.Empty;
1335                         else {
1336                                 button_area = text_area;
1337                                 button_area.X = text_area.Right - button_width - border;
1338                                 button_area.Y = text_area.Y + border;
1339                                 button_area.Width = button_width;
1340                                 button_area.Height = text_area.Height - 2 * border;
1341 #if NET_2_0
1342                                 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1343                                         button_area.Inflate (1, 1);
1344                                         button_area.X += 2;
1345                                         button_area.Width -= 2;
1346                                 }
1347 #endif
1348                         }
1349
1350                         if (button_area != prev_button_area) {
1351                                 prev_button_area.Y -= border;
1352                                 prev_button_area.Width += border;
1353                                 prev_button_area.Height += 2 * border;
1354                                 Invalidate (prev_button_area);
1355                                 Invalidate (button_area);
1356                         }
1357
1358                         if (textbox_ctrl != null) {
1359                                 textbox_ctrl.Location = new Point (text_area.X + border, text_area.Y + border);
1360                                 textbox_ctrl.Width = text_area.Width - button_area.Width - border * 2;
1361                                 textbox_ctrl.Height = text_area.Height - border * 2;
1362                         }
1363
1364                         if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1365                                 listbox_ctrl.Location = listbox_area.Location;
1366                                 listbox_ctrl.CalcListBoxArea ();
1367                         }
1368                 }
1369
1370                 private void CreateComboListBox ()
1371                 {
1372                         listbox_ctrl = new ComboListBox (this);
1373                         listbox_ctrl.HighlightedIndex = SelectedIndex;
1374                 }
1375                 
1376                 internal void Draw (Rectangle clip, Graphics dc)
1377                 {
1378                         Theme theme = ThemeEngine.Current;
1379                         FlatStyle style = FlatStyle.Standard;
1380                         bool is_flat = false;
1381
1382 #if NET_2_0
1383                         style = this.FlatStyle;
1384                         is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1385 #endif
1386
1387                         if (DropDownStyle == ComboBoxStyle.Simple)
1388                                 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1389
1390                         if (style == FlatStyle.Popup && (is_entered || Focused)) {
1391                                 Rectangle area = text_area;
1392                                 area.Height -= 1;
1393                                 area.Width -= 1;
1394                                 dc.DrawRectangle (theme.ResPool.GetPen (SystemColors.ControlDark), area);
1395                                 dc.DrawLine (theme.ResPool.GetPen (SystemColors.ControlDark), button_area.X - 1, button_area.Top, button_area.X - 1, button_area.Bottom);
1396                         }
1397                         if (!is_flat && clip.IntersectsWith (text_area))
1398                                 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1399
1400                         int border = theme.Border3DSize.Width;
1401
1402                         // No edit control, we paint the edit ourselves
1403                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1404                                 DrawItemState state = DrawItemState.None;
1405                                 Rectangle item_rect = text_area;
1406                                 item_rect.X += border;
1407                                 item_rect.Y += border;
1408                                 item_rect.Width -= (button_area.Width + 2 * border);
1409                                 item_rect.Height -= 2 * border;
1410                                 
1411                                 if (Focused) {
1412                                         state = DrawItemState.Selected;
1413                                         state |= DrawItemState.Focus;
1414                                 }
1415                                 
1416                                 state |= DrawItemState.ComboBoxEdit;
1417                                 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1418                         }
1419                         
1420                         if (show_dropdown_button) {
1421                                 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1422
1423                                 if (!is_enabled)
1424                                         button_state = ButtonState.Inactive;
1425                                 
1426                                 if (is_flat) {
1427                                         theme.DrawFlatStyleComboButton (dc, button_area, button_state);
1428                                 } else {
1429                                         theme.CPDrawComboButton (dc, button_area, button_state);
1430                                 }
1431                         }
1432                 }
1433
1434                 internal void DropDownListBox ()
1435                 {
1436                         if (DropDownStyle == ComboBoxStyle.Simple)
1437                                 return;
1438                         
1439                         if (listbox_ctrl == null)
1440                                 CreateComboListBox ();
1441
1442                         listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1443
1444                         if (listbox_ctrl.ShowWindow ())
1445                                 dropped_down = true;
1446
1447                         button_state = ButtonState.Pushed;
1448                         if (dropdown_style == ComboBoxStyle.DropDownList)
1449                                 Invalidate (text_area);
1450                 }
1451                 
1452                 internal void DropDownListBoxFinished ()
1453                 {
1454                         if (DropDownStyle == ComboBoxStyle.Simple)
1455                                 return;
1456                                 
1457                         button_state = ButtonState.Normal;
1458                         Invalidate (button_area);
1459                         dropped_down = false;
1460 #if NET_2_0
1461                         OnDropDownClosed (EventArgs.Empty);
1462 #endif
1463                 }
1464                 
1465                 private int FindStringCaseInsensitive (string search)
1466                 {
1467                         if (search.Length == 0) {
1468                                 return -1;
1469                         }
1470                         
1471                         for (int i = 0; i < Items.Count; i++) 
1472                         {
1473                                 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1474                                         return i;
1475                         }
1476
1477                         return -1;
1478                 }
1479
1480                 internal int FindStringCaseInsensitive (string search, int start_index)
1481                 {
1482                         if (search.Length == 0) {
1483                                 return -1;
1484                         }
1485
1486                         for (int i = 0; i < Items.Count; i++) {
1487                                 int index = (i + start_index) % Items.Count;
1488                                 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1489                                         return index;
1490                         }
1491
1492                         return -1;
1493                 }
1494
1495                 private void OnKeyDownCB(object sender, KeyEventArgs e)
1496                 {
1497                         if (Items.Count == 0)
1498                                 return;
1499
1500                         switch (e.KeyCode) 
1501                         {
1502                                 case Keys.Up:
1503                                         SelectedIndex = Math.Max(SelectedIndex-1, 0);
1504                                         break;
1505         
1506                                 case Keys.Down:
1507                                         SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1508                                         break;
1509                                 
1510                                 case Keys.PageUp:
1511                                         if (listbox_ctrl != null)
1512                                                 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1513                                         break;
1514         
1515                                 case Keys.PageDown:
1516                                         if (listbox_ctrl != null)
1517                                                 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1518                                         break;
1519                                 
1520                                 default:
1521                                         break;
1522                         }
1523                 }
1524                 
1525                 void OnMouseDownCB (object sender, MouseEventArgs e)
1526                 {
1527                         Rectangle area;
1528                         if (DropDownStyle == ComboBoxStyle.DropDownList)
1529                                 area = ClientRectangle;
1530                         else
1531                                 area = button_area;
1532
1533                         if (area.Contains (e.X, e.Y)) {
1534                                 DropDownListBox ();
1535                                 Invalidate (button_area);
1536                                 Update ();
1537                         }
1538                         Capture = true;
1539                 }
1540
1541                 void OnMouseMoveCB (object sender, MouseEventArgs e)
1542                 {
1543                         if (DropDownStyle == ComboBoxStyle.Simple)
1544                                 return;
1545
1546                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1547                                 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1548                                 if (listbox_ctrl.ClientRectangle.Contains (location))
1549                                         listbox_ctrl.Capture = true;
1550                         }
1551                 }
1552
1553                 void OnMouseUpCB (object sender, MouseEventArgs e)
1554                 {
1555                         Capture = false;
1556                         OnClick (EventArgs.Empty);
1557
1558                         if (dropped_down)
1559                                 listbox_ctrl.Capture = true;
1560                 }
1561
1562                 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1563                 {
1564                         if (Items.Count == 0)
1565                                 return;
1566
1567                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1568                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1569                                 listbox_ctrl.Scroll (-lines);
1570                         } else {
1571                                 int lines = me.Delta / 120;
1572                                 int index = SelectedIndex - lines;
1573                                 if (index < 0)
1574                                         index = 0;
1575                                 else if (index >= Items.Count)
1576                                         index = Items.Count - 1;
1577                                 SelectedIndex = index;
1578                         }
1579                 }
1580
1581                 internal override void OnPaintInternal (PaintEventArgs pevent)
1582                 {
1583                         if (suspend_ctrlupdate)
1584                                 return;
1585
1586                         Draw (ClientRectangle, pevent.Graphics);
1587                 }
1588                 
1589                 private void OnTextBoxClick (object sender, EventArgs e)
1590                 {
1591                         OnClick (e);
1592                 }
1593
1594                 private void OnTextChangedEdit (object sender, EventArgs e)
1595                 {
1596                         if (process_textchanged_event == false)
1597                                 return; 
1598                                 
1599                         OnTextChanged (EventArgs.Empty);
1600
1601                         int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1602                         
1603                         if (item == -1)
1604                                 return;
1605
1606                         // TODO:  THIS IS BROKEN-ISH
1607                         // I don't think we should hilight, and setting the top item does weirdness
1608                         // when there is no scrollbar
1609                         
1610                         if (listbox_ctrl != null) {
1611                                 listbox_ctrl.SetTopItem (item);
1612                                 listbox_ctrl.HighlightedIndex = item;
1613                         }
1614
1615                         base.Text = textbox_ctrl.Text;
1616                 }
1617                 
1618                 internal void SetControlText (string s)
1619                 {
1620                         process_textchanged_event = false;
1621                         textbox_ctrl.Text = s;
1622                         process_textchanged_event = true;
1623                 }
1624                 
1625                 void UpdateComboBoxBounds ()
1626                 {
1627                         if (requested_height == -1)
1628                                 return;
1629
1630                         // Save the requested height since set bounds can destroy it
1631                         int save_height = requested_height;
1632                         SetBounds (0, 0, 0, requested_height, BoundsSpecified.Height);
1633                         requested_height = save_height;
1634                 }
1635
1636                 private void UpdatedItems ()
1637                 {
1638                         if (listbox_ctrl != null) {
1639                                 listbox_ctrl.UpdateLastVisibleItem ();
1640                                 listbox_ctrl.CalcListBoxArea ();
1641                                 listbox_ctrl.Refresh ();
1642                         }
1643                 }
1644
1645                 #endregion Private Methods
1646
1647                 [ListBindableAttribute (false)]
1648                 public class ObjectCollection : IList, ICollection, IEnumerable
1649                 {
1650
1651                         private ComboBox owner;
1652                         internal ArrayList object_items = new ArrayList ();
1653
1654                         public ObjectCollection (ComboBox owner)
1655                         {
1656                                 this.owner = owner;
1657                         }
1658
1659                         #region Public Properties
1660                         public int Count {
1661                                 get { return object_items.Count; }
1662                         }
1663
1664                         public bool IsReadOnly {
1665                                 get { return false; }
1666                         }
1667
1668                         [Browsable (false)]
1669                         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1670                         public virtual object this [int index] {
1671                                 get {
1672                                         if (index < 0 || index >= Count)
1673                                                 throw new ArgumentOutOfRangeException ("Index of out range");
1674
1675                                         return object_items[index];
1676                                 }
1677                                 set {
1678                                         if (index < 0 || index >= Count)
1679                                                 throw new ArgumentOutOfRangeException ("Index of out range");
1680                                         if (value == null)
1681                                                 throw new ArgumentNullException ("value");
1682
1683                                         object_items[index] = value;
1684                                         if (owner.listbox_ctrl != null)
1685                                                 owner.listbox_ctrl.InvalidateItem (index);
1686                                         if (index == owner.SelectedIndex) {
1687                                                 if (owner.textbox_ctrl == null)
1688                                                         owner.Refresh ();
1689                                                 else
1690                                                         owner.textbox_ctrl.SelectedText = value.ToString ();
1691                                         }
1692                                 }
1693                         }
1694
1695                         bool ICollection.IsSynchronized {
1696                                 get { return false; }
1697                         }
1698
1699                         object ICollection.SyncRoot {
1700                                 get { return this; }
1701                         }
1702
1703                         bool IList.IsFixedSize {
1704                                 get { return false; }
1705                         }
1706
1707                         #endregion Public Properties
1708                         
1709                         #region Public Methods
1710                         public int Add (object item)
1711                         {
1712                                 int idx;
1713
1714                                 idx = AddItem (item);
1715                                 owner.UpdatedItems ();
1716                                 return idx;
1717                         }
1718
1719                         public void AddRange (object[] items)
1720                         {
1721                                 if (items == null)
1722                                         throw new ArgumentNullException ("items");
1723
1724                                 foreach (object mi in items)
1725                                         AddItem (mi);
1726                                         
1727                                 owner.UpdatedItems ();
1728                         }
1729
1730                         public void Clear ()
1731                         {
1732                                 owner.selected_index = -1;
1733                                 object_items.Clear ();
1734                                 owner.UpdatedItems ();
1735                                 owner.Refresh ();
1736                         }
1737                         
1738                         public bool Contains (object value)
1739                         {
1740                                 if (value == null)
1741                                         throw new ArgumentNullException ("value");
1742
1743                                 return object_items.Contains (value);
1744                         }
1745
1746                         public void CopyTo (object[] dest, int arrayIndex)
1747                         {
1748                                 object_items.CopyTo (dest, arrayIndex);
1749                         }
1750
1751                         void ICollection.CopyTo (Array dest, int index)
1752                         {
1753                                 object_items.CopyTo (dest, index);
1754                         }
1755
1756                         public IEnumerator GetEnumerator ()
1757                         {
1758                                 return object_items.GetEnumerator ();
1759                         }
1760
1761                         int IList.Add (object item)
1762                         {
1763                                 return Add (item);
1764                         }
1765
1766                         public int IndexOf (object value)
1767                         {
1768                                 if (value == null)
1769                                         throw new ArgumentNullException ("value");
1770
1771                                 return object_items.IndexOf (value);
1772                         }
1773
1774                         public void Insert (int index,  object item)
1775                         {
1776                                 if (index < 0 || index > Count)
1777                                         throw new ArgumentOutOfRangeException ("Index of out range");
1778                                 if (item == null)
1779                                         throw new ArgumentNullException ("item");
1780                                 
1781                                 owner.BeginUpdate ();
1782                                 
1783                                 if (owner.Sorted)
1784                                         AddItem (item);
1785                                 else
1786                                         object_items.Insert (index, item);
1787                                 
1788                                 owner.EndUpdate ();     // Calls UpdatedItems
1789                         }
1790
1791                         public void Remove (object value)
1792                         {
1793                                 if (value == null)
1794                                         return;
1795
1796                                 if (IndexOf (value) == owner.SelectedIndex)
1797                                         owner.SelectedIndex = -1;
1798                                 
1799                                 RemoveAt (IndexOf (value));
1800                         }
1801
1802                         public void RemoveAt (int index)
1803                         {
1804                                 if (index < 0 || index >= Count)
1805                                         throw new ArgumentOutOfRangeException ("Index of out range");
1806                                         
1807                                 if (index == owner.SelectedIndex)
1808                                         owner.SelectedIndex = -1;
1809
1810                                 object_items.RemoveAt (index);
1811                                 owner.UpdatedItems ();
1812                         }
1813                         #endregion Public Methods
1814
1815                         #region Private Methods
1816                         private int AddItem (object item)
1817                         {
1818                                 if (item == null)
1819                                         throw new ArgumentNullException ("item");
1820
1821                                 if (owner.Sorted) {
1822                                         int index = 0;
1823                                         foreach (object o in object_items) {
1824                                                 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
1825                                                         object_items.Insert (index, item);
1826                                                         return index;
1827                                                 }
1828                                                 index++;
1829                                         }
1830                                 }
1831                                 object_items.Add (item);
1832                                 return object_items.Count - 1;
1833                         }
1834                         
1835                         internal void AddRange (IList items)
1836                         {
1837                                 foreach (object mi in items)
1838                                         AddItem (mi);
1839                                 
1840                                 owner.UpdatedItems ();
1841                         }
1842
1843                         internal void Sort ()
1844                         {
1845                                 object_items.Sort ();
1846                         }
1847
1848                         #endregion Private Methods
1849                 }
1850
1851                 internal class ComboTextBox : TextBox {
1852
1853                         private ComboBox owner;
1854
1855                         public ComboTextBox (ComboBox owner)
1856                         {
1857                                 this.owner = owner;
1858                                 ShowSelection = false;
1859                         }
1860
1861                         internal void SetSelectable (bool selectable)
1862                         {
1863                                 SetStyle (ControlStyles.Selectable, selectable);
1864                         }
1865
1866                         internal void ActivateCaret (bool active)
1867                         {
1868                                 if (active)
1869                                         document.CaretHasFocus ();
1870                                 else
1871                                         document.CaretLostFocus ();
1872                         }
1873                         
1874 #if NET_2_0
1875                         internal override void OnTextUpdate ()
1876                         {
1877                                 base.OnTextUpdate ();
1878                                 owner.OnTextUpdate (EventArgs.Empty);
1879                         }
1880 #endif
1881                         
1882                         protected override void OnGotFocus (EventArgs e)
1883                         {
1884                                 owner.Select (false, true);
1885                         }
1886
1887                         protected override void OnLostFocus (EventArgs e)
1888                         {
1889                                 owner.Select (false, true);
1890                         }
1891                 }
1892
1893                 internal class ComboListBox : Control
1894                 {
1895                         private ComboBox owner;
1896                         private VScrollBarLB vscrollbar_ctrl;
1897                         private int top_item;                   /* First item that we show the in the current page */
1898                         private int last_item;                  /* Last visible item */
1899                         internal int page_size;                 /* Number of listbox items per page */
1900                         private Rectangle textarea_drawable;    /* Rectangle of the drawable text area */
1901                         
1902                         internal enum ItemNavigation
1903                         {
1904                                 First,
1905                                 Last,
1906                                 Next,
1907                                 Previous,
1908                                 NextPage,
1909                                 PreviousPage,
1910                         }
1911                         
1912                         class VScrollBarLB : VScrollBar
1913                         {
1914                                 public VScrollBarLB ()
1915                                 {
1916                                 }
1917                                 
1918                                 internal override bool InternalCapture {
1919                                         get { return Capture; }
1920                                         set { }
1921                                 }
1922
1923                                 public void FireMouseDown (MouseEventArgs e) 
1924                                 {
1925                                         if (!Visible) 
1926                                                 return;
1927
1928                                         e = TranslateEvent (e);
1929                                         OnMouseDown (e);
1930                                 }
1931                                 
1932                                 public void FireMouseUp (MouseEventArgs e) 
1933                                 {
1934                                         if (!Visible)
1935                                                 return;
1936
1937                                         e = TranslateEvent (e);
1938                                         OnMouseUp (e);
1939                                 }
1940                                 
1941                                 public void FireMouseMove (MouseEventArgs e) 
1942                                 {
1943                                         if (!Visible)
1944                                                 return;
1945
1946                                         e = TranslateEvent (e);
1947                                         OnMouseMove (e);
1948                                 }
1949                                 
1950                                 MouseEventArgs TranslateEvent (MouseEventArgs e)
1951                                 {
1952                                         Point loc = PointToClient (Control.MousePosition);
1953                                         return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1954                                 }
1955                         }
1956
1957                         public ComboListBox (ComboBox owner)
1958                         {
1959                                 this.owner = owner;
1960                                 top_item = 0;
1961                                 last_item = 0;
1962                                 page_size = 0;
1963
1964                                 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
1965
1966                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1967                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1968
1969                                 this.is_visible = false;
1970                                 Hwnd.ObjectFromHandle (this.Handle).no_activate = true;
1971
1972                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1973                                         InternalBorderStyle = BorderStyle.Fixed3D;
1974                                 else
1975                                         InternalBorderStyle = BorderStyle.FixedSingle;
1976                         }
1977
1978                         protected override CreateParams CreateParams
1979                         {
1980                                 get {
1981                                         CreateParams cp = base.CreateParams;
1982                                         if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
1983                                                 return cp;
1984
1985                                         cp.Style ^= (int) WindowStyles.WS_CHILD;
1986                                         cp.Style |= (int) WindowStyles.WS_POPUP;
1987                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
1988                                         return cp;
1989                                 }
1990                         }
1991
1992                         protected override void Select (bool directed, bool forward)
1993                         {
1994                                 // Do nothing, we never want to be selected
1995                         }
1996
1997                         internal override bool InternalCapture {
1998                                 get {
1999                                         return Capture;
2000                                 }
2001
2002                                 set {
2003                                 }
2004                         }
2005
2006                         #region Private Methods
2007
2008                         // Calcs the listbox area
2009                         internal void CalcListBoxArea ()
2010                         {
2011                                 int width, height;
2012                                 bool show_scrollbar = false;
2013                                 
2014                                 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2015                                         Rectangle area = owner.listbox_area;
2016                                         width = area.Width;
2017                                         height = area.Height;
2018                                 }
2019                                 else { // DropDown or DropDownList
2020                                         
2021                                         width = owner.DropDownWidth;
2022                                         int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2023                                         
2024                                         if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2025                                                 height = 0;
2026                                                 for (int i = 0; i < count; i++) {
2027                                                         height += owner.GetItemHeight (i);
2028                                                 }
2029                                                 
2030                                         } else  {
2031 #if NET_2_0
2032                                                 height = owner.DropDownHeight;
2033 #else           
2034                                                 height = owner.ItemHeight * count;
2035 #endif
2036                                         }
2037                                 }
2038                                 
2039                                 page_size = height / owner.ItemHeight;
2040
2041                                 if (owner.Items.Count <= owner.MaxDropDownItems) {
2042                                         if (vscrollbar_ctrl != null)
2043                                                 vscrollbar_ctrl.Visible = false;
2044                                 } else {
2045                                         /* Need vertical scrollbar */
2046                                         if (vscrollbar_ctrl == null) {
2047                                                 vscrollbar_ctrl = new VScrollBarLB ();
2048                                                 vscrollbar_ctrl.Minimum = 0;
2049                                                 vscrollbar_ctrl.SmallChange = 1;
2050                                                 vscrollbar_ctrl.LargeChange = 1;
2051                                                 vscrollbar_ctrl.Maximum = 0;
2052                                                 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2053                                                 Controls.AddImplicit (vscrollbar_ctrl);
2054                                         }
2055                                         
2056                                         vscrollbar_ctrl.Dock = DockStyle.Right;
2057
2058                                         vscrollbar_ctrl.Maximum = owner.Items.Count - 2;
2059                                         int large = (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2060                                         if (large < 0)
2061                                                 large = 0;
2062                                         vscrollbar_ctrl.LargeChange = large;
2063                                         show_scrollbar = vscrollbar_ctrl.Visible = true;
2064
2065                                         int hli = HighlightedIndex;
2066                                         if (hli > 0) {
2067                                                 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2068                                                 vscrollbar_ctrl.Value = hli;
2069                                         }
2070                                 }
2071                                 
2072                                 Size = new Size (width, height);
2073                                 textarea_drawable = ClientRectangle;
2074                                 textarea_drawable.Width = width;
2075                                 textarea_drawable.Height = height;
2076                                 
2077                                 if (vscrollbar_ctrl != null && show_scrollbar)
2078                                         textarea_drawable.Width -= vscrollbar_ctrl.Width;
2079
2080                                 last_item = LastVisibleItem ();
2081                         }
2082
2083                         private void Draw (Rectangle clip, Graphics dc)
2084                         {
2085                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2086
2087                                 if (owner.Items.Count > 0) {
2088                                         
2089                                         for (int i = top_item; i <= last_item; i++) {
2090                                                 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2091
2092                                                 if (!clip.IntersectsWith (item_rect))
2093                                                         continue;
2094
2095                                                 DrawItemState state = DrawItemState.None;
2096
2097                                                 if (i == HighlightedIndex) {
2098                                                         state |= DrawItemState.Selected;
2099                                                         
2100                                                         if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2101                                                                 state |= DrawItemState.Focus;
2102                                                         }
2103                                                 }
2104                                                 
2105                                                 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2106                                                         i, state, owner.ForeColor, owner.BackColor));
2107                                         }
2108                                 }
2109                         }
2110
2111                         int highlighted_index = -1;
2112
2113                         public int HighlightedIndex {
2114                                 get { return highlighted_index; }
2115                                 set { 
2116                                         if (highlighted_index == value)
2117                                                 return;
2118
2119                                         if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2120                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2121                                         highlighted_index = value;
2122                                         if (highlighted_index != -1)
2123                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2124                                 }
2125                         }
2126
2127                         private Rectangle GetItemDisplayRectangle (int index, int top_index)
2128                         {
2129                                 if (index < 0 || index >= owner.Items.Count)
2130                                         throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2131
2132                                 Rectangle item_rect = new Rectangle ();
2133                                 int height = owner.GetItemHeight (index);
2134
2135                                 item_rect.X = 0;
2136                                 item_rect.Width = textarea_drawable.Width;
2137                                 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2138                                         item_rect.Y = 0;
2139                                         for (int i = top_index; i < index; i++)
2140                                                 item_rect.Y += owner.GetItemHeight (i);
2141                                 } else
2142                                         item_rect.Y = height * (index - top_index);
2143
2144                                 item_rect.Height = height;
2145                                 return item_rect;
2146                         }
2147
2148                         public void HideWindow ()
2149                         {
2150                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2151                                         return;
2152                                         
2153                                 Capture = false;
2154                                 Hide ();
2155                                 owner.DropDownListBoxFinished ();
2156                         }
2157
2158                         private int IndexFromPointDisplayRectangle (int x, int y)
2159                         {
2160                                 for (int i = top_item; i <= last_item; i++) {
2161                                         if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2162                                                 return i;
2163                                 }
2164
2165                                 return -1;
2166                         }
2167
2168                         public void InvalidateItem (int index)
2169                         {
2170                                 if (Visible)
2171                                         Invalidate (GetItemDisplayRectangle (index, top_item));
2172                         }
2173
2174                         private int LastVisibleItem ()
2175                         {
2176                                 Rectangle item_rect;
2177                                 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2178                                 int i = 0;
2179                                 
2180                                 for (i = top_item; i < owner.Items.Count; i++) {
2181                                         item_rect = GetItemDisplayRectangle (i, top_item);
2182                                         if (item_rect.Y + item_rect.Height > top_y) {
2183                                                 return i;
2184                                         }
2185                                 }
2186                                 return i - 1;
2187                         }
2188
2189                         public void SetTopItem (int item)
2190                         {
2191                                 if (top_item == item)
2192                                         return;
2193                                 top_item = item;
2194                                 UpdateLastVisibleItem ();
2195                                 Invalidate ();
2196                         }
2197
2198                         bool scrollbar_grabbed = false;
2199
2200                         bool InScrollBar {
2201                                 get {
2202                                         if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2203                                                 return false;
2204
2205                                         return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2206                                 }
2207                         }
2208
2209                         protected override void OnMouseDown (MouseEventArgs e)
2210                         {
2211                                 if (InScrollBar) {
2212                                         vscrollbar_ctrl.FireMouseDown (e);
2213                                         scrollbar_grabbed = true;
2214                                 }
2215                         }
2216
2217                         protected override void OnMouseMove (MouseEventArgs e)
2218                         {
2219                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2220                                         return;
2221
2222                                 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2223                                         vscrollbar_ctrl.FireMouseMove (e);
2224                                         return;
2225                                 }
2226
2227                                 Point pt = PointToClient (Control.MousePosition);
2228                                 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2229
2230                                 if (index != -1)
2231                                         HighlightedIndex = index;
2232                         }
2233                         
2234                         protected override void OnMouseUp (MouseEventArgs e)
2235                         {
2236                                 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2237
2238                                 if (scrollbar_grabbed) {
2239                                         vscrollbar_ctrl.FireMouseUp (e);
2240                                         scrollbar_grabbed = false;
2241                                         if (index != -1)
2242                                                 HighlightedIndex = index;
2243                                         return;
2244                                 }
2245
2246                                 if (index == -1) {
2247                                         HideWindow ();
2248                                         return;
2249                                 }
2250
2251                                 owner.SelectedIndex = index;
2252                                 owner.OnSelectionChangeCommitted (new EventArgs ());
2253                                 HideWindow ();
2254                         }
2255
2256                         internal override void OnPaintInternal (PaintEventArgs pevent)
2257                         {
2258                                 Draw (pevent.ClipRectangle,pevent.Graphics);
2259                         }
2260
2261                         public bool ShowWindow ()
2262                         {
2263                                 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2264                                         return false;
2265
2266                                 HighlightedIndex = owner.SelectedIndex;
2267
2268                                 CalcListBoxArea ();
2269                                 Show ();
2270
2271                                 Refresh ();
2272                                 owner.OnDropDown (EventArgs.Empty);
2273                                 return true;
2274                         }
2275                         
2276                         public void UpdateLastVisibleItem ()
2277                         {
2278                                 last_item = LastVisibleItem ();
2279                         }
2280
2281                         public void Scroll (int delta)
2282                         {
2283                                 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2284                                         return;
2285
2286                                 int max = owner.Items.Count - page_size;
2287
2288                                 int val = vscrollbar_ctrl.Value + delta;
2289                                 if (val > max)
2290                                         val = max;
2291                                 else if (val < vscrollbar_ctrl.Minimum)
2292                                         val = vscrollbar_ctrl.Minimum;
2293                                 vscrollbar_ctrl.Value = val;
2294                         }
2295
2296                         private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2297                         {
2298                                 if (owner.Items.Count == 0)
2299                                         return;
2300
2301                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2302                                 Scroll (-lines);
2303                         }
2304
2305                         // Value Changed
2306                         private void VerticalScrollEvent (object sender, EventArgs e)
2307                         {
2308                                 if (top_item == vscrollbar_ctrl.Value)
2309                                         return;
2310
2311                                 top_item =  vscrollbar_ctrl.Value;
2312                                 UpdateLastVisibleItem ();
2313                                 Invalidate ();
2314                         }
2315                         
2316                         protected override void WndProc(ref Message m) {
2317                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2318                                         if (m.WParam != IntPtr.Zero) {
2319                                                 XplatUI.SetFocus(m.WParam);
2320                                         }
2321                                 }
2322                                 base.WndProc (ref m);
2323                         }
2324
2325                         #endregion Private Methods
2326                 }
2327         }
2328 }