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 = Enabled;
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 MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
1892 Point loc = PointToClient (Control.MousePosition);
1893 return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
1896 internal override void OnPaintInternal (PaintEventArgs pevent)
1898 if (suspend_ctrlupdate)
1901 Draw (ClientRectangle, pevent.Graphics);
1904 private void OnTextBoxClick (object sender, EventArgs e)
1909 private void OnTextChangedEdit (object sender, EventArgs e)
1911 if (process_textchanged_event == false)
1914 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1917 // Setting base.Text below will raise this event
1918 // if we found something
1919 OnTextChanged (EventArgs.Empty);
1923 if (listbox_ctrl != null) {
1925 if (process_texchanged_autoscroll)
1926 listbox_ctrl.EnsureTop (item);
1929 base.Text = textbox_ctrl.Text;
1932 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1934 selected_index = -1;
1935 if (listbox_ctrl != null)
1936 listbox_ctrl.HighlightedIndex = -1;
1939 internal void SetControlText (string s, bool suppressTextChanged)
1941 SetControlText (s, suppressTextChanged, false);
1944 internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
1946 if (suppressTextChanged)
1947 process_textchanged_event = false;
1948 if (supressAutoScroll)
1949 process_texchanged_autoscroll = false;
1951 textbox_ctrl.Text = s;
1952 textbox_ctrl.SelectAll ();
1953 process_textchanged_event = true;
1954 process_texchanged_autoscroll = true;
1957 void UpdateComboBoxBounds ()
1959 if (requested_height == -1)
1962 // Save the requested height since set bounds can destroy it
1963 int save_height = requested_height;
1964 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1965 BoundsSpecified.Height);
1966 requested_height = save_height;
1969 int SnapHeight (int height)
1971 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1972 if (IntegralHeight) {
1973 int border = ThemeEngine.Current.Border3DSize.Height;
1974 int lb_height = (height - PreferredHeight - 2) - border * 2;
1975 if (lb_height > ItemHeight) {
1976 int partial = (lb_height) % ItemHeight;
1978 } else if (lb_height < ItemHeight)
1979 height = PreferredHeight;
1982 height = PreferredHeight;
1987 private void UpdatedItems ()
1989 if (listbox_ctrl != null) {
1990 listbox_ctrl.UpdateLastVisibleItem ();
1991 listbox_ctrl.CalcListBoxArea ();
1992 listbox_ctrl.Refresh ();
1996 #endregion Private Methods
1998 [ListBindableAttribute (false)]
1999 public class ObjectCollection : IList, ICollection, IEnumerable
2002 private ComboBox owner;
2003 internal ArrayList object_items = new ArrayList ();
2005 #region UIA Framework Events
2009 // We are using Reflection to add/remove internal events.
2010 // Class ListProvider uses the events.
2012 //Event used to generate UIA StructureChangedEvent
2013 static object UIACollectionChangedEvent = new object ();
2015 internal event CollectionChangeEventHandler UIACollectionChanged {
2016 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
2017 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
2020 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
2022 CollectionChangeEventHandler eh
2023 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
2029 #endregion UIA Framework Events
2031 public ObjectCollection (ComboBox owner)
2036 #region Public Properties
2038 get { return object_items.Count; }
2041 public bool IsReadOnly {
2042 get { return false; }
2046 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2047 public virtual object this [int index] {
2049 if (index < 0 || index >= Count)
2050 throw new ArgumentOutOfRangeException ("index");
2052 return object_items[index];
2055 if (index < 0 || index >= Count)
2056 throw new ArgumentOutOfRangeException ("index");
2058 throw new ArgumentNullException ("value");
2061 //UIA Framework event: Item Removed
2062 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2065 object_items[index] = value;
2068 //UIA Framework event: Item Added
2069 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2072 if (owner.listbox_ctrl != null)
2073 owner.listbox_ctrl.InvalidateItem (index);
2074 if (index == owner.SelectedIndex) {
2075 if (owner.textbox_ctrl == null)
2078 owner.textbox_ctrl.SelectedText = value.ToString ();
2083 bool ICollection.IsSynchronized {
2084 get { return false; }
2087 object ICollection.SyncRoot {
2088 get { return this; }
2091 bool IList.IsFixedSize {
2092 get { return false; }
2095 #endregion Public Properties
2097 #region Public Methods
2098 public int Add (object item)
2102 idx = AddItem (item, false);
2103 owner.UpdatedItems ();
2107 public void AddRange (object[] items)
2110 throw new ArgumentNullException ("items");
2112 foreach (object mi in items)
2118 owner.UpdatedItems ();
2121 public void Clear ()
2123 owner.selected_index = -1;
2124 object_items.Clear ();
2125 owner.UpdatedItems ();
2129 //UIA Framework event: Items list cleared
2130 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2134 public bool Contains (object value)
2137 throw new ArgumentNullException ("value");
2139 return object_items.Contains (value);
2143 public void CopyTo (object [] destination, int arrayIndex)
2145 object_items.CopyTo (destination, arrayIndex);
2148 void ICollection.CopyTo (Array destination, int index)
2150 object_items.CopyTo (destination, index);
2153 public void CopyTo (object [] dest, int arrayIndex)
2155 object_items.CopyTo (dest, arrayIndex);
2158 void ICollection.CopyTo (Array dest, int index)
2160 object_items.CopyTo (dest, index);
2164 public IEnumerator GetEnumerator ()
2166 return object_items.GetEnumerator ();
2169 int IList.Add (object item)
2174 public int IndexOf (object value)
2177 throw new ArgumentNullException ("value");
2179 return object_items.IndexOf (value);
2182 public void Insert (int index, object item)
2184 if (index < 0 || index > Count)
2185 throw new ArgumentOutOfRangeException ("index");
2187 throw new ArgumentNullException ("item");
2189 owner.BeginUpdate ();
2192 AddItem (item, false);
2194 object_items.Insert (index, item);
2196 //UIA Framework event: Item added
2197 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2201 owner.EndUpdate (); // Calls UpdatedItems
2204 public void Remove (object value)
2209 if (IndexOf (value) == owner.SelectedIndex)
2210 owner.SelectedIndex = -1;
2212 RemoveAt (IndexOf (value));
2215 public void RemoveAt (int index)
2217 if (index < 0 || index >= Count)
2218 throw new ArgumentOutOfRangeException ("index");
2220 if (index == owner.SelectedIndex)
2221 owner.SelectedIndex = -1;
2224 object removed = object_items [index];
2228 object_items.RemoveAt (index);
2229 owner.UpdatedItems ();
2232 //UIA Framework event: Item removed
2233 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2236 #endregion Public Methods
2238 #region Private Methods
2239 private int AddItem (object item, bool suspend)
2241 // suspend means do not sort as we put new items in, we will do a
2242 // big sort at the end
2244 throw new ArgumentNullException ("item");
2246 if (owner.Sorted && !suspend) {
2248 foreach (object o in object_items) {
2249 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2250 object_items.Insert (index, item);
2252 // If we added the new item before the selectedindex
2253 // bump the selectedindex by one, behavior differs if
2254 // Handle has not been created.
2255 if (index <= owner.selected_index && owner.IsHandleCreated)
2256 owner.selected_index++;
2259 //UIA Framework event: Item added
2260 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2268 object_items.Add (item);
2271 //UIA Framework event: Item added
2272 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2275 return object_items.Count - 1;
2278 internal void AddRange (IList items)
2280 foreach (object mi in items)
2281 AddItem (mi, false);
2286 owner.UpdatedItems ();
2289 internal void Sort ()
2291 // If the objects the user put here don't have their own comparer,
2292 // use one that compares based on the object's ToString
2293 if (object_items.Count > 0 && object_items[0] is IComparer)
2294 object_items.Sort ();
2296 object_items.Sort (new ObjectComparer (owner));
2299 private class ObjectComparer : IComparer
2301 private ListControl owner;
2303 public ObjectComparer (ListControl owner)
2308 #region IComparer Members
2309 public int Compare (object x, object y)
2311 return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2315 #endregion Private Methods
2318 internal class ComboTextBox : TextBox {
2320 private ComboBox owner;
2322 public ComboTextBox (ComboBox owner)
2325 ShowSelection = false;
2326 owner.EnabledChanged += OwnerEnabledChangedHandler;
2328 owner.LostFocus += OwnerLostFocusHandler;
2332 void OwnerEnabledChangedHandler (object o, EventArgs args)
2334 ShowSelection = owner.Focused && owner.Enabled;
2338 void OwnerLostFocusHandler (object o, EventArgs args)
2340 if (IsAutoCompleteAvailable)
2344 protected override void OnKeyDown (KeyEventArgs args)
2346 if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
2349 base.OnKeyDown (args);
2352 internal override void OnAutoCompleteValueSelected (EventArgs args)
2354 base.OnAutoCompleteValueSelected (args);
2359 internal void SetSelectable (bool selectable)
2361 SetStyle (ControlStyles.Selectable, selectable);
2364 internal void ActivateCaret (bool active)
2367 document.CaretHasFocus ();
2369 document.CaretLostFocus ();
2373 internal override void OnTextUpdate ()
2375 base.OnTextUpdate ();
2376 owner.OnTextUpdate (EventArgs.Empty);
2380 protected override void OnGotFocus (EventArgs e)
2382 owner.Select (false, true);
2385 protected override void OnLostFocus (EventArgs e)
2387 owner.Select (false, true);
2390 // We have to pass these events to our owner - MouseMove is not, however.
2392 protected override void OnMouseDown (MouseEventArgs e)
2394 base.OnMouseDown (e);
2395 owner.OnMouseDown (owner.TranslateMouseEventArgs (e));
2398 protected override void OnMouseUp (MouseEventArgs e)
2401 owner.OnMouseUp (owner.TranslateMouseEventArgs (e));
2404 protected override void OnMouseClick (MouseEventArgs e)
2406 base.OnMouseClick (e);
2407 owner.OnMouseClick (owner.TranslateMouseEventArgs (e));
2410 protected override void OnMouseDoubleClick (MouseEventArgs e)
2412 base.OnMouseDoubleClick (e);
2413 owner.OnMouseDoubleClick (owner.TranslateMouseEventArgs (e));
2416 public override bool Focused {
2418 return owner.Focused;
2422 internal override bool ActivateOnShow { get { return false; } }
2425 internal class ComboListBox : Control
2427 private ComboBox owner;
2428 private VScrollBarLB vscrollbar_ctrl;
2429 private int top_item; /* First item that we show the in the current page */
2430 private int last_item; /* Last visible item */
2431 internal int page_size; /* Number of listbox items per page */
2432 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2434 internal enum ItemNavigation
2444 #region UIA Framework: Properties
2446 internal int UIATopItem {
2447 get { return top_item; }
2450 internal int UIALastItem {
2451 get { return last_item; }
2454 internal ScrollBar UIAVScrollBar {
2455 get { return vscrollbar_ctrl; }
2460 class VScrollBarLB : VScrollBar
2462 public VScrollBarLB ()
2466 internal override bool InternalCapture {
2467 get { return Capture; }
2471 public void FireMouseDown (MouseEventArgs e)
2476 e = TranslateEvent (e);
2480 public void FireMouseUp (MouseEventArgs e)
2485 e = TranslateEvent (e);
2489 public void FireMouseMove (MouseEventArgs e)
2494 e = TranslateEvent (e);
2498 MouseEventArgs TranslateEvent (MouseEventArgs e)
2500 Point loc = PointToClient (Control.MousePosition);
2501 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2505 public ComboListBox (ComboBox owner)
2512 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2514 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2515 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2517 this.is_visible = false;
2519 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2520 InternalBorderStyle = BorderStyle.Fixed3D;
2522 InternalBorderStyle = BorderStyle.FixedSingle;
2525 protected override CreateParams CreateParams
2528 CreateParams cp = base.CreateParams;
2529 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2532 cp.Style ^= (int)WindowStyles.WS_CHILD;
2533 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2534 cp.Style |= (int)WindowStyles.WS_POPUP;
2535 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2540 internal override bool InternalCapture {
2549 internal override bool ActivateOnShow { get { return false; } }
2550 #region Private Methods
2552 // Calcs the listbox area
2553 internal void CalcListBoxArea ()
2556 bool show_scrollbar;
2558 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2559 Rectangle area = owner.listbox_area;
2561 height = area.Height;
2562 show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
2564 // No calculation needed
2565 if (height <= 0 || width <= 0)
2569 else { // DropDown or DropDownList
2571 width = owner.DropDownWidth;
2572 int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2574 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2576 for (int i = 0; i < visible_items_count; i++) {
2577 height += owner.GetItemHeight (i);
2580 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2584 if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
2585 height = owner.ItemHeight * visible_items_count;
2586 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2588 // ignore visible items count, and use manual height instead
2589 height = owner.DropDownHeight;
2590 show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
2593 height = owner.ItemHeight * visible_items_count;
2594 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2599 page_size = Math.Max (height / owner.ItemHeight, 1);
2601 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2602 if (!show_scrollbar) {
2604 if (vscrollbar_ctrl != null)
2605 vscrollbar_ctrl.Visible = false;
2606 if (dropdown_style != ComboBoxStyle.Simple)
2607 height = owner.ItemHeight * owner.items.Count;
2609 /* Need vertical scrollbar */
2610 if (vscrollbar_ctrl == null) {
2611 vscrollbar_ctrl = new VScrollBarLB ();
2612 vscrollbar_ctrl.Minimum = 0;
2613 vscrollbar_ctrl.SmallChange = 1;
2614 vscrollbar_ctrl.LargeChange = 1;
2615 vscrollbar_ctrl.Maximum = 0;
2616 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2617 Controls.AddImplicit (vscrollbar_ctrl);
2620 vscrollbar_ctrl.Dock = DockStyle.Right;
2622 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2624 int large = page_size;
2626 int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2630 vscrollbar_ctrl.LargeChange = large;
2631 vscrollbar_ctrl.Visible = true;
2633 int hli = HighlightedIndex;
2635 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2636 vscrollbar_ctrl.Value = hli;
2640 Size = new Size (width, height);
2641 textarea_drawable = ClientRectangle;
2642 textarea_drawable.Width = width;
2643 textarea_drawable.Height = height;
2645 if (vscrollbar_ctrl != null && show_scrollbar)
2646 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2648 last_item = LastVisibleItem ();
2651 private void Draw (Rectangle clip, Graphics dc)
2653 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2655 if (owner.Items.Count > 0) {
2657 for (int i = top_item; i <= last_item; i++) {
2658 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2660 if (!clip.IntersectsWith (item_rect))
2663 DrawItemState state = DrawItemState.None;
2664 Color back_color = owner.BackColor;
2665 Color fore_color = owner.ForeColor;
2667 if (i == HighlightedIndex) {
2668 state |= DrawItemState.Selected;
2669 back_color = SystemColors.Highlight;
2670 fore_color = SystemColors.HighlightText;
2672 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2673 state |= DrawItemState.Focus;
2677 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2678 i, state, fore_color, back_color));
2683 int highlighted_index = -1;
2685 public int HighlightedIndex {
2686 get { return highlighted_index; }
2688 if (highlighted_index == value)
2691 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2692 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2693 highlighted_index = value;
2694 if (highlighted_index != -1)
2695 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2699 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2701 if (index < 0 || index >= owner.Items.Count)
2702 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2704 Rectangle item_rect = new Rectangle ();
2705 int height = owner.GetItemHeight (index);
2708 item_rect.Width = textarea_drawable.Width;
2709 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2711 for (int i = top_index; i < index; i++)
2712 item_rect.Y += owner.GetItemHeight (i);
2714 item_rect.Y = height * (index - top_index);
2716 item_rect.Height = height;
2720 public void HideWindow ()
2722 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2727 owner.DropDownListBoxFinished ();
2730 private int IndexFromPointDisplayRectangle (int x, int y)
2732 for (int i = top_item; i <= last_item; i++) {
2733 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2740 public void InvalidateItem (int index)
2743 Invalidate (GetItemDisplayRectangle (index, top_item));
2746 public int LastVisibleItem ()
2748 Rectangle item_rect;
2749 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2752 for (i = top_item; i < owner.Items.Count; i++) {
2753 item_rect = GetItemDisplayRectangle (i, top_item);
2754 if (item_rect.Y + item_rect.Height > top_y) {
2761 public void SetTopItem (int item)
2763 if (top_item == item)
2766 UpdateLastVisibleItem ();
2770 public int FirstVisibleItem ()
2775 public void EnsureTop (int item)
2777 if (owner.Items.Count == 0)
2779 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2782 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2785 else if (item < vscrollbar_ctrl.Minimum)
2786 item = vscrollbar_ctrl.Minimum;
2788 vscrollbar_ctrl.Value = item;
2791 bool scrollbar_grabbed = false;
2795 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2798 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2802 protected override void OnMouseDown (MouseEventArgs e)
2805 vscrollbar_ctrl.FireMouseDown (e);
2806 scrollbar_grabbed = true;
2810 protected override void OnMouseMove (MouseEventArgs e)
2812 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2815 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2816 vscrollbar_ctrl.FireMouseMove (e);
2820 Point pt = PointToClient (Control.MousePosition);
2821 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2824 HighlightedIndex = index;
2827 protected override void OnMouseUp (MouseEventArgs e)
2829 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2831 if (scrollbar_grabbed) {
2832 vscrollbar_ctrl.FireMouseUp (e);
2833 scrollbar_grabbed = false;
2835 HighlightedIndex = index;
2844 bool is_change = owner.SelectedIndex != index;
2846 owner.SetSelectedIndex (index, true);
2847 owner.OnSelectionChangeCommitted (new EventArgs ());
2849 // If the user selected the already selected item, SelectedIndex
2850 // won't fire these events, but .Net does, so we do it here
2852 owner.OnSelectedValueChanged (EventArgs.Empty);
2853 owner.OnSelectedIndexChanged (EventArgs.Empty);
2859 internal override void OnPaintInternal (PaintEventArgs pevent)
2861 Draw (pevent.ClipRectangle,pevent.Graphics);
2864 public bool ShowWindow ()
2866 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2869 HighlightedIndex = owner.SelectedIndex;
2875 owner.OnDropDown (EventArgs.Empty);
2879 public void UpdateLastVisibleItem ()
2881 last_item = LastVisibleItem ();
2884 public void Scroll (int delta)
2886 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2889 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2891 int val = vscrollbar_ctrl.Value + delta;
2894 else if (val < vscrollbar_ctrl.Minimum)
2895 val = vscrollbar_ctrl.Minimum;
2896 vscrollbar_ctrl.Value = val;
2899 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2901 if (owner.Items.Count == 0)
2904 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2909 private void VerticalScrollEvent (object sender, EventArgs e)
2911 if (top_item == vscrollbar_ctrl.Value)
2914 top_item = vscrollbar_ctrl.Value;
2915 UpdateLastVisibleItem ();
2919 protected override void WndProc(ref Message m) {
2920 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2921 owner.Select (false, true);
2923 base.WndProc (ref m);
2926 #endregion Private Methods