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 (selected_index == value)
391 if (value <= -2 || value >= Items.Count)
392 throw new ArgumentOutOfRangeException ("Index of out range");
393 selected_index = value;
395 if (dropdown_style != ComboBoxStyle.DropDownList) {
399 SetControlText (GetItemText (Items [value]));
402 if (DropDownStyle == ComboBoxStyle.DropDownList)
405 if (listbox_ctrl != null)
406 listbox_ctrl.HighlightedIndex = value;
408 OnSelectedValueChanged (new EventArgs ());
409 OnSelectedIndexChanged (new EventArgs ());
410 OnSelectedItemChanged (new EventArgs ());
415 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
417 public object SelectedItem {
418 get { return selected_index == -1 ? null : Items [selected_index]; }
420 object item = selected_index == -1 ? null : Items [selected_index];
424 SelectedIndex = Items.IndexOf (value);
429 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
430 public string SelectedText {
432 if (dropdown_style == ComboBoxStyle.DropDownList)
435 return textbox_ctrl.SelectedText;
438 if (dropdown_style == ComboBoxStyle.DropDownList)
441 textbox_ctrl.SelectedText = value;
446 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
447 public int SelectionLength {
449 if (dropdown_style == ComboBoxStyle.DropDownList)
452 return textbox_ctrl.SelectionLength;
455 if (dropdown_style == ComboBoxStyle.DropDownList)
458 if (textbox_ctrl.SelectionLength == value)
461 textbox_ctrl.SelectionLength = value;
466 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
467 public int SelectionStart {
469 if (dropdown_style == ComboBoxStyle.DropDownList)
472 return textbox_ctrl.SelectionStart;
475 if (dropdown_style == ComboBoxStyle.DropDownList)
478 if (textbox_ctrl.SelectionStart == value)
481 textbox_ctrl.SelectionStart = value;
485 [DefaultValue (false)]
487 get { return sorted; }
504 public override string Text {
506 if (dropdown_style != ComboBoxStyle.DropDownList) {
507 if (textbox_ctrl != null) {
508 return textbox_ctrl.Text;
512 if (SelectedItem != null)
513 return GetItemText (SelectedItem);
523 int index = FindString (value);
526 SelectedIndex = index;
530 if (dropdown_style != ComboBoxStyle.DropDownList)
531 textbox_ctrl.Text = GetItemText (value);
535 #endregion Public Properties
537 #region Public Methods
538 protected virtual void AddItemsCore (object[] value)
543 public void BeginUpdate ()
545 suspend_ctrlupdate = true;
548 protected override void Dispose (bool disposing)
551 if (listbox_ctrl != null) {
552 listbox_ctrl.Dispose ();
553 Controls.RemoveImplicit (listbox_ctrl);
557 if (textbox_ctrl != null) {
558 Controls.RemoveImplicit (textbox_ctrl);
559 textbox_ctrl.Dispose ();
564 base.Dispose (disposing);
567 public void EndUpdate ()
569 suspend_ctrlupdate = false;
574 public int FindString (string s)
576 return FindString (s, -1);
579 public int FindString (string s, int startIndex)
581 if (Items.Count == 0)
582 return -1; // No exception throwing if empty
584 if (startIndex < -1 || startIndex >= Items.Count - 1)
585 throw new ArgumentOutOfRangeException ("Index of out range");
588 for (int i = startIndex; i < Items.Count; i++) {
589 if ((GetItemText (Items[i])).StartsWith (s))
596 public int FindStringExact (string s)
598 return FindStringExact (s, -1);
601 public int FindStringExact (string s, int startIndex)
603 if (Items.Count == 0)
604 return -1; // No exception throwing if empty
606 if (startIndex < -1 || startIndex >= Items.Count - 1)
607 throw new ArgumentOutOfRangeException ("Index of out range");
610 for (int i = startIndex; i < Items.Count; i++) {
611 if ((GetItemText (Items[i])).Equals (s))
618 public int GetItemHeight (int index)
620 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
622 if (index < 0 || index >= Items.Count )
623 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
625 object item = Items [index];
626 if (item_heights.Contains (item))
627 return (int) item_heights [item];
629 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
630 OnMeasureItem (args);
631 item_heights [item] = args.ItemHeight;
632 return args.ItemHeight;
638 protected override bool IsInputKey (Keys keyData)
654 protected override void OnBackColorChanged (EventArgs e)
656 base.OnBackColorChanged (e);
659 protected override void OnDataSourceChanged (EventArgs e)
661 base.OnDataSourceChanged (e);
664 if (DataSource == null || DataManager == null) {
668 SelectedIndex = DataManager.Position;
672 protected override void OnDisplayMemberChanged (EventArgs e)
674 base.OnDisplayMemberChanged (e);
676 if (DataManager == null || !IsHandleCreated)
680 SelectedIndex = DataManager.Position;
683 protected virtual void OnDrawItem (DrawItemEventArgs e)
686 case DrawMode.OwnerDrawFixed:
687 case DrawMode.OwnerDrawVariable:
688 if (DrawItem != null)
692 ThemeEngine.Current.DrawComboBoxItem (this, e);
697 protected virtual void OnDropDown (EventArgs e)
699 if (DropDown != null)
703 protected virtual void OnDropDownStyleChanged (EventArgs e)
705 if (DropDownStyleChanged != null)
706 DropDownStyleChanged (this, e);
709 protected override void OnFontChanged (EventArgs e)
711 base.OnFontChanged (e);
713 if (textbox_ctrl != null)
714 textbox_ctrl.Font = Font;
716 if (!item_height_specified) {
717 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
718 item_height = (int) sz.Height;
727 protected override void OnForeColorChanged (EventArgs e)
729 base.OnForeColorChanged (e);
732 [EditorBrowsable(EditorBrowsableState.Advanced)]
733 protected override void OnGotFocus (EventArgs e)
735 if (dropdown_style == ComboBoxStyle.DropDownList) {
736 // We draw DDL styles manually, so they require a
737 // refresh to have their selection drawn
741 if (textbox_ctrl != null) {
742 textbox_ctrl.SetSelectable (false);
743 textbox_ctrl.ActivateCaret (true);
744 textbox_ctrl.ShowSelection = true;
745 textbox_ctrl.SelectAll ();
751 [EditorBrowsable(EditorBrowsableState.Advanced)]
752 protected override void OnLostFocus (EventArgs e)
754 if (dropdown_style == ComboBoxStyle.DropDownList) {
755 // We draw DDL styles manually, so they require a
756 // refresh to have their selection drawn
760 if (listbox_ctrl != null && dropped_down) {
761 listbox_ctrl.HideWindow ();
764 if (textbox_ctrl != null) {
765 textbox_ctrl.SetSelectable (true);
766 textbox_ctrl.ActivateCaret (false);
767 textbox_ctrl.ShowSelection = false;
770 base.OnLostFocus (e);
773 protected override void OnHandleCreated (EventArgs e)
775 base.OnHandleCreated (e);
777 if (listbox_ctrl != null)
778 Controls.AddImplicit (listbox_ctrl);
780 if (textbox_ctrl != null)
781 Controls.AddImplicit (textbox_ctrl);
786 protected override void OnHandleDestroyed (EventArgs e)
788 base.OnHandleDestroyed (e);
791 protected override void OnKeyPress (KeyPressEventArgs e)
793 // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
795 // SelectedIndex = index;
800 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
802 if (MeasureItem != null)
803 MeasureItem (this, e);
806 protected override void OnParentBackColorChanged (EventArgs e)
808 base.OnParentBackColorChanged (e);
811 protected override void OnResize (EventArgs e)
814 if (listbox_ctrl != null)
815 listbox_ctrl.CalcListBoxArea ();
818 protected override void OnSelectedIndexChanged (EventArgs e)
820 base.OnSelectedIndexChanged (e);
822 if (SelectedIndexChanged != null)
823 SelectedIndexChanged (this, e);
826 protected virtual void OnSelectedItemChanged (EventArgs e)
831 protected override void OnSelectedValueChanged (EventArgs e)
833 base.OnSelectedValueChanged (e);
836 protected virtual void OnSelectionChangeCommitted (EventArgs e)
838 if (SelectionChangeCommitted != null)
839 SelectionChangeCommitted (this, e);
842 protected override void RefreshItem (int index)
844 if (index < 0 || index >= Items.Count)
845 throw new ArgumentOutOfRangeException ("Index of out range");
847 if (draw_mode == DrawMode.OwnerDrawVariable)
848 item_heights.Remove (Items [index]);
851 public void Select (int start, int length)
854 throw new ArgumentException ("Start cannot be less than zero");
857 throw new ArgumentException ("length cannot be less than zero");
859 if (dropdown_style == ComboBoxStyle.DropDownList)
862 textbox_ctrl.Select (start, length);
865 public void SelectAll ()
867 if (dropdown_style == ComboBoxStyle.DropDownList)
870 if (textbox_ctrl != null) {
871 textbox_ctrl.ShowSelection = true;
872 textbox_ctrl.SelectAll ();
876 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
878 if ((specified & BoundsSpecified.Height) != 0) {
879 requested_height = height;
881 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
882 if (IntegralHeight) {
883 int border = ThemeEngine.Current.Border3DSize.Height;
884 int lb_height = height - PreferredHeight - 2;
885 if (lb_height - 2 * border > ItemHeight) {
886 int partial = (lb_height - 2 * border) % ItemHeight;
889 height = PreferredHeight;
892 height = PreferredHeight;
895 base.SetBoundsCore (x, y, width, height, specified);
898 protected override void SetItemCore (int index, object value)
900 if (index < 0 || index >= Items.Count)
903 Items[index] = value;
906 protected override void SetItemsCore (IList value)
911 Items.AddRange (value);
917 public override string ToString ()
919 return base.ToString () + ", Items.Count:" + Items.Count;
922 protected override void WndProc (ref Message m)
924 switch ((Msg) m.Msg) {
927 Keys keys = (Keys) m.WParam.ToInt32 ();
928 if (keys == Keys.Up || keys == Keys.Down)
930 goto case Msg.WM_CHAR;
932 if (textbox_ctrl != null)
933 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
935 case Msg.WM_MOUSE_LEAVE:
936 Point location = PointToClient (Control.MousePosition);
937 if (ClientRectangle.Contains (location))
943 base.WndProc (ref m);
946 #endregion Public Methods
948 #region Private Methods
950 internal override bool InternalCapture {
951 get { return Capture; }
957 int border = ThemeEngine.Current.Border3DSize.Width;
959 text_area = ClientRectangle;
960 text_area.Height = PreferredHeight;
962 listbox_area = ClientRectangle;
963 listbox_area.Y = text_area.Bottom + 3;
964 listbox_area.Height -= (text_area.Height + 2);
966 Rectangle prev_button_area = button_area;
968 if (DropDownStyle == ComboBoxStyle.Simple)
969 button_area = Rectangle.Empty;
971 button_area = text_area;
972 button_area.X = text_area.Right - button_width - border;
973 button_area.Y = text_area.Y + border;
974 button_area.Width = button_width;
975 button_area.Height = text_area.Height - 2 * border;
978 if (button_area != prev_button_area) {
979 prev_button_area.Y -= border;
980 prev_button_area.Width += border;
981 prev_button_area.Height += 2 * border;
982 Invalidate (prev_button_area);
983 Invalidate (button_area);
986 if (textbox_ctrl != null) {
987 textbox_ctrl.Location = new Point (text_area.X + border, text_area.Y + border);
988 textbox_ctrl.Width = text_area.Width - button_area.Width - border * 2;
989 textbox_ctrl.Height = text_area.Height - border * 2;
992 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
993 listbox_ctrl.Location = listbox_area.Location;
994 listbox_ctrl.CalcListBoxArea ();
998 private void CreateComboListBox ()
1000 listbox_ctrl = new ComboListBox (this);
1001 listbox_ctrl.HighlightedIndex = SelectedIndex;
1004 internal void Draw (Rectangle clip, Graphics dc)
1006 Theme theme = ThemeEngine.Current;
1008 if (DropDownStyle == ComboBoxStyle.Simple)
1009 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1011 if (clip.IntersectsWith (text_area))
1012 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1014 int border = theme.Border3DSize.Width;
1016 // No edit control, we paint the edit ourselves
1017 if (dropdown_style == ComboBoxStyle.DropDownList) {
1018 DrawItemState state = DrawItemState.None;
1019 Rectangle item_rect = text_area;
1020 item_rect.X += border;
1021 item_rect.Y += border;
1022 item_rect.Width -= (button_area.Width + 2 * border);
1023 item_rect.Height -= 2 * border;
1026 state = DrawItemState.Selected;
1027 state |= DrawItemState.Focus;
1030 state |= DrawItemState.ComboBoxEdit;
1031 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1034 if (show_dropdown_button) {
1035 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1038 button_state = ButtonState.Inactive;
1040 theme.CPDrawComboButton (dc, button_area, button_state);
1044 internal void DropDownListBox ()
1046 if (DropDownStyle == ComboBoxStyle.Simple)
1049 if (listbox_ctrl == null)
1050 CreateComboListBox ();
1052 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1054 if (listbox_ctrl.ShowWindow ())
1055 dropped_down = true;
1057 button_state = ButtonState.Pushed;
1058 if (dropdown_style == ComboBoxStyle.DropDownList)
1059 Invalidate (text_area);
1062 internal void DropDownListBoxFinished ()
1064 if (DropDownStyle == ComboBoxStyle.Simple)
1067 button_state = ButtonState.Normal;
1068 Invalidate (button_area);
1069 dropped_down = false;
1072 private int FindStringCaseInsensitive (string search)
1074 if (search.Length == 0) {
1078 for (int i = 0; i < Items.Count; i++)
1080 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1087 internal int FindStringCaseInsensitive (string search, int start_index)
1089 if (search.Length == 0) {
1093 for (int i = 0; i < Items.Count; i++) {
1094 int index = (i + start_index) % Items.Count;
1095 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1102 private void OnKeyDownCB(object sender, KeyEventArgs e)
1104 if (Items.Count == 0)
1110 SelectedIndex = Math.Max(SelectedIndex-1, 0);
1114 SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1118 if (listbox_ctrl != null)
1119 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1123 if (listbox_ctrl != null)
1124 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1132 void OnMouseDownCB (object sender, MouseEventArgs e)
1135 if (DropDownStyle == ComboBoxStyle.DropDownList)
1136 area = ClientRectangle;
1140 if (area.Contains (e.X, e.Y)) {
1142 Invalidate (button_area);
1148 void OnMouseMoveCB (object sender, MouseEventArgs e)
1150 if (DropDownStyle == ComboBoxStyle.Simple)
1153 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1154 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1155 if (listbox_ctrl.ClientRectangle.Contains (location))
1156 listbox_ctrl.Capture = true;
1160 void OnMouseUpCB (object sender, MouseEventArgs e)
1163 OnClick (EventArgs.Empty);
1166 listbox_ctrl.Capture = true;
1169 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1171 if (Items.Count == 0)
1174 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1175 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1176 listbox_ctrl.Scroll (-lines);
1178 int lines = me.Delta / 120;
1179 int index = SelectedIndex - lines;
1182 else if (index >= Items.Count)
1183 index = Items.Count - 1;
1184 SelectedIndex = index;
1188 internal override void OnPaintInternal (PaintEventArgs pevent)
1190 if (suspend_ctrlupdate)
1193 Draw (ClientRectangle, pevent.Graphics);
1196 private void OnTextBoxClick (object sender, EventArgs e)
1201 private void OnTextChangedEdit (object sender, EventArgs e)
1203 if (process_textchanged_event == false)
1206 OnTextChanged (EventArgs.Empty);
1208 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1213 // TODO: THIS IS BROKEN-ISH
1214 // I don't think we should hilight, and setting the top item does weirdness
1215 // when there is no scrollbar
1217 if (listbox_ctrl != null) {
1218 listbox_ctrl.SetTopItem (item);
1219 listbox_ctrl.HighlightedIndex = item;
1222 base.Text = textbox_ctrl.Text;
1225 internal void SetControlText (string s)
1227 process_textchanged_event = false;
1228 textbox_ctrl.Text = s;
1229 process_textchanged_event = true;
1232 void UpdateBounds ()
1234 if (requested_height != -1)
1235 SetBounds (0, 0, 0, requested_height, BoundsSpecified.Height);
1238 private void UpdatedItems ()
1240 if (listbox_ctrl != null) {
1241 listbox_ctrl.UpdateLastVisibleItem ();
1242 listbox_ctrl.CalcListBoxArea ();
1243 listbox_ctrl.Refresh ();
1247 #endregion Private Methods
1249 [ListBindableAttribute (false)]
1250 public class ObjectCollection : IList, ICollection, IEnumerable
1253 private ComboBox owner;
1254 internal ArrayList object_items = new ArrayList ();
1256 public ObjectCollection (ComboBox owner)
1261 #region Public Properties
1263 get { return object_items.Count; }
1266 public bool IsReadOnly {
1267 get { return false; }
1271 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1272 public virtual object this [int index] {
1274 if (index < 0 || index >= Count)
1275 throw new ArgumentOutOfRangeException ("Index of out range");
1277 return object_items[index];
1280 if (index < 0 || index >= Count)
1281 throw new ArgumentOutOfRangeException ("Index of out range");
1283 throw new ArgumentNullException ("value");
1285 object_items[index] = value;
1286 if (owner.listbox_ctrl != null)
1287 owner.listbox_ctrl.InvalidateItem (index);
1288 if (index == owner.SelectedIndex) {
1289 if (owner.textbox_ctrl == null)
1292 owner.textbox_ctrl.SelectedText = value.ToString ();
1297 bool ICollection.IsSynchronized {
1298 get { return false; }
1301 object ICollection.SyncRoot {
1302 get { return this; }
1305 bool IList.IsFixedSize {
1306 get { return false; }
1309 #endregion Public Properties
1311 #region Public Methods
1312 public int Add (object item)
1316 idx = AddItem (item);
1317 owner.UpdatedItems ();
1321 public void AddRange (object[] items)
1324 throw new ArgumentNullException ("items");
1326 foreach (object mi in items)
1329 owner.UpdatedItems ();
1332 public void Clear ()
1334 owner.selected_index = -1;
1335 object_items.Clear ();
1336 owner.UpdatedItems ();
1340 public bool Contains (object obj)
1343 throw new ArgumentNullException ("obj");
1345 return object_items.Contains (obj);
1348 public void CopyTo (object[] dest, int arrayIndex)
1350 object_items.CopyTo (dest, arrayIndex);
1353 void ICollection.CopyTo (Array dest, int index)
1355 object_items.CopyTo (dest, index);
1358 public IEnumerator GetEnumerator ()
1360 return object_items.GetEnumerator ();
1363 int IList.Add (object item)
1368 public int IndexOf (object value)
1371 throw new ArgumentNullException ("value");
1373 return object_items.IndexOf (value);
1376 public void Insert (int index, object item)
1378 if (index < 0 || index > Count)
1379 throw new ArgumentOutOfRangeException ("Index of out range");
1381 throw new ArgumentNullException ("item");
1383 owner.BeginUpdate ();
1388 object_items.Insert (index, item);
1390 owner.EndUpdate (); // Calls UpdatedItems
1393 public void Remove (object value)
1398 if (IndexOf (value) == owner.SelectedIndex)
1399 owner.SelectedIndex = -1;
1401 RemoveAt (IndexOf (value));
1404 public void RemoveAt (int index)
1406 if (index < 0 || index >= Count)
1407 throw new ArgumentOutOfRangeException ("Index of out range");
1409 if (index == owner.SelectedIndex)
1410 owner.SelectedIndex = -1;
1412 object_items.RemoveAt (index);
1413 owner.UpdatedItems ();
1415 #endregion Public Methods
1417 #region Private Methods
1418 private int AddItem (object item)
1421 throw new ArgumentNullException ("item");
1425 foreach (object o in object_items) {
1426 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
1427 object_items.Insert (index, item);
1433 object_items.Add (item);
1434 return object_items.Count - 1;
1437 internal void AddRange (IList items)
1439 foreach (object mi in items)
1442 owner.UpdatedItems ();
1445 internal void Sort ()
1447 object_items.Sort ();
1450 #endregion Private Methods
1453 internal class ComboTextBox : TextBox {
1455 private ComboBox owner;
1457 public ComboTextBox (ComboBox owner)
1460 ShowSelection = false;
1463 internal void SetSelectable (bool selectable)
1465 SetStyle (ControlStyles.Selectable, selectable);
1468 internal void ActivateCaret (bool active)
1471 document.CaretHasFocus ();
1473 document.CaretLostFocus ();
1476 internal override void OnGotFocusInternal (EventArgs e)
1478 owner.Select (false, true);
1481 internal override void OnLostFocusInternal (EventArgs e)
1483 owner.Select (false, true);
1487 internal class ComboListBox : Control
1489 private ComboBox owner;
1490 private VScrollBarLB vscrollbar_ctrl;
1491 private int top_item; /* First item that we show the in the current page */
1492 private int last_item; /* Last visible item */
1493 internal int page_size; /* Number of listbox items per page */
1494 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1496 internal enum ItemNavigation
1506 class VScrollBarLB : VScrollBar
1508 public VScrollBarLB ()
1512 internal override bool InternalCapture {
1513 get { return Capture; }
1517 public void FireMouseDown (MouseEventArgs e)
1520 e = TranslateEvent (e);
1521 if (ClientRectangle.Contains (e.X, e.Y))
1526 public bool FireMouseUp (MouseEventArgs e)
1529 e = TranslateEvent (e);
1530 if (ClientRectangle.Contains (e.X, e.Y)) {
1538 public void FireMouseMove (MouseEventArgs e)
1541 e = TranslateEvent (e);
1542 if (ClientRectangle.Contains (e.X, e.Y))
1547 MouseEventArgs TranslateEvent (MouseEventArgs e)
1549 Point loc = PointToClient (Control.MousePosition);
1550 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1554 public ComboListBox (ComboBox owner)
1561 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
1563 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1564 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1566 this.is_visible = false;
1567 Hwnd.ObjectFromHandle (this.Handle).no_activate = true;
1569 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1570 InternalBorderStyle = BorderStyle.Fixed3D;
1572 InternalBorderStyle = BorderStyle.FixedSingle;
1575 protected override CreateParams CreateParams
1578 CreateParams cp = base.CreateParams;
1579 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
1582 cp.Style ^= (int) WindowStyles.WS_CHILD;
1583 cp.Style |= (int) WindowStyles.WS_POPUP;
1584 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
1589 protected override void Select (bool directed, bool forward)
1591 // Do nothing, we never want to be selected
1594 internal override bool InternalCapture {
1605 switch (border_style) {
1606 case BorderStyle.Fixed3D:
1607 return ThemeEngine.Current.Border3DSize.Width;
1609 return ThemeEngine.Current.BorderSize.Width;
1614 #region Private Methods
1615 // Calcs the listbox area
1616 internal void CalcListBoxArea ()
1619 bool show_scrollbar = false;
1621 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1622 Rectangle area = owner.listbox_area;
1624 height = area.Height;
1626 else { // DropDown or DropDownList
1628 width = owner.DropDownWidth;
1629 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1631 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1633 for (int i = 0; i < count; i++) {
1634 height += owner.GetItemHeight (i);
1638 height = owner.ItemHeight * count;
1642 if (owner.Items.Count <= owner.MaxDropDownItems) {
1643 if (vscrollbar_ctrl != null)
1644 vscrollbar_ctrl.Visible = false;
1646 /* Need vertical scrollbar */
1647 if (vscrollbar_ctrl == null) {
1648 vscrollbar_ctrl = new VScrollBarLB ();
1649 vscrollbar_ctrl.Minimum = 0;
1650 vscrollbar_ctrl.SmallChange = 1;
1651 vscrollbar_ctrl.LargeChange = 1;
1652 vscrollbar_ctrl.Maximum = 0;
1653 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1654 Controls.AddImplicit (vscrollbar_ctrl);
1657 vscrollbar_ctrl.Height = height - 2 * BorderWidth;
1659 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - BorderWidth - 1, 0);
1661 vscrollbar_ctrl.Maximum = owner.Items.Count - (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items);
1662 vscrollbar_ctrl.LargeChange = owner.MaxDropDownItems - 1;
1663 show_scrollbar = vscrollbar_ctrl.Visible = true;
1665 int hli = HighlightedIndex;
1667 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
1668 vscrollbar_ctrl.Value = hli;
1672 Size = new Size (width, height);
1673 textarea_drawable = ClientRectangle;
1674 textarea_drawable.Width = width;
1675 textarea_drawable.Height = height;
1677 if (vscrollbar_ctrl != null && show_scrollbar)
1678 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1680 last_item = LastVisibleItem ();
1681 page_size = textarea_drawable.Height / owner.ItemHeight;
1684 private void Draw (Rectangle clip, Graphics dc)
1686 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
1688 if (owner.Items.Count > 0) {
1690 for (int i = top_item; i <= last_item; i++) {
1691 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
1693 if (!clip.IntersectsWith (item_rect))
1696 DrawItemState state = DrawItemState.None;
1698 if (i == HighlightedIndex) {
1699 state |= DrawItemState.Selected;
1701 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1702 state |= DrawItemState.Focus;
1706 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
1707 i, state, owner.ForeColor, owner.BackColor));
1712 int highlighted_index = -1;
1714 public int HighlightedIndex {
1715 get { return highlighted_index; }
1717 if (highlighted_index == value)
1720 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
1721 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
1722 highlighted_index = value;
1723 if (highlighted_index != -1)
1724 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
1728 private Rectangle GetItemDisplayRectangle (int index, int top_index)
1730 if (index < 0 || index >= owner.Items.Count)
1731 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1733 Rectangle item_rect = new Rectangle ();
1734 int height = owner.GetItemHeight (index);
1737 item_rect.Width = textarea_drawable.Width;
1738 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1740 for (int i = top_index; i < index; i++)
1741 item_rect.Y += owner.GetItemHeight (i);
1743 item_rect.Y = height * (index - top_index);
1745 item_rect.Height = height;
1749 public void HideWindow ()
1751 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1756 owner.DropDownListBoxFinished ();
1759 private int IndexFromPointDisplayRectangle (int x, int y)
1761 for (int i = top_item; i <= last_item; i++) {
1762 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1769 public void InvalidateItem (int index)
1772 Invalidate (GetItemDisplayRectangle (index, top_item));
1775 private int LastVisibleItem ()
1777 Rectangle item_rect;
1778 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1781 for (i = top_item; i < owner.Items.Count; i++) {
1782 item_rect = GetItemDisplayRectangle (i, top_item);
1783 if (item_rect.Y + item_rect.Height > top_y) {
1790 public void SetTopItem (int item)
1792 if (top_item == item)
1795 UpdateLastVisibleItem ();
1799 protected override void OnMouseDown (MouseEventArgs e)
1801 if (vscrollbar_ctrl != null)
1802 vscrollbar_ctrl.FireMouseDown (e);
1805 protected override void OnMouseMove (MouseEventArgs e)
1807 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1810 Point pt = PointToClient (Control.MousePosition);
1811 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
1814 HighlightedIndex = index;
1818 if (vscrollbar_ctrl != null)
1819 vscrollbar_ctrl.FireMouseMove (e);
1822 protected override void OnMouseUp (MouseEventArgs e)
1824 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1827 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.FireMouseUp (e))
1830 owner.SelectedIndex = index;
1831 owner.OnSelectionChangeCommitted (new EventArgs ());
1835 if (vscrollbar_ctrl != null)
1836 vscrollbar_ctrl.FireMouseUp (e);
1839 internal override void OnPaintInternal (PaintEventArgs pevent)
1841 Draw (pevent.ClipRectangle,pevent.Graphics);
1844 public bool ShowWindow ()
1846 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
1849 HighlightedIndex = owner.SelectedIndex;
1855 owner.OnDropDown (EventArgs.Empty);
1859 public void UpdateLastVisibleItem ()
1861 last_item = LastVisibleItem ();
1864 public void Scroll (int delta)
1866 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
1869 int max = vscrollbar_ctrl.Maximum;//- (page_size) + 1;
1871 int val = vscrollbar_ctrl.Value + delta;
1874 else if (val < vscrollbar_ctrl.Minimum)
1875 val = vscrollbar_ctrl.Minimum;
1876 vscrollbar_ctrl.Value = val;
1879 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
1881 if (owner.Items.Count == 0)
1884 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1889 private void VerticalScrollEvent (object sender, EventArgs e)
1891 if (top_item == vscrollbar_ctrl.Value)
1894 top_item = vscrollbar_ctrl.Value;
1895 UpdateLastVisibleItem ();
1899 protected override void WndProc(ref Message m) {
1900 if (m.Msg == (int)Msg.WM_SETFOCUS) {
1901 if (m.WParam != IntPtr.Zero) {
1902 XplatUI.SetFocus(m.WParam);
1905 base.WndProc (ref m);
1908 #endregion Private Methods