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