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 object selected_item = null;
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 KeyDown +=new KeyEventHandler(OnKeyDownCB);
106 [EditorBrowsable (EditorBrowsableState.Never)]
107 public new event EventHandler BackgroundImageChanged {
108 add { base.BackgroundImageChanged += value; }
109 remove { base.BackgroundImageChanged -= value; }
112 public event DrawItemEventHandler DrawItem;
113 public event EventHandler DropDown;
114 public event EventHandler DropDownStyleChanged;
115 public event MeasureItemEventHandler MeasureItem;
118 [EditorBrowsable (EditorBrowsableState.Never)]
119 public new event PaintEventHandler Paint {
120 add { base.Paint += value; }
121 remove { base.Paint -= value; }
124 public event EventHandler SelectedIndexChanged;
125 public event EventHandler SelectionChangeCommitted;
128 #region Public Properties
129 public override Color BackColor {
130 get { return base.BackColor; }
132 if (base.BackColor == value)
135 base.BackColor = value;
141 [EditorBrowsable (EditorBrowsableState.Never)]
142 public override Image BackgroundImage {
143 get { return base.BackgroundImage; }
145 if (base.BackgroundImage == value)
148 base.BackgroundImage = value;
153 protected override CreateParams CreateParams {
154 get { return base.CreateParams;}
157 protected override Size DefaultSize {
158 get { return new Size (121, 21); }
161 [RefreshProperties(RefreshProperties.Repaint)]
162 [DefaultValue (DrawMode.Normal)]
163 public DrawMode DrawMode {
164 get { return draw_mode; }
167 if (!Enum.IsDefined (typeof (DrawMode), value))
168 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
170 if (draw_mode == value)
173 if (draw_mode == DrawMode.OwnerDrawVariable)
176 if (draw_mode == DrawMode.OwnerDrawVariable)
177 item_heights = new Hashtable ();
182 [DefaultValue (ComboBoxStyle.DropDown)]
183 [RefreshProperties(RefreshProperties.Repaint)]
184 public ComboBoxStyle DropDownStyle {
185 get { return dropdown_style; }
189 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
190 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
192 if (dropdown_style == value)
195 if (dropdown_style == ComboBoxStyle.Simple) {
196 if (listbox_ctrl != null) {
197 Controls.RemoveImplicit (listbox_ctrl);
198 listbox_ctrl.Dispose ();
203 dropdown_style = value;
205 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
206 Controls.RemoveImplicit (textbox_ctrl);
207 textbox_ctrl.Dispose ();
211 if (dropdown_style == ComboBoxStyle.Simple) {
212 show_dropdown_button = false;
214 CreateComboListBox ();
217 Controls.AddImplicit (listbox_ctrl);
219 show_dropdown_button = true;
220 button_state = ButtonState.Normal;
223 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
224 textbox_ctrl = new ComboTextBox (this);
225 if (selected_item != null)
226 textbox_ctrl.Text = GetItemText (selected_item);
227 textbox_ctrl.BorderStyle = BorderStyle.None;
228 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
230 if (IsHandleCreated == true) {
231 Controls.AddImplicit (textbox_ctrl);
235 OnDropDownStyleChanged (EventArgs.Empty);
243 public int DropDownWidth {
245 if (dropdown_width == -1)
248 return dropdown_width;
251 if (dropdown_width == value)
255 throw new ArgumentException ("The DropDownWidth value is less than one");
257 dropdown_width = value;
262 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
263 public bool DroppedDown {
265 if (dropdown_style == ComboBoxStyle.Simple)
271 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
277 listbox_ctrl.Hide ();
281 public override bool Focused {
282 get { return base.Focused; }
285 public override Color ForeColor {
286 get { return base.ForeColor; }
288 if (base.ForeColor == value)
291 base.ForeColor = value;
296 [DefaultValue (true)]
298 public bool IntegralHeight {
299 get { return integral_height; }
301 if (integral_height == value)
304 integral_height = value;
311 public int ItemHeight {
313 if (item_height == -1) {
314 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
315 item_height = (int) sz.Height;
321 throw new ArgumentException ("The item height value is less than zero");
323 item_height_specified = true;
332 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
334 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
335 public ComboBox.ObjectCollection Items {
336 get { return items; }
341 public int MaxDropDownItems {
342 get { return maxdrop_items; }
344 if (maxdrop_items == value)
347 maxdrop_items = value;
353 public int MaxLength {
354 get { return max_length; }
356 if (max_length == value)
361 if (dropdown_style != ComboBoxStyle.DropDownList) {
367 textbox_ctrl.MaxLength = value;
372 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
374 public int PreferredHeight {
376 return ItemHeight + 5;
381 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
382 public override int SelectedIndex {
383 get { return Items.IndexOf (selected_item); }
385 if (value <= -2 || value >= Items.Count)
386 throw new ArgumentOutOfRangeException ("Index of out range");
390 item = Items [value];
397 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
399 public object SelectedItem {
400 get { return selected_item; }
402 if (selected_item == value)
405 selected_item = value;
407 if (dropdown_style != ComboBoxStyle.DropDownList) {
408 if (selected_item == null)
411 SetControlText (GetItemText (selected_item));
414 OnSelectedValueChanged (new EventArgs ());
415 OnSelectedIndexChanged (new EventArgs ());
416 OnSelectedItemChanged (new EventArgs ());
417 if (DropDownStyle == ComboBoxStyle.DropDownList)
420 if (listbox_ctrl != null)
421 listbox_ctrl.HighlightedItem = value;
426 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
427 public string SelectedText {
429 if (dropdown_style == ComboBoxStyle.DropDownList)
432 return textbox_ctrl.SelectedText;
435 if (dropdown_style == ComboBoxStyle.DropDownList)
438 textbox_ctrl.SelectedText = value;
443 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
444 public int SelectionLength {
446 if (dropdown_style == ComboBoxStyle.DropDownList)
449 return textbox_ctrl.SelectionLength;
452 if (dropdown_style == ComboBoxStyle.DropDownList)
455 if (textbox_ctrl.SelectionLength == value)
458 textbox_ctrl.SelectionLength = value;
463 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
464 public int SelectionStart {
466 if (dropdown_style == ComboBoxStyle.DropDownList)
469 return textbox_ctrl.SelectionStart;
472 if (dropdown_style == ComboBoxStyle.DropDownList)
475 if (textbox_ctrl.SelectionStart == value)
478 textbox_ctrl.SelectionStart = value;
482 [DefaultValue (false)]
484 get { return sorted; }
496 public override string Text {
498 if (dropdown_style != ComboBoxStyle.DropDownList) {
499 if (textbox_ctrl != null) {
500 return textbox_ctrl.Text;
504 if (SelectedItem != null)
505 return GetItemText (SelectedItem);
515 int index = FindString (value);
518 SelectedIndex = index;
522 if (dropdown_style != ComboBoxStyle.DropDownList)
523 textbox_ctrl.Text = GetItemText (value);
527 #endregion Public Properties
529 #region Public Methods
530 protected virtual void AddItemsCore (object[] value)
535 public void BeginUpdate ()
537 suspend_ctrlupdate = true;
540 protected override void Dispose (bool disposing)
543 if (listbox_ctrl != null) {
544 listbox_ctrl.Dispose ();
545 Controls.RemoveImplicit (listbox_ctrl);
549 if (textbox_ctrl != null) {
550 Controls.RemoveImplicit (textbox_ctrl);
551 textbox_ctrl.Dispose ();
556 base.Dispose (disposing);
559 public void EndUpdate ()
561 suspend_ctrlupdate = false;
566 public int FindString (string s)
568 return FindString (s, -1);
571 public int FindString (string s, int startIndex)
573 if (Items.Count == 0)
574 return -1; // No exception throwing if empty
576 if (startIndex < -1 || startIndex >= Items.Count - 1)
577 throw new ArgumentOutOfRangeException ("Index of out range");
580 for (int i = startIndex; i < Items.Count; i++) {
581 if ((GetItemText (Items[i])).StartsWith (s))
588 public int FindStringExact (string s)
590 return FindStringExact (s, -1);
593 public int FindStringExact (string s, int startIndex)
595 if (Items.Count == 0)
596 return -1; // No exception throwing if empty
598 if (startIndex < -1 || startIndex >= Items.Count - 1)
599 throw new ArgumentOutOfRangeException ("Index of out range");
602 for (int i = startIndex; i < Items.Count; i++) {
603 if ((GetItemText (Items[i])).Equals (s))
610 public int GetItemHeight (int index)
612 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
614 if (index < 0 || index >= Items.Count )
615 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
617 object item = Items [index];
618 if (item_heights.Contains (item))
619 return (int) item_heights [item];
621 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
622 OnMeasureItem (args);
623 item_heights [item] = args.ItemHeight;
624 return args.ItemHeight;
630 protected override bool IsInputKey (Keys keyData)
646 protected override void OnBackColorChanged (EventArgs e)
648 base.OnBackColorChanged (e);
651 protected override void OnDataSourceChanged (EventArgs e)
653 base.OnDataSourceChanged (e);
656 if (DataSource == null || DataManager == null) {
660 SelectedIndex = DataManager.Position;
664 protected override void OnDisplayMemberChanged (EventArgs e)
666 base.OnDisplayMemberChanged (e);
668 if (DataManager == null || !IsHandleCreated)
672 SelectedIndex = DataManager.Position;
675 protected virtual void OnDrawItem (DrawItemEventArgs e)
678 case DrawMode.OwnerDrawFixed:
679 case DrawMode.OwnerDrawVariable:
680 if (DrawItem != null)
684 ThemeEngine.Current.DrawComboBoxItem (this, e);
689 protected virtual void OnDropDown (EventArgs e)
691 if (DropDown != null)
695 protected virtual void OnDropDownStyleChanged (EventArgs e)
697 if (DropDownStyleChanged != null)
698 DropDownStyleChanged (this, e);
701 protected override void OnFontChanged (EventArgs e)
703 base.OnFontChanged (e);
705 if (textbox_ctrl != null)
706 textbox_ctrl.Font = Font;
708 if (!item_height_specified) {
709 SizeF sz = DeviceContext.MeasureString ("The quick brown Fox", Font);
710 item_height = (int) sz.Height;
719 protected override void OnForeColorChanged (EventArgs e)
721 base.OnForeColorChanged (e);
724 [EditorBrowsable(EditorBrowsableState.Advanced)]
725 protected override void OnGotFocus (EventArgs e)
727 if (dropdown_style == ComboBoxStyle.DropDownList) {
728 // We draw DDL styles manually, so they require a
729 // refresh to have their selection drawn
733 if (textbox_ctrl != null) {
734 textbox_ctrl.SetSelectable (false);
735 textbox_ctrl.ActivateCaret (true);
736 textbox_ctrl.ShowSelection = true;
737 textbox_ctrl.SelectAll ();
743 [EditorBrowsable(EditorBrowsableState.Advanced)]
744 protected override void OnLostFocus (EventArgs e)
746 if (dropdown_style == ComboBoxStyle.DropDownList) {
747 // We draw DDL styles manually, so they require a
748 // refresh to have their selection drawn
752 if (listbox_ctrl != null && dropped_down) {
753 listbox_ctrl.HideWindow ();
756 if (textbox_ctrl != null) {
757 textbox_ctrl.SetSelectable (true);
758 textbox_ctrl.ActivateCaret (false);
759 textbox_ctrl.ShowSelection = false;
762 base.OnLostFocus (e);
765 protected override void OnHandleCreated (EventArgs e)
767 base.OnHandleCreated (e);
769 if (listbox_ctrl != null)
770 Controls.AddImplicit (listbox_ctrl);
772 if (textbox_ctrl != null)
773 Controls.AddImplicit (textbox_ctrl);
778 protected override void OnHandleDestroyed (EventArgs e)
780 base.OnHandleDestroyed (e);
783 protected override void OnKeyPress (KeyPressEventArgs e)
785 // int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex);
787 // SelectedIndex = index;
792 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
794 if (MeasureItem != null)
795 MeasureItem (this, e);
798 protected override void OnParentBackColorChanged (EventArgs e)
800 base.OnParentBackColorChanged (e);
803 protected override void OnResize (EventArgs e)
806 if (listbox_ctrl != null)
807 listbox_ctrl.CalcListBoxArea ();
810 protected override void OnSelectedIndexChanged (EventArgs e)
812 base.OnSelectedIndexChanged (e);
814 if (SelectedIndexChanged != null)
815 SelectedIndexChanged (this, e);
818 protected virtual void OnSelectedItemChanged (EventArgs e)
823 protected override void OnSelectedValueChanged (EventArgs e)
825 base.OnSelectedValueChanged (e);
828 protected virtual void OnSelectionChangeCommitted (EventArgs e)
830 if (SelectionChangeCommitted != null)
831 SelectionChangeCommitted (this, e);
834 protected override void RefreshItem (int index)
836 if (index < 0 || index >= Items.Count)
837 throw new ArgumentOutOfRangeException ("Index of out range");
839 if (draw_mode == DrawMode.OwnerDrawVariable)
840 item_heights.Remove (Items [index]);
843 public void Select (int start, int length)
846 throw new ArgumentException ("Start cannot be less than zero");
849 throw new ArgumentException ("length cannot be less than zero");
851 if (dropdown_style == ComboBoxStyle.DropDownList)
854 textbox_ctrl.Select (start, length);
857 public void SelectAll ()
859 if (dropdown_style == ComboBoxStyle.DropDownList)
862 if (textbox_ctrl != null) {
863 textbox_ctrl.ShowSelection = true;
864 textbox_ctrl.SelectAll ();
868 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
870 if ((specified & BoundsSpecified.Height) != 0) {
871 requested_height = height;
873 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
874 if (IntegralHeight) {
875 int border = ThemeEngine.Current.Border3DSize.Height;
876 int lb_height = height - PreferredHeight - 2;
877 if (lb_height - 2 * border > ItemHeight) {
878 int partial = (lb_height - 2 * border) % ItemHeight;
881 height = PreferredHeight;
884 height = PreferredHeight;
887 base.SetBoundsCore (x, y, width, height, specified);
890 protected override void SetItemCore (int index, object value)
892 if (index < 0 || index >= Items.Count)
895 Items[index] = value;
898 protected override void SetItemsCore (IList value)
903 Items.AddRange (value);
909 public override string ToString ()
911 return base.ToString () + ", Items.Count:" + Items.Count;
914 protected override void WndProc (ref Message m)
916 switch ((Msg) m.Msg) {
919 Keys keys = (Keys) m.WParam.ToInt32 ();
920 if (keys == Keys.Up || keys == Keys.Down)
922 goto case Msg.WM_CHAR;
924 if (textbox_ctrl != null)
925 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
927 case Msg.WM_MOUSE_LEAVE:
928 Point location = PointToClient (Control.MousePosition);
929 if (ClientRectangle.Contains (location))
935 base.WndProc (ref m);
938 #endregion Public Methods
940 #region Private Methods
942 internal override bool InternalCapture {
943 get { return Capture; }
949 int border = ThemeEngine.Current.Border3DSize.Width;
951 text_area = ClientRectangle;
952 text_area.Height = PreferredHeight;
954 listbox_area = ClientRectangle;
955 listbox_area.Y = text_area.Bottom + 3;
956 listbox_area.Height -= (text_area.Height + 2);
958 Rectangle prev_button_area = button_area;
960 if (DropDownStyle == ComboBoxStyle.Simple)
961 button_area = Rectangle.Empty;
963 button_area = text_area;
964 button_area.X = text_area.Right - button_width - border;
965 button_area.Y = text_area.Y + border;
966 button_area.Width = button_width;
967 button_area.Height = text_area.Height - 2 * border;
970 if (button_area != prev_button_area) {
971 prev_button_area.Y -= border;
972 prev_button_area.Width += border;
973 prev_button_area.Height += 2 * border;
974 Invalidate (prev_button_area);
975 Invalidate (button_area);
978 if (textbox_ctrl != null) {
979 textbox_ctrl.Location = new Point (text_area.X + border, text_area.Y + border);
980 textbox_ctrl.Width = text_area.Width - button_area.Width - border * 2;
981 textbox_ctrl.Height = text_area.Height - border * 2;
984 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
985 listbox_ctrl.Location = listbox_area.Location;
986 listbox_ctrl.CalcListBoxArea ();
990 private void CreateComboListBox ()
992 listbox_ctrl = new ComboListBox (this);
993 if (selected_item != null)
994 listbox_ctrl.HighlightedItem = selected_item;
997 internal void Draw (Rectangle clip, Graphics dc)
999 Theme theme = ThemeEngine.Current;
1001 if (DropDownStyle == ComboBoxStyle.Simple)
1002 dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
1004 if (clip.IntersectsWith (text_area))
1005 ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
1007 int border = theme.Border3DSize.Width;
1009 // No edit control, we paint the edit ourselves
1010 if (dropdown_style == ComboBoxStyle.DropDownList) {
1011 DrawItemState state = DrawItemState.None;
1012 Rectangle item_rect = text_area;
1013 item_rect.X += border;
1014 item_rect.Y += border;
1015 item_rect.Width -= (button_area.Width + 2 * border);
1016 item_rect.Height -= 2 * border;
1019 state = DrawItemState.Selected;
1020 state |= DrawItemState.Focus;
1023 state |= DrawItemState.ComboBoxEdit;
1024 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
1027 if (show_dropdown_button) {
1028 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1031 button_state = ButtonState.Inactive;
1033 theme.CPDrawComboButton (dc, button_area, button_state);
1037 internal void DropDownListBox ()
1039 if (DropDownStyle == ComboBoxStyle.Simple)
1042 if (listbox_ctrl == null)
1043 CreateComboListBox ();
1045 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1047 if (listbox_ctrl.ShowWindow ())
1048 dropped_down = true;
1050 button_state = ButtonState.Pushed;
1051 if (dropdown_style == ComboBoxStyle.DropDownList)
1052 Invalidate (text_area);
1054 OnDropDown (EventArgs.Empty);
1057 internal void DropDownListBoxFinished ()
1059 if (DropDownStyle == ComboBoxStyle.Simple)
1062 button_state = ButtonState.Normal;
1063 Invalidate (button_area);
1064 dropped_down = false;
1067 private int FindStringCaseInsensitive (string search)
1069 if (search.Length == 0) {
1073 for (int i = 0; i < Items.Count; i++)
1075 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1082 internal int FindStringCaseInsensitive (string search, int start_index)
1084 if (search.Length == 0) {
1088 for (int i = 0; i < Items.Count; i++) {
1089 int index = (i + start_index) % Items.Count;
1090 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1097 private void OnKeyDownCB(object sender, KeyEventArgs e)
1099 if (Items.Count == 0)
1105 SelectedIndex = Math.Max(SelectedIndex-1, 0);
1109 SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
1113 if (listbox_ctrl != null)
1114 SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
1118 if (listbox_ctrl != null)
1119 SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
1127 void OnMouseDownCB (object sender, MouseEventArgs e)
1130 if (DropDownStyle == ComboBoxStyle.DropDownList)
1131 area = ClientRectangle;
1135 if (area.Contains (e.X, e.Y)) {
1137 Invalidate (button_area);
1143 void OnMouseMoveCB (object sender, MouseEventArgs e)
1145 if (DropDownStyle == ComboBoxStyle.Simple)
1148 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1149 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1150 if (listbox_ctrl.ClientRectangle.Contains (location))
1151 listbox_ctrl.Capture = true;
1155 void OnMouseUpCB (object sender, MouseEventArgs e)
1158 OnClick (EventArgs.Empty);
1161 listbox_ctrl.Capture = true;
1164 internal override void OnPaintInternal (PaintEventArgs pevent)
1166 if (suspend_ctrlupdate)
1169 Draw (ClientRectangle, pevent.Graphics);
1172 private void OnTextChangedEdit (object sender, EventArgs e)
1174 if (process_textchanged_event == false)
1177 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1182 // TODO: THIS IS BROKEN-ISH
1183 // I don't think we should hilight, and setting the top item does weirdness
1184 // when there is no scrollbar
1186 if (listbox_ctrl != null) {
1187 listbox_ctrl.SetTopItem (item);
1188 listbox_ctrl.HighlightedItem = Items [item];
1191 base.Text = textbox_ctrl.Text;
1194 internal void SetControlText (string s)
1196 process_textchanged_event = false;
1197 textbox_ctrl.Text = s;
1198 process_textchanged_event = true;
1201 void UpdateBounds ()
1203 if (requested_height != -1)
1204 SetBoundsCore (0, 0, 0, requested_height, BoundsSpecified.Height);
1207 private void UpdatedItems ()
1209 if (listbox_ctrl != null) {
1210 listbox_ctrl.UpdateLastVisibleItem ();
1211 listbox_ctrl.CalcListBoxArea ();
1212 listbox_ctrl.Refresh ();
1216 #endregion Private Methods
1218 [ListBindableAttribute (false)]
1219 public class ObjectCollection : IList, ICollection, IEnumerable
1222 private ComboBox owner;
1223 internal ArrayList object_items = new ArrayList ();
1225 public ObjectCollection (ComboBox owner)
1230 #region Public Properties
1232 get { return object_items.Count; }
1235 public bool IsReadOnly {
1236 get { return false; }
1240 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1241 public virtual object this [int index] {
1243 if (index < 0 || index >= Count)
1244 throw new ArgumentOutOfRangeException ("Index of out range");
1246 return object_items[index];
1249 if (index < 0 || index >= Count)
1250 throw new ArgumentOutOfRangeException ("Index of out range");
1252 object_items[index] = value;
1256 bool ICollection.IsSynchronized {
1257 get { return false; }
1260 object ICollection.SyncRoot {
1261 get { return this; }
1264 bool IList.IsFixedSize {
1265 get { return false; }
1268 #endregion Public Properties
1270 #region Private Properties
1271 internal ArrayList ObjectItems {
1272 get { return object_items;}
1274 object_items = value;
1278 #endregion Private Properties
1280 #region Public Methods
1281 public int Add (object item)
1285 idx = AddItem (item);
1286 owner.UpdatedItems ();
1290 public void AddRange (object[] items)
1292 foreach (object mi in items)
1295 owner.UpdatedItems ();
1298 public void Clear ()
1300 owner.selected_item = null;
1301 object_items.Clear ();
1302 owner.UpdatedItems ();
1306 public bool Contains (object obj)
1308 return object_items.Contains (obj);
1311 public void CopyTo (object[] dest, int arrayIndex)
1313 object_items.CopyTo (dest, arrayIndex);
1316 void ICollection.CopyTo (Array dest, int index)
1318 object_items.CopyTo (dest, index);
1321 public IEnumerator GetEnumerator ()
1323 return object_items.GetEnumerator ();
1326 int IList.Add (object item)
1331 public int IndexOf (object value)
1333 return object_items.IndexOf (value);
1336 public void Insert (int index, object item)
1338 if (index < 0 || index > Count)
1339 throw new ArgumentOutOfRangeException ("Index of out range");
1341 owner.BeginUpdate ();
1343 object_items.Insert (index, item);
1345 owner.EndUpdate (); // Calls UpdatedItems
1348 public void Remove (object value)
1350 if (IndexOf (value) == owner.SelectedIndex)
1351 owner.SelectedItem = null;
1353 RemoveAt (IndexOf (value));
1357 public void RemoveAt (int index)
1359 if (index < 0 || index >= Count)
1360 throw new ArgumentOutOfRangeException ("Index of out range");
1362 if (index == owner.SelectedIndex)
1363 owner.SelectedItem = null;
1365 object_items.RemoveAt (index);
1366 owner.UpdatedItems ();
1368 #endregion Public Methods
1370 #region Private Methods
1371 private int AddItem (object item)
1373 int cnt = object_items.Count;
1374 object_items.Add (item);
1378 internal void AddRange (IList items)
1380 foreach (object mi in items)
1383 owner.UpdatedItems ();
1386 #endregion Private Methods
1389 internal class ComboTextBox : TextBox {
1391 private ComboBox owner;
1393 public ComboTextBox (ComboBox owner)
1396 ShowSelection = false;
1399 internal void SetSelectable (bool selectable)
1401 SetStyle (ControlStyles.Selectable, selectable);
1404 internal void ActivateCaret (bool active)
1407 document.CaretHasFocus ();
1409 document.CaretLostFocus ();
1412 internal override void OnGotFocusInternal (EventArgs e)
1414 owner.Select (false, true);
1417 internal override void OnLostFocusInternal (EventArgs e)
1419 owner.Select (false, true);
1423 internal class ComboListBox : Control
1425 private ComboBox owner;
1426 private VScrollBarLB vscrollbar_ctrl;
1427 private int top_item; /* First item that we show the in the current page */
1428 private int last_item; /* Last visible item */
1429 internal int page_size; /* Number of listbox items per page */
1430 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1432 internal enum ItemNavigation
1442 class VScrollBarLB : VScrollBar
1444 public VScrollBarLB ()
1448 internal override bool InternalCapture {
1449 get { return Capture; }
1453 public bool FireMouseDown (MouseEventArgs e)
1456 e = TranslateEvent (e);
1457 if (ClientRectangle.Contains (e.X, e.Y)) {
1465 public void FireMouseUp (MouseEventArgs e)
1468 e = TranslateEvent (e);
1469 if (ClientRectangle.Contains (e.X, e.Y))
1474 public void FireMouseMove (MouseEventArgs e)
1477 e = TranslateEvent (e);
1478 if (ClientRectangle.Contains (e.X, e.Y))
1483 MouseEventArgs TranslateEvent (MouseEventArgs e)
1485 Point loc = PointToClient (Control.MousePosition);
1486 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
1490 public ComboListBox (ComboBox owner)
1497 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
1499 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1500 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1502 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1503 InternalBorderStyle = BorderStyle.Fixed3D;
1505 InternalBorderStyle = BorderStyle.FixedSingle;
1508 protected override CreateParams CreateParams
1511 CreateParams cp = base.CreateParams;
1512 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
1515 cp.Style ^= (int) WindowStyles.WS_CHILD;
1516 cp.Style |= (int) WindowStyles.WS_POPUP;
1517 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
1522 protected override void Select (bool directed, bool forward)
1524 // Do nothing, we never want to be selected
1527 internal override bool InternalCapture {
1538 switch (border_style) {
1539 case BorderStyle.Fixed3D:
1540 return ThemeEngine.Current.Border3DSize.Width;
1542 return ThemeEngine.Current.BorderSize.Width;
1547 #region Private Methods
1548 // Calcs the listbox area
1549 internal void CalcListBoxArea ()
1552 bool show_scrollbar = false;
1554 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1555 Rectangle area = owner.listbox_area;
1557 height = area.Height;
1559 else { // DropDown or DropDownList
1561 width = owner.DropDownWidth;
1562 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1564 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1566 for (int i = 0; i < count; i++) {
1567 height += owner.GetItemHeight (i);
1571 height = owner.ItemHeight * count;
1575 if (owner.Items.Count <= owner.MaxDropDownItems) {
1576 if (vscrollbar_ctrl != null)
1577 vscrollbar_ctrl.Visible = false;
1579 /* Need vertical scrollbar */
1580 if (vscrollbar_ctrl == null) {
1581 vscrollbar_ctrl = new VScrollBarLB ();
1582 vscrollbar_ctrl.Minimum = 0;
1583 vscrollbar_ctrl.SmallChange = 1;
1584 vscrollbar_ctrl.LargeChange = 1;
1585 vscrollbar_ctrl.Maximum = 0;
1586 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1587 Controls.AddImplicit (vscrollbar_ctrl);
1590 vscrollbar_ctrl.Height = height - 2 * BorderWidth;
1592 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - BorderWidth - 1, 0);
1594 vscrollbar_ctrl.Maximum = owner.Items.Count - (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items);
1595 show_scrollbar = vscrollbar_ctrl.Visible = true;
1597 int hli = HighlightedIndex;
1599 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
1600 vscrollbar_ctrl.Value = hli;
1604 Size = new Size (width, height);
1605 textarea_drawable = ClientRectangle;
1606 textarea_drawable.Width = width;
1607 textarea_drawable.Height = height;
1609 if (vscrollbar_ctrl != null && show_scrollbar)
1610 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1612 last_item = LastVisibleItem ();
1613 page_size = textarea_drawable.Height / owner.ItemHeight;
1616 private void Draw (Rectangle clip, Graphics dc)
1618 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
1620 if (owner.Items.Count > 0) {
1622 for (int i = top_item; i <= last_item; i++) {
1623 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
1625 if (!clip.IntersectsWith (item_rect))
1628 DrawItemState state = DrawItemState.None;
1630 if (i == HighlightedIndex) {
1631 state |= DrawItemState.Selected;
1633 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1634 state |= DrawItemState.Focus;
1638 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
1639 i, state, owner.ForeColor, owner.BackColor));
1644 public int HighlightedIndex {
1645 get { return owner.Items.IndexOf (highlighted_item); }
1649 item = owner.Items [value];
1650 HighlightedItem = item;
1654 object highlighted_item = null;
1656 public object HighlightedItem {
1657 get { return highlighted_item; }
1659 if (highlighted_item == value)
1662 int index = owner.Items.IndexOf (highlighted_item);
1664 Invalidate (GetItemDisplayRectangle (index, top_item));
1665 highlighted_item = value;
1666 index = owner.Items.IndexOf (highlighted_item);
1668 Invalidate (GetItemDisplayRectangle (index, top_item));
1672 private Rectangle GetItemDisplayRectangle (int index, int top_index)
1674 if (index < 0 || index >= owner.Items.Count)
1675 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1677 Rectangle item_rect = new Rectangle ();
1678 int height = owner.GetItemHeight (index);
1681 item_rect.Width = textarea_drawable.Width;
1682 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1684 for (int i = top_index; i < index; i++)
1685 item_rect.Y += owner.GetItemHeight (i);
1687 item_rect.Y = height * (index - top_index);
1689 item_rect.Height = height;
1693 public void HideWindow ()
1695 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1700 owner.DropDownListBoxFinished ();
1703 private int IndexFromPointDisplayRectangle (int x, int y)
1705 for (int i = top_item; i <= last_item; i++) {
1706 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1713 private int LastVisibleItem ()
1715 Rectangle item_rect;
1716 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1719 for (i = top_item; i < owner.Items.Count; i++) {
1720 item_rect = GetItemDisplayRectangle (i, top_item);
1721 if (item_rect.Y + item_rect.Height > top_y) {
1728 public void SetTopItem (int item)
1730 if (top_item == item)
1733 UpdateLastVisibleItem ();
1737 protected override void OnMouseDown (MouseEventArgs e)
1739 if (vscrollbar_ctrl != null)
1740 vscrollbar_ctrl.FireMouseDown (e);
1743 protected override void OnMouseMove (MouseEventArgs e)
1745 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1748 Point pt = PointToClient (Control.MousePosition);
1749 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
1752 HighlightedIndex = index;
1756 if (vscrollbar_ctrl != null)
1757 vscrollbar_ctrl.FireMouseMove (e);
1760 protected override void OnMouseUp (MouseEventArgs e)
1762 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1765 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.FireMouseDown (e))
1768 owner.OnSelectionChangeCommitted (new EventArgs ());
1769 owner.SelectedIndex = index;
1770 HighlightedIndex = index;
1774 if (vscrollbar_ctrl != null)
1775 vscrollbar_ctrl.FireMouseUp (e);
1778 internal override void OnPaintInternal (PaintEventArgs pevent)
1780 Draw (pevent.ClipRectangle,pevent.Graphics);
1783 public bool ShowWindow ()
1785 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
1788 HighlightedIndex = owner.SelectedIndex;
1794 owner.OnDropDown (EventArgs.Empty);
1798 public void UpdateLastVisibleItem ()
1800 last_item = LastVisibleItem ();
1803 private void Scroll (int delta)
1805 if (delta == 0 || !vscrollbar_ctrl.Visible)
1808 int max = vscrollbar_ctrl.Maximum;//- (page_size) + 1;
1810 int val = vscrollbar_ctrl.Value + delta;
1813 else if (val < vscrollbar_ctrl.Minimum)
1814 val = vscrollbar_ctrl.Minimum;
1815 vscrollbar_ctrl.Value = val;
1818 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
1820 if (owner.Items.Count == 0)
1823 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1828 private void VerticalScrollEvent (object sender, EventArgs e)
1830 if (top_item == vscrollbar_ctrl.Value)
1833 top_item = vscrollbar_ctrl.Value;
1834 UpdateLastVisibleItem ();
1838 protected override void WndProc(ref Message m) {
1839 if (m.Msg == (int)Msg.WM_SETFOCUS) {
1840 if (m.WParam != IntPtr.Zero) {
1841 XplatUI.SetFocus(m.WParam);
1844 base.WndProc (ref m);
1847 #endregion Private Methods