Merge branch 'cecil-light'
[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                 MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
1891                 {
1892                         Point loc = PointToClient (Control.MousePosition);
1893                         return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
1894                 }
1895
1896                 internal override void OnPaintInternal (PaintEventArgs pevent)
1897                 {
1898                         if (suspend_ctrlupdate)
1899                                 return;
1900
1901                         Draw (ClientRectangle, pevent.Graphics);
1902                 }
1903                 
1904                 private void OnTextBoxClick (object sender, EventArgs e)
1905                 {
1906                         OnClick (e);
1907                 }
1908
1909                 private void OnTextChangedEdit (object sender, EventArgs e)
1910                 {
1911                         if (process_textchanged_event == false)
1912                                 return; 
1913
1914                         int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1915                         
1916                         if (item == -1) {
1917                                 // Setting base.Text below will raise this event
1918                                 // if we found something
1919                                 OnTextChanged (EventArgs.Empty);
1920                                 return;
1921                         }
1922                         
1923                         if (listbox_ctrl != null) {
1924                                 // Set as top item
1925                                 if (process_texchanged_autoscroll)
1926                                         listbox_ctrl.EnsureTop (item);
1927                         }
1928
1929                         base.Text = textbox_ctrl.Text;
1930                 }
1931
1932                 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1933                 {
1934                         selected_index = -1;
1935                         if (listbox_ctrl != null)
1936                                 listbox_ctrl.HighlightedIndex = -1;
1937                 }
1938
1939                 internal void SetControlText (string s, bool suppressTextChanged)
1940                 {
1941                         SetControlText (s, suppressTextChanged, false);
1942                 }
1943
1944                 internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
1945                 {
1946                         if (suppressTextChanged)
1947                                 process_textchanged_event = false;
1948                         if (supressAutoScroll)
1949                                 process_texchanged_autoscroll = false;
1950                                 
1951                         textbox_ctrl.Text = s;
1952                         textbox_ctrl.SelectAll ();
1953                         process_textchanged_event = true;
1954                         process_texchanged_autoscroll = true;
1955                 }
1956                 
1957                 void UpdateComboBoxBounds ()
1958                 {
1959                         if (requested_height == -1)
1960                                 return;
1961
1962                         // Save the requested height since set bounds can destroy it
1963                         int save_height = requested_height;
1964                         SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1965                                 BoundsSpecified.Height);
1966                         requested_height = save_height;
1967                 }
1968
1969                 int SnapHeight (int height)
1970                 {
1971                         if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1972                                 if (IntegralHeight) {
1973                                         int border = ThemeEngine.Current.Border3DSize.Height;
1974                                         int lb_height = (height - PreferredHeight - 2) - border * 2;
1975                                         if (lb_height > ItemHeight) {
1976                                                 int partial = (lb_height) % ItemHeight;
1977                                                 height -= partial;
1978                                         } else if (lb_height < ItemHeight)
1979                                                 height = PreferredHeight;
1980                                 }
1981                         } else
1982                                 height = PreferredHeight;
1983
1984                         return height;
1985                 }
1986
1987                 private void UpdatedItems ()
1988                 {
1989                         if (listbox_ctrl != null) {
1990                                 listbox_ctrl.UpdateLastVisibleItem ();
1991                                 listbox_ctrl.CalcListBoxArea ();
1992                                 listbox_ctrl.Refresh ();
1993                         }
1994                 }
1995
1996                 #endregion Private Methods
1997
1998                 [ListBindableAttribute (false)]
1999                 public class ObjectCollection : IList, ICollection, IEnumerable
2000                 {
2001
2002                         private ComboBox owner;
2003                         internal ArrayList object_items = new ArrayList ();
2004                         
2005                         #region UIA Framework Events
2006
2007 #if NET_2_0
2008                         //NOTE:
2009                         //      We are using Reflection to add/remove internal events.
2010                         //      Class ListProvider uses the events.
2011                         //
2012                         //Event used to generate UIA StructureChangedEvent
2013                         static object UIACollectionChangedEvent = new object ();
2014
2015                         internal event CollectionChangeEventHandler UIACollectionChanged {
2016                                 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
2017                                 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
2018                         }
2019                         
2020                         internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
2021                         {
2022                                 CollectionChangeEventHandler eh
2023                                         = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
2024                                 if (eh != null)
2025                                         eh (owner, args);
2026                         }
2027 #endif
2028
2029                         #endregion UIA Framework Events
2030
2031                         public ObjectCollection (ComboBox owner)
2032                         {
2033                                 this.owner = owner;
2034                         }
2035
2036                         #region Public Properties
2037                         public int Count {
2038                                 get { return object_items.Count; }
2039                         }
2040
2041                         public bool IsReadOnly {
2042                                 get { return false; }
2043                         }
2044
2045                         [Browsable (false)]
2046                         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2047                         public virtual object this [int index] {
2048                                 get {
2049                                         if (index < 0 || index >= Count)
2050                                                 throw new ArgumentOutOfRangeException ("index");
2051
2052                                         return object_items[index];
2053                                 }
2054                                 set {
2055                                         if (index < 0 || index >= Count)
2056                                                 throw new ArgumentOutOfRangeException ("index");
2057                                         if (value == null)
2058                                                 throw new ArgumentNullException ("value");
2059
2060 #if NET_2_0
2061                                         //UIA Framework event: Item Removed
2062                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2063 #endif
2064
2065                                         object_items[index] = value;
2066                                         
2067 #if NET_2_0
2068                                         //UIA Framework event: Item Added
2069                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2070 #endif
2071
2072                                         if (owner.listbox_ctrl != null)
2073                                                 owner.listbox_ctrl.InvalidateItem (index);
2074                                         if (index == owner.SelectedIndex) {
2075                                                 if (owner.textbox_ctrl == null)
2076                                                         owner.Refresh ();
2077                                                 else
2078                                                         owner.textbox_ctrl.SelectedText = value.ToString ();
2079                                         }
2080                                 }
2081                         }
2082
2083                         bool ICollection.IsSynchronized {
2084                                 get { return false; }
2085                         }
2086
2087                         object ICollection.SyncRoot {
2088                                 get { return this; }
2089                         }
2090
2091                         bool IList.IsFixedSize {
2092                                 get { return false; }
2093                         }
2094
2095                         #endregion Public Properties
2096                         
2097                         #region Public Methods
2098                         public int Add (object item)
2099                         {
2100                                 int idx;
2101
2102                                 idx = AddItem (item, false);
2103                                 owner.UpdatedItems ();
2104                                 return idx;
2105                         }
2106
2107                         public void AddRange (object[] items)
2108                         {
2109                                 if (items == null)
2110                                         throw new ArgumentNullException ("items");
2111
2112                                 foreach (object mi in items)
2113                                         AddItem (mi, true);
2114
2115                                 if (owner.sorted)
2116                                         Sort ();
2117                                 
2118                                 owner.UpdatedItems ();
2119                         }
2120
2121                         public void Clear ()
2122                         {
2123                                 owner.selected_index = -1;
2124                                 object_items.Clear ();
2125                                 owner.UpdatedItems ();
2126                                 owner.Refresh ();
2127                                 
2128 #if NET_2_0
2129                                 //UIA Framework event: Items list cleared
2130                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2131 #endif
2132                         }
2133                         
2134                         public bool Contains (object value)
2135                         {
2136                                 if (value == null)
2137                                         throw new ArgumentNullException ("value");
2138
2139                                 return object_items.Contains (value);
2140                         }
2141
2142 #if NET_2_0
2143                         public void CopyTo (object [] destination, int arrayIndex)
2144                         {
2145                                 object_items.CopyTo (destination, arrayIndex);
2146                         }
2147
2148                         void ICollection.CopyTo (Array destination, int index)
2149                         {
2150                                 object_items.CopyTo (destination, index);
2151                         }
2152 #else
2153                         public void CopyTo (object [] dest, int arrayIndex)
2154                         {
2155                                 object_items.CopyTo (dest, arrayIndex);
2156                         }
2157
2158                         void ICollection.CopyTo (Array dest, int index)
2159                         {
2160                                 object_items.CopyTo (dest, index);
2161                         }
2162 #endif
2163
2164                         public IEnumerator GetEnumerator ()
2165                         {
2166                                 return object_items.GetEnumerator ();
2167                         }
2168
2169                         int IList.Add (object item)
2170                         {
2171                                 return Add (item);
2172                         }
2173
2174                         public int IndexOf (object value)
2175                         {
2176                                 if (value == null)
2177                                         throw new ArgumentNullException ("value");
2178
2179                                 return object_items.IndexOf (value);
2180                         }
2181
2182                         public void Insert (int index,  object item)
2183                         {
2184                                 if (index < 0 || index > Count)
2185                                         throw new ArgumentOutOfRangeException ("index");
2186                                 if (item == null)
2187                                         throw new ArgumentNullException ("item");
2188                                 
2189                                 owner.BeginUpdate ();
2190                                 
2191                                 if (owner.Sorted)
2192                                         AddItem (item, false);
2193                                 else {
2194                                         object_items.Insert (index, item);
2195 #if NET_2_0
2196                                         //UIA Framework event: Item added
2197                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2198 #endif                                  
2199                                 }
2200                                 
2201                                 owner.EndUpdate ();     // Calls UpdatedItems
2202                         }
2203
2204                         public void Remove (object value)
2205                         {
2206                                 if (value == null)
2207                                         return;
2208
2209                                 if (IndexOf (value) == owner.SelectedIndex)
2210                                         owner.SelectedIndex = -1;
2211                                 
2212                                 RemoveAt (IndexOf (value));
2213                         }
2214
2215                         public void RemoveAt (int index)
2216                         {
2217                                 if (index < 0 || index >= Count)
2218                                         throw new ArgumentOutOfRangeException ("index");
2219                                         
2220                                 if (index == owner.SelectedIndex)
2221                                         owner.SelectedIndex = -1;
2222
2223 #if NET_2_0
2224                                 object removed = object_items [index];
2225 #endif
2226
2227
2228                                 object_items.RemoveAt (index);
2229                                 owner.UpdatedItems ();
2230                                 
2231 #if NET_2_0
2232                                 //UIA Framework event: Item removed
2233                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2234 #endif
2235                         }
2236                         #endregion Public Methods
2237
2238                         #region Private Methods
2239                         private int AddItem (object item, bool suspend)
2240                         {
2241                                 // suspend means do not sort as we put new items in, we will do a
2242                                 // big sort at the end
2243                                 if (item == null)
2244                                         throw new ArgumentNullException ("item");
2245
2246                                 if (owner.Sorted && !suspend) {
2247                                         int index = 0;
2248                                         foreach (object o in object_items) {
2249                                                 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2250                                                         object_items.Insert (index, item);
2251                                                         
2252                                                         // If we added the new item before the selectedindex
2253                                                         // bump the selectedindex by one, behavior differs if
2254                                                         // Handle has not been created.
2255                                                         if (index <= owner.selected_index && owner.IsHandleCreated)
2256                                                                 owner.selected_index++;
2257                                                                 
2258 #if NET_2_0
2259                                                         //UIA Framework event: Item added
2260                                                         OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2261 #endif
2262
2263                                                         return index;
2264                                                 }
2265                                                 index++;
2266                                         }
2267                                 }
2268                                 object_items.Add (item);
2269                                 
2270 #if NET_2_0
2271                                 //UIA Framework event: Item added
2272                                 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2273 #endif
2274                                 
2275                                 return object_items.Count - 1;
2276                         }
2277                         
2278                         internal void AddRange (IList items)
2279                         {
2280                                 foreach (object mi in items)
2281                                         AddItem (mi, false);
2282                                 
2283                                 if (owner.sorted)
2284                                         Sort ();
2285                                 
2286                                 owner.UpdatedItems ();
2287                         }
2288
2289                         internal void Sort ()
2290                         {
2291                                 // If the objects the user put here don't have their own comparer,
2292                                 // use one that compares based on the object's ToString
2293                                 if (object_items.Count > 0 && object_items[0] is IComparer)
2294                                         object_items.Sort ();
2295                                 else
2296                                         object_items.Sort (new ObjectComparer (owner));
2297                         }
2298
2299                         private class ObjectComparer : IComparer
2300                         {
2301                                 private ListControl owner;
2302                                 
2303                                 public ObjectComparer (ListControl owner)
2304                                 {
2305                                         this.owner = owner;
2306                                 }
2307                                 
2308                                 #region IComparer Members
2309                                 public int Compare (object x, object y)
2310                                 {
2311                                         return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2312                                 }
2313                                 #endregion
2314                         }
2315                         #endregion Private Methods
2316                 }
2317
2318                 internal class ComboTextBox : TextBox {
2319
2320                         private ComboBox owner;
2321
2322                         public ComboTextBox (ComboBox owner)
2323                         {
2324                                 this.owner = owner;
2325                                 ShowSelection = false;
2326                                 owner.EnabledChanged += OwnerEnabledChangedHandler;
2327 #if NET_2_0
2328                                 owner.LostFocus += OwnerLostFocusHandler;
2329 #endif
2330                         }
2331
2332                         void OwnerEnabledChangedHandler (object o, EventArgs args)
2333                         {
2334                                 ShowSelection = owner.Focused && owner.Enabled;
2335                         }
2336
2337 #if NET_2_0
2338                         void OwnerLostFocusHandler (object o, EventArgs args)
2339                         {
2340                                 if (IsAutoCompleteAvailable)
2341                                         owner.Text = Text;
2342                         }
2343
2344                         protected override void OnKeyDown (KeyEventArgs args)
2345                         {
2346                                 if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
2347                                         owner.Text = Text;
2348
2349                                 base.OnKeyDown (args);
2350                         }
2351
2352                         internal override void OnAutoCompleteValueSelected (EventArgs args)
2353                         {
2354                                 base.OnAutoCompleteValueSelected (args);
2355                                 owner.Text = Text;
2356                         }
2357 #endif
2358
2359                         internal void SetSelectable (bool selectable)
2360                         {
2361                                 SetStyle (ControlStyles.Selectable, selectable);
2362                         }
2363
2364                         internal void ActivateCaret (bool active)
2365                         {
2366                                 if (active)
2367                                         document.CaretHasFocus ();
2368                                 else
2369                                         document.CaretLostFocus ();
2370                         }
2371                         
2372 #if NET_2_0
2373                         internal override void OnTextUpdate ()
2374                         {
2375                                 base.OnTextUpdate ();
2376                                 owner.OnTextUpdate (EventArgs.Empty);
2377                         }
2378 #endif
2379                         
2380                         protected override void OnGotFocus (EventArgs e)
2381                         {
2382                                 owner.Select (false, true);
2383                         }
2384
2385                         protected override void OnLostFocus (EventArgs e)
2386                         {
2387                                 owner.Select (false, true);
2388                         }
2389
2390                         // We have to pass these events to our owner - MouseMove is not, however.
2391
2392                         protected override void OnMouseDown (MouseEventArgs e)
2393                         {
2394                                 base.OnMouseDown (e);
2395                                 owner.OnMouseDown (owner.TranslateMouseEventArgs (e));
2396                         }
2397
2398                         protected override void OnMouseUp (MouseEventArgs e)
2399                         {
2400                                 base.OnMouseUp (e);
2401                                 owner.OnMouseUp (owner.TranslateMouseEventArgs (e));
2402                         }
2403
2404                         protected override void OnMouseClick (MouseEventArgs e)
2405                         {
2406                                 base.OnMouseClick (e);
2407                                 owner.OnMouseClick (owner.TranslateMouseEventArgs (e));
2408                         }
2409
2410                         protected override void OnMouseDoubleClick (MouseEventArgs e)
2411                         {
2412                                 base.OnMouseDoubleClick (e);
2413                                 owner.OnMouseDoubleClick (owner.TranslateMouseEventArgs (e));
2414                         }
2415
2416                         public override bool Focused {
2417                                 get {
2418                                         return owner.Focused;
2419                                 }
2420                         }
2421                         
2422                         internal override bool ActivateOnShow { get { return false; } }
2423                 }
2424
2425                 internal class ComboListBox : Control
2426                 {
2427                         private ComboBox owner;
2428                         private VScrollBarLB vscrollbar_ctrl;
2429                         private int top_item;                   /* First item that we show the in the current page */
2430                         private int last_item;                  /* Last visible item */
2431                         internal int page_size;                 /* Number of listbox items per page */
2432                         private Rectangle textarea_drawable;    /* Rectangle of the drawable text area */
2433                         
2434                         internal enum ItemNavigation
2435                         {
2436                                 First,
2437                                 Last,
2438                                 Next,
2439                                 Previous,
2440                                 NextPage,
2441                                 PreviousPage,
2442                         }
2443
2444                         #region UIA Framework: Properties
2445
2446                         internal int UIATopItem {
2447                                 get { return top_item; }
2448                         }
2449                         
2450                         internal int UIALastItem {
2451                                 get { return last_item; }
2452                         }
2453
2454                         internal ScrollBar UIAVScrollBar {
2455                                 get { return vscrollbar_ctrl; }
2456                         }
2457
2458                         #endregion
2459                         
2460                         class VScrollBarLB : VScrollBar
2461                         {
2462                                 public VScrollBarLB ()
2463                                 {
2464                                 }
2465                                 
2466                                 internal override bool InternalCapture {
2467                                         get { return Capture; }
2468                                         set { }
2469                                 }
2470
2471                                 public void FireMouseDown (MouseEventArgs e) 
2472                                 {
2473                                         if (!Visible) 
2474                                                 return;
2475
2476                                         e = TranslateEvent (e);
2477                                         OnMouseDown (e);
2478                                 }
2479                                 
2480                                 public void FireMouseUp (MouseEventArgs e) 
2481                                 {
2482                                         if (!Visible)
2483                                                 return;
2484
2485                                         e = TranslateEvent (e);
2486                                         OnMouseUp (e);
2487                                 }
2488                                 
2489                                 public void FireMouseMove (MouseEventArgs e) 
2490                                 {
2491                                         if (!Visible)
2492                                                 return;
2493
2494                                         e = TranslateEvent (e);
2495                                         OnMouseMove (e);
2496                                 }
2497                                 
2498                                 MouseEventArgs TranslateEvent (MouseEventArgs e)
2499                                 {
2500                                         Point loc = PointToClient (Control.MousePosition);
2501                                         return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2502                                 }
2503                         }
2504
2505                         public ComboListBox (ComboBox owner)
2506                         {
2507                                 this.owner = owner;
2508                                 top_item = 0;
2509                                 last_item = 0;
2510                                 page_size = 0;
2511
2512                                 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2513
2514                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2515                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2516
2517                                 this.is_visible = false;
2518
2519                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2520                                         InternalBorderStyle = BorderStyle.Fixed3D;
2521                                 else
2522                                         InternalBorderStyle = BorderStyle.FixedSingle;
2523                         }
2524
2525                         protected override CreateParams CreateParams
2526                         {
2527                                 get {
2528                                         CreateParams cp = base.CreateParams;
2529                                         if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2530                                                 return cp;
2531
2532                                         cp.Style ^= (int)WindowStyles.WS_CHILD;
2533                                         cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2534                                         cp.Style |= (int)WindowStyles.WS_POPUP;
2535                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2536                                         return cp;
2537                                 }
2538                         }
2539
2540                         internal override bool InternalCapture {
2541                                 get {
2542                                         return Capture;
2543                                 }
2544
2545                                 set {
2546                                 }
2547                         }
2548
2549                         internal override bool ActivateOnShow { get { return false; } }
2550                         #region Private Methods
2551
2552                         // Calcs the listbox area
2553                         internal void CalcListBoxArea ()
2554                         {
2555                                 int width, height;
2556                                 bool show_scrollbar;
2557
2558                                 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2559                                         Rectangle area = owner.listbox_area;
2560                                         width = area.Width;
2561                                         height = area.Height;
2562                                         show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
2563
2564                                         // No calculation needed
2565                                         if (height <= 0 || width <= 0)
2566                                                 return;
2567
2568                                 }
2569                                 else { // DropDown or DropDownList
2570                                         
2571                                         width = owner.DropDownWidth;
2572                                         int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2573                                         
2574                                         if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2575                                                 height = 0;
2576                                                 for (int i = 0; i < visible_items_count; i++) {
2577                                                         height += owner.GetItemHeight (i);
2578                                                 }
2579
2580                                                 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2581                                                 
2582                                         } else  {
2583 #if NET_2_0
2584                                                 if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
2585                                                         height = owner.ItemHeight * visible_items_count;
2586                                                         show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2587                                                 } else {
2588                                                         // ignore visible items count, and use manual height instead
2589                                                         height = owner.DropDownHeight;
2590                                                         show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
2591                                                 }
2592 #else           
2593                                                 height = owner.ItemHeight * visible_items_count;
2594                                                 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2595 #endif
2596                                         }
2597                                 }
2598                                 
2599                                 page_size = Math.Max (height / owner.ItemHeight, 1);
2600
2601                                 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2602                                 if (!show_scrollbar) {
2603
2604                                         if (vscrollbar_ctrl != null)
2605                                                 vscrollbar_ctrl.Visible = false;
2606                                         if (dropdown_style != ComboBoxStyle.Simple)
2607                                                 height = owner.ItemHeight * owner.items.Count;
2608                                 } else {
2609                                         /* Need vertical scrollbar */
2610                                         if (vscrollbar_ctrl == null) {
2611                                                 vscrollbar_ctrl = new VScrollBarLB ();
2612                                                 vscrollbar_ctrl.Minimum = 0;
2613                                                 vscrollbar_ctrl.SmallChange = 1;
2614                                                 vscrollbar_ctrl.LargeChange = 1;
2615                                                 vscrollbar_ctrl.Maximum = 0;
2616                                                 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2617                                                 Controls.AddImplicit (vscrollbar_ctrl);
2618                                         }
2619                                         
2620                                         vscrollbar_ctrl.Dock = DockStyle.Right;
2621
2622                                         vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2623 #if NET_2_0
2624                                         int large = page_size;
2625 #else
2626                                         int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2627 #endif
2628                                         if (large < 1)
2629                                                 large = 1;
2630                                         vscrollbar_ctrl.LargeChange = large;
2631                                         vscrollbar_ctrl.Visible = true;
2632
2633                                         int hli = HighlightedIndex;
2634                                         if (hli > 0) {
2635                                                 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2636                                                 vscrollbar_ctrl.Value = hli;
2637                                         }
2638                                 }
2639                                 
2640                                 Size = new Size (width, height);
2641                                 textarea_drawable = ClientRectangle;
2642                                 textarea_drawable.Width = width;
2643                                 textarea_drawable.Height = height;
2644                                 
2645                                 if (vscrollbar_ctrl != null && show_scrollbar)
2646                                         textarea_drawable.Width -= vscrollbar_ctrl.Width;
2647
2648                                 last_item = LastVisibleItem ();
2649                         }
2650
2651                         private void Draw (Rectangle clip, Graphics dc)
2652                         {
2653                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2654
2655                                 if (owner.Items.Count > 0) {
2656                                         
2657                                         for (int i = top_item; i <= last_item; i++) {
2658                                                 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2659
2660                                                 if (!clip.IntersectsWith (item_rect))
2661                                                         continue;
2662
2663                                                 DrawItemState state = DrawItemState.None;
2664                                                 Color back_color = owner.BackColor;
2665                                                 Color fore_color = owner.ForeColor;
2666
2667                                                 if (i == HighlightedIndex) {
2668                                                         state |= DrawItemState.Selected;
2669                                                         back_color = SystemColors.Highlight;
2670                                                         fore_color = SystemColors.HighlightText;
2671                                                         
2672                                                         if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2673                                                                 state |= DrawItemState.Focus;
2674                                                         }
2675                                                 }
2676
2677                                                 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2678                                                         i, state, fore_color, back_color));
2679                                         }
2680                                 }
2681                         }
2682
2683                         int highlighted_index = -1;
2684
2685                         public int HighlightedIndex {
2686                                 get { return highlighted_index; }
2687                                 set { 
2688                                         if (highlighted_index == value)
2689                                                 return;
2690
2691                                         if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2692                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2693                                         highlighted_index = value;
2694                                         if (highlighted_index != -1)
2695                                                 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2696                                 }
2697                         }
2698
2699                         private Rectangle GetItemDisplayRectangle (int index, int top_index)
2700                         {
2701                                 if (index < 0 || index >= owner.Items.Count)
2702                                         throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2703
2704                                 Rectangle item_rect = new Rectangle ();
2705                                 int height = owner.GetItemHeight (index);
2706
2707                                 item_rect.X = 0;
2708                                 item_rect.Width = textarea_drawable.Width;
2709                                 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2710                                         item_rect.Y = 0;
2711                                         for (int i = top_index; i < index; i++)
2712                                                 item_rect.Y += owner.GetItemHeight (i);
2713                                 } else
2714                                         item_rect.Y = height * (index - top_index);
2715
2716                                 item_rect.Height = height;
2717                                 return item_rect;
2718                         }
2719
2720                         public void HideWindow ()
2721                         {
2722                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2723                                         return;
2724                                         
2725                                 Capture = false;
2726                                 Hide ();
2727                                 owner.DropDownListBoxFinished ();
2728                         }
2729
2730                         private int IndexFromPointDisplayRectangle (int x, int y)
2731                         {
2732                                 for (int i = top_item; i <= last_item; i++) {
2733                                         if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2734                                                 return i;
2735                                 }
2736
2737                                 return -1;
2738                         }
2739
2740                         public void InvalidateItem (int index)
2741                         {
2742                                 if (Visible)
2743                                         Invalidate (GetItemDisplayRectangle (index, top_item));
2744                         }
2745
2746                         public int LastVisibleItem ()
2747                         {
2748                                 Rectangle item_rect;
2749                                 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2750                                 int i = 0;
2751                                 
2752                                 for (i = top_item; i < owner.Items.Count; i++) {
2753                                         item_rect = GetItemDisplayRectangle (i, top_item);
2754                                         if (item_rect.Y + item_rect.Height > top_y) {
2755                                                 return i;
2756                                         }
2757                                 }
2758                                 return i - 1;
2759                         }
2760
2761                         public void SetTopItem (int item)
2762                         {
2763                                 if (top_item == item)
2764                                         return;
2765                                 top_item = item;
2766                                 UpdateLastVisibleItem ();
2767                                 Invalidate ();
2768                         }
2769
2770                         public int FirstVisibleItem ()
2771                         {
2772                                 return top_item;
2773                         }
2774
2775                         public void EnsureTop (int item)
2776                         {
2777                                 if (owner.Items.Count == 0)
2778                                         return;
2779                                 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2780                                         return;
2781
2782                                 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2783                                 if (item > max)
2784                                         item = max;
2785                                 else if (item < vscrollbar_ctrl.Minimum)
2786                                         item = vscrollbar_ctrl.Minimum;
2787
2788                                 vscrollbar_ctrl.Value = item;
2789                         }
2790
2791                         bool scrollbar_grabbed = false;
2792
2793                         bool InScrollBar {
2794                                 get {
2795                                         if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2796                                                 return false;
2797
2798                                         return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2799                                 }
2800                         }
2801
2802                         protected override void OnMouseDown (MouseEventArgs e)
2803                         {
2804                                 if (InScrollBar) {
2805                                         vscrollbar_ctrl.FireMouseDown (e);
2806                                         scrollbar_grabbed = true;
2807                                 }
2808                         }
2809
2810                         protected override void OnMouseMove (MouseEventArgs e)
2811                         {
2812                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2813                                         return;
2814
2815                                 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2816                                         vscrollbar_ctrl.FireMouseMove (e);
2817                                         return;
2818                                 }
2819
2820                                 Point pt = PointToClient (Control.MousePosition);
2821                                 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2822
2823                                 if (index != -1)
2824                                         HighlightedIndex = index;
2825                         }
2826                         
2827                         protected override void OnMouseUp (MouseEventArgs e)
2828                         {
2829                                 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2830
2831                                 if (scrollbar_grabbed) {
2832                                         vscrollbar_ctrl.FireMouseUp (e);
2833                                         scrollbar_grabbed = false;
2834                                         if (index != -1)
2835                                                 HighlightedIndex = index;
2836                                         return;
2837                                 }
2838
2839                                 if (index == -1) {
2840                                         HideWindow ();
2841                                         return;
2842                                 }
2843
2844                                 bool is_change = owner.SelectedIndex != index;
2845                                 
2846                                 owner.SetSelectedIndex (index, true);
2847                                 owner.OnSelectionChangeCommitted (new EventArgs ());
2848                                 
2849                                 // If the user selected the already selected item, SelectedIndex
2850                                 // won't fire these events, but .Net does, so we do it here
2851                                 if (!is_change) {
2852                                         owner.OnSelectedValueChanged (EventArgs.Empty);
2853                                         owner.OnSelectedIndexChanged (EventArgs.Empty);
2854                                 }
2855                                 
2856                                 HideWindow ();
2857                         }
2858
2859                         internal override void OnPaintInternal (PaintEventArgs pevent)
2860                         {
2861                                 Draw (pevent.ClipRectangle,pevent.Graphics);
2862                         }
2863
2864                         public bool ShowWindow ()
2865                         {
2866                                 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2867                                         return false;
2868
2869                                 HighlightedIndex = owner.SelectedIndex;
2870
2871                                 CalcListBoxArea ();
2872                                 Show ();
2873
2874                                 Refresh ();
2875                                 owner.OnDropDown (EventArgs.Empty);
2876                                 return true;
2877                         }
2878                         
2879                         public void UpdateLastVisibleItem ()
2880                         {
2881                                 last_item = LastVisibleItem ();
2882                         }
2883
2884                         public void Scroll (int delta)
2885                         {
2886                                 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2887                                         return;
2888
2889                                 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2890
2891                                 int val = vscrollbar_ctrl.Value + delta;
2892                                 if (val > max)
2893                                         val = max;
2894                                 else if (val < vscrollbar_ctrl.Minimum)
2895                                         val = vscrollbar_ctrl.Minimum;
2896                                 vscrollbar_ctrl.Value = val;
2897                         }
2898
2899                         private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2900                         {
2901                                 if (owner.Items.Count == 0)
2902                                         return;
2903
2904                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2905                                 Scroll (-lines);
2906                         }
2907
2908                         // Value Changed
2909                         private void VerticalScrollEvent (object sender, EventArgs e)
2910                         {
2911                                 if (top_item == vscrollbar_ctrl.Value)
2912                                         return;
2913
2914                                 top_item =  vscrollbar_ctrl.Value;
2915                                 UpdateLastVisibleItem ();
2916                                 Invalidate ();
2917                         }
2918                         
2919                         protected override void WndProc(ref Message m) {
2920                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2921                                         owner.Select (false, true);
2922                                 }
2923                                 base.WndProc (ref m);
2924                         }
2925
2926                         #endregion Private Methods
2927                 }
2928         }
2929 }