1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Mike Kestner <mkestner@novell.com>
25 // Daniel Nauck (dna(at)mono-project(dot)de)
28 using System.Collections;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms
39 [DefaultProperty("Items")]
40 [DefaultEvent("SelectedIndexChanged")]
41 [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [DefaultBindingProperty ("Text")]
44 [ClassInterface (ClassInterfaceType.AutoDispatch)]
47 public class ComboBox : ListControl
49 private DrawMode draw_mode = DrawMode.Normal;
50 private ComboBoxStyle dropdown_style;
51 private int dropdown_width = -1;
52 private int selected_index = -1;
53 private ObjectCollection items;
54 private bool suspend_ctrlupdate;
55 private int maxdrop_items = 8;
56 private bool integral_height = true;
58 private int max_length;
59 private ComboListBox listbox_ctrl;
60 private ComboTextBox textbox_ctrl;
61 private bool process_textchanged_event = true;
62 private bool item_height_specified;
63 private int item_height;
64 private int requested_height = -1;
65 private Hashtable item_heights;
66 private bool show_dropdown_button;
67 private ButtonState button_state = ButtonState.Normal;
68 private bool dropped_down;
69 private Rectangle text_area;
70 private Rectangle button_area;
71 private Rectangle listbox_area;
72 private const int button_width = 16;
73 bool drop_down_button_entered;
75 private AutoCompleteStringCollection auto_complete_custom_source = null;
76 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
77 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
78 private int drop_down_height;
79 private FlatStyle flat_style;
83 public class ChildAccessibleObject : AccessibleObject {
85 public ChildAccessibleObject (ComboBox owner, IntPtr handle)
90 public override string Name {
99 items = new ObjectCollection (this);
100 DropDownStyle = ComboBoxStyle.DropDown;
101 item_height = FontHeight + 2;
102 background_color = ThemeEngine.Current.ColorWindow;
103 border_style = BorderStyle.None;
106 drop_down_height = 106;
107 flat_style = FlatStyle.Standard;
111 MouseDown += new MouseEventHandler (OnMouseDownCB);
112 MouseUp += new MouseEventHandler (OnMouseUpCB);
113 MouseMove += new MouseEventHandler (OnMouseMoveCB);
114 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
115 MouseEnter += new EventHandler (OnMouseEnter);
116 MouseLeave += new EventHandler (OnMouseLeave);
117 KeyDown +=new KeyEventHandler(OnKeyDownCB);
123 [EditorBrowsable (EditorBrowsableState.Never)]
124 public new event EventHandler BackgroundImageChanged {
125 add { base.BackgroundImageChanged += value; }
126 remove { base.BackgroundImageChanged -= value; }
132 [EditorBrowsable (EditorBrowsableState.Never)]
133 public new event EventHandler BackgroundImageLayoutChanged
135 add { base.BackgroundImageLayoutChanged += value; }
136 remove { base.BackgroundImageLayoutChanged -= value; }
140 [EditorBrowsable (EditorBrowsableState.Never)]
141 public new event EventHandler DoubleClick
143 add { base.DoubleClick += value; }
144 remove { base.DoubleClick -= value; }
148 static object DrawItemEvent = new object ();
149 static object DropDownEvent = new object ();
150 static object DropDownStyleChangedEvent = new object ();
151 static object MeasureItemEvent = new object ();
152 static object SelectedIndexChangedEvent = new object ();
153 static object SelectionChangeCommittedEvent = new object ();
155 static object DropDownClosedEvent = new object ();
156 static object TextUpdateEvent = new object ();
159 public event DrawItemEventHandler DrawItem {
160 add { Events.AddHandler (DrawItemEvent, value); }
161 remove { Events.RemoveHandler (DrawItemEvent, value); }
164 public event EventHandler DropDown {
165 add { Events.AddHandler (DropDownEvent, value); }
166 remove { Events.RemoveHandler (DropDownEvent, value); }
169 public event EventHandler DropDownClosed
171 add { Events.AddHandler (DropDownClosedEvent, value); }
172 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
176 public event EventHandler DropDownStyleChanged {
177 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
178 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
181 public event MeasureItemEventHandler MeasureItem {
182 add { Events.AddHandler (MeasureItemEvent, value); }
183 remove { Events.RemoveHandler (MeasureItemEvent, value); }
187 [EditorBrowsable (EditorBrowsableState.Never)]
188 public new event EventHandler PaddingChanged
190 add { base.PaddingChanged += value; }
191 remove { base.PaddingChanged -= value; }
196 [EditorBrowsable (EditorBrowsableState.Never)]
197 public new event PaintEventHandler Paint {
198 add { base.Paint += value; }
199 remove { base.Paint -= value; }
202 public event EventHandler SelectedIndexChanged {
203 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
204 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
207 public event EventHandler SelectionChangeCommitted {
208 add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
209 remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
212 public event EventHandler TextUpdate
214 add { Events.AddHandler (TextUpdateEvent, value); }
215 remove { Events.RemoveHandler (TextUpdateEvent, value); }
221 #region Public Properties
223 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
224 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
226 [EditorBrowsable (EditorBrowsableState.Always)]
228 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
229 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
230 public AutoCompleteStringCollection AutoCompleteCustomSource {
232 if(auto_complete_custom_source == null) {
233 auto_complete_custom_source = new AutoCompleteStringCollection ();
234 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
236 return auto_complete_custom_source;
239 if(auto_complete_custom_source == value)
242 if(auto_complete_custom_source != null) //remove eventhandler from old collection
243 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
245 auto_complete_custom_source = value;
247 if(auto_complete_custom_source != null)
248 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
250 SetTextBoxAutoCompleteData ();
254 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
256 [EditorBrowsable (EditorBrowsableState.Always)]
257 [DefaultValue (AutoCompleteMode.None)]
258 public AutoCompleteMode AutoCompleteMode {
259 get { return auto_complete_mode; }
261 if(auto_complete_mode == value)
264 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
265 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
267 auto_complete_mode = value;
268 SetTextBoxAutoCompleteData ();
272 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
274 [EditorBrowsable (EditorBrowsableState.Always)]
275 [DefaultValue (AutoCompleteSource.None)]
276 public AutoCompleteSource AutoCompleteSource {
277 get { return auto_complete_source; }
279 if(auto_complete_source == value)
282 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
283 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
285 auto_complete_source = value;
286 SetTextBoxAutoCompleteData ();
290 void SetTextBoxAutoCompleteData ()
292 if (textbox_ctrl == null)
295 textbox_ctrl.AutoCompleteMode = auto_complete_mode;
297 if (auto_complete_source == AutoCompleteSource.ListItems) {
298 textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
299 textbox_ctrl.AutoCompleteCustomSource = null;
300 textbox_ctrl.AutoCompleteInternalSource = this;
302 textbox_ctrl.AutoCompleteSource = auto_complete_source;
303 textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
304 textbox_ctrl.AutoCompleteInternalSource = null;
308 public override Color BackColor {
309 get { return base.BackColor; }
311 if (base.BackColor == value)
313 base.BackColor = value;
319 [EditorBrowsable (EditorBrowsableState.Never)]
320 public override Image BackgroundImage {
321 get { return base.BackgroundImage; }
323 if (base.BackgroundImage == value)
325 base.BackgroundImage = value;
332 [EditorBrowsable (EditorBrowsableState.Never)]
333 public override ImageLayout BackgroundImageLayout {
334 get { return base.BackgroundImageLayout; }
335 set { base.BackgroundImageLayout = value; }
339 protected override CreateParams CreateParams {
340 get { return base.CreateParams;}
344 [DefaultValue ((string)null)]
345 [AttributeProvider (typeof (IListSource))]
346 [RefreshProperties (RefreshProperties.Repaint)]
347 [MWFCategory("Data")]
348 public new object DataSource {
349 get { return base.DataSource; }
350 set { base.DataSource = value; }
354 protected override Size DefaultSize {
355 get { return new Size (121, 21); }
358 [RefreshProperties(RefreshProperties.Repaint)]
359 [DefaultValue (DrawMode.Normal)]
360 [MWFCategory("Behavior")]
361 public DrawMode DrawMode {
362 get { return draw_mode; }
364 if (!Enum.IsDefined (typeof (DrawMode), value))
365 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
367 if (draw_mode == value)
370 if (draw_mode == DrawMode.OwnerDrawVariable)
373 if (draw_mode == DrawMode.OwnerDrawVariable)
374 item_heights = new Hashtable ();
382 [EditorBrowsable (EditorBrowsableState.Always)]
383 [MWFCategory("Behavior")]
384 public int DropDownHeight {
386 return drop_down_height;
390 throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
392 drop_down_height = value;
393 IntegralHeight = false;
398 [DefaultValue (ComboBoxStyle.DropDown)]
399 [RefreshProperties(RefreshProperties.Repaint)]
400 [MWFCategory("Appearance")]
401 public ComboBoxStyle DropDownStyle {
402 get { return dropdown_style; }
404 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
405 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
407 if (dropdown_style == value)
412 if (dropdown_style == ComboBoxStyle.Simple) {
413 if (listbox_ctrl != null) {
414 Controls.RemoveImplicit (listbox_ctrl);
415 listbox_ctrl.Dispose ();
420 dropdown_style = value;
422 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
423 Controls.RemoveImplicit (textbox_ctrl);
424 textbox_ctrl.Dispose ();
428 if (dropdown_style == ComboBoxStyle.Simple) {
429 show_dropdown_button = false;
431 CreateComboListBox ();
432 Controls.AddImplicit (listbox_ctrl);
433 listbox_ctrl.Visible = true;
435 // This should give us a 150 default height
436 // for Simple mode if size hasn't been set
437 // (DefaultSize doesn't work for us in this case)
438 if (requested_height == -1)
439 requested_height = 150;
441 show_dropdown_button = true;
442 button_state = ButtonState.Normal;
445 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
446 textbox_ctrl = new ComboTextBox (this);
447 object selected_item = SelectedItem;
448 if (selected_item != null)
449 textbox_ctrl.Text = GetItemText (selected_item);
450 textbox_ctrl.BorderStyle = BorderStyle.None;
451 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
452 textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
453 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
454 textbox_ctrl.ContextMenu = ContextMenu;
456 if (IsHandleCreated == true)
457 Controls.AddImplicit (textbox_ctrl);
459 SetTextBoxAutoCompleteData ();
464 OnDropDownStyleChanged (EventArgs.Empty);
467 UpdateComboBoxBounds ();
472 [MWFCategory("Behavior")]
473 public int DropDownWidth {
475 if (dropdown_width == -1)
478 return dropdown_width;
481 if (dropdown_width == value)
486 throw new ArgumentOutOfRangeException ("DropDownWidth",
487 "The DropDownWidth value is less than one.");
489 throw new ArgumentException ("The DropDownWidth value is less than one.");
492 dropdown_width = value;
497 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
498 public bool DroppedDown {
500 if (dropdown_style == ComboBoxStyle.Simple)
506 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
512 listbox_ctrl.HideWindow ();
517 [DefaultValue (FlatStyle.Standard)]
519 [MWFCategory("Appearance")]
520 public FlatStyle FlatStyle {
521 get { return flat_style; }
523 if (!Enum.IsDefined (typeof (FlatStyle), value))
524 throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
533 public override bool Focused {
534 get { return base.Focused; }
537 public override Color ForeColor {
538 get { return base.ForeColor; }
540 if (base.ForeColor == value)
542 base.ForeColor = value;
547 [DefaultValue (true)]
549 [MWFCategory("Behavior")]
550 public bool IntegralHeight {
551 get { return integral_height; }
553 if (integral_height == value)
555 integral_height = value;
556 UpdateComboBoxBounds ();
562 [MWFCategory("Behavior")]
563 public int ItemHeight {
565 if (item_height == -1) {
566 SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
567 item_height = (int) sz.Height;
574 throw new ArgumentOutOfRangeException ("ItemHeight",
575 "The item height value is less than one.");
577 throw new ArgumentException ("The item height value is less than one.");
580 item_height_specified = true;
583 UpdateComboBoxBounds ();
589 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
591 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
593 [MergableProperty (false)]
595 [MWFCategory("Data")]
596 public ComboBox.ObjectCollection Items {
597 get { return items; }
602 [MWFCategory("Behavior")]
603 public int MaxDropDownItems {
604 get { return maxdrop_items; }
606 if (maxdrop_items == value)
608 maxdrop_items = value;
613 public override Size MaximumSize {
614 get { return base.MaximumSize; }
616 base.MaximumSize = new Size (value.Width, 0);
623 [MWFCategory("Behavior")]
624 public int MaxLength {
625 get { return max_length; }
627 if (max_length == value)
632 if (dropdown_style != ComboBoxStyle.DropDownList) {
636 textbox_ctrl.MaxLength = value;
642 public override Size MinimumSize {
643 get { return base.MinimumSize; }
645 base.MinimumSize = new Size (value.Width, 0);
649 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
650 [EditorBrowsable (EditorBrowsableState.Never)]
652 public new Padding Padding {
653 get { return base.Padding; }
654 set { base.Padding = value; }
658 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
660 public int PreferredHeight {
661 get { return Font.Height + 8; }
665 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
666 public override int SelectedIndex {
667 get { return selected_index; }
669 if (selected_index == value)
672 if (value <= -2 || value >= Items.Count)
673 throw new ArgumentOutOfRangeException ("SelectedIndex");
674 selected_index = value;
676 if (dropdown_style != ComboBoxStyle.DropDownList) {
678 SetControlText (string.Empty, false);
680 SetControlText (GetItemText (Items [value]), false);
683 if (DropDownStyle == ComboBoxStyle.DropDownList)
686 if (listbox_ctrl != null)
687 listbox_ctrl.HighlightedIndex = value;
689 OnSelectedValueChanged (new EventArgs ());
690 OnSelectedIndexChanged (new EventArgs ());
691 OnSelectedItemChanged (new EventArgs ());
696 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
698 public object SelectedItem {
699 get { return selected_index == -1 ? null : Items [selected_index]; }
701 object item = selected_index == -1 ? null : Items [selected_index];
708 SelectedIndex = Items.IndexOf (value);
713 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
714 public string SelectedText {
716 if (dropdown_style == ComboBoxStyle.DropDownList)
719 string retval = textbox_ctrl.SelectedText;
722 // On 1.1, the textbox will return null, combobox returns ""
723 if (retval == null && !textbox_ctrl.IsHandleCreated)
729 if (dropdown_style == ComboBoxStyle.DropDownList)
731 textbox_ctrl.SelectedText = value;
736 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
737 public int SelectionLength {
739 if (dropdown_style == ComboBoxStyle.DropDownList)
742 int result = textbox_ctrl.SelectionLength;
743 return result == -1 ? 0 : result;
746 if (dropdown_style == ComboBoxStyle.DropDownList)
748 if (textbox_ctrl.SelectionLength == value)
750 textbox_ctrl.SelectionLength = value;
755 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
756 public int SelectionStart {
758 if (dropdown_style == ComboBoxStyle.DropDownList)
760 return textbox_ctrl.SelectionStart;
763 if (dropdown_style == ComboBoxStyle.DropDownList)
765 if (textbox_ctrl.SelectionStart == value)
767 textbox_ctrl.SelectionStart = value;
771 [DefaultValue (false)]
772 [MWFCategory("Behavior")]
774 get { return sorted; }
789 public override string Text {
791 if (dropdown_style != ComboBoxStyle.DropDownList) {
792 if (textbox_ctrl != null) {
793 return textbox_ctrl.Text;
797 if (SelectedItem != null)
798 return GetItemText (SelectedItem);
804 if (SelectedIndex == -1) {
805 if (dropdown_style != ComboBoxStyle.DropDownList)
806 SetControlText (string.Empty, false);
813 // do nothing if value exactly matches text of selected item
814 if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
817 // find exact match using case-sensitive comparison, and if does
818 // not result in any match then use case-insensitive comparison
819 int index = FindStringExact (value, -1, false);
821 index = FindStringExact (value, -1, true);
824 SelectedIndex = index;
828 if (dropdown_style != ComboBoxStyle.DropDownList)
829 textbox_ctrl.Text = GetItemText (value);
833 #endregion Public Properties
835 #region Internal Properties
836 internal Rectangle ButtonArea {
837 get { return button_area; }
840 internal Rectangle TextArea {
841 get { return text_area; }
845 #region UIA Framework Properties
847 internal TextBox UIATextBox {
848 get { return textbox_ctrl; }
851 #endregion UIA Framework Properties
853 #region Public Methods
855 [Obsolete ("This method has been deprecated")]
857 protected virtual void AddItemsCore (object[] value)
862 public void BeginUpdate ()
864 suspend_ctrlupdate = true;
868 protected override AccessibleObject CreateAccessibilityInstance ()
870 return base.CreateAccessibilityInstance ();
873 protected override void CreateHandle ()
875 base.CreateHandle ();
879 protected override void Dispose (bool disposing)
882 if (listbox_ctrl != null) {
883 listbox_ctrl.Dispose ();
884 Controls.RemoveImplicit (listbox_ctrl);
888 if (textbox_ctrl != null) {
889 Controls.RemoveImplicit (textbox_ctrl);
890 textbox_ctrl.Dispose ();
895 base.Dispose (disposing);
898 public void EndUpdate ()
900 suspend_ctrlupdate = false;
905 public int FindString (string s)
907 return FindString (s, -1);
910 public int FindString (string s, int startIndex)
912 if (s == null || Items.Count == 0)
916 if (startIndex < -1 || startIndex >= Items.Count)
918 if (startIndex < -1 || startIndex >= Items.Count - 1)
920 throw new ArgumentOutOfRangeException ("startIndex");
924 if (i == (Items.Count - 1))
929 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
931 if (i == (Items.Count - 1))
933 } while (i != startIndex);
938 public int FindStringExact (string s)
940 return FindStringExact (s, -1);
943 public int FindStringExact (string s, int startIndex)
945 return FindStringExact (s, startIndex, true);
948 private int FindStringExact (string s, int startIndex, bool ignoreCase)
950 if (s == null || Items.Count == 0)
954 if (startIndex < -1 || startIndex >= Items.Count)
956 if (startIndex < -1 || startIndex >= Items.Count - 1)
958 throw new ArgumentOutOfRangeException ("startIndex");
962 if (i == (Items.Count - 1))
967 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
969 if (i == (Items.Count - 1))
971 } while (i != startIndex);
976 public int GetItemHeight (int index)
978 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
980 if (index < 0 || index >= Items.Count )
981 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
983 object item = Items [index];
984 if (item_heights.Contains (item))
985 return (int) item_heights [item];
987 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
988 OnMeasureItem (args);
989 item_heights [item] = args.ItemHeight;
990 return args.ItemHeight;
996 protected override bool IsInputKey (Keys keyData)
998 switch (keyData & ~Keys.Modifiers) {
1014 protected override void OnBackColorChanged (EventArgs e)
1016 base.OnBackColorChanged (e);
1018 if (textbox_ctrl != null)
1019 textbox_ctrl.BackColor = BackColor;
1022 protected override void OnDataSourceChanged (EventArgs e)
1024 base.OnDataSourceChanged (e);
1027 if (DataSource == null || DataManager == null) {
1031 SelectedIndex = DataManager.Position;
1035 protected override void OnDisplayMemberChanged (EventArgs e)
1037 base.OnDisplayMemberChanged (e);
1039 if (DataManager == null)
1042 SelectedIndex = DataManager.Position;
1044 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1045 SetControlText (GetItemText (Items [selected_index]), true);
1047 if (!IsHandleCreated)
1053 protected virtual void OnDrawItem (DrawItemEventArgs e)
1055 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1060 internal void HandleDrawItem (DrawItemEventArgs e)
1062 // Only raise OnDrawItem if we are in an OwnerDraw mode
1064 case DrawMode.OwnerDrawFixed:
1065 case DrawMode.OwnerDrawVariable:
1069 ThemeEngine.Current.DrawComboBoxItem (this, e);
1074 protected virtual void OnDropDown (EventArgs e)
1076 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1082 protected virtual void OnDropDownClosed (EventArgs e)
1084 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1090 protected virtual void OnDropDownStyleChanged (EventArgs e)
1092 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1097 protected override void OnFontChanged (EventArgs e)
1099 base.OnFontChanged (e);
1101 if (textbox_ctrl != null)
1102 textbox_ctrl.Font = Font;
1104 if (!item_height_specified)
1105 item_height = Font.Height + 2;
1108 UpdateComboBoxBounds ();
1113 protected override void OnForeColorChanged (EventArgs e)
1115 base.OnForeColorChanged (e);
1116 if (textbox_ctrl != null)
1117 textbox_ctrl.ForeColor = ForeColor;
1120 [EditorBrowsable(EditorBrowsableState.Advanced)]
1121 protected override void OnGotFocus (EventArgs e)
1123 if (dropdown_style == ComboBoxStyle.DropDownList) {
1124 // We draw DDL styles manually, so they require a
1125 // refresh to have their selection drawn
1129 if (textbox_ctrl != null) {
1130 textbox_ctrl.SetSelectable (false);
1131 textbox_ctrl.ShowSelection = true;
1132 textbox_ctrl.ActivateCaret (true);
1133 textbox_ctrl.SelectAll ();
1136 base.OnGotFocus (e);
1139 [EditorBrowsable(EditorBrowsableState.Advanced)]
1140 protected override void OnLostFocus (EventArgs e)
1142 if (dropdown_style == ComboBoxStyle.DropDownList) {
1143 // We draw DDL styles manually, so they require a
1144 // refresh to have their selection drawn
1148 if (listbox_ctrl != null && dropped_down) {
1149 listbox_ctrl.HideWindow ();
1152 if (textbox_ctrl != null) {
1153 textbox_ctrl.SetSelectable (true);
1154 textbox_ctrl.ActivateCaret (false);
1155 textbox_ctrl.ShowSelection = false;
1156 textbox_ctrl.SelectionLength = 0;
1159 base.OnLostFocus (e);
1162 protected override void OnHandleCreated (EventArgs e)
1164 base.OnHandleCreated (e);
1166 SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1168 if (textbox_ctrl != null)
1169 Controls.AddImplicit (textbox_ctrl);
1172 UpdateComboBoxBounds ();
1175 protected override void OnHandleDestroyed (EventArgs e)
1177 base.OnHandleDestroyed (e);
1180 protected override void OnKeyPress (KeyPressEventArgs e)
1182 if (dropdown_style == ComboBoxStyle.DropDownList) {
1183 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1185 SelectedIndex = index;
1186 if (DroppedDown) { //Scroll into view
1187 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1188 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1189 // Or, selecting an item earlier in the list.
1190 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1191 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1196 base.OnKeyPress (e);
1199 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1201 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1206 protected override void OnParentBackColorChanged (EventArgs e)
1208 base.OnParentBackColorChanged (e);
1211 protected override void OnResize (EventArgs e)
1214 if (listbox_ctrl != null)
1215 listbox_ctrl.CalcListBoxArea ();
1218 protected override void OnSelectedIndexChanged (EventArgs e)
1220 base.OnSelectedIndexChanged (e);
1222 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1227 protected virtual void OnSelectedItemChanged (EventArgs e)
1231 protected override void OnSelectedValueChanged (EventArgs e)
1233 base.OnSelectedValueChanged (e);
1236 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1238 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1243 protected override void RefreshItem (int index)
1245 if (index < 0 || index >= Items.Count)
1246 throw new ArgumentOutOfRangeException ("index");
1248 if (draw_mode == DrawMode.OwnerDrawVariable)
1249 item_heights.Remove (Items [index]);
1253 protected override void RefreshItems ()
1255 for (int i = 0; i < Items.Count; i++) {
1262 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1263 SetControlText (GetItemText (Items [selected_index]), false);
1266 public override void ResetText ()
1268 Text = String.Empty;
1271 protected override bool ProcessKeyEventArgs (ref Message m)
1273 return base.ProcessKeyEventArgs (ref m);
1276 [EditorBrowsable (EditorBrowsableState.Advanced)]
1277 protected override void OnKeyDown (KeyEventArgs e)
1282 [EditorBrowsable (EditorBrowsableState.Advanced)]
1283 protected override void OnValidating (CancelEventArgs e)
1285 base.OnValidating (e);
1288 [EditorBrowsable (EditorBrowsableState.Advanced)]
1289 protected override void OnTextChanged (EventArgs e)
1291 base.OnTextChanged (e);
1295 protected virtual void OnTextUpdate (EventArgs e)
1297 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1302 protected override void OnMouseLeave (EventArgs e)
1305 if (flat_style == FlatStyle.Popup)
1308 base.OnMouseLeave (e);
1311 protected override void OnMouseEnter (EventArgs e)
1314 if (flat_style == FlatStyle.Popup)
1317 base.OnMouseEnter (e);
1322 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1324 base.ScaleControl (factor, specified);
1328 public void Select (int start, int length)
1331 throw new ArgumentException ("Start cannot be less than zero");
1334 throw new ArgumentException ("length cannot be less than zero");
1336 if (dropdown_style == ComboBoxStyle.DropDownList)
1339 textbox_ctrl.Select (start, length);
1342 public void SelectAll ()
1344 if (dropdown_style == ComboBoxStyle.DropDownList)
1347 if (textbox_ctrl != null) {
1348 textbox_ctrl.ShowSelection = true;
1349 textbox_ctrl.SelectAll ();
1353 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1355 bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1356 bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1358 if ((specified & BoundsSpecified.Height) != 0 ||
1359 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1361 requested_height = height;
1362 height = SnapHeight (height);
1365 base.SetBoundsCore (x, y, width, height, specified);
1368 protected override void SetItemCore (int index, object value)
1370 if (index < 0 || index >= Items.Count)
1373 Items[index] = value;
1376 protected override void SetItemsCore (IList value)
1381 Items.AddRange (value);
1387 public override string ToString ()
1389 return base.ToString () + ", Items.Count:" + Items.Count;
1392 protected override void WndProc (ref Message m)
1394 switch ((Msg) m.Msg) {
1396 case Msg.WM_KEYDOWN:
1397 Keys keys = (Keys) m.WParam.ToInt32 ();
1399 // Don't pass the message to base if auto complete is being used and available.
1400 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1401 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1405 if (keys == Keys.Up || keys == Keys.Down)
1407 goto case Msg.WM_CHAR;
1409 if (textbox_ctrl != null)
1410 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1412 case Msg.WM_MOUSELEAVE:
1413 Point location = PointToClient (Control.MousePosition);
1414 if (ClientRectangle.Contains (location))
1420 base.WndProc (ref m);
1423 #endregion Public Methods
1425 #region Private Methods
1427 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1428 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1429 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1434 internal override bool InternalCapture {
1435 get { return Capture; }
1439 void LayoutComboBox ()
1441 int border = ThemeEngine.Current.Border3DSize.Width;
1443 text_area = ClientRectangle;
1444 text_area.Height = PreferredHeight;
1446 listbox_area = ClientRectangle;
1447 listbox_area.Y = text_area.Bottom + 3;
1448 listbox_area.Height -= (text_area.Height + 2);
1450 Rectangle prev_button_area = button_area;
1452 if (DropDownStyle == ComboBoxStyle.Simple)
1453 button_area = Rectangle.Empty;
1455 button_area = text_area;
1456 button_area.X = text_area.Right - button_width - border;
1457 button_area.Y = text_area.Y + border;
1458 button_area.Width = button_width;
1459 button_area.Height = text_area.Height - 2 * border;
1461 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1462 button_area.Inflate (1, 1);
1464 button_area.Width -= 2;
1469 if (button_area != prev_button_area) {
1470 prev_button_area.Y -= border;
1471 prev_button_area.Width += border;
1472 prev_button_area.Height += 2 * border;
1473 Invalidate (prev_button_area);
1474 Invalidate (button_area);
1477 if (textbox_ctrl != null) {
1478 int text_border = border + 1;
1479 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1480 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1481 textbox_ctrl.Height = text_area.Height - text_border * 2;
1484 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1485 listbox_ctrl.Location = listbox_area.Location;
1486 listbox_ctrl.CalcListBoxArea ();
1490 private void CreateComboListBox ()
1492 listbox_ctrl = new ComboListBox (this);
1493 listbox_ctrl.HighlightedIndex = SelectedIndex;
1496 internal void Draw (Rectangle clip, Graphics dc)
1498 Theme theme = ThemeEngine.Current;
1499 FlatStyle style = FlatStyle.Standard;
1500 bool is_flat = false;
1503 style = this.FlatStyle;
1504 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1507 theme.ComboBoxDrawBackground (this, dc, clip, style);
1509 int border = theme.Border3DSize.Width;
1511 // No edit control, we paint the edit ourselves
1512 if (dropdown_style == ComboBoxStyle.DropDownList) {
1513 DrawItemState state = DrawItemState.None;
1514 Rectangle item_rect = text_area;
1515 item_rect.X += border;
1516 item_rect.Y += border;
1517 item_rect.Width -= (button_area.Width + 2 * border);
1518 item_rect.Height -= 2 * border;
1521 state = DrawItemState.Selected;
1522 state |= DrawItemState.Focus;
1525 state |= DrawItemState.ComboBoxEdit;
1526 HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1529 if (show_dropdown_button) {
1530 ButtonState current_state;
1532 current_state = button_state;
1534 current_state = ButtonState.Inactive;
1536 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1537 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1540 theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1542 theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
1547 internal bool DropDownButtonEntered {
1548 get { return drop_down_button_entered; }
1550 if (drop_down_button_entered == value)
1552 drop_down_button_entered = value;
1553 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1554 Invalidate (button_area);
1558 internal void DropDownListBox ()
1560 DropDownButtonEntered = false;
1562 if (DropDownStyle == ComboBoxStyle.Simple)
1565 if (listbox_ctrl == null)
1566 CreateComboListBox ();
1568 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1570 FindMatchOrSetIndex(SelectedIndex);
1573 if (textbox_ctrl != null)
1574 textbox_ctrl.HideAutoCompleteList ();
1577 if (listbox_ctrl.ShowWindow ())
1578 dropped_down = true;
1580 button_state = ButtonState.Pushed;
1581 if (dropdown_style == ComboBoxStyle.DropDownList)
1582 Invalidate (text_area);
1585 internal void DropDownListBoxFinished ()
1587 if (DropDownStyle == ComboBoxStyle.Simple)
1590 FindMatchOrSetIndex (SelectedIndex);
1591 button_state = ButtonState.Normal;
1592 Invalidate (button_area);
1593 dropped_down = false;
1595 OnDropDownClosed (EventArgs.Empty);
1598 * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1599 * this causes the popup to appear under the main form. This is horrible but necessary
1602 // If the user opens a new form in an event, it will close our dropdown,
1603 // so we need a null check here
1604 if (listbox_ctrl != null) {
1605 listbox_ctrl.Dispose ();
1606 listbox_ctrl = null;
1610 private int FindStringCaseInsensitive (string search)
1612 if (search.Length == 0) {
1616 for (int i = 0; i < Items.Count; i++)
1618 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1625 // Search in the list for the substring, starting the search at the list
1626 // position specified, the search wraps thus covering all the list.
1627 internal int FindStringCaseInsensitive (string search, int start_index)
1629 if (search.Length == 0) {
1632 // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1633 if (start_index < 0 || start_index > Items.Count)
1634 throw new ArgumentOutOfRangeException("start_index");
1636 for (int i = 0; i < Items.Count; i++) {
1637 int index = (i + start_index) % Items.Count;
1638 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1645 internal override bool IsInputCharInternal (char charCode)
1650 internal override ContextMenu ContextMenuInternal {
1652 return base.ContextMenuInternal;
1655 base.ContextMenuInternal = value;
1656 if (textbox_ctrl != null) {
1657 textbox_ctrl.ContextMenu = value;
1662 internal void RestoreContextMenu ()
1664 textbox_ctrl.RestoreContextMenu ();
1667 private void OnKeyDownCB(object sender, KeyEventArgs e)
1669 if (Items.Count == 0)
1676 FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1679 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1680 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1684 if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1687 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1690 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1691 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1695 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1699 SelectedIndex = Math.Max (SelectedIndex - offset, 0);
1702 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1703 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1707 if (SelectedIndex == -1) {
1709 if (dropdown_style != ComboBoxStyle.Simple)
1713 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1717 SelectedIndex = Math.Min (SelectedIndex + offset, Items.Count - 1);
1720 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1721 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1726 DropDownListBoxFinished ();
1730 if (dropdown_style == ComboBoxStyle.DropDownList) {
1734 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1735 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1740 if (dropdown_style == ComboBoxStyle.DropDownList) {
1741 SelectedIndex = Items.Count - 1;
1744 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1745 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1754 // If no item is currently selected, and an item is found matching the text
1755 // in the textbox, then selected that item. Otherwise the item at the given
1756 // index is selected.
1757 private void FindMatchOrSetIndex(int index)
1760 if (SelectedIndex == -1 && Text.Length != 0)
1761 match = FindStringCaseInsensitive(Text);
1763 SelectedIndex = match;
1765 SelectedIndex = index;
1768 void OnMouseDownCB (object sender, MouseEventArgs e)
1771 if (DropDownStyle == ComboBoxStyle.DropDownList)
1772 area = ClientRectangle;
1776 if (area.Contains (e.X, e.Y)) {
1777 if (Items.Count > 0)
1780 button_state = ButtonState.Pushed;
1781 OnDropDown (EventArgs.Empty);
1784 Invalidate (button_area);
1790 void OnMouseEnter (object sender, EventArgs e)
1792 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1796 void OnMouseLeave (object sender, EventArgs e)
1798 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1799 drop_down_button_entered = false;
1802 if (show_dropdown_button)
1803 DropDownButtonEntered = false;
1807 void OnMouseMoveCB (object sender, MouseEventArgs e)
1809 if (show_dropdown_button && !dropped_down)
1810 DropDownButtonEntered = button_area.Contains (e.Location);
1812 if (DropDownStyle == ComboBoxStyle.Simple)
1815 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1816 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1817 if (listbox_ctrl.ClientRectangle.Contains (location))
1818 listbox_ctrl.Capture = true;
1822 void OnMouseUpCB (object sender, MouseEventArgs e)
1826 button_state = ButtonState.Normal;
1827 Invalidate (button_area);
1829 OnClick (EventArgs.Empty);
1832 listbox_ctrl.Capture = true;
1835 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1837 if (Items.Count == 0)
1840 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1841 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1842 listbox_ctrl.Scroll (-lines);
1844 int lines = me.Delta / 120;
1845 int index = SelectedIndex - lines;
1848 else if (index >= Items.Count)
1849 index = Items.Count - 1;
1850 SelectedIndex = index;
1854 internal override void OnPaintInternal (PaintEventArgs pevent)
1856 if (suspend_ctrlupdate)
1859 Draw (ClientRectangle, pevent.Graphics);
1862 private void OnTextBoxClick (object sender, EventArgs e)
1867 private void OnTextChangedEdit (object sender, EventArgs e)
1869 if (process_textchanged_event == false)
1872 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1875 // Setting base.Text below will raise this event
1876 // if we found something
1877 OnTextChanged (EventArgs.Empty);
1881 // TODO: THIS IS BROKEN-ISH
1882 // I don't think we should hilight, and setting the top item does weirdness
1883 // when there is no scrollbar
1885 if (listbox_ctrl != null) {
1886 listbox_ctrl.SetTopItem (item);
1887 listbox_ctrl.HighlightedIndex = item;
1890 base.Text = textbox_ctrl.Text;
1893 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1895 selected_index = -1;
1898 internal void SetControlText (string s, bool suppressTextChanged)
1900 if (suppressTextChanged)
1901 process_textchanged_event = false;
1903 textbox_ctrl.Text = s;
1904 textbox_ctrl.SelectAll ();
1905 process_textchanged_event = true;
1908 void UpdateComboBoxBounds ()
1910 if (requested_height == -1)
1913 // Save the requested height since set bounds can destroy it
1914 int save_height = requested_height;
1915 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1916 BoundsSpecified.Height);
1917 requested_height = save_height;
1920 int SnapHeight (int height)
1922 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1923 if (IntegralHeight) {
1924 int border = ThemeEngine.Current.Border3DSize.Height;
1925 int lb_height = (height - PreferredHeight - 2) - border * 2;
1926 if (lb_height > ItemHeight) {
1927 int partial = (lb_height) % ItemHeight;
1929 } else if (lb_height < ItemHeight)
1930 height = PreferredHeight;
1933 height = PreferredHeight;
1938 private void UpdatedItems ()
1940 if (listbox_ctrl != null) {
1941 listbox_ctrl.UpdateLastVisibleItem ();
1942 listbox_ctrl.CalcListBoxArea ();
1943 listbox_ctrl.Refresh ();
1947 #endregion Private Methods
1949 [ListBindableAttribute (false)]
1950 public class ObjectCollection : IList, ICollection, IEnumerable
1953 private ComboBox owner;
1954 internal ArrayList object_items = new ArrayList ();
1956 #region UIA Framework Events
1960 // We are using Reflection to add/remove internal events.
1961 // Class ListProvider uses the events.
1963 //Event used to generate UIA StructureChangedEvent
1964 static object UIACollectionChangedEvent = new object ();
1966 internal event CollectionChangeEventHandler UIACollectionChanged {
1967 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1968 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1971 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1973 CollectionChangeEventHandler eh
1974 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1980 #endregion UIA Framework Events
1982 public ObjectCollection (ComboBox owner)
1987 #region Public Properties
1989 get { return object_items.Count; }
1992 public bool IsReadOnly {
1993 get { return false; }
1997 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1998 public virtual object this [int index] {
2000 if (index < 0 || index >= Count)
2001 throw new ArgumentOutOfRangeException ("index");
2003 return object_items[index];
2006 if (index < 0 || index >= Count)
2007 throw new ArgumentOutOfRangeException ("index");
2009 throw new ArgumentNullException ("value");
2012 //UIA Framework event: Item Removed
2013 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
2016 object_items[index] = value;
2019 //UIA Framework event: Item Added
2020 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
2023 if (owner.listbox_ctrl != null)
2024 owner.listbox_ctrl.InvalidateItem (index);
2025 if (index == owner.SelectedIndex) {
2026 if (owner.textbox_ctrl == null)
2029 owner.textbox_ctrl.SelectedText = value.ToString ();
2034 bool ICollection.IsSynchronized {
2035 get { return false; }
2038 object ICollection.SyncRoot {
2039 get { return this; }
2042 bool IList.IsFixedSize {
2043 get { return false; }
2046 #endregion Public Properties
2048 #region Public Methods
2049 public int Add (object item)
2053 idx = AddItem (item);
2054 owner.UpdatedItems ();
2058 public void AddRange (object[] items)
2061 throw new ArgumentNullException ("items");
2063 foreach (object mi in items)
2066 owner.UpdatedItems ();
2069 public void Clear ()
2071 owner.selected_index = -1;
2072 object_items.Clear ();
2073 owner.UpdatedItems ();
2077 //UIA Framework event: Items list cleared
2078 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2082 public bool Contains (object value)
2085 throw new ArgumentNullException ("value");
2087 return object_items.Contains (value);
2091 public void CopyTo (object [] destination, int arrayIndex)
2093 object_items.CopyTo (destination, arrayIndex);
2096 void ICollection.CopyTo (Array destination, int index)
2098 object_items.CopyTo (destination, index);
2101 public void CopyTo (object [] dest, int arrayIndex)
2103 object_items.CopyTo (dest, arrayIndex);
2106 void ICollection.CopyTo (Array dest, int index)
2108 object_items.CopyTo (dest, index);
2112 public IEnumerator GetEnumerator ()
2114 return object_items.GetEnumerator ();
2117 int IList.Add (object item)
2122 public int IndexOf (object value)
2125 throw new ArgumentNullException ("value");
2127 return object_items.IndexOf (value);
2130 public void Insert (int index, object item)
2132 if (index < 0 || index > Count)
2133 throw new ArgumentOutOfRangeException ("index");
2135 throw new ArgumentNullException ("item");
2137 owner.BeginUpdate ();
2142 object_items.Insert (index, item);
2144 //UIA Framework event: Item added
2145 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2149 owner.EndUpdate (); // Calls UpdatedItems
2152 public void Remove (object value)
2157 if (IndexOf (value) == owner.SelectedIndex)
2158 owner.SelectedIndex = -1;
2160 RemoveAt (IndexOf (value));
2163 public void RemoveAt (int index)
2165 if (index < 0 || index >= Count)
2166 throw new ArgumentOutOfRangeException ("index");
2168 if (index == owner.SelectedIndex)
2169 owner.SelectedIndex = -1;
2172 object removed = object_items [index];
2176 object_items.RemoveAt (index);
2177 owner.UpdatedItems ();
2180 //UIA Framework event: Item removed
2181 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2184 #endregion Public Methods
2186 #region Private Methods
2187 private int AddItem (object item)
2190 throw new ArgumentNullException ("item");
2194 foreach (object o in object_items) {
2195 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2196 object_items.Insert (index, item);
2198 // If we added the new item before the selectedindex
2199 // bump the selectedindex by one, behavior differs if
2200 // Handle has not been created.
2201 if (index <= owner.selected_index && owner.IsHandleCreated)
2202 owner.selected_index++;
2205 //UIA Framework event: Item added
2206 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2214 object_items.Add (item);
2217 //UIA Framework event: Item added
2218 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2221 return object_items.Count - 1;
2224 internal void AddRange (IList items)
2226 foreach (object mi in items)
2229 owner.UpdatedItems ();
2232 internal void Sort ()
2234 // If the objects the user put here don't have their own comparer,
2235 // use one that compares based on the object's ToString
2236 if (object_items.Count > 0 && object_items[0] is IComparer)
2237 object_items.Sort ();
2239 object_items.Sort (new ObjectComparer (owner));
2242 private class ObjectComparer : IComparer
2244 private ListControl owner;
2246 public ObjectComparer (ListControl owner)
2251 #region IComparer Members
2252 public int Compare (object x, object y)
2254 return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2258 #endregion Private Methods
2261 internal class ComboTextBox : TextBox {
2263 private ComboBox owner;
2265 public ComboTextBox (ComboBox owner)
2268 ShowSelection = false;
2269 HideSelection = false;
2272 internal void SetSelectable (bool selectable)
2274 SetStyle (ControlStyles.Selectable, selectable);
2277 internal void ActivateCaret (bool active)
2280 document.CaretHasFocus ();
2282 document.CaretLostFocus ();
2286 internal override void OnTextUpdate ()
2288 base.OnTextUpdate ();
2289 owner.OnTextUpdate (EventArgs.Empty);
2293 protected override void OnGotFocus (EventArgs e)
2295 owner.Select (false, true);
2298 protected override void OnLostFocus (EventArgs e)
2300 owner.Select (false, true);
2303 public override bool Focused {
2305 return owner.Focused;
2309 internal override bool ActivateOnShow { get { return false; } }
2312 internal class ComboListBox : Control
2314 private ComboBox owner;
2315 private VScrollBarLB vscrollbar_ctrl;
2316 private int top_item; /* First item that we show the in the current page */
2317 private int last_item; /* Last visible item */
2318 internal int page_size; /* Number of listbox items per page */
2319 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2321 internal enum ItemNavigation
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 = false;
2429 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2430 Rectangle area = owner.listbox_area;
2432 height = area.Height;
2434 // No calculation needed
2435 if (height <= 0 || width <= 0)
2438 else { // DropDown or DropDownList
2440 width = owner.DropDownWidth;
2441 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2443 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2445 for (int i = 0; i < count; i++) {
2446 height += owner.GetItemHeight (i);
2451 height = owner.DropDownHeight;
2453 height = owner.ItemHeight * count;
2458 page_size = Math.Max (height / owner.ItemHeight, 1);
2460 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2461 if ((dropdown_style != ComboBoxStyle.Simple && owner.Items.Count <= owner.MaxDropDownItems)
2462 || (dropdown_style == ComboBoxStyle.Simple && owner.Items.Count * owner.ItemHeight < height)) {
2464 if (vscrollbar_ctrl != null)
2465 vscrollbar_ctrl.Visible = false;
2466 if (dropdown_style != ComboBoxStyle.Simple)
2467 height = owner.ItemHeight * owner.items.Count;
2469 /* Need vertical scrollbar */
2470 if (vscrollbar_ctrl == null) {
2471 vscrollbar_ctrl = new VScrollBarLB ();
2472 vscrollbar_ctrl.Minimum = 0;
2473 vscrollbar_ctrl.SmallChange = 1;
2474 vscrollbar_ctrl.LargeChange = 1;
2475 vscrollbar_ctrl.Maximum = 0;
2476 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2477 Controls.AddImplicit (vscrollbar_ctrl);
2480 vscrollbar_ctrl.Dock = DockStyle.Right;
2482 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2484 int large = page_size;
2486 int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2490 vscrollbar_ctrl.LargeChange = large;
2491 show_scrollbar = vscrollbar_ctrl.Visible = true;
2493 int hli = HighlightedIndex;
2495 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2496 vscrollbar_ctrl.Value = hli;
2500 Size = new Size (width, height);
2501 textarea_drawable = ClientRectangle;
2502 textarea_drawable.Width = width;
2503 textarea_drawable.Height = height;
2505 if (vscrollbar_ctrl != null && show_scrollbar)
2506 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2508 last_item = LastVisibleItem ();
2511 private void Draw (Rectangle clip, Graphics dc)
2513 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2515 if (owner.Items.Count > 0) {
2517 for (int i = top_item; i <= last_item; i++) {
2518 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2520 if (!clip.IntersectsWith (item_rect))
2523 DrawItemState state = DrawItemState.None;
2525 if (i == HighlightedIndex) {
2526 state |= DrawItemState.Selected;
2528 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2529 state |= DrawItemState.Focus;
2533 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2534 i, state, owner.ForeColor, owner.BackColor));
2539 int highlighted_index = -1;
2541 public int HighlightedIndex {
2542 get { return highlighted_index; }
2544 if (highlighted_index == value)
2547 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2548 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2549 highlighted_index = value;
2550 if (highlighted_index != -1)
2551 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2555 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2557 if (index < 0 || index >= owner.Items.Count)
2558 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2560 Rectangle item_rect = new Rectangle ();
2561 int height = owner.GetItemHeight (index);
2564 item_rect.Width = textarea_drawable.Width;
2565 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2567 for (int i = top_index; i < index; i++)
2568 item_rect.Y += owner.GetItemHeight (i);
2570 item_rect.Y = height * (index - top_index);
2572 item_rect.Height = height;
2576 public void HideWindow ()
2578 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2583 owner.DropDownListBoxFinished ();
2586 private int IndexFromPointDisplayRectangle (int x, int y)
2588 for (int i = top_item; i <= last_item; i++) {
2589 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2596 public void InvalidateItem (int index)
2599 Invalidate (GetItemDisplayRectangle (index, top_item));
2602 public int LastVisibleItem ()
2604 Rectangle item_rect;
2605 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2608 for (i = top_item; i < owner.Items.Count; i++) {
2609 item_rect = GetItemDisplayRectangle (i, top_item);
2610 if (item_rect.Y + item_rect.Height > top_y) {
2617 public void SetTopItem (int item)
2619 if (top_item == item)
2622 UpdateLastVisibleItem ();
2626 public int FirstVisibleItem ()
2631 bool scrollbar_grabbed = false;
2635 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2638 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2642 protected override void OnMouseDown (MouseEventArgs e)
2645 vscrollbar_ctrl.FireMouseDown (e);
2646 scrollbar_grabbed = true;
2650 protected override void OnMouseMove (MouseEventArgs e)
2652 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2655 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2656 vscrollbar_ctrl.FireMouseMove (e);
2660 Point pt = PointToClient (Control.MousePosition);
2661 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2664 HighlightedIndex = index;
2667 protected override void OnMouseUp (MouseEventArgs e)
2669 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2671 if (scrollbar_grabbed) {
2672 vscrollbar_ctrl.FireMouseUp (e);
2673 scrollbar_grabbed = false;
2675 HighlightedIndex = index;
2684 bool is_change = owner.SelectedIndex != index;
2686 owner.SelectedIndex = index;
2687 owner.OnSelectionChangeCommitted (new EventArgs ());
2689 // If the user selected the already selected item, SelectedIndex
2690 // won't fire these events, but .Net does, so we do it here
2692 owner.OnSelectedValueChanged (EventArgs.Empty);
2693 owner.OnSelectedIndexChanged (EventArgs.Empty);
2699 internal override void OnPaintInternal (PaintEventArgs pevent)
2701 Draw (pevent.ClipRectangle,pevent.Graphics);
2704 public bool ShowWindow ()
2706 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2709 HighlightedIndex = owner.SelectedIndex;
2715 owner.OnDropDown (EventArgs.Empty);
2719 public void UpdateLastVisibleItem ()
2721 last_item = LastVisibleItem ();
2724 public void Scroll (int delta)
2726 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2729 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2731 int val = vscrollbar_ctrl.Value + delta;
2734 else if (val < vscrollbar_ctrl.Minimum)
2735 val = vscrollbar_ctrl.Minimum;
2736 vscrollbar_ctrl.Value = val;
2739 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2741 if (owner.Items.Count == 0)
2744 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2749 private void VerticalScrollEvent (object sender, EventArgs e)
2751 if (top_item == vscrollbar_ctrl.Value)
2754 top_item = vscrollbar_ctrl.Value;
2755 UpdateLastVisibleItem ();
2759 protected override void WndProc(ref Message m) {
2760 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2761 owner.Select (false, true);
2763 base.WndProc (ref m);
2766 #endregion Private Methods