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);
671 protected override void OnDisplayMemberChanged (EventArgs e)
673 base.OnDisplayMemberChanged (e);
676 protected virtual void OnDrawItem (DrawItemEventArgs e)
678 if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
683 ThemeEngine.Current.DrawComboBoxItem (this, e);
686 protected virtual void OnDropDown (EventArgs e)
688 if (DropDown != null)
692 protected virtual void OnDropDownStyleChanged (EventArgs e)
694 if (DropDownStyleChanged != null)
695 DropDownStyleChanged (this, e);
698 protected override void OnFontChanged (EventArgs e)
700 base.OnFontChanged (e);
702 if (textbox_ctrl != null) {
703 textbox_ctrl.Font = Font;
706 combobox_info.item_height = FontHeight + 2;
710 protected override void OnForeColorChanged (EventArgs e)
712 base.OnForeColorChanged (e);
715 protected override void OnHandleCreated (EventArgs e)
717 base.OnHandleCreated (e);
719 if (listbox_ctrl != null) {
720 Controls.Add (listbox_ctrl);
723 if (textbox_ctrl != null) {
724 Controls.Add (textbox_ctrl);
730 protected override void OnHandleDestroyed (EventArgs e)
732 base.OnHandleDestroyed (e);
735 protected override void OnKeyPress (KeyPressEventArgs e)
740 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
742 if (MeasureItem != null)
743 MeasureItem (this, e);
746 protected override void OnParentBackColorChanged (EventArgs e)
748 base.OnParentBackColorChanged (e);
751 protected override void OnResize (EventArgs e)
757 protected override void OnSelectedIndexChanged (EventArgs e)
759 base.OnSelectedIndexChanged (e);
761 if (SelectedIndexChanged != null)
762 SelectedIndexChanged (this, e);
765 protected virtual void OnSelectedItemChanged (EventArgs e)
767 if (SelectedIndexChanged != null)
768 SelectedIndexChanged (this, e);
771 protected override void OnSelectedValueChanged (EventArgs e)
773 base.OnSelectedValueChanged (e);
776 protected virtual void OnSelectionChangeCommitted (EventArgs e)
778 if (SelectionChangeCommitted != null)
779 SelectionChangeCommitted (this, e);
782 protected override void RefreshItem (int index)
784 if (index < 0 || index >= Items.Count)
785 throw new ArgumentOutOfRangeException ("Index of out range");
787 if (draw_mode == DrawMode.OwnerDrawVariable) {
788 (Items.GetComboBoxItem (index)).ItemHeight = -1;
792 public void Select (int start, int lenght)
795 throw new ArgumentException ("Start cannot be less than zero");
798 throw new ArgumentException ("Start cannot be less than zero");
800 if (dropdown_style == ComboBoxStyle.DropDownList)
803 textbox_ctrl.Select (start, lenght);
806 public void SelectAll ()
808 if (dropdown_style == ComboBoxStyle.DropDownList)
811 textbox_ctrl.SelectAll ();
814 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
816 base.SetBoundsCore (x, y, width, height, specified);
819 protected override void SetItemCore (int index, object value)
821 if (index < 0 || index >= Items.Count)
824 Items[index] = value;
827 protected override void SetItemsCore (IList value)
829 Items.AddRange (value);
832 public override string ToString ()
834 return base.ToString () + ", Items.Count:" + Items.Count;
837 protected override void WndProc (ref Message m)
840 switch ((Msg) m.Msg) {
843 PaintEventArgs paint_event;
844 paint_event = XplatUI.PaintEventStart (Handle);
845 OnPaintCB (paint_event);
846 XplatUI.PaintEventEnd (Handle);
850 case Msg.WM_ERASEBKGND:
851 m.Result = (IntPtr) 1;
858 base.WndProc (ref m);
862 #endregion Public Methods
864 #region Private Methods
865 private void textbox_ctrl_KeyPress(object sender, KeyPressEventArgs e) {
\r
869 // Calcs the text area size
870 internal void CalcTextArea ()
872 combobox_info.textarea = ClientRectangle;
875 combobox_info.textarea.Height = ItemHeight + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
876 ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
877 // TODO: Does the +2 change at different font resolutions?
879 /* Edit area - minus decorations (text drawable area) */
880 combobox_info.textarea_drawable = combobox_info.textarea;
881 combobox_info.textarea_drawable.Y += ThemeEngine.Current.DrawComboBoxEditDecorationTop ();
882 combobox_info.textarea_drawable.X += ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
883 combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationBottom ();
884 combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationTop();
885 combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationRight ();
886 combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
888 /* Non-drawable area */
889 Region area = new Region (ClientRectangle);
890 area.Exclude (combobox_info.textarea);
891 RectangleF bounds = area.GetBounds (DeviceContext);
892 combobox_info.listbox_area = new Rectangle ((int)bounds.X, (int)bounds.Y,
893 (int)bounds.Width, (int)bounds.Height);
895 if (CBoxInfo.show_button) {
896 combobox_info.textarea_drawable.Width -= def_button_width;
898 combobox_info.button_rect = new Rectangle (combobox_info.textarea_drawable.X + combobox_info.textarea_drawable.Width,
899 combobox_info.textarea_drawable.Y, def_button_width, combobox_info.textarea_drawable.Height);
903 if (dropdown_style != ComboBoxStyle.DropDownList) { /* There is an edit control*/
904 if (textbox_ctrl != null) {
905 textbox_ctrl.Location = new Point (combobox_info.textarea_drawable.X, combobox_info.textarea_drawable.Y);
906 textbox_ctrl.Size = new Size (combobox_info.textarea_drawable.Width, combobox_info.textarea_drawable.Height);
910 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
911 listbox_ctrl.Location = new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
912 combobox_info.textarea.Height);
913 listbox_ctrl.CalcListBoxArea ();
917 private void CreateComboListBox ()
919 listbox_ctrl = new ComboListBox (this);
922 internal void Draw (Rectangle clip)
924 // No edit control, we paint the edit ourselfs
925 if (dropdown_style == ComboBoxStyle.DropDownList) {
926 DrawItemState state = DrawItemState.None;
927 Rectangle item_rect = combobox_info.textarea_drawable;
928 item_rect.Height = ItemHeight + 2;
930 if (has_focus == true) {
931 state = DrawItemState.Selected;
932 state |= DrawItemState.Focus;
935 OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
936 selected_index, state, ForeColor, BackColor));
939 if (clip.IntersectsWith (combobox_info.listbox_area) == true) {
940 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
941 combobox_info.listbox_area);
944 if (CBoxInfo.show_button) {
945 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
946 combobox_info.button_rect);
948 ThemeEngine.Current.CPDrawComboButton (DeviceContext,
949 combobox_info.button_rect, combobox_info.button_status);
952 ThemeEngine.Current.DrawComboBoxEditDecorations (DeviceContext, this, combobox_info.textarea);
955 internal void DropDownListBox ()
957 if (DropDownStyle == ComboBoxStyle.Simple)
960 if (listbox_ctrl == null) {
961 CreateComboListBox ();
964 listbox_ctrl.Location = PointToScreen (new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
965 combobox_info.textarea.Height));
967 if (listbox_ctrl.ShowWindow () == true) {
968 CBoxInfo.droppeddown = true;
971 combobox_info.button_status = ButtonState.Pushed;
972 if (dropdown_style == ComboBoxStyle.DropDownList) {
973 Invalidate (combobox_info.textarea_drawable);
977 internal void DropDownListBoxFinished ()
979 if (DropDownStyle == ComboBoxStyle.Simple)
982 combobox_info.button_status = ButtonState.Normal;
983 Invalidate (combobox_info.button_rect);
984 CBoxInfo.droppeddown = false;
988 private int FindStringCaseInsensitive (string search)
990 for (int i = 0; i < Items.Count; i++)
992 if (String.Compare (Items[i].ToString (), 0, search, 0, search.Length, true) == 0)
999 private void OnGotFocus (object sender, EventArgs e)
1005 private void OnLostFocus (object sender, EventArgs e)
1011 internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
1013 /* Click On button*/
1016 if (dropdown_style == ComboBoxStyle.DropDownList) {
1017 hit_rect = combobox_info.textarea;
1019 hit_rect = combobox_info.button_rect;
1022 if (hit_rect.Contains (e.X, e.Y)) {
1023 if (clicked == false) {
1027 listbox_ctrl.Hide ();
1028 DropDownListBoxFinished ();
1031 Invalidate (combobox_info.button_rect);
1035 internal virtual void OnMouseMoveCB (object sender, MouseEventArgs e)
1037 /* When there are no items, act as a regular button */
1038 if (clicked == true && Items.Count == 0 &&
1039 combobox_info.button_rect.Contains (e.X, e.Y) == false) {
1040 DropDownListBoxFinished ();
1044 internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
1046 /* Click on button*/
1047 if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
1048 DropDownListBoxFinished ();
1052 private void OnPaintCB (PaintEventArgs pevent)
1054 if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
1057 /* Copies memory drawing buffer to screen*/
1058 Draw (ClientRectangle);
1059 pevent.Graphics.DrawImage (ImageBuffer, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
1062 Paint (this, pevent);
1065 private void OnTextChangedEdit (object sender, EventArgs e)
1067 if (process_textchanged_event == false)
1070 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1075 listbox_ctrl.SetTopItem (item);
1076 listbox_ctrl.SetHighLightedItem (Items[item]);
1079 internal void SetControlText (string s)
1081 process_textchanged_event = false;
1082 textbox_ctrl.Text = s;
1083 process_textchanged_event = true;
1086 private void UpdatedItems ()
1088 if (dropdown_style != ComboBoxStyle.Simple)
1091 listbox_ctrl.UpdateLastVisibleItem ();
1092 listbox_ctrl.CalcListBoxArea ();
1093 listbox_ctrl.Refresh ();
1096 #endregion Private Methods
1100 ComboBox.ObjectCollection
1102 [ListBindableAttribute (false)]
1103 public class ObjectCollection : IList, ICollection, IEnumerable
1106 private ComboBox owner;
1107 internal ArrayList object_items = new ArrayList ();
1108 internal ArrayList combobox_items = new ArrayList ();
1110 public ObjectCollection (ComboBox owner)
1115 #region Public Properties
1116 public virtual int Count {
1117 get { return object_items.Count; }
1120 public virtual bool IsReadOnly {
1121 get { return false; }
1125 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1126 public virtual object this [int index] {
1128 if (index < 0 || index >= Count)
1129 throw new ArgumentOutOfRangeException ("Index of out range");
1131 return object_items[index];
1134 if (index < 0 || index >= Count)
1135 throw new ArgumentOutOfRangeException ("Index of out range");
1137 object_items[index] = value;
1141 bool ICollection.IsSynchronized {
1142 get { return false; }
1145 object ICollection.SyncRoot {
1146 get { return this; }
1149 bool IList.IsFixedSize {
1150 get { return false; }
1153 #endregion Public Properties
1155 #region Private Properties
1156 internal ArrayList ObjectItems {
1157 get { return object_items;}
1159 object_items = value;
1163 internal ArrayList ListBoxItems {
1164 get { return combobox_items;}
1166 combobox_items = value;
1169 #endregion Private Properties
1171 #region Public Methods
1172 public int Add (object item)
1176 idx = AddItem (item);
1177 owner.UpdatedItems ();
1181 public void AddRange (object[] items)
1183 foreach (object mi in items)
1186 owner.UpdatedItems ();
1189 public virtual void Clear ()
1191 owner.selected_index = -1;
1192 object_items.Clear ();
1193 combobox_items.Clear ();
1194 owner.UpdatedItems ();
1198 public virtual bool Contains (object obj)
1200 return object_items.Contains (obj);
1203 public void CopyTo (object[] dest, int arrayIndex)
1205 object_items.CopyTo (dest, arrayIndex);
1208 void ICollection.CopyTo (Array dest, int index)
1210 object_items.CopyTo (dest, index);
1213 public virtual IEnumerator GetEnumerator ()
1215 return object_items.GetEnumerator ();
1218 int IList.Add (object item)
1223 public virtual int IndexOf (object value)
1225 return object_items.IndexOf (value);
1228 public virtual void Insert (int index, object item)
1230 if (index < 0 || index >= Count)
1231 throw new ArgumentOutOfRangeException ("Index of out range");
1233 ObjectCollection new_items = new ObjectCollection (owner);
1234 object sel_item = owner.SelectedItem;
1236 owner.BeginUpdate ();
1238 for (int i = 0; i < index; i++) {
1239 new_items.AddItem (ObjectItems[i]);
1242 new_items.AddItem (item);
1244 for (int i = index; i < Count; i++){
1245 new_items.AddItem (ObjectItems[i]);
1248 ObjectItems = new_items.ObjectItems;
1249 ListBoxItems = new_items.ListBoxItems;
1251 if (sel_item != null) {
1252 int idx = IndexOf (sel_item);
1253 owner.selected_index = idx;
1254 owner.listbox_ctrl.SetHighLightedItem (owner.Items[idx]);
1257 owner.EndUpdate (); // Calls UpdatedItems
1260 public virtual void Remove (object value)
1262 if (IndexOf (value) == owner.SelectedIndex)
1263 owner.SelectedItem = null;
1265 RemoveAt (IndexOf (value));
1269 public virtual void RemoveAt (int index)
1271 if (index < 0 || index >= Count)
1272 throw new ArgumentOutOfRangeException ("Index of out range");
1274 if (index == owner.SelectedIndex)
1275 owner.SelectedItem = null;
1277 object_items.RemoveAt (index);
1278 combobox_items.RemoveAt (index);
1279 owner.UpdatedItems ();
1281 #endregion Public Methods
1283 #region Private Methods
1284 private int AddItem (object item)
1286 int cnt = object_items.Count;
1287 object_items.Add (item);
1288 combobox_items.Add (new ComboBox.ComboBoxItem (cnt));
1292 internal void AddRange (IList items)
1294 foreach (object mi in items)
1297 owner.UpdatedItems ();
1300 internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
1302 if (index < 0 || index >= Count)
1303 throw new ArgumentOutOfRangeException ("Index of out range");
1305 return (ComboBox.ComboBoxItem) combobox_items[index];
1308 internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
1310 if (index < 0 || index >= Count)
1311 throw new ArgumentOutOfRangeException ("Index of out range");
1313 combobox_items[index] = item;
1316 #endregion Private Methods
1322 internal class ComboListBox : Control
1324 private ComboBox owner;
1325 private VScrollBarLB vscrollbar_ctrl;
1326 private int top_item; /* First item that we show the in the current page */
1327 private int last_item; /* Last visible item */
1328 public object highlighted_item; /* Item that is currently selected */
1329 internal int page_size; /* Number of listbox items per page */
1330 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
1332 internal enum ItemNavigation
1342 class VScrollBarLB : VScrollBar
1344 public VScrollBarLB ()
1348 public void FireMouseDown (MouseEventArgs e)
1353 public void FireMouseUp (MouseEventArgs e)
1358 public void FireMouseMove (MouseEventArgs e)
1365 public ComboListBox (ComboBox owner) : base ()
1371 highlighted_item = null;
1373 MouseDown += new MouseEventHandler (OnMouseDownPUW);
1374 MouseUp += new MouseEventHandler (OnMouseUpPUW);
1375 MouseMove += new MouseEventHandler (OnMouseMovePUW);
1376 KeyDown += new KeyEventHandler (OnKeyDownPUW);
1377 Paint += new PaintEventHandler (OnPaintPUW);
1378 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
1379 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
1382 protected override CreateParams CreateParams
1385 CreateParams cp = base.CreateParams;
1386 if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
1387 cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
1388 cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);
1394 #region Private Methods
1396 protected override void CreateHandle ()
1398 base.CreateHandle ();
1401 // Calcs the listbox area
1402 internal void CalcListBoxArea ()
1405 int item_height = owner.ItemHeight;
1406 bool show_scrollbar = false;
1408 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1409 width = owner.CBoxInfo.listbox_area.Width;
1410 height = owner.CBoxInfo.listbox_area.Height;
1412 if (owner.IntegralHeight == true) {
1413 int remaining = (height -
1414 ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1415 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
1418 if (remaining > 0) {
1419 height -= remaining;
1423 else { // DropDown or DropDownList
1425 width = owner.DropDownWidth;
1426 int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
1428 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
1430 for (int i = 0; i < count; i++) {
1431 height += owner.GetItemHeight (i);
1435 height = (item_height - 2) * count;
1439 height += ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1440 height += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1443 if (owner.Items.Count <= owner.MaxDropDownItems) {
1445 /* Does not need vertical scrollbar*/
1446 if (vscrollbar_ctrl != null) {
1447 vscrollbar_ctrl.Visible = false;
1451 /* Need vertical scrollbar */
1452 if (vscrollbar_ctrl == null) {
1453 vscrollbar_ctrl = new VScrollBarLB ();
1454 vscrollbar_ctrl.Minimum = 0;
1455 vscrollbar_ctrl.SmallChange = 1;
1456 vscrollbar_ctrl.LargeChange = 1;
1457 vscrollbar_ctrl.Maximum = 0;
1458 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
1460 Controls.Add (vscrollbar_ctrl);
1463 vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
1464 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1466 vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
1467 ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
1469 vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
1470 show_scrollbar = vscrollbar_ctrl.Visible = true;
1474 Size = new Size (width, height);
1475 textarea_drawable = ClientRectangle;
1476 textarea_drawable.Width = width;
1477 textarea_drawable.Height = height;
1479 // Exclude decorations
1480 textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1481 textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1482 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1483 textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
1484 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
1485 textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
1487 if (vscrollbar_ctrl != null && show_scrollbar)
1488 textarea_drawable.Width -= vscrollbar_ctrl.Width;
1490 last_item = LastVisibleItem ();
1491 page_size = textarea_drawable.Height / (item_height - 2);
1494 private void Draw (Rectangle clip)
1496 DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
1497 (owner.BackColor), ClientRectangle);
1499 if (owner.Items.Count > 0) {
1500 Rectangle item_rect;
1501 DrawItemState state = DrawItemState.None;
1503 for (int i = top_item; i <= last_item; i++) {
1504 item_rect = GetItemDisplayRectangle (i, top_item);
1506 if (clip.IntersectsWith (item_rect) == false)
1510 state = DrawItemState.None;
1512 if (i == GetHighLightedIndex () ) {
1513 state |= DrawItemState.Selected;
1515 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
1516 state |= DrawItemState.Focus;
1520 owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
1521 i, state, owner.ForeColor, owner.BackColor));
1525 ThemeEngine.Current.DrawComboListBoxDecorations (DeviceContext, owner, ClientRectangle);
1528 public int GetHighLightedIndex ()
1530 return owner.Items.IndexOf (highlighted_item);
1533 public object GetHighLightedItem ()
1535 return highlighted_item;
1538 private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
1540 if (index < 0 || index >= owner.Items.Count)
1541 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
1543 Rectangle item_rect = new Rectangle ();
1544 int height = owner.GetItemHeight (index);
1546 item_rect.X = ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
1547 item_rect.Width = textarea_drawable.Width;
1548 item_rect.Y = 2 + ((height - 2) * (index - first_displayble));
1549 item_rect.Height = height;
1553 public void HideWindow ()
1555 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1560 highlighted_item = -1;
1561 owner.DropDownListBoxFinished ();
1564 private int IndexFromPointDisplayRectangle (int x, int y)
1566 for (int i = top_item; i <= last_item; i++) {
1567 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
1574 protected override bool IsInputKey (Keys keyData)
1576 return owner.IsInputKey (keyData);
1579 private int LastVisibleItem ()
1581 Rectangle item_rect;
1582 int top_y = textarea_drawable.Y + textarea_drawable.Height;
1585 for (i = top_item; i < owner.Items.Count; i++) {
1586 item_rect = GetItemDisplayRectangle (i, top_item);
1587 if (item_rect.Y + item_rect.Height > top_y) {
1594 private void NavigateItemVisually (ItemNavigation navigation)
1598 switch (navigation) {
1599 case ItemNavigation.Next: {
1600 if (GetHighLightedIndex () + 1 < owner.Items.Count) {
1602 if (GetHighLightedIndex () + 1 > last_item) {
1604 vscrollbar_ctrl.Value = top_item;
1606 item = GetHighLightedIndex () + 1;
1611 case ItemNavigation.Previous: {
1612 if (GetHighLightedIndex () > 0) {
1614 if (GetHighLightedIndex () - 1 < top_item) {
1616 vscrollbar_ctrl.Value = top_item;
1618 item = GetHighLightedIndex () - 1;
1623 case ItemNavigation.NextPage: {
1624 if (GetHighLightedIndex () + page_size - 1 >= owner.Items.Count) {
1625 top_item = owner.Items.Count - page_size;
1626 vscrollbar_ctrl.Value = top_item;
1627 item = owner.Items.Count - 1;
1630 if (GetHighLightedIndex () + page_size - 1 > last_item) {
1631 top_item = GetHighLightedIndex ();
1632 vscrollbar_ctrl.Value = GetHighLightedIndex ();
1635 item = GetHighLightedIndex () + page_size - 1;
1640 case ItemNavigation.PreviousPage: {
1642 /* Go to the first item*/
1643 if (GetHighLightedIndex () - (page_size - 1) <= 0) {
1646 vscrollbar_ctrl.Value = top_item;
1649 else { /* One page back */
1650 if (GetHighLightedIndex () - (page_size - 1) < top_item) {
1651 top_item = GetHighLightedIndex () - (page_size - 1);
1652 vscrollbar_ctrl.Value = top_item;
1655 item = GetHighLightedIndex () - (page_size - 1);
1666 SetHighLightedItem (owner.Items[item]);
1668 owner.OnSelectionChangeCommitted (new EventArgs ());
1670 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
1671 owner.SetControlText (owner.Items[item].ToString ());
1676 private void OnKeyDownPUW (object sender, KeyEventArgs e)
1678 switch (e.KeyCode) {
1680 NavigateItemVisually (ItemNavigation.Previous);
1684 NavigateItemVisually (ItemNavigation.Next);
1688 NavigateItemVisually (ItemNavigation.PreviousPage);
1692 NavigateItemVisually (ItemNavigation.NextPage);
1700 public void SetHighLightedItem (object item)
1702 Rectangle invalidate;
1704 if (GetHighLightedItem () == item)
1708 if (GetHighLightedIndex () != -1) {
1709 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1710 if (ClientRectangle.Contains (invalidate))
1711 Invalidate (invalidate);
1714 highlighted_item = item;
1716 if (highlighted_item != null) {
1718 invalidate = GetItemDisplayRectangle (GetHighLightedIndex (), top_item);
1719 if (ClientRectangle.Contains (invalidate))
1720 Invalidate (invalidate);
1725 public void SetTopItem (int item)
1728 UpdateLastVisibleItem ();
1732 private void OnMouseDownPUW (object sender, MouseEventArgs e)
1734 Rectangle scrollbar_screenrect;
1735 Point mouse_screen, scrollbar_screen;
1736 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1738 /* Click on an element ? */
1739 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1741 owner.SelectedIndex = index;
1742 SetHighLightedItem (owner.Items[index]);
1743 owner.OnSelectionChangeCommitted (new EventArgs ());
1748 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1751 /* Reroute event to scrollbar */
1752 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1753 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1754 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1755 scrollbar_screenrect.X = scrollbar_screen.X;
1756 scrollbar_screenrect.Y = scrollbar_screen.Y;
1758 if (scrollbar_screenrect.Contains (mouse_screen)){
1759 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1760 vscrollbar_ctrl.FireMouseDown (new MouseEventArgs (e.Button, e.Clicks,
1761 pnt_client.X, pnt_client.Y, e.Delta));
1762 } else { /* Click in a non-client area*/
1765 } else { /* Click in a non-client area*/
1770 private void OnMouseMovePUW (object sender, MouseEventArgs e)
1772 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1775 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1778 SetHighLightedItem (owner.Items[index]);
1782 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1785 /* Reroute event to scrollbar */
1786 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1787 Rectangle scrollbar_screenrect;
1788 Point mouse_screen, scrollbar_screen;
1789 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1791 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1792 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1793 scrollbar_screenrect.X = scrollbar_screen.X;
1794 scrollbar_screenrect.Y = scrollbar_screen.Y;
1796 if (scrollbar_screenrect.Contains (mouse_screen)){
1797 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1799 vscrollbar_ctrl.FireMouseMove (new MouseEventArgs (e.Button, e.Clicks,
1800 pnt_client.X, pnt_client.Y, e.Delta));
1805 private void OnMouseUpPUW (object sender, MouseEventArgs e)
1807 if (owner.DropDownStyle == ComboBoxStyle.Simple)
1810 /* Reroute event to scrollbar */
1811 Rectangle scrollbar_screenrect;
1812 Point mouse_screen, scrollbar_screen;
1813 mouse_screen = PointToScreen (new Point (e.X, e.Y));
1815 if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
1816 scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
1817 scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
1818 scrollbar_screenrect.X = scrollbar_screen.X;
1819 scrollbar_screenrect.Y = scrollbar_screen.Y;
1821 if (scrollbar_screenrect.Contains (mouse_screen)){
1822 Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
1824 vscrollbar_ctrl.FireMouseUp (new MouseEventArgs (e.Button, e.Clicks,
1825 pnt_client.X, pnt_client.Y, e.Delta));
1830 private void OnPaintPUW (Object o, PaintEventArgs pevent)
1832 if (Width <= 0 || Height <= 0 || Visible == false)
1835 Draw (pevent.ClipRectangle);
1836 pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
1839 public bool ShowWindow ()
1841 if (owner.DropDownStyle != ComboBoxStyle.Simple && owner.Items.Count == 0)
1845 SetHighLightedItem (owner.SelectedItem);
1850 if (owner.DropDownStyle != ComboBoxStyle.Simple) {
1856 if (owner.DropDown != null) {
1857 owner.DropDown (owner, EventArgs.Empty);
1863 public void UpdateLastVisibleItem ()
1865 last_item = LastVisibleItem ();
1869 private void VerticalScrollEvent (object sender, EventArgs e)
1871 top_item = vscrollbar_ctrl.Value;
1872 UpdateLastVisibleItem ();
1876 #endregion Private Methods