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