2009-10-30 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TextBox.cs
1 //
2 // System.Web.UI.WebControls.TextBox.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
32 using System.Text;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41         [DefaultEvent ("TextChanged")]
42         [DefaultProperty ("Text")]
43         [ValidationProperty ("Text")]
44         [ControlBuilder (typeof (TextBoxControlBuilder))]
45 #if NET_2_0
46         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47         [ParseChildren (true, "Text")]
48         [ControlValueProperty ("Text", null)]
49         [SupportsEventValidation]
50 #else
51         [ParseChildren (false)]
52 #endif          
53         public class TextBox : WebControl, IPostBackDataHandler
54 #if NET_2_0
55         , IEditableTextControl, ITextControl
56 #endif            
57         {
58 #if NET_2_0
59                 readonly static string [] VCardValues = new string [] {
60                         null,
61                         null,
62                         "vCard.Cellular",
63                         "vCard.Company",
64                         "vCard.Department",
65                         "vCard.DisplayName",
66                         "vCard.Email",
67                         "vCard.FirstName",
68                         "vCard.Gender",
69                         "vCard.Home.City",
70                         "HomeCountry",
71                         "vCard.Home.Fax",
72                         "vCard.Home.Phone",
73                         "vCard.Home.State",
74                         "vCard.Home.StreetAddress",
75                         "vCard.Home.ZipCode",
76                         "vCard.Home.page",
77                         "vCard.JobTitle",
78                         "vCard.LastName",
79                         "vCard.MiddleName",
80                         "vCard.Notes",
81                         "vCard.Office",
82                         "vCard.Pager",
83                         "vCard.Business.City",
84                         "BusinessCountry",
85                         "vCard.Business.Fax",
86                         "vCard.Business.Phone",
87                         "vCard.Business.State",
88                         "vCard.Business.StreetAddress",
89                         "vCard.Business.Url",
90                         "vCard.Business.ZipCode",
91                         "search"
92                 };
93 #endif
94
95                 protected override void AddAttributesToRender (HtmlTextWriter w)
96                 {
97                         Page page = Page;
98                         if (page != null)
99                                 page.VerifyRenderingInServerForm (this);
100                         
101                         switch (TextMode) {
102                         case TextBoxMode.MultiLine:
103                                 if (Columns != 0)
104                                         w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString (), false);
105 #if NET_2_0
106                                 else
107                                         w.AddAttribute (HtmlTextWriterAttribute.Cols, "20", false);
108 #endif
109                                 
110                                 if (Rows != 0)
111                                         w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString (), false);
112 #if NET_2_0
113                                 else
114                                         w.AddAttribute (HtmlTextWriterAttribute.Rows, "2", false);
115 #endif
116
117                                 if (!Wrap)
118                                         w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off", false);
119                                 
120                                 break;
121                                 
122                         case TextBoxMode.SingleLine:
123                         case TextBoxMode.Password:
124                                 
125                                 if (TextMode == TextBoxMode.Password)
126                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "password", false);
127                                 else {
128                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "text", false);
129                                         if (Text.Length > 0)
130                                                 w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
131                                 }
132                                 
133                                 if (Columns != 0)
134                                         w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString (), false);
135                 
136                                 if (MaxLength != 0)
137                                         w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString (), false);
138
139 #if NET_2_0
140                                 if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
141                                         if (AutoCompleteType != AutoCompleteType.Disabled)
142                                                 w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
143                                         else
144                                                 w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off", false);
145 #endif
146                                 break;  
147                         }
148
149 #if NET_2_0
150                         if (AutoPostBack) {
151                                 w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;", false);
152
153                                 if (page != null) {
154                                         string onchange = page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
155                                         onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
156                                         w.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
157                                 }
158                         } else if (page != null)
159                                 page.ClientScript.RegisterForEventValidation (UniqueID, String.Empty);
160                                 
161 #else           
162                         if (page != null && AutoPostBack)
163                                 w.AddAttribute (HtmlTextWriterAttribute.Onchange,
164                                                 BuildScriptAttribute ("onchange",
165                                                         page.ClientScript.GetPostBackClientHyperlink (this, "")));
166 #endif
167
168                         if (ReadOnly)
169                                 w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly", false);
170
171                         w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
172                         
173                         base.AddAttributesToRender (w);
174                 }
175
176                 protected override void AddParsedSubObject (object obj)
177                 {
178                         LiteralControl l = obj as LiteralControl;
179                         if (l != null)
180                                 Text = l.Text;
181                 }
182
183 #if NET_2_0
184                 protected internal
185 #else           
186                 protected
187 #endif          
188                 override void OnPreRender (EventArgs e)
189                 {
190                         // What do i do here?
191                         base.OnPreRender (e);
192 #if NET_2_0
193                         if (AutoPostBack) {
194                                 RegisterKeyHandlerClientScript ();
195                         }
196
197                         Page page = Page;
198                         if (page != null && Enabled)
199                                 page.RegisterEnabledControl (this);
200 #endif
201                 }
202
203 #if NET_2_0
204                 protected internal
205 #else           
206                 protected
207 #endif          
208                 override void Render (HtmlTextWriter w)
209                 {
210                         // Why didn't msft just override RenderContents!?
211                         RenderBeginTag (w);
212                         if (TextMode == TextBoxMode.MultiLine)
213                                 HttpUtility.HtmlEncode (Text, w);
214                         RenderEndTag (w);
215                 }
216                 
217 #if NET_2_0
218                 protected virtual
219 #endif
220                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
221                 {
222 #if NET_2_0
223                         ValidateEvent (postDataKey, String.Empty);
224 #endif
225                         if (Text != postCollection [postDataKey]) {
226                                 Text = postCollection [postDataKey];
227                                 return true;
228                         }
229                         
230                         return false;
231                 }
232
233 #if NET_2_0
234                 protected virtual
235 #endif
236                 void RaisePostDataChangedEvent ()
237                 {
238 #if NET_2_0
239                         if (CausesValidation)
240                                 Page.Validate (ValidationGroup);
241 #endif
242                         OnTextChanged (EventArgs.Empty);
243                 }
244
245                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
246                 {
247                         return LoadPostData (postDataKey, postCollection);
248                 }
249         
250                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
251                 {
252                         RaisePostDataChangedEvent ();
253                 }
254
255 #if NET_2_0
256                 protected override object SaveViewState ()
257                 {
258                         if (TextMode == TextBoxMode.Password)
259                                 ViewState.SetItemDirty ("Text", false);
260                         return base.SaveViewState ();
261                 }
262 #endif          
263         
264 #if NET_2_0
265                 PostBackOptions GetPostBackOptions () {
266                         PostBackOptions options = new PostBackOptions (this);
267                         options.ActionUrl = null;
268                         options.ValidationGroup = null;
269                         options.Argument = String.Empty;
270                         options.RequiresJavaScriptProtocol = false;
271                         options.ClientSubmit = true;
272                         
273                         Page page = Page;
274                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
275                         if (options.PerformValidation)
276                                 options.ValidationGroup = ValidationGroup;
277
278                         return options;
279                 }
280
281                 void RegisterKeyHandlerClientScript () {
282
283                         if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
284                                 StringBuilder script=new StringBuilder();
285                                 script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
286                                 script.AppendLine ("\tvar target = event.target;");
287                                 script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
288                                 script.AppendLine ("\tif (event.keyCode == 13) {");
289                                 script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
290                                 script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
291                                 script.AppendLine ("\t\t\t\ttarget.onchange();");
292                                 script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
293                                 script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
294                                 script.AppendLine ("\t\t\t\treturn false;");
295                                 script.AppendLine ("\t\t\t}");
296                                 script.AppendLine ("\t\t}");
297                                 script.AppendLine ("\t}");
298                                 script.AppendLine ("\treturn true;");
299                                 script.AppendLine ("}");
300                                 Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
301                         }
302                 }
303 #endif
304
305 #if NET_2_0
306                 [DefaultValue (AutoCompleteType.None)]
307                 [Themeable (false)]
308                 public virtual AutoCompleteType AutoCompleteType 
309                 {
310                         get {
311                                 object o = ViewState ["AutoCompleteType"];
312                                 return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
313                         }
314                         set {
315                                 ViewState ["AutoCompleteType"] = value;
316                         }
317                 }
318 #endif          
319                 
320                 [DefaultValue(false)]
321 #if NET_2_0
322                 [Themeable (false)]
323 #endif          
324                 [WebSysDescription ("")]
325                 [WebCategory ("Behavior")]
326                 public virtual bool AutoPostBack {
327                         get {
328                                 return ViewState.GetBool ("AutoPostBack", false);
329                         }
330                         set {
331                                 ViewState ["AutoPostBack"] = value;
332                         }
333                 }
334
335 #if NET_2_0
336                 [DefaultValue (false)]
337                 [Themeable (false)]
338                 public virtual bool CausesValidation
339                 {
340                         get {
341                                 return ViewState.GetBool ("CausesValidation", false);
342                         }
343                         set {
344                                 ViewState["CausesValidation"] = value;
345                         }
346                 }
347 #endif          
348
349 #if ONLY_1_1
350                 [Bindable(true)]
351 #endif          
352                 [DefaultValue(0)]
353                 [WebSysDescription ("")]
354                 [WebCategory ("Appearance")]
355                 public virtual int Columns {
356                         get {
357                                 return ViewState.GetInt ("Columns", 0);
358                         }
359                         set {
360                                 if (value < 0)
361                                         throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
362                                 else
363                                         ViewState ["Columns"] = value;
364                         }
365                 }
366
367 #if ONLY_1_1
368                 [Bindable(true)]
369 #endif          
370                 [DefaultValue(0)]
371 #if NET_2_0
372                 [Themeable (false)]
373 #endif
374                 [WebSysDescription ("")]
375                 [WebCategory ("Behavior")]
376                 public virtual int MaxLength {
377                         get {
378                                 return ViewState.GetInt ("MaxLength", 0);
379                         }
380                         set {
381                                 if (value < 0)
382                                         throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
383                                 else
384                                         ViewState ["MaxLength"] = value;
385                         }
386                 }
387
388                 [Bindable(true)]
389                 [DefaultValue(false)]
390 #if NET_2_0
391                 [Themeable (false)]
392 #endif
393                 [WebSysDescription ("")]
394                 [WebCategory ("Behavior")]
395                 public virtual bool ReadOnly {
396                         get {
397                                 return ViewState.GetBool ("ReadOnly", false);
398                         }
399                         set {
400                                 ViewState ["ReadOnly"] = value;
401                         }
402                 }
403
404 #if ONLY_1_1
405                 [Bindable(true)]
406 #endif          
407                 [DefaultValue(0)]
408 #if NET_2_0
409                 [Themeable (false)]
410 #endif
411                 [WebSysDescription ("")]
412                 [WebCategory ("Behavior")]
413                 public virtual int Rows {
414                         get {
415                                 return ViewState.GetInt ("Rows", 0);
416                         }
417                         set {
418                                 if (value < 0)
419                                         throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
420                                 else
421                                         ViewState ["Rows"] = value;
422                         }
423                 }
424         
425 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
426                 protected virtual new
427 #else           
428                 protected override
429 #endif
430                 HtmlTextWriterTag TagKey {
431                         get {
432                                 return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
433                         }
434                 }
435
436 #if NET_2_0
437                 [Bindable(true, BindingDirection.TwoWay)]
438 #else
439                 [Bindable(true)]
440 #endif          
441                 [DefaultValue("")]
442                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
443 #if NET_2_0
444                 [Localizable (true)]
445                 [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
446 #endif
447                 [WebSysDescription ("")]
448                 [WebCategory ("Appearance")]
449                 public virtual string Text {
450                         get {
451                                 return ViewState.GetString ("Text", "");
452                         }
453                         set {
454                                 ViewState ["Text"] = value;
455 #if ONLY_1_1
456                                 if (TextMode == TextBoxMode.Password)
457                                         ViewState.SetItemDirty ("Text", false);
458 #endif
459                         }
460                 }
461         
462                 [DefaultValue(TextBoxMode.SingleLine)]
463 #if NET_2_0
464                 [Themeable (false)]
465 #endif
466                 [WebSysDescription ("")]
467                 [WebCategory ("Behavior")]
468                 public virtual TextBoxMode TextMode {
469                         get {
470                                 return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
471                         }
472                         set {
473                                 ViewState ["TextMode"] = (int) value;
474                         }
475                 }
476
477 #if NET_2_0
478                 [Themeable (false)]
479                 [DefaultValue ("")]
480                 public virtual string ValidationGroup
481                 {
482                         get {
483                                 return ViewState.GetString ("ValidationGroup", "");
484                         }
485                         set {
486                                 ViewState ["ValidationGroup"] = value;
487                         }
488                 }
489 #endif          
490         
491                 [DefaultValue(true)]
492                 [WebSysDescription ("")]
493                 [WebCategory ("Layout")]
494                 public virtual bool Wrap {
495                         get {
496                                 return ViewState.GetBool ("Wrap", true);
497                         }
498                         set {
499                                 ViewState ["Wrap"] = value;
500                         }
501                 }
502
503                 protected virtual void OnTextChanged (EventArgs e)
504                 {
505                         EventHandler h = (EventHandler) Events [TextChangedEvent];
506                         if (h != null)
507                                 h (this, e);
508                 }
509                 
510                 static readonly object TextChangedEvent = new object ();
511
512                 [WebSysDescription ("")]
513                 [WebCategory ("Action")]
514                 public event EventHandler TextChanged {
515                         add { Events.AddHandler (TextChangedEvent, value); }
516                         remove { Events.RemoveHandler (TextChangedEvent, value); }
517                 }
518         }
519 }