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