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)
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 (DeviceContext, Font, item_rect,
947 selected_index, state, ForeColor, BackColor));
950 if (clip.IntersectsWith (combobox_info.listbox_area) == true) {
951 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
952 combobox_info.listbox_area);
955 if (CBoxInfo.show_button) {
956 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
957 combobox_info.button_rect);
959 ThemeEngine.Current.CPDrawComboButton (DeviceContext,
960 combobox_info.button_rect, combobox_info.button_status);
963 ThemeEngine.Current.DrawComboBoxEditDecorations (DeviceContext, 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);
1070 pevent.Graphics.DrawImage (ImageBuffer, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
1073 Paint (this, pevent);
1076 private void OnTextChangedEdit (object sender, EventArgs e)
1078 if (process_textchanged_event == false)
1081 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1086 listbox_ctrl.SetTopItem (item);
1087 listbox_ctrl.SetHighLightedItem (Items[item]);
1090 internal void SetControlText (string s)
1092 process_textchanged_event = false;
1093 textbox_ctrl.Text = s;
1094 process_textchanged_event = true;
1097 private void UpdatedItems ()
1099 if (dropdown_style != ComboBoxStyle.Simple)
1102 listbox_ctrl.UpdateLastVisibleItem ();
1103 listbox_ctrl.CalcListBoxArea ();
1104 listbox_ctrl.Refresh ();
1107 #endregion Private Methods
1111 ComboBox.ObjectCollection
1113 [ListBindableAttribute (false)]
1114 public class ObjectCollection : IList, ICollection, IEnumerable
1117 private ComboBox owner;
1118 internal ArrayList object_items = new ArrayList ();
1119 internal ArrayList combobox_items = new ArrayList ();
1121 public ObjectCollection (ComboBox owner)
1126 #region Public Properties
1127 public virtual int Count {
1128 get { return object_items.Count; }
1131 public virtual bool IsReadOnly {
1132 get { return false; }
1136 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1137 public virtual object this [int index] {
1139 if (index < 0 || index >= Count)
1140 throw new ArgumentOutOfRangeException ("Index of out range");
1142 return object_items[index];
1145 if (index < 0 || index >= Count)
1146 throw new ArgumentOutOfRangeException ("Index of out range");
1148 object_items[index] = value;
1152 bool ICollection.IsSynchronized {
1153 get { return false; }
1156 object ICollection.SyncRoot {
1157 get { return this; }
1160 bool IList.IsFixedSize {
1161 get { return false; }
1164 #endregion Public Properties
1166 #region Private Properties
1167 internal ArrayList ObjectItems {
1168 get { return object_items;}
1170 object_items = value;
1174 internal ArrayList ListBoxItems {
1175 get { return combobox_items;}
1177 combobox_items = value;
1180 #endregion Private Properties
1182 #region Public Methods
1183 public int Add (object item)
1187 idx = AddItem (item);
1188 owner.UpdatedItems ();
1192 public void AddRange (object[] items)
1194 foreach (object mi in items)
1197 owner.UpdatedItems ();
1200 public virtual void Clear ()
1202 owner.selected_index = -1;
1203 object_items.Clear ();
1204 combobox_items.Clear ();
1205 owner.UpdatedItems ();
1209 public virtual bool Contains (object obj)
1211 return object_items.Contains (obj);
1214 public void CopyTo (object[] dest, int arrayIndex)
1216 object_items.CopyTo (dest, arrayIndex);
1219 void ICollection.CopyTo (Array dest, int index)
1221 object_items.CopyTo (dest, index);
1224 public virtual IEnumerator GetEnumerator ()
1226 return object_items.GetEnumerator ();
1229 int IList.Add (object item)
1234 public virtual int IndexOf (object value)
1236 return object_items.IndexOf (value);
1239 public virtual void Insert (int index, object item)
1241 if (index < 0 || index >= Count)
1242 throw new ArgumentOutOfRangeException ("Index of out range");
1244 ObjectCollection new_items = new ObjectCollection (owner);
1245 object sel_item = owner.SelectedItem;
1247 owner.BeginUpdate ();
1249 for (int i = 0; i < index; i++) {
1250 new_items.AddItem (ObjectItems[i]);
1253 new_items.AddItem (item);
1255 for (int i = index; i < Count; i++){
1256 new_items.AddItem (ObjectItems[i]);
1259 ObjectItems = new_items.ObjectItems;
1260 ListBoxItems = new_items.ListBoxItems;
1262 if (sel_item != null) {
1263 int idx = IndexOf (sel_item);
1264 owner.selected_index = idx;
1265 owner.listbox_ctrl.SetHighLightedItem (owner.Items[idx]);
1268 owner.EndUpdate (); // Calls UpdatedItems
1271 public virtual void Remove (object value)
1273 if (IndexOf (value) == owner.SelectedIndex)
1274 owner.SelectedItem = null;
1276 RemoveAt (IndexOf (value));
1280 public virtual void RemoveAt (int index)
1282 if (index < 0 || index >= Count)
1283 throw new ArgumentOutOfRangeException ("Index of out range");
1285 if (index == owner.SelectedIndex)
1286 owner.SelectedItem = null;
1288 object_items.RemoveAt (index);
1289 combobox_items.RemoveAt (index);
1290 owner.UpdatedItems ();
1292 #endregion Public Methods
1294 #region Private Methods
1295 private int AddItem (object item)
1297 int cnt = object_items.Count;
1298 object_items.Add (item);
1299 combobox_items.Add (new ComboBox.ComboBoxItem (cnt));
1303 internal void AddRange (IList items)
1305 foreach (object mi in items)
1308 owner.UpdatedItems ();
1311 internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
1313 if (index < 0 || index >= Count)
1314 throw new ArgumentOutOfRangeException ("Index of out range");
1316 return (ComboBox.ComboBoxItem) combobox_items[index];
1319 internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
1321 if (index < 0 || index >= Count)
1322 throw new ArgumentOutOfRangeException ("Index of out range");
1324 combobox_items[index] = item;
1327 #endregion Private Methods
1333 internal class ComboListBox : Control
1335 private ComboBox owner;
1336 private VScrollBarLB vscrollbar_ctrl;
1337 private int top_item; /* First item that we show the in the current page */
1338 private int last_item; /* Last visible item */
1339 public object highlighted_item; /* Item that is currently selected */
1340 internal int page_size; /* Number of listbox items per page */
1341 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1343 internal enum ItemNavigation
1353 class VScrollBarLB : VScrollBar
1355 public VScrollBarLB ()
1359 public void FireMouseDown (MouseEventArgs e)
1364 public void FireMouseUp (MouseEventArgs e)
1369 public void FireMouseMove (MouseEventArgs e)
1376 public ComboListBox (ComboBox owner) : base ()
1382 highlighted_item = null;
1384 MouseDown += new MouseEventHandler (OnMouseDownPUW);
1385 MouseUp += new MouseEventHandler (OnMouseUpPUW);
1386 MouseMove += new MouseEventHandler (OnMouseMovePUW);
1387 KeyDown += new KeyEventHandler (OnKeyDownPUW);
1388 Paint += new PaintEventHandler (OnPaintPUW);
1389 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1390 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1393 protected override CreateParams CreateParams
1396 CreateParams cp = base.CreateParams;
1397 if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
1398 cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
1399 cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);
1405 #region Private Methods
1407 protected override void CreateHandle ()
1409 base.CreateHandle ();
1412 // Calcs the listbox area
1413 internal void CalcListBoxArea ()
1416 int item_height = owner.ItemHeight;
1417 bool show_scrollbar = false;
1419 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1420 width = owner.CBoxInfo.listbox_area.Width;
1421 height = owner.CBoxInfo.listbox_area.Height;
1423 if (owner.IntegralHeight == true) {
1424 int remaining = (height -
1425 ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1426 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
1429 if (remaining > 0) {
1430 height -= remaining;
1434 else { // DropDown or DropDownList
1436 width = owner.DropDownWidth;
1437 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1439 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1441 for (int i = 0; i < count; i++) {
1442 height += owner.GetItemHeight (i);
1446 height = (item_height - 2) * count;
1450 height += ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1451 height += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1454 if (owner.Items.Count <= owner.MaxDropDownItems) {
1456 /* Does not need vertical scrollbar*/
1457 if (vscrollbar_ctrl != null) {
1458 vscrollbar_ctrl.Visible = false;
1462 /* Need vertical scrollbar */
1463 if (vscrollbar_ctrl == null) {
1464 vscrollbar_ctrl = new VScrollBarLB ();
1465 vscrollbar_ctrl.Minimum = 0;
1466 vscrollbar_ctrl.SmallChange = 1;
1467 vscrollbar_ctrl.LargeChange = 1;
1468 vscrollbar_ctrl.Maximum = 0;
1469 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1471 Controls.Add (vscrollbar_ctrl);
1474 vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1475 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1477 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
1478 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
1480 vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
1481 show_scrollbar = vscrollbar_ctrl.Visible = true;
1485 Size = new Size (width, height);
1486 textarea_drawable = ClientRectangle;
1487 textarea_drawable.Width = width;
1488 textarea_drawable.Height = height;
1490 // Exclude decorations
1491 textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1492 textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1493 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1494 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1495 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1496 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1498 if (vscrollbar_ctrl != null && show_scrollbar)
1499 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1501 last_item = LastVisibleItem ();
1502 page_size = textarea_drawable.Height / (item_height - 2);
1505 private void Draw (Rectangle clip)
1507 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
1508 (owner.BackColor), ClientRectangle);
1510 if (owner.Items.Count > 0) {
1511 Rectangle item_rect;
1512 DrawItemState state = DrawItemState.None;
1514 for (int i = top_item; i <= last_item; i++) {
1515 item_rect = GetItemDisplayRectangle (i, top_item);
1517 if (clip.IntersectsWith (item_rect) == false)
1521 state = DrawItemState.None;
1523 if (i == GetHighLightedIndex () ) {
1524 state |= DrawItemState.Selected;
1526 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1527 state |= DrawItemState.Focus;
1531 owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
1532 i, state, owner.ForeColor, owner.BackColor));
1536 ThemeEngine.Current.DrawComboListBoxDecorations (DeviceContext, owner, ClientRectangle);
1539 public int GetHighLightedIndex ()
1541 return owner.Items.IndexOf (highlighted_item);
1544 public object GetHighLightedItem ()
1546 return highlighted_item;
1549 private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
1551 if (index < 0 || index >= owner.Items.Count)
1552 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1554 Rectangle item_rect = new Rectangle ();
1555 int height = owner.GetItemHeight (index);
1557 item_rect.X = ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1558 item_rect.Width = textarea_drawable.Width;
1559 item_rect.Y = 2 + ((height - 2) * (index - first_displayble));
1560 item_rect.Height = height;
1564 public void HideWindow ()
1566 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1571 highlighted_item = -1;
1572 owner.DropDownListBoxFinished ();
1575 private int IndexFromPointDisplayRectangle (int x, int y)
1577 for (int i = top_item; i <= last_item; i++) {
1578 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1585 protected override bool IsInputKey (Keys keyData)
1587 return owner.IsInputKey (keyData);
1590 private int LastVisibleItem ()
1592 Rectangle item_rect;
1593 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1596 for (i = top_item; i < owner.Items.Count; i++) {
1597 item_rect = GetItemDisplayRectangle (i, top_item);
1598 if (item_rect.Y + item_rect.Height > top_y) {
1605 private void NavigateItemVisually (ItemNavigation navigation)
1609 switch (navigation) {
1610 case ItemNavigation.Next: {
1611 if (GetHighLightedIndex () + 1 < owner.Items.Count) {
1613 if (GetHighLightedIndex () + 1 > last_item) {
1615 vscrollbar_ctrl.Value = top_item;
1617 item = GetHighLightedIndex () + 1;
1622 case ItemNavigation.Previous: {
1623 if (GetHighLightedIndex () > 0) {
1625 if (GetHighLightedIndex () - 1 < top_item) {
1627 vscrollbar_ctrl.Value = top_item;
1629 item = GetHighLightedIndex () - 1;
1634 case ItemNavigation.NextPage: {
1635 if (GetHighLightedIndex () + page_size - 1 >= owner.Items.Count) {
1636 top_item = owner.Items.Count - page_size;
1637 vscrollbar_ctrl.Value = top_item;
1638 item = owner.Items.Count - 1;
1641 if (GetHighLightedIndex () + page_size - 1 > last_item) {
1642 top_item = GetHighLightedIndex ();
1643 vscrollbar_ctrl.Value = GetHighLightedIndex ();
1646 item = GetHighLightedIndex () + page_size - 1;
1651 case ItemNavigation.PreviousPage: {
1653 /* Go to the first item*/
1654 if (GetHighLightedIndex () - (page_size - 1) <= 0) {
1657 vscrollbar_ctrl.Value = top_item;
1660 else { /* One page back */
1661 if (GetHighLightedIndex () - (page_size - 1) < top_item) {
1662 top_item = GetHighLightedIndex () - (page_size - 1);
1663 vscrollbar_ctrl.Value = top_item;
1666 item = GetHighLightedIndex () - (page_size - 1);
1677 SetHighLightedItem (owner.Items[item]);
1679 owner.OnSelectionChangeCommitted (new EventArgs ());
1681 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1682 owner.SetControlText (owner.Items[item].ToString ());
1687 private void OnKeyDownPUW (object sender, KeyEventArgs e)
1689 switch (e.KeyCode) {
1691 NavigateItemVisually (ItemNavigation.Previous);
1695 NavigateItemVisually (ItemNavigation.Next);
1699 NavigateItemVisually (ItemNavigation.PreviousPage);
1703 NavigateItemVisually (ItemNavigation.NextPage);
1711 public void SetHighLightedItem (object item)
1713 Rectangle invalidate;
1715 if (GetHighLightedItem () == item)
1719 if (GetHighLightedIndex () != -1) {
1720 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1721 if (ClientRectangle.Contains (invalidate))
1722 Invalidate (invalidate);
1725 highlighted_item = item;
1727 if (highlighted_item != null) {
1729 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1730 if (ClientRectangle.Contains (invalidate))
1731 Invalidate (invalidate);
1736 public void SetTopItem (int item)
1739 UpdateLastVisibleItem ();
1743 private void OnMouseDownPUW (object sender, MouseEventArgs e)
1745 Rectangle scrollbar_screenrect;
1746 Point mouse_screen, scrollbar_screen;
1747 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1749 /* Click on an element ? */
1750 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1752 owner.SelectedIndex = index;
1753 SetHighLightedItem (owner.Items[index]);
1754 owner.OnSelectionChangeCommitted (new EventArgs ());
1759 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1762 /* Reroute event to scrollbar */
1763 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1764 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1765 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1766 scrollbar_screenrect.X = scrollbar_screen.X;
1767 scrollbar_screenrect.Y = scrollbar_screen.Y;
1769 if (scrollbar_screenrect.Contains (mouse_screen)){
1770 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1771 vscrollbar_ctrl.FireMouseDown (new MouseEventArgs (e.Button, e.Clicks,
1772 pnt_client.X, pnt_client.Y, e.Delta));
1773 } else { /* Click in a non-client area*/
1776 } else { /* Click in a non-client area*/
1781 private void OnMouseMovePUW (object sender, MouseEventArgs e)
1783 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1786 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1789 SetHighLightedItem (owner.Items[index]);
1793 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1796 /* Reroute event to scrollbar */
1797 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1798 Rectangle scrollbar_screenrect;
1799 Point mouse_screen, scrollbar_screen;
1800 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1802 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1803 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1804 scrollbar_screenrect.X = scrollbar_screen.X;
1805 scrollbar_screenrect.Y = scrollbar_screen.Y;
1807 if (scrollbar_screenrect.Contains (mouse_screen)){
1808 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1810 vscrollbar_ctrl.FireMouseMove (new MouseEventArgs (e.Button, e.Clicks,
1811 pnt_client.X, pnt_client.Y, e.Delta));
1816 private void OnMouseUpPUW (object sender, MouseEventArgs e)
1818 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1821 /* Reroute event to scrollbar */
1822 Rectangle scrollbar_screenrect;
1823 Point mouse_screen, scrollbar_screen;
1824 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1826 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1827 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1828 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1829 scrollbar_screenrect.X = scrollbar_screen.X;
1830 scrollbar_screenrect.Y = scrollbar_screen.Y;
1832 if (scrollbar_screenrect.Contains (mouse_screen)){
1833 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1835 vscrollbar_ctrl.FireMouseUp (new MouseEventArgs (e.Button, e.Clicks,
1836 pnt_client.X, pnt_client.Y, e.Delta));
1841 private void OnPaintPUW (Object o, PaintEventArgs pevent)
1843 if (Width <= 0 || Height <= 0 || Visible == false)
1846 Draw (pevent.ClipRectangle);
1847 pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
1850 public bool ShowWindow ()
1852 if (owner.DropDownStyle != ComboBoxStyle.Simple && owner.Items.Count == 0)
1856 SetHighLightedItem (owner.SelectedItem);
1861 if (owner.DropDownStyle != ComboBoxStyle.Simple) {
1867 if (owner.DropDown != null) {
1868 owner.DropDown (owner, EventArgs.Empty);
1874 public void UpdateLastVisibleItem ()
1876 last_item = LastVisibleItem ();
1880 private void VerticalScrollEvent (object sender, EventArgs e)
1882 top_item = vscrollbar_ctrl.Value;
1883 UpdateLastVisibleItem ();
1887 #endregion Private Methods