2008-02-25 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                         if (Page != null)
98                                 Page.VerifyRenderingInServerForm (this);
99                         
100                         switch (TextMode) {
101                         case TextBoxMode.MultiLine:
102                                 if (Columns != 0)
103                                         w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString (), false);
104 #if NET_2_0
105                                 else
106                                         w.AddAttribute (HtmlTextWriterAttribute.Cols, "20", false);
107 #endif
108                                 
109                                 if (Rows != 0)
110                                         w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString (), false);
111 #if NET_2_0
112                                 else
113                                         w.AddAttribute (HtmlTextWriterAttribute.Rows, "2", false);
114 #endif
115
116                                 if (!Wrap)
117                                         w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off", false);
118                                 
119                                 break;
120                                 
121                         case TextBoxMode.SingleLine:
122                         case TextBoxMode.Password:
123                                 
124                                 if (TextMode == TextBoxMode.Password)
125                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "password", false);
126                                 else {
127                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "text", false);
128                                         if (Text.Length > 0)
129                                                 w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
130                                 }
131                                 
132                                 if (Columns != 0)
133                                         w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString (), false);
134                 
135                                 if (MaxLength != 0)
136                                         w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString (), false);
137
138 #if NET_2_0
139                                 if (AutoCompleteType != AutoCompleteType.None && TextMode == TextBoxMode.SingleLine)
140                                         if (AutoCompleteType != AutoCompleteType.Disabled)
141                                                 w.AddAttribute (HtmlTextWriterAttribute.VCardName, VCardValues [(int) AutoCompleteType]);
142                                         else
143                                                 w.AddAttribute (HtmlTextWriterAttribute.AutoComplete, "off", false);
144 #endif
145                                 break;  
146                         }
147
148 #if NET_2_0
149                         if (AutoPostBack) {
150                                 w.AddAttribute ("onkeypress", "if (WebForm_TextBoxKeyHandler(event) == false) return false;", false);
151                                 
152                                 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
153                                 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
154                                 w.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
155                         }
156 #else           
157                         if (AutoPostBack)
158                                 w.AddAttribute (HtmlTextWriterAttribute.Onchange,
159                                                 BuildScriptAttribute ("onchange",
160                                                         Page.ClientScript.GetPostBackClientHyperlink (this, "")));
161 #endif
162
163                         if (ReadOnly)
164                                 w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly", false);
165
166                         w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
167                         
168                         base.AddAttributesToRender (w);
169                 }
170
171                 protected override void AddParsedSubObject (object obj)
172                 {
173                         LiteralControl l = obj as LiteralControl;
174                         if (l != null)
175                                 Text = l.Text;
176                 }
177
178 #if NET_2_0
179                 protected internal
180 #else           
181                 protected
182 #endif          
183                 override void OnPreRender (EventArgs e)
184                 {
185                         // What do i do here?
186                         base.OnPreRender (e);
187 #if NET_2_0
188                         if (AutoPostBack) {
189                                 RegisterKeyHandlerClientScript ();
190                         }
191                         
192                         if (Page != null && Enabled)
193                                 Page.RegisterEnabledControl (this);
194 #endif
195                 }
196
197 #if NET_2_0
198                 protected internal
199 #else           
200                 protected
201 #endif          
202                 override void Render (HtmlTextWriter w)
203                 {
204                         // Why didn't msft just override RenderContents!?
205                         RenderBeginTag (w);
206                         if (TextMode == TextBoxMode.MultiLine)
207                                 HttpUtility.HtmlEncode (Text, w);
208                         RenderEndTag (w);
209                 }
210                 
211 #if NET_2_0
212                 protected virtual
213 #endif
214                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
215                 {
216                         if (Text != postCollection [postDataKey]) {
217                                 Text = postCollection [postDataKey];
218                                 return true;
219                         }
220                         
221                         return false;
222                 }
223
224 #if NET_2_0
225                 protected virtual
226 #endif
227                 void RaisePostDataChangedEvent ()
228                 {
229 #if NET_2_0
230                         if (CausesValidation)
231                                 Page.Validate (ValidationGroup);
232 #endif
233                         OnTextChanged (EventArgs.Empty);
234                 }
235
236                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
237                 {
238                         return LoadPostData (postDataKey, postCollection);
239                 }
240         
241                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
242                 {
243                         RaisePostDataChangedEvent ();
244                 }
245
246 #if NET_2_0
247                 protected override object SaveViewState ()
248                 {
249                         if (TextMode == TextBoxMode.Password)
250                                 ViewState.SetItemDirty ("Text", false);
251                         return base.SaveViewState ();
252                 }
253 #endif          
254         
255 #if NET_2_0
256                 PostBackOptions GetPostBackOptions () {
257                         PostBackOptions options = new PostBackOptions (this);
258                         options.ActionUrl = null;
259                         options.ValidationGroup = null;
260                         options.Argument = "";
261                         options.RequiresJavaScriptProtocol = false;
262                         options.ClientSubmit = true;
263                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
264                         if (options.PerformValidation)
265                                 options.ValidationGroup = ValidationGroup;
266
267                         return options;
268                 }
269
270                 void RegisterKeyHandlerClientScript () {
271
272                         if (!Page.ClientScript.IsClientScriptBlockRegistered (typeof (TextBox), "KeyHandler")) {
273                                 StringBuilder script=new StringBuilder();
274                                 script.AppendLine ("function WebForm_TextBoxKeyHandler(event) {");
275                                 script.AppendLine ("\tvar target = event.target;");
276                                 script.AppendLine ("\tif ((target == null) || (typeof(target) == \"undefined\")) target = event.srcElement;");
277                                 script.AppendLine ("\tif (event.keyCode == 13) {");
278                                 script.AppendLine ("\t\tif ((typeof(target) != \"undefined\") && (target != null)) {");
279                                 script.AppendLine ("\t\t\tif (typeof(target.onchange) != \"undefined\") {");
280                                 script.AppendLine ("\t\t\t\ttarget.onchange();");
281                                 script.AppendLine ("\t\t\t\tevent.cancelBubble = true;");
282                                 script.AppendLine ("\t\t\t\tif (event.stopPropagation) event.stopPropagation();");
283                                 script.AppendLine ("\t\t\t\treturn false;");
284                                 script.AppendLine ("\t\t\t}");
285                                 script.AppendLine ("\t\t}");
286                                 script.AppendLine ("\t}");
287                                 script.AppendLine ("\treturn true;");
288                                 script.AppendLine ("}");
289                                 Page.ClientScript.RegisterClientScriptBlock (typeof (TextBox), "KeyHandler", script.ToString(), true);
290                         }
291                 }
292 #endif
293
294 #if NET_2_0
295                 [DefaultValue (AutoCompleteType.None)]
296                 [Themeable (false)]
297                 public virtual AutoCompleteType AutoCompleteType 
298                 {
299                         get {
300                                 object o = ViewState ["AutoCompleteType"];
301                                 return o != null ? (AutoCompleteType) o : AutoCompleteType.None;
302                         }
303                         set {
304                                 ViewState ["AutoCompleteType"] = value;
305                         }
306                 }
307 #endif          
308                 
309                 [DefaultValue(false)]
310 #if NET_2_0
311                 [Themeable (false)]
312 #endif          
313                 [WebSysDescription ("")]
314                 [WebCategory ("Behavior")]
315                 public virtual bool AutoPostBack {
316                         get {
317                                 return ViewState.GetBool ("AutoPostBack", false);
318                         }
319                         set {
320                                 ViewState ["AutoPostBack"] = value;
321                         }
322                 }
323
324 #if NET_2_0
325                 [DefaultValue (false)]
326                 [Themeable (false)]
327                 public virtual bool CausesValidation
328                 {
329                         get {
330                                 return ViewState.GetBool ("CausesValidation", false);
331                         }
332                         set {
333                                 ViewState["CausesValidation"] = value;
334                         }
335                 }
336 #endif          
337
338 #if ONLY_1_1
339                 [Bindable(true)]
340 #endif          
341                 [DefaultValue(0)]
342                 [WebSysDescription ("")]
343                 [WebCategory ("Appearance")]
344                 public virtual int Columns {
345                         get {
346                                 return ViewState.GetInt ("Columns", 0);
347                         }
348                         set {
349                                 if (value < 0)
350                                         throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
351                                 else
352                                         ViewState ["Columns"] = value;
353                         }
354                 }
355
356 #if ONLY_1_1
357                 [Bindable(true)]
358 #endif          
359                 [DefaultValue(0)]
360 #if NET_2_0
361                 [Themeable (false)]
362 #endif
363                 [WebSysDescription ("")]
364                 [WebCategory ("Behavior")]
365                 public virtual int MaxLength {
366                         get {
367                                 return ViewState.GetInt ("MaxLength", 0);
368                         }
369                         set {
370                                 if (value < 0)
371                                         throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
372                                 else
373                                         ViewState ["MaxLength"] = value;
374                         }
375                 }
376
377                 [Bindable(true)]
378                 [DefaultValue(false)]
379 #if NET_2_0
380                 [Themeable (false)]
381 #endif
382                 [WebSysDescription ("")]
383                 [WebCategory ("Behavior")]
384                 public virtual bool ReadOnly {
385                         get {
386                                 return ViewState.GetBool ("ReadOnly", false);
387                         }
388                         set {
389                                 ViewState ["ReadOnly"] = value;
390                         }
391                 }
392
393 #if ONLY_1_1
394                 [Bindable(true)]
395 #endif          
396                 [DefaultValue(0)]
397 #if NET_2_0
398                 [Themeable (false)]
399 #endif
400                 [WebSysDescription ("")]
401                 [WebCategory ("Behavior")]
402                 public virtual int Rows {
403                         get {
404                                 return ViewState.GetInt ("Rows", 0);
405                         }
406                         set {
407                                 if (value < 0)
408                                         throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
409                                 else
410                                         ViewState ["Rows"] = value;
411                         }
412                 }
413         
414 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
415                 protected virtual new
416 #else           
417                 protected override
418 #endif
419                 HtmlTextWriterTag TagKey {
420                         get {
421                                 return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
422                         }
423                 }
424
425 #if NET_2_0
426                 [Bindable(true, BindingDirection.TwoWay)]
427 #else
428                 [Bindable(true)]
429 #endif          
430                 [DefaultValue("")]
431                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
432 #if NET_2_0
433                 [Localizable (true)]
434                 [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
435 #endif
436                 [WebSysDescription ("")]
437                 [WebCategory ("Appearance")]
438                 public virtual string Text {
439                         get {
440                                 return ViewState.GetString ("Text", "");
441                         }
442                         set {
443                                 ViewState ["Text"] = value;
444 #if ONLY_1_1
445                                 if (TextMode == TextBoxMode.Password)
446                                         ViewState.SetItemDirty ("Text", false);
447 #endif
448                         }
449                 }
450         
451                 [DefaultValue(TextBoxMode.SingleLine)]
452 #if NET_2_0
453                 [Themeable (false)]
454 #endif
455                 [WebSysDescription ("")]
456                 [WebCategory ("Behavior")]
457                 public virtual TextBoxMode TextMode {
458                         get {
459                                 return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
460                         }
461                         set {
462                                 ViewState ["TextMode"] = (int) value;
463                         }
464                 }
465
466 #if NET_2_0
467                 [Themeable (false)]
468                 [DefaultValue ("")]
469                 public virtual string ValidationGroup
470                 {
471                         get {
472                                 return ViewState.GetString ("ValidationGroup", "");
473                         }
474                         set {
475                                 ViewState ["ValidationGroup"] = value;
476                         }
477                 }
478 #endif          
479         
480                 [DefaultValue(true)]
481                 [WebSysDescription ("")]
482                 [WebCategory ("Layout")]
483                 public virtual bool Wrap {
484                         get {
485                                 return ViewState.GetBool ("Wrap", true);
486                         }
487                         set {
488                                 ViewState ["Wrap"] = value;
489                         }
490                 }
491
492                 protected virtual void OnTextChanged (EventArgs e)
493                 {
494                         EventHandler h = (EventHandler) Events [TextChangedEvent];
495                         if (h != null)
496                                 h (this, e);
497                 }
498                 
499                 static readonly object TextChangedEvent = new object ();
500
501                 [WebSysDescription ("")]
502                 [WebCategory ("Action")]
503                 public event EventHandler TextChanged {
504                         add { Events.AddHandler (TextChangedEvent, value); }
505                         remove { Events.RemoveHandler (TextChangedEvent, value); }
506                 }
507         }
508 }