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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Mike Kestner <mkestner@novell.com>
25 // Daniel Nauck (dna(at)mono-project(dot)de)
28 using System.Collections;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms
39 [DefaultProperty("Items")]
40 [DefaultEvent("SelectedIndexChanged")]
41 [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [DefaultBindingProperty ("Text")]
44 [ClassInterface (ClassInterfaceType.AutoDispatch)]
47 public class ComboBox : ListControl
49 private DrawMode draw_mode = DrawMode.Normal;
50 private ComboBoxStyle dropdown_style;
51 private int dropdown_width = -1;
52 private int selected_index = -1;
53 private ObjectCollection items;
54 private bool suspend_ctrlupdate;
55 private int maxdrop_items = 8;
56 private bool integral_height = true;
58 private int max_length;
59 private ComboListBox listbox_ctrl;
60 private ComboTextBox textbox_ctrl;
61 private bool process_textchanged_event = true;
62 private bool item_height_specified;
63 private int item_height;
64 private int requested_height = -1;
65 private Hashtable item_heights;
66 private bool show_dropdown_button;
67 private ButtonState button_state = ButtonState.Normal;
68 private bool dropped_down;
69 private Rectangle text_area;
70 private Rectangle button_area;
71 private Rectangle listbox_area;
72 private const int button_width = 16;
74 private AutoCompleteStringCollection auto_complete_custom_source = null;
75 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
76 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
77 private int drop_down_height;
78 private FlatStyle flat_style;
82 public class ChildAccessibleObject : AccessibleObject {
84 public ChildAccessibleObject (ComboBox owner, IntPtr handle)
89 public override string Name {
98 items = new ObjectCollection (this);
99 DropDownStyle = ComboBoxStyle.DropDown;
100 item_height = FontHeight + 2;
101 BackColor = ThemeEngine.Current.ColorWindow;
102 border_style = BorderStyle.None;
105 drop_down_height = 106;
106 flat_style = FlatStyle.Standard;
110 MouseDown += new MouseEventHandler (OnMouseDownCB);
111 MouseUp += new MouseEventHandler (OnMouseUpCB);
112 MouseMove += new MouseEventHandler (OnMouseMoveCB);
113 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
114 KeyDown +=new KeyEventHandler(OnKeyDownCB);
120 [EditorBrowsable (EditorBrowsableState.Never)]
121 public new event EventHandler BackgroundImageChanged {
122 add { base.BackgroundImageChanged += value; }
123 remove { base.BackgroundImageChanged -= value; }
129 [EditorBrowsable (EditorBrowsableState.Never)]
130 public new event EventHandler BackgroundImageLayoutChanged
132 add { base.BackgroundImageLayoutChanged += value; }
133 remove { base.BackgroundImageLayoutChanged -= value; }
137 [EditorBrowsable (EditorBrowsableState.Never)]
138 public new event EventHandler DoubleClick
140 add { base.DoubleClick += value; }
141 remove { base.DoubleClick -= value; }
145 static object DrawItemEvent = new object ();
146 static object DropDownEvent = new object ();
147 static object DropDownStyleChangedEvent = new object ();
148 static object MeasureItemEvent = new object ();
149 static object SelectedIndexChangedEvent = new object ();
150 static object SelectionChangeCommittedEvent = new object ();
152 static object DropDownClosedEvent = new object ();
153 static object TextUpdateEvent = new object ();
156 public event DrawItemEventHandler DrawItem {
157 add { Events.AddHandler (DrawItemEvent, value); }
158 remove { Events.RemoveHandler (DrawItemEvent, value); }
161 public event EventHandler DropDown {
162 add { Events.AddHandler (DropDownEvent, value); }
163 remove { Events.RemoveHandler (DropDownEvent, value); }
166 public event EventHandler DropDownClosed
168 add { Events.AddHandler (DropDownClosedEvent, value); }
169 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
173 public event EventHandler DropDownStyleChanged {
174 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
175 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
178 public event MeasureItemEventHandler MeasureItem {
179 add { Events.AddHandler (MeasureItemEvent, value); }
180 remove { Events.RemoveHandler (MeasureItemEvent, value); }
184 [EditorBrowsable (EditorBrowsableState.Never)]
185 public new event EventHandler PaddingChanged
187 add { base.PaddingChanged += value; }
188 remove { base.PaddingChanged -= value; }
193 [EditorBrowsable (EditorBrowsableState.Never)]
194 public new event PaintEventHandler Paint {
195 add { base.Paint += value; }
196 remove { base.Paint -= value; }
199 public event EventHandler SelectedIndexChanged {
200 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
201 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
204 public event EventHandler SelectionChangeCommitted {
205 add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
206 remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
209 public event EventHandler TextUpdate
211 add { Events.AddHandler (TextUpdateEvent, value); }
212 remove { Events.RemoveHandler (TextUpdateEvent, value); }
218 #region Public Properties
220 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
221 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
223 [EditorBrowsable (EditorBrowsableState.Always)]
225 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
226 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
227 public AutoCompleteStringCollection AutoCompleteCustomSource {
229 if(auto_complete_custom_source == null) {
230 auto_complete_custom_source = new AutoCompleteStringCollection ();
231 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
233 return auto_complete_custom_source;
236 if(auto_complete_custom_source == value)
239 if(auto_complete_custom_source != null) //remove eventhandler from old collection
240 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
242 auto_complete_custom_source = value;
244 if(auto_complete_custom_source != null)
245 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
249 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
251 [EditorBrowsable (EditorBrowsableState.Always)]
252 [DefaultValue (AutoCompleteMode.None)]
253 public AutoCompleteMode AutoCompleteMode {
254 get { return auto_complete_mode; }
256 if(auto_complete_mode == value)
259 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
260 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
262 auto_complete_mode = value;
266 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
268 [EditorBrowsable (EditorBrowsableState.Always)]
269 [DefaultValue (AutoCompleteSource.None)]
270 public AutoCompleteSource AutoCompleteSource {
271 get { return auto_complete_source; }
273 if(auto_complete_source == value)
276 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
277 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
279 auto_complete_source = value;
283 public override Color BackColor {
284 get { return base.BackColor; }
286 if (base.BackColor == value)
288 base.BackColor = value;
294 [EditorBrowsable (EditorBrowsableState.Never)]
295 public override Image BackgroundImage {
296 get { return base.BackgroundImage; }
298 if (base.BackgroundImage == value)
300 base.BackgroundImage = value;
307 [EditorBrowsable (EditorBrowsableState.Never)]
308 public override ImageLayout BackgroundImageLayout {
309 get { return base.BackgroundImageLayout; }
310 set { base.BackgroundImageLayout = value; }
314 protected override CreateParams CreateParams {
315 get { return base.CreateParams;}
319 [DefaultValue ((string)null)]
320 [AttributeProvider (typeof (IListSource))]
321 [RefreshProperties (RefreshProperties.Repaint)]
322 public new object DataSource {
323 get { return base.DataSource; }
324 set { base.DataSource = value; }
328 protected override Size DefaultSize {
329 get { return new Size (121, 21); }
332 [RefreshProperties(RefreshProperties.Repaint)]
333 [DefaultValue (DrawMode.Normal)]
334 public DrawMode DrawMode {
335 get { return draw_mode; }
337 if (!Enum.IsDefined (typeof (DrawMode), value))
338 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
340 if (draw_mode == value)
343 if (draw_mode == DrawMode.OwnerDrawVariable)
346 if (draw_mode == DrawMode.OwnerDrawVariable)
347 item_heights = new Hashtable ();
355 [EditorBrowsable (EditorBrowsableState.Always)]
356 public int DropDownHeight {
358 return drop_down_height;
362 throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
364 drop_down_height = value;
365 IntegralHeight = false;
370 [DefaultValue (ComboBoxStyle.DropDown)]
371 [RefreshProperties(RefreshProperties.Repaint)]
372 public ComboBoxStyle DropDownStyle {
373 get { return dropdown_style; }
375 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
376 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
378 if (dropdown_style == value)
383 if (dropdown_style == ComboBoxStyle.Simple) {
384 if (listbox_ctrl != null) {
385 Controls.RemoveImplicit (listbox_ctrl);
386 listbox_ctrl.Dispose ();
391 dropdown_style = value;
393 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
394 Controls.RemoveImplicit (textbox_ctrl);
395 textbox_ctrl.Dispose ();
399 if (dropdown_style == ComboBoxStyle.Simple) {
400 show_dropdown_button = false;
402 CreateComboListBox ();
404 if (IsHandleCreated) {
405 Controls.AddImplicit (listbox_ctrl);
406 listbox_ctrl.Visible = true;
409 show_dropdown_button = true;
410 button_state = ButtonState.Normal;
413 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
414 textbox_ctrl = new ComboTextBox (this);
415 object selected_item = SelectedItem;
416 if (selected_item != null)
417 textbox_ctrl.Text = GetItemText (selected_item);
418 textbox_ctrl.BorderStyle = BorderStyle.None;
419 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
420 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
421 textbox_ctrl.ContextMenu = ContextMenu;
423 if (IsHandleCreated == true)
424 Controls.AddImplicit (textbox_ctrl);
428 OnDropDownStyleChanged (EventArgs.Empty);
431 UpdateComboBoxBounds ();
436 public int DropDownWidth {
438 if (dropdown_width == -1)
441 return dropdown_width;
444 if (dropdown_width == value)
449 throw new ArgumentOutOfRangeException ("DropDownWidth",
450 "The DropDownWidth value is less than one.");
452 throw new ArgumentException ("The DropDownWidth value is less than one.");
455 dropdown_width = value;
460 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
461 public bool DroppedDown {
463 if (dropdown_style == ComboBoxStyle.Simple)
469 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
475 listbox_ctrl.Hide ();
480 [DefaultValue (FlatStyle.Standard)]
482 public FlatStyle FlatStyle {
483 get { return flat_style; }
485 if (!Enum.IsDefined (typeof (FlatStyle), value))
486 throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
495 public override bool Focused {
496 get { return base.Focused; }
499 public override Color ForeColor {
500 get { return base.ForeColor; }
502 if (base.ForeColor == value)
504 base.ForeColor = value;
509 [DefaultValue (true)]
511 public bool IntegralHeight {
512 get { return integral_height; }
514 if (integral_height == value)
516 integral_height = value;
517 UpdateComboBoxBounds ();
523 public int ItemHeight {
525 if (item_height == -1) {
526 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
527 item_height = (int) sz.Height;
534 throw new ArgumentOutOfRangeException ("ItemHeight",
535 "The item height value is less than one.");
537 throw new ArgumentException ("The item height value is less than one.");
540 item_height_specified = true;
543 UpdateComboBoxBounds ();
549 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
551 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
553 [MergableProperty (false)]
555 public ComboBox.ObjectCollection Items {
556 get { return items; }
561 public int MaxDropDownItems {
562 get { return maxdrop_items; }
564 if (maxdrop_items == value)
566 maxdrop_items = value;
571 public override Size MaximumSize {
572 get { return base.MaximumSize; }
574 base.MaximumSize = new Size (value.Width, 0);
581 public int MaxLength {
582 get { return max_length; }
584 if (max_length == value)
589 if (dropdown_style != ComboBoxStyle.DropDownList) {
593 textbox_ctrl.MaxLength = value;
599 public override Size MinimumSize {
600 get { return base.MinimumSize; }
602 base.MinimumSize = new Size (value.Width, 0);
606 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
607 [EditorBrowsable (EditorBrowsableState.Never)]
609 public new Padding Padding {
610 get { return base.Padding; }
611 set { base.Padding = value; }
615 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
617 public int PreferredHeight {
619 return ItemHeight + 6;
624 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
625 public override int SelectedIndex {
626 get { return selected_index; }
628 if (selected_index == value)
631 if (value <= -2 || value >= Items.Count)
632 throw new ArgumentOutOfRangeException ("SelectedIndex");
633 selected_index = value;
635 if (dropdown_style != ComboBoxStyle.DropDownList) {
639 SetControlText (GetItemText (Items [value]));
642 if (DropDownStyle == ComboBoxStyle.DropDownList)
645 if (listbox_ctrl != null)
646 listbox_ctrl.HighlightedIndex = value;
648 OnSelectedValueChanged (new EventArgs ());
649 OnSelectedIndexChanged (new EventArgs ());
650 OnSelectedItemChanged (new EventArgs ());
655 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
657 public object SelectedItem {
658 get { return selected_index == -1 ? null : Items [selected_index]; }
660 object item = selected_index == -1 ? null : Items [selected_index];
663 SelectedIndex = Items.IndexOf (value);
668 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
669 public string SelectedText {
671 if (dropdown_style == ComboBoxStyle.DropDownList)
674 return textbox_ctrl.SelectedText;
677 if (dropdown_style == ComboBoxStyle.DropDownList)
679 textbox_ctrl.SelectedText = value;
684 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
685 public int SelectionLength {
687 if (dropdown_style == ComboBoxStyle.DropDownList)
690 int result = textbox_ctrl.SelectionLength;
691 return result == -1 ? 0 : result;
694 if (dropdown_style == ComboBoxStyle.DropDownList)
696 if (textbox_ctrl.SelectionLength == value)
698 textbox_ctrl.SelectionLength = value;
703 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
704 public int SelectionStart {
706 if (dropdown_style == ComboBoxStyle.DropDownList)
708 return textbox_ctrl.SelectionStart;
711 if (dropdown_style == ComboBoxStyle.DropDownList)
713 if (textbox_ctrl.SelectionStart == value)
715 textbox_ctrl.SelectionStart = value;
719 [DefaultValue (false)]
721 get { return sorted; }
736 public override string Text {
738 if (dropdown_style != ComboBoxStyle.DropDownList) {
739 if (textbox_ctrl != null) {
740 return textbox_ctrl.Text;
744 if (SelectedItem != null)
745 return GetItemText (SelectedItem);
751 if (SelectedIndex == -1) {
752 if (dropdown_style != ComboBoxStyle.DropDownList)
760 // do nothing if value exactly matches text of selected item
761 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
764 // find exact match using case-sensitive comparison, and if does
765 // not result in any match then use case-insensitive comparison
766 int index = FindStringExact (value, -1, false);
768 index = FindStringExact (value, -1, true);
771 SelectedIndex = index;
775 if (dropdown_style != ComboBoxStyle.DropDownList)
776 textbox_ctrl.Text = GetItemText (value);
780 #endregion Public Properties
782 #region Public Methods
784 [Obsolete ("This method has been deprecated")]
786 protected virtual void AddItemsCore (object[] value)
791 public void BeginUpdate ()
793 suspend_ctrlupdate = true;
797 protected override AccessibleObject CreateAccessibilityInstance ()
799 return base.CreateAccessibilityInstance ();
802 protected override void CreateHandle ()
804 base.CreateHandle ();
808 protected override void Dispose (bool disposing)
811 if (listbox_ctrl != null) {
812 listbox_ctrl.Dispose ();
813 Controls.RemoveImplicit (listbox_ctrl);
817 if (textbox_ctrl != null) {
818 Controls.RemoveImplicit (textbox_ctrl);
819 textbox_ctrl.Dispose ();
824 base.Dispose (disposing);
827 public void EndUpdate ()
829 suspend_ctrlupdate = false;
834 public int FindString (string s)
836 return FindString (s, -1);
839 public int FindString (string s, int startIndex)
841 if (s == null || Items.Count == 0)
845 if (startIndex < -1 || startIndex >= Items.Count)
847 if (startIndex < -1 || startIndex >= Items.Count - 1)
849 throw new ArgumentOutOfRangeException ("startIndex");
853 if (i == (Items.Count - 1))
858 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
860 if (i == (Items.Count - 1))
862 } while (i != startIndex);
867 public int FindStringExact (string s)
869 return FindStringExact (s, -1);
872 public int FindStringExact (string s, int startIndex)
874 return FindStringExact (s, startIndex, true);
877 private int FindStringExact (string s, int startIndex, bool ignoreCase)
879 if (s == null || Items.Count == 0)
883 if (startIndex < -1 || startIndex >= Items.Count)
885 if (startIndex < -1 || startIndex >= Items.Count - 1)
887 throw new ArgumentOutOfRangeException ("startIndex");
891 if (i == (Items.Count - 1))
896 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
898 if (i == (Items.Count - 1))
900 } while (i != startIndex);
905 public int GetItemHeight (int index)
907 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
909 if (index < 0 || index >= Items.Count )
910 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
912 object item = Items [index];
913 if (item_heights.Contains (item))
914 return (int) item_heights [item];
916 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
917 OnMeasureItem (args);
918 item_heights [item] = args.ItemHeight;
919 return args.ItemHeight;
925 protected override bool IsInputKey (Keys keyData)
927 switch (keyData & ~Keys.Modifiers) {
943 protected override void OnBackColorChanged (EventArgs e)
945 base.OnBackColorChanged (e);
947 if (textbox_ctrl != null)
948 textbox_ctrl.BackColor = BackColor;
951 protected override void OnDataSourceChanged (EventArgs e)
953 base.OnDataSourceChanged (e);
956 if (DataSource == null || DataManager == null) {
960 SelectedIndex = DataManager.Position;
964 protected override void OnDisplayMemberChanged (EventArgs e)
966 base.OnDisplayMemberChanged (e);
968 if (DataManager == null || !IsHandleCreated)
972 SelectedIndex = DataManager.Position;
975 protected virtual void OnDrawItem (DrawItemEventArgs e)
978 case DrawMode.OwnerDrawFixed:
979 case DrawMode.OwnerDrawVariable:
980 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
985 ThemeEngine.Current.DrawComboBoxItem (this, e);
990 protected virtual void OnDropDown (EventArgs e)
992 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
998 protected virtual void OnDropDownClosed (EventArgs e)
1000 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1006 protected virtual void OnDropDownStyleChanged (EventArgs e)
1008 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1013 protected override void OnFontChanged (EventArgs e)
1015 base.OnFontChanged (e);
1017 if (textbox_ctrl != null)
1018 textbox_ctrl.Font = Font;
1020 if (!item_height_specified) {
1021 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
1022 item_height = (int) sz.Height;
1026 UpdateComboBoxBounds ();
1031 protected override void OnForeColorChanged (EventArgs e)
1033 base.OnForeColorChanged (e);
1034 if (textbox_ctrl != null)
1035 textbox_ctrl.ForeColor = ForeColor;
1038 [EditorBrowsable(EditorBrowsableState.Advanced)]
1039 protected override void OnGotFocus (EventArgs e)
1041 if (dropdown_style == ComboBoxStyle.DropDownList) {
1042 // We draw DDL styles manually, so they require a
1043 // refresh to have their selection drawn
1047 if (textbox_ctrl != null) {
1048 textbox_ctrl.SetSelectable (false);
1049 textbox_ctrl.ShowSelection = true;
1050 textbox_ctrl.ActivateCaret (true);
1051 textbox_ctrl.SelectAll ();
1054 base.OnGotFocus (e);
1057 [EditorBrowsable(EditorBrowsableState.Advanced)]
1058 protected override void OnLostFocus (EventArgs e)
1060 if (dropdown_style == ComboBoxStyle.DropDownList) {
1061 // We draw DDL styles manually, so they require a
1062 // refresh to have their selection drawn
1066 if (listbox_ctrl != null && dropped_down) {
1067 listbox_ctrl.HideWindow ();
1070 if (textbox_ctrl != null) {
1071 textbox_ctrl.SetSelectable (true);
1072 textbox_ctrl.ActivateCaret (false);
1073 textbox_ctrl.ShowSelection = false;
1076 base.OnLostFocus (e);
1079 protected override void OnHandleCreated (EventArgs e)
1081 base.OnHandleCreated (e);
1083 if (listbox_ctrl != null) {
1084 Controls.AddImplicit (listbox_ctrl);
1085 listbox_ctrl.Visible = true;
1088 if (textbox_ctrl != null)
1089 Controls.AddImplicit (textbox_ctrl);
1094 protected override void OnHandleDestroyed (EventArgs e)
1096 base.OnHandleDestroyed (e);
1099 protected override void OnKeyPress (KeyPressEventArgs e)
1101 // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
1103 // SelectedIndex = index;
1105 base.OnKeyPress (e);
1108 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1110 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1115 protected override void OnParentBackColorChanged (EventArgs e)
1117 base.OnParentBackColorChanged (e);
1120 protected override void OnResize (EventArgs e)
1123 if (listbox_ctrl != null)
1124 listbox_ctrl.CalcListBoxArea ();
1127 protected override void OnSelectedIndexChanged (EventArgs e)
1129 base.OnSelectedIndexChanged (e);
1131 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1136 protected virtual void OnSelectedItemChanged (EventArgs e)
1140 protected override void OnSelectedValueChanged (EventArgs e)
1142 base.OnSelectedValueChanged (e);
1145 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1147 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1152 protected override void RefreshItem (int index)
1154 if (index < 0 || index >= Items.Count)
1155 throw new ArgumentOutOfRangeException ("index");
1157 if (draw_mode == DrawMode.OwnerDrawVariable)
1158 item_heights.Remove (Items [index]);
1162 protected override void RefreshItems ()
1164 for (int i = 0; i < Items.Count; i++) {
1169 public override void ResetText ()
1171 Text = String.Empty;
1174 protected override bool ProcessKeyEventArgs (ref Message m)
1176 return base.ProcessKeyEventArgs (ref m);
1179 [EditorBrowsable (EditorBrowsableState.Advanced)]
1180 protected override void OnKeyDown (KeyEventArgs e)
1185 [EditorBrowsable (EditorBrowsableState.Advanced)]
1186 protected override void OnValidating (CancelEventArgs e)
1188 base.OnValidating (e);
1191 [EditorBrowsable (EditorBrowsableState.Advanced)]
1192 protected override void OnTextChanged (EventArgs e)
1194 base.OnTextChanged (e);
1198 protected virtual void OnTextUpdate (EventArgs e)
1200 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1205 protected override void OnMouseLeave (EventArgs e)
1208 if (flat_style == FlatStyle.Popup)
1211 base.OnMouseLeave (e);
1214 protected override void OnMouseEnter (EventArgs e)
1217 if (flat_style == FlatStyle.Popup)
1220 base.OnMouseEnter (e);
1224 public void Select (int start, int length)
1227 throw new ArgumentException ("Start cannot be less than zero");
1230 throw new ArgumentException ("length cannot be less than zero");
1232 if (dropdown_style == ComboBoxStyle.DropDownList)
1235 textbox_ctrl.Select (start, length);
1238 public void SelectAll ()
1240 if (dropdown_style == ComboBoxStyle.DropDownList)
1243 if (textbox_ctrl != null) {
1244 textbox_ctrl.ShowSelection = true;
1245 textbox_ctrl.SelectAll ();
1249 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1251 if ((specified & BoundsSpecified.Height) != 0) {
1252 requested_height = height;
1254 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1255 if (IntegralHeight) {
1256 int border = ThemeEngine.Current.Border3DSize.Height;
1257 int lb_height = height - PreferredHeight - 2;
1258 if (lb_height - 2 * border > ItemHeight) {
1259 int partial = (lb_height - 2 * border) % ItemHeight;
1262 height = PreferredHeight;
1265 height = PreferredHeight;
1268 base.SetBoundsCore (x, y, width, height, specified);
1271 protected override void SetItemCore (int index, object value)
1273 if (index < 0 || index >= Items.Count)
1276 Items[index] = value;
1279 protected override void SetItemsCore (IList value)
1284 Items.AddRange (value);
1290 public override string ToString ()
1292 return base.ToString () + ", Items.Count:" + Items.Count;
1295 protected override void WndProc (ref Message m)
1297 switch ((Msg) m.Msg) {
1299 case Msg.WM_KEYDOWN:
1300 Keys keys = (Keys) m.WParam.ToInt32 ();
1301 if (keys == Keys.Up || keys == Keys.Down)
1303 goto case Msg.WM_CHAR;
1305 if (textbox_ctrl != null)
1306 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1308 case Msg.WM_MOUSELEAVE:
1309 Point location = PointToClient (Control.MousePosition);
1310 if (ClientRectangle.Contains (location))
1316 base.WndProc (ref m);
1319 #endregion Public Methods
1321 #region Private Methods
1323 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1324 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1325 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1330 internal override bool InternalCapture {
1331 get { return Capture; }
1335 void LayoutComboBox ()
1337 int border = ThemeEngine.Current.Border3DSize.Width;
1339 text_area = ClientRectangle;
1340 text_area.Height = PreferredHeight;
1342 listbox_area = ClientRectangle;
1343 listbox_area.Y = text_area.Bottom + 3;
1344 listbox_area.Height -= (text_area.Height + 2);
1346 Rectangle prev_button_area = button_area;
1348 if (DropDownStyle == ComboBoxStyle.Simple)
1349 button_area = Rectangle.Empty;
1351 button_area = text_area;
1352 button_area.X = text_area.Right - button_width - border;
1353 button_area.Y = text_area.Y + border;
1354 button_area.Width = button_width;
1355 button_area.Height = text_area.Height - 2 * border;
1357 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1358 button_area.Inflate (1, 1);
1360 button_area.Width -= 2;
1365 if (button_area != prev_button_area) {
1366 prev_button_area.Y -= border;
1367 prev_button_area.Width += border;
1368 prev_button_area.Height += 2 * border;
1369 Invalidate (prev_button_area);
1370 Invalidate (button_area);
1373 if (textbox_ctrl != null) {
1374 int text_border = border + 1;
1375 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1376 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1377 textbox_ctrl.Height = text_area.Height - text_border * 2;
1380 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1381 listbox_ctrl.Location = listbox_area.Location;
1382 listbox_ctrl.CalcListBoxArea ();
1386 private void CreateComboListBox ()
1388 listbox_ctrl = new ComboListBox (this);
1389 listbox_ctrl.HighlightedIndex = SelectedIndex;
1392 internal void Draw (Rectangle clip, Graphics dc)
1394 Theme theme = ThemeEngine.Current;
1395 FlatStyle style = FlatStyle.Standard;
1396 bool is_flat = false;
1399 style = this.FlatStyle;
1400 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1403 if (DropDownStyle == ComboBoxStyle.Simple)
1404 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1406 if (style == FlatStyle.Popup && (is_entered || Focused)) {
1407 Rectangle area = text_area;
1410 dc.DrawRectangle (theme.ResPool.GetPen (SystemColors.ControlDark), area);
1411 dc.DrawLine (theme.ResPool.GetPen (SystemColors.ControlDark), button_area.X - 1, button_area.Top, button_area.X - 1, button_area.Bottom);
1413 if (!is_flat && clip.IntersectsWith (text_area))
1414 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1416 int border = theme.Border3DSize.Width;
1418 // No edit control, we paint the edit ourselves
1419 if (dropdown_style == ComboBoxStyle.DropDownList) {
1420 DrawItemState state = DrawItemState.None;
1421 Rectangle item_rect = text_area;
1422 item_rect.X += border;
1423 item_rect.Y += border;
1424 item_rect.Width -= (button_area.Width + 2 * border);
1425 item_rect.Height -= 2 * border;
1428 state = DrawItemState.Selected;
1429 state |= DrawItemState.Focus;
1432 state |= DrawItemState.ComboBoxEdit;
1433 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1436 if (show_dropdown_button) {
1437 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1440 button_state = ButtonState.Inactive;
1443 theme.DrawFlatStyleComboButton (dc, button_area, button_state);
1445 theme.CPDrawComboButton (dc, button_area, button_state);
1450 internal void DropDownListBox ()
1452 if (DropDownStyle == ComboBoxStyle.Simple)
1455 if (listbox_ctrl == null)
1456 CreateComboListBox ();
1458 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1460 if (listbox_ctrl.ShowWindow ())
1461 dropped_down = true;
1463 button_state = ButtonState.Pushed;
1464 if (dropdown_style == ComboBoxStyle.DropDownList)
1465 Invalidate (text_area);
1468 internal void DropDownListBoxFinished ()
1470 if (DropDownStyle == ComboBoxStyle.Simple)
1473 button_state = ButtonState.Normal;
1474 Invalidate (button_area);
1475 dropped_down = false;
1477 OnDropDownClosed (EventArgs.Empty);
1481 private int FindStringCaseInsensitive (string search)
1483 if (search.Length == 0) {
1487 for (int i = 0; i < Items.Count; i++)
1489 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1496 internal int FindStringCaseInsensitive (string search, int start_index)
1498 if (search.Length == 0) {
1502 for (int i = 0; i < Items.Count; i++) {
1503 int index = (i + start_index) % Items.Count;
1504 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1511 internal override ContextMenu ContextMenuInternal {
1513 return base.ContextMenuInternal;
1516 base.ContextMenuInternal = value;
1517 if (textbox_ctrl != null) {
1518 textbox_ctrl.ContextMenu = value;
1523 private void OnKeyDownCB(object sender, KeyEventArgs e)
1525 if (Items.Count == 0)
1531 SelectedIndex = Math.Max(SelectedIndex-1, 0);
1535 SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1539 if (listbox_ctrl != null)
1540 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1544 if (listbox_ctrl != null)
1545 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1553 void OnMouseDownCB (object sender, MouseEventArgs e)
1556 if (DropDownStyle == ComboBoxStyle.DropDownList)
1557 area = ClientRectangle;
1561 if (area.Contains (e.X, e.Y)) {
1563 Invalidate (button_area);
1569 void OnMouseMoveCB (object sender, MouseEventArgs e)
1571 if (DropDownStyle == ComboBoxStyle.Simple)
1574 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1575 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1576 if (listbox_ctrl.ClientRectangle.Contains (location))
1577 listbox_ctrl.Capture = true;
1581 void OnMouseUpCB (object sender, MouseEventArgs e)
1584 OnClick (EventArgs.Empty);
1587 listbox_ctrl.Capture = true;
1590 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1592 if (Items.Count == 0)
1595 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1596 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1597 listbox_ctrl.Scroll (-lines);
1599 int lines = me.Delta / 120;
1600 int index = SelectedIndex - lines;
1603 else if (index >= Items.Count)
1604 index = Items.Count - 1;
1605 SelectedIndex = index;
1609 internal override void OnPaintInternal (PaintEventArgs pevent)
1611 if (suspend_ctrlupdate)
1614 Draw (ClientRectangle, pevent.Graphics);
1617 private void OnTextBoxClick (object sender, EventArgs e)
1622 private void OnTextChangedEdit (object sender, EventArgs e)
1624 if (process_textchanged_event == false)
1627 OnTextChanged (EventArgs.Empty);
1629 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1634 // TODO: THIS IS BROKEN-ISH
1635 // I don't think we should hilight, and setting the top item does weirdness
1636 // when there is no scrollbar
1638 if (listbox_ctrl != null) {
1639 listbox_ctrl.SetTopItem (item);
1640 listbox_ctrl.HighlightedIndex = item;
1643 base.Text = textbox_ctrl.Text;
1646 internal void SetControlText (string s)
1648 process_textchanged_event = false;
1649 textbox_ctrl.Text = s;
1650 process_textchanged_event = true;
1653 void UpdateComboBoxBounds ()
1655 if (requested_height == -1)
1658 // Save the requested height since set bounds can destroy it
1659 int save_height = requested_height;
1660 SetBounds (bounds.X, bounds.Y, bounds.Width, requested_height, BoundsSpecified.Height);
1661 requested_height = save_height;
1664 private void UpdatedItems ()
1666 if (listbox_ctrl != null) {
1667 listbox_ctrl.UpdateLastVisibleItem ();
1668 listbox_ctrl.CalcListBoxArea ();
1669 listbox_ctrl.Refresh ();
1673 #endregion Private Methods
1675 [ListBindableAttribute (false)]
1676 public class ObjectCollection : IList, ICollection, IEnumerable
1679 private ComboBox owner;
1680 internal ArrayList object_items = new ArrayList ();
1682 public ObjectCollection (ComboBox owner)
1687 #region Public Properties
1689 get { return object_items.Count; }
1692 public bool IsReadOnly {
1693 get { return false; }
1697 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1698 public virtual object this [int index] {
1700 if (index < 0 || index >= Count)
1701 throw new ArgumentOutOfRangeException ("index");
1703 return object_items[index];
1706 if (index < 0 || index >= Count)
1707 throw new ArgumentOutOfRangeException ("index");
1709 throw new ArgumentNullException ("value");
1711 object_items[index] = value;
1712 if (owner.listbox_ctrl != null)
1713 owner.listbox_ctrl.InvalidateItem (index);
1714 if (index == owner.SelectedIndex) {
1715 if (owner.textbox_ctrl == null)
1718 owner.textbox_ctrl.SelectedText = value.ToString ();
1723 bool ICollection.IsSynchronized {
1724 get { return false; }
1727 object ICollection.SyncRoot {
1728 get { return this; }
1731 bool IList.IsFixedSize {
1732 get { return false; }
1735 #endregion Public Properties
1737 #region Public Methods
1738 public int Add (object item)
1742 idx = AddItem (item);
1743 owner.UpdatedItems ();
1747 public void AddRange (object[] items)
1750 throw new ArgumentNullException ("items");
1752 foreach (object mi in items)
1755 owner.UpdatedItems ();
1758 public void Clear ()
1760 owner.selected_index = -1;
1761 object_items.Clear ();
1762 owner.UpdatedItems ();
1766 public bool Contains (object value)
1769 throw new ArgumentNullException ("value");
1771 return object_items.Contains (value);
1774 public void CopyTo (object[] dest, int arrayIndex)
1776 object_items.CopyTo (dest, arrayIndex);
1779 void ICollection.CopyTo (Array dest, int index)
1781 object_items.CopyTo (dest, index);
1784 public IEnumerator GetEnumerator ()
1786 return object_items.GetEnumerator ();
1789 int IList.Add (object item)
1794 public int IndexOf (object value)
1797 throw new ArgumentNullException ("value");
1799 return object_items.IndexOf (value);
1802 public void Insert (int index, object item)
1804 if (index < 0 || index > Count)
1805 throw new ArgumentOutOfRangeException ("index");
1807 throw new ArgumentNullException ("item");
1809 owner.BeginUpdate ();
1814 object_items.Insert (index, item);
1816 owner.EndUpdate (); // Calls UpdatedItems
1819 public void Remove (object value)
1824 if (IndexOf (value) == owner.SelectedIndex)
1825 owner.SelectedIndex = -1;
1827 RemoveAt (IndexOf (value));
1830 public void RemoveAt (int index)
1832 if (index < 0 || index >= Count)
1833 throw new ArgumentOutOfRangeException ("index");
1835 if (index == owner.SelectedIndex)
1836 owner.SelectedIndex = -1;
1838 object_items.RemoveAt (index);
1839 owner.UpdatedItems ();
1841 #endregion Public Methods
1843 #region Private Methods
1844 private int AddItem (object item)
1847 throw new ArgumentNullException ("item");
1851 foreach (object o in object_items) {
1852 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
1853 object_items.Insert (index, item);
1859 object_items.Add (item);
1860 return object_items.Count - 1;
1863 internal void AddRange (IList items)
1865 foreach (object mi in items)
1868 owner.UpdatedItems ();
1871 internal void Sort ()
1873 object_items.Sort ();
1876 #endregion Private Methods
1879 internal class ComboTextBox : TextBox {
1881 private ComboBox owner;
1883 public ComboTextBox (ComboBox owner)
1886 ShowSelection = false;
1889 internal void SetSelectable (bool selectable)
1891 SetStyle (ControlStyles.Selectable, selectable);
1894 internal void ActivateCaret (bool active)
1897 document.CaretHasFocus ();
1899 document.CaretLostFocus ();
1903 internal override void OnTextUpdate ()
1905 base.OnTextUpdate ();
1906 owner.OnTextUpdate (EventArgs.Empty);
1910 protected override void OnGotFocus (EventArgs e)
1912 owner.Select (false, true);
1915 protected override void OnLostFocus (EventArgs e)
1917 owner.Select (false, true);
1920 public override bool Focused {
1922 return owner.Focused;
1926 internal override bool ActivateOnShow { get { return false; } }
1929 internal class ComboListBox : Control
1931 private ComboBox owner;
1932 private VScrollBarLB vscrollbar_ctrl;
1933 private int top_item; /* First item that we show the in the current page */
1934 private int last_item; /* Last visible item */
1935 internal int page_size; /* Number of listbox items per page */
1936 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1938 internal enum ItemNavigation
1948 class VScrollBarLB : VScrollBar
1950 public VScrollBarLB ()
1954 internal override bool InternalCapture {
1955 get { return Capture; }
1959 public void FireMouseDown (MouseEventArgs e)
1964 e = TranslateEvent (e);
1968 public void FireMouseUp (MouseEventArgs e)
1973 e = TranslateEvent (e);
1977 public void FireMouseMove (MouseEventArgs e)
1982 e = TranslateEvent (e);
1986 MouseEventArgs TranslateEvent (MouseEventArgs e)
1988 Point loc = PointToClient (Control.MousePosition);
1989 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1993 public ComboListBox (ComboBox owner)
2000 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2002 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2003 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2005 this.is_visible = false;
2007 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2008 InternalBorderStyle = BorderStyle.Fixed3D;
2010 InternalBorderStyle = BorderStyle.FixedSingle;
2013 protected override CreateParams CreateParams
2016 CreateParams cp = base.CreateParams;
2017 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2020 cp.Style ^= (int)WindowStyles.WS_CHILD;
2021 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2022 cp.Style |= (int)WindowStyles.WS_POPUP;
2023 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2028 protected override void Select (bool directed, bool forward)
2030 // Do nothing, we never want to be selected
2033 internal override bool InternalCapture {
2042 internal override bool ActivateOnShow { get { return false; } }
2043 #region Private Methods
2045 // Calcs the listbox area
2046 internal void CalcListBoxArea ()
2049 bool show_scrollbar = false;
2051 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2052 Rectangle area = owner.listbox_area;
2054 height = area.Height;
2056 else { // DropDown or DropDownList
2058 width = owner.DropDownWidth;
2059 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2061 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2063 for (int i = 0; i < count; i++) {
2064 height += owner.GetItemHeight (i);
2069 height = owner.DropDownHeight;
2071 height = owner.ItemHeight * count;
2076 page_size = height / owner.ItemHeight;
2078 if (owner.Items.Count <= owner.MaxDropDownItems) {
2079 if (vscrollbar_ctrl != null)
2080 vscrollbar_ctrl.Visible = false;
2081 height = owner.ItemHeight * owner.items.Count;
2083 /* Need vertical scrollbar */
2084 if (vscrollbar_ctrl == null) {
2085 vscrollbar_ctrl = new VScrollBarLB ();
2086 vscrollbar_ctrl.Minimum = 0;
2087 vscrollbar_ctrl.SmallChange = 1;
2088 vscrollbar_ctrl.LargeChange = 1;
2089 vscrollbar_ctrl.Maximum = 0;
2090 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2091 Controls.AddImplicit (vscrollbar_ctrl);
2094 vscrollbar_ctrl.Dock = DockStyle.Right;
2096 vscrollbar_ctrl.Maximum = owner.Items.Count - 2;
2097 int large = (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2100 vscrollbar_ctrl.LargeChange = large;
2101 show_scrollbar = vscrollbar_ctrl.Visible = true;
2103 int hli = HighlightedIndex;
2105 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2106 vscrollbar_ctrl.Value = hli;
2110 Size = new Size (width, height);
2111 textarea_drawable = ClientRectangle;
2112 textarea_drawable.Width = width;
2113 textarea_drawable.Height = height;
2115 if (vscrollbar_ctrl != null && show_scrollbar)
2116 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2118 last_item = LastVisibleItem ();
2121 private void Draw (Rectangle clip, Graphics dc)
2123 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2125 if (owner.Items.Count > 0) {
2127 for (int i = top_item; i <= last_item; i++) {
2128 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2130 if (!clip.IntersectsWith (item_rect))
2133 DrawItemState state = DrawItemState.None;
2135 if (i == HighlightedIndex) {
2136 state |= DrawItemState.Selected;
2138 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2139 state |= DrawItemState.Focus;
2143 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2144 i, state, owner.ForeColor, owner.BackColor));
2149 int highlighted_index = -1;
2151 public int HighlightedIndex {
2152 get { return highlighted_index; }
2154 if (highlighted_index == value)
2157 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2158 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2159 highlighted_index = value;
2160 if (highlighted_index != -1)
2161 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2165 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2167 if (index < 0 || index >= owner.Items.Count)
2168 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2170 Rectangle item_rect = new Rectangle ();
2171 int height = owner.GetItemHeight (index);
2174 item_rect.Width = textarea_drawable.Width;
2175 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2177 for (int i = top_index; i < index; i++)
2178 item_rect.Y += owner.GetItemHeight (i);
2180 item_rect.Y = height * (index - top_index);
2182 item_rect.Height = height;
2186 public void HideWindow ()
2188 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2193 owner.DropDownListBoxFinished ();
2196 private int IndexFromPointDisplayRectangle (int x, int y)
2198 for (int i = top_item; i <= last_item; i++) {
2199 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2206 public void InvalidateItem (int index)
2209 Invalidate (GetItemDisplayRectangle (index, top_item));
2212 private int LastVisibleItem ()
2214 Rectangle item_rect;
2215 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2218 for (i = top_item; i < owner.Items.Count; i++) {
2219 item_rect = GetItemDisplayRectangle (i, top_item);
2220 if (item_rect.Y + item_rect.Height > top_y) {
2227 public void SetTopItem (int item)
2229 if (top_item == item)
2232 UpdateLastVisibleItem ();
2236 bool scrollbar_grabbed = false;
2240 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2243 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2247 protected override void OnMouseDown (MouseEventArgs e)
2250 vscrollbar_ctrl.FireMouseDown (e);
2251 scrollbar_grabbed = true;
2255 protected override void OnMouseMove (MouseEventArgs e)
2257 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2260 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2261 vscrollbar_ctrl.FireMouseMove (e);
2265 Point pt = PointToClient (Control.MousePosition);
2266 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2269 HighlightedIndex = index;
2272 protected override void OnMouseUp (MouseEventArgs e)
2274 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2276 if (scrollbar_grabbed) {
2277 vscrollbar_ctrl.FireMouseUp (e);
2278 scrollbar_grabbed = false;
2280 HighlightedIndex = index;
2289 owner.SelectedIndex = index;
2290 owner.OnSelectionChangeCommitted (new EventArgs ());
2294 internal override void OnPaintInternal (PaintEventArgs pevent)
2296 Draw (pevent.ClipRectangle,pevent.Graphics);
2299 public bool ShowWindow ()
2301 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2304 HighlightedIndex = owner.SelectedIndex;
2310 owner.OnDropDown (EventArgs.Empty);
2314 public void UpdateLastVisibleItem ()
2316 last_item = LastVisibleItem ();
2319 public void Scroll (int delta)
2321 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2324 int max = owner.Items.Count - page_size;
2326 int val = vscrollbar_ctrl.Value + delta;
2329 else if (val < vscrollbar_ctrl.Minimum)
2330 val = vscrollbar_ctrl.Minimum;
2331 vscrollbar_ctrl.Value = val;
2334 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2336 if (owner.Items.Count == 0)
2339 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2344 private void VerticalScrollEvent (object sender, EventArgs e)
2346 if (top_item == vscrollbar_ctrl.Value)
2349 top_item = vscrollbar_ctrl.Value;
2350 UpdateLastVisibleItem ();
2354 protected override void WndProc(ref Message m) {
2355 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2356 if (m.WParam != IntPtr.Zero) {
2357 XplatUI.SetFocus(m.WParam);
2360 base.WndProc (ref m);
2363 #endregion Private Methods