* roottypes.cs: Rename from tree.cs.
[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
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
40         [DefaultEvent ("TextChanged")]
41         [DefaultProperty ("Text")]
42         [ValidationProperty ("Text")]
43         [ControlBuilder (typeof (TextBoxControlBuilder))]
44 #if NET_2_0
45         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
46         [ParseChildren (true, "Text")]
47         [ControlValueProperty ("Text", null)]
48         [SupportsEventValidation]
49 #else
50         [ParseChildren (false)]
51 #endif          
52         public class TextBox : WebControl, IPostBackDataHandler
53 #if NET_2_0
54         , IEditableTextControl, ITextControl
55 #endif            
56         {
57                 protected override void AddAttributesToRender (HtmlTextWriter w)
58                 {
59                         if (Page != null)
60                                 Page.VerifyRenderingInServerForm (this);
61
62                         base.AddAttributesToRender (w);
63                         
64                         switch (TextMode) {
65                         case TextBoxMode.MultiLine:
66                                 if (Columns != 0)
67                                         w.AddAttribute (HtmlTextWriterAttribute.Cols, Columns.ToString ());
68                                 if (Rows != 0)
69                                         w.AddAttribute (HtmlTextWriterAttribute.Rows, Rows.ToString ());
70                                 if (!Wrap)
71                                         w.AddAttribute (HtmlTextWriterAttribute.Wrap, "off");
72                                 
73                                 break;
74                                 
75                         case TextBoxMode.SingleLine:
76                         case TextBoxMode.Password:
77                                 
78                                 if (TextMode == TextBoxMode.Password)
79                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "password");
80                                 else {
81                                         w.AddAttribute (HtmlTextWriterAttribute.Type, "text");
82                                         if (Text.Length > 0)
83                                                 w.AddAttribute (HtmlTextWriterAttribute.Value, Text);
84                                 }
85                                 
86                                 if (Columns != 0)
87                                         w.AddAttribute (HtmlTextWriterAttribute.Size, Columns.ToString ());
88                 
89                                 if (MaxLength != 0)
90                                         w.AddAttribute (HtmlTextWriterAttribute.Maxlength, MaxLength.ToString ());
91                                 break;  
92                         }
93
94                         if (AutoPostBack)
95                                 w.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
96                         
97                         if (ReadOnly)
98                                 w.AddAttribute (HtmlTextWriterAttribute.ReadOnly, "ReadOnly");
99 #if NET_2_0
100                         if (ID != null)
101                                 w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
102 #else
103                         w.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
104 #endif
105                 }
106
107                 protected override void AddParsedSubObject (object obj)
108                 {
109                         LiteralControl l = obj as LiteralControl;
110                         if (l != null)
111                                 Text = l.Text;
112                 }
113
114                 [MonoTODO]
115 #if NET_2_0
116                 protected internal
117 #else           
118                 protected
119 #endif          
120                 override void OnPreRender (EventArgs e)
121                 {
122                         // What do i do here?
123                         base.OnPreRender (e);
124                 }
125
126                 [MonoTODO ("Am I missing something here")]
127 #if NET_2_0
128                 protected internal
129 #else           
130                 protected
131 #endif          
132                 override void Render (HtmlTextWriter w)
133                 {
134                         // Why didn't msft just override RenderContents!?
135                         RenderBeginTag (w);
136                         if (TextMode == TextBoxMode.MultiLine)
137                                 HttpUtility.HtmlEncode (Text, w);
138                         RenderEndTag (w);
139                 }
140                 
141 #if NET_2_0
142                 protected virtual
143 #endif
144                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
145                 {
146                         if (Text != postCollection [postDataKey]) {
147                                 Text = postCollection [postDataKey];
148                                 return true;
149                         }
150                         
151                         return false;
152                 }
153
154 #if NET_2_0
155                 protected virtual
156 #endif
157                 void RaisePostDataChangedEvent ()
158                 {
159                         OnTextChanged (EventArgs.Empty);
160                 }
161
162                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
163                 {
164                         return LoadPostData (postDataKey, postCollection);
165                 }
166         
167                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
168                 {
169                         RaisePostDataChangedEvent ();
170                 }
171
172 #if NET_2_0
173                 [MonoTODO]
174                 protected override object SaveViewState ()
175                 {
176                         return base.SaveViewState ();
177                 }
178 #endif          
179         
180 #if NET_2_0
181                 [DefaultValue (AutoCompleteType.None)]
182                 [Themeable (false)]
183                 [MonoTODO]
184                 public virtual AutoCompleteType AutoCompleteType 
185                 {
186                         get {
187                                 throw new NotImplementedException ();
188                         }
189                         set {
190                                 throw new NotImplementedException ();
191                         }
192                 }
193 #endif          
194                 
195                 [DefaultValue(false)]
196 #if NET_2_0
197                 [Themeable (false)]
198 #endif          
199                 [WebSysDescription ("")]
200                 [WebCategory ("Behavior")]
201                 public virtual bool AutoPostBack {
202                         get {
203                                 return ViewState.GetBool ("AutoPostBack", false);
204                         }
205                         set {
206                                 ViewState ["AutoPostBack"] = value;
207                         }
208                 }
209
210 #if NET_2_0
211                 [DefaultValue (false)]
212                 [Themeable (false)]
213                 public virtual bool CausesValidation
214                 {
215                         get {
216                                 return ViewState.GetBool ("CausesValidation", false);
217                         }
218                         set {
219                                 ViewState["CausesValidation"] = value;
220                         }
221                 }
222 #endif          
223
224 #if ONLY_1_1
225                 [Bindable(true)]
226 #endif          
227                 [DefaultValue(0)]
228                 [WebSysDescription ("")]
229                 [WebCategory ("Appearance")]
230                 public virtual int Columns {
231                         get {
232                                 return ViewState.GetInt ("Columns", 0);
233                         }
234                         set {
235                                 if (value < 0)
236                                         throw new ArgumentOutOfRangeException("value", "Columns value has to be 0 for 'not set' or bigger than 0.");
237                                 else
238                                         ViewState ["Columns"] = value;
239                         }
240                 }
241
242 #if ONLY_1_1
243                 [Bindable(true)]
244 #endif          
245                 [DefaultValue(0)]
246 #if NET_2_0
247                 [Themeable (false)]
248 #endif
249                 [WebSysDescription ("")]
250                 [WebCategory ("Behavior")]
251                 public virtual int MaxLength {
252                         get {
253                                 return ViewState.GetInt ("MaxLength", 0);
254                         }
255                         set {
256                                 if (value < 0)
257                                         throw new ArgumentOutOfRangeException("value", "MaxLength value has to be 0 for 'not set' or bigger than 0.");
258                                 else
259                                         ViewState ["MaxLength"] = value;
260                         }
261                 }
262
263                 [Bindable(true)]
264                 [DefaultValue(false)]
265 #if NET_2_0
266                 [Themeable (false)]
267 #endif
268                 [WebSysDescription ("")]
269                 [WebCategory ("Behavior")]
270                 public virtual bool ReadOnly {
271                         get {
272                                 return ViewState.GetBool ("ReadOnly", false);
273                         }
274                         set {
275                                 ViewState ["ReadOnly"] = value;
276                         }
277                 }
278
279 #if ONLY_1_1
280                 [Bindable(true)]
281 #endif          
282                 [DefaultValue(0)]
283 #if NET_2_0
284                 [Themeable (false)]
285 #endif
286                 [WebSysDescription ("")]
287                 [WebCategory ("Behavior")]
288                 public virtual int Rows {
289                         get {
290                                 return ViewState.GetInt ("Rows", 0);
291                         }
292                         set {
293                                 if (value < 0)
294                                         throw new ArgumentOutOfRangeException("value", "Rows value has to be 0 for 'not set' or bigger than 0.");
295                                 else
296                                         ViewState ["Rows"] = value;
297                         }
298                 }
299         
300 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
301                 protected virtual new
302 #else           
303                 protected override
304 #endif
305                 HtmlTextWriterTag TagKey {
306                         get {
307                                 return TextMode == TextBoxMode.MultiLine ? HtmlTextWriterTag.Textarea : HtmlTextWriterTag.Input;
308                         }
309                 }
310
311 #if NET_2_0
312                 [Bindable(true, BindingDirection.TwoWay)]
313 #else
314                 [Bindable(true)]
315 #endif          
316                 [DefaultValue("")]
317                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
318 #if NET_2_0
319                 [Localizable (true)]
320                 [Editor ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
321 #endif
322                 [WebSysDescription ("")]
323                 [WebCategory ("Appearance")]
324                 public virtual string Text {
325                         get {
326                                 return ViewState.GetString ("Text", "");
327                         }
328                         set {
329                                 ViewState ["Text"] = value;
330                                 if (TextMode == TextBoxMode.Password)
331                                         ViewState.SetItemDirty ("Text", false);
332                         }
333                 }
334         
335                 [DefaultValue(TextBoxMode.SingleLine)]
336 #if NET_2_0
337                 [Themeable (false)]
338 #endif
339                 [WebSysDescription ("")]
340                 [WebCategory ("Behavior")]
341                 public virtual TextBoxMode TextMode {
342                         get {
343                                 return (TextBoxMode) ViewState.GetInt ("TextMode", (int) TextBoxMode.SingleLine);
344                         }
345                         set {
346                                 ViewState ["TextMode"] = (int) value;
347                         }
348                 }
349
350 #if NET_2_0
351                 [Themeable (false)]
352                 [DefaultValue ("")]
353                 public virtual string ValidationGroup
354                 {
355                         get {
356                                 return ViewState.GetString ("ValidationGroup", "");
357                         }
358                         set {
359                                 ViewState ["ValidationGroup"] = value;
360                         }
361                 }
362 #endif          
363         
364                 [DefaultValue(true)]
365                 [WebSysDescription ("")]
366                 [WebCategory ("Layout")]
367                 public virtual bool Wrap {
368                         get {
369                                 return ViewState.GetBool ("Wrap", true);
370                         }
371                         set {
372                                 ViewState ["Wrap"] = value;
373                         }
374                 }
375
376                 protected virtual void OnTextChanged (EventArgs e)
377                 {
378                         EventHandler h = (EventHandler) Events [TextChangedEvent];
379                         if (h != null)
380                                 h (this, e);
381                 }
382                 
383                 static readonly object TextChangedEvent = new object ();
384
385                 [WebSysDescription ("")]
386                 [WebCategory ("Action")]
387                 public event EventHandler TextChanged {
388                         add { Events.AddHandler (TextChangedEvent, value); }
389                         remove { Events.RemoveHandler (TextChangedEvent, value); }
390                 }
391         }
392 }