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 process_texchanged_autoscroll = true;
63 private bool item_height_specified;
64 private int item_height;
65 private int requested_height = -1;
66 private Hashtable item_heights;
67 private bool show_dropdown_button;
68 private ButtonState button_state = ButtonState.Normal;
69 private bool dropped_down;
70 private Rectangle text_area;
71 private Rectangle button_area;
72 private Rectangle listbox_area;
73 private const int button_width = 16;
74 bool drop_down_button_entered;
76 private AutoCompleteStringCollection auto_complete_custom_source = null;
77 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
78 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
79 private FlatStyle flat_style;
80 private int drop_down_height;
81 const int default_drop_down_height = 106;
85 public class ChildAccessibleObject : AccessibleObject {
87 public ChildAccessibleObject (ComboBox owner, IntPtr handle)
92 public override string Name {
101 items = new ObjectCollection (this);
102 DropDownStyle = ComboBoxStyle.DropDown;
103 item_height = FontHeight + 2;
104 background_color = ThemeEngine.Current.ColorWindow;
105 border_style = BorderStyle.None;
108 drop_down_height = default_drop_down_height;
109 flat_style = FlatStyle.Standard;
113 MouseDown += new MouseEventHandler (OnMouseDownCB);
114 MouseUp += new MouseEventHandler (OnMouseUpCB);
115 MouseMove += new MouseEventHandler (OnMouseMoveCB);
116 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
117 MouseEnter += new EventHandler (OnMouseEnter);
118 MouseLeave += new EventHandler (OnMouseLeave);
119 KeyDown +=new KeyEventHandler(OnKeyDownCB);
125 [EditorBrowsable (EditorBrowsableState.Never)]
126 public new event EventHandler BackgroundImageChanged {
127 add { base.BackgroundImageChanged += value; }
128 remove { base.BackgroundImageChanged -= value; }
134 [EditorBrowsable (EditorBrowsableState.Never)]
135 public new event EventHandler BackgroundImageLayoutChanged
137 add { base.BackgroundImageLayoutChanged += value; }
138 remove { base.BackgroundImageLayoutChanged -= value; }
142 [EditorBrowsable (EditorBrowsableState.Never)]
143 public new event EventHandler DoubleClick
145 add { base.DoubleClick += value; }
146 remove { base.DoubleClick -= value; }
150 static object DrawItemEvent = new object ();
151 static object DropDownEvent = new object ();
152 static object DropDownStyleChangedEvent = new object ();
153 static object MeasureItemEvent = new object ();
154 static object SelectedIndexChangedEvent = new object ();
155 static object SelectionChangeCommittedEvent = new object ();
157 static object DropDownClosedEvent = new object ();
158 static object TextUpdateEvent = new object ();
161 public event DrawItemEventHandler DrawItem {
162 add { Events.AddHandler (DrawItemEvent, value); }
163 remove { Events.RemoveHandler (DrawItemEvent, value); }
166 public event EventHandler DropDown {
167 add { Events.AddHandler (DropDownEvent, value); }
168 remove { Events.RemoveHandler (DropDownEvent, value); }
171 public event EventHandler DropDownClosed
173 add { Events.AddHandler (DropDownClosedEvent, value); }
174 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
178 public event EventHandler DropDownStyleChanged {
179 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
180 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
183 public event MeasureItemEventHandler MeasureItem {
184 add { Events.AddHandler (MeasureItemEvent, value); }
185 remove { Events.RemoveHandler (MeasureItemEvent, value); }
189 [EditorBrowsable (EditorBrowsableState.Never)]
190 public new event EventHandler PaddingChanged
192 add { base.PaddingChanged += value; }
193 remove { base.PaddingChanged -= value; }
198 [EditorBrowsable (EditorBrowsableState.Never)]
199 public new event PaintEventHandler Paint {
200 add { base.Paint += value; }
201 remove { base.Paint -= value; }
204 public event EventHandler SelectedIndexChanged {
205 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
206 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
209 public event EventHandler SelectionChangeCommitted {
210 add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
211 remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
214 public event EventHandler TextUpdate
216 add { Events.AddHandler (TextUpdateEvent, value); }
217 remove { Events.RemoveHandler (TextUpdateEvent, value); }
223 #region Public Properties
225 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
226 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
228 [EditorBrowsable (EditorBrowsableState.Always)]
230 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
231 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
232 public AutoCompleteStringCollection AutoCompleteCustomSource {
234 if(auto_complete_custom_source == null) {
235 auto_complete_custom_source = new AutoCompleteStringCollection ();
236 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
238 return auto_complete_custom_source;
241 if(auto_complete_custom_source == value)
244 if(auto_complete_custom_source != null) //remove eventhandler from old collection
245 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
247 auto_complete_custom_source = value;
249 if(auto_complete_custom_source != null)
250 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
252 SetTextBoxAutoCompleteData ();
256 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
258 [EditorBrowsable (EditorBrowsableState.Always)]
259 [DefaultValue (AutoCompleteMode.None)]
260 public AutoCompleteMode AutoCompleteMode {
261 get { return auto_complete_mode; }
263 if(auto_complete_mode == value)
266 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
267 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
269 auto_complete_mode = value;
270 SetTextBoxAutoCompleteData ();
274 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
276 [EditorBrowsable (EditorBrowsableState.Always)]
277 [DefaultValue (AutoCompleteSource.None)]
278 public AutoCompleteSource AutoCompleteSource {
279 get { return auto_complete_source; }
281 if(auto_complete_source == value)
284 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
285 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
287 auto_complete_source = value;
288 SetTextBoxAutoCompleteData ();
292 void SetTextBoxAutoCompleteData ()
294 if (textbox_ctrl == null)
297 textbox_ctrl.AutoCompleteMode = auto_complete_mode;
299 if (auto_complete_source == AutoCompleteSource.ListItems) {
300 textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
301 textbox_ctrl.AutoCompleteCustomSource = null;
302 textbox_ctrl.AutoCompleteInternalSource = this;
304 textbox_ctrl.AutoCompleteSource = auto_complete_source;
305 textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
306 textbox_ctrl.AutoCompleteInternalSource = null;
310 public override Color BackColor {
311 get { return base.BackColor; }
313 if (base.BackColor == value)
315 base.BackColor = value;
321 [EditorBrowsable (EditorBrowsableState.Never)]
322 public override Image BackgroundImage {
323 get { return base.BackgroundImage; }
325 if (base.BackgroundImage == value)
327 base.BackgroundImage = value;
334 [EditorBrowsable (EditorBrowsableState.Never)]
335 public override ImageLayout BackgroundImageLayout {
336 get { return base.BackgroundImageLayout; }
337 set { base.BackgroundImageLayout = value; }
341 protected override CreateParams CreateParams {
342 get { return base.CreateParams;}
346 [DefaultValue ((string)null)]
347 [AttributeProvider (typeof (IListSource))]
348 [RefreshProperties (RefreshProperties.Repaint)]
349 [MWFCategory("Data")]
350 public new object DataSource {
351 get { return base.DataSource; }
352 set { base.DataSource = value; }
356 protected override Size DefaultSize {
357 get { return new Size (121, 21); }
360 [RefreshProperties(RefreshProperties.Repaint)]
361 [DefaultValue (DrawMode.Normal)]
362 [MWFCategory("Behavior")]
363 public DrawMode DrawMode {
364 get { return draw_mode; }
366 if (!Enum.IsDefined (typeof (DrawMode), value))
367 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
369 if (draw_mode == value)
372 if (draw_mode == DrawMode.OwnerDrawVariable)
375 if (draw_mode == DrawMode.OwnerDrawVariable)
376 item_heights = new Hashtable ();
384 [EditorBrowsable (EditorBrowsableState.Always)]
385 [MWFCategory("Behavior")]
386 public int DropDownHeight {
388 return drop_down_height;
392 throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
394 if (value == drop_down_height)
397 drop_down_height = value;
398 IntegralHeight = false;
403 [DefaultValue (ComboBoxStyle.DropDown)]
404 [RefreshProperties(RefreshProperties.Repaint)]
405 [MWFCategory("Appearance")]
406 public ComboBoxStyle DropDownStyle {
407 get { return dropdown_style; }
409 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
410 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
412 if (dropdown_style == value)
417 if (dropdown_style == ComboBoxStyle.Simple) {
418 if (listbox_ctrl != null) {
419 Controls.RemoveImplicit (listbox_ctrl);
420 listbox_ctrl.Dispose ();
425 dropdown_style = value;
427 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
428 Controls.RemoveImplicit (textbox_ctrl);
429 textbox_ctrl.Dispose ();
433 if (dropdown_style == ComboBoxStyle.Simple) {
434 show_dropdown_button = false;
436 CreateComboListBox ();
437 Controls.AddImplicit (listbox_ctrl);
438 listbox_ctrl.Visible = true;
440 // This should give us a 150 default height
441 // for Simple mode if size hasn't been set
442 // (DefaultSize doesn't work for us in this case)
443 if (requested_height == -1)
444 requested_height = 150;
446 show_dropdown_button = true;
447 button_state = ButtonState.Normal;
450 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
451 textbox_ctrl = new ComboTextBox (this);
452 object selected_item = SelectedItem;
453 if (selected_item != null)
454 textbox_ctrl.Text = GetItemText (selected_item);
455 textbox_ctrl.BorderStyle = BorderStyle.None;
456 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
457 textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
458 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
459 textbox_ctrl.ContextMenu = ContextMenu;
460 textbox_ctrl.TopMargin = 1; // since we don't have borders, adjust manually the top
462 if (IsHandleCreated == true)
463 Controls.AddImplicit (textbox_ctrl);
465 SetTextBoxAutoCompleteData ();
470 OnDropDownStyleChanged (EventArgs.Empty);
473 UpdateComboBoxBounds ();
478 [MWFCategory("Behavior")]
479 public int DropDownWidth {
481 if (dropdown_width == -1)
484 return dropdown_width;
487 if (dropdown_width == value)
492 throw new ArgumentOutOfRangeException ("DropDownWidth",
493 "The DropDownWidth value is less than one.");
495 throw new ArgumentException ("The DropDownWidth value is less than one.");
498 dropdown_width = value;
503 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
504 public bool DroppedDown {
506 if (dropdown_style == ComboBoxStyle.Simple)
512 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
518 listbox_ctrl.HideWindow ();
523 [DefaultValue (FlatStyle.Standard)]
525 [MWFCategory("Appearance")]
526 public FlatStyle FlatStyle {
527 get { return flat_style; }
529 if (!Enum.IsDefined (typeof (FlatStyle), value))
530 throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
539 public override bool Focused {
540 get { return base.Focused; }
543 public override Color ForeColor {
544 get { return base.ForeColor; }
546 if (base.ForeColor == value)
548 base.ForeColor = value;
553 [DefaultValue (true)]
555 [MWFCategory("Behavior")]
556 public bool IntegralHeight {
557 get { return integral_height; }
559 if (integral_height == value)
561 integral_height = value;
562 UpdateComboBoxBounds ();
568 [MWFCategory("Behavior")]
569 public int ItemHeight {
571 if (item_height == -1) {
572 SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
573 item_height = (int) sz.Height;
580 throw new ArgumentOutOfRangeException ("ItemHeight",
581 "The item height value is less than one.");
583 throw new ArgumentException ("The item height value is less than one.");
586 item_height_specified = true;
589 UpdateComboBoxBounds ();
595 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
597 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
599 [MergableProperty (false)]
601 [MWFCategory("Data")]
602 public ComboBox.ObjectCollection Items {
603 get { return items; }
608 [MWFCategory("Behavior")]
609 public int MaxDropDownItems {
610 get { return maxdrop_items; }
612 if (maxdrop_items == value)
614 maxdrop_items = value;
619 public override Size MaximumSize {
620 get { return base.MaximumSize; }
622 base.MaximumSize = new Size (value.Width, 0);
629 [MWFCategory("Behavior")]
630 public int MaxLength {
631 get { return max_length; }
633 if (max_length == value)
638 if (dropdown_style != ComboBoxStyle.DropDownList) {
642 textbox_ctrl.MaxLength = value;
648 public override Size MinimumSize {
649 get { return base.MinimumSize; }
651 base.MinimumSize = new Size (value.Width, 0);
655 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
656 [EditorBrowsable (EditorBrowsableState.Never)]
658 public new Padding Padding {
659 get { return base.Padding; }
660 set { base.Padding = value; }
664 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
666 public int PreferredHeight {
667 get { return Font.Height + 8; }
671 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
672 public override int SelectedIndex {
673 get { return selected_index; }
675 SetSelectedIndex (value, false);
680 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
682 public object SelectedItem {
683 get { return selected_index == -1 ? null : Items [selected_index]; }
685 object item = selected_index == -1 ? null : Items [selected_index];
692 SelectedIndex = Items.IndexOf (value);
697 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
698 public string SelectedText {
700 if (dropdown_style == ComboBoxStyle.DropDownList)
703 string retval = textbox_ctrl.SelectedText;
706 // On 1.1, the textbox will return null, combobox returns ""
707 if (retval == null && !textbox_ctrl.IsHandleCreated)
713 if (dropdown_style == ComboBoxStyle.DropDownList)
715 textbox_ctrl.SelectedText = value;
720 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
721 public int SelectionLength {
723 if (dropdown_style == ComboBoxStyle.DropDownList)
726 int result = textbox_ctrl.SelectionLength;
727 return result == -1 ? 0 : result;
730 if (dropdown_style == ComboBoxStyle.DropDownList)
732 if (textbox_ctrl.SelectionLength == value)
734 textbox_ctrl.SelectionLength = value;
739 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
740 public int SelectionStart {
742 if (dropdown_style == ComboBoxStyle.DropDownList)
744 return textbox_ctrl.SelectionStart;
747 if (dropdown_style == ComboBoxStyle.DropDownList)
749 if (textbox_ctrl.SelectionStart == value)
751 textbox_ctrl.SelectionStart = value;
755 [DefaultValue (false)]
756 [MWFCategory("Behavior")]
758 get { return sorted; }
773 public override string Text {
775 if (dropdown_style != ComboBoxStyle.DropDownList) {
776 if (textbox_ctrl != null) {
777 return textbox_ctrl.Text;
781 if (SelectedItem != null)
782 return GetItemText (SelectedItem);
788 if (SelectedIndex == -1) {
789 if (dropdown_style != ComboBoxStyle.DropDownList)
790 SetControlText (string.Empty, false);
797 // do nothing if value exactly matches text of selected item
798 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
801 // find exact match using case-sensitive comparison, and if does
802 // not result in any match then use case-insensitive comparison
803 int index = FindStringExact (value, -1, false);
805 index = FindStringExact (value, -1, true);
808 SelectedIndex = index;
812 // set directly the passed value, since we already know it's not matching any item
813 if (dropdown_style != ComboBoxStyle.DropDownList)
814 textbox_ctrl.Text = value;
818 #endregion Public Properties
820 #region Internal Properties
821 internal Rectangle ButtonArea {
822 get { return button_area; }
825 internal Rectangle TextArea {
826 get { return text_area; }
830 #region UIA Framework Properties
832 internal TextBox UIATextBox {
833 get { return textbox_ctrl; }
836 internal ComboListBox UIAComboListBox {
837 get { return listbox_ctrl; }
840 #endregion UIA Framework Properties
842 #region Public Methods
844 [Obsolete ("This method has been deprecated")]
846 protected virtual void AddItemsCore (object[] value)
851 public void BeginUpdate ()
853 suspend_ctrlupdate = true;
857 protected override AccessibleObject CreateAccessibilityInstance ()
859 return base.CreateAccessibilityInstance ();
862 protected override void CreateHandle ()
864 base.CreateHandle ();
868 protected override void Dispose (bool disposing)
871 if (listbox_ctrl != null) {
872 listbox_ctrl.Dispose ();
873 Controls.RemoveImplicit (listbox_ctrl);
877 if (textbox_ctrl != null) {
878 Controls.RemoveImplicit (textbox_ctrl);
879 textbox_ctrl.Dispose ();
884 base.Dispose (disposing);
887 public void EndUpdate ()
889 suspend_ctrlupdate = false;
894 public int FindString (string s)
896 return FindString (s, -1);
899 public int FindString (string s, int startIndex)
901 if (s == null || Items.Count == 0)
905 if (startIndex < -1 || startIndex >= Items.Count)
907 if (startIndex < -1 || startIndex >= Items.Count - 1)
909 throw new ArgumentOutOfRangeException ("startIndex");
913 if (i == (Items.Count - 1))
918 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
920 if (i == (Items.Count - 1))
922 } while (i != startIndex);
927 public int FindStringExact (string s)
929 return FindStringExact (s, -1);
932 public int FindStringExact (string s, int startIndex)
934 return FindStringExact (s, startIndex, true);
937 private int FindStringExact (string s, int startIndex, bool ignoreCase)
939 if (s == null || Items.Count == 0)
943 if (startIndex < -1 || startIndex >= Items.Count)
945 if (startIndex < -1 || startIndex >= Items.Count - 1)
947 throw new ArgumentOutOfRangeException ("startIndex");
951 if (i == (Items.Count - 1))
956 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
958 if (i == (Items.Count - 1))
960 } while (i != startIndex);
965 public int GetItemHeight (int index)
967 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
969 if (index < 0 || index >= Items.Count )
970 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
972 object item = Items [index];
973 if (item_heights.Contains (item))
974 return (int) item_heights [item];
976 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
977 OnMeasureItem (args);
978 item_heights [item] = args.ItemHeight;
979 return args.ItemHeight;
985 protected override bool IsInputKey (Keys keyData)
987 switch (keyData & ~Keys.Modifiers) {
1003 protected override void OnBackColorChanged (EventArgs e)
1005 base.OnBackColorChanged (e);
1007 if (textbox_ctrl != null)
1008 textbox_ctrl.BackColor = BackColor;
1011 protected override void OnDataSourceChanged (EventArgs e)
1013 base.OnDataSourceChanged (e);
1016 if (DataSource == null || DataManager == null) {
1020 SelectedIndex = DataManager.Position;
1024 protected override void OnDisplayMemberChanged (EventArgs e)
1026 base.OnDisplayMemberChanged (e);
1028 if (DataManager == null)
1031 SelectedIndex = DataManager.Position;
1033 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1034 SetControlText (GetItemText (Items [selected_index]), true);
1036 if (!IsHandleCreated)
1042 protected virtual void OnDrawItem (DrawItemEventArgs e)
1044 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1049 internal void HandleDrawItem (DrawItemEventArgs e)
1051 // Only raise OnDrawItem if we are in an OwnerDraw mode
1053 case DrawMode.OwnerDrawFixed:
1054 case DrawMode.OwnerDrawVariable:
1058 ThemeEngine.Current.DrawComboBoxItem (this, e);
1063 protected virtual void OnDropDown (EventArgs e)
1065 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1071 protected virtual void OnDropDownClosed (EventArgs e)
1073 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1079 protected virtual void OnDropDownStyleChanged (EventArgs e)
1081 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1086 protected override void OnFontChanged (EventArgs e)
1088 base.OnFontChanged (e);
1090 if (textbox_ctrl != null)
1091 textbox_ctrl.Font = Font;
1093 if (!item_height_specified)
1094 item_height = Font.Height + 2;
1097 UpdateComboBoxBounds ();
1102 protected override void OnForeColorChanged (EventArgs e)
1104 base.OnForeColorChanged (e);
1105 if (textbox_ctrl != null)
1106 textbox_ctrl.ForeColor = ForeColor;
1109 [EditorBrowsable(EditorBrowsableState.Advanced)]
1110 protected override void OnGotFocus (EventArgs e)
1112 if (dropdown_style == ComboBoxStyle.DropDownList) {
1113 // We draw DDL styles manually, so they require a
1114 // refresh to have their selection drawn
1118 if (textbox_ctrl != null) {
1119 textbox_ctrl.SetSelectable (false);
1120 textbox_ctrl.ShowSelection = true;
1121 textbox_ctrl.ActivateCaret (true);
1122 textbox_ctrl.SelectAll ();
1125 base.OnGotFocus (e);
1128 [EditorBrowsable(EditorBrowsableState.Advanced)]
1129 protected override void OnLostFocus (EventArgs e)
1131 if (dropdown_style == ComboBoxStyle.DropDownList) {
1132 // We draw DDL styles manually, so they require a
1133 // refresh to have their selection drawn
1137 if (listbox_ctrl != null && dropped_down) {
1138 listbox_ctrl.HideWindow ();
1141 if (textbox_ctrl != null) {
1142 textbox_ctrl.SetSelectable (true);
1143 textbox_ctrl.ActivateCaret (false);
1144 textbox_ctrl.ShowSelection = false;
1145 textbox_ctrl.SelectionLength = 0;
1147 textbox_ctrl.HideAutoCompleteList ();
1151 base.OnLostFocus (e);
1154 protected override void OnHandleCreated (EventArgs e)
1156 base.OnHandleCreated (e);
1158 SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1160 if (textbox_ctrl != null)
1161 Controls.AddImplicit (textbox_ctrl);
1164 UpdateComboBoxBounds ();
1167 protected override void OnHandleDestroyed (EventArgs e)
1169 base.OnHandleDestroyed (e);
1172 protected override void OnKeyPress (KeyPressEventArgs e)
1174 if (dropdown_style == ComboBoxStyle.DropDownList) {
1175 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1177 SelectedIndex = index;
1178 if (DroppedDown) { //Scroll into view
1179 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1180 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1181 // Or, selecting an item earlier in the list.
1182 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1183 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1188 base.OnKeyPress (e);
1191 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1193 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1198 protected override void OnParentBackColorChanged (EventArgs e)
1200 base.OnParentBackColorChanged (e);
1203 protected override void OnResize (EventArgs e)
1206 if (listbox_ctrl != null)
1207 listbox_ctrl.CalcListBoxArea ();
1210 protected override void OnSelectedIndexChanged (EventArgs e)
1212 base.OnSelectedIndexChanged (e);
1214 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1219 protected virtual void OnSelectedItemChanged (EventArgs e)
1223 protected override void OnSelectedValueChanged (EventArgs e)
1225 base.OnSelectedValueChanged (e);
1228 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1230 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1235 protected override void RefreshItem (int index)
1237 if (index < 0 || index >= Items.Count)
1238 throw new ArgumentOutOfRangeException ("index");
1240 if (draw_mode == DrawMode.OwnerDrawVariable)
1241 item_heights.Remove (Items [index]);
1245 protected override void RefreshItems ()
1247 for (int i = 0; i < Items.Count; i++) {
1254 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1255 SetControlText (GetItemText (Items [selected_index]), false);
1258 public override void ResetText ()
1260 Text = String.Empty;
1263 protected override bool ProcessKeyEventArgs (ref Message m)
1265 return base.ProcessKeyEventArgs (ref m);
1268 [EditorBrowsable (EditorBrowsableState.Advanced)]
1269 protected override void OnKeyDown (KeyEventArgs e)
1274 [EditorBrowsable (EditorBrowsableState.Advanced)]
1275 protected override void OnValidating (CancelEventArgs e)
1277 base.OnValidating (e);
1280 [EditorBrowsable (EditorBrowsableState.Advanced)]
1281 protected override void OnTextChanged (EventArgs e)
1283 base.OnTextChanged (e);
1287 protected virtual void OnTextUpdate (EventArgs e)
1289 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1294 protected override void OnMouseLeave (EventArgs e)
1297 if (flat_style == FlatStyle.Popup)
1300 base.OnMouseLeave (e);
1303 protected override void OnMouseEnter (EventArgs e)
1306 if (flat_style == FlatStyle.Popup)
1309 base.OnMouseEnter (e);
1314 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1316 base.ScaleControl (factor, specified);
1320 public void Select (int start, int length)
1323 throw new ArgumentException ("Start cannot be less than zero");
1326 throw new ArgumentException ("length cannot be less than zero");
1328 if (dropdown_style == ComboBoxStyle.DropDownList)
1331 textbox_ctrl.Select (start, length);
1334 public void SelectAll ()
1336 if (dropdown_style == ComboBoxStyle.DropDownList)
1339 if (textbox_ctrl != null) {
1340 textbox_ctrl.ShowSelection = true;
1341 textbox_ctrl.SelectAll ();
1345 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1347 bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1348 bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1350 if ((specified & BoundsSpecified.Height) != 0 ||
1351 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1353 requested_height = height;
1354 height = SnapHeight (height);
1357 base.SetBoundsCore (x, y, width, height, specified);
1360 protected override void SetItemCore (int index, object value)
1362 if (index < 0 || index >= Items.Count)
1365 Items[index] = value;
1368 protected override void SetItemsCore (IList value)
1373 Items.AddRange (value);
1379 public override string ToString ()
1381 return base.ToString () + ", Items.Count:" + Items.Count;
1384 protected override void WndProc (ref Message m)
1386 switch ((Msg) m.Msg) {
1388 case Msg.WM_KEYDOWN:
1389 Keys keys = (Keys) m.WParam.ToInt32 ();
1391 // Don't pass the message to base if auto complete is being used and available.
1392 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1393 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1397 if (keys == Keys.Up || keys == Keys.Down)
1399 goto case Msg.WM_CHAR;
1401 // Call our own handler first and send the message to the TextBox if still needed
1402 if (!ProcessKeyMessage (ref m) && textbox_ctrl != null)
1403 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1405 case Msg.WM_MOUSELEAVE:
1406 Point location = PointToClient (Control.MousePosition);
1407 if (ClientRectangle.Contains (location))
1413 base.WndProc (ref m);
1416 #endregion Public Methods
1418 #region Private Methods
1420 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1421 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1422 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1427 internal override bool InternalCapture {
1428 get { return Capture; }
1432 void LayoutComboBox ()
1434 int border = ThemeEngine.Current.Border3DSize.Width;
1436 text_area = ClientRectangle;
1437 text_area.Height = PreferredHeight;
1439 listbox_area = ClientRectangle;
1440 listbox_area.Y = text_area.Bottom + 3;
1441 listbox_area.Height -= (text_area.Height + 2);
1443 Rectangle prev_button_area = button_area;
1445 if (DropDownStyle == ComboBoxStyle.Simple)
1446 button_area = Rectangle.Empty;
1448 button_area = text_area;
1449 button_area.X = text_area.Right - button_width - border;
1450 button_area.Y = text_area.Y + border;
1451 button_area.Width = button_width;
1452 button_area.Height = text_area.Height - 2 * border;
1454 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1455 button_area.Inflate (1, 1);
1457 button_area.Width -= 2;
1462 if (button_area != prev_button_area) {
1463 prev_button_area.Y -= border;
1464 prev_button_area.Width += border;
1465 prev_button_area.Height += 2 * border;
1466 Invalidate (prev_button_area);
1467 Invalidate (button_area);
1470 if (textbox_ctrl != null) {
1471 int text_border = border + 1;
1472 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1473 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1474 textbox_ctrl.Height = text_area.Height - text_border * 2;
1477 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1478 listbox_ctrl.Location = listbox_area.Location;
1479 listbox_ctrl.CalcListBoxArea ();
1483 private void CreateComboListBox ()
1485 listbox_ctrl = new ComboListBox (this);
1486 listbox_ctrl.HighlightedIndex = SelectedIndex;
1489 internal void Draw (Rectangle clip, Graphics dc)
1491 Theme theme = ThemeEngine.Current;
1492 FlatStyle style = FlatStyle.Standard;
1493 bool is_flat = false;
1496 style = this.FlatStyle;
1497 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1500 theme.ComboBoxDrawBackground (this, dc, clip, style);
1502 int border = theme.Border3DSize.Width;
1504 // No edit control, we paint the edit ourselves
1505 if (dropdown_style == ComboBoxStyle.DropDownList) {
1506 DrawItemState state = DrawItemState.None;
1507 Color back_color = BackColor;
1508 Color fore_color = ForeColor;
1509 Rectangle item_rect = text_area;
1510 item_rect.X += border;
1511 item_rect.Y += border;
1512 item_rect.Width -= (button_area.Width + 2 * border);
1513 item_rect.Height -= 2 * border;
1516 state = DrawItemState.Selected;
1517 state |= DrawItemState.Focus;
1518 back_color = SystemColors.Highlight;
1519 fore_color = SystemColors.HighlightText;
1522 state |= DrawItemState.ComboBoxEdit;
1523 HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, fore_color, back_color));
1526 if (show_dropdown_button) {
1527 ButtonState current_state;
1529 current_state = button_state;
1531 current_state = ButtonState.Inactive;
1533 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1534 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1537 theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1539 theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
1544 internal bool DropDownButtonEntered {
1545 get { return drop_down_button_entered; }
1547 if (drop_down_button_entered == value)
1549 drop_down_button_entered = value;
1550 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1551 Invalidate (button_area);
1555 internal void DropDownListBox ()
1557 DropDownButtonEntered = false;
1559 if (DropDownStyle == ComboBoxStyle.Simple)
1562 if (listbox_ctrl == null)
1563 CreateComboListBox ();
1565 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1567 FindMatchOrSetIndex(SelectedIndex);
1570 if (textbox_ctrl != null)
1571 textbox_ctrl.HideAutoCompleteList ();
1574 if (listbox_ctrl.ShowWindow ())
1575 dropped_down = true;
1577 button_state = ButtonState.Pushed;
1578 if (dropdown_style == ComboBoxStyle.DropDownList)
1579 Invalidate (text_area);
1582 internal void DropDownListBoxFinished ()
1584 if (DropDownStyle == ComboBoxStyle.Simple)
1587 FindMatchOrSetIndex (SelectedIndex);
1588 button_state = ButtonState.Normal;
1589 Invalidate (button_area);
1590 dropped_down = false;
1592 OnDropDownClosed (EventArgs.Empty);
1595 * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1596 * this causes the popup to appear under the main form. This is horrible but necessary
1599 // If the user opens a new form in an event, it will close our dropdown,
1600 // so we need a null check here
1601 if (listbox_ctrl != null) {
1602 listbox_ctrl.Dispose ();
1603 listbox_ctrl = null;
1606 // The auto complete list could have been shown after the listbox,
1607 // so make sure it's hidden.
1608 if (textbox_ctrl != null)
1609 textbox_ctrl.HideAutoCompleteList ();
1613 private int FindStringCaseInsensitive (string search)
1615 if (search.Length == 0) {
1619 for (int i = 0; i < Items.Count; i++)
1621 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1628 // Search in the list for the substring, starting the search at the list
1629 // position specified, the search wraps thus covering all the list.
1630 internal int FindStringCaseInsensitive (string search, int start_index)
1632 if (search.Length == 0) {
1635 // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1636 if (start_index < 0 || start_index > Items.Count)
1637 throw new ArgumentOutOfRangeException("start_index");
1639 for (int i = 0; i < Items.Count; i++) {
1640 int index = (i + start_index) % Items.Count;
1641 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1648 internal override bool IsInputCharInternal (char charCode)
1653 internal override ContextMenu ContextMenuInternal {
1655 return base.ContextMenuInternal;
1658 base.ContextMenuInternal = value;
1659 if (textbox_ctrl != null) {
1660 textbox_ctrl.ContextMenu = value;
1665 internal void RestoreContextMenu ()
1667 textbox_ctrl.RestoreContextMenu ();
1670 private void OnKeyDownCB(object sender, KeyEventArgs e)
1672 if (Items.Count == 0)
1675 // for keyboard navigation, we have to do our own scroll, since
1676 // the default behaviour for the SelectedIndex property is a little different,
1677 // setting the selected index in the top always
1683 FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1686 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1687 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1691 if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1694 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1697 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1698 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1702 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1706 SetSelectedIndex (Math.Max (SelectedIndex - offset, 0), true);
1709 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1710 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1714 if (SelectedIndex == -1) {
1716 if (dropdown_style != ComboBoxStyle.Simple)
1720 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1724 SetSelectedIndex (Math.Min (SelectedIndex + offset, Items.Count - 1), true);
1727 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1728 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1733 if (listbox_ctrl != null && listbox_ctrl.Visible)
1734 DropDownListBoxFinished ();
1738 if (dropdown_style == ComboBoxStyle.DropDownList) {
1742 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1743 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1748 if (dropdown_style == ComboBoxStyle.DropDownList) {
1749 SetSelectedIndex (Items.Count - 1, true);
1752 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1753 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1762 void SetSelectedIndex (int value, bool supressAutoScroll)
1764 if (selected_index == value)
1767 if (value <= -2 || value >= Items.Count)
1768 throw new ArgumentOutOfRangeException ("SelectedIndex");
1770 selected_index = value;
1772 if (dropdown_style != ComboBoxStyle.DropDownList) {
1774 SetControlText (string.Empty, false, supressAutoScroll);
1776 SetControlText (GetItemText (Items [value]), false, supressAutoScroll);
1779 if (DropDownStyle == ComboBoxStyle.DropDownList)
1782 if (listbox_ctrl != null)
1783 listbox_ctrl.HighlightedIndex = value;
1785 OnSelectedValueChanged (EventArgs.Empty);
1786 OnSelectedIndexChanged (EventArgs.Empty);
1787 OnSelectedItemChanged (EventArgs.Empty);
1790 // If no item is currently selected, and an item is found matching the text
1791 // in the textbox, then selected that item. Otherwise the item at the given
1792 // index is selected.
1793 private void FindMatchOrSetIndex(int index)
1796 if (SelectedIndex == -1 && Text.Length != 0)
1797 match = FindStringCaseInsensitive(Text);
1799 SetSelectedIndex (match, true);
1801 SetSelectedIndex (index, true);
1804 void OnMouseDownCB (object sender, MouseEventArgs e)
1807 if (DropDownStyle == ComboBoxStyle.DropDownList)
1808 area = ClientRectangle;
1812 if (area.Contains (e.X, e.Y)) {
1813 if (Items.Count > 0)
1816 button_state = ButtonState.Pushed;
1817 OnDropDown (EventArgs.Empty);
1820 Invalidate (button_area);
1826 void OnMouseEnter (object sender, EventArgs e)
1828 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1832 void OnMouseLeave (object sender, EventArgs e)
1834 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1835 drop_down_button_entered = false;
1838 if (show_dropdown_button)
1839 DropDownButtonEntered = false;
1843 void OnMouseMoveCB (object sender, MouseEventArgs e)
1845 if (show_dropdown_button && !dropped_down)
1846 DropDownButtonEntered = button_area.Contains (e.Location);
1848 if (DropDownStyle == ComboBoxStyle.Simple)
1851 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1852 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1853 if (listbox_ctrl.ClientRectangle.Contains (location))
1854 listbox_ctrl.Capture = true;
1858 void OnMouseUpCB (object sender, MouseEventArgs e)
1862 button_state = ButtonState.Normal;
1863 Invalidate (button_area);
1865 OnClick (EventArgs.Empty);
1868 listbox_ctrl.Capture = true;
1871 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1873 if (Items.Count == 0)
1876 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1877 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1878 listbox_ctrl.Scroll (-lines);
1880 int lines = me.Delta / 120;
1881 int index = SelectedIndex - lines;
1884 else if (index >= Items.Count)
1885 index = Items.Count - 1;
1886 SelectedIndex = index;
1890 internal override void OnPaintInternal (PaintEventArgs pevent)
1892 if (suspend_ctrlupdate)
1895 Draw (ClientRectangle, pevent.Graphics);
1898 private void OnTextBoxClick (object sender, EventArgs e)
1903 private void OnTextChangedEdit (object sender, EventArgs e)
1905 if (process_textchanged_event == false)
1908 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1911 // Setting base.Text below will raise this event
1912 // if we found something
1913 OnTextChanged (EventArgs.Empty);
1917 if (listbox_ctrl != null) {
1919 if (process_texchanged_autoscroll)
1920 listbox_ctrl.EnsureTop (item);
1923 base.Text = textbox_ctrl.Text;
1926 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1928 selected_index = -1;
1929 if (listbox_ctrl != null)
1930 listbox_ctrl.HighlightedIndex = -1;
1933 internal void SetControlText (string s, bool suppressTextChanged)
1935 SetControlText (s, suppressTextChanged, false);
1938 internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
1940 if (suppressTextChanged)
1941 process_textchanged_event = false;
1942 if (supressAutoScroll)
1943 process_texchanged_autoscroll = false;
1945 textbox_ctrl.Text = s;
1946 textbox_ctrl.SelectAll ();
1947 process_textchanged_event = true;
1948 process_texchanged_autoscroll = true;
1951 void UpdateComboBoxBounds ()
1953 if (requested_height == -1)
1956 // Save the requested height since set bounds can destroy it
1957 int save_height = requested_height;
1958 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1959 BoundsSpecified.Height);
1960 requested_height = save_height;
1963 int SnapHeight (int height)
1965 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1966 if (IntegralHeight) {
1967 int border = ThemeEngine.Current.Border3DSize.Height;
1968 int lb_height = (height - PreferredHeight - 2) - border * 2;
1969 if (lb_height > ItemHeight) {
1970 int partial = (lb_height) % ItemHeight;
1972 } else if (lb_height < ItemHeight)
1973 height = PreferredHeight;
1976 height = PreferredHeight;
1981 private void UpdatedItems ()
1983 if (listbox_ctrl != null) {
1984 listbox_ctrl.UpdateLastVisibleItem ();
1985 listbox_ctrl.CalcListBoxArea ();
1986 listbox_ctrl.Refresh ();
1990 #endregion Private Methods
1992 [ListBindableAttribute (false)]
1993 public class ObjectCollection : IList, ICollection, IEnumerable
1996 private ComboBox owner;
1997 internal ArrayList object_items = new ArrayList ();
1999 #region UIA Framework Events
2003 // We are using Reflection to add/remove internal events.
2004 // Class ListProvider uses the events.
2006 //Event used to generate UIA StructureChangedEvent
2007 static object UIACollectionChangedEvent = new object ();
2009 internal event CollectionChangeEventHandler UIACollectionChanged {
2010 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
2011 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
2014 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
2016 CollectionChangeEventHandler eh
2017 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
2023 #endregion UIA Framework Events
2025 public ObjectCollection (ComboBox owner)
2030 #region Public Properties
2032 get { return object_items.Count; }
2035 public bool IsReadOnly {
2036 get { return false; }
2040 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2041 public virtual object this [int index] {
2043 if (index < 0 || index >= Count)
2044 throw new ArgumentOutOfRangeException ("index");
2046 return object_items[index];
2049 if (index < 0 || index >= Count)
2050 throw new ArgumentOutOfRangeException ("index");
2052 throw new ArgumentNullException ("value");
2055 //UIA Framework event: Item Removed
2056 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2059 object_items[index] = value;
2062 //UIA Framework event: Item Added
2063 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2066 if (owner.listbox_ctrl != null)
2067 owner.listbox_ctrl.InvalidateItem (index);
2068 if (index == owner.SelectedIndex) {
2069 if (owner.textbox_ctrl == null)
2072 owner.textbox_ctrl.SelectedText = value.ToString ();
2077 bool ICollection.IsSynchronized {
2078 get { return false; }
2081 object ICollection.SyncRoot {
2082 get { return this; }
2085 bool IList.IsFixedSize {
2086 get { return false; }
2089 #endregion Public Properties
2091 #region Public Methods
2092 public int Add (object item)
2096 idx = AddItem (item, false);
2097 owner.UpdatedItems ();
2101 public void AddRange (object[] items)
2104 throw new ArgumentNullException ("items");
2106 foreach (object mi in items)
2112 owner.UpdatedItems ();
2115 public void Clear ()
2117 owner.selected_index = -1;
2118 object_items.Clear ();
2119 owner.UpdatedItems ();
2123 //UIA Framework event: Items list cleared
2124 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2128 public bool Contains (object value)
2131 throw new ArgumentNullException ("value");
2133 return object_items.Contains (value);
2137 public void CopyTo (object [] destination, int arrayIndex)
2139 object_items.CopyTo (destination, arrayIndex);
2142 void ICollection.CopyTo (Array destination, int index)
2144 object_items.CopyTo (destination, index);
2147 public void CopyTo (object [] dest, int arrayIndex)
2149 object_items.CopyTo (dest, arrayIndex);
2152 void ICollection.CopyTo (Array dest, int index)
2154 object_items.CopyTo (dest, index);
2158 public IEnumerator GetEnumerator ()
2160 return object_items.GetEnumerator ();
2163 int IList.Add (object item)
2168 public int IndexOf (object value)
2171 throw new ArgumentNullException ("value");
2173 return object_items.IndexOf (value);
2176 public void Insert (int index, object item)
2178 if (index < 0 || index > Count)
2179 throw new ArgumentOutOfRangeException ("index");
2181 throw new ArgumentNullException ("item");
2183 owner.BeginUpdate ();
2186 AddItem (item, false);
2188 object_items.Insert (index, item);
2190 //UIA Framework event: Item added
2191 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2195 owner.EndUpdate (); // Calls UpdatedItems
2198 public void Remove (object value)
2203 if (IndexOf (value) == owner.SelectedIndex)
2204 owner.SelectedIndex = -1;
2206 RemoveAt (IndexOf (value));
2209 public void RemoveAt (int index)
2211 if (index < 0 || index >= Count)
2212 throw new ArgumentOutOfRangeException ("index");
2214 if (index == owner.SelectedIndex)
2215 owner.SelectedIndex = -1;
2218 object removed = object_items [index];
2222 object_items.RemoveAt (index);
2223 owner.UpdatedItems ();
2226 //UIA Framework event: Item removed
2227 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2230 #endregion Public Methods
2232 #region Private Methods
2233 private int AddItem (object item, bool suspend)
2235 // suspend means do not sort as we put new items in, we will do a
2236 // big sort at the end
2238 throw new ArgumentNullException ("item");
2240 if (owner.Sorted && !suspend) {
2242 foreach (object o in object_items) {
2243 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2244 object_items.Insert (index, item);
2246 // If we added the new item before the selectedindex
2247 // bump the selectedindex by one, behavior differs if
2248 // Handle has not been created.
2249 if (index <= owner.selected_index && owner.IsHandleCreated)
2250 owner.selected_index++;
2253 //UIA Framework event: Item added
2254 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2262 object_items.Add (item);
2265 //UIA Framework event: Item added
2266 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2269 return object_items.Count - 1;
2272 internal void AddRange (IList items)
2274 foreach (object mi in items)
2275 AddItem (mi, false);
2280 owner.UpdatedItems ();
2283 internal void Sort ()
2285 // If the objects the user put here don't have their own comparer,
2286 // use one that compares based on the object's ToString
2287 if (object_items.Count > 0 && object_items[0] is IComparer)
2288 object_items.Sort ();
2290 object_items.Sort (new ObjectComparer (owner));
2293 private class ObjectComparer : IComparer
2295 private ListControl owner;
2297 public ObjectComparer (ListControl owner)
2302 #region IComparer Members
2303 public int Compare (object x, object y)
2305 return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2309 #endregion Private Methods
2312 internal class ComboTextBox : TextBox {
2314 private ComboBox owner;
2316 public ComboTextBox (ComboBox owner)
2319 ShowSelection = false;
2320 HideSelection = false;
2322 owner.LostFocus += OwnerLostFocusHandler;
2327 void OwnerLostFocusHandler (object o, EventArgs args)
2329 if (IsAutoCompleteAvailable)
2333 protected override void OnKeyDown (KeyEventArgs args)
2335 if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
2338 base.OnKeyDown (args);
2341 internal override void OnAutoCompleteValueSelected (EventArgs args)
2343 base.OnAutoCompleteValueSelected (args);
2348 internal void SetSelectable (bool selectable)
2350 SetStyle (ControlStyles.Selectable, selectable);
2353 internal void ActivateCaret (bool active)
2356 document.CaretHasFocus ();
2358 document.CaretLostFocus ();
2362 internal override void OnTextUpdate ()
2364 base.OnTextUpdate ();
2365 owner.OnTextUpdate (EventArgs.Empty);
2369 protected override void OnGotFocus (EventArgs e)
2371 owner.Select (false, true);
2374 protected override void OnLostFocus (EventArgs e)
2376 owner.Select (false, true);
2379 public override bool Focused {
2381 return owner.Focused;
2385 internal override bool ActivateOnShow { get { return false; } }
2388 internal class ComboListBox : Control
2390 private ComboBox owner;
2391 private VScrollBarLB vscrollbar_ctrl;
2392 private int top_item; /* First item that we show the in the current page */
2393 private int last_item; /* Last visible item */
2394 internal int page_size; /* Number of listbox items per page */
2395 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2397 internal enum ItemNavigation
2407 #region UIA Framework: Properties
2409 internal int UIATopItem {
2410 get { return top_item; }
2413 internal int UIALastItem {
2414 get { return last_item; }
2417 internal ScrollBar UIAVScrollBar {
2418 get { return vscrollbar_ctrl; }
2423 class VScrollBarLB : VScrollBar
2425 public VScrollBarLB ()
2429 internal override bool InternalCapture {
2430 get { return Capture; }
2434 public void FireMouseDown (MouseEventArgs e)
2439 e = TranslateEvent (e);
2443 public void FireMouseUp (MouseEventArgs e)
2448 e = TranslateEvent (e);
2452 public void FireMouseMove (MouseEventArgs e)
2457 e = TranslateEvent (e);
2461 MouseEventArgs TranslateEvent (MouseEventArgs e)
2463 Point loc = PointToClient (Control.MousePosition);
2464 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2468 public ComboListBox (ComboBox owner)
2475 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2477 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2478 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2480 this.is_visible = false;
2482 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2483 InternalBorderStyle = BorderStyle.Fixed3D;
2485 InternalBorderStyle = BorderStyle.FixedSingle;
2488 protected override CreateParams CreateParams
2491 CreateParams cp = base.CreateParams;
2492 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2495 cp.Style ^= (int)WindowStyles.WS_CHILD;
2496 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2497 cp.Style |= (int)WindowStyles.WS_POPUP;
2498 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2503 internal override bool InternalCapture {
2512 internal override bool ActivateOnShow { get { return false; } }
2513 #region Private Methods
2515 // Calcs the listbox area
2516 internal void CalcListBoxArea ()
2519 bool show_scrollbar;
2521 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2522 Rectangle area = owner.listbox_area;
2524 height = area.Height;
2525 show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
2527 // No calculation needed
2528 if (height <= 0 || width <= 0)
2532 else { // DropDown or DropDownList
2534 width = owner.DropDownWidth;
2535 int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2537 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2539 for (int i = 0; i < visible_items_count; i++) {
2540 height += owner.GetItemHeight (i);
2543 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2547 if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
2548 height = owner.ItemHeight * visible_items_count;
2549 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2551 // ignore visible items count, and use manual height instead
2552 height = owner.DropDownHeight;
2553 show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
2556 height = owner.ItemHeight * visible_items_count;
2557 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2562 page_size = Math.Max (height / owner.ItemHeight, 1);
2564 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2565 if (!show_scrollbar) {
2567 if (vscrollbar_ctrl != null)
2568 vscrollbar_ctrl.Visible = false;
2569 if (dropdown_style != ComboBoxStyle.Simple)
2570 height = owner.ItemHeight * owner.items.Count;
2572 /* Need vertical scrollbar */
2573 if (vscrollbar_ctrl == null) {
2574 vscrollbar_ctrl = new VScrollBarLB ();
2575 vscrollbar_ctrl.Minimum = 0;
2576 vscrollbar_ctrl.SmallChange = 1;
2577 vscrollbar_ctrl.LargeChange = 1;
2578 vscrollbar_ctrl.Maximum = 0;
2579 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2580 Controls.AddImplicit (vscrollbar_ctrl);
2583 vscrollbar_ctrl.Dock = DockStyle.Right;
2585 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2587 int large = page_size;
2589 int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2593 vscrollbar_ctrl.LargeChange = large;
2594 vscrollbar_ctrl.Visible = true;
2596 int hli = HighlightedIndex;
2598 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2599 vscrollbar_ctrl.Value = hli;
2603 Size = new Size (width, height);
2604 textarea_drawable = ClientRectangle;
2605 textarea_drawable.Width = width;
2606 textarea_drawable.Height = height;
2608 if (vscrollbar_ctrl != null && show_scrollbar)
2609 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2611 last_item = LastVisibleItem ();
2614 private void Draw (Rectangle clip, Graphics dc)
2616 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2618 if (owner.Items.Count > 0) {
2620 for (int i = top_item; i <= last_item; i++) {
2621 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2623 if (!clip.IntersectsWith (item_rect))
2626 DrawItemState state = DrawItemState.None;
2627 Color back_color = owner.BackColor;
2628 Color fore_color = owner.ForeColor;
2630 if (i == HighlightedIndex) {
2631 state |= DrawItemState.Selected;
2632 back_color = SystemColors.Highlight;
2633 fore_color = SystemColors.HighlightText;
2635 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2636 state |= DrawItemState.Focus;
2640 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2641 i, state, fore_color, back_color));
2646 int highlighted_index = -1;
2648 public int HighlightedIndex {
2649 get { return highlighted_index; }
2651 if (highlighted_index == value)
2654 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2655 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2656 highlighted_index = value;
2657 if (highlighted_index != -1)
2658 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2662 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2664 if (index < 0 || index >= owner.Items.Count)
2665 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2667 Rectangle item_rect = new Rectangle ();
2668 int height = owner.GetItemHeight (index);
2671 item_rect.Width = textarea_drawable.Width;
2672 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2674 for (int i = top_index; i < index; i++)
2675 item_rect.Y += owner.GetItemHeight (i);
2677 item_rect.Y = height * (index - top_index);
2679 item_rect.Height = height;
2683 public void HideWindow ()
2685 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2690 owner.DropDownListBoxFinished ();
2693 private int IndexFromPointDisplayRectangle (int x, int y)
2695 for (int i = top_item; i <= last_item; i++) {
2696 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2703 public void InvalidateItem (int index)
2706 Invalidate (GetItemDisplayRectangle (index, top_item));
2709 public int LastVisibleItem ()
2711 Rectangle item_rect;
2712 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2715 for (i = top_item; i < owner.Items.Count; i++) {
2716 item_rect = GetItemDisplayRectangle (i, top_item);
2717 if (item_rect.Y + item_rect.Height > top_y) {
2724 public void SetTopItem (int item)
2726 if (top_item == item)
2729 UpdateLastVisibleItem ();
2733 public int FirstVisibleItem ()
2738 public void EnsureTop (int item)
2740 if (owner.Items.Count == 0)
2742 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2745 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2748 else if (item < vscrollbar_ctrl.Minimum)
2749 item = vscrollbar_ctrl.Minimum;
2751 vscrollbar_ctrl.Value = item;
2754 bool scrollbar_grabbed = false;
2758 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2761 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2765 protected override void OnMouseDown (MouseEventArgs e)
2768 vscrollbar_ctrl.FireMouseDown (e);
2769 scrollbar_grabbed = true;
2773 protected override void OnMouseMove (MouseEventArgs e)
2775 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2778 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2779 vscrollbar_ctrl.FireMouseMove (e);
2783 Point pt = PointToClient (Control.MousePosition);
2784 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2787 HighlightedIndex = index;
2790 protected override void OnMouseUp (MouseEventArgs e)
2792 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2794 if (scrollbar_grabbed) {
2795 vscrollbar_ctrl.FireMouseUp (e);
2796 scrollbar_grabbed = false;
2798 HighlightedIndex = index;
2807 bool is_change = owner.SelectedIndex != index;
2809 owner.SetSelectedIndex (index, true);
2810 owner.OnSelectionChangeCommitted (new EventArgs ());
2812 // If the user selected the already selected item, SelectedIndex
2813 // won't fire these events, but .Net does, so we do it here
2815 owner.OnSelectedValueChanged (EventArgs.Empty);
2816 owner.OnSelectedIndexChanged (EventArgs.Empty);
2822 internal override void OnPaintInternal (PaintEventArgs pevent)
2824 Draw (pevent.ClipRectangle,pevent.Graphics);
2827 public bool ShowWindow ()
2829 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2832 HighlightedIndex = owner.SelectedIndex;
2838 owner.OnDropDown (EventArgs.Empty);
2842 public void UpdateLastVisibleItem ()
2844 last_item = LastVisibleItem ();
2847 public void Scroll (int delta)
2849 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2852 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2854 int val = vscrollbar_ctrl.Value + delta;
2857 else if (val < vscrollbar_ctrl.Minimum)
2858 val = vscrollbar_ctrl.Minimum;
2859 vscrollbar_ctrl.Value = val;
2862 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2864 if (owner.Items.Count == 0)
2867 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2872 private void VerticalScrollEvent (object sender, EventArgs e)
2874 if (top_item == vscrollbar_ctrl.Value)
2877 top_item = vscrollbar_ctrl.Value;
2878 UpdateLastVisibleItem ();
2882 protected override void WndProc(ref Message m) {
2883 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2884 owner.Select (false, true);
2886 base.WndProc (ref m);
2889 #endregion Private Methods