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