New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextBox.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.Drawing;
33
34 namespace System.Windows.Forms {
35         public class TextBox : TextBoxBase {
36                 #region Variables
37                 private ContextMenu     menu;
38                 private MenuItem        undo;
39                 private MenuItem        cut;
40                 private MenuItem        copy;
41                 private MenuItem        paste;
42                 private MenuItem        delete;
43                 private MenuItem        select_all;
44                 #endregion      // Variables
45
46                 #region Public Constructors
47                 public TextBox() {
48
49                         scrollbars = RichTextBoxScrollBars.None;
50                         alignment = HorizontalAlignment.Left;
51                         this.LostFocus +=new EventHandler(TextBox_LostFocus);
52                         this.BackColor = ThemeEngine.Current.ColorWindow;
53                         this.ForeColor = ThemeEngine.Current.ColorWindowText;
54
55                         SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
56                         SetStyle (ControlStyles.FixedHeight, true);
57
58                         undo = new MenuItem(Locale.GetText("&Undo"));
59                         cut = new MenuItem(Locale.GetText("Cu&t"));
60                         copy = new MenuItem(Locale.GetText("&Copy"));
61                         paste = new MenuItem(Locale.GetText("&Paste"));
62                         delete = new MenuItem(Locale.GetText("&Delete"));
63                         select_all = new MenuItem(Locale.GetText("Select &All"));
64
65                         menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
66                         ContextMenu = menu;
67
68                         menu.Popup += new EventHandler(menu_Popup);
69                         undo.Click += new EventHandler(undo_Click);
70                         cut.Click += new EventHandler(cut_Click);
71                         copy.Click += new EventHandler(copy_Click);
72                         paste.Click += new EventHandler(paste_Click);
73                         delete.Click += new EventHandler(delete_Click);
74                         select_all.Click += new EventHandler(select_all_Click);
75                 }
76                 #endregion      // Public Constructors
77
78
79                 #region Private & Internal Methods
80                 private void TextBox_LostFocus(object sender, EventArgs e) {
81                         has_focus = false;
82                         Invalidate();
83                 }
84                 #endregion      // Private & Internal Methods
85
86                 #region Public Instance Properties
87 #if NET_2_0
88                 private bool use_system_password_char = false;
89
90                 [DefaultValue(false)]
91                 public bool UseSystemPasswordChar {
92                         get {
93                                 return use_system_password_char;
94                         }
95
96                         set {
97                                 use_system_password_char = value;
98                         }
99                 }
100 #endif
101
102                 [DefaultValue(false)]
103                 [MWFCategory("Behavior")]
104                 public bool AcceptsReturn {
105                         get {
106                                 return accepts_return;
107                         }
108
109                         set {
110                                 if (value != accepts_return) {
111                                         accepts_return = value;
112                                 }       
113                         }
114                 }
115
116                 [DefaultValue(CharacterCasing.Normal)]
117                 [MWFCategory("Behavior")]
118                 public CharacterCasing CharacterCasing {
119                         get {
120                                 return character_casing;
121                         }
122
123                         set {
124                                 if (value != character_casing) {
125                                         character_casing = value;
126                                 }
127                         }
128                 }
129
130                 [Localizable(true)]
131                 [DefaultValue('\0')]
132                 [MWFCategory("Behavior")]
133                 public char PasswordChar {
134                         get {
135 #if NET_2_0
136                                 if (use_system_password_char) {
137                                         return '*';
138                                 }
139 #endif
140                                 return password_char;
141                         }
142
143                         set {
144                                 if (value != password_char) {
145                                         password_char = value;
146                                         if (!multiline) {
147                                                 document.PasswordChar = value.ToString();
148                                         } else {
149                                                 document.PasswordChar = "";
150                                         }
151                                 }
152                         }
153                 }
154
155                 [DefaultValue(ScrollBars.None)]
156                 [Localizable(true)]
157                 [MWFCategory("Appearance")]
158                 public ScrollBars ScrollBars {
159                         get {
160                                 return (ScrollBars)scrollbars;
161                         }
162
163                         set {
164                                 if (value != (ScrollBars)scrollbars) {
165                                         scrollbars = (RichTextBoxScrollBars)value;
166                                         base.CalculateScrollBars();
167                                 }
168                         }
169                 }
170
171                 [Browsable(false)]
172                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
173                 public override int SelectionLength {
174                         get {
175                                 return base.SelectionLength;
176                         }
177                         set {
178                                 base.SelectionLength = value;
179                         }
180                 }
181
182
183                 public override string Text {
184                         get {
185                                 return base.Text;
186                         }
187
188                         set {
189                                 base.Text = value;
190                         }
191                 }
192
193                 [DefaultValue(HorizontalAlignment.Left)]
194                 [Localizable(true)]
195                 [MWFCategory("Appearance")]
196                 public HorizontalAlignment TextAlign {
197                         get {
198                                 return alignment;
199                         }
200
201                         set {
202                                 if (value != alignment) {
203                                         alignment = value;
204
205                                         // MS word-wraps if alignment isn't left
206                                         if (multiline) {
207                                                 if (alignment != HorizontalAlignment.Left) {
208                                                         document.Wrap = true;
209                                                 } else {
210                                                         document.Wrap = word_wrap;
211                                                 }
212                                         }
213
214                                         for (int i = 1; i <= document.Lines; i++) {
215                                                 document.GetLine(i).Alignment = value;
216                                         }
217                                         document.RecalculateDocument(CreateGraphicsInternal());
218                                         OnTextAlignChanged(EventArgs.Empty);
219                                 }
220                         }
221                 }
222                 #endregion      // Public Instance Properties
223
224                 #region Protected Instance Methods
225                 protected override CreateParams CreateParams {
226                         get {
227                                 return base.CreateParams;
228                         }
229                 }
230
231                 protected override ImeMode DefaultImeMode {
232                         get {
233                                 return base.DefaultImeMode;
234                         }
235                 }
236                 #endregion      // Protected Instance Methods
237
238                 #region Protected Instance Methods
239                 protected override bool IsInputKey(Keys keyData) {
240                         return base.IsInputKey (keyData);
241                 }
242
243                 protected override void OnGotFocus(EventArgs e) {
244                         has_focus=true;
245                         Invalidate();
246                         base.OnGotFocus (e);
247                 }
248
249                 protected override void OnHandleCreated(EventArgs e) {
250                         base.OnHandleCreated (e);
251                 }
252
253                 protected override void OnMouseUp(MouseEventArgs e) {
254                         base.OnMouseUp (e);
255                 }
256
257                 protected virtual void OnTextAlignChanged(EventArgs e) {
258                         if (TextAlignChanged != null) {
259                                 TextAlignChanged(this, e);
260                         }
261                 }
262
263                 protected override void WndProc(ref Message m) {
264                         base.WndProc(ref m);
265                 }
266                 #endregion      // Protected Instance Methods
267
268                 #region Events
269                 public event EventHandler TextAlignChanged;
270                 #endregion      // Events
271
272                 #region Private Methods
273                 private void menu_Popup(object sender, EventArgs e) {
274                         if (SelectionLength == 0) {
275                                 cut.Enabled = false;
276                                 copy.Enabled = false;
277                         } else {
278                                 cut.Enabled = true;
279                                 copy.Enabled = true;
280                         }
281
282                         if (SelectionLength == TextLength) {
283                                 select_all.Enabled = false;
284                         } else {
285                                 select_all.Enabled = true;
286                         }
287
288                         if (!CanUndo) {
289                                 undo.Enabled = false;
290                         } else {
291                                 undo.Enabled = true;
292                         }
293                 }
294
295                 private void undo_Click(object sender, EventArgs e) {
296                         Undo();
297                 }
298
299                 private void cut_Click(object sender, EventArgs e) {
300                         Cut();
301                 }
302
303                 private void copy_Click(object sender, EventArgs e) {
304                         Copy();
305                 }
306
307                 private void paste_Click(object sender, EventArgs e) {
308                         Paste();
309                 }
310
311                 private void delete_Click(object sender, EventArgs e) {
312                         SelectedText = "";
313                 }
314
315                 private void select_all_Click(object sender, EventArgs e) {
316                         SelectAll();
317                 }
318                 #endregion      // Private Methods
319         }
320 }