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);
66 BackColor = SystemColors.Window;
67 ForeColor = SystemColors.WindowText;
68 backcolor_set = false;
70 SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
71 SetStyle (ControlStyles.FixedHeight, true);
73 undo = new MenuItem(Locale.GetText("&Undo"));
74 cut = new MenuItem(Locale.GetText("Cu&t"));
75 copy = new MenuItem(Locale.GetText("&Copy"));
76 paste = new MenuItem(Locale.GetText("&Paste"));
77 delete = new MenuItem(Locale.GetText("&Delete"));
78 select_all = new MenuItem(Locale.GetText("Select &All"));
80 menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
83 menu.Popup += new EventHandler(menu_Popup);
84 undo.Click += new EventHandler(undo_Click);
85 cut.Click += new EventHandler(cut_Click);
86 copy.Click += new EventHandler(copy_Click);
87 paste.Click += new EventHandler(paste_Click);
88 delete.Click += new EventHandler(delete_Click);
89 select_all.Click += new EventHandler(select_all_Click);
91 document.multiline = false;
93 #endregion // Public Constructors
96 #region Private & Internal Methods
97 private void TextBox_LostFocus(object sender, EventArgs e) {
101 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
102 if(auto_complete_source == AutoCompleteSource.CustomSource) {
103 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
107 #endregion // Private & Internal Methods
109 #region Public Instance Properties
111 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
112 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
114 [EditorBrowsable (EditorBrowsableState.Always)]
116 public AutoCompleteStringCollection AutoCompleteCustomSource {
118 if(auto_complete_custom_source == null) {
119 auto_complete_custom_source = new AutoCompleteStringCollection ();
120 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
122 return auto_complete_custom_source;
125 if(auto_complete_custom_source == value)
128 if(auto_complete_custom_source != null) //remove eventhandler from old collection
129 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
131 auto_complete_custom_source = value;
133 if(auto_complete_custom_source != null)
134 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
138 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
140 [EditorBrowsable (EditorBrowsableState.Always)]
141 [DefaultValue (AutoCompleteMode.None)]
142 public AutoCompleteMode AutoCompleteMode {
143 get { return auto_complete_mode; }
145 if(auto_complete_mode == value)
148 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
149 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
151 auto_complete_mode = value;
155 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
157 [EditorBrowsable (EditorBrowsableState.Always)]
158 [DefaultValue (AutoCompleteSource.None)]
159 public AutoCompleteSource AutoCompleteSource {
160 get { return auto_complete_source; }
162 if(auto_complete_source == value)
165 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
166 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
168 auto_complete_source = value;
172 [DefaultValue(false)]
173 public bool UseSystemPasswordChar {
175 return use_system_password_char;
179 use_system_password_char = value;
184 [DefaultValue(false)]
185 [MWFCategory("Behavior")]
186 public bool AcceptsReturn {
188 return accepts_return;
192 if (value != accepts_return) {
193 accepts_return = value;
198 [DefaultValue(CharacterCasing.Normal)]
199 [MWFCategory("Behavior")]
200 public CharacterCasing CharacterCasing {
202 return character_casing;
206 if (value != character_casing) {
207 character_casing = value;
214 [MWFCategory("Behavior")]
215 public char PasswordChar {
218 if (use_system_password_char) {
222 return password_char;
226 if (value != password_char) {
227 password_char = value;
229 document.PasswordChar = value.ToString();
231 document.PasswordChar = "";
233 this.CalculateDocument();
238 [DefaultValue(ScrollBars.None)]
240 [MWFCategory("Appearance")]
241 public ScrollBars ScrollBars {
243 return (ScrollBars)scrollbars;
247 if (value != (ScrollBars)scrollbars) {
248 scrollbars = (RichTextBoxScrollBars)value;
249 base.CalculateScrollBars();
255 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
256 public override int SelectionLength {
258 return base.SelectionLength;
261 base.SelectionLength = value;
266 public override string Text {
276 [DefaultValue(HorizontalAlignment.Left)]
278 [MWFCategory("Appearance")]
279 public HorizontalAlignment TextAlign {
285 if (value != alignment) {
288 // MS word-wraps if alignment isn't left
290 if (alignment != HorizontalAlignment.Left) {
291 document.Wrap = true;
293 document.Wrap = word_wrap;
297 for (int i = 1; i <= document.Lines; i++) {
298 document.GetLine(i).Alignment = value;
300 document.RecalculateDocument(CreateGraphicsInternal());
301 OnTextAlignChanged(EventArgs.Empty);
305 #endregion // Public Instance Properties
307 #region Protected Instance Methods
308 protected override CreateParams CreateParams {
310 return base.CreateParams;
314 protected override ImeMode DefaultImeMode {
316 return base.DefaultImeMode;
319 #endregion // Protected Instance Methods
321 #region Protected Instance Methods
323 protected override void Dispose (bool disposing)
325 base.Dispose (disposing);
329 protected override bool IsInputKey(Keys keyData) {
330 return base.IsInputKey (keyData);
333 protected override void OnGotFocus(EventArgs e) {
337 protected override void OnHandleCreated(EventArgs e) {
338 base.OnHandleCreated (e);
342 protected override void OnMouseUp(MouseEventArgs e) {
346 protected virtual void OnTextAlignChanged(EventArgs e) {
347 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
352 protected override void WndProc(ref Message m) {
353 switch ((Msg)m.Msg) {
354 case Msg.WM_LBUTTONDOWN:
361 #endregion // Protected Instance Methods
364 static object TextAlignChangedEvent = new object ();
366 public event EventHandler TextAlignChanged {
367 add { Events.AddHandler (TextAlignChangedEvent, value); }
368 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
372 #region Private Methods
374 internal override ContextMenu GetContextMenuInternal ()
376 ContextMenu res = base.GetContextMenuInternal ();
382 private void menu_Popup(object sender, EventArgs e) {
383 if (SelectionLength == 0) {
385 copy.Enabled = false;
391 if (SelectionLength == TextLength) {
392 select_all.Enabled = false;
394 select_all.Enabled = true;
398 undo.Enabled = false;
404 private void undo_Click(object sender, EventArgs e) {
408 private void cut_Click(object sender, EventArgs e) {
412 private void copy_Click(object sender, EventArgs e) {
416 private void paste_Click(object sender, EventArgs e) {
420 private void delete_Click(object sender, EventArgs e) {
424 private void select_all_Click(object sender, EventArgs e) {
427 #endregion // Private Methods
430 public override bool Multiline {
432 return base.Multiline;
436 base.Multiline = value;
440 protected override void OnFontChanged (EventArgs e)
442 base.OnFontChanged (e);