1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
24 // Daniel Nauck (dna(at)mono-project(dot)de)
30 using System.Collections;
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
35 using System.Collections.Generic;
36 using System.Runtime.InteropServices;
39 namespace System.Windows.Forms {
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
44 [Designer ("System.Windows.Forms.Design.TextBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
46 public class TextBox : TextBoxBase {
48 private ContextMenu menu;
49 private MenuItem undo;
51 private MenuItem copy;
52 private MenuItem paste;
53 private MenuItem delete;
54 private MenuItem select_all;
57 private bool use_system_password_char;
58 private AutoCompleteStringCollection auto_complete_custom_source;
59 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
60 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
61 private AutoCompleteListBox auto_complete_listbox;
62 private string auto_complete_original_text;
63 private ComboBox auto_complete_cb_source;
65 #endregion // Variables
67 #region Public Constructors
70 scrollbars = RichTextBoxScrollBars.None;
71 alignment = HorizontalAlignment.Left;
72 this.LostFocus +=new EventHandler(TextBox_LostFocus);
73 this.RightToLeftChanged += new EventHandler (TextBox_RightToLeftChanged);
75 MouseWheel += new MouseEventHandler (TextBox_MouseWheel);
78 BackColor = SystemColors.Window;
79 ForeColor = SystemColors.WindowText;
80 backcolor_set = false;
82 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
83 SetStyle (ControlStyles.FixedHeight, true);
85 undo = new MenuItem(Locale.GetText("&Undo"));
86 cut = new MenuItem(Locale.GetText("Cu&t"));
87 copy = new MenuItem(Locale.GetText("&Copy"));
88 paste = new MenuItem(Locale.GetText("&Paste"));
89 delete = new MenuItem(Locale.GetText("&Delete"));
90 select_all = new MenuItem(Locale.GetText("Select &All"));
92 menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
95 menu.Popup += new EventHandler(menu_Popup);
96 undo.Click += new EventHandler(undo_Click);
97 cut.Click += new EventHandler(cut_Click);
98 copy.Click += new EventHandler(copy_Click);
99 paste.Click += new EventHandler(paste_Click);
100 delete.Click += new EventHandler(delete_Click);
101 select_all.Click += new EventHandler(select_all_Click);
103 document.multiline = false;
106 #endregion // Public Constructors
108 #region Private & Internal Methods
110 void TextBox_RightToLeftChanged (object sender, EventArgs e)
115 private void TextBox_LostFocus (object sender, EventArgs e) {
117 document.InvalidateSelectionArea ();
119 if (auto_complete_listbox != null && auto_complete_listbox.Visible)
120 auto_complete_listbox.HideListBox (false);
125 private void TextBox_MouseWheel (object o, MouseEventArgs args)
127 if (auto_complete_listbox == null || !auto_complete_listbox.Visible)
130 int lines = args.Delta / 120;
131 auto_complete_listbox.Scroll (-lines);
134 private void ShowAutoCompleteListBox ()
136 if (auto_complete_mode == AutoCompleteMode.None || auto_complete_source == AutoCompleteSource.None)
140 // We only support CustomSource *and* Suggest mode by now
143 if (auto_complete_mode != AutoCompleteMode.Suggest)
147 if (auto_complete_cb_source == null)
148 source = auto_complete_custom_source;
150 source = auto_complete_cb_source.Items;
152 if (auto_complete_source != AutoCompleteSource.CustomSource ||
153 source == null || source.Count == 0)
156 if (Text.Length == 0) {
157 if (auto_complete_listbox != null)
158 auto_complete_listbox.HideListBox (false);
162 if (auto_complete_listbox == null)
163 auto_complete_listbox = new AutoCompleteListBox (this);
166 auto_complete_listbox.Items.Clear ();
168 for (int i = 0; i < source.Count; i++) {
169 string item_text = auto_complete_cb_source == null ? auto_complete_custom_source [i] :
170 auto_complete_cb_source.GetItemText (auto_complete_cb_source.Items [i]);
171 if (item_text.StartsWith (text, StringComparison.CurrentCultureIgnoreCase))
172 auto_complete_listbox.Items.Add (item_text);
175 IList<string> matches = auto_complete_listbox.Items;
176 if ((matches.Count == 0) ||
177 (matches.Count == 1 && matches [0].Equals (text, StringComparison.CurrentCultureIgnoreCase))) { // Exact single match
179 if (auto_complete_listbox.Visible)
180 auto_complete_listbox.HideListBox (false);
184 // Show or update auto complete listbox contents
185 auto_complete_listbox.Location = PointToScreen (new Point (0, Height));
186 auto_complete_listbox.ShowListBox ();
189 internal ComboBox AutoCompleteInternalSource {
191 return auto_complete_cb_source;
194 auto_complete_cb_source = value;
198 void NavigateAutoCompleteList (Keys key)
200 int index = auto_complete_listbox.HighlightedIndex;
202 auto_complete_original_text = Text;
208 index = auto_complete_listbox.Items.Count - 1;
212 if (index >= auto_complete_listbox.Items.Count)
217 index = auto_complete_listbox.Items.Count - 1;
221 index -= auto_complete_listbox.page_size - 1;
229 else if (index == auto_complete_listbox.Items.Count - 1)
232 index += auto_complete_listbox.page_size - 1;
233 if (index >= auto_complete_listbox.Items.Count)
234 index = auto_complete_listbox.Items.Count - 1;
241 auto_complete_listbox.HighlightedIndex = index;
242 Text = index == -1 ? auto_complete_original_text : auto_complete_listbox.Items [index];
246 private void UpdateAlignment ()
248 HorizontalAlignment new_alignment = alignment;
249 RightToLeft rtol = GetInheritedRtoL ();
251 if (rtol == RightToLeft.Yes) {
252 if (new_alignment == HorizontalAlignment.Left)
253 new_alignment = HorizontalAlignment.Right;
254 else if (new_alignment == HorizontalAlignment.Right)
255 new_alignment = HorizontalAlignment.Left;
258 document.alignment = new_alignment;
260 // MS word-wraps if alignment isn't left
262 if (alignment != HorizontalAlignment.Left) {
263 document.Wrap = true;
265 document.Wrap = word_wrap;
269 for (int i = 1; i <= document.Lines; i++) {
270 document.GetLine (i).Alignment = new_alignment;
273 document.RecalculateDocument (CreateGraphicsInternal ());
275 Invalidate (); // Make sure we refresh
278 internal override Color ChangeBackColor (Color backColor)
280 if (backColor == Color.Empty) {
283 backColor = SystemColors.Window;
285 backColor = SystemColors.Window;
287 backcolor_set = false;
293 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
294 if(auto_complete_source == AutoCompleteSource.CustomSource) {
295 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
299 #endregion // Private & Internal Methods
301 #region Public Instance Properties
303 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
304 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
306 [EditorBrowsable (EditorBrowsableState.Always)]
308 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
309 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
310 public AutoCompleteStringCollection AutoCompleteCustomSource {
312 if(auto_complete_custom_source == null) {
313 auto_complete_custom_source = new AutoCompleteStringCollection ();
314 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
316 return auto_complete_custom_source;
319 if(auto_complete_custom_source == value)
322 if(auto_complete_custom_source != null) //remove eventhandler from old collection
323 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
325 auto_complete_custom_source = value;
327 if(auto_complete_custom_source != null)
328 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
332 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
334 [EditorBrowsable (EditorBrowsableState.Always)]
335 [DefaultValue (AutoCompleteMode.None)]
336 public AutoCompleteMode AutoCompleteMode {
337 get { return auto_complete_mode; }
339 if(auto_complete_mode == value)
342 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
343 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
345 auto_complete_mode = value;
349 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
351 [EditorBrowsable (EditorBrowsableState.Always)]
352 [DefaultValue (AutoCompleteSource.None)]
353 [TypeConverter (typeof (TextBoxAutoCompleteSourceConverter))]
354 public AutoCompleteSource AutoCompleteSource {
355 get { return auto_complete_source; }
357 if(auto_complete_source == value)
360 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
361 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
363 auto_complete_source = value;
367 [DefaultValue(false)]
368 [RefreshProperties (RefreshProperties.Repaint)]
369 public bool UseSystemPasswordChar {
371 return use_system_password_char;
375 if (use_system_password_char != value) {
376 use_system_password_char = value;
379 document.PasswordChar = PasswordChar.ToString ();
381 document.PasswordChar = string.Empty;
388 [DefaultValue(false)]
389 [MWFCategory("Behavior")]
390 public bool AcceptsReturn {
392 return accepts_return;
396 if (value != accepts_return) {
397 accepts_return = value;
402 [DefaultValue(CharacterCasing.Normal)]
403 [MWFCategory("Behavior")]
404 public CharacterCasing CharacterCasing {
406 return character_casing;
410 if (value != character_casing) {
411 character_casing = value;
418 [MWFCategory("Behavior")]
420 [RefreshProperties (RefreshProperties.Repaint)]
422 public char PasswordChar {
425 if (use_system_password_char) {
429 return password_char;
433 if (value != password_char) {
434 password_char = value;
436 document.PasswordChar = PasswordChar.ToString ();
438 document.PasswordChar = string.Empty;
440 this.CalculateDocument();
445 [DefaultValue(ScrollBars.None)]
447 [MWFCategory("Appearance")]
448 public ScrollBars ScrollBars {
450 return (ScrollBars)scrollbars;
454 if (!Enum.IsDefined (typeof (ScrollBars), value))
455 throw new InvalidEnumArgumentException ("value", (int) value,
456 typeof (ScrollBars));
458 if (value != (ScrollBars)scrollbars) {
459 scrollbars = (RichTextBoxScrollBars)value;
460 base.CalculateScrollBars();
467 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
468 public override int SelectionLength {
470 return base.SelectionLength;
473 base.SelectionLength = value;
478 public override string Text {
488 [DefaultValue(HorizontalAlignment.Left)]
490 [MWFCategory("Appearance")]
491 public HorizontalAlignment TextAlign {
497 if (value != alignment) {
502 OnTextAlignChanged(EventArgs.Empty);
506 #endregion // Public Instance Properties
509 public void Paste (string text)
511 document.ReplaceSelection (CaseAdjust (text), false);
514 OnTextChanged(EventArgs.Empty);
517 #region Protected Instance Methods
518 protected override CreateParams CreateParams {
520 return base.CreateParams;
525 protected override ImeMode DefaultImeMode {
527 return base.DefaultImeMode;
532 protected override void Dispose (bool disposing)
534 base.Dispose (disposing);
538 protected override bool IsInputKey (Keys keyData)
540 return base.IsInputKey (keyData);
543 protected override void OnGotFocus (EventArgs e)
546 if (selection_length == -1 && !has_been_focused)
547 SelectAllNoScroll ();
548 has_been_focused = true;
551 protected override void OnHandleCreated (EventArgs e)
553 base.OnHandleCreated (e);
557 protected override void OnMouseUp(MouseEventArgs mevent)
559 base.OnMouseUp (mevent);
563 protected virtual void OnTextAlignChanged (EventArgs e)
565 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
570 protected override void WndProc (ref Message m)
572 switch ((Msg)m.Msg) {
575 if (auto_complete_listbox == null || !auto_complete_listbox.Visible)
578 Keys key_data = (Keys)m.WParam.ToInt32 ();
584 NavigateAutoCompleteList (key_data);
585 m.Result = IntPtr.Zero;
592 // Need to call base.WndProc before to have access to
593 // the updated Text property value
594 base.WndProc (ref m);
595 ShowAutoCompleteListBox ();
598 case Msg.WM_LBUTTONDOWN:
599 // When the textbox gets focus by LBUTTON (but not by middle or right)
600 // it does not do the select all / scroll thing.
601 has_been_focused = true;
602 FocusInternal (true);
608 #endregion // Protected Instance Methods
611 static object TextAlignChangedEvent = new object ();
613 public event EventHandler TextAlignChanged {
614 add { Events.AddHandler (TextAlignChangedEvent, value); }
615 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
619 #region Private Methods
621 internal override ContextMenu ContextMenuInternal {
623 ContextMenu res = base.ContextMenuInternal;
629 base.ContextMenuInternal = value;
633 internal void RestoreContextMenu ()
635 ContextMenuInternal = menu;
638 private void menu_Popup(object sender, EventArgs e) {
639 if (SelectionLength == 0) {
641 copy.Enabled = false;
647 if (SelectionLength == TextLength) {
648 select_all.Enabled = false;
650 select_all.Enabled = true;
654 undo.Enabled = false;
660 undo.Enabled = cut.Enabled = paste.Enabled = delete.Enabled = false;
664 private void undo_Click(object sender, EventArgs e) {
668 private void cut_Click(object sender, EventArgs e) {
672 private void copy_Click(object sender, EventArgs e) {
676 private void paste_Click(object sender, EventArgs e) {
680 private void delete_Click(object sender, EventArgs e) {
681 SelectedText = string.Empty;
684 private void select_all_Click(object sender, EventArgs e) {
687 #endregion // Private Methods
690 public override bool Multiline {
692 return base.Multiline;
696 base.Multiline = value;
700 protected override void OnBackColorChanged (EventArgs e)
702 base.OnBackColorChanged (e);
705 protected override void OnFontChanged (EventArgs e)
707 base.OnFontChanged (e);
710 protected override void OnHandleDestroyed (EventArgs e)
712 base.OnHandleDestroyed (e);
715 class AutoCompleteListBox : Control
722 internal int page_size;
724 int highlighted_index = -1;
725 bool user_defined_size;
727 Rectangle resizer_bounds;
729 const int DefaultDropDownItems = 7;
731 public AutoCompleteListBox (TextBox tb)
734 items = new List<string> ();
735 item_height = FontHeight + 2;
737 vscroll = new VScrollBar ();
738 vscroll.ValueChanged += VScrollValueChanged;
739 Controls.Add (vscroll);
742 InternalBorderStyle = BorderStyle.FixedSingle;
745 protected override CreateParams CreateParams {
747 CreateParams cp = base.CreateParams;
749 cp.Style ^= (int)WindowStyles.WS_CHILD;
750 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
751 cp.Style |= (int)WindowStyles.WS_POPUP;
752 cp.ExStyle |= (int)WindowExStyles.WS_EX_TOPMOST | (int)WindowExStyles.WS_EX_TOOLWINDOW;
757 public IList<string> Items {
763 public int HighlightedIndex {
765 return highlighted_index;
768 if (value == highlighted_index)
771 if (highlighted_index != -1)
772 Invalidate (GetItemBounds (highlighted_index));
773 highlighted_index = value;
774 if (highlighted_index != -1)
775 Invalidate (GetItemBounds (highlighted_index));
777 if (highlighted_index != -1)
778 EnsureVisible (highlighted_index);
782 public void Scroll (int lines)
784 int max = vscroll.Maximum - page_size + 1;
785 int val = vscroll.Value + lines;
788 else if (val < vscroll.Minimum)
789 val = vscroll.Minimum;
794 public void EnsureVisible (int index)
796 if (index < top_item) {
797 vscroll.Value = index;
799 int max = vscroll.Maximum - page_size + 1;
800 int rows = Height / item_height;
801 if (index > top_item + rows - 1) {
802 index = index - rows + 1;
803 vscroll.Value = index > max ? max : index;
808 internal override bool ActivateOnShow {
814 void VScrollValueChanged (object o, EventArgs args)
816 if (top_item == vscroll.Value)
819 top_item = vscroll.Value;
820 last_item = GetLastVisibleItem ();
824 int GetLastVisibleItem ()
828 for (int i = top_item; i < items.Count; i++) {
829 int pos = i - top_item; // relative to visible area
830 if ((pos * item_height) + item_height >= top_y)
834 return items.Count - 1;
837 Rectangle GetItemBounds (int index)
839 int pos = index - top_item;
840 Rectangle bounds = new Rectangle (0, pos * item_height, Width, item_height);
842 bounds.Width -= vscroll.Width;
847 int GetItemAt (Point loc)
849 if (loc.Y > (last_item - top_item) * item_height + item_height)
852 int retval = loc.Y / item_height;
858 void LayoutListBox ()
860 int total_height = items.Count * item_height;
861 page_size = Math.Max (Height / item_height, 1);
862 last_item = GetLastVisibleItem ();
864 if (Height < total_height) {
865 vscroll.Visible = true;
866 vscroll.Maximum = items.Count - 1;
867 vscroll.LargeChange = page_size;
868 vscroll.Location = new Point (Width - vscroll.Width, 0);
869 vscroll.Height = Height - item_height;
871 vscroll.Visible = false;
873 resizer_bounds = new Rectangle (Width - item_height, Height - item_height,
874 item_height, item_height);
877 public void HideListBox (bool set_text)
880 owner.Text = items [HighlightedIndex];
888 public void ShowListBox ()
890 if (!user_defined_size) {
891 // This should call the Layout routine for us
892 int height = items.Count > DefaultDropDownItems ? DefaultDropDownItems * item_height :
893 (items.Count + 1) * item_height;
894 Size = new Size (owner.Width, height);
899 HighlightedIndex = -1;
905 protected override void OnResize (EventArgs args)
907 base.OnResize (args);
913 protected override void OnMouseDown (MouseEventArgs args)
915 base.OnMouseDown (args);
917 if (!resizer_bounds.Contains (args.Location))
920 user_defined_size = true;
925 protected override void OnMouseMove (MouseEventArgs args)
927 base.OnMouseMove (args);
930 Point mouse_loc = Control.MousePosition;
931 Point ctrl_loc = PointToScreen (Point.Empty);
933 Size new_size = new Size (mouse_loc.X - ctrl_loc.X, mouse_loc.Y - ctrl_loc.Y);
934 if (new_size.Height < item_height)
935 new_size.Height = item_height;
936 if (new_size.Width < item_height)
937 new_size.Width = item_height;
943 Cursor = resizer_bounds.Contains (args.Location) ? Cursors.SizeNWSE : Cursors.Default;
945 int item_idx = GetItemAt (args.Location);
947 HighlightedIndex = item_idx;
950 protected override void OnMouseUp (MouseEventArgs args)
952 base.OnMouseUp (args);
954 int item_idx = GetItemAt (args.Location);
955 if (item_idx != -1 && !resizing)
962 internal override void OnPaintInternal (PaintEventArgs args)
964 Graphics g = args.Graphics;
965 Brush brush = ThemeEngine.Current.ResPool.GetSolidBrush (ForeColor);
967 int highlighted_idx = HighlightedIndex;
970 int last = GetLastVisibleItem ();
971 for (int i = top_item; i <= last; i++) {
972 Rectangle item_bounds = GetItemBounds (i);
973 if (!item_bounds.IntersectsWith (args.ClipRectangle))
976 if (i == highlighted_idx) {
977 g.FillRectangle (SystemBrushes.Highlight, item_bounds);
978 g.DrawString (items [i], Font, SystemBrushes.HighlightText, item_bounds);
980 g.DrawString (items [i], Font, brush, item_bounds);
985 ThemeEngine.Current.CPDrawSizeGrip (g, SystemColors.Control, resizer_bounds);
992 internal class TextBoxAutoCompleteSourceConverter : EnumConverter
994 public TextBoxAutoCompleteSourceConverter(Type type)
998 public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
1000 StandardValuesCollection stdv = base.GetStandardValues(context);
1001 AutoCompleteSource[] arr = new AutoCompleteSource[stdv.Count];
1002 stdv.CopyTo(arr, 0);
1003 AutoCompleteSource[] arr2 = Array.FindAll(arr, delegate (AutoCompleteSource value) {
1004 // No "ListItems" in a TextBox.
1005 return value != AutoCompleteSource.ListItems;
1007 return new StandardValuesCollection(arr2);