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.ComponentModel;
31 using System.ComponentModel.Design;
34 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms {
42 public class TextBox : TextBoxBase {
44 private ContextMenu menu;
45 private MenuItem undo;
47 private MenuItem copy;
48 private MenuItem paste;
49 private MenuItem delete;
50 private MenuItem select_all;
52 private bool use_system_password_char = false;
53 private AutoCompleteStringCollection auto_complete_custom_source = null;
54 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
55 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
57 #endregion // Variables
59 #region Public Constructors
62 scrollbars = RichTextBoxScrollBars.None;
63 alignment = HorizontalAlignment.Left;
64 this.LostFocus +=new EventHandler(TextBox_LostFocus);
65 this.BackColor = ThemeEngine.Current.ColorWindow;
66 this.ForeColor = ThemeEngine.Current.ColorWindowText;
68 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
69 SetStyle (ControlStyles.FixedHeight, true);
71 undo = new MenuItem(Locale.GetText("&Undo"));
72 cut = new MenuItem(Locale.GetText("Cu&t"));
73 copy = new MenuItem(Locale.GetText("&Copy"));
74 paste = new MenuItem(Locale.GetText("&Paste"));
75 delete = new MenuItem(Locale.GetText("&Delete"));
76 select_all = new MenuItem(Locale.GetText("Select &All"));
78 menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
81 menu.Popup += new EventHandler(menu_Popup);
82 undo.Click += new EventHandler(undo_Click);
83 cut.Click += new EventHandler(cut_Click);
84 copy.Click += new EventHandler(copy_Click);
85 paste.Click += new EventHandler(paste_Click);
86 delete.Click += new EventHandler(delete_Click);
87 select_all.Click += new EventHandler(select_all_Click);
89 #endregion // Public Constructors
92 #region Private & Internal Methods
93 private void TextBox_LostFocus(object sender, EventArgs e) {
97 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
98 if(auto_complete_source == AutoCompleteSource.CustomSource) {
99 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
103 #endregion // Private & Internal Methods
105 #region Public Instance Properties
107 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
108 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
110 [EditorBrowsable (EditorBrowsableState.Always)]
112 public AutoCompleteStringCollection AutoCompleteCustomSource {
114 if(auto_complete_custom_source == null) {
115 auto_complete_custom_source = new AutoCompleteStringCollection ();
116 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
118 return auto_complete_custom_source;
121 if(auto_complete_custom_source == value)
124 if(auto_complete_custom_source != null) //remove eventhandler from old collection
125 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
127 auto_complete_custom_source = value;
129 if(auto_complete_custom_source != null)
130 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
134 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
136 [EditorBrowsable (EditorBrowsableState.Always)]
137 [DefaultValue (AutoCompleteMode.None)]
138 public AutoCompleteMode AutoCompleteMode {
139 get { return auto_complete_mode; }
141 if(auto_complete_mode == value)
144 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
145 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
147 auto_complete_mode = value;
151 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
153 [EditorBrowsable (EditorBrowsableState.Always)]
154 [DefaultValue (AutoCompleteSource.None)]
155 public AutoCompleteSource AutoCompleteSource {
156 get { return auto_complete_source; }
158 if(auto_complete_source == value)
161 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
162 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
164 auto_complete_source = value;
168 [DefaultValue(false)]
169 public bool UseSystemPasswordChar {
171 return use_system_password_char;
175 use_system_password_char = value;
180 [DefaultValue(false)]
181 [MWFCategory("Behavior")]
182 public bool AcceptsReturn {
184 return accepts_return;
188 if (value != accepts_return) {
189 accepts_return = value;
194 [DefaultValue(CharacterCasing.Normal)]
195 [MWFCategory("Behavior")]
196 public CharacterCasing CharacterCasing {
198 return character_casing;
202 if (value != character_casing) {
203 character_casing = value;
210 [MWFCategory("Behavior")]
211 public char PasswordChar {
214 if (use_system_password_char) {
218 return password_char;
222 if (value != password_char) {
223 password_char = value;
225 document.PasswordChar = value.ToString();
227 document.PasswordChar = "";
233 [DefaultValue(ScrollBars.None)]
235 [MWFCategory("Appearance")]
236 public ScrollBars ScrollBars {
238 return (ScrollBars)scrollbars;
242 if (value != (ScrollBars)scrollbars) {
243 scrollbars = (RichTextBoxScrollBars)value;
244 base.CalculateScrollBars();
250 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
251 public override int SelectionLength {
253 return base.SelectionLength;
256 base.SelectionLength = value;
261 public override string Text {
271 [DefaultValue(HorizontalAlignment.Left)]
273 [MWFCategory("Appearance")]
274 public HorizontalAlignment TextAlign {
280 if (value != alignment) {
283 // MS word-wraps if alignment isn't left
285 if (alignment != HorizontalAlignment.Left) {
286 document.Wrap = true;
288 document.Wrap = word_wrap;
292 for (int i = 1; i <= document.Lines; i++) {
293 document.GetLine(i).Alignment = value;
295 document.RecalculateDocument(CreateGraphicsInternal());
296 OnTextAlignChanged(EventArgs.Empty);
300 #endregion // Public Instance Properties
302 #region Protected Instance Methods
303 protected override CreateParams CreateParams {
305 return base.CreateParams;
309 protected override ImeMode DefaultImeMode {
311 return base.DefaultImeMode;
314 #endregion // Protected Instance Methods
316 #region Protected Instance Methods
317 protected override bool IsInputKey(Keys keyData) {
318 return base.IsInputKey (keyData);
321 protected override void OnGotFocus(EventArgs e) {
325 protected override void OnHandleCreated(EventArgs e) {
326 base.OnHandleCreated (e);
330 protected override void OnMouseUp(MouseEventArgs e) {
334 protected virtual void OnTextAlignChanged(EventArgs e) {
335 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
340 protected override void WndProc(ref Message m) {
343 #endregion // Protected Instance Methods
346 static object TextAlignChangedEvent = new object ();
348 public event EventHandler TextAlignChanged {
349 add { Events.AddHandler (TextAlignChangedEvent, value); }
350 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
354 #region Private Methods
356 internal override ContextMenu GetContextMenuInternal ()
358 ContextMenu res = base.GetContextMenuInternal ();
364 private void menu_Popup(object sender, EventArgs e) {
365 if (SelectionLength == 0) {
367 copy.Enabled = false;
373 if (SelectionLength == TextLength) {
374 select_all.Enabled = false;
376 select_all.Enabled = true;
380 undo.Enabled = false;
386 private void undo_Click(object sender, EventArgs e) {
390 private void cut_Click(object sender, EventArgs e) {
394 private void copy_Click(object sender, EventArgs e) {
398 private void paste_Click(object sender, EventArgs e) {
402 private void delete_Click(object sender, EventArgs e) {
406 private void select_all_Click(object sender, EventArgs e) {
409 #endregion // Private Methods
412 public override bool Multiline {
414 return base.Multiline;
418 base.Multiline = value;
422 protected override void OnFontChanged (EventArgs e)
424 base.OnFontChanged (e);