Mark tests as not working under TARGET_JVM
[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 //     Daniel Nauck    (dna(at)mono-project(dot)de)
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.Drawing;
33 #if NET_2_0
34 using System.Runtime.InteropServices;
35 #endif
36
37 namespace System.Windows.Forms {
38
39 #if NET_2_0
40         [ComVisible(true)]
41 #endif
42         public class TextBox : TextBoxBase {
43                 #region Variables
44                 private ContextMenu     menu;
45                 private MenuItem        undo;
46                 private MenuItem        cut;
47                 private MenuItem        copy;
48                 private MenuItem        paste;
49                 private MenuItem        delete;
50                 private MenuItem        select_all;
51 #if NET_2_0
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;
56 #endif
57                 #endregion      // Variables
58
59                 #region Public Constructors
60                 public TextBox() {
61
62                         scrollbars = RichTextBoxScrollBars.None;
63                         alignment = HorizontalAlignment.Left;
64                         this.LostFocus +=new EventHandler(TextBox_LostFocus);
65
66                         BackColor = SystemColors.Window;
67                         ForeColor = SystemColors.WindowText;
68                         backcolor_set = false;
69
70                         SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
71                         SetStyle (ControlStyles.FixedHeight, true);
72
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"));
79
80                         menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
81                         ContextMenu = menu;
82
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);
90
91                         document.multiline = false;
92                 }
93                 #endregion      // Public Constructors
94
95
96                 #region Private & Internal Methods
97                 private void TextBox_LostFocus(object sender, EventArgs e) {
98                         Invalidate();
99                 }
100 #if NET_2_0
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.
104                         }
105                 }
106 #endif
107                 #endregion      // Private & Internal Methods
108
109                 #region Public Instance Properties
110 #if NET_2_0
111                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
112                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
113                 [Browsable (true)]
114                 [EditorBrowsable (EditorBrowsableState.Always)]
115                 [Localizable (true)]
116                 public AutoCompleteStringCollection AutoCompleteCustomSource { 
117                         get {
118                                 if(auto_complete_custom_source == null) {
119                                         auto_complete_custom_source = new AutoCompleteStringCollection ();
120                                         auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
121                                 }
122                                 return auto_complete_custom_source;
123                         }
124                         set {
125                                 if(auto_complete_custom_source == value)
126                                         return;
127
128                                 if(auto_complete_custom_source != null) //remove eventhandler from old collection
129                                         auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
130
131                                 auto_complete_custom_source = value;
132
133                                 if(auto_complete_custom_source != null)
134                                         auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
135                         }
136                 }
137
138                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
139                 [Browsable (true)]
140                 [EditorBrowsable (EditorBrowsableState.Always)]
141                 [DefaultValue (AutoCompleteMode.None)]
142                 public AutoCompleteMode AutoCompleteMode {
143                         get { return auto_complete_mode; }
144                         set {
145                                 if(auto_complete_mode == value)
146                                         return;
147
148                                 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
149                                         throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
150
151                                 auto_complete_mode = value;
152                         }
153                 }
154
155                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
156                 [Browsable (true)]
157                 [EditorBrowsable (EditorBrowsableState.Always)]
158                 [DefaultValue (AutoCompleteSource.None)]
159                 public AutoCompleteSource AutoCompleteSource {
160                         get { return auto_complete_source; }
161                         set {
162                                 if(auto_complete_source == value)
163                                         return;
164
165                                 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
166                                         throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
167
168                                 auto_complete_source = value;
169                         }
170                 }
171
172                 [DefaultValue(false)]
173                 public bool UseSystemPasswordChar {
174                         get {
175                                 return use_system_password_char;
176                         }
177
178                         set {
179                                 use_system_password_char = value;
180                         }
181                 }
182 #endif
183
184                 [DefaultValue(false)]
185                 [MWFCategory("Behavior")]
186                 public bool AcceptsReturn {
187                         get {
188                                 return accepts_return;
189                         }
190
191                         set {
192                                 if (value != accepts_return) {
193                                         accepts_return = value;
194                                 }       
195                         }
196                 }
197
198                 [DefaultValue(CharacterCasing.Normal)]
199                 [MWFCategory("Behavior")]
200                 public CharacterCasing CharacterCasing {
201                         get {
202                                 return character_casing;
203                         }
204
205                         set {
206                                 if (value != character_casing) {
207                                         character_casing = value;
208                                 }
209                         }
210                 }
211
212                 [Localizable(true)]
213                 [DefaultValue('\0')]
214                 [MWFCategory("Behavior")]
215                 public char PasswordChar {
216                         get {
217 #if NET_2_0
218                                 if (use_system_password_char) {
219                                         return '*';
220                                 }
221 #endif
222                                 return password_char;
223                         }
224
225                         set {
226                                 if (value != password_char) {
227                                         password_char = value;
228                                         if (!Multiline) {
229                                                 document.PasswordChar = value.ToString();
230                                         } else {
231                                                 document.PasswordChar = "";
232                                         }
233                                         this.CalculateDocument();
234                                 }
235                         }
236                 }
237
238                 [DefaultValue(ScrollBars.None)]
239                 [Localizable(true)]
240                 [MWFCategory("Appearance")]
241                 public ScrollBars ScrollBars {
242                         get {
243                                 return (ScrollBars)scrollbars;
244                         }
245
246                         set {
247                                 if (value != (ScrollBars)scrollbars) {
248                                         scrollbars = (RichTextBoxScrollBars)value;
249                                         base.CalculateScrollBars();
250                                 }
251                         }
252                 }
253
254                 [Browsable(false)]
255                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
256                 public override int SelectionLength {
257                         get {
258                                 return base.SelectionLength;
259                         }
260                         set {
261                                 base.SelectionLength = value;
262                         }
263                 }
264
265
266                 public override string Text {
267                         get {
268                                 return base.Text;
269                         }
270
271                         set {
272                                 base.Text = value;
273                         }
274                 }
275
276                 [DefaultValue(HorizontalAlignment.Left)]
277                 [Localizable(true)]
278                 [MWFCategory("Appearance")]
279                 public HorizontalAlignment TextAlign {
280                         get {
281                                 return alignment;
282                         }
283
284                         set {
285                                 if (value != alignment) {
286                                         alignment = value;
287
288                                         // MS word-wraps if alignment isn't left
289                                         if (Multiline) {
290                                                 if (alignment != HorizontalAlignment.Left) {
291                                                         document.Wrap = true;
292                                                 } else {
293                                                         document.Wrap = word_wrap;
294                                                 }
295                                         }
296
297                                         for (int i = 1; i <= document.Lines; i++) {
298                                                 document.GetLine(i).Alignment = value;
299                                         }
300                                         document.RecalculateDocument(CreateGraphicsInternal());
301                                         OnTextAlignChanged(EventArgs.Empty);
302                                 }
303                         }
304                 }
305                 #endregion      // Public Instance Properties
306
307                 #region Protected Instance Methods
308                 protected override CreateParams CreateParams {
309                         get {
310                                 return base.CreateParams;
311                         }
312                 }
313
314                 protected override ImeMode DefaultImeMode {
315                         get {
316                                 return base.DefaultImeMode;
317                         }
318                 }
319                 #endregion      // Protected Instance Methods
320
321                 #region Protected Instance Methods
322 #if NET_2_0
323                 protected override void Dispose (bool disposing)
324                 {
325                         base.Dispose (disposing);
326                 }
327 #endif
328
329                 protected override bool IsInputKey(Keys keyData) {
330                         return base.IsInputKey (keyData);
331                 }
332
333                 protected override void OnGotFocus(EventArgs e) {
334                         base.OnGotFocus (e);
335                 }
336
337                 protected override void OnHandleCreated(EventArgs e) {
338                         base.OnHandleCreated (e);
339                         SelectAll ();
340                 }
341
342                 protected override void OnMouseUp(MouseEventArgs e) {
343                         base.OnMouseUp (e);
344                 }
345
346                 protected virtual void OnTextAlignChanged(EventArgs e) {
347                         EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
348                         if (eh != null)
349                                 eh (this, e);
350                 }
351
352                 protected override void WndProc(ref Message m) {
353                         switch ((Msg)m.Msg) {
354                                 case Msg.WM_LBUTTONDOWN:
355                                         FocusInternal ();
356                                         break;
357                         }
358
359                         base.WndProc(ref m);
360                 }
361                 #endregion      // Protected Instance Methods
362
363                 #region Events
364                 static object TextAlignChangedEvent = new object ();
365
366                 public event EventHandler TextAlignChanged {
367                         add { Events.AddHandler (TextAlignChangedEvent, value); }
368                         remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
369                 }
370                 #endregion      // Events
371
372                 #region Private Methods
373
374                 internal override ContextMenu GetContextMenuInternal ()
375                 {
376                         ContextMenu  res = base.GetContextMenuInternal ();
377                         if (res == menu)
378                                 return null;
379                         return res;
380                 }
381
382                 private void menu_Popup(object sender, EventArgs e) {
383                         if (SelectionLength == 0) {
384                                 cut.Enabled = false;
385                                 copy.Enabled = false;
386                         } else {
387                                 cut.Enabled = true;
388                                 copy.Enabled = true;
389                         }
390
391                         if (SelectionLength == TextLength) {
392                                 select_all.Enabled = false;
393                         } else {
394                                 select_all.Enabled = true;
395                         }
396
397                         if (!CanUndo) {
398                                 undo.Enabled = false;
399                         } else {
400                                 undo.Enabled = true;
401                         }
402                 }
403
404                 private void undo_Click(object sender, EventArgs e) {
405                         Undo();
406                 }
407
408                 private void cut_Click(object sender, EventArgs e) {
409                         Cut();
410                 }
411
412                 private void copy_Click(object sender, EventArgs e) {
413                         Copy();
414                 }
415
416                 private void paste_Click(object sender, EventArgs e) {
417                         Paste();
418                 }
419
420                 private void delete_Click(object sender, EventArgs e) {
421                         SelectedText = "";
422                 }
423
424                 private void select_all_Click(object sender, EventArgs e) {
425                         SelectAll();
426                 }
427                 #endregion      // Private Methods
428
429 #if NET_2_0
430                 public override bool Multiline {
431                         get {
432                                 return base.Multiline;
433                         }
434
435                         set {
436                                 base.Multiline = value;
437                         }
438                 }
439
440                 protected override void OnFontChanged (EventArgs e)
441                 {
442                         base.OnFontChanged (e);
443                 }
444 #endif
445         }
446 }