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.Hide ();
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 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1043 SetControlText (GetItemText (Items [selected_index]), true);
1045 if (!IsHandleCreated)
1048 SelectedIndex = DataManager.Position;
1052 protected virtual void OnDrawItem (DrawItemEventArgs e)
1055 case DrawMode.OwnerDrawFixed:
1056 case DrawMode.OwnerDrawVariable:
1057 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1062 ThemeEngine.Current.DrawComboBoxItem (this, e);
1067 protected virtual void OnDropDown (EventArgs e)
1069 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1075 protected virtual void OnDropDownClosed (EventArgs e)
1077 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1083 protected virtual void OnDropDownStyleChanged (EventArgs e)
1085 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1090 protected override void OnFontChanged (EventArgs e)
1092 base.OnFontChanged (e);
1094 if (textbox_ctrl != null)
1095 textbox_ctrl.Font = Font;
1097 if (!item_height_specified) {
1098 SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
1099 item_height = (int) sz.Height;
1103 UpdateComboBoxBounds ();
1108 protected override void OnForeColorChanged (EventArgs e)
1110 base.OnForeColorChanged (e);
1111 if (textbox_ctrl != null)
1112 textbox_ctrl.ForeColor = ForeColor;
1115 [EditorBrowsable(EditorBrowsableState.Advanced)]
1116 protected override void OnGotFocus (EventArgs e)
1118 if (dropdown_style == ComboBoxStyle.DropDownList) {
1119 // We draw DDL styles manually, so they require a
1120 // refresh to have their selection drawn
1124 if (textbox_ctrl != null) {
1125 textbox_ctrl.SetSelectable (false);
1126 textbox_ctrl.ShowSelection = true;
1127 textbox_ctrl.ActivateCaret (true);
1128 textbox_ctrl.SelectAll ();
1131 base.OnGotFocus (e);
1134 [EditorBrowsable(EditorBrowsableState.Advanced)]
1135 protected override void OnLostFocus (EventArgs e)
1137 if (dropdown_style == ComboBoxStyle.DropDownList) {
1138 // We draw DDL styles manually, so they require a
1139 // refresh to have their selection drawn
1143 if (listbox_ctrl != null && dropped_down) {
1144 listbox_ctrl.HideWindow ();
1147 if (textbox_ctrl != null) {
1148 textbox_ctrl.SetSelectable (true);
1149 textbox_ctrl.ActivateCaret (false);
1150 textbox_ctrl.ShowSelection = false;
1151 textbox_ctrl.SelectionLength = 0;
1154 base.OnLostFocus (e);
1157 protected override void OnHandleCreated (EventArgs e)
1159 base.OnHandleCreated (e);
1161 SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1163 if (textbox_ctrl != null)
1164 Controls.AddImplicit (textbox_ctrl);
1167 UpdateComboBoxBounds ();
1170 protected override void OnHandleDestroyed (EventArgs e)
1172 base.OnHandleDestroyed (e);
1175 protected override void OnKeyPress (KeyPressEventArgs e)
1177 if (dropdown_style == ComboBoxStyle.DropDownList) {
1178 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1180 SelectedIndex = index;
1181 if (DroppedDown) { //Scroll into view
1182 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1183 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1184 // Or, selecting an item earlier in the list.
1185 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1186 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1191 base.OnKeyPress (e);
1194 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1196 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1201 protected override void OnParentBackColorChanged (EventArgs e)
1203 base.OnParentBackColorChanged (e);
1206 protected override void OnResize (EventArgs e)
1209 if (listbox_ctrl != null)
1210 listbox_ctrl.CalcListBoxArea ();
1213 protected override void OnSelectedIndexChanged (EventArgs e)
1215 base.OnSelectedIndexChanged (e);
1217 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1222 protected virtual void OnSelectedItemChanged (EventArgs e)
1226 protected override void OnSelectedValueChanged (EventArgs e)
1228 base.OnSelectedValueChanged (e);
1231 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1233 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1238 protected override void RefreshItem (int index)
1240 if (index < 0 || index >= Items.Count)
1241 throw new ArgumentOutOfRangeException ("index");
1243 if (draw_mode == DrawMode.OwnerDrawVariable)
1244 item_heights.Remove (Items [index]);
1248 protected override void RefreshItems ()
1250 for (int i = 0; i < Items.Count; i++) {
1257 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1258 SetControlText (GetItemText (Items [selected_index]), false);
1261 public override void ResetText ()
1263 Text = String.Empty;
1266 protected override bool ProcessKeyEventArgs (ref Message m)
1268 return base.ProcessKeyEventArgs (ref m);
1271 [EditorBrowsable (EditorBrowsableState.Advanced)]
1272 protected override void OnKeyDown (KeyEventArgs e)
1277 [EditorBrowsable (EditorBrowsableState.Advanced)]
1278 protected override void OnValidating (CancelEventArgs e)
1280 base.OnValidating (e);
1283 [EditorBrowsable (EditorBrowsableState.Advanced)]
1284 protected override void OnTextChanged (EventArgs e)
1286 base.OnTextChanged (e);
1290 protected virtual void OnTextUpdate (EventArgs e)
1292 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1297 protected override void OnMouseLeave (EventArgs e)
1300 if (flat_style == FlatStyle.Popup)
1303 base.OnMouseLeave (e);
1306 protected override void OnMouseEnter (EventArgs e)
1309 if (flat_style == FlatStyle.Popup)
1312 base.OnMouseEnter (e);
1317 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1319 base.ScaleControl (factor, specified);
1323 public void Select (int start, int length)
1326 throw new ArgumentException ("Start cannot be less than zero");
1329 throw new ArgumentException ("length cannot be less than zero");
1331 if (dropdown_style == ComboBoxStyle.DropDownList)
1334 textbox_ctrl.Select (start, length);
1337 public void SelectAll ()
1339 if (dropdown_style == ComboBoxStyle.DropDownList)
1342 if (textbox_ctrl != null) {
1343 textbox_ctrl.ShowSelection = true;
1344 textbox_ctrl.SelectAll ();
1348 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1350 bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1351 bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1353 if ((specified & BoundsSpecified.Height) != 0 ||
1354 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1356 requested_height = height;
1357 height = SnapHeight (height);
1360 base.SetBoundsCore (x, y, width, height, specified);
1363 protected override void SetItemCore (int index, object value)
1365 if (index < 0 || index >= Items.Count)
1368 Items[index] = value;
1371 protected override void SetItemsCore (IList value)
1376 Items.AddRange (value);
1382 public override string ToString ()
1384 return base.ToString () + ", Items.Count:" + Items.Count;
1387 protected override void WndProc (ref Message m)
1389 switch ((Msg) m.Msg) {
1391 case Msg.WM_KEYDOWN:
1392 Keys keys = (Keys) m.WParam.ToInt32 ();
1393 if (keys == Keys.Up || keys == Keys.Down)
1395 goto case Msg.WM_CHAR;
1397 if (textbox_ctrl != null)
1398 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1400 case Msg.WM_MOUSELEAVE:
1401 Point location = PointToClient (Control.MousePosition);
1402 if (ClientRectangle.Contains (location))
1408 base.WndProc (ref m);
1411 #endregion Public Methods
1413 #region Private Methods
1415 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1416 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1417 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1422 internal override bool InternalCapture {
1423 get { return Capture; }
1427 void LayoutComboBox ()
1429 int border = ThemeEngine.Current.Border3DSize.Width;
1431 text_area = ClientRectangle;
1432 text_area.Height = PreferredHeight;
1434 listbox_area = ClientRectangle;
1435 listbox_area.Y = text_area.Bottom + 3;
1436 listbox_area.Height -= (text_area.Height + 2);
1438 Rectangle prev_button_area = button_area;
1440 if (DropDownStyle == ComboBoxStyle.Simple)
1441 button_area = Rectangle.Empty;
1443 button_area = text_area;
1444 button_area.X = text_area.Right - button_width - border;
1445 button_area.Y = text_area.Y + border;
1446 button_area.Width = button_width;
1447 button_area.Height = text_area.Height - 2 * border;
1449 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1450 button_area.Inflate (1, 1);
1452 button_area.Width -= 2;
1457 if (button_area != prev_button_area) {
1458 prev_button_area.Y -= border;
1459 prev_button_area.Width += border;
1460 prev_button_area.Height += 2 * border;
1461 Invalidate (prev_button_area);
1462 Invalidate (button_area);
1465 if (textbox_ctrl != null) {
1466 int text_border = border + 1;
1467 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1468 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1469 textbox_ctrl.Height = text_area.Height - text_border * 2;
1472 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1473 listbox_ctrl.Location = listbox_area.Location;
1474 listbox_ctrl.CalcListBoxArea ();
1478 private void CreateComboListBox ()
1480 listbox_ctrl = new ComboListBox (this);
1481 listbox_ctrl.HighlightedIndex = SelectedIndex;
1484 internal void Draw (Rectangle clip, Graphics dc)
1486 Theme theme = ThemeEngine.Current;
1487 FlatStyle style = FlatStyle.Standard;
1488 bool is_flat = false;
1491 style = this.FlatStyle;
1492 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1495 theme.ComboBoxDrawBackground (this, dc, clip, style);
1497 int border = theme.Border3DSize.Width;
1499 // No edit control, we paint the edit ourselves
1500 if (dropdown_style == ComboBoxStyle.DropDownList) {
1501 DrawItemState state = DrawItemState.None;
1502 Rectangle item_rect = text_area;
1503 item_rect.X += border;
1504 item_rect.Y += border;
1505 item_rect.Width -= (button_area.Width + 2 * border);
1506 item_rect.Height -= 2 * border;
1509 state = DrawItemState.Selected;
1510 state |= DrawItemState.Focus;
1513 state |= DrawItemState.ComboBoxEdit;
1514 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1517 if (show_dropdown_button) {
1518 ButtonState current_state;
1520 current_state = button_state;
1522 current_state = ButtonState.Inactive;
1524 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1525 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1528 theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1530 theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
1535 internal bool DropDownButtonEntered {
1536 get { return drop_down_button_entered; }
1538 if (drop_down_button_entered == value)
1540 drop_down_button_entered = value;
1541 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1542 Invalidate (button_area);
1546 internal void DropDownListBox ()
1548 DropDownButtonEntered = false;
1550 if (DropDownStyle == ComboBoxStyle.Simple)
1553 if (listbox_ctrl == null)
1554 CreateComboListBox ();
1556 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1558 FindMatchOrSetIndex(SelectedIndex);
1560 if (listbox_ctrl.ShowWindow ())
1561 dropped_down = true;
1563 button_state = ButtonState.Pushed;
1564 if (dropdown_style == ComboBoxStyle.DropDownList)
1565 Invalidate (text_area);
1568 internal void DropDownListBoxFinished ()
1570 if (DropDownStyle == ComboBoxStyle.Simple)
1573 FindMatchOrSetIndex (SelectedIndex);
1574 button_state = ButtonState.Normal;
1575 Invalidate (button_area);
1576 dropped_down = false;
1578 OnDropDownClosed (EventArgs.Empty);
1581 * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1582 * this causes the popup to appear under the main form. This is horrible but necessary
1585 // If the user opens a new form in an event, it will close our dropdown,
1586 // so we need a null check here
1587 if (listbox_ctrl != null) {
1588 listbox_ctrl.Dispose ();
1589 listbox_ctrl = null;
1593 private int FindStringCaseInsensitive (string search)
1595 if (search.Length == 0) {
1599 for (int i = 0; i < Items.Count; i++)
1601 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1608 // Search in the list for the substring, starting the search at the list
1609 // position specified, the search wraps thus covering all the list.
1610 internal int FindStringCaseInsensitive (string search, int start_index)
1612 if (search.Length == 0) {
1615 // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1616 if (start_index < 0 || start_index > Items.Count)
1617 throw new ArgumentOutOfRangeException("start_index");
1619 for (int i = 0; i < Items.Count; i++) {
1620 int index = (i + start_index) % Items.Count;
1621 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1628 internal override bool IsInputCharInternal (char charCode)
1633 internal override ContextMenu ContextMenuInternal {
1635 return base.ContextMenuInternal;
1638 base.ContextMenuInternal = value;
1639 if (textbox_ctrl != null) {
1640 textbox_ctrl.ContextMenu = value;
1645 internal void RestoreContextMenu ()
1647 textbox_ctrl.RestoreContextMenu ();
1650 private void OnKeyDownCB(object sender, KeyEventArgs e)
1652 if (Items.Count == 0)
1659 FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1662 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1663 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1667 if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1670 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1673 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1674 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1678 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1682 SelectedIndex = Math.Max (SelectedIndex - offset, 0);
1685 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1686 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1690 if (SelectedIndex == -1) {
1692 if (dropdown_style != ComboBoxStyle.Simple)
1696 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1700 SelectedIndex = Math.Min (SelectedIndex + offset, Items.Count - 1);
1703 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1704 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1709 DropDownListBoxFinished ();
1713 if (dropdown_style == ComboBoxStyle.DropDownList) {
1717 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1718 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1723 if (dropdown_style == ComboBoxStyle.DropDownList) {
1724 SelectedIndex = Items.Count - 1;
1727 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1728 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1737 // If no item is currently selected, and an item is found matching the text
1738 // in the textbox, then selected that item. Otherwise the item at the given
1739 // index is selected.
1740 private void FindMatchOrSetIndex(int index)
1743 if (SelectedIndex == -1 && Text.Length != 0)
1744 match = FindStringCaseInsensitive(Text);
1746 SelectedIndex = match;
1748 SelectedIndex = index;
1751 void OnMouseDownCB (object sender, MouseEventArgs e)
1754 if (DropDownStyle == ComboBoxStyle.DropDownList)
1755 area = ClientRectangle;
1759 if (area.Contains (e.X, e.Y)) {
1760 if (Items.Count > 0)
1763 button_state = ButtonState.Pushed;
1764 OnDropDown (EventArgs.Empty);
1767 Invalidate (button_area);
1773 void OnMouseEnter (object sender, EventArgs e)
1775 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1779 void OnMouseLeave (object sender, EventArgs e)
1781 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1782 drop_down_button_entered = false;
1785 if (show_dropdown_button)
1786 DropDownButtonEntered = false;
1790 void OnMouseMoveCB (object sender, MouseEventArgs e)
1792 if (show_dropdown_button && !dropped_down)
1793 DropDownButtonEntered = button_area.Contains (e.Location);
1795 if (DropDownStyle == ComboBoxStyle.Simple)
1798 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1799 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1800 if (listbox_ctrl.ClientRectangle.Contains (location))
1801 listbox_ctrl.Capture = true;
1805 void OnMouseUpCB (object sender, MouseEventArgs e)
1809 button_state = ButtonState.Normal;
1810 Invalidate (button_area);
1812 OnClick (EventArgs.Empty);
1815 listbox_ctrl.Capture = true;
1818 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1820 if (Items.Count == 0)
1823 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1824 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1825 listbox_ctrl.Scroll (-lines);
1827 int lines = me.Delta / 120;
1828 int index = SelectedIndex - lines;
1831 else if (index >= Items.Count)
1832 index = Items.Count - 1;
1833 SelectedIndex = index;
1837 internal override void OnPaintInternal (PaintEventArgs pevent)
1839 if (suspend_ctrlupdate)
1842 Draw (ClientRectangle, pevent.Graphics);
1845 private void OnTextBoxClick (object sender, EventArgs e)
1850 private void OnTextChangedEdit (object sender, EventArgs e)
1852 if (process_textchanged_event == false)
1855 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1858 // Setting base.Text below will raise this event
1859 // if we found something
1860 OnTextChanged (EventArgs.Empty);
1864 // TODO: THIS IS BROKEN-ISH
1865 // I don't think we should hilight, and setting the top item does weirdness
1866 // when there is no scrollbar
1868 if (listbox_ctrl != null) {
1869 listbox_ctrl.SetTopItem (item);
1870 listbox_ctrl.HighlightedIndex = item;
1873 base.Text = textbox_ctrl.Text;
1876 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1878 selected_index = -1;
1881 internal void SetControlText (string s, bool suppressTextChanged)
1883 if (suppressTextChanged)
1884 process_textchanged_event = false;
1886 textbox_ctrl.Text = s;
1887 textbox_ctrl.SelectAll ();
1888 process_textchanged_event = true;
1891 void UpdateComboBoxBounds ()
1893 if (requested_height == -1)
1896 // Save the requested height since set bounds can destroy it
1897 int save_height = requested_height;
1898 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1899 BoundsSpecified.Height);
1900 requested_height = save_height;
1903 int SnapHeight (int height)
1905 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1906 if (IntegralHeight) {
1907 int border = ThemeEngine.Current.Border3DSize.Height;
1908 int lb_height = (height - PreferredHeight - 2) - border * 2;
1909 if (lb_height > ItemHeight) {
1910 int partial = (lb_height) % ItemHeight;
1912 } else if (lb_height < ItemHeight)
1913 height = PreferredHeight;
1916 height = PreferredHeight;
1921 private void UpdatedItems ()
1923 if (listbox_ctrl != null) {
1924 listbox_ctrl.UpdateLastVisibleItem ();
1925 listbox_ctrl.CalcListBoxArea ();
1926 listbox_ctrl.Refresh ();
1930 #endregion Private Methods
1932 [ListBindableAttribute (false)]
1933 public class ObjectCollection : IList, ICollection, IEnumerable
1936 private ComboBox owner;
1937 internal ArrayList object_items = new ArrayList ();
1939 #region UIA Framework Events
1943 // We are using Reflection to add/remove internal events.
1944 // Class ListProvider uses the events.
1946 //Event used to generate UIA StructureChangedEvent
1947 static object UIACollectionChangedEvent = new object ();
1949 internal event CollectionChangeEventHandler UIACollectionChanged {
1950 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1951 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1954 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1956 CollectionChangeEventHandler eh
1957 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1963 #endregion UIA Framework Events
1965 public ObjectCollection (ComboBox owner)
1970 #region Public Properties
1972 get { return object_items.Count; }
1975 public bool IsReadOnly {
1976 get { return false; }
1980 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1981 public virtual object this [int index] {
1983 if (index < 0 || index >= Count)
1984 throw new ArgumentOutOfRangeException ("index");
1986 return object_items[index];
1989 if (index < 0 || index >= Count)
1990 throw new ArgumentOutOfRangeException ("index");
1992 throw new ArgumentNullException ("value");
1995 //UIA Framework event: Item Removed
1996 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
1999 object_items[index] = value;
2002 //UIA Framework event: Item Added
2003 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
2006 if (owner.listbox_ctrl != null)
2007 owner.listbox_ctrl.InvalidateItem (index);
2008 if (index == owner.SelectedIndex) {
2009 if (owner.textbox_ctrl == null)
2012 owner.textbox_ctrl.SelectedText = value.ToString ();
2017 bool ICollection.IsSynchronized {
2018 get { return false; }
2021 object ICollection.SyncRoot {
2022 get { return this; }
2025 bool IList.IsFixedSize {
2026 get { return false; }
2029 #endregion Public Properties
2031 #region Public Methods
2032 public int Add (object item)
2036 idx = AddItem (item);
2037 owner.UpdatedItems ();
2041 public void AddRange (object[] items)
2044 throw new ArgumentNullException ("items");
2046 foreach (object mi in items)
2049 owner.UpdatedItems ();
2052 public void Clear ()
2054 owner.selected_index = -1;
2055 object_items.Clear ();
2056 owner.UpdatedItems ();
2060 //UIA Framework event: Items list cleared
2061 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, -1));
2065 public bool Contains (object value)
2068 throw new ArgumentNullException ("value");
2070 return object_items.Contains (value);
2074 public void CopyTo (object [] destination, int arrayIndex)
2076 object_items.CopyTo (destination, arrayIndex);
2079 void ICollection.CopyTo (Array destination, int index)
2081 object_items.CopyTo (destination, index);
2084 public void CopyTo (object [] dest, int arrayIndex)
2086 object_items.CopyTo (dest, arrayIndex);
2089 void ICollection.CopyTo (Array dest, int index)
2091 object_items.CopyTo (dest, index);
2095 public IEnumerator GetEnumerator ()
2097 return object_items.GetEnumerator ();
2100 int IList.Add (object item)
2105 public int IndexOf (object value)
2108 throw new ArgumentNullException ("value");
2110 return object_items.IndexOf (value);
2113 public void Insert (int index, object item)
2115 if (index < 0 || index > Count)
2116 throw new ArgumentOutOfRangeException ("index");
2118 throw new ArgumentNullException ("item");
2120 owner.BeginUpdate ();
2125 object_items.Insert (index, item);
2127 //UIA Framework event: Item added
2128 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
2132 owner.EndUpdate (); // Calls UpdatedItems
2135 public void Remove (object value)
2140 if (IndexOf (value) == owner.SelectedIndex)
2141 owner.SelectedIndex = -1;
2143 RemoveAt (IndexOf (value));
2146 public void RemoveAt (int index)
2148 if (index < 0 || index >= Count)
2149 throw new ArgumentOutOfRangeException ("index");
2151 if (index == owner.SelectedIndex)
2152 owner.SelectedIndex = -1;
2154 object_items.RemoveAt (index);
2155 owner.UpdatedItems ();
2158 //UIA Framework event: Item removed
2159 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
2162 #endregion Public Methods
2164 #region Private Methods
2165 private int AddItem (object item)
2168 throw new ArgumentNullException ("item");
2172 foreach (object o in object_items) {
2173 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2174 object_items.Insert (index, item);
2176 // If we added the new item before the selectedindex
2177 // bump the selectedindex by one, behavior differs if
2178 // Handle has not been created.
2179 if (index <= owner.selected_index && owner.IsHandleCreated)
2180 owner.selected_index++;
2183 //UIA Framework event: Item added
2184 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
2192 object_items.Add (item);
2195 //UIA Framework event: Item added
2196 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, object_items.Count - 1));
2199 return object_items.Count - 1;
2202 internal void AddRange (IList items)
2204 foreach (object mi in items)
2207 owner.UpdatedItems ();
2210 internal void Sort ()
2212 object_items.Sort ();
2215 #endregion Private Methods
2218 internal class ComboTextBox : TextBox {
2220 private ComboBox owner;
2222 public ComboTextBox (ComboBox owner)
2225 ShowSelection = false;
2226 HideSelection = false;
2229 internal void SetSelectable (bool selectable)
2231 SetStyle (ControlStyles.Selectable, selectable);
2234 internal void ActivateCaret (bool active)
2237 document.CaretHasFocus ();
2239 document.CaretLostFocus ();
2243 internal override void OnTextUpdate ()
2245 base.OnTextUpdate ();
2246 owner.OnTextUpdate (EventArgs.Empty);
2250 protected override void OnGotFocus (EventArgs e)
2252 owner.Select (false, true);
2255 protected override void OnLostFocus (EventArgs e)
2257 owner.Select (false, true);
2260 public override bool Focused {
2262 return owner.Focused;
2266 internal override bool ActivateOnShow { get { return false; } }
2269 internal class ComboListBox : Control
2271 private ComboBox owner;
2272 private VScrollBarLB vscrollbar_ctrl;
2273 private int top_item; /* First item that we show the in the current page */
2274 private int last_item; /* Last visible item */
2275 internal int page_size; /* Number of listbox items per page */
2276 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2278 internal enum ItemNavigation
2288 class VScrollBarLB : VScrollBar
2290 public VScrollBarLB ()
2294 internal override bool InternalCapture {
2295 get { return Capture; }
2299 public void FireMouseDown (MouseEventArgs e)
2304 e = TranslateEvent (e);
2308 public void FireMouseUp (MouseEventArgs e)
2313 e = TranslateEvent (e);
2317 public void FireMouseMove (MouseEventArgs e)
2322 e = TranslateEvent (e);
2326 MouseEventArgs TranslateEvent (MouseEventArgs e)
2328 Point loc = PointToClient (Control.MousePosition);
2329 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2333 public ComboListBox (ComboBox owner)
2340 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2342 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2343 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2345 this.is_visible = false;
2347 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2348 InternalBorderStyle = BorderStyle.Fixed3D;
2350 InternalBorderStyle = BorderStyle.FixedSingle;
2353 protected override CreateParams CreateParams
2356 CreateParams cp = base.CreateParams;
2357 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2360 cp.Style ^= (int)WindowStyles.WS_CHILD;
2361 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2362 cp.Style |= (int)WindowStyles.WS_POPUP;
2363 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2368 internal override bool InternalCapture {
2377 internal override bool ActivateOnShow { get { return false; } }
2378 #region Private Methods
2380 // Calcs the listbox area
2381 internal void CalcListBoxArea ()
2384 bool show_scrollbar = false;
2386 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2387 Rectangle area = owner.listbox_area;
2389 height = area.Height;
2391 // No calculation needed
2392 if (height <= 0 || width <= 0)
2395 else { // DropDown or DropDownList
2397 width = owner.DropDownWidth;
2398 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2400 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2402 for (int i = 0; i < count; i++) {
2403 height += owner.GetItemHeight (i);
2408 height = owner.DropDownHeight;
2410 height = owner.ItemHeight * count;
2415 page_size = Math.Max (height / owner.ItemHeight, 1);
2417 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2418 if ((dropdown_style != ComboBoxStyle.Simple && owner.Items.Count <= owner.MaxDropDownItems)
2419 || (dropdown_style == ComboBoxStyle.Simple && owner.Items.Count * owner.ItemHeight < height)) {
2421 if (vscrollbar_ctrl != null)
2422 vscrollbar_ctrl.Visible = false;
2423 if (dropdown_style != ComboBoxStyle.Simple)
2424 height = owner.ItemHeight * owner.items.Count;
2426 /* Need vertical scrollbar */
2427 if (vscrollbar_ctrl == null) {
2428 vscrollbar_ctrl = new VScrollBarLB ();
2429 vscrollbar_ctrl.Minimum = 0;
2430 vscrollbar_ctrl.SmallChange = 1;
2431 vscrollbar_ctrl.LargeChange = 1;
2432 vscrollbar_ctrl.Maximum = 0;
2433 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2434 Controls.AddImplicit (vscrollbar_ctrl);
2437 vscrollbar_ctrl.Dock = DockStyle.Right;
2439 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2441 int large = page_size;
2443 int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
2447 vscrollbar_ctrl.LargeChange = large;
2448 show_scrollbar = vscrollbar_ctrl.Visible = true;
2450 int hli = HighlightedIndex;
2452 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2453 vscrollbar_ctrl.Value = hli;
2457 Size = new Size (width, height);
2458 textarea_drawable = ClientRectangle;
2459 textarea_drawable.Width = width;
2460 textarea_drawable.Height = height;
2462 if (vscrollbar_ctrl != null && show_scrollbar)
2463 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2465 last_item = LastVisibleItem ();
2468 private void Draw (Rectangle clip, Graphics dc)
2470 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2472 if (owner.Items.Count > 0) {
2474 for (int i = top_item; i <= last_item; i++) {
2475 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2477 if (!clip.IntersectsWith (item_rect))
2480 DrawItemState state = DrawItemState.None;
2482 if (i == HighlightedIndex) {
2483 state |= DrawItemState.Selected;
2485 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2486 state |= DrawItemState.Focus;
2490 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2491 i, state, owner.ForeColor, owner.BackColor));
2496 int highlighted_index = -1;
2498 public int HighlightedIndex {
2499 get { return highlighted_index; }
2501 if (highlighted_index == value)
2504 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2505 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2506 highlighted_index = value;
2507 if (highlighted_index != -1)
2508 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2512 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2514 if (index < 0 || index >= owner.Items.Count)
2515 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2517 Rectangle item_rect = new Rectangle ();
2518 int height = owner.GetItemHeight (index);
2521 item_rect.Width = textarea_drawable.Width;
2522 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2524 for (int i = top_index; i < index; i++)
2525 item_rect.Y += owner.GetItemHeight (i);
2527 item_rect.Y = height * (index - top_index);
2529 item_rect.Height = height;
2533 public void HideWindow ()
2535 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2540 owner.DropDownListBoxFinished ();
2543 private int IndexFromPointDisplayRectangle (int x, int y)
2545 for (int i = top_item; i <= last_item; i++) {
2546 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2553 public void InvalidateItem (int index)
2556 Invalidate (GetItemDisplayRectangle (index, top_item));
2559 public int LastVisibleItem ()
2561 Rectangle item_rect;
2562 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2565 for (i = top_item; i < owner.Items.Count; i++) {
2566 item_rect = GetItemDisplayRectangle (i, top_item);
2567 if (item_rect.Y + item_rect.Height > top_y) {
2574 public void SetTopItem (int item)
2576 if (top_item == item)
2579 UpdateLastVisibleItem ();
2583 public int FirstVisibleItem ()
2588 bool scrollbar_grabbed = false;
2592 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2595 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2599 protected override void OnMouseDown (MouseEventArgs e)
2602 vscrollbar_ctrl.FireMouseDown (e);
2603 scrollbar_grabbed = true;
2607 protected override void OnMouseMove (MouseEventArgs e)
2609 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2612 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2613 vscrollbar_ctrl.FireMouseMove (e);
2617 Point pt = PointToClient (Control.MousePosition);
2618 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2621 HighlightedIndex = index;
2624 protected override void OnMouseUp (MouseEventArgs e)
2626 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2628 if (scrollbar_grabbed) {
2629 vscrollbar_ctrl.FireMouseUp (e);
2630 scrollbar_grabbed = false;
2632 HighlightedIndex = index;
2641 owner.SelectedIndex = index;
2642 owner.OnSelectionChangeCommitted (new EventArgs ());
2646 internal override void OnPaintInternal (PaintEventArgs pevent)
2648 Draw (pevent.ClipRectangle,pevent.Graphics);
2651 public bool ShowWindow ()
2653 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2656 HighlightedIndex = owner.SelectedIndex;
2662 owner.OnDropDown (EventArgs.Empty);
2666 public void UpdateLastVisibleItem ()
2668 last_item = LastVisibleItem ();
2671 public void Scroll (int delta)
2673 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2676 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2678 int val = vscrollbar_ctrl.Value + delta;
2681 else if (val < vscrollbar_ctrl.Minimum)
2682 val = vscrollbar_ctrl.Minimum;
2683 vscrollbar_ctrl.Value = val;
2686 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2688 if (owner.Items.Count == 0)
2691 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2696 private void VerticalScrollEvent (object sender, EventArgs e)
2698 if (top_item == vscrollbar_ctrl.Value)
2701 top_item = vscrollbar_ctrl.Value;
2702 UpdateLastVisibleItem ();
2706 protected override void WndProc(ref Message m) {
2707 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2708 owner.Select (false, true);
2710 base.WndProc (ref m);
2713 #endregion Private Methods