* ComboBox.cs: Send the mouse down to the scrollbar.
[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 //
26 // NOT COMPLETE
27
28 using System;
29 using System.Drawing;
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Reflection;
33 using System.ComponentModel.Design;
34 using System.ComponentModel.Design.Serialization;
35 using System.Runtime.InteropServices;
36
37
38 namespace System.Windows.Forms
39 {
40
41         [DefaultProperty("Items")]
42         [DefaultEvent("SelectedIndexChanged")]
43         [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44         public class ComboBox : ListControl
45         {
46                 private DrawMode draw_mode = DrawMode.Normal;
47                 private ComboBoxStyle dropdown_style = (ComboBoxStyle)(-1);
48                 private int dropdown_width = -1;                
49                 private object selected_item = null;
50                 internal ObjectCollection items = null;
51                 private bool suspend_ctrlupdate;
52                 private int maxdrop_items = 8;                  
53                 private bool integral_height = true;
54                 private bool sorted;
55                 private int max_length;
56                 private ComboListBox listbox_ctrl;              
57                 private ComboTextBox textbox_ctrl;
58                 private bool process_textchanged_event = true;
59                 private bool item_height_specified = false;
60                 private int item_height;
61                 private int requested_height = -1;
62                 private Hashtable item_heights;
63                 private bool show_dropdown_button = false;
64                 private ButtonState button_state = ButtonState.Normal;
65                 private bool dropped_down;
66                 private Rectangle text_area;
67                 private Rectangle button_area;
68                 private Rectangle listbox_area;
69                 private const int button_width = 16;
70
71                 [ComVisible(true)]
72                 public class ChildAccessibleObject : AccessibleObject {
73                         private ComboBox        owner;
74                         private IntPtr          handle;
75
76                         public ChildAccessibleObject (ComboBox owner, IntPtr handle) {
77                                 this.owner = owner;
78                                 this.handle = handle;
79                         }
80
81                         public override string Name {
82                                 get {
83                                         return base.Name;
84                                 }
85                         }
86                 }
87
88                 public ComboBox ()
89                 {
90                         items = new ObjectCollection (this);
91                         DropDownStyle = ComboBoxStyle.DropDown;
92                         item_height = FontHeight + 2;
93                         BackColor = ThemeEngine.Current.ColorWindow;
94                         border_style = BorderStyle.None;
95
96                         /* Events */
97                         MouseDown += new MouseEventHandler (OnMouseDownCB);
98                         MouseUp += new MouseEventHandler (OnMouseUpCB);
99                         MouseMove += new MouseEventHandler (OnMouseMoveCB);
100                         KeyDown +=new KeyEventHandler(OnKeyDownCB);
101                 }
102
103                 #region events
104                 
105                 [Browsable (false)]
106                 [EditorBrowsable (EditorBrowsableState.Never)]
107                 public new event EventHandler BackgroundImageChanged {
108                         add { base.BackgroundImageChanged += value; }
109                         remove { base.BackgroundImageChanged -= value; }
110                 }
111                 
112                 public event DrawItemEventHandler DrawItem;             
113                 public event EventHandler DropDown;             
114                 public event EventHandler DropDownStyleChanged;         
115                 public event MeasureItemEventHandler MeasureItem;
116                 
117                 [Browsable (false)]
118                 [EditorBrowsable (EditorBrowsableState.Never)]
119                 public new event PaintEventHandler Paint {
120                         add { base.Paint += value; }
121                         remove { base.Paint -= value; }
122                 }
123                 
124                 public event EventHandler SelectedIndexChanged;         
125                 public event EventHandler SelectionChangeCommitted;
126                 #endregion Events
127
128                 #region Public Properties
129                 public override Color BackColor {
130                         get { return base.BackColor; }
131                         set {
132                                 if (base.BackColor == value)
133                                         return;
134
135                                 base.BackColor = value;
136                                 Refresh ();
137                         }
138                 }
139
140                 [Browsable (false)]
141                 [EditorBrowsable (EditorBrowsableState.Never)]
142                 public override Image BackgroundImage {
143                         get { return base.BackgroundImage; }
144                         set {
145                                 if (base.BackgroundImage == value)
146                                         return;
147
148                                 base.BackgroundImage = value;
149                                 Refresh ();
150                         }
151                 }
152
153                 protected override CreateParams CreateParams {
154                         get { return base.CreateParams;}
155                 }
156
157                 protected override Size DefaultSize {
158                         get { return new Size (121, 21); }
159                 }
160
161                 [RefreshProperties(RefreshProperties.Repaint)]
162                 [DefaultValue (DrawMode.Normal)]
163                 public DrawMode DrawMode {
164                         get { return draw_mode; }
165
166                         set {
167                                 if (!Enum.IsDefined (typeof (DrawMode), value))
168                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
169
170                                 if (draw_mode == value)
171                                         return;
172
173                                 if (draw_mode == DrawMode.OwnerDrawVariable)
174                                         item_heights = null;
175                                 draw_mode = value;
176                                 if (draw_mode == DrawMode.OwnerDrawVariable)
177                                         item_heights = new Hashtable ();
178                                 Refresh ();
179                         }
180                 }
181
182                 [DefaultValue (ComboBoxStyle.DropDown)]
183                 [RefreshProperties(RefreshProperties.Repaint)]
184                 public ComboBoxStyle DropDownStyle {
185                         get { return dropdown_style; }
186
187                         set {
188                 
189                                 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
190                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
191
192                                 if (dropdown_style == value)
193                                         return;                                 
194                                                                         
195                                 if (dropdown_style == ComboBoxStyle.Simple) {
196                                         if (listbox_ctrl != null) {                                             
197                                                 Controls.RemoveImplicit (listbox_ctrl);
198                                                 listbox_ctrl.Dispose ();                                                
199                                                 listbox_ctrl = null;
200                                         }
201                                 }
202
203                                 dropdown_style = value;                                 
204                                 
205                                 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
206                                         Controls.RemoveImplicit (textbox_ctrl);
207                                         textbox_ctrl.Dispose ();                                                
208                                         textbox_ctrl = null;                                            
209                                 }                               
210
211                                 if (dropdown_style == ComboBoxStyle.Simple) {
212                                         show_dropdown_button = false;                                   
213                                         
214                                         CreateComboListBox ();
215
216                                         if (IsHandleCreated)
217                                                 Controls.AddImplicit (listbox_ctrl);
218                                 } else {
219                                         show_dropdown_button = true;
220                                         button_state = ButtonState.Normal;
221                                 }                               
222         
223                                 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
224                                         textbox_ctrl = new ComboTextBox (this);
225                                         if (selected_item != null)
226                                                 textbox_ctrl.Text = GetItemText (selected_item);
227                                         textbox_ctrl.BorderStyle = BorderStyle.None;
228                                         textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
229
230                                         if (IsHandleCreated == true) {
231                                                 Controls.AddImplicit (textbox_ctrl);
232                                         }
233                                 }
234                                 
235                                 OnDropDownStyleChanged (EventArgs.Empty);
236                                 
237                                 Layout ();
238                                 UpdateBounds ();
239                                 Refresh ();
240                         }
241                 }
242
243                 public int DropDownWidth {
244                         get { 
245                                 if (dropdown_width == -1)
246                                         return Width;
247                                         
248                                 return dropdown_width; 
249                         }
250                         set {
251                                 if (dropdown_width == value)
252                                         return;
253                                         
254                                 if (value < 1)
255                                         throw new ArgumentException ("The DropDownWidth value is less than one");
256
257                                 dropdown_width = value;                         
258                         }
259                 }
260                 
261                 [Browsable (false)]
262                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]              
263                 public bool DroppedDown {
264                         get { 
265                                 if (dropdown_style == ComboBoxStyle.Simple)                             
266                                         return true;
267                                 
268                                 return dropped_down;
269                         }
270                         set {
271                                 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
272                                         return;
273                                         
274                                 if (value) 
275                                         DropDownListBox ();
276                                 else
277                                         listbox_ctrl.Hide ();
278                         }
279                 }               
280
281                 public override bool Focused {
282                         get { return base.Focused; }
283                 }
284
285                 public override Color ForeColor {
286                         get { return base.ForeColor; }
287                         set {
288                                 if (base.ForeColor == value)
289                                         return;
290
291                                 base.ForeColor = value;
292                                 Refresh ();
293                         }
294                 }
295
296                 [DefaultValue (true)]
297                 [Localizable (true)]            
298                 public bool IntegralHeight {
299                         get { return integral_height; }
300                         set {
301                                 if (integral_height == value)
302                                         return;
303
304                                 integral_height = value;
305                                 UpdateBounds ();
306                                 Refresh ();
307                         }
308                 }
309
310                 [Localizable (true)]
311                 public int ItemHeight {
312                         get {
313                                 if (item_height == -1) {
314                                         SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
315                                         item_height = (int) sz.Height;
316                                 }
317                                 return item_height;
318                         }
319                         set {
320                                 if (value < 0)
321                                         throw new ArgumentException ("The item height value is less than zero");
322
323                                 item_height_specified = true;
324                                 item_height = value;
325                                 if (IntegralHeight)
326                                         UpdateBounds ();
327                                 Layout ();
328                                 Refresh ();
329                         }
330                 }
331
332                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
333                 [Localizable (true)]
334                 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]                
335                 public ComboBox.ObjectCollection Items {
336                         get { return items; }
337                 }
338
339                 [DefaultValue (8)]
340                 [Localizable (true)]
341                 public int MaxDropDownItems {
342                         get { return maxdrop_items; }
343                         set {
344                                 if (maxdrop_items == value)
345                                         return;
346
347                                 maxdrop_items = value;
348                         }
349                 }
350
351                 [DefaultValue (0)]
352                 [Localizable (true)]
353                 public int MaxLength {
354                         get { return max_length; }
355                         set {
356                                 if (max_length == value)
357                                         return;
358
359                                 max_length = value;
360                                 
361                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
362                                         
363                                         if (value < 0) {
364                                                 value = 0;
365                                         }
366                                         
367                                         textbox_ctrl.MaxLength = value;
368                                 }                       
369                         }
370                 }
371
372                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
373                 [Browsable (false)]             
374                 public int PreferredHeight {
375                         get {
376                                 return ItemHeight + 5;
377                         }
378                 }
379
380                 [Browsable (false)]
381                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
382                 public override int SelectedIndex {
383                         get { return Items.IndexOf (selected_item); }
384                         set {
385                                 if (value <= -2 || value >= Items.Count)
386                                         throw new ArgumentOutOfRangeException ("Index of out range");
387
388                                 object item = null;
389                                 if (value != -1)
390                                         item = Items [value];
391
392                                 SelectedItem = item;
393                         }
394                 }
395
396                 [Browsable (false)]
397                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
398                 [Bindable(true)]
399                 public object SelectedItem {
400                         get { return selected_item; }
401                         set {                           
402                                 if (selected_item == value)
403                                         return;
404
405                                 selected_item = value;
406                                 
407                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
408                                         if (selected_item == null)
409                                                 SetControlText("");
410                                         else
411                                                 SetControlText (GetItemText (selected_item));
412                                 }
413
414                                 OnSelectedValueChanged (new EventArgs ());
415                                 OnSelectedIndexChanged  (new EventArgs ());
416                                 OnSelectedItemChanged (new EventArgs ());
417                                 if (DropDownStyle == ComboBoxStyle.DropDownList)
418                                         Invalidate ();
419
420                                 if (listbox_ctrl != null)
421                                         listbox_ctrl.HighlightedItem = value;
422                         }
423                 }
424                 
425                 [Browsable (false)]
426                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
427                 public string SelectedText {
428                         get {
429                                 if (dropdown_style == ComboBoxStyle.DropDownList)
430                                         return "";
431                                         
432                                 return textbox_ctrl.SelectedText;
433                         }
434                         set {
435                                 if (dropdown_style == ComboBoxStyle.DropDownList)
436                                         return;
437                                 
438                                 textbox_ctrl.SelectedText = value;
439                         }
440                 }
441
442                 [Browsable (false)]
443                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
444                 public int SelectionLength {
445                         get {
446                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
447                                         return 0;
448                                 
449                                 return textbox_ctrl.SelectionLength;
450                         }
451                         set {
452                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
453                                         return;
454                                         
455                                 if (textbox_ctrl.SelectionLength == value)
456                                         return;
457                                         
458                                 textbox_ctrl.SelectionLength = value;
459                         }
460                 }
461
462                 [Browsable (false)]
463                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
464                 public int SelectionStart {
465                         get { 
466                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
467                                         return 0;                                       
468                                 
469                                 return textbox_ctrl.SelectionStart;                             
470                         }
471                         set {
472                                 if (dropdown_style == ComboBoxStyle.DropDownList) 
473                                         return;
474                                 
475                                 if (textbox_ctrl.SelectionStart == value)
476                                         return;                                 
477                                 
478                                 textbox_ctrl.SelectionStart = value;
479                         }
480                 }
481
482                 [DefaultValue (false)]
483                 public bool Sorted {
484                         get { return sorted; }
485
486                         set {
487                                 if (sorted == value)
488                                         return;
489
490                                 sorted = value;
491                         }
492                 }
493
494                 [Bindable (true)]
495                 [Localizable (true)]
496                 public override string Text {
497                         get {
498                                 if (dropdown_style != ComboBoxStyle.DropDownList) {
499                                         if (textbox_ctrl != null) {
500                                                 return textbox_ctrl.Text;
501                                         }
502                                 }
503
504                                 if (SelectedItem != null)
505                                         return GetItemText (SelectedItem);
506                                                                 
507                                 return base.Text;                               
508                         }
509                         set {                           
510                                 if (value == null) {
511                                         SelectedIndex = -1;
512                                         return;
513                                 }
514                                 
515                                 int index = FindString (value);
516                                 
517                                 if (index != -1) {
518                                         SelectedIndex = index;
519                                         return;                                 
520                                 }
521                                 
522                                 if (dropdown_style != ComboBoxStyle.DropDownList)
523                                         textbox_ctrl.Text = GetItemText (value);
524                         }
525                 }
526
527                 #endregion Public Properties
528
529                 #region Public Methods
530                 protected virtual void AddItemsCore (object[] value)
531                 {
532                         
533                 }
534
535                 public void BeginUpdate ()
536                 {
537                         suspend_ctrlupdate = true;
538                 }
539
540                 protected override void Dispose (bool disposing)
541                 {                                               
542                         if (disposing) {
543                                 if (listbox_ctrl != null) {
544                                         listbox_ctrl.Dispose ();
545                                         Controls.RemoveImplicit (listbox_ctrl);
546                                         listbox_ctrl = null;
547                                 }                       
548                         
549                                 if (textbox_ctrl != null) {
550                                         Controls.RemoveImplicit (textbox_ctrl);
551                                         textbox_ctrl.Dispose ();
552                                         textbox_ctrl = null;
553                                 }                       
554                         }
555                         
556                         base.Dispose (disposing);
557                 }
558
559                 public void EndUpdate ()
560                 {
561                         suspend_ctrlupdate = false;
562                         UpdatedItems ();
563                         Refresh ();
564                 }
565
566                 public int FindString (string s)
567                 {
568                         return FindString (s, -1);
569                 }
570
571                 public int FindString (string s, int startIndex)
572                 {
573                         if (Items.Count == 0) 
574                                 return -1; // No exception throwing if empty
575
576                         if (startIndex < -1 || startIndex >= Items.Count - 1)
577                                 throw new  ArgumentOutOfRangeException ("Index of out range");
578
579                         startIndex++;
580                         for (int i = startIndex; i < Items.Count; i++) {
581                                 if ((GetItemText (Items[i])).StartsWith (s))
582                                         return i;
583                         }
584
585                         return -1;
586                 }
587
588                 public int FindStringExact (string s)
589                 {
590                         return FindStringExact (s, -1);
591                 }
592
593                 public int FindStringExact (string s, int startIndex)
594                 {
595                         if (Items.Count == 0) 
596                                 return -1; // No exception throwing if empty
597
598                         if (startIndex < -1 || startIndex >= Items.Count - 1)
599                                 throw new ArgumentOutOfRangeException ("Index of out range");
600
601                         startIndex++;
602                         for (int i = startIndex; i < Items.Count; i++) {
603                                 if ((GetItemText (Items[i])).Equals (s))
604                                         return i;
605                         }
606
607                         return -1;
608                 }
609
610                 public int GetItemHeight (int index)
611                 {       
612                         if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
613
614                                 if (index < 0 || index >= Items.Count )
615                                         throw new ArgumentOutOfRangeException ("The item height value is less than zero");
616                                 
617                                 object item = Items [index];
618                                 if (item_heights.Contains (item))
619                                         return (int) item_heights [item];
620                                 
621                                 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
622                                 OnMeasureItem (args);
623                                 item_heights [item] = args.ItemHeight;
624                                 return args.ItemHeight;
625                         }
626
627                         return ItemHeight;
628                 }
629
630                 protected override bool IsInputKey (Keys keyData)
631                 {
632                         switch (keyData) {
633                         case Keys.Up:
634                         case Keys.Down:
635                         case Keys.Left:
636                         case Keys.Right:
637                         case Keys.PageUp:
638                         case Keys.PageDown:                     
639                                 return true;
640                         
641                         default:                                        
642                                 return false;
643                         }
644                 }
645
646                 protected override void OnBackColorChanged (EventArgs e)
647                 {
648                         base.OnBackColorChanged (e);
649                 }
650
651                 protected override void OnDataSourceChanged (EventArgs e)
652                 {
653                         base.OnDataSourceChanged (e);
654                         BindDataItems ();
655                         
656                         if (DataSource == null || DataManager == null) {
657                                 SelectedIndex = -1;
658                         } 
659                         else {
660                                 SelectedIndex = DataManager.Position;
661                         }
662                 }
663
664                 protected override void OnDisplayMemberChanged (EventArgs e)
665                 {
666                         base.OnDisplayMemberChanged (e);
667
668                         if (DataManager == null || !IsHandleCreated)
669                                return;
670
671                         BindDataItems ();
672                         SelectedIndex = DataManager.Position;
673                 }
674
675                 protected virtual void OnDrawItem (DrawItemEventArgs e)
676                 {
677                         switch (DrawMode) {
678                         case DrawMode.OwnerDrawFixed:
679                         case DrawMode.OwnerDrawVariable:
680                                 if (DrawItem != null)
681                                         DrawItem (this, e);
682                                 break;
683                         default:
684                                 ThemeEngine.Current.DrawComboBoxItem (this, e);
685                                 break;
686                         }
687                 }               
688
689                 protected virtual void OnDropDown (EventArgs e)
690                 {
691                         if (DropDown != null)
692                                 DropDown (this, e);
693                 }
694
695                 protected virtual void OnDropDownStyleChanged (EventArgs e)
696                 {
697                         if (DropDownStyleChanged != null)
698                                 DropDownStyleChanged (this, e);
699                 }
700
701                 protected override void OnFontChanged (EventArgs e)
702                 {
703                         base.OnFontChanged (e);
704
705                         if (textbox_ctrl != null)
706                                 textbox_ctrl.Font = Font;
707                         
708                         if (!item_height_specified) {
709                                 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
710                                 item_height = (int) sz.Height;
711                         }
712
713                         if (IntegralHeight)
714                                 UpdateBounds ();
715
716                         Layout ();
717                 }
718
719                 protected override void OnForeColorChanged (EventArgs e)
720                 {
721                         base.OnForeColorChanged (e);
722                 }
723
724                 [EditorBrowsable(EditorBrowsableState.Advanced)]                
725                 protected override void OnGotFocus (EventArgs e)
726                 {
727                         if (dropdown_style == ComboBoxStyle.DropDownList) {
728                                 // We draw DDL styles manually, so they require a
729                                 // refresh to have their selection drawn
730                                 Invalidate ();
731                         }
732                         
733                         if (textbox_ctrl != null) {
734                                 textbox_ctrl.SetSelectable (false);
735                                 textbox_ctrl.ActivateCaret (true);
736                                 textbox_ctrl.ShowSelection = true;
737                                 textbox_ctrl.SelectAll ();
738                         }
739
740                         base.OnGotFocus (e);
741                 }
742
743                 [EditorBrowsable(EditorBrowsableState.Advanced)]                
744                 protected override void OnLostFocus (EventArgs e)
745                 {
746                         if (dropdown_style == ComboBoxStyle.DropDownList) {
747                                 // We draw DDL styles manually, so they require a
748                                 // refresh to have their selection drawn
749                                 Invalidate ();
750                         }
751
752                         if (listbox_ctrl != null && dropped_down) {
753                                 listbox_ctrl.HideWindow ();
754                         }
755
756                         if (textbox_ctrl != null) {
757                                 textbox_ctrl.SetSelectable (true);
758                                 textbox_ctrl.ActivateCaret (false);
759                                 textbox_ctrl.ShowSelection = false;
760                         }
761
762                         base.OnLostFocus (e);
763                 }
764
765                 protected override void OnHandleCreated (EventArgs e)
766                 {
767                         base.OnHandleCreated (e);
768
769                         if (listbox_ctrl != null)
770                                 Controls.AddImplicit (listbox_ctrl);
771                         
772                         if (textbox_ctrl != null)
773                                 Controls.AddImplicit (textbox_ctrl);
774
775                         Layout ();
776                 }
777
778                 protected override void OnHandleDestroyed (EventArgs e)
779                 {
780                         base.OnHandleDestroyed (e);
781                 }
782
783                 protected override void OnKeyPress (KeyPressEventArgs e)
784                 {
785                         // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
786                         //if (index != -1)
787                         //      SelectedIndex = index;
788
789                         base.OnKeyPress (e);
790                 }
791
792                 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
793                 {
794                         if (MeasureItem != null)
795                                 MeasureItem (this, e);
796                 }
797
798                 protected override void OnParentBackColorChanged (EventArgs e)
799                 {
800                         base.OnParentBackColorChanged (e);
801                 }
802
803                 protected override void OnResize (EventArgs e)
804                 {                       
805                         Layout ();
806                         if (listbox_ctrl != null)
807                                 listbox_ctrl.CalcListBoxArea ();
808                 }
809
810                 protected override void OnSelectedIndexChanged (EventArgs e)
811                 {
812                         base.OnSelectedIndexChanged (e);
813
814                         if (SelectedIndexChanged != null)
815                                 SelectedIndexChanged (this, e);
816                 }
817
818                 protected virtual void OnSelectedItemChanged (EventArgs e)
819                 {
820                         
821                 }
822
823                 protected override void OnSelectedValueChanged (EventArgs e)
824                 {
825                         base.OnSelectedValueChanged (e);
826                 }
827
828                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
829                 {
830                         if (SelectionChangeCommitted != null)
831                                 SelectionChangeCommitted (this, e);
832                 }
833
834                 protected override void RefreshItem (int index)
835                 {
836                         if (index < 0 || index >= Items.Count)
837                                 throw new ArgumentOutOfRangeException ("Index of out range");
838                                 
839                         if (draw_mode == DrawMode.OwnerDrawVariable)
840                                 item_heights.Remove (Items [index]);
841                 }
842
843                 public void Select (int start, int length)
844                 {
845                         if (start < 0)
846                                 throw new ArgumentException ("Start cannot be less than zero");
847                                 
848                         if (length < 0)
849                                 throw new ArgumentException ("length cannot be less than zero");
850                                 
851                         if (dropdown_style == ComboBoxStyle.DropDownList)
852                                 return;
853
854                         textbox_ctrl.Select (start, length);
855                 }
856
857                 public void SelectAll ()
858                 {
859                         if (dropdown_style == ComboBoxStyle.DropDownList)
860                                 return;
861
862                         if (textbox_ctrl != null) {
863                                 textbox_ctrl.ShowSelection = true;
864                                 textbox_ctrl.SelectAll ();
865                         }
866                 }               
867
868                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
869                 {                       
870                         if ((specified & BoundsSpecified.Height) != 0) {
871                                 requested_height = height;
872
873                                 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
874                                         if (IntegralHeight) {
875                                                 int border = ThemeEngine.Current.Border3DSize.Height;
876                                                 int lb_height = height - PreferredHeight - 2;
877                                                 if (lb_height - 2 * border > ItemHeight) {
878                                                         int partial = (lb_height - 2 * border) % ItemHeight;
879                                                         height -= partial;
880                                                 } else
881                                                         height = PreferredHeight;
882                                         }
883                                 } else
884                                         height = PreferredHeight;
885                         }
886
887                         base.SetBoundsCore (x, y, width, height, specified);
888                 }
889
890                 protected override void SetItemCore (int index, object value)
891                 {
892                         if (index < 0 || index >= Items.Count)
893                                 return;
894
895                         Items[index] = value;
896                 }
897
898                 protected override void SetItemsCore (IList value)
899                 {
900                         BeginUpdate ();
901                         try {
902                                 Items.Clear ();
903                                 Items.AddRange (value);
904                         } finally {
905                                EndUpdate ();
906                         }
907                 }
908
909                 public override string ToString ()
910                 {
911                         return base.ToString () + ", Items.Count:" + Items.Count;
912                 }
913
914                 protected override void WndProc (ref Message m)
915                 {
916                         switch ((Msg) m.Msg) {
917                         case Msg.WM_KEYUP:
918                         case Msg.WM_KEYDOWN:
919                                 Keys keys = (Keys) m.WParam.ToInt32 ();
920                                 if (keys == Keys.Up || keys == Keys.Down)
921                                         break;
922                                 goto case Msg.WM_CHAR;
923                         case Msg.WM_CHAR:
924                                 if (textbox_ctrl != null)
925                                         XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
926                                 break;
927                         case Msg.WM_MOUSE_LEAVE:
928                                 Point location = PointToClient (Control.MousePosition);
929                                 if (ClientRectangle.Contains (location))
930                                         return;
931                                 break;
932                         default:
933                                 break;
934                         }
935                         base.WndProc (ref m);
936                 }
937
938                 #endregion Public Methods
939
940                 #region Private Methods
941
942                 internal override bool InternalCapture {
943                         get { return Capture; }
944                         set {}
945                 }
946                 
947                 void Layout ()
948                 {                       
949                         int border = ThemeEngine.Current.Border3DSize.Width;
950
951                         text_area = ClientRectangle;
952                         text_area.Height = PreferredHeight;
953                         
954                         listbox_area = ClientRectangle;
955                         listbox_area.Y = text_area.Bottom + 3;
956                         listbox_area.Height -= (text_area.Height + 2);
957
958                         Rectangle prev_button_area = button_area;
959
960                         if (DropDownStyle == ComboBoxStyle.Simple)
961                                 button_area = Rectangle.Empty;
962                         else {
963                                 button_area = text_area;
964                                 button_area.X = text_area.Right - button_width - border;
965                                 button_area.Y = text_area.Y + border;
966                                 button_area.Width = button_width;
967                                 button_area.Height = text_area.Height - 2 * border;
968                         }
969
970                         if (button_area != prev_button_area) {
971                                 prev_button_area.Y -= border;
972                                 prev_button_area.Width += border;
973                                 prev_button_area.Height += 2 * border;
974                                 Invalidate (prev_button_area);
975                                 Invalidate (button_area);
976                         }
977
978                         if (textbox_ctrl != null) {
979                                 textbox_ctrl.Location = new Point (text_area.X + border, text_area.Y + border);
980                                 textbox_ctrl.Width = text_area.Width - button_area.Width - border * 2;
981                                 textbox_ctrl.Height = text_area.Height - border * 2;
982                         }
983
984                         if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
985                                 listbox_ctrl.Location = listbox_area.Location;
986                                 listbox_ctrl.CalcListBoxArea ();
987                         }
988                 }
989
990                 private void CreateComboListBox ()
991                 {                       
992                         listbox_ctrl = new ComboListBox (this);                 
993                         if (selected_item != null)
994                                 listbox_ctrl.HighlightedItem = selected_item;
995                 }
996                 
997                 internal void Draw (Rectangle clip, Graphics dc)
998                 {                               
999                         Theme theme = ThemeEngine.Current;
1000
1001                         if (DropDownStyle == ComboBoxStyle.Simple)
1002                                 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1003
1004                         if (clip.IntersectsWith (text_area))
1005                                 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1006
1007                         int border = theme.Border3DSize.Width;
1008
1009                         // No edit control, we paint the edit ourselves
1010                         if (dropdown_style == ComboBoxStyle.DropDownList) {
1011                                 DrawItemState state = DrawItemState.None;
1012                                 Rectangle item_rect = text_area;
1013                                 item_rect.X += border;
1014                                 item_rect.Y += border;
1015                                 item_rect.Width -= (button_area.Width + 2 * border);                            
1016                                 item_rect.Height -= 2 * border;                         
1017                                                                 
1018                                 if (Focused) {
1019                                         state = DrawItemState.Selected;
1020                                         state |= DrawItemState.Focus;
1021                                 }
1022                                 
1023                                 state |= DrawItemState.ComboBoxEdit;                            
1024                                 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1025                         }                                               
1026                         
1027                         if (show_dropdown_button) {
1028                                 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1029
1030                                 if (!is_enabled)
1031                                         button_state = ButtonState.Inactive;
1032                                 
1033                                 theme.CPDrawComboButton (dc, button_area, button_state);
1034                         }                       
1035                 }
1036
1037                 internal void DropDownListBox ()
1038                 {
1039                         if (DropDownStyle == ComboBoxStyle.Simple)
1040                                 return;                 
1041                         
1042                         if (listbox_ctrl == null)
1043                                 CreateComboListBox ();
1044
1045                         listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1046                                                 
1047                         if (listbox_ctrl.ShowWindow ())                                 
1048                                 dropped_down = true;                                    
1049                         
1050                         button_state = ButtonState.Pushed;                              
1051                         if (dropdown_style == ComboBoxStyle.DropDownList)
1052                                 Invalidate (text_area);
1053
1054                         OnDropDown (EventArgs.Empty);
1055                 }
1056                 
1057                 internal void DropDownListBoxFinished ()
1058                 {
1059                         if (DropDownStyle == ComboBoxStyle.Simple)
1060                                 return;                 
1061                                 
1062                         button_state = ButtonState.Normal;
1063                         Invalidate (button_area);
1064                         dropped_down = false;
1065                 }
1066                 
1067                 private int FindStringCaseInsensitive (string search)
1068                 {                       
1069                         if (search.Length == 0) {
1070                                 return -1;
1071                         }
1072                         
1073                         for (int i = 0; i < Items.Count; i++) 
1074                         {                               
1075                                 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1076                                         return i;
1077                         }
1078
1079                         return -1;
1080                 }
1081
1082                 internal int FindStringCaseInsensitive (string search, int start_index)
1083                 {
1084                         if (search.Length == 0) {
1085                                 return -1;
1086                         }
1087
1088                         for (int i = 0; i < Items.Count; i++) {
1089                                 int index = (i + start_index) % Items.Count;
1090                                 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1091                                         return index;
1092                         }
1093
1094                         return -1;
1095                 }
1096
1097                 private void OnKeyDownCB(object sender, KeyEventArgs e)
1098                 {
1099                         if (Items.Count == 0)
1100                                 return;
1101
1102                         switch (e.KeyCode) 
1103                         {                       
1104                                 case Keys.Up:
1105                                         SelectedIndex = Math.Max(SelectedIndex-1, 0);
1106                                         break;                          
1107         
1108                                 case Keys.Down:
1109                                         SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1110                                         break;
1111                                 
1112                                 case Keys.PageUp:
1113                                         if (listbox_ctrl != null)
1114                                                 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1115                                         break;                          
1116         
1117                                 case Keys.PageDown:             
1118                                         if (listbox_ctrl != null)               
1119                                                 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1120                                         break;
1121                                 
1122                                 default:
1123                                         break;
1124                         }
1125                 }
1126                 
1127                 void OnMouseDownCB (object sender, MouseEventArgs e)
1128                 {
1129                         Rectangle area;
1130                         if (DropDownStyle == ComboBoxStyle.DropDownList)
1131                                 area = ClientRectangle;
1132                         else
1133                                 area = button_area;
1134
1135                         if (area.Contains (e.X, e.Y)) {
1136                                 DropDownListBox ();                                     
1137                                 Invalidate (button_area);
1138                                 Update ();
1139                         }
1140                         Capture = true;
1141                 }
1142
1143                 void OnMouseMoveCB (object sender, MouseEventArgs e)
1144                 {                       
1145                         if (DropDownStyle == ComboBoxStyle.Simple)
1146                                 return;
1147
1148                         if (listbox_ctrl != null && listbox_ctrl.Visible) {
1149                                 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1150                                 if (listbox_ctrl.ClientRectangle.Contains (location))
1151                                         listbox_ctrl.Capture = true;
1152                         }
1153                 }
1154
1155                 void OnMouseUpCB (object sender, MouseEventArgs e)
1156                 {
1157                         Capture = false;
1158                         OnClick (EventArgs.Empty);
1159
1160                         if (dropped_down)
1161                                 listbox_ctrl.Capture = true;
1162                 }
1163
1164                 internal override void OnPaintInternal (PaintEventArgs pevent)
1165                 {
1166                         if (suspend_ctrlupdate)
1167                                 return;
1168                                 
1169                         Draw (ClientRectangle, pevent.Graphics);                        
1170                 }
1171                 
1172                 private void OnTextChangedEdit (object sender, EventArgs e)
1173                 {
1174                         if (process_textchanged_event == false)
1175                                 return; 
1176                                 
1177                         int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1178                         
1179                         if (item == -1)
1180                                 return;
1181
1182                         // TODO:  THIS IS BROKEN-ISH
1183                         // I don't think we should hilight, and setting the top item does weirdness
1184                         // when there is no scrollbar
1185                         
1186                         if (listbox_ctrl != null) {
1187                                 listbox_ctrl.SetTopItem (item);
1188                                 listbox_ctrl.HighlightedItem = Items [item];
1189                         }
1190
1191                         base.Text = textbox_ctrl.Text;
1192                 }
1193                 
1194                 internal void SetControlText (string s)
1195                 {               
1196                         process_textchanged_event = false;
1197                         textbox_ctrl.Text = s;
1198                         process_textchanged_event = true;
1199                 }
1200                 
1201                 void UpdateBounds ()
1202                 {
1203                         if (requested_height != -1)
1204                                 SetBoundsCore (0, 0, 0, requested_height, BoundsSpecified.Height);
1205                 }
1206
1207                 private void UpdatedItems ()
1208                 {
1209                         if (listbox_ctrl != null) {
1210                                 listbox_ctrl.UpdateLastVisibleItem ();
1211                                 listbox_ctrl.CalcListBoxArea ();
1212                                 listbox_ctrl.Refresh ();
1213                         }
1214                 }
1215
1216                 #endregion Private Methods
1217
1218                 [ListBindableAttribute (false)]
1219                 public class ObjectCollection : IList, ICollection, IEnumerable
1220                 {
1221
1222                         private ComboBox owner;
1223                         internal ArrayList object_items = new ArrayList ();
1224
1225                         public ObjectCollection (ComboBox owner)
1226                         {
1227                                 this.owner = owner;
1228                         }
1229
1230                         #region Public Properties
1231                         public int Count {
1232                                 get { return object_items.Count; }
1233                         }
1234
1235                         public bool IsReadOnly {
1236                                 get { return false; }
1237                         }
1238
1239                         [Browsable (false)]
1240                         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1241                         public virtual object this [int index] {
1242                                 get {
1243                                         if (index < 0 || index >= Count)
1244                                                 throw new ArgumentOutOfRangeException ("Index of out range");
1245
1246                                         return object_items[index];
1247                                 }
1248                                 set {
1249                                         if (index < 0 || index >= Count)
1250                                                 throw new ArgumentOutOfRangeException ("Index of out range");
1251
1252                                         object_items[index] = value;
1253                                 }
1254                         }
1255
1256                         bool ICollection.IsSynchronized {
1257                                 get { return false; }
1258                         }
1259
1260                         object ICollection.SyncRoot {
1261                                 get { return this; }
1262                         }
1263
1264                         bool IList.IsFixedSize {
1265                                 get { return false; }
1266                         }
1267
1268                         #endregion Public Properties
1269                         
1270                         #region Private Properties                      
1271                         internal ArrayList ObjectItems {
1272                                 get { return object_items;}
1273                                 set {
1274                                         object_items = value;
1275                                 }
1276                         }
1277                         
1278                         #endregion Private Properties
1279
1280                         #region Public Methods
1281                         public int Add (object item)
1282                         {
1283                                 int idx;
1284
1285                                 idx = AddItem (item);
1286                                 owner.UpdatedItems ();
1287                                 return idx;
1288                         }
1289
1290                         public void AddRange (object[] items)
1291                         {
1292                                 foreach (object mi in items)
1293                                         AddItem (mi);
1294                                         
1295                                 owner.UpdatedItems ();
1296                         }
1297
1298                         public void Clear ()
1299                         {
1300                                 owner.selected_item = null;
1301                                 object_items.Clear ();
1302                                 owner.UpdatedItems ();
1303                                 owner.Refresh ();
1304                         }
1305                         
1306                         public bool Contains (object obj)
1307                         {
1308                                 return object_items.Contains (obj);
1309                         }
1310
1311                         public void CopyTo (object[] dest, int arrayIndex)
1312                         {
1313                                 object_items.CopyTo (dest, arrayIndex);
1314                         }
1315
1316                         void ICollection.CopyTo (Array dest, int index)
1317                         {
1318                                 object_items.CopyTo (dest, index);
1319                         }
1320
1321                         public IEnumerator GetEnumerator ()
1322                         {
1323                                 return object_items.GetEnumerator ();
1324                         }
1325
1326                         int IList.Add (object item)
1327                         {
1328                                 return Add (item);
1329                         }
1330
1331                         public int IndexOf (object value)
1332                         {
1333                                 return object_items.IndexOf (value);
1334                         }
1335
1336                         public void Insert (int index,  object item)
1337                         {
1338                                 if (index < 0 || index > Count)
1339                                         throw new ArgumentOutOfRangeException ("Index of out range");                                   
1340                                 
1341                                 owner.BeginUpdate ();
1342                                 
1343                                 object_items.Insert (index, item);
1344                                                                                                 
1345                                 owner.EndUpdate ();     // Calls UpdatedItems
1346                         }
1347
1348                         public void Remove (object value)
1349                         {                               
1350                                 if (IndexOf (value) == owner.SelectedIndex)
1351                                         owner.SelectedItem = null;
1352                                 
1353                                 RemoveAt (IndexOf (value));                             
1354                                 
1355                         }
1356
1357                         public void RemoveAt (int index)
1358                         {
1359                                 if (index < 0 || index >= Count)
1360                                         throw new ArgumentOutOfRangeException ("Index of out range");
1361                                         
1362                                 if (index == owner.SelectedIndex)
1363                                         owner.SelectedItem = null;
1364
1365                                 object_items.RemoveAt (index);
1366                                 owner.UpdatedItems ();
1367                         }
1368                         #endregion Public Methods
1369
1370                         #region Private Methods
1371                         private int AddItem (object item)
1372                         {
1373                                 int cnt = object_items.Count;
1374                                 object_items.Add (item);
1375                                 return cnt;
1376                         }
1377                         
1378                         internal void AddRange (IList items)
1379                         {
1380                                 foreach (object mi in items)
1381                                         AddItem (mi);
1382                                                                                 
1383                                 owner.UpdatedItems ();
1384                         }
1385
1386                         #endregion Private Methods
1387                 }
1388
1389                 internal class ComboTextBox : TextBox {
1390
1391                         private ComboBox owner;
1392
1393                         public ComboTextBox (ComboBox owner)
1394                         {
1395                                 this.owner = owner;
1396                                 ShowSelection = false;
1397                         }
1398
1399                         internal void SetSelectable (bool selectable)
1400                         {
1401                                 SetStyle (ControlStyles.Selectable, selectable);
1402                         }
1403
1404                         internal void ActivateCaret (bool active)
1405                         {
1406                                 if (active)
1407                                         document.CaretHasFocus ();
1408                                 else
1409                                         document.CaretLostFocus ();
1410                         }
1411
1412                         internal override void OnGotFocusInternal (EventArgs e)
1413                         {
1414                                 owner.Select (false, true);
1415                         }
1416
1417                         internal override void OnLostFocusInternal (EventArgs e)
1418                         {
1419                                 owner.Select (false, true);
1420                         }                       
1421                 }
1422
1423                 internal class ComboListBox : Control
1424                 {
1425                         private ComboBox owner;                 
1426                         private VScrollBarLB vscrollbar_ctrl;
1427                         private int top_item;                   /* First item that we show the in the current page */
1428                         private int last_item;                  /* Last visible item */
1429                         internal int page_size;                 /* Number of listbox items per page */
1430                         private Rectangle textarea_drawable;    /* Rectangle of the drawable text area */
1431                         
1432                         internal enum ItemNavigation
1433                         {
1434                                 First,
1435                                 Last,
1436                                 Next,
1437                                 Previous,
1438                                 NextPage,
1439                                 PreviousPage,
1440                         }
1441                         
1442                         class VScrollBarLB : VScrollBar
1443                         {
1444                                 public VScrollBarLB ()
1445                                 {                                       
1446                                 }
1447                                 
1448                                 internal override bool InternalCapture {
1449                                         get { return Capture; }
1450                                         set { }
1451                                 }
1452
1453                                 public bool FireMouseDown (MouseEventArgs e) 
1454                                 {
1455                                         if (Visible) {
1456                                                 e = TranslateEvent (e);
1457                                                 if (ClientRectangle.Contains (e.X, e.Y)) {
1458                                                         OnMouseDown (e);
1459                                                         return true;
1460                                                 }
1461                                         }
1462                                         return false;
1463                                 }       
1464                                 
1465                                 public void FireMouseUp (MouseEventArgs e) 
1466                                 {
1467                                         if (Visible) {
1468                                                 e = TranslateEvent (e);
1469                                                 if (ClientRectangle.Contains (e.X, e.Y))
1470                                                         OnMouseUp (e);
1471                                         }
1472                                 }
1473                                 
1474                                 public void FireMouseMove (MouseEventArgs e) 
1475                                 {
1476                                         if (Visible) {
1477                                                 e = TranslateEvent (e);
1478                                                 if (ClientRectangle.Contains (e.X, e.Y))
1479                                                         OnMouseMove (e);
1480                                         }
1481                                 }                       
1482                                 
1483                                 MouseEventArgs TranslateEvent (MouseEventArgs e)
1484                                 {
1485                                         Point loc = PointToClient (Control.MousePosition);
1486                                         return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1487                                 }
1488                         }
1489
1490                         public ComboListBox (ComboBox owner)
1491                         {                                       
1492                                 this.owner = owner;                                                             
1493                                 top_item = 0;
1494                                 last_item = 0;
1495                                 page_size = 0;
1496
1497                                 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);                          
1498
1499                                 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1500                                 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1501
1502                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1503                                         InternalBorderStyle = BorderStyle.Fixed3D;
1504                                 else
1505                                         InternalBorderStyle = BorderStyle.FixedSingle;
1506                         }
1507
1508                         protected override CreateParams CreateParams
1509                         {
1510                                 get {
1511                                         CreateParams cp = base.CreateParams;                                    
1512                                         if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
1513                                                 return cp;
1514
1515                                         cp.Style ^= (int) WindowStyles.WS_CHILD;
1516                                         cp.Style |= (int) WindowStyles.WS_POPUP;
1517                                         cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
1518                                         return cp;
1519                                 }
1520                         }
1521
1522                         protected override void Select (bool directed, bool forward)
1523                         {
1524                                 // Do nothing, we never want to be selected
1525                         }
1526
1527                         internal override bool InternalCapture {
1528                                 get {
1529                                         return Capture;
1530                                 }
1531
1532                                 set {
1533                                 }
1534                         }
1535
1536                         int BorderWidth {
1537                                 get {
1538                                         switch (border_style) {
1539                                         case BorderStyle.Fixed3D:
1540                                                 return ThemeEngine.Current.Border3DSize.Width;
1541                                         default:
1542                                                 return ThemeEngine.Current.BorderSize.Width;
1543                                         }
1544                                 }
1545                         }
1546
1547                         #region Private Methods                 
1548                         // Calcs the listbox area
1549                         internal void CalcListBoxArea ()
1550                         {                               
1551                                 int width, height;
1552                                 bool show_scrollbar = false;
1553                                 
1554                                 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1555                                         Rectangle area = owner.listbox_area;
1556                                         width = area.Width;
1557                                         height = area.Height;
1558                                 }
1559                                 else { // DropDown or DropDownList
1560                                         
1561                                         width = owner.DropDownWidth;
1562                                         int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;                         
1563                                         
1564                                         if (owner.DrawMode == DrawMode.OwnerDrawVariable) {                                             
1565                                                 height = 0;
1566                                                 for (int i = 0; i < count; i++) {
1567                                                         height += owner.GetItemHeight (i);
1568                                                 }
1569                                                 
1570                                         } else  {
1571                                                 height = owner.ItemHeight * count;
1572                                         }
1573                                 }
1574                                 
1575                                 if (owner.Items.Count <= owner.MaxDropDownItems) {                                      
1576                                         if (vscrollbar_ctrl != null)
1577                                                 vscrollbar_ctrl.Visible = false;
1578                                 } else {                                        
1579                                         /* Need vertical scrollbar */
1580                                         if (vscrollbar_ctrl == null) {
1581                                                 vscrollbar_ctrl = new VScrollBarLB ();
1582                                                 vscrollbar_ctrl.Minimum = 0;
1583                                                 vscrollbar_ctrl.SmallChange = 1;
1584                                                 vscrollbar_ctrl.LargeChange = 1;
1585                                                 vscrollbar_ctrl.Maximum = 0;
1586                                                 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1587                                                 Controls.AddImplicit (vscrollbar_ctrl);
1588                                         }
1589                                         
1590                                         vscrollbar_ctrl.Height = height - 2 * BorderWidth;
1591                                                         
1592                                         vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - BorderWidth - 1, 0);
1593
1594                                         vscrollbar_ctrl.Maximum = owner.Items.Count - (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items);
1595                                         show_scrollbar = vscrollbar_ctrl.Visible = true;
1596
1597                                         int hli = HighlightedIndex;
1598                                         if (hli > 0) {
1599                                                 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
1600                                                 vscrollbar_ctrl.Value = hli;
1601                                         }
1602                                 }
1603                                 
1604                                 Size = new Size (width, height);
1605                                 textarea_drawable = ClientRectangle;
1606                                 textarea_drawable.Width = width;
1607                                 textarea_drawable.Height = height;                              
1608                                 
1609                                 if (vscrollbar_ctrl != null && show_scrollbar)
1610                                         textarea_drawable.Width -= vscrollbar_ctrl.Width;
1611
1612                                 last_item = LastVisibleItem ();
1613                                 page_size = textarea_drawable.Height / owner.ItemHeight;
1614                         }                       
1615
1616                         private void Draw (Rectangle clip, Graphics dc)
1617                         {       
1618                                 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
1619
1620                                 if (owner.Items.Count > 0) {
1621                                         
1622                                         for (int i = top_item; i <= last_item; i++) {
1623                                                 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
1624
1625                                                 if (!clip.IntersectsWith (item_rect))
1626                                                         continue;
1627
1628                                                 DrawItemState state = DrawItemState.None;
1629
1630                                                 if (i == HighlightedIndex) {
1631                                                         state |= DrawItemState.Selected;
1632                                                         
1633                                                         if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1634                                                                 state |= DrawItemState.Focus;
1635                                                         }                                                       
1636                                                 }
1637                                                         
1638                                                 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
1639                                                         i, state, owner.ForeColor, owner.BackColor));
1640                                         }
1641                                 }
1642                         }
1643
1644                         public int HighlightedIndex {
1645                                 get { return owner.Items.IndexOf (highlighted_item); }
1646                                 set { 
1647                                         object item = null;
1648                                         if (value != -1)
1649                                                 item = owner.Items [value];
1650                                         HighlightedItem = item; 
1651                                 }
1652                         }
1653
1654                         object highlighted_item = null;
1655
1656                         public object HighlightedItem {
1657                                 get { return highlighted_item; }
1658                                 set {
1659                                         if (highlighted_item == value)
1660                                                 return;
1661                                 
1662                                         int index = owner.Items.IndexOf (highlighted_item);
1663                                         if (index != -1)
1664                                                 Invalidate (GetItemDisplayRectangle (index, top_item));
1665                                         highlighted_item = value;
1666                                         index = owner.Items.IndexOf (highlighted_item);
1667                                         if (index != -1)
1668                                                 Invalidate (GetItemDisplayRectangle (index, top_item));
1669                                 }
1670                         }
1671                         
1672                         private Rectangle GetItemDisplayRectangle (int index, int top_index)
1673                         {
1674                                 if (index < 0 || index >= owner.Items.Count)
1675                                         throw new  ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1676
1677                                 Rectangle item_rect = new Rectangle ();
1678                                 int height = owner.GetItemHeight (index);
1679
1680                                 item_rect.X = 0;
1681                                 item_rect.Width = textarea_drawable.Width;
1682                                 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1683                                         item_rect.Y = 0;
1684                                         for (int i = top_index; i < index; i++)
1685                                                 item_rect.Y += owner.GetItemHeight (i);
1686                                 } else
1687                                         item_rect.Y = height * (index - top_index);
1688
1689                                 item_rect.Height = height;
1690                                 return item_rect;
1691                         }
1692
1693                         public void HideWindow ()
1694                         {
1695                                 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1696                                         return;
1697                                         
1698                                 Capture = false;
1699                                 Hide ();
1700                                 owner.DropDownListBoxFinished ();
1701                         }
1702
1703                         private int IndexFromPointDisplayRectangle (int x, int y)
1704                         {
1705                                 for (int i = top_item; i <= last_item; i++) {
1706                                         if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1707                                                 return i;
1708                                 }
1709
1710                                 return -1;
1711                         }
1712
1713                         private int LastVisibleItem ()
1714                         {
1715                                 Rectangle item_rect;
1716                                 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1717                                 int i = 0;                              
1718                                 
1719                                 for (i = top_item; i < owner.Items.Count; i++) {
1720                                         item_rect = GetItemDisplayRectangle (i, top_item);                              
1721                                         if (item_rect.Y + item_rect.Height > top_y) {
1722                                                 return i;
1723                                         }
1724                                 }
1725                                 return i - 1;
1726                         }
1727
1728                         public void SetTopItem (int item)
1729                         {
1730                                 if (top_item == item)
1731                                         return;
1732                                 top_item = item;
1733                                 UpdateLastVisibleItem ();
1734                                 Refresh ();
1735                         }                       
1736
1737                         protected override void OnMouseDown (MouseEventArgs e)
1738                         {
1739                                 if (vscrollbar_ctrl != null)
1740                                         vscrollbar_ctrl.FireMouseDown (e);
1741                         }
1742
1743                         protected override void OnMouseMove (MouseEventArgs e)
1744                         {                                               
1745                                 Point pt = PointToClient (Control.MousePosition);
1746                                 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
1747
1748                                 if (index != -1) {
1749                                         HighlightedIndex = index;
1750                                         return;
1751                                 }
1752                                 
1753                                 if (vscrollbar_ctrl != null)
1754                                         vscrollbar_ctrl.FireMouseMove (e);
1755                         }
1756                         
1757                         protected override void OnMouseUp (MouseEventArgs e)
1758                         {
1759                                 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1760
1761                                 if (index == -1) {                                      
1762                                         if (vscrollbar_ctrl == null || !vscrollbar_ctrl.FireMouseDown (e))
1763                                                 HideWindow ();
1764                                 } else {
1765                                         owner.OnSelectionChangeCommitted (new EventArgs ());
1766                                         owner.SelectedIndex = index;
1767                                         HighlightedIndex = index;
1768                                         HideWindow ();
1769                                 }
1770
1771                                 if (vscrollbar_ctrl != null)
1772                                         vscrollbar_ctrl.FireMouseUp (e);
1773                         }
1774
1775                         internal override void OnPaintInternal (PaintEventArgs pevent)
1776                         {                               
1777                                 Draw (pevent.ClipRectangle,pevent.Graphics);
1778                         }
1779
1780                         public bool ShowWindow ()
1781                         {
1782                                 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
1783                                         return false;
1784
1785                                 HighlightedIndex = owner.SelectedIndex;
1786
1787                                 CalcListBoxArea ();                             
1788                                 Show ();
1789
1790                                 Refresh ();
1791                                 owner.OnDropDown (EventArgs.Empty);
1792                                 return true;
1793                         }
1794                         
1795                         public void UpdateLastVisibleItem ()
1796                         {
1797                                 last_item = LastVisibleItem ();
1798                         }
1799
1800                         private void Scroll (int delta)
1801                         {
1802                                 if (delta == 0 || !vscrollbar_ctrl.Visible)
1803                                         return;
1804
1805                                 int max = vscrollbar_ctrl.Maximum;//- (page_size) + 1;
1806
1807                                 int val = vscrollbar_ctrl.Value + delta;
1808                                 if (val > max)
1809                                         val = max;
1810                                 else if (val < vscrollbar_ctrl.Minimum)
1811                                         val = vscrollbar_ctrl.Minimum;
1812                                 vscrollbar_ctrl.Value = val;
1813                         }
1814
1815                         private void OnMouseWheelCLB (object sender, MouseEventArgs me)
1816                         {
1817                                 if (owner.Items.Count == 0)
1818                                         return;
1819
1820                                 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1821                                 Scroll (-lines);
1822                         }
1823
1824                         // Value Changed
1825                         private void VerticalScrollEvent (object sender, EventArgs e)
1826                         {                               
1827                                 if (top_item == vscrollbar_ctrl.Value)
1828                                         return;
1829
1830                                 top_item =  vscrollbar_ctrl.Value;
1831                                 UpdateLastVisibleItem ();
1832                                 Refresh ();
1833                         }                       
1834                         
1835                         protected override void WndProc(ref Message m) {
1836                                 if (m.Msg == (int)Msg.WM_SETFOCUS) {
1837                                         if (m.WParam != IntPtr.Zero) {
1838                                                 XplatUI.SetFocus(m.WParam);
1839                                         }
1840                                 }
1841                                 base.WndProc (ref m);
1842                         }
1843
1844                         #endregion Private Methods
1845                 }
1846         }
1847 }
1848