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