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")]
42 [DefaultBindingProperty ("Text")]
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
45 public class ComboBox : ListControl
47 private DrawMode draw_mode = DrawMode.Normal;
48 private ComboBoxStyle dropdown_style;
49 private int dropdown_width = -1;
50 private int selected_index = -1;
51 private ObjectCollection items;
52 private bool suspend_ctrlupdate;
53 private int maxdrop_items = 8;
54 private bool integral_height = true;
56 private int max_length;
57 private ComboListBox listbox_ctrl;
58 private ComboTextBox textbox_ctrl;
59 private bool process_textchanged_event = true;
60 private bool process_texchanged_autoscroll = true;
61 private bool item_height_specified;
62 private int item_height;
63 private int requested_height = -1;
64 private Hashtable item_heights;
65 private bool show_dropdown_button;
66 private ButtonState button_state = ButtonState.Normal;
67 private bool dropped_down;
68 private Rectangle text_area;
69 private Rectangle button_area;
70 private Rectangle listbox_area;
71 private const int button_width = 16;
72 bool drop_down_button_entered;
73 private AutoCompleteStringCollection auto_complete_custom_source = null;
74 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
75 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
76 private FlatStyle flat_style;
77 private int drop_down_height;
78 const int default_drop_down_height = 106;
81 public class ChildAccessibleObject : AccessibleObject {
83 public ChildAccessibleObject (ComboBox owner, IntPtr handle)
88 public override string Name {
97 items = new ObjectCollection (this);
98 DropDownStyle = ComboBoxStyle.DropDown;
99 item_height = FontHeight + 2;
100 background_color = ThemeEngine.Current.ColorWindow;
101 border_style = BorderStyle.None;
103 drop_down_height = default_drop_down_height;
104 flat_style = FlatStyle.Standard;
107 MouseDown += new MouseEventHandler (OnMouseDownCB);
108 MouseUp += new MouseEventHandler (OnMouseUpCB);
109 MouseMove += new MouseEventHandler (OnMouseMoveCB);
110 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
111 MouseEnter += new EventHandler (OnMouseEnter);
112 MouseLeave += new EventHandler (OnMouseLeave);
113 KeyDown +=new KeyEventHandler(OnKeyDownCB);
119 [EditorBrowsable (EditorBrowsableState.Never)]
120 public new event EventHandler BackgroundImageChanged {
121 add { base.BackgroundImageChanged += value; }
122 remove { base.BackgroundImageChanged -= value; }
126 [EditorBrowsable (EditorBrowsableState.Never)]
127 public new event EventHandler BackgroundImageLayoutChanged
129 add { base.BackgroundImageLayoutChanged += value; }
130 remove { base.BackgroundImageLayoutChanged -= value; }
134 [EditorBrowsable (EditorBrowsableState.Never)]
135 public new event EventHandler DoubleClick
137 add { base.DoubleClick += value; }
138 remove { base.DoubleClick -= value; }
141 static object DrawItemEvent = new object ();
142 static object DropDownEvent = new object ();
143 static object DropDownStyleChangedEvent = new object ();
144 static object MeasureItemEvent = new object ();
145 static object SelectedIndexChangedEvent = new object ();
146 static object SelectionChangeCommittedEvent = new object ();
147 static object DropDownClosedEvent = new object ();
148 static object TextUpdateEvent = new object ();
150 public event DrawItemEventHandler DrawItem {
151 add { Events.AddHandler (DrawItemEvent, value); }
152 remove { Events.RemoveHandler (DrawItemEvent, value); }
155 public event EventHandler DropDown {
156 add { Events.AddHandler (DropDownEvent, value); }
157 remove { Events.RemoveHandler (DropDownEvent, value); }
159 public event EventHandler DropDownClosed
161 add { Events.AddHandler (DropDownClosedEvent, value); }
162 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
165 public event EventHandler DropDownStyleChanged {
166 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
167 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
170 public event MeasureItemEventHandler MeasureItem {
171 add { Events.AddHandler (MeasureItemEvent, value); }
172 remove { Events.RemoveHandler (MeasureItemEvent, value); }
175 [EditorBrowsable (EditorBrowsableState.Never)]
176 public new event EventHandler PaddingChanged
178 add { base.PaddingChanged += value; }
179 remove { base.PaddingChanged -= value; }
183 [EditorBrowsable (EditorBrowsableState.Never)]
184 public new event PaintEventHandler Paint {
185 add { base.Paint += value; }
186 remove { base.Paint -= value; }
189 public event EventHandler SelectedIndexChanged {
190 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
191 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
194 public event EventHandler SelectionChangeCommitted {
195 add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
196 remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
198 public event EventHandler TextUpdate
200 add { Events.AddHandler (TextUpdateEvent, value); }
201 remove { Events.RemoveHandler (TextUpdateEvent, value); }
206 #region Public Properties
207 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
208 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
210 [EditorBrowsable (EditorBrowsableState.Always)]
212 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
213 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
214 public AutoCompleteStringCollection AutoCompleteCustomSource {
216 if(auto_complete_custom_source == null) {
217 auto_complete_custom_source = new AutoCompleteStringCollection ();
218 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
220 return auto_complete_custom_source;
223 if(auto_complete_custom_source == value)
226 if(auto_complete_custom_source != null) //remove eventhandler from old collection
227 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
229 auto_complete_custom_source = value;
231 if(auto_complete_custom_source != null)
232 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
234 SetTextBoxAutoCompleteData ();
238 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
240 [EditorBrowsable (EditorBrowsableState.Always)]
241 [DefaultValue (AutoCompleteMode.None)]
242 public AutoCompleteMode AutoCompleteMode {
243 get { return auto_complete_mode; }
245 if(auto_complete_mode == value)
248 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
249 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
251 auto_complete_mode = value;
252 SetTextBoxAutoCompleteData ();
256 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
258 [EditorBrowsable (EditorBrowsableState.Always)]
259 [DefaultValue (AutoCompleteSource.None)]
260 public AutoCompleteSource AutoCompleteSource {
261 get { return auto_complete_source; }
263 if(auto_complete_source == value)
266 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
267 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
269 auto_complete_source = value;
270 SetTextBoxAutoCompleteData ();
274 void SetTextBoxAutoCompleteData ()
276 if (textbox_ctrl == null)
279 textbox_ctrl.AutoCompleteMode = auto_complete_mode;
281 if (auto_complete_source == AutoCompleteSource.ListItems) {
282 textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
283 textbox_ctrl.AutoCompleteCustomSource = null;
284 textbox_ctrl.AutoCompleteInternalSource = this;
286 textbox_ctrl.AutoCompleteSource = auto_complete_source;
287 textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
288 textbox_ctrl.AutoCompleteInternalSource = null;
291 public override Color BackColor {
292 get { return base.BackColor; }
294 if (base.BackColor == value)
296 base.BackColor = value;
302 [EditorBrowsable (EditorBrowsableState.Never)]
303 public override Image BackgroundImage {
304 get { return base.BackgroundImage; }
306 if (base.BackgroundImage == value)
308 base.BackgroundImage = value;
314 [EditorBrowsable (EditorBrowsableState.Never)]
315 public override ImageLayout BackgroundImageLayout {
316 get { return base.BackgroundImageLayout; }
317 set { base.BackgroundImageLayout = value; }
320 protected override CreateParams CreateParams {
321 get { return base.CreateParams;}
324 [DefaultValue ((string)null)]
325 [AttributeProvider (typeof (IListSource))]
326 [RefreshProperties (RefreshProperties.Repaint)]
327 [MWFCategory("Data")]
328 public new object DataSource {
329 get { return base.DataSource; }
330 set { base.DataSource = value; }
333 protected override Size DefaultSize {
334 get { return new Size (121, 21); }
337 [RefreshProperties(RefreshProperties.Repaint)]
338 [DefaultValue (DrawMode.Normal)]
339 [MWFCategory("Behavior")]
340 public DrawMode DrawMode {
341 get { return draw_mode; }
343 if (!Enum.IsDefined (typeof (DrawMode), value))
344 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
346 if (draw_mode == value)
349 if (draw_mode == DrawMode.OwnerDrawVariable)
352 if (draw_mode == DrawMode.OwnerDrawVariable)
353 item_heights = new Hashtable ();
360 [EditorBrowsable (EditorBrowsableState.Always)]
361 [MWFCategory("Behavior")]
362 public int DropDownHeight {
364 return drop_down_height;
368 throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
370 if (value == drop_down_height)
373 drop_down_height = value;
374 IntegralHeight = false;
378 [DefaultValue (ComboBoxStyle.DropDown)]
379 [RefreshProperties(RefreshProperties.Repaint)]
380 [MWFCategory("Appearance")]
381 public ComboBoxStyle DropDownStyle {
382 get { return dropdown_style; }
384 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
385 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
387 if (dropdown_style == value)
392 if (dropdown_style == ComboBoxStyle.Simple) {
393 if (listbox_ctrl != null) {
394 Controls.RemoveImplicit (listbox_ctrl);
395 listbox_ctrl.Dispose ();
400 dropdown_style = value;
402 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
403 Controls.RemoveImplicit (textbox_ctrl);
404 textbox_ctrl.Dispose ();
408 if (dropdown_style == ComboBoxStyle.Simple) {
409 show_dropdown_button = false;
411 CreateComboListBox ();
412 Controls.AddImplicit (listbox_ctrl);
413 listbox_ctrl.Visible = true;
415 // This should give us a 150 default height
416 // for Simple mode if size hasn't been set
417 // (DefaultSize doesn't work for us in this case)
418 if (requested_height == -1)
419 requested_height = 150;
421 show_dropdown_button = true;
422 button_state = ButtonState.Normal;
425 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
426 textbox_ctrl = new ComboTextBox (this);
427 object selected_item = SelectedItem;
428 if (selected_item != null)
429 textbox_ctrl.Text = GetItemText (selected_item);
430 textbox_ctrl.BorderStyle = BorderStyle.None;
431 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
432 textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
433 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
434 textbox_ctrl.ContextMenu = ContextMenu;
435 textbox_ctrl.TopMargin = 1; // since we don't have borders, adjust manually the top
437 if (IsHandleCreated == true)
438 Controls.AddImplicit (textbox_ctrl);
439 SetTextBoxAutoCompleteData ();
443 OnDropDownStyleChanged (EventArgs.Empty);
446 UpdateComboBoxBounds ();
451 [MWFCategory("Behavior")]
452 public int DropDownWidth {
454 if (dropdown_width == -1)
457 return dropdown_width;
460 if (dropdown_width == value)
464 throw new ArgumentOutOfRangeException ("DropDownWidth",
465 "The DropDownWidth value is less than one.");
467 dropdown_width = value;
472 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
473 public bool DroppedDown {
475 if (dropdown_style == ComboBoxStyle.Simple)
481 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
487 listbox_ctrl.HideWindow ();
491 [DefaultValue (FlatStyle.Standard)]
493 [MWFCategory("Appearance")]
494 public FlatStyle FlatStyle {
495 get { return flat_style; }
497 if (!Enum.IsDefined (typeof (FlatStyle), value))
498 throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
506 public override bool Focused {
507 get { return base.Focused; }
510 public override Color ForeColor {
511 get { return base.ForeColor; }
513 if (base.ForeColor == value)
515 base.ForeColor = value;
520 [DefaultValue (true)]
522 [MWFCategory("Behavior")]
523 public bool IntegralHeight {
524 get { return integral_height; }
526 if (integral_height == value)
528 integral_height = value;
529 UpdateComboBoxBounds ();
535 [MWFCategory("Behavior")]
536 public int ItemHeight {
538 if (item_height == -1) {
539 SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
540 item_height = (int) sz.Height;
546 throw new ArgumentOutOfRangeException ("ItemHeight",
547 "The item height value is less than one.");
549 item_height_specified = true;
552 UpdateComboBoxBounds ();
558 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
560 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
561 [MergableProperty (false)]
562 [MWFCategory("Data")]
563 public ComboBox.ObjectCollection Items {
564 get { return items; }
569 [MWFCategory("Behavior")]
570 public int MaxDropDownItems {
571 get { return maxdrop_items; }
573 if (maxdrop_items == value)
575 maxdrop_items = value;
579 public override Size MaximumSize {
580 get { return base.MaximumSize; }
582 base.MaximumSize = new Size (value.Width, 0);
588 [MWFCategory("Behavior")]
589 public int MaxLength {
590 get { return max_length; }
592 if (max_length == value)
597 if (dropdown_style != ComboBoxStyle.DropDownList) {
601 textbox_ctrl.MaxLength = value;
606 public override Size MinimumSize {
607 get { return base.MinimumSize; }
609 base.MinimumSize = new Size (value.Width, 0);
613 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
614 [EditorBrowsable (EditorBrowsableState.Never)]
616 public new Padding Padding {
617 get { return base.Padding; }
618 set { base.Padding = value; }
621 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
623 public int PreferredHeight {
624 get { return Font.Height + 8; }
628 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
629 public override int SelectedIndex {
630 get { return selected_index; }
632 SetSelectedIndex (value, false);
637 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
639 public object SelectedItem {
640 get { return selected_index == -1 ? null : Items [selected_index]; }
642 object item = selected_index == -1 ? null : Items [selected_index];
649 SelectedIndex = Items.IndexOf (value);
654 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
655 public string SelectedText {
657 if (dropdown_style == ComboBoxStyle.DropDownList)
660 string retval = textbox_ctrl.SelectedText;
665 if (dropdown_style == ComboBoxStyle.DropDownList)
667 textbox_ctrl.SelectedText = value;
672 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
673 public int SelectionLength {
675 if (dropdown_style == ComboBoxStyle.DropDownList)
678 int result = textbox_ctrl.SelectionLength;
679 return result == -1 ? 0 : result;
682 if (dropdown_style == ComboBoxStyle.DropDownList)
684 if (textbox_ctrl.SelectionLength == value)
686 textbox_ctrl.SelectionLength = value;
691 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
692 public int SelectionStart {
694 if (dropdown_style == ComboBoxStyle.DropDownList)
696 return textbox_ctrl.SelectionStart;
699 if (dropdown_style == ComboBoxStyle.DropDownList)
701 if (textbox_ctrl.SelectionStart == value)
703 textbox_ctrl.SelectionStart = value;
707 [DefaultValue (false)]
708 [MWFCategory("Behavior")]
710 get { return sorted; }
725 public override string Text {
727 if (dropdown_style != ComboBoxStyle.DropDownList) {
728 if (textbox_ctrl != null) {
729 return textbox_ctrl.Text;
733 if (SelectedItem != null)
734 return GetItemText (SelectedItem);
740 if (SelectedIndex == -1) {
741 if (dropdown_style != ComboBoxStyle.DropDownList)
742 SetControlText (string.Empty, false);
749 // do nothing if value exactly matches text of selected item
750 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
753 // find exact match using case-sensitive comparison, and if does
754 // not result in any match then use case-insensitive comparison
755 int index = FindStringExact (value, -1, false);
757 index = FindStringExact (value, -1, true);
760 SelectedIndex = index;
764 // set directly the passed value, since we already know it's not matching any item
765 if (dropdown_style != ComboBoxStyle.DropDownList)
766 textbox_ctrl.Text = value;
770 #endregion Public Properties
772 #region Internal Properties
773 internal Rectangle ButtonArea {
774 get { return button_area; }
777 internal Rectangle TextArea {
778 get { return text_area; }
782 #region UIA Framework Properties
784 internal TextBox UIATextBox {
785 get { return textbox_ctrl; }
788 internal ComboListBox UIAComboListBox {
789 get { return listbox_ctrl; }
792 #endregion UIA Framework Properties
794 #region Public Methods
795 [Obsolete ("This method has been deprecated")]
796 protected virtual void AddItemsCore (object[] value)
801 public void BeginUpdate ()
803 suspend_ctrlupdate = true;
806 protected override AccessibleObject CreateAccessibilityInstance ()
808 return base.CreateAccessibilityInstance ();
811 protected override void CreateHandle ()
813 base.CreateHandle ();
816 protected override void Dispose (bool disposing)
819 if (listbox_ctrl != null) {
820 listbox_ctrl.Dispose ();
821 Controls.RemoveImplicit (listbox_ctrl);
825 if (textbox_ctrl != null) {
826 Controls.RemoveImplicit (textbox_ctrl);
827 textbox_ctrl.Dispose ();
832 base.Dispose (disposing);
835 public void EndUpdate ()
837 suspend_ctrlupdate = false;
842 public int FindString (string s)
844 return FindString (s, -1);
847 public int FindString (string s, int startIndex)
849 if (s == null || Items.Count == 0)
852 if (startIndex < -1 || startIndex >= Items.Count)
853 throw new ArgumentOutOfRangeException ("startIndex");
856 if (i == (Items.Count - 1))
860 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
862 if (i == (Items.Count - 1))
864 } while (i != startIndex);
869 public int FindStringExact (string s)
871 return FindStringExact (s, -1);
874 public int FindStringExact (string s, int startIndex)
876 return FindStringExact (s, startIndex, true);
879 private int FindStringExact (string s, int startIndex, bool ignoreCase)
881 if (s == null || Items.Count == 0)
884 if (startIndex < -1 || startIndex >= Items.Count)
885 throw new ArgumentOutOfRangeException ("startIndex");
888 if (i == (Items.Count - 1))
892 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
894 if (i == (Items.Count - 1))
896 } while (i != startIndex);
901 public int GetItemHeight (int index)
903 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
905 if (index < 0 || index >= Items.Count )
906 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
908 object item = Items [index];
909 if (item_heights.Contains (item))
910 return (int) item_heights [item];
912 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
913 OnMeasureItem (args);
914 item_heights [item] = args.ItemHeight;
915 return args.ItemHeight;
921 protected override bool IsInputKey (Keys keyData)
923 switch (keyData & ~Keys.Modifiers) {
939 protected override void OnBackColorChanged (EventArgs e)
941 base.OnBackColorChanged (e);
943 if (textbox_ctrl != null)
944 textbox_ctrl.BackColor = BackColor;
947 protected override void OnDataSourceChanged (EventArgs e)
949 base.OnDataSourceChanged (e);
952 if (DataSource == null || DataManager == null) {
956 SelectedIndex = DataManager.Position;
960 protected override void OnDisplayMemberChanged (EventArgs e)
962 base.OnDisplayMemberChanged (e);
964 if (DataManager == null)
967 SelectedIndex = DataManager.Position;
969 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
970 SetControlText (GetItemText (Items [selected_index]), true);
972 if (!IsHandleCreated)
978 protected virtual void OnDrawItem (DrawItemEventArgs e)
980 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
985 internal void HandleDrawItem (DrawItemEventArgs e)
987 // Only raise OnDrawItem if we are in an OwnerDraw mode
989 case DrawMode.OwnerDrawFixed:
990 case DrawMode.OwnerDrawVariable:
994 ThemeEngine.Current.DrawComboBoxItem (this, e);
999 protected virtual void OnDropDown (EventArgs e)
1001 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1006 protected virtual void OnDropDownClosed (EventArgs e)
1008 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1013 protected virtual void OnDropDownStyleChanged (EventArgs e)
1015 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1020 protected override void OnFontChanged (EventArgs e)
1022 base.OnFontChanged (e);
1024 if (textbox_ctrl != null)
1025 textbox_ctrl.Font = Font;
1027 if (!item_height_specified)
1028 item_height = Font.Height + 2;
1031 UpdateComboBoxBounds ();
1036 protected override void OnForeColorChanged (EventArgs e)
1038 base.OnForeColorChanged (e);
1039 if (textbox_ctrl != null)
1040 textbox_ctrl.ForeColor = ForeColor;
1043 [EditorBrowsable(EditorBrowsableState.Advanced)]
1044 protected override void OnGotFocus (EventArgs e)
1046 if (dropdown_style == ComboBoxStyle.DropDownList) {
1047 // We draw DDL styles manually, so they require a
1048 // refresh to have their selection drawn
1052 if (textbox_ctrl != null) {
1053 textbox_ctrl.SetSelectable (false);
1054 textbox_ctrl.ShowSelection = Enabled;
1055 textbox_ctrl.ActivateCaret (true);
1056 textbox_ctrl.SelectAll ();
1059 base.OnGotFocus (e);
1062 [EditorBrowsable(EditorBrowsableState.Advanced)]
1063 protected override void OnLostFocus (EventArgs e)
1065 if (dropdown_style == ComboBoxStyle.DropDownList) {
1066 // We draw DDL styles manually, so they require a
1067 // refresh to have their selection drawn
1071 if (listbox_ctrl != null && dropped_down) {
1072 listbox_ctrl.HideWindow ();
1075 if (textbox_ctrl != null) {
1076 textbox_ctrl.SetSelectable (true);
1077 textbox_ctrl.ActivateCaret (false);
1078 textbox_ctrl.ShowSelection = false;
1079 textbox_ctrl.SelectionLength = 0;
1080 textbox_ctrl.HideAutoCompleteList ();
1083 base.OnLostFocus (e);
1086 protected override void OnHandleCreated (EventArgs e)
1088 base.OnHandleCreated (e);
1090 SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1092 if (textbox_ctrl != null)
1093 Controls.AddImplicit (textbox_ctrl);
1096 UpdateComboBoxBounds ();
1099 protected override void OnHandleDestroyed (EventArgs e)
1101 base.OnHandleDestroyed (e);
1104 protected override void OnKeyPress (KeyPressEventArgs e)
1106 if (dropdown_style == ComboBoxStyle.DropDownList) {
1107 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1109 SelectedIndex = index;
1110 if (DroppedDown) { //Scroll into view
1111 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1112 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1113 // Or, selecting an item earlier in the list.
1114 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1115 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1120 base.OnKeyPress (e);
1123 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1125 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1130 protected override void OnParentBackColorChanged (EventArgs e)
1132 base.OnParentBackColorChanged (e);
1135 protected override void OnResize (EventArgs e)
1138 if (listbox_ctrl != null)
1139 listbox_ctrl.CalcListBoxArea ();
1142 protected override void OnSelectedIndexChanged (EventArgs e)
1144 base.OnSelectedIndexChanged (e);
1146 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1151 protected virtual void OnSelectedItemChanged (EventArgs e)
1155 protected override void OnSelectedValueChanged (EventArgs e)
1157 base.OnSelectedValueChanged (e);
1160 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1162 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1167 protected override void RefreshItem (int index)
1169 if (index < 0 || index >= Items.Count)
1170 throw new ArgumentOutOfRangeException ("index");
1172 if (draw_mode == DrawMode.OwnerDrawVariable)
1173 item_heights.Remove (Items [index]);
1176 protected override void RefreshItems ()
1178 for (int i = 0; i < Items.Count; i++) {
1185 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1186 SetControlText (GetItemText (Items [selected_index]), false);
1189 public override void ResetText ()
1191 Text = String.Empty;
1194 protected override bool ProcessKeyEventArgs (ref Message m)
1196 return base.ProcessKeyEventArgs (ref m);
1199 [EditorBrowsable (EditorBrowsableState.Advanced)]
1200 protected override void OnKeyDown (KeyEventArgs e)
1205 [EditorBrowsable (EditorBrowsableState.Advanced)]
1206 protected override void OnValidating (CancelEventArgs e)
1208 base.OnValidating (e);
1211 [EditorBrowsable (EditorBrowsableState.Advanced)]
1212 protected override void OnTextChanged (EventArgs e)
1214 base.OnTextChanged (e);
1217 protected virtual void OnTextUpdate (EventArgs e)
1219 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1223 protected override void OnMouseLeave (EventArgs e)
1225 if (flat_style == FlatStyle.Popup)
1227 base.OnMouseLeave (e);
1230 protected override void OnMouseEnter (EventArgs e)
1232 if (flat_style == FlatStyle.Popup)
1234 base.OnMouseEnter (e);
1237 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1239 base.ScaleControl (factor, specified);
1242 public void Select (int start, int length)
1245 throw new ArgumentException ("Start cannot be less than zero");
1248 throw new ArgumentException ("length cannot be less than zero");
1250 if (dropdown_style == ComboBoxStyle.DropDownList)
1253 textbox_ctrl.Select (start, length);
1256 public void SelectAll ()
1258 if (dropdown_style == ComboBoxStyle.DropDownList)
1261 if (textbox_ctrl != null) {
1262 textbox_ctrl.ShowSelection = true;
1263 textbox_ctrl.SelectAll ();
1267 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1269 bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1270 bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1272 if ((specified & BoundsSpecified.Height) != 0 ||
1273 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1275 requested_height = height;
1276 height = SnapHeight (height);
1279 base.SetBoundsCore (x, y, width, height, specified);
1282 protected override void SetItemCore (int index, object value)
1284 if (index < 0 || index >= Items.Count)
1287 Items[index] = value;
1290 protected override void SetItemsCore (IList value)
1295 Items.AddRange (value);
1301 public override string ToString ()
1303 return base.ToString () + ", Items.Count:" + Items.Count;
1306 protected override void WndProc (ref Message m)
1308 switch ((Msg) m.Msg) {
1310 case Msg.WM_KEYDOWN:
1311 Keys keys = (Keys) m.WParam.ToInt32 ();
1312 // Don't pass the message to base if auto complete is being used and available.
1313 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1314 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1317 if (keys == Keys.Up || keys == Keys.Down)
1319 goto case Msg.WM_CHAR;
1321 // Call our own handler first and send the message to the TextBox if still needed
1322 if (!ProcessKeyMessage (ref m) && textbox_ctrl != null)
1323 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1325 case Msg.WM_MOUSELEAVE:
1326 Point location = PointToClient (Control.MousePosition);
1327 if (ClientRectangle.Contains (location))
1333 base.WndProc (ref m);
1336 #endregion Public Methods
1338 #region Private Methods
1339 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1340 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1341 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1345 internal override bool InternalCapture {
1346 get { return Capture; }
1350 void LayoutComboBox ()
1352 int border = ThemeEngine.Current.Border3DSize.Width;
1354 text_area = ClientRectangle;
1355 text_area.Height = PreferredHeight;
1357 listbox_area = ClientRectangle;
1358 listbox_area.Y = text_area.Bottom + 3;
1359 listbox_area.Height -= (text_area.Height + 2);
1361 Rectangle prev_button_area = button_area;
1363 if (DropDownStyle == ComboBoxStyle.Simple)
1364 button_area = Rectangle.Empty;
1366 button_area = text_area;
1367 button_area.X = text_area.Right - button_width - border;
1368 button_area.Y = text_area.Y + border;
1369 button_area.Width = button_width;
1370 button_area.Height = text_area.Height - 2 * border;
1371 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1372 button_area.Inflate (1, 1);
1374 button_area.Width -= 2;
1378 if (button_area != prev_button_area) {
1379 prev_button_area.Y -= border;
1380 prev_button_area.Width += border;
1381 prev_button_area.Height += 2 * border;
1382 Invalidate (prev_button_area);
1383 Invalidate (button_area);
1386 if (textbox_ctrl != null) {
1387 int text_border = border + 1;
1388 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1389 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1390 textbox_ctrl.Height = text_area.Height - text_border * 2;
1393 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1394 listbox_ctrl.Location = listbox_area.Location;
1395 listbox_ctrl.CalcListBoxArea ();
1399 private void CreateComboListBox ()
1401 listbox_ctrl = new ComboListBox (this);
1402 listbox_ctrl.HighlightedIndex = SelectedIndex;
1405 internal void Draw (Rectangle clip, Graphics dc)
1407 Theme theme = ThemeEngine.Current;
1408 FlatStyle style = FlatStyle.Standard;
1409 bool is_flat = false;
1411 style = this.FlatStyle;
1412 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1414 theme.ComboBoxDrawBackground (this, dc, clip, style);
1416 int border = theme.Border3DSize.Width;
1418 // No edit control, we paint the edit ourselves
1419 if (dropdown_style == ComboBoxStyle.DropDownList) {
1420 DrawItemState state = DrawItemState.None;
1421 Color back_color = BackColor;
1422 Color fore_color = ForeColor;
1423 Rectangle item_rect = text_area;
1424 item_rect.X += border;
1425 item_rect.Y += border;
1426 item_rect.Width -= (button_area.Width + 2 * border);
1427 item_rect.Height -= 2 * border;
1430 state = DrawItemState.Selected;
1431 state |= DrawItemState.Focus;
1432 back_color = SystemColors.Highlight;
1433 fore_color = SystemColors.HighlightText;
1436 state |= DrawItemState.ComboBoxEdit;
1437 HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, fore_color, back_color));
1440 if (show_dropdown_button) {
1441 ButtonState current_state;
1443 current_state = button_state;
1445 current_state = ButtonState.Inactive;
1447 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1448 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1451 theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1453 theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
1458 internal bool DropDownButtonEntered {
1459 get { return drop_down_button_entered; }
1461 if (drop_down_button_entered == value)
1463 drop_down_button_entered = value;
1464 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1465 Invalidate (button_area);
1469 internal void DropDownListBox ()
1471 DropDownButtonEntered = false;
1473 if (DropDownStyle == ComboBoxStyle.Simple)
1476 if (listbox_ctrl == null)
1477 CreateComboListBox ();
1479 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1481 FindMatchOrSetIndex(SelectedIndex);
1483 if (textbox_ctrl != null)
1484 textbox_ctrl.HideAutoCompleteList ();
1486 if (listbox_ctrl.ShowWindow ())
1487 dropped_down = true;
1489 button_state = ButtonState.Pushed;
1490 if (dropdown_style == ComboBoxStyle.DropDownList)
1491 Invalidate (text_area);
1494 internal void DropDownListBoxFinished ()
1496 if (DropDownStyle == ComboBoxStyle.Simple)
1499 FindMatchOrSetIndex (SelectedIndex);
1500 button_state = ButtonState.Normal;
1501 Invalidate (button_area);
1502 dropped_down = false;
1503 OnDropDownClosed (EventArgs.Empty);
1505 * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1506 * this causes the popup to appear under the main form. This is horrible but necessary
1509 // If the user opens a new form in an event, it will close our dropdown,
1510 // so we need a null check here
1511 if (listbox_ctrl != null) {
1512 listbox_ctrl.Dispose ();
1513 listbox_ctrl = null;
1515 // The auto complete list could have been shown after the listbox,
1516 // so make sure it's hidden.
1517 if (textbox_ctrl != null)
1518 textbox_ctrl.HideAutoCompleteList ();
1521 private int FindStringCaseInsensitive (string search)
1523 if (search.Length == 0) {
1527 for (int i = 0; i < Items.Count; i++)
1529 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1536 // Search in the list for the substring, starting the search at the list
1537 // position specified, the search wraps thus covering all the list.
1538 internal int FindStringCaseInsensitive (string search, int start_index)
1540 if (search.Length == 0) {
1543 // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1544 if (start_index < 0 || start_index > Items.Count)
1545 throw new ArgumentOutOfRangeException("start_index");
1547 for (int i = 0; i < Items.Count; i++) {
1548 int index = (i + start_index) % Items.Count;
1549 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1556 internal override bool IsInputCharInternal (char charCode)
1561 internal override ContextMenu ContextMenuInternal {
1563 return base.ContextMenuInternal;
1566 base.ContextMenuInternal = value;
1567 if (textbox_ctrl != null) {
1568 textbox_ctrl.ContextMenu = value;
1573 internal void RestoreContextMenu ()
1575 textbox_ctrl.RestoreContextMenu ();
1578 private void OnKeyDownCB(object sender, KeyEventArgs e)
1580 if (Items.Count == 0)
1583 // for keyboard navigation, we have to do our own scroll, since
1584 // the default behaviour for the SelectedIndex property is a little different,
1585 // setting the selected index in the top always
1591 FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1594 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1595 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1599 if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1602 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1605 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1606 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1610 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1614 SetSelectedIndex (Math.Max (SelectedIndex - offset, 0), true);
1617 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1618 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1622 if (SelectedIndex == -1) {
1624 if (dropdown_style != ComboBoxStyle.Simple)
1628 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1632 SetSelectedIndex (Math.Min (SelectedIndex + offset, Items.Count - 1), true);
1635 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1636 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1641 if (listbox_ctrl != null && listbox_ctrl.Visible)
1642 DropDownListBoxFinished ();
1646 if (dropdown_style == ComboBoxStyle.DropDownList) {
1650 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1651 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1656 if (dropdown_style == ComboBoxStyle.DropDownList) {
1657 SetSelectedIndex (Items.Count - 1, true);
1660 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1661 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1670 void SetSelectedIndex (int value, bool supressAutoScroll)
1672 if (selected_index == value)
1675 if (value <= -2 || value >= Items.Count)
1676 throw new ArgumentOutOfRangeException ("SelectedIndex");
1678 selected_index = value;
1680 if (dropdown_style != ComboBoxStyle.DropDownList) {
1682 SetControlText (string.Empty, false, supressAutoScroll);
1684 SetControlText (GetItemText (Items [value]), false, supressAutoScroll);
1687 if (DropDownStyle == ComboBoxStyle.DropDownList)
1690 if (listbox_ctrl != null)
1691 listbox_ctrl.HighlightedIndex = value;
1693 OnSelectedValueChanged (EventArgs.Empty);
1694 OnSelectedIndexChanged (EventArgs.Empty);
1695 OnSelectedItemChanged (EventArgs.Empty);
1698 // If no item is currently selected, and an item is found matching the text
1699 // in the textbox, then selected that item. Otherwise the item at the given
1700 // index is selected.
1701 private void FindMatchOrSetIndex(int index)
1704 if (SelectedIndex == -1 && Text.Length != 0)
1705 match = FindStringCaseInsensitive(Text);
1707 SetSelectedIndex (match, true);
1709 SetSelectedIndex (index, true);
1712 void OnMouseDownCB (object sender, MouseEventArgs e)
1715 if (DropDownStyle == ComboBoxStyle.DropDownList)
1716 area = ClientRectangle;
1720 if (area.Contains (e.X, e.Y)) {
1721 if (Items.Count > 0)
1724 button_state = ButtonState.Pushed;
1725 OnDropDown (EventArgs.Empty);
1728 Invalidate (button_area);
1734 void OnMouseEnter (object sender, EventArgs e)
1736 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1740 void OnMouseLeave (object sender, EventArgs e)
1742 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1743 drop_down_button_entered = false;
1746 if (show_dropdown_button)
1747 DropDownButtonEntered = false;
1751 void OnMouseMoveCB (object sender, MouseEventArgs e)
1753 if (show_dropdown_button && !dropped_down)
1754 DropDownButtonEntered = button_area.Contains (e.Location);
1756 if (DropDownStyle == ComboBoxStyle.Simple)
1759 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1760 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1761 if (listbox_ctrl.ClientRectangle.Contains (location))
1762 listbox_ctrl.Capture = true;
1766 void OnMouseUpCB (object sender, MouseEventArgs e)
1770 button_state = ButtonState.Normal;
1771 Invalidate (button_area);
1773 OnClick (EventArgs.Empty);
1776 listbox_ctrl.Capture = true;
1779 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1781 if (Items.Count == 0)
1784 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1785 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1786 listbox_ctrl.Scroll (-lines);
1788 int lines = me.Delta / 120;
1789 int index = SelectedIndex - lines;
1792 else if (index >= Items.Count)
1793 index = Items.Count - 1;
1794 SelectedIndex = index;
1798 MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
1800 Point loc = PointToClient (Control.MousePosition);
1801 return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
1804 internal override void OnPaintInternal (PaintEventArgs pevent)
1806 if (suspend_ctrlupdate)
1809 Draw (ClientRectangle, pevent.Graphics);
1812 private void OnTextBoxClick (object sender, EventArgs e)
1817 private void OnTextChangedEdit (object sender, EventArgs e)
1819 if (process_textchanged_event == false)
1822 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1825 // Setting base.Text below will raise this event
1826 // if we found something
1827 OnTextChanged (EventArgs.Empty);
1831 if (listbox_ctrl != null) {
1833 if (process_texchanged_autoscroll)
1834 listbox_ctrl.EnsureTop (item);
1837 base.Text = textbox_ctrl.Text;
1840 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1842 selected_index = -1;
1843 if (listbox_ctrl != null)
1844 listbox_ctrl.HighlightedIndex = -1;
1847 internal void SetControlText (string s, bool suppressTextChanged)
1849 SetControlText (s, suppressTextChanged, false);
1852 internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
1854 if (suppressTextChanged)
1855 process_textchanged_event = false;
1856 if (supressAutoScroll)
1857 process_texchanged_autoscroll = false;
1859 textbox_ctrl.Text = s;
1860 textbox_ctrl.SelectAll ();
1861 process_textchanged_event = true;
1862 process_texchanged_autoscroll = true;
1865 void UpdateComboBoxBounds ()
1867 if (requested_height == -1)
1870 // Save the requested height since set bounds can destroy it
1871 int save_height = requested_height;
1872 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1873 BoundsSpecified.Height);
1874 requested_height = save_height;
1877 int SnapHeight (int height)
1879 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1880 if (IntegralHeight) {
1881 int border = ThemeEngine.Current.Border3DSize.Height;
1882 int lb_height = (height - PreferredHeight - 2) - border * 2;
1883 if (lb_height > ItemHeight) {
1884 int partial = (lb_height) % ItemHeight;
1886 } else if (lb_height < ItemHeight)
1887 height = PreferredHeight;
1890 height = PreferredHeight;
1895 private void UpdatedItems ()
1897 if (listbox_ctrl != null) {
1898 listbox_ctrl.UpdateLastVisibleItem ();
1899 listbox_ctrl.CalcListBoxArea ();
1900 listbox_ctrl.Refresh ();
1904 #endregion Private Methods
1906 [ListBindableAttribute (false)]
1907 public class ObjectCollection : IList, ICollection, IEnumerable
1910 private ComboBox owner;
1911 internal ArrayList object_items = new ArrayList ();
1913 #region UIA Framework Events
1916 // We are using Reflection to add/remove internal events.
1917 // Class ListProvider uses the events.
1919 //Event used to generate UIA StructureChangedEvent
1920 static object UIACollectionChangedEvent = new object ();
1922 internal event CollectionChangeEventHandler UIACollectionChanged {
1923 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1924 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1927 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1929 CollectionChangeEventHandler eh
1930 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1935 #endregion UIA Framework Events
1937 public ObjectCollection (ComboBox owner)
1942 #region Public Properties
1944 get { return object_items.Count; }
1947 public bool IsReadOnly {
1948 get { return false; }
1952 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1953 public virtual object this [int index] {
1955 if (index < 0 || index >= Count)
1956 throw new ArgumentOutOfRangeException ("index");
1958 return object_items[index];
1961 if (index < 0 || index >= Count)
1962 throw new ArgumentOutOfRangeException ("index");
1964 throw new ArgumentNullException ("value");
1966 //UIA Framework event: Item Removed
1967 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
1969 object_items[index] = value;
1971 //UIA Framework event: Item Added
1972 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
1974 if (owner.listbox_ctrl != null)
1975 owner.listbox_ctrl.InvalidateItem (index);
1976 if (index == owner.SelectedIndex) {
1977 if (owner.textbox_ctrl == null)
1980 owner.textbox_ctrl.SelectedText = value.ToString ();
1985 bool ICollection.IsSynchronized {
1986 get { return false; }
1989 object ICollection.SyncRoot {
1990 get { return this; }
1993 bool IList.IsFixedSize {
1994 get { return false; }
1997 #endregion Public Properties
1999 #region Public Methods
2000 public int Add (object item)
2004 idx = AddItem (item, false);
2005 owner.UpdatedItems ();
2009 public void AddRange (object[] items)
2012 throw new ArgumentNullException ("items");
2014 foreach (object mi in items)
2020 owner.UpdatedItems ();
2023 public void Clear ()
2025 owner.selected_index = -1;
2026 object_items.Clear ();
2027 owner.UpdatedItems ();
2030 //UIA Framework event: Items list cleared
2031 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2034 public bool Contains (object value)
2037 throw new ArgumentNullException ("value");
2039 return object_items.Contains (value);
2042 public void CopyTo (object [] destination, int arrayIndex)
2044 object_items.CopyTo (destination, arrayIndex);
2047 void ICollection.CopyTo (Array destination, int index)
2049 object_items.CopyTo (destination, index);
2052 public IEnumerator GetEnumerator ()
2054 return object_items.GetEnumerator ();
2057 int IList.Add (object item)
2062 public int IndexOf (object value)
2065 throw new ArgumentNullException ("value");
2067 return object_items.IndexOf (value);
2070 public void Insert (int index, object item)
2072 if (index < 0 || index > Count)
2073 throw new ArgumentOutOfRangeException ("index");
2075 throw new ArgumentNullException ("item");
2077 owner.BeginUpdate ();
2080 AddItem (item, false);
2082 object_items.Insert (index, item);
2083 //UIA Framework event: Item added
2084 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2087 owner.EndUpdate (); // Calls UpdatedItems
2090 public void Remove (object value)
2095 if (IndexOf (value) == owner.SelectedIndex)
2096 owner.SelectedIndex = -1;
2098 RemoveAt (IndexOf (value));
2101 public void RemoveAt (int index)
2103 if (index < 0 || index >= Count)
2104 throw new ArgumentOutOfRangeException ("index");
2106 if (index == owner.SelectedIndex)
2107 owner.SelectedIndex = -1;
2109 object removed = object_items [index];
2111 object_items.RemoveAt (index);
2112 owner.UpdatedItems ();
2114 //UIA Framework event: Item removed
2115 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2117 #endregion Public Methods
2119 #region Private Methods
2120 private int AddItem (object item, bool suspend)
2122 // suspend means do not sort as we put new items in, we will do a
2123 // big sort at the end
2125 throw new ArgumentNullException ("item");
2127 if (owner.Sorted && !suspend) {
2129 foreach (object o in object_items) {
2130 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2131 object_items.Insert (index, item);
2133 // If we added the new item before the selectedindex
2134 // bump the selectedindex by one, behavior differs if
2135 // Handle has not been created.
2136 if (index <= owner.selected_index && owner.IsHandleCreated)
2137 owner.selected_index++;
2139 //UIA Framework event: Item added
2140 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2147 object_items.Add (item);
2149 //UIA Framework event: Item added
2150 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2152 return object_items.Count - 1;
2155 internal void AddRange (IList items)
2157 foreach (object mi in items)
2158 AddItem (mi, false);
2163 owner.UpdatedItems ();
2166 internal void Sort ()
2168 // If the objects the user put here don't have their own comparer,
2169 // use one that compares based on the object's ToString
2170 if (object_items.Count > 0 && object_items[0] is IComparer)
2171 object_items.Sort ();
2173 object_items.Sort (new ObjectComparer (owner));
2176 private class ObjectComparer : IComparer
2178 private ListControl owner;
2180 public ObjectComparer (ListControl owner)
2185 #region IComparer Members
2186 public int Compare (object x, object y)
2188 return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2192 #endregion Private Methods
2195 internal class ComboTextBox : TextBox {
2197 private ComboBox owner;
2199 public ComboTextBox (ComboBox owner)
2202 ShowSelection = false;
2203 owner.EnabledChanged += OwnerEnabledChangedHandler;
2204 owner.LostFocus += OwnerLostFocusHandler;
2207 void OwnerEnabledChangedHandler (object o, EventArgs args)
2209 ShowSelection = owner.Focused && owner.Enabled;
2212 void OwnerLostFocusHandler (object o, EventArgs args)
2214 if (IsAutoCompleteAvailable)
2218 protected override void OnKeyDown (KeyEventArgs args)
2220 if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
2223 base.OnKeyDown (args);
2226 internal override void OnAutoCompleteValueSelected (EventArgs args)
2228 base.OnAutoCompleteValueSelected (args);
2232 internal void SetSelectable (bool selectable)
2234 SetStyle (ControlStyles.Selectable, selectable);
2237 internal void ActivateCaret (bool active)
2240 document.CaretHasFocus ();
2242 document.CaretLostFocus ();
2245 internal override void OnTextUpdate ()
2247 base.OnTextUpdate ();
2248 owner.OnTextUpdate (EventArgs.Empty);
2251 protected override void OnGotFocus (EventArgs e)
2253 owner.Select (false, true);
2256 protected override void OnLostFocus (EventArgs e)
2258 owner.Select (false, true);
2261 // We have to pass these events to our owner - MouseMove is not, however.
2263 protected override void OnMouseDown (MouseEventArgs e)
2265 base.OnMouseDown (e);
2266 owner.OnMouseDown (owner.TranslateMouseEventArgs (e));
2269 protected override void OnMouseUp (MouseEventArgs e)
2272 owner.OnMouseUp (owner.TranslateMouseEventArgs (e));
2275 protected override void OnMouseClick (MouseEventArgs e)
2277 base.OnMouseClick (e);
2278 owner.OnMouseClick (owner.TranslateMouseEventArgs (e));
2281 protected override void OnMouseDoubleClick (MouseEventArgs e)
2283 base.OnMouseDoubleClick (e);
2284 owner.OnMouseDoubleClick (owner.TranslateMouseEventArgs (e));
2287 public override bool Focused {
2289 return owner.Focused;
2293 internal override bool ActivateOnShow { get { return false; } }
2296 internal class ComboListBox : Control
2298 private ComboBox owner;
2299 private VScrollBarLB vscrollbar_ctrl;
2300 private int top_item; /* First item that we show the in the current page */
2301 private int last_item; /* Last visible item */
2302 internal int page_size; /* Number of listbox items per page */
2303 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2305 internal enum ItemNavigation
2315 #region UIA Framework: Properties
2317 internal int UIATopItem {
2318 get { return top_item; }
2321 internal int UIALastItem {
2322 get { return last_item; }
2325 internal ScrollBar UIAVScrollBar {
2326 get { return vscrollbar_ctrl; }
2331 class VScrollBarLB : VScrollBar
2333 public VScrollBarLB ()
2337 internal override bool InternalCapture {
2338 get { return Capture; }
2342 public void FireMouseDown (MouseEventArgs e)
2347 e = TranslateEvent (e);
2351 public void FireMouseUp (MouseEventArgs e)
2356 e = TranslateEvent (e);
2360 public void FireMouseMove (MouseEventArgs e)
2365 e = TranslateEvent (e);
2369 MouseEventArgs TranslateEvent (MouseEventArgs e)
2371 Point loc = PointToClient (Control.MousePosition);
2372 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2376 public ComboListBox (ComboBox owner)
2383 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2385 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2386 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2388 this.is_visible = false;
2390 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2391 InternalBorderStyle = BorderStyle.Fixed3D;
2393 InternalBorderStyle = BorderStyle.FixedSingle;
2396 protected override CreateParams CreateParams
2399 CreateParams cp = base.CreateParams;
2400 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2403 cp.Style ^= (int)WindowStyles.WS_CHILD;
2404 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2405 cp.Style |= (int)WindowStyles.WS_POPUP;
2406 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2411 internal override bool InternalCapture {
2420 internal override bool ActivateOnShow { get { return false; } }
2421 #region Private Methods
2423 // Calcs the listbox area
2424 internal void CalcListBoxArea ()
2427 bool show_scrollbar;
2429 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2430 Rectangle area = owner.listbox_area;
2432 height = area.Height;
2433 show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
2435 // No calculation needed
2436 if (height <= 0 || width <= 0)
2440 else { // DropDown or DropDownList
2442 width = owner.DropDownWidth;
2443 int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2445 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2447 for (int i = 0; i < visible_items_count; i++) {
2448 height += owner.GetItemHeight (i);
2451 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2454 if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
2455 height = owner.ItemHeight * visible_items_count;
2456 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2458 // ignore visible items count, and use manual height instead
2459 height = owner.DropDownHeight;
2460 show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
2465 page_size = Math.Max (height / owner.ItemHeight, 1);
2467 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2468 if (!show_scrollbar) {
2470 if (vscrollbar_ctrl != null)
2471 vscrollbar_ctrl.Visible = false;
2472 if (dropdown_style != ComboBoxStyle.Simple)
2473 height = owner.ItemHeight * owner.items.Count;
2475 /* Need vertical scrollbar */
2476 if (vscrollbar_ctrl == null) {
2477 vscrollbar_ctrl = new VScrollBarLB ();
2478 vscrollbar_ctrl.Minimum = 0;
2479 vscrollbar_ctrl.SmallChange = 1;
2480 vscrollbar_ctrl.LargeChange = 1;
2481 vscrollbar_ctrl.Maximum = 0;
2482 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2483 Controls.AddImplicit (vscrollbar_ctrl);
2486 vscrollbar_ctrl.Dock = DockStyle.Right;
2488 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2489 int large = page_size;
2492 vscrollbar_ctrl.LargeChange = large;
2493 vscrollbar_ctrl.Visible = true;
2495 int hli = HighlightedIndex;
2497 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2498 vscrollbar_ctrl.Value = hli;
2502 Size = new Size (width, height);
2503 textarea_drawable = ClientRectangle;
2504 textarea_drawable.Width = width;
2505 textarea_drawable.Height = height;
2507 if (vscrollbar_ctrl != null && show_scrollbar)
2508 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2510 last_item = LastVisibleItem ();
2513 private void Draw (Rectangle clip, Graphics dc)
2515 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2517 if (owner.Items.Count > 0) {
2519 for (int i = top_item; i <= last_item; i++) {
2520 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2522 if (!clip.IntersectsWith (item_rect))
2525 DrawItemState state = DrawItemState.None;
2526 Color back_color = owner.BackColor;
2527 Color fore_color = owner.ForeColor;
2529 if (i == HighlightedIndex) {
2530 state |= DrawItemState.Selected;
2531 back_color = SystemColors.Highlight;
2532 fore_color = SystemColors.HighlightText;
2534 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2535 state |= DrawItemState.Focus;
2539 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2540 i, state, fore_color, back_color));
2545 int highlighted_index = -1;
2547 public int HighlightedIndex {
2548 get { return highlighted_index; }
2550 if (highlighted_index == value)
2553 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2554 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2555 highlighted_index = value;
2556 if (highlighted_index != -1)
2557 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2561 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2563 if (index < 0 || index >= owner.Items.Count)
2564 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2566 Rectangle item_rect = new Rectangle ();
2567 int height = owner.GetItemHeight (index);
2570 item_rect.Width = textarea_drawable.Width;
2571 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2573 for (int i = top_index; i < index; i++)
2574 item_rect.Y += owner.GetItemHeight (i);
2576 item_rect.Y = height * (index - top_index);
2578 item_rect.Height = height;
2582 public void HideWindow ()
2584 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2589 owner.DropDownListBoxFinished ();
2592 private int IndexFromPointDisplayRectangle (int x, int y)
2594 for (int i = top_item; i <= last_item; i++) {
2595 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2602 public void InvalidateItem (int index)
2605 Invalidate (GetItemDisplayRectangle (index, top_item));
2608 public int LastVisibleItem ()
2610 Rectangle item_rect;
2611 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2614 for (i = top_item; i < owner.Items.Count; i++) {
2615 item_rect = GetItemDisplayRectangle (i, top_item);
2616 if (item_rect.Y + item_rect.Height > top_y) {
2623 public void SetTopItem (int item)
2625 if (top_item == item)
2628 UpdateLastVisibleItem ();
2632 public int FirstVisibleItem ()
2637 public void EnsureTop (int item)
2639 if (owner.Items.Count == 0)
2641 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2644 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2647 else if (item < vscrollbar_ctrl.Minimum)
2648 item = vscrollbar_ctrl.Minimum;
2650 vscrollbar_ctrl.Value = item;
2653 bool scrollbar_grabbed = false;
2657 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2660 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2664 protected override void OnMouseDown (MouseEventArgs e)
2667 vscrollbar_ctrl.FireMouseDown (e);
2668 scrollbar_grabbed = true;
2672 protected override void OnMouseMove (MouseEventArgs e)
2674 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2677 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2678 vscrollbar_ctrl.FireMouseMove (e);
2682 Point pt = PointToClient (Control.MousePosition);
2683 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2686 HighlightedIndex = index;
2689 protected override void OnMouseUp (MouseEventArgs e)
2691 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2693 if (scrollbar_grabbed) {
2694 vscrollbar_ctrl.FireMouseUp (e);
2695 scrollbar_grabbed = false;
2697 HighlightedIndex = index;
2706 bool is_change = owner.SelectedIndex != index;
2708 owner.SetSelectedIndex (index, true);
2709 owner.OnSelectionChangeCommitted (new EventArgs ());
2711 // If the user selected the already selected item, SelectedIndex
2712 // won't fire these events, but .Net does, so we do it here
2714 owner.OnSelectedValueChanged (EventArgs.Empty);
2715 owner.OnSelectedIndexChanged (EventArgs.Empty);
2721 internal override void OnPaintInternal (PaintEventArgs pevent)
2723 Draw (pevent.ClipRectangle,pevent.Graphics);
2726 public bool ShowWindow ()
2728 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2731 HighlightedIndex = owner.SelectedIndex;
2737 owner.OnDropDown (EventArgs.Empty);
2741 public void UpdateLastVisibleItem ()
2743 last_item = LastVisibleItem ();
2746 public void Scroll (int delta)
2748 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2751 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2753 int val = vscrollbar_ctrl.Value + delta;
2756 else if (val < vscrollbar_ctrl.Minimum)
2757 val = vscrollbar_ctrl.Minimum;
2758 vscrollbar_ctrl.Value = val;
2761 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2763 if (owner.Items.Count == 0)
2766 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2771 private void VerticalScrollEvent (object sender, EventArgs e)
2773 if (top_item == vscrollbar_ctrl.Value)
2776 top_item = vscrollbar_ctrl.Value;
2777 UpdateLastVisibleItem ();
2781 protected override void WndProc(ref Message m) {
2782 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2783 owner.Select (false, true);
2785 base.WndProc (ref m);
2788 #endregion Private Methods