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