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>
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Reflection;
33 using System.ComponentModel.Design;
34 using System.ComponentModel.Design.Serialization;
35 using System.Runtime.InteropServices;
38 namespace System.Windows.Forms
41 [DefaultProperty("Items")]
42 [DefaultEvent("SelectedIndexChanged")]
43 [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 public class ComboBox : ListControl
46 private DrawMode draw_mode = DrawMode.Normal;
47 private ComboBoxStyle dropdown_style = (ComboBoxStyle)(-1);
48 private int dropdown_width = -1;
49 private int selected_index = -1;
50 internal ObjectCollection items = null;
51 private bool suspend_ctrlupdate;
52 private int maxdrop_items = 8;
53 private bool integral_height = true;
55 private int max_length;
56 private ComboListBox listbox_ctrl;
57 private ComboTextBox textbox_ctrl;
58 private bool process_textchanged_event = true;
59 private bool item_height_specified = false;
60 private int item_height;
61 private int requested_height = -1;
62 private Hashtable item_heights;
63 private bool show_dropdown_button = false;
64 private ButtonState button_state = ButtonState.Normal;
65 private bool dropped_down;
66 private Rectangle text_area;
67 private Rectangle button_area;
68 private Rectangle listbox_area;
69 private const int button_width = 16;
72 public class ChildAccessibleObject : AccessibleObject {
73 private ComboBox owner;
74 private IntPtr handle;
76 public ChildAccessibleObject (ComboBox owner, IntPtr handle) {
81 public override string Name {
90 items = new ObjectCollection (this);
91 DropDownStyle = ComboBoxStyle.DropDown;
92 item_height = FontHeight + 2;
93 BackColor = ThemeEngine.Current.ColorWindow;
94 border_style = BorderStyle.None;
97 MouseDown += new MouseEventHandler (OnMouseDownCB);
98 MouseUp += new MouseEventHandler (OnMouseUpCB);
99 MouseMove += new MouseEventHandler (OnMouseMoveCB);
100 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
101 KeyDown +=new KeyEventHandler(OnKeyDownCB);
107 [EditorBrowsable (EditorBrowsableState.Never)]
108 public new event EventHandler BackgroundImageChanged {
109 add { base.BackgroundImageChanged += value; }
110 remove { base.BackgroundImageChanged -= value; }
113 public event DrawItemEventHandler DrawItem;
114 public event EventHandler DropDown;
115 public event EventHandler DropDownStyleChanged;
116 public event MeasureItemEventHandler MeasureItem;
119 [EditorBrowsable (EditorBrowsableState.Never)]
120 public new event PaintEventHandler Paint {
121 add { base.Paint += value; }
122 remove { base.Paint -= value; }
125 public event EventHandler SelectedIndexChanged;
126 public event EventHandler SelectionChangeCommitted;
129 #region Public Properties
130 public override Color BackColor {
131 get { return base.BackColor; }
133 if (base.BackColor == value)
136 base.BackColor = value;
142 [EditorBrowsable (EditorBrowsableState.Never)]
143 public override Image BackgroundImage {
144 get { return base.BackgroundImage; }
146 if (base.BackgroundImage == value)
149 base.BackgroundImage = value;
154 protected override CreateParams CreateParams {
155 get { return base.CreateParams;}
158 protected override Size DefaultSize {
159 get { return new Size (121, 21); }
162 [RefreshProperties(RefreshProperties.Repaint)]
163 [DefaultValue (DrawMode.Normal)]
164 public DrawMode DrawMode {
165 get { return draw_mode; }
168 if (!Enum.IsDefined (typeof (DrawMode), value))
169 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
171 if (draw_mode == value)
174 if (draw_mode == DrawMode.OwnerDrawVariable)
177 if (draw_mode == DrawMode.OwnerDrawVariable)
178 item_heights = new Hashtable ();
183 [DefaultValue (ComboBoxStyle.DropDown)]
184 [RefreshProperties(RefreshProperties.Repaint)]
185 public ComboBoxStyle DropDownStyle {
186 get { return dropdown_style; }
190 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
191 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
193 if (dropdown_style == value)
196 if (dropdown_style == ComboBoxStyle.Simple) {
197 if (listbox_ctrl != null) {
198 Controls.RemoveImplicit (listbox_ctrl);
199 listbox_ctrl.Dispose ();
204 dropdown_style = value;
206 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
207 Controls.RemoveImplicit (textbox_ctrl);
208 textbox_ctrl.Dispose ();
212 if (dropdown_style == ComboBoxStyle.Simple) {
213 show_dropdown_button = false;
215 CreateComboListBox ();
218 Controls.AddImplicit (listbox_ctrl);
220 show_dropdown_button = true;
221 button_state = ButtonState.Normal;
224 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
225 textbox_ctrl = new ComboTextBox (this);
226 object selected_item = SelectedItem;
227 if (selected_item != null)
228 textbox_ctrl.Text = GetItemText (selected_item);
229 textbox_ctrl.BorderStyle = BorderStyle.None;
230 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
231 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
233 if (IsHandleCreated == true) {
234 Controls.AddImplicit (textbox_ctrl);
238 OnDropDownStyleChanged (EventArgs.Empty);
246 public int DropDownWidth {
248 if (dropdown_width == -1)
251 return dropdown_width;
254 if (dropdown_width == value)
258 throw new ArgumentException ("The DropDownWidth value is less than one");
260 dropdown_width = value;
265 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
266 public bool DroppedDown {
268 if (dropdown_style == ComboBoxStyle.Simple)
274 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
280 listbox_ctrl.Hide ();
284 public override bool Focused {
285 get { return base.Focused; }
288 public override Color ForeColor {
289 get { return base.ForeColor; }
291 if (base.ForeColor == value)
294 base.ForeColor = value;
299 [DefaultValue (true)]
301 public bool IntegralHeight {
302 get { return integral_height; }
304 if (integral_height == value)
307 integral_height = value;
314 public int ItemHeight {
316 if (item_height == -1) {
317 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
318 item_height = (int) sz.Height;
324 throw new ArgumentException ("The item height value is less than zero");
326 item_height_specified = true;
335 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
337 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
338 public ComboBox.ObjectCollection Items {
339 get { return items; }
344 public int MaxDropDownItems {
345 get { return maxdrop_items; }
347 if (maxdrop_items == value)
350 maxdrop_items = value;
356 public int MaxLength {
357 get { return max_length; }
359 if (max_length == value)
364 if (dropdown_style != ComboBoxStyle.DropDownList) {
370 textbox_ctrl.MaxLength = value;
375 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
377 public int PreferredHeight {
379 return ItemHeight + 5;
384 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
385 public override int SelectedIndex {
386 get { return selected_index; }
388 if (value <= -2 || value >= Items.Count)
389 throw new ArgumentOutOfRangeException ("Index of out range");
390 selected_index = value;
392 if (dropdown_style != ComboBoxStyle.DropDownList) {
396 SetControlText (GetItemText (Items [value]));
399 OnSelectedValueChanged (new EventArgs ());
400 OnSelectedIndexChanged (new EventArgs ());
401 OnSelectedItemChanged (new EventArgs ());
402 if (DropDownStyle == ComboBoxStyle.DropDownList)
405 if (listbox_ctrl != null)
406 listbox_ctrl.HighlightedIndex = value;
411 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
413 public object SelectedItem {
414 get { return selected_index == -1 ? null : Items [selected_index]; }
416 object item = selected_index == -1 ? null : Items [selected_index];
420 SelectedIndex = Items.IndexOf (value);
425 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
426 public string SelectedText {
428 if (dropdown_style == ComboBoxStyle.DropDownList)
431 return textbox_ctrl.SelectedText;
434 if (dropdown_style == ComboBoxStyle.DropDownList)
437 textbox_ctrl.SelectedText = value;
442 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
443 public int SelectionLength {
445 if (dropdown_style == ComboBoxStyle.DropDownList)
448 return textbox_ctrl.SelectionLength;
451 if (dropdown_style == ComboBoxStyle.DropDownList)
454 if (textbox_ctrl.SelectionLength == value)
457 textbox_ctrl.SelectionLength = value;
462 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
463 public int SelectionStart {
465 if (dropdown_style == ComboBoxStyle.DropDownList)
468 return textbox_ctrl.SelectionStart;
471 if (dropdown_style == ComboBoxStyle.DropDownList)
474 if (textbox_ctrl.SelectionStart == value)
477 textbox_ctrl.SelectionStart = value;
481 [DefaultValue (false)]
483 get { return sorted; }
500 public override string Text {
502 if (dropdown_style != ComboBoxStyle.DropDownList) {
503 if (textbox_ctrl != null) {
504 return textbox_ctrl.Text;
508 if (SelectedItem != null)
509 return GetItemText (SelectedItem);
519 int index = FindString (value);
522 SelectedIndex = index;
526 if (dropdown_style != ComboBoxStyle.DropDownList)
527 textbox_ctrl.Text = GetItemText (value);
531 #endregion Public Properties
533 #region Public Methods
534 protected virtual void AddItemsCore (object[] value)
539 public void BeginUpdate ()
541 suspend_ctrlupdate = true;
544 protected override void Dispose (bool disposing)
547 if (listbox_ctrl != null) {
548 listbox_ctrl.Dispose ();
549 Controls.RemoveImplicit (listbox_ctrl);
553 if (textbox_ctrl != null) {
554 Controls.RemoveImplicit (textbox_ctrl);
555 textbox_ctrl.Dispose ();
560 base.Dispose (disposing);
563 public void EndUpdate ()
565 suspend_ctrlupdate = false;
570 public int FindString (string s)
572 return FindString (s, -1);
575 public int FindString (string s, int startIndex)
577 if (Items.Count == 0)
578 return -1; // No exception throwing if empty
580 if (startIndex < -1 || startIndex >= Items.Count - 1)
581 throw new ArgumentOutOfRangeException ("Index of out range");
584 for (int i = startIndex; i < Items.Count; i++) {
585 if ((GetItemText (Items[i])).StartsWith (s))
592 public int FindStringExact (string s)
594 return FindStringExact (s, -1);
597 public int FindStringExact (string s, int startIndex)
599 if (Items.Count == 0)
600 return -1; // No exception throwing if empty
602 if (startIndex < -1 || startIndex >= Items.Count - 1)
603 throw new ArgumentOutOfRangeException ("Index of out range");
606 for (int i = startIndex; i < Items.Count; i++) {
607 if ((GetItemText (Items[i])).Equals (s))
614 public int GetItemHeight (int index)
616 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
618 if (index < 0 || index >= Items.Count )
619 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
621 object item = Items [index];
622 if (item_heights.Contains (item))
623 return (int) item_heights [item];
625 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
626 OnMeasureItem (args);
627 item_heights [item] = args.ItemHeight;
628 return args.ItemHeight;
634 protected override bool IsInputKey (Keys keyData)
650 protected override void OnBackColorChanged (EventArgs e)
652 base.OnBackColorChanged (e);
655 protected override void OnDataSourceChanged (EventArgs e)
657 base.OnDataSourceChanged (e);
660 if (DataSource == null || DataManager == null) {
664 SelectedIndex = DataManager.Position;
668 protected override void OnDisplayMemberChanged (EventArgs e)
670 base.OnDisplayMemberChanged (e);
672 if (DataManager == null || !IsHandleCreated)
676 SelectedIndex = DataManager.Position;
679 protected virtual void OnDrawItem (DrawItemEventArgs e)
682 case DrawMode.OwnerDrawFixed:
683 case DrawMode.OwnerDrawVariable:
684 if (DrawItem != null)
688 ThemeEngine.Current.DrawComboBoxItem (this, e);
693 protected virtual void OnDropDown (EventArgs e)
695 if (DropDown != null)
699 protected virtual void OnDropDownStyleChanged (EventArgs e)
701 if (DropDownStyleChanged != null)
702 DropDownStyleChanged (this, e);
705 protected override void OnFontChanged (EventArgs e)
707 base.OnFontChanged (e);
709 if (textbox_ctrl != null)
710 textbox_ctrl.Font = Font;
712 if (!item_height_specified) {
713 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
714 item_height = (int) sz.Height;
723 protected override void OnForeColorChanged (EventArgs e)
725 base.OnForeColorChanged (e);
728 [EditorBrowsable(EditorBrowsableState.Advanced)]
729 protected override void OnGotFocus (EventArgs e)
731 if (dropdown_style == ComboBoxStyle.DropDownList) {
732 // We draw DDL styles manually, so they require a
733 // refresh to have their selection drawn
737 if (textbox_ctrl != null) {
738 textbox_ctrl.SetSelectable (false);
739 textbox_ctrl.ActivateCaret (true);
740 textbox_ctrl.ShowSelection = true;
741 textbox_ctrl.SelectAll ();
747 [EditorBrowsable(EditorBrowsableState.Advanced)]
748 protected override void OnLostFocus (EventArgs e)
750 if (dropdown_style == ComboBoxStyle.DropDownList) {
751 // We draw DDL styles manually, so they require a
752 // refresh to have their selection drawn
756 if (listbox_ctrl != null && dropped_down) {
757 listbox_ctrl.HideWindow ();
760 if (textbox_ctrl != null) {
761 textbox_ctrl.SetSelectable (true);
762 textbox_ctrl.ActivateCaret (false);
763 textbox_ctrl.ShowSelection = false;
766 base.OnLostFocus (e);
769 protected override void OnHandleCreated (EventArgs e)
771 base.OnHandleCreated (e);
773 if (listbox_ctrl != null)
774 Controls.AddImplicit (listbox_ctrl);
776 if (textbox_ctrl != null)
777 Controls.AddImplicit (textbox_ctrl);
782 protected override void OnHandleDestroyed (EventArgs e)
784 base.OnHandleDestroyed (e);
787 protected override void OnKeyPress (KeyPressEventArgs e)
789 // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
791 // SelectedIndex = index;
796 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
798 if (MeasureItem != null)
799 MeasureItem (this, e);
802 protected override void OnParentBackColorChanged (EventArgs e)
804 base.OnParentBackColorChanged (e);
807 protected override void OnResize (EventArgs e)
810 if (listbox_ctrl != null)
811 listbox_ctrl.CalcListBoxArea ();
814 protected override void OnSelectedIndexChanged (EventArgs e)
816 base.OnSelectedIndexChanged (e);
818 if (SelectedIndexChanged != null)
819 SelectedIndexChanged (this, e);
822 protected virtual void OnSelectedItemChanged (EventArgs e)
827 protected override void OnSelectedValueChanged (EventArgs e)
829 base.OnSelectedValueChanged (e);
832 protected virtual void OnSelectionChangeCommitted (EventArgs e)
834 if (SelectionChangeCommitted != null)
835 SelectionChangeCommitted (this, e);
838 protected override void RefreshItem (int index)
840 if (index < 0 || index >= Items.Count)
841 throw new ArgumentOutOfRangeException ("Index of out range");
843 if (draw_mode == DrawMode.OwnerDrawVariable)
844 item_heights.Remove (Items [index]);
847 public void Select (int start, int length)
850 throw new ArgumentException ("Start cannot be less than zero");
853 throw new ArgumentException ("length cannot be less than zero");
855 if (dropdown_style == ComboBoxStyle.DropDownList)
858 textbox_ctrl.Select (start, length);
861 public void SelectAll ()
863 if (dropdown_style == ComboBoxStyle.DropDownList)
866 if (textbox_ctrl != null) {
867 textbox_ctrl.ShowSelection = true;
868 textbox_ctrl.SelectAll ();
872 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
874 if ((specified & BoundsSpecified.Height) != 0) {
875 requested_height = height;
877 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
878 if (IntegralHeight) {
879 int border = ThemeEngine.Current.Border3DSize.Height;
880 int lb_height = height - PreferredHeight - 2;
881 if (lb_height - 2 * border > ItemHeight) {
882 int partial = (lb_height - 2 * border) % ItemHeight;
885 height = PreferredHeight;
888 height = PreferredHeight;
891 base.SetBoundsCore (x, y, width, height, specified);
894 protected override void SetItemCore (int index, object value)
896 if (index < 0 || index >= Items.Count)
899 Items[index] = value;
902 protected override void SetItemsCore (IList value)
907 Items.AddRange (value);
913 public override string ToString ()
915 return base.ToString () + ", Items.Count:" + Items.Count;
918 protected override void WndProc (ref Message m)
920 switch ((Msg) m.Msg) {
923 Keys keys = (Keys) m.WParam.ToInt32 ();
924 if (keys == Keys.Up || keys == Keys.Down)
926 goto case Msg.WM_CHAR;
928 if (textbox_ctrl != null)
929 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
931 case Msg.WM_MOUSE_LEAVE:
932 Point location = PointToClient (Control.MousePosition);
933 if (ClientRectangle.Contains (location))
939 base.WndProc (ref m);
942 #endregion Public Methods
944 #region Private Methods
946 internal override bool InternalCapture {
947 get { return Capture; }
953 int border = ThemeEngine.Current.Border3DSize.Width;
955 text_area = ClientRectangle;
956 text_area.Height = PreferredHeight;
958 listbox_area = ClientRectangle;
959 listbox_area.Y = text_area.Bottom + 3;
960 listbox_area.Height -= (text_area.Height + 2);
962 Rectangle prev_button_area = button_area;
964 if (DropDownStyle == ComboBoxStyle.Simple)
965 button_area = Rectangle.Empty;
967 button_area = text_area;
968 button_area.X = text_area.Right - button_width - border;
969 button_area.Y = text_area.Y + border;
970 button_area.Width = button_width;
971 button_area.Height = text_area.Height - 2 * border;
974 if (button_area != prev_button_area) {
975 prev_button_area.Y -= border;
976 prev_button_area.Width += border;
977 prev_button_area.Height += 2 * border;
978 Invalidate (prev_button_area);
979 Invalidate (button_area);
982 if (textbox_ctrl != null) {
983 textbox_ctrl.Location = new Point (text_area.X + border, text_area.Y + border);
984 textbox_ctrl.Width = text_area.Width - button_area.Width - border * 2;
985 textbox_ctrl.Height = text_area.Height - border * 2;
988 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
989 listbox_ctrl.Location = listbox_area.Location;
990 listbox_ctrl.CalcListBoxArea ();
994 private void CreateComboListBox ()
996 listbox_ctrl = new ComboListBox (this);
997 listbox_ctrl.HighlightedIndex = SelectedIndex;
1000 internal void Draw (Rectangle clip, Graphics dc)
1002 Theme theme = ThemeEngine.Current;
1004 if (DropDownStyle == ComboBoxStyle.Simple)
1005 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1007 if (clip.IntersectsWith (text_area))
1008 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1010 int border = theme.Border3DSize.Width;
1012 // No edit control, we paint the edit ourselves
1013 if (dropdown_style == ComboBoxStyle.DropDownList) {
1014 DrawItemState state = DrawItemState.None;
1015 Rectangle item_rect = text_area;
1016 item_rect.X += border;
1017 item_rect.Y += border;
1018 item_rect.Width -= (button_area.Width + 2 * border);
1019 item_rect.Height -= 2 * border;
1022 state = DrawItemState.Selected;
1023 state |= DrawItemState.Focus;
1026 state |= DrawItemState.ComboBoxEdit;
1027 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1030 if (show_dropdown_button) {
1031 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1034 button_state = ButtonState.Inactive;
1036 theme.CPDrawComboButton (dc, button_area, button_state);
1040 internal void DropDownListBox ()
1042 if (DropDownStyle == ComboBoxStyle.Simple)
1045 if (listbox_ctrl == null)
1046 CreateComboListBox ();
1048 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1050 if (listbox_ctrl.ShowWindow ())
1051 dropped_down = true;
1053 button_state = ButtonState.Pushed;
1054 if (dropdown_style == ComboBoxStyle.DropDownList)
1055 Invalidate (text_area);
1057 OnDropDown (EventArgs.Empty);
1060 internal void DropDownListBoxFinished ()
1062 if (DropDownStyle == ComboBoxStyle.Simple)
1065 button_state = ButtonState.Normal;
1066 Invalidate (button_area);
1067 dropped_down = false;
1070 private int FindStringCaseInsensitive (string search)
1072 if (search.Length == 0) {
1076 for (int i = 0; i < Items.Count; i++)
1078 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1085 internal int FindStringCaseInsensitive (string search, int start_index)
1087 if (search.Length == 0) {
1091 for (int i = 0; i < Items.Count; i++) {
1092 int index = (i + start_index) % Items.Count;
1093 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1100 private void OnKeyDownCB(object sender, KeyEventArgs e)
1102 if (Items.Count == 0)
1108 SelectedIndex = Math.Max(SelectedIndex-1, 0);
1112 SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1116 if (listbox_ctrl != null)
1117 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1121 if (listbox_ctrl != null)
1122 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1130 void OnMouseDownCB (object sender, MouseEventArgs e)
1133 if (DropDownStyle == ComboBoxStyle.DropDownList)
1134 area = ClientRectangle;
1138 if (area.Contains (e.X, e.Y)) {
1140 Invalidate (button_area);
1146 void OnMouseMoveCB (object sender, MouseEventArgs e)
1148 if (DropDownStyle == ComboBoxStyle.Simple)
1151 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1152 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1153 if (listbox_ctrl.ClientRectangle.Contains (location))
1154 listbox_ctrl.Capture = true;
1158 void OnMouseUpCB (object sender, MouseEventArgs e)
1161 OnClick (EventArgs.Empty);
1164 listbox_ctrl.Capture = true;
1167 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1169 if (Items.Count == 0)
1172 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1173 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1174 listbox_ctrl.Scroll (-lines);
1176 int lines = me.Delta / 120;
1177 int index = SelectedIndex - lines;
1180 else if (index >= Items.Count)
1181 index = Items.Count - 1;
1182 SelectedIndex = index;
1186 internal override void OnPaintInternal (PaintEventArgs pevent)
1188 if (suspend_ctrlupdate)
1191 Draw (ClientRectangle, pevent.Graphics);
1194 private void OnTextBoxClick (object sender, EventArgs e)
1199 private void OnTextChangedEdit (object sender, EventArgs e)
1201 if (process_textchanged_event == false)
1204 OnTextChanged (EventArgs.Empty);
1206 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1211 // TODO: THIS IS BROKEN-ISH
1212 // I don't think we should hilight, and setting the top item does weirdness
1213 // when there is no scrollbar
1215 if (listbox_ctrl != null) {
1216 listbox_ctrl.SetTopItem (item);
1217 listbox_ctrl.HighlightedIndex = item;
1220 base.Text = textbox_ctrl.Text;
1223 internal void SetControlText (string s)
1225 process_textchanged_event = false;
1226 textbox_ctrl.Text = s;
1227 process_textchanged_event = true;
1230 void UpdateBounds ()
1232 if (requested_height != -1)
1233 SetBounds (0, 0, 0, requested_height, BoundsSpecified.Height);
1236 private void UpdatedItems ()
1238 if (listbox_ctrl != null) {
1239 listbox_ctrl.UpdateLastVisibleItem ();
1240 listbox_ctrl.CalcListBoxArea ();
1241 listbox_ctrl.Refresh ();
1245 #endregion Private Methods
1247 [ListBindableAttribute (false)]
1248 public class ObjectCollection : IList, ICollection, IEnumerable
1251 private ComboBox owner;
1252 internal ArrayList object_items = new ArrayList ();
1254 public ObjectCollection (ComboBox owner)
1259 #region Public Properties
1261 get { return object_items.Count; }
1264 public bool IsReadOnly {
1265 get { return false; }
1269 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1270 public virtual object this [int index] {
1272 if (index < 0 || index >= Count)
1273 throw new ArgumentOutOfRangeException ("Index of out range");
1275 return object_items[index];
1278 if (index < 0 || index >= Count)
1279 throw new ArgumentOutOfRangeException ("Index of out range");
1281 throw new ArgumentNullException ("value");
1283 object_items[index] = value;
1284 if (owner.listbox_ctrl != null)
1285 owner.listbox_ctrl.InvalidateItem (index);
1286 if (index == owner.SelectedIndex) {
1287 if (owner.textbox_ctrl == null)
1290 owner.textbox_ctrl.SelectedText = value.ToString ();
1295 bool ICollection.IsSynchronized {
1296 get { return false; }
1299 object ICollection.SyncRoot {
1300 get { return this; }
1303 bool IList.IsFixedSize {
1304 get { return false; }
1307 #endregion Public Properties
1309 #region Public Methods
1310 public int Add (object item)
1314 idx = AddItem (item);
1315 owner.UpdatedItems ();
1319 public void AddRange (object[] items)
1322 throw new ArgumentNullException ("items");
1324 foreach (object mi in items)
1327 owner.UpdatedItems ();
1330 public void Clear ()
1332 owner.selected_index = -1;
1333 object_items.Clear ();
1334 owner.UpdatedItems ();
1338 public bool Contains (object obj)
1341 throw new ArgumentNullException ("obj");
1343 return object_items.Contains (obj);
1346 public void CopyTo (object[] dest, int arrayIndex)
1348 object_items.CopyTo (dest, arrayIndex);
1351 void ICollection.CopyTo (Array dest, int index)
1353 object_items.CopyTo (dest, index);
1356 public IEnumerator GetEnumerator ()
1358 return object_items.GetEnumerator ();
1361 int IList.Add (object item)
1366 public int IndexOf (object value)
1369 throw new ArgumentNullException ("value");
1371 return object_items.IndexOf (value);
1374 public void Insert (int index, object item)
1376 if (index < 0 || index > Count)
1377 throw new ArgumentOutOfRangeException ("Index of out range");
1379 throw new ArgumentNullException ("item");
1381 owner.BeginUpdate ();
1386 object_items.Insert (index, item);
1388 owner.EndUpdate (); // Calls UpdatedItems
1391 public void Remove (object value)
1396 if (IndexOf (value) == owner.SelectedIndex)
1397 owner.SelectedIndex = -1;
1399 RemoveAt (IndexOf (value));
1402 public void RemoveAt (int index)
1404 if (index < 0 || index >= Count)
1405 throw new ArgumentOutOfRangeException ("Index of out range");
1407 if (index == owner.SelectedIndex)
1408 owner.SelectedIndex = -1;
1410 object_items.RemoveAt (index);
1411 owner.UpdatedItems ();
1413 #endregion Public Methods
1415 #region Private Methods
1416 private int AddItem (object item)
1419 throw new ArgumentNullException ("item");
1423 foreach (string s in object_items) {
1424 if (String.Compare (item as String, s) < 0) {
1425 object_items.Insert (index, item);
1431 object_items.Add (item);
1432 return object_items.Count - 1;
1435 internal void AddRange (IList items)
1437 foreach (object mi in items)
1440 owner.UpdatedItems ();
1443 internal void Sort ()
1445 object_items.Sort ();
1448 #endregion Private Methods
1451 internal class ComboTextBox : TextBox {
1453 private ComboBox owner;
1455 public ComboTextBox (ComboBox owner)
1458 ShowSelection = false;
1461 internal void SetSelectable (bool selectable)
1463 SetStyle (ControlStyles.Selectable, selectable);
1466 internal void ActivateCaret (bool active)
1469 document.CaretHasFocus ();
1471 document.CaretLostFocus ();
1474 internal override void OnGotFocusInternal (EventArgs e)
1476 owner.Select (false, true);
1479 internal override void OnLostFocusInternal (EventArgs e)
1481 owner.Select (false, true);
1485 internal class ComboListBox : Control
1487 private ComboBox owner;
1488 private VScrollBarLB vscrollbar_ctrl;
1489 private int top_item; /* First item that we show the in the current page */
1490 private int last_item; /* Last visible item */
1491 internal int page_size; /* Number of listbox items per page */
1492 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1494 internal enum ItemNavigation
1504 class VScrollBarLB : VScrollBar
1506 public VScrollBarLB ()
1510 internal override bool InternalCapture {
1511 get { return Capture; }
1515 public bool FireMouseDown (MouseEventArgs e)
1518 e = TranslateEvent (e);
1519 if (ClientRectangle.Contains (e.X, e.Y)) {
1527 public void FireMouseUp (MouseEventArgs e)
1530 e = TranslateEvent (e);
1531 if (ClientRectangle.Contains (e.X, e.Y))
1536 public void FireMouseMove (MouseEventArgs e)
1539 e = TranslateEvent (e);
1540 if (ClientRectangle.Contains (e.X, e.Y))
1545 MouseEventArgs TranslateEvent (MouseEventArgs e)
1547 Point loc = PointToClient (Control.MousePosition);
1548 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1552 public ComboListBox (ComboBox owner)
1559 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
1561 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1562 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1564 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1565 InternalBorderStyle = BorderStyle.Fixed3D;
1567 InternalBorderStyle = BorderStyle.FixedSingle;
1570 protected override CreateParams CreateParams
1573 CreateParams cp = base.CreateParams;
1574 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
1577 cp.Style ^= (int) WindowStyles.WS_CHILD;
1578 cp.Style |= (int) WindowStyles.WS_POPUP;
1579 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
1584 protected override void Select (bool directed, bool forward)
1586 // Do nothing, we never want to be selected
1589 internal override bool InternalCapture {
1600 switch (border_style) {
1601 case BorderStyle.Fixed3D:
1602 return ThemeEngine.Current.Border3DSize.Width;
1604 return ThemeEngine.Current.BorderSize.Width;
1609 #region Private Methods
1610 // Calcs the listbox area
1611 internal void CalcListBoxArea ()
1614 bool show_scrollbar = false;
1616 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1617 Rectangle area = owner.listbox_area;
1619 height = area.Height;
1621 else { // DropDown or DropDownList
1623 width = owner.DropDownWidth;
1624 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1626 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1628 for (int i = 0; i < count; i++) {
1629 height += owner.GetItemHeight (i);
1633 height = owner.ItemHeight * count;
1637 if (owner.Items.Count <= owner.MaxDropDownItems) {
1638 if (vscrollbar_ctrl != null)
1639 vscrollbar_ctrl.Visible = false;
1641 /* Need vertical scrollbar */
1642 if (vscrollbar_ctrl == null) {
1643 vscrollbar_ctrl = new VScrollBarLB ();
1644 vscrollbar_ctrl.Minimum = 0;
1645 vscrollbar_ctrl.SmallChange = 1;
1646 vscrollbar_ctrl.LargeChange = 1;
1647 vscrollbar_ctrl.Maximum = 0;
1648 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1649 Controls.AddImplicit (vscrollbar_ctrl);
1652 vscrollbar_ctrl.Height = height - 2 * BorderWidth;
1654 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - BorderWidth - 1, 0);
1656 vscrollbar_ctrl.Maximum = owner.Items.Count - (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items);
1657 show_scrollbar = vscrollbar_ctrl.Visible = true;
1659 int hli = HighlightedIndex;
1661 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
1662 vscrollbar_ctrl.Value = hli;
1666 Size = new Size (width, height);
1667 textarea_drawable = ClientRectangle;
1668 textarea_drawable.Width = width;
1669 textarea_drawable.Height = height;
1671 if (vscrollbar_ctrl != null && show_scrollbar)
1672 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1674 last_item = LastVisibleItem ();
1675 page_size = textarea_drawable.Height / owner.ItemHeight;
1678 private void Draw (Rectangle clip, Graphics dc)
1680 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
1682 if (owner.Items.Count > 0) {
1684 for (int i = top_item; i <= last_item; i++) {
1685 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
1687 if (!clip.IntersectsWith (item_rect))
1690 DrawItemState state = DrawItemState.None;
1692 if (i == HighlightedIndex) {
1693 state |= DrawItemState.Selected;
1695 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1696 state |= DrawItemState.Focus;
1700 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
1701 i, state, owner.ForeColor, owner.BackColor));
1706 int highlighted_index = -1;
1708 public int HighlightedIndex {
1709 get { return highlighted_index; }
1711 if (highlighted_index == value)
1714 if (highlighted_index != -1)
1715 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
1716 highlighted_index = value;
1717 if (highlighted_index != -1)
1718 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
1722 private Rectangle GetItemDisplayRectangle (int index, int top_index)
1724 if (index < 0 || index >= owner.Items.Count)
1725 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1727 Rectangle item_rect = new Rectangle ();
1728 int height = owner.GetItemHeight (index);
1731 item_rect.Width = textarea_drawable.Width;
1732 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1734 for (int i = top_index; i < index; i++)
1735 item_rect.Y += owner.GetItemHeight (i);
1737 item_rect.Y = height * (index - top_index);
1739 item_rect.Height = height;
1743 public void HideWindow ()
1745 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1750 owner.DropDownListBoxFinished ();
1753 private int IndexFromPointDisplayRectangle (int x, int y)
1755 for (int i = top_item; i <= last_item; i++) {
1756 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1763 public void InvalidateItem (int index)
1766 Invalidate (GetItemDisplayRectangle (index, top_item));
1769 private int LastVisibleItem ()
1771 Rectangle item_rect;
1772 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1775 for (i = top_item; i < owner.Items.Count; i++) {
1776 item_rect = GetItemDisplayRectangle (i, top_item);
1777 if (item_rect.Y + item_rect.Height > top_y) {
1784 public void SetTopItem (int item)
1786 if (top_item == item)
1789 UpdateLastVisibleItem ();
1793 protected override void OnMouseDown (MouseEventArgs e)
1795 if (vscrollbar_ctrl != null)
1796 vscrollbar_ctrl.FireMouseDown (e);
1799 protected override void OnMouseMove (MouseEventArgs e)
1801 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1804 Point pt = PointToClient (Control.MousePosition);
1805 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
1808 HighlightedIndex = index;
1812 if (vscrollbar_ctrl != null)
1813 vscrollbar_ctrl.FireMouseMove (e);
1816 protected override void OnMouseUp (MouseEventArgs e)
1818 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1821 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.FireMouseDown (e))
1824 owner.OnSelectionChangeCommitted (new EventArgs ());
1825 owner.SelectedIndex = index;
1829 if (vscrollbar_ctrl != null)
1830 vscrollbar_ctrl.FireMouseUp (e);
1833 internal override void OnPaintInternal (PaintEventArgs pevent)
1835 Draw (pevent.ClipRectangle,pevent.Graphics);
1838 public bool ShowWindow ()
1840 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
1843 HighlightedIndex = owner.SelectedIndex;
1849 owner.OnDropDown (EventArgs.Empty);
1853 public void UpdateLastVisibleItem ()
1855 last_item = LastVisibleItem ();
1858 public void Scroll (int delta)
1860 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
1863 int max = vscrollbar_ctrl.Maximum;//- (page_size) + 1;
1865 int val = vscrollbar_ctrl.Value + delta;
1868 else if (val < vscrollbar_ctrl.Minimum)
1869 val = vscrollbar_ctrl.Minimum;
1870 vscrollbar_ctrl.Value = val;
1873 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
1875 if (owner.Items.Count == 0)
1878 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1883 private void VerticalScrollEvent (object sender, EventArgs e)
1885 if (top_item == vscrollbar_ctrl.Value)
1888 top_item = vscrollbar_ctrl.Value;
1889 UpdateLastVisibleItem ();
1893 protected override void WndProc(ref Message m) {
1894 if (m.Msg == (int)Msg.WM_SETFOCUS) {
1895 if (m.WParam != IntPtr.Zero) {
1896 XplatUI.SetFocus(m.WParam);
1899 base.WndProc (ref m);
1902 #endregion Private Methods