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-2005 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
34 using System.ComponentModel.Design;
35 using System.ComponentModel.Design.Serialization;
38 namespace System.Windows.Forms
41 [DefaultProperty("Items")]
42 [DefaultEvent("SelectedIndexChanged")]
43 [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, (string)null)]
44 public class ComboBox : ListControl
46 private DrawMode draw_mode;
47 private ComboBoxStyle dropdown_style;
48 private int dropdown_width;
49 private const int preferred_height = 20;
50 private int selected_index;
51 private object selected_item;
52 internal ObjectCollection items = null;
53 private bool suspend_ctrlupdate;
54 private int maxdrop_items;
55 private bool integral_height;
57 internal ComboBoxInfo combobox_info;
58 private readonly int def_button_width = 16;
60 private int max_length;
61 private ComboListBox listbox_ctrl;
62 private TextBox textbox_ctrl;
63 private bool process_textchanged_event;
64 private bool has_focus;
66 internal class ComboBoxInfo
68 internal int item_height; /* Item's height */
69 internal Rectangle textarea; /* Rectangle of the editable text area */
70 internal Rectangle textarea_drawable; /* Rectangle of the editable text area - decorations - button if present*/
71 internal Rectangle button_rect;
72 internal bool show_button; /* Is the DropDown button shown? */
73 internal ButtonState button_status; /* Drop button status */
74 internal Size listbox_size;
75 internal Rectangle listbox_area; /* ListBox area in Simple combox, not used in the rest */
76 internal bool droppeddown; /* Is the associated ListBox dropped down? */
78 public ComboBoxInfo ()
80 button_status = ButtonState.Normal;
87 internal class ComboBoxItem
90 internal int ItemHeight; /* Only used for OwnerDrawVariable */
92 public ComboBoxItem (int index)
101 items = new ObjectCollection (this);
104 combobox_info = new ComboBoxInfo ();
105 combobox_info.item_height = FontHeight + 2;
106 dropdown_style = (ComboBoxStyle)(-1);
107 DropDownStyle = ComboBoxStyle.DropDown;
108 BackColor = ThemeEngine.Current.ColorWindow;
109 draw_mode = DrawMode.Normal;
111 selected_item = null;
113 suspend_ctrlupdate = false;
117 integral_height = true;
118 process_textchanged_event = true;
122 MouseDown += new MouseEventHandler (OnMouseDownCB);
123 MouseUp += new MouseEventHandler (OnMouseUpCB);
124 MouseMove += new MouseEventHandler (OnMouseMoveCB);
125 GotFocus += new EventHandler (OnGotFocus);
126 LostFocus += new EventHandler (OnLostFocus);
132 [EditorBrowsable (EditorBrowsableState.Never)]
133 public new event EventHandler BackgroundImageChanged;
135 public event DrawItemEventHandler DrawItem;
136 public event EventHandler DropDown;
137 public event EventHandler DropDownStyleChanged;
138 public event MeasureItemEventHandler MeasureItem;
141 [EditorBrowsable (EditorBrowsableState.Never)]
142 public new event PaintEventHandler Paint;
144 public event EventHandler SelectedIndexChanged;
145 public event EventHandler SelectionChangeCommitted;
148 #region Public Properties
149 public override Color BackColor {
150 get { return base.BackColor; }
152 if (base.BackColor == value)
155 base.BackColor = value;
161 [EditorBrowsable (EditorBrowsableState.Never)]
162 public override Image BackgroundImage {
163 get { return base.BackgroundImage; }
165 if (base.BackgroundImage == value)
168 base.BackgroundImage = value;
170 if (BackgroundImageChanged != null)
171 BackgroundImageChanged (this, EventArgs.Empty);
177 protected override CreateParams CreateParams {
178 get { return base.CreateParams;}
181 protected override Size DefaultSize {
182 get { return new Size (121, PreferredHeight); }
185 [RefreshProperties(RefreshProperties.Repaint)]
186 [DefaultValue (DrawMode.Normal)]
187 public DrawMode DrawMode {
188 get { return draw_mode; }
191 if (!Enum.IsDefined (typeof (DrawMode), value))
192 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
194 if (draw_mode == value)
202 [DefaultValue (ComboBoxStyle.DropDown)]
203 [RefreshProperties(RefreshProperties.Repaint)]
204 public ComboBoxStyle DropDownStyle {
205 get { return dropdown_style; }
209 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
210 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
212 if (dropdown_style == value)
215 if (dropdown_style == ComboBoxStyle.Simple) {
216 if (listbox_ctrl != null) {
217 Controls.Remove (listbox_ctrl);
218 listbox_ctrl.Dispose ();
223 if (dropdown_style != ComboBoxStyle.DropDownList && value == ComboBoxStyle.DropDownList) {
224 if (textbox_ctrl != null) {
225 Controls.Remove (textbox_ctrl);
226 textbox_ctrl.Dispose ();
231 dropdown_style = value;
233 if (dropdown_style == ComboBoxStyle.Simple) {
234 CBoxInfo.show_button = false;
235 CreateComboListBox ();
237 if (IsHandleCreated == true) {
238 Controls.Add (listbox_ctrl);
242 CBoxInfo.show_button = true;
243 CBoxInfo.button_status = ButtonState.Normal;
246 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
247 textbox_ctrl = new TextBox ();
248 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
249 textbox_ctrl.KeyPress += new KeyPressEventHandler(textbox_ctrl_KeyPress);
251 if (IsHandleCreated == true) {
252 Controls.Add (textbox_ctrl);
256 if (DropDownStyleChanged != null)
257 DropDownStyleChanged (this, EventArgs.Empty);
264 public int DropDownWidth {
266 if (dropdown_width == -1)
269 return dropdown_width;
272 if (dropdown_width == value)
276 throw new ArgumentException ("The DropDownWidth value is less than one");
278 dropdown_width = value;
283 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
284 public bool DroppedDown {
286 if (dropdown_style == ComboBoxStyle.Simple)
289 return CBoxInfo.droppeddown;
292 if (dropdown_style == ComboBoxStyle.Simple)
300 listbox_ctrl.Hide ();
303 if (DropDown != null)
304 DropDown (this, EventArgs.Empty);
308 public override bool Focused {
309 get { return base.Focused; }
312 public override Color ForeColor {
313 get { return base.ForeColor; }
315 if (base.ForeColor == value)
318 base.ForeColor = value;
323 [DefaultValue (true)]
325 public bool IntegralHeight {
326 get { return integral_height; }
328 if (integral_height == value)
331 integral_height = value;
337 public virtual int ItemHeight {
338 get { return combobox_info.item_height; }
341 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
343 combobox_info.item_height = value;
349 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
351 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
352 public ComboBox.ObjectCollection Items {
353 get { return items; }
358 public int MaxDropDownItems {
359 get { return maxdrop_items; }
361 if (maxdrop_items == value)
364 maxdrop_items = value;
370 public int MaxLength {
371 get { return max_length; }
373 if (max_length == value)
378 if (dropdown_style != ComboBoxStyle.DropDownList) {
384 textbox_ctrl.MaxLength = value;
389 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
391 public int PreferredHeight {
392 get { return preferred_height; }
396 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
397 public override int SelectedIndex {
398 get { return selected_index; }
400 if (value < -2 || value >= Items.Count)
401 throw new ArgumentOutOfRangeException ("Index of out range");
403 if (selected_index == value)
406 selected_index = value;
408 if (dropdown_style != ComboBoxStyle.DropDownList) {
409 SetControlText (Items[selected_index].ToString ());
412 OnSelectedIndexChanged (new EventArgs ());
418 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
420 public object SelectedItem {
422 if (selected_index !=-1 && Items !=null && Items.Count > 0)
423 return Items[selected_index];
428 int index = Items.IndexOf (value);
433 if (selected_index == index)
436 selected_index = index;
438 if (dropdown_style != ComboBoxStyle.DropDownList) {
439 SetControlText (Items[selected_index].ToString ());
442 OnSelectedItemChanged (new EventArgs ());
448 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
449 public string SelectedText {
451 if (dropdown_style == ComboBoxStyle.DropDownList)
454 return textbox_ctrl.SelectedText;
457 if (dropdown_style == ComboBoxStyle.DropDownList) {
461 textbox_ctrl.SelectedText = value;
466 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
467 public int SelectionLength {
469 if (dropdown_style == ComboBoxStyle.DropDownList)
472 return textbox_ctrl.SelectionLength;
475 if (dropdown_style == ComboBoxStyle.DropDownList)
478 if (textbox_ctrl.SelectionLength == value)
481 textbox_ctrl.SelectionLength = value;
486 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
487 public int SelectionStart {
489 if (dropdown_style == ComboBoxStyle.DropDownList)
492 return textbox_ctrl.SelectionStart;
495 if (dropdown_style == ComboBoxStyle.DropDownList)
498 if (textbox_ctrl.SelectionStart == value)
501 textbox_ctrl.SelectionStart = value;
505 [DefaultValue (false)]
507 get { return sorted; }
519 public override string Text {
521 if (dropdown_style != ComboBoxStyle.DropDownList) {
522 if (textbox_ctrl != null) {
523 return textbox_ctrl.Text;
527 if (SelectedItem != null) {
528 return SelectedItem.ToString ();
539 int index = FindString (value);
542 SelectedIndex = index;
546 if (dropdown_style != ComboBoxStyle.DropDownList) {
547 textbox_ctrl.Text = value.ToString ();
552 #endregion Public Properties
554 #region Private Properties
555 internal ComboBoxInfo CBoxInfo {
556 get { return combobox_info; }
559 #endregion Private Properties
561 #region Public Methods
562 protected virtual void AddItemsCore (object[] value)
567 public void BeginUpdate ()
569 suspend_ctrlupdate = true;
572 protected override void Dispose (bool disposing)
574 if (disposing == true) {
575 if (listbox_ctrl != null) {
576 listbox_ctrl.Dispose ();
577 Controls.Remove (listbox_ctrl);
581 if (textbox_ctrl != null) {
582 Controls.Remove (textbox_ctrl);
583 textbox_ctrl.Dispose ();
588 base.Dispose (disposing);
591 public void EndUpdate ()
593 suspend_ctrlupdate = false;
597 public int FindString (string s)
599 return FindString (s, 0);
602 public int FindString (string s, int startIndex)
604 for (int i = startIndex; i < Items.Count; i++) {
605 if ((Items[i].ToString ()).StartsWith (s))
612 public int FindStringExact (string s)
614 return FindStringExact (s, 0);
617 public int FindStringExact (string s, int startIndex)
619 for (int i = startIndex; i < Items.Count; i++) {
620 if ((Items[i].ToString ()).Equals (s))
627 public int GetItemHeight (int index)
629 if (index < 0 || index >= Items.Count )
630 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
632 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated == true) {
634 if ((Items.GetComboBoxItem (index)).ItemHeight != -1) {
635 return (Items.GetComboBoxItem (index)).ItemHeight;
638 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
639 OnMeasureItem (args);
640 (Items.GetComboBoxItem (index)).ItemHeight = args.ItemHeight;
641 return args.ItemHeight;
647 protected override bool IsInputKey (Keys keyData)
661 protected override void OnBackColorChanged (EventArgs e)
663 base.OnBackColorChanged (e);
666 protected override void OnDataSourceChanged (EventArgs e)
668 base.OnDataSourceChanged (e);
670 if (DataSource != null)
673 BindDataItems (items);
676 protected override void OnDisplayMemberChanged (EventArgs e)
678 base.OnDisplayMemberChanged (e);
680 if (DataManager == null || !IsHandleCreated)
683 BindDataItems (items);
684 SelectedIndex = DataManager.Position;
687 protected virtual void OnDrawItem (DrawItemEventArgs e)
689 if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
694 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;
717 combobox_info.item_height = FontHeight + 2;
721 protected override void OnForeColorChanged (EventArgs e)
723 base.OnForeColorChanged (e);
726 protected override void OnHandleCreated (EventArgs e)
728 base.OnHandleCreated (e);
730 if (listbox_ctrl != null) {
731 Controls.Add (listbox_ctrl);
734 if (textbox_ctrl != null) {
735 Controls.Add (textbox_ctrl);
741 protected override void OnHandleDestroyed (EventArgs e)
743 base.OnHandleDestroyed (e);
746 protected override void OnKeyPress (KeyPressEventArgs e)
751 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
753 if (MeasureItem != null)
754 MeasureItem (this, e);
757 protected override void OnParentBackColorChanged (EventArgs e)
759 base.OnParentBackColorChanged (e);
762 protected override void OnResize (EventArgs e)
768 protected override void OnSelectedIndexChanged (EventArgs e)
770 base.OnSelectedIndexChanged (e);
772 if (SelectedIndexChanged != null)
773 SelectedIndexChanged (this, e);
776 protected virtual void OnSelectedItemChanged (EventArgs e)
778 if (SelectedIndexChanged != null)
779 SelectedIndexChanged (this, e);
782 protected override void OnSelectedValueChanged (EventArgs e)
784 base.OnSelectedValueChanged (e);
787 protected virtual void OnSelectionChangeCommitted (EventArgs e)
789 if (SelectionChangeCommitted != null)
790 SelectionChangeCommitted (this, e);
793 protected override void RefreshItem (int index)
795 if (index < 0 || index >= Items.Count)
796 throw new ArgumentOutOfRangeException ("Index of out range");
798 if (draw_mode == DrawMode.OwnerDrawVariable) {
799 (Items.GetComboBoxItem (index)).ItemHeight = -1;
803 public void Select (int start, int lenght)
806 throw new ArgumentException ("Start cannot be less than zero");
809 throw new ArgumentException ("Start cannot be less than zero");
811 if (dropdown_style == ComboBoxStyle.DropDownList)
814 textbox_ctrl.Select (start, lenght);
817 public void SelectAll ()
819 if (dropdown_style == ComboBoxStyle.DropDownList)
822 textbox_ctrl.SelectAll ();
825 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
827 base.SetBoundsCore (x, y, width, height, specified);
830 protected override void SetItemCore (int index, object value)
832 if (index < 0 || index >= Items.Count)
835 Items[index] = value;
838 protected override void SetItemsCore (IList value)
840 Items.AddRange (value);
843 public override string ToString ()
845 return base.ToString () + ", Items.Count:" + Items.Count;
848 protected override void WndProc (ref Message m)
851 switch ((Msg) m.Msg) {
854 PaintEventArgs paint_event;
855 paint_event = XplatUI.PaintEventStart (Handle);
856 OnPaintCB (paint_event);
857 XplatUI.PaintEventEnd (Handle);
861 case Msg.WM_ERASEBKGND:
862 m.Result = (IntPtr) 1;
869 base.WndProc (ref m);
873 #endregion Public Methods
875 #region Private Methods
876 private void textbox_ctrl_KeyPress(object sender, KeyPressEventArgs e) {
\r
880 // Calcs the text area size
881 internal void CalcTextArea ()
883 combobox_info.textarea = ClientRectangle;
886 combobox_info.textarea.Height = ItemHeight + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
887 ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
888 // TODO: Does the +2 change at different font resolutions?
890 /* Edit area - minus decorations (text drawable area) */
891 combobox_info.textarea_drawable = combobox_info.textarea;
892 combobox_info.textarea_drawable.Y += ThemeEngine.Current.DrawComboBoxEditDecorationTop ();
893 combobox_info.textarea_drawable.X += ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
894 combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationBottom ();
895 combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationTop();
896 combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationRight ();
897 combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
899 /* Non-drawable area */
900 Region area = new Region (ClientRectangle);
901 area.Exclude (combobox_info.textarea);
902 RectangleF bounds = area.GetBounds (DeviceContext);
903 combobox_info.listbox_area = new Rectangle ((int)bounds.X, (int)bounds.Y,
904 (int)bounds.Width, (int)bounds.Height);
906 if (CBoxInfo.show_button) {
907 combobox_info.textarea_drawable.Width -= def_button_width;
909 combobox_info.button_rect = new Rectangle (combobox_info.textarea_drawable.X + combobox_info.textarea_drawable.Width,
910 combobox_info.textarea_drawable.Y, def_button_width, combobox_info.textarea_drawable.Height);
914 if (dropdown_style != ComboBoxStyle.DropDownList) { /* There is an edit control*/
915 if (textbox_ctrl != null) {
916 textbox_ctrl.Location = new Point (combobox_info.textarea_drawable.X, combobox_info.textarea_drawable.Y);
917 textbox_ctrl.Size = new Size (combobox_info.textarea_drawable.Width, combobox_info.textarea_drawable.Height);
921 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
922 listbox_ctrl.Location = new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
923 combobox_info.textarea.Height);
924 listbox_ctrl.CalcListBoxArea ();
928 private void CreateComboListBox ()
930 listbox_ctrl = new ComboListBox (this);
933 internal void Draw (Rectangle clip, Graphics dc)
935 // No edit control, we paint the edit ourselfs
936 if (dropdown_style == ComboBoxStyle.DropDownList) {
937 DrawItemState state = DrawItemState.None;
938 Rectangle item_rect = combobox_info.textarea_drawable;
939 item_rect.Height = ItemHeight + 2;
941 if (has_focus == true) {
942 state = DrawItemState.Selected;
943 state |= DrawItemState.Focus;
946 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect,
947 selected_index, state, ForeColor, BackColor));
950 if (clip.IntersectsWith (combobox_info.listbox_area) == true) {
951 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
952 combobox_info.listbox_area);
955 if (CBoxInfo.show_button) {
956 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
957 combobox_info.button_rect);
959 ThemeEngine.Current.CPDrawComboButton (dc,
960 combobox_info.button_rect, combobox_info.button_status);
963 ThemeEngine.Current.DrawComboBoxEditDecorations (dc, this, combobox_info.textarea);
966 internal void DropDownListBox ()
968 if (DropDownStyle == ComboBoxStyle.Simple)
971 if (listbox_ctrl == null) {
972 CreateComboListBox ();
975 listbox_ctrl.Location = PointToScreen (new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
976 combobox_info.textarea.Height));
978 if (listbox_ctrl.ShowWindow () == true) {
979 CBoxInfo.droppeddown = true;
982 combobox_info.button_status = ButtonState.Pushed;
983 if (dropdown_style == ComboBoxStyle.DropDownList) {
984 Invalidate (combobox_info.textarea_drawable);
988 internal void DropDownListBoxFinished ()
990 if (DropDownStyle == ComboBoxStyle.Simple)
993 combobox_info.button_status = ButtonState.Normal;
994 Invalidate (combobox_info.button_rect);
995 CBoxInfo.droppeddown = false;
999 private int FindStringCaseInsensitive (string search)
1001 for (int i = 0; i < Items.Count; i++)
1003 if (String.Compare (Items[i].ToString (), 0, search, 0, search.Length, true) == 0)
1010 private void OnGotFocus (object sender, EventArgs e)
1016 private void OnLostFocus (object sender, EventArgs e)
1022 internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
1024 /* Click On button*/
1027 if (dropdown_style == ComboBoxStyle.DropDownList) {
1028 hit_rect = combobox_info.textarea;
1030 hit_rect = combobox_info.button_rect;
1033 if (hit_rect.Contains (e.X, e.Y)) {
1034 if (clicked == false) {
1038 listbox_ctrl.Hide ();
1039 DropDownListBoxFinished ();
1042 Invalidate (combobox_info.button_rect);
1046 internal virtual void OnMouseMoveCB (object sender, MouseEventArgs e)
1048 /* When there are no items, act as a regular button */
1049 if (clicked == true && Items.Count == 0 &&
1050 combobox_info.button_rect.Contains (e.X, e.Y) == false) {
1051 DropDownListBoxFinished ();
1055 internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
1057 /* Click on button*/
1058 if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
1059 DropDownListBoxFinished ();
1063 private void OnPaintCB (PaintEventArgs pevent)
1065 if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
1068 /* Copies memory drawing buffer to screen*/
1069 Draw (ClientRectangle, pevent.Graphics);
1072 Paint (this, pevent);
1075 private void OnTextChangedEdit (object sender, EventArgs e)
1077 if (process_textchanged_event == false)
1080 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1085 listbox_ctrl.SetTopItem (item);
1086 listbox_ctrl.SetHighLightedItem (Items[item]);
1089 internal void SetControlText (string s)
1091 process_textchanged_event = false;
1092 textbox_ctrl.Text = s;
1093 process_textchanged_event = true;
1096 private void UpdatedItems ()
1098 if (dropdown_style != ComboBoxStyle.Simple)
1101 listbox_ctrl.UpdateLastVisibleItem ();
1102 listbox_ctrl.CalcListBoxArea ();
1103 listbox_ctrl.Refresh ();
1106 #endregion Private Methods
1110 ComboBox.ObjectCollection
1112 [ListBindableAttribute (false)]
1113 public class ObjectCollection : IList, ICollection, IEnumerable
1116 private ComboBox owner;
1117 internal ArrayList object_items = new ArrayList ();
1118 internal ArrayList combobox_items = new ArrayList ();
1120 public ObjectCollection (ComboBox owner)
1125 #region Public Properties
1126 public virtual int Count {
1127 get { return object_items.Count; }
1130 public virtual bool IsReadOnly {
1131 get { return false; }
1135 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1136 public virtual object this [int index] {
1138 if (index < 0 || index >= Count)
1139 throw new ArgumentOutOfRangeException ("Index of out range");
1141 return object_items[index];
1144 if (index < 0 || index >= Count)
1145 throw new ArgumentOutOfRangeException ("Index of out range");
1147 object_items[index] = value;
1151 bool ICollection.IsSynchronized {
1152 get { return false; }
1155 object ICollection.SyncRoot {
1156 get { return this; }
1159 bool IList.IsFixedSize {
1160 get { return false; }
1163 #endregion Public Properties
1165 #region Private Properties
1166 internal ArrayList ObjectItems {
1167 get { return object_items;}
1169 object_items = value;
1173 internal ArrayList ListBoxItems {
1174 get { return combobox_items;}
1176 combobox_items = value;
1179 #endregion Private Properties
1181 #region Public Methods
1182 public int Add (object item)
1186 idx = AddItem (item);
1187 owner.UpdatedItems ();
1191 public void AddRange (object[] items)
1193 foreach (object mi in items)
1196 owner.UpdatedItems ();
1199 public virtual void Clear ()
1201 owner.selected_index = -1;
1202 object_items.Clear ();
1203 combobox_items.Clear ();
1204 owner.UpdatedItems ();
1208 public virtual bool Contains (object obj)
1210 return object_items.Contains (obj);
1213 public void CopyTo (object[] dest, int arrayIndex)
1215 object_items.CopyTo (dest, arrayIndex);
1218 void ICollection.CopyTo (Array dest, int index)
1220 object_items.CopyTo (dest, index);
1223 public virtual IEnumerator GetEnumerator ()
1225 return object_items.GetEnumerator ();
1228 int IList.Add (object item)
1233 public virtual int IndexOf (object value)
1235 return object_items.IndexOf (value);
1238 public virtual void Insert (int index, object item)
1240 if (index < 0 || index >= Count)
1241 throw new ArgumentOutOfRangeException ("Index of out range");
1243 ObjectCollection new_items = new ObjectCollection (owner);
1244 object sel_item = owner.SelectedItem;
1246 owner.BeginUpdate ();
1248 for (int i = 0; i < index; i++) {
1249 new_items.AddItem (ObjectItems[i]);
1252 new_items.AddItem (item);
1254 for (int i = index; i < Count; i++){
1255 new_items.AddItem (ObjectItems[i]);
1258 ObjectItems = new_items.ObjectItems;
1259 ListBoxItems = new_items.ListBoxItems;
1261 if (sel_item != null) {
1262 int idx = IndexOf (sel_item);
1263 owner.selected_index = idx;
1264 owner.listbox_ctrl.SetHighLightedItem (owner.Items[idx]);
1267 owner.EndUpdate (); // Calls UpdatedItems
1270 public virtual void Remove (object value)
1272 if (IndexOf (value) == owner.SelectedIndex)
1273 owner.SelectedItem = null;
1275 RemoveAt (IndexOf (value));
1279 public virtual void RemoveAt (int index)
1281 if (index < 0 || index >= Count)
1282 throw new ArgumentOutOfRangeException ("Index of out range");
1284 if (index == owner.SelectedIndex)
1285 owner.SelectedItem = null;
1287 object_items.RemoveAt (index);
1288 combobox_items.RemoveAt (index);
1289 owner.UpdatedItems ();
1291 #endregion Public Methods
1293 #region Private Methods
1294 private int AddItem (object item)
1296 int cnt = object_items.Count;
1297 object_items.Add (item);
1298 combobox_items.Add (new ComboBox.ComboBoxItem (cnt));
1302 internal void AddRange (IList items)
1304 foreach (object mi in items)
1307 owner.UpdatedItems ();
1310 internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
1312 if (index < 0 || index >= Count)
1313 throw new ArgumentOutOfRangeException ("Index of out range");
1315 return (ComboBox.ComboBoxItem) combobox_items[index];
1318 internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
1320 if (index < 0 || index >= Count)
1321 throw new ArgumentOutOfRangeException ("Index of out range");
1323 combobox_items[index] = item;
1326 #endregion Private Methods
1332 internal class ComboListBox : Control
1334 private ComboBox owner;
1335 private VScrollBarLB vscrollbar_ctrl;
1336 private int top_item; /* First item that we show the in the current page */
1337 private int last_item; /* Last visible item */
1338 public object highlighted_item; /* Item that is currently selected */
1339 internal int page_size; /* Number of listbox items per page */
1340 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1342 internal enum ItemNavigation
1352 class VScrollBarLB : VScrollBar
1354 public VScrollBarLB ()
1358 public void FireMouseDown (MouseEventArgs e)
1363 public void FireMouseUp (MouseEventArgs e)
1368 public void FireMouseMove (MouseEventArgs e)
1375 public ComboListBox (ComboBox owner) : base ()
1381 highlighted_item = null;
1383 MouseDown += new MouseEventHandler (OnMouseDownPUW);
1384 MouseUp += new MouseEventHandler (OnMouseUpPUW);
1385 MouseMove += new MouseEventHandler (OnMouseMovePUW);
1386 KeyDown += new KeyEventHandler (OnKeyDownPUW);
1387 Paint += new PaintEventHandler (OnPaintPUW);
1388 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1389 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1392 protected override CreateParams CreateParams
1395 CreateParams cp = base.CreateParams;
1396 if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
1397 cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
1398 cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);
1404 #region Private Methods
1406 protected override void CreateHandle ()
1408 base.CreateHandle ();
1411 // Calcs the listbox area
1412 internal void CalcListBoxArea ()
1415 int item_height = owner.ItemHeight;
1416 bool show_scrollbar = false;
1418 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1419 width = owner.CBoxInfo.listbox_area.Width;
1420 height = owner.CBoxInfo.listbox_area.Height;
1422 if (owner.IntegralHeight == true) {
1423 int remaining = (height -
1424 ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1425 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
1428 if (remaining > 0) {
1429 height -= remaining;
1433 else { // DropDown or DropDownList
1435 width = owner.DropDownWidth;
1436 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1438 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1440 for (int i = 0; i < count; i++) {
1441 height += owner.GetItemHeight (i);
1445 height = (item_height - 2) * count;
1449 height += ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1450 height += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1453 if (owner.Items.Count <= owner.MaxDropDownItems) {
1455 /* Does not need vertical scrollbar*/
1456 if (vscrollbar_ctrl != null) {
1457 vscrollbar_ctrl.Visible = false;
1461 /* Need vertical scrollbar */
1462 if (vscrollbar_ctrl == null) {
1463 vscrollbar_ctrl = new VScrollBarLB ();
1464 vscrollbar_ctrl.Minimum = 0;
1465 vscrollbar_ctrl.SmallChange = 1;
1466 vscrollbar_ctrl.LargeChange = 1;
1467 vscrollbar_ctrl.Maximum = 0;
1468 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1470 Controls.Add (vscrollbar_ctrl);
1473 vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1474 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1476 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
1477 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
1479 vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
1480 show_scrollbar = vscrollbar_ctrl.Visible = true;
1484 Size = new Size (width, height);
1485 textarea_drawable = ClientRectangle;
1486 textarea_drawable.Width = width;
1487 textarea_drawable.Height = height;
1489 // Exclude decorations
1490 textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1491 textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1492 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1493 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1494 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1495 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1497 if (vscrollbar_ctrl != null && show_scrollbar)
1498 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1500 last_item = LastVisibleItem ();
1501 page_size = textarea_drawable.Height / (item_height - 2);
1504 private void Draw (Rectangle clip, Graphics dc)
1506 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
1507 (owner.BackColor), ClientRectangle);
1509 if (owner.Items.Count > 0) {
1510 Rectangle item_rect;
1511 DrawItemState state = DrawItemState.None;
1513 for (int i = top_item; i <= last_item; i++) {
1514 item_rect = GetItemDisplayRectangle (i, top_item);
1516 if (clip.IntersectsWith (item_rect) == false)
1520 state = DrawItemState.None;
1522 if (i == GetHighLightedIndex () ) {
1523 state |= DrawItemState.Selected;
1525 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1526 state |= DrawItemState.Focus;
1530 owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
1531 i, state, owner.ForeColor, owner.BackColor));
1535 ThemeEngine.Current.DrawComboListBoxDecorations (dc, owner, ClientRectangle);
1538 public int GetHighLightedIndex ()
1540 return owner.Items.IndexOf (highlighted_item);
1543 public object GetHighLightedItem ()
1545 return highlighted_item;
1548 private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
1550 if (index < 0 || index >= owner.Items.Count)
1551 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1553 Rectangle item_rect = new Rectangle ();
1554 int height = owner.GetItemHeight (index);
1556 item_rect.X = ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1557 item_rect.Width = textarea_drawable.Width;
1558 item_rect.Y = 2 + ((height - 2) * (index - first_displayble));
1559 item_rect.Height = height;
1563 public void HideWindow ()
1565 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1570 highlighted_item = -1;
1571 owner.DropDownListBoxFinished ();
1574 private int IndexFromPointDisplayRectangle (int x, int y)
1576 for (int i = top_item; i <= last_item; i++) {
1577 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1584 protected override bool IsInputKey (Keys keyData)
1586 return owner.IsInputKey (keyData);
1589 private int LastVisibleItem ()
1591 Rectangle item_rect;
1592 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1595 for (i = top_item; i < owner.Items.Count; i++) {
1596 item_rect = GetItemDisplayRectangle (i, top_item);
1597 if (item_rect.Y + item_rect.Height > top_y) {
1604 private void NavigateItemVisually (ItemNavigation navigation)
1608 switch (navigation) {
1609 case ItemNavigation.Next: {
1610 if (GetHighLightedIndex () + 1 < owner.Items.Count) {
1612 if (GetHighLightedIndex () + 1 > last_item) {
1614 vscrollbar_ctrl.Value = top_item;
1616 item = GetHighLightedIndex () + 1;
1621 case ItemNavigation.Previous: {
1622 if (GetHighLightedIndex () > 0) {
1624 if (GetHighLightedIndex () - 1 < top_item) {
1626 vscrollbar_ctrl.Value = top_item;
1628 item = GetHighLightedIndex () - 1;
1633 case ItemNavigation.NextPage: {
1634 if (GetHighLightedIndex () + page_size - 1 >= owner.Items.Count) {
1635 top_item = owner.Items.Count - page_size;
1636 vscrollbar_ctrl.Value = top_item;
1637 item = owner.Items.Count - 1;
1640 if (GetHighLightedIndex () + page_size - 1 > last_item) {
1641 top_item = GetHighLightedIndex ();
1642 vscrollbar_ctrl.Value = GetHighLightedIndex ();
1645 item = GetHighLightedIndex () + page_size - 1;
1650 case ItemNavigation.PreviousPage: {
1652 /* Go to the first item*/
1653 if (GetHighLightedIndex () - (page_size - 1) <= 0) {
1656 vscrollbar_ctrl.Value = top_item;
1659 else { /* One page back */
1660 if (GetHighLightedIndex () - (page_size - 1) < top_item) {
1661 top_item = GetHighLightedIndex () - (page_size - 1);
1662 vscrollbar_ctrl.Value = top_item;
1665 item = GetHighLightedIndex () - (page_size - 1);
1676 SetHighLightedItem (owner.Items[item]);
1678 owner.OnSelectionChangeCommitted (new EventArgs ());
1680 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1681 owner.SetControlText (owner.Items[item].ToString ());
1686 private void OnKeyDownPUW (object sender, KeyEventArgs e)
1688 switch (e.KeyCode) {
1690 NavigateItemVisually (ItemNavigation.Previous);
1694 NavigateItemVisually (ItemNavigation.Next);
1698 NavigateItemVisually (ItemNavigation.PreviousPage);
1702 NavigateItemVisually (ItemNavigation.NextPage);
1710 public void SetHighLightedItem (object item)
1712 Rectangle invalidate;
1714 if (GetHighLightedItem () == item)
1718 if (GetHighLightedIndex () != -1) {
1719 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1720 if (ClientRectangle.Contains (invalidate))
1721 Invalidate (invalidate);
1724 highlighted_item = item;
1726 if (highlighted_item != null) {
1728 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1729 if (ClientRectangle.Contains (invalidate))
1730 Invalidate (invalidate);
1735 public void SetTopItem (int item)
1738 UpdateLastVisibleItem ();
1742 private void OnMouseDownPUW (object sender, MouseEventArgs e)
1744 Rectangle scrollbar_screenrect;
1745 Point mouse_screen, scrollbar_screen;
1746 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1748 /* Click on an element ? */
1749 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1751 owner.SelectedIndex = index;
1752 SetHighLightedItem (owner.Items[index]);
1753 owner.OnSelectionChangeCommitted (new EventArgs ());
1758 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1761 /* Reroute event to scrollbar */
1762 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1763 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1764 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1765 scrollbar_screenrect.X = scrollbar_screen.X;
1766 scrollbar_screenrect.Y = scrollbar_screen.Y;
1768 if (scrollbar_screenrect.Contains (mouse_screen)){
1769 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1770 vscrollbar_ctrl.FireMouseDown (new MouseEventArgs (e.Button, e.Clicks,
1771 pnt_client.X, pnt_client.Y, e.Delta));
1772 } else { /* Click in a non-client area*/
1775 } else { /* Click in a non-client area*/
1780 private void OnMouseMovePUW (object sender, MouseEventArgs e)
1782 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1785 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1788 SetHighLightedItem (owner.Items[index]);
1792 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1795 /* Reroute event to scrollbar */
1796 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1797 Rectangle scrollbar_screenrect;
1798 Point mouse_screen, scrollbar_screen;
1799 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1801 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1802 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1803 scrollbar_screenrect.X = scrollbar_screen.X;
1804 scrollbar_screenrect.Y = scrollbar_screen.Y;
1806 if (scrollbar_screenrect.Contains (mouse_screen)){
1807 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1809 vscrollbar_ctrl.FireMouseMove (new MouseEventArgs (e.Button, e.Clicks,
1810 pnt_client.X, pnt_client.Y, e.Delta));
1815 private void OnMouseUpPUW (object sender, MouseEventArgs e)
1817 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1820 /* Reroute event to scrollbar */
1821 Rectangle scrollbar_screenrect;
1822 Point mouse_screen, scrollbar_screen;
1823 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1825 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1826 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1827 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1828 scrollbar_screenrect.X = scrollbar_screen.X;
1829 scrollbar_screenrect.Y = scrollbar_screen.Y;
1831 if (scrollbar_screenrect.Contains (mouse_screen)){
1832 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1834 vscrollbar_ctrl.FireMouseUp (new MouseEventArgs (e.Button, e.Clicks,
1835 pnt_client.X, pnt_client.Y, e.Delta));
1840 private void OnPaintPUW (Object o, PaintEventArgs pevent)
1842 Draw (pevent.ClipRectangle,pevent.Graphics);
1845 public bool ShowWindow ()
1847 if (owner.DropDownStyle != ComboBoxStyle.Simple && owner.Items.Count == 0)
1851 SetHighLightedItem (owner.SelectedItem);
1856 if (owner.DropDownStyle != ComboBoxStyle.Simple) {
1862 if (owner.DropDown != null) {
1863 owner.DropDown (owner, EventArgs.Empty);
1869 public void UpdateLastVisibleItem ()
1871 last_item = LastVisibleItem ();
1875 private void VerticalScrollEvent (object sender, EventArgs e)
1877 top_item = vscrollbar_ctrl.Value;
1878 UpdateLastVisibleItem ();
1882 #endregion Private Methods