Add this for backwards compatibility
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
1 //
2 // System.Web.UI.WebControls.CheckBox.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (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.Globalization;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35         // CAS
36         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41         [DefaultEvent ("CheckedChanged")]
42         [DefaultProperty ("Text")]
43 #if NET_2_0
44         [ControlValueProperty ("Checked", null)]
45         [SupportsEventValidation]
46 #endif          
47         public class CheckBox : WebControl, IPostBackDataHandler
48 #if NET_2_0
49         , ICheckBoxControl
50 #endif
51         {
52                 string render_type;
53
54 #if NET_2_0
55                 AttributeCollection inputAttributes;
56                 StateBag inputAttributesState;
57                 AttributeCollection labelAttributes;
58                 StateBag labelAttributesState;
59 #endif
60                 
61                 public CheckBox () : base (HtmlTextWriterTag.Input)
62                 {
63                         render_type = "checkbox";
64                 }
65
66                 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
67                 {
68                         this.render_type = render_type;
69                 }
70
71                 [DefaultValue (false)]
72 #if NET_2_0
73                 [Themeable (false)]
74 #endif          
75                 [WebSysDescription ("")]
76                 [WebCategory ("Behavior")]
77                 public virtual bool AutoPostBack 
78                 {
79                         get {
80                                 return (ViewState.GetBool ("AutoPostBack",
81                                                            false));
82                         }
83                         set {
84                                 ViewState["AutoPostBack"] = value;
85                         }
86                 }
87
88 #if NET_2_0
89                 [DefaultValue (false)]
90                 [Themeable (false)]
91                 [MonoTODO]
92                 [WebSysDescription ("")]
93                 [WebCategory ("Behavior")]
94                 public virtual bool CausesValidation 
95                 {
96                         get { return ViewState.GetBool ("CausesValidation", false); }
97                         set { ViewState ["CausesValidation"] = value; }
98                 }
99 #endif          
100                 
101
102                 [DefaultValue (false)]
103 #if NET_2_0
104                 [Bindable (true, BindingDirection.TwoWay)]
105                 [Themeable (false)]
106 #else           
107                 [Bindable (true)]
108 #endif          
109                 [WebSysDescription ("")]
110                 [WebCategory ("Behavior")]
111                 public virtual bool Checked 
112                 {
113                         get {
114                                 return (ViewState.GetBool ("Checked", false));
115                         }
116                         set {
117                                 ViewState["Checked"] = value;
118                         }
119                 }
120
121 #if NET_2_0
122                 [Browsable (false)]
123                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
124                 public AttributeCollection InputAttributes 
125                 {
126                         get {
127                                 if (inputAttributes == null) {
128                                         if (inputAttributesState == null) {
129                                                 inputAttributesState = new StateBag (true);
130                                                 if (IsTrackingViewState)
131                                                         inputAttributesState.TrackViewState();
132                                         }
133                                         inputAttributes = new AttributeCollection (inputAttributesState);
134                                 }
135                                 return inputAttributes;
136                         }
137                 }
138
139                 [Browsable (false)]
140                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
141                 public AttributeCollection LabelAttributes
142                 {
143                         get {
144                                 if (labelAttributes == null) {
145                                         if (labelAttributesState == null) {
146                                                 labelAttributesState = new StateBag (true);
147                                                 if (IsTrackingViewState)
148                                                         labelAttributesState.TrackViewState();
149                                         }
150                                         labelAttributes = new AttributeCollection (labelAttributesState);
151                                 }
152                                 return labelAttributes;
153                         }
154                 }
155 #endif          
156
157                 [DefaultValue ("")]
158                 [Bindable (true)]
159 #if NET_2_0
160                 [Localizable (true)]
161 #endif          
162                 [WebSysDescription ("")]
163                 [WebCategory ("Appearance")]
164                 public virtual string Text 
165                 {
166                         get {
167                                 return (ViewState.GetString ("Text",
168                                                              String.Empty));
169                         }
170                         set {
171                                 ViewState["Text"] = value;
172                         }
173                 }
174
175                 [DefaultValue (TextAlign.Right)]
176 #if ONLY_1_1
177                 [Bindable (true)]
178 #endif          
179                 [WebSysDescription ("")]
180                 [WebCategory ("Appearance")]
181                 public virtual TextAlign TextAlign
182                 {
183                         get {
184                                 object o = ViewState["TextAlign"];
185
186                                 if (o == null) {
187                                         return (TextAlign.Right);
188                                 } else {
189                                         return ((TextAlign)o);
190                                 }
191                         }
192                         set {
193                                 if (value != TextAlign.Left &&
194                                     value != TextAlign.Right) {
195                                         throw new ArgumentOutOfRangeException ("value");
196                                 }
197                                 
198                                 ViewState["TextAlign"] = value;
199                         }
200                 }
201
202 #if NET_2_0
203                 [Themeable (false)]
204                 [DefaultValue ("")]
205                 [WebSysDescription ("")]
206                 [WebCategoryAttribute ("Behavior")]
207                 public string ValidationGroup
208                 {
209                         get { return ViewState.GetString ("ValidationGroup", String.Empty); }
210                         set { ViewState["ValidationGroup"] = value; }
211                 }
212 #endif          
213
214                 private static readonly object EventCheckedChanged = new object ();
215                 [WebSysDescription ("")]
216                 [WebCategory ("Action")]
217                 public event EventHandler CheckedChanged 
218                 {
219                         add {
220                                 Events.AddHandler (EventCheckedChanged, value);
221                         }
222                         remove {
223                                 Events.RemoveHandler (EventCheckedChanged, value);
224                         }
225                 }
226
227                 protected virtual void OnCheckedChanged (EventArgs e)
228                 {
229                         EventHandler handler = (EventHandler)Events[EventCheckedChanged];
230                         
231                         if (handler != null) {
232                                 handler (this, e);
233                         }
234                 }
235
236                 internal virtual string NameAttribute 
237                 {
238                         get {
239                                 return (this.UniqueID);
240                         }
241                 }
242                 
243 #if NET_2_0
244                 protected override void LoadViewState (object savedState)
245                 {
246                         if (savedState == null) {
247                                 base.LoadViewState (null);
248                                 return;
249                         }
250
251                         Triplet saved = (Triplet) savedState;
252                         base.LoadViewState (saved.First);
253
254                         if (saved.Second != null) {
255                                 if (inputAttributesState == null) {
256                                         inputAttributesState = new StateBag(true);
257                                         inputAttributesState.TrackViewState ();
258                                 }
259                                 inputAttributesState.LoadViewState (saved.Second);
260                         }
261
262                         if (saved.Third != null) {
263                                 if (labelAttributesState == null) {
264                                         labelAttributesState = new StateBag(true);
265                                         labelAttributesState.TrackViewState ();
266                                 }
267                                 labelAttributesState.LoadViewState (saved.Third);
268                         }
269                 }
270
271                 protected override object SaveViewState ()
272                 {
273                         object baseView = base.SaveViewState ();
274                         object inputAttrView = null;
275                         object labelAttrView = null;
276
277                         if (inputAttributesState != null)
278                                 inputAttrView = inputAttributesState.SaveViewState ();
279
280                         if (labelAttributesState != null)
281                                 labelAttrView = labelAttributesState.SaveViewState ();
282
283                         if (baseView == null && inputAttrView == null && labelAttrView == null)
284                                 return null;
285
286                         return new Triplet (baseView, inputAttrView, labelAttrView);            
287                 }
288
289                 protected override void TrackViewState ()
290                 {
291                         base.TrackViewState();
292                         if (inputAttributesState != null)
293                                 inputAttributesState.TrackViewState ();
294                         if (labelAttributesState != null)
295                                 labelAttributesState.TrackViewState ();
296                 }
297 #endif          
298
299 #if NET_2_0
300                 protected internal
301 #else           
302                 protected
303 #endif          
304                 override void OnPreRender (EventArgs e)
305                 {
306                         base.OnPreRender (e);
307
308                         if (Page != null) {
309                                 Page.RegisterRequiresPostBack (this);
310                         }
311                 }
312
313 #if NET_2_0
314                 protected internal
315 #else           
316                 protected
317 #endif          
318                 override void Render (HtmlTextWriter w)
319                 {
320                         if (Page != null)
321                                 Page.VerifyRenderingInServerForm (this);
322
323                         bool need_span = ControlStyleCreated;
324                         if (need_span)
325                                 ControlStyle.AddAttributesToRender (w, this);
326
327                         if (!Enabled) {
328                                 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
329                                 //need_span = true;
330                         }
331
332                         string tt = ToolTip;
333                         if (tt != ""){
334                                 w.AddAttribute ("title", tt);
335                                 need_span = true;
336                         }
337
338                         if (Attributes.Count > 0){
339                                 Attributes.AddAttributes (w);
340                                 need_span = true;
341                         }
342
343                         if (need_span)
344                                 w.RenderBeginTag (HtmlTextWriterTag.Span);
345
346                         TextAlign align = TextAlign;
347                         if (align == TextAlign.Right) {
348                                 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
349                                 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
350                                 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
351                                 InternalAddAttributesToRender (w);
352                                 if (Checked)
353                                         w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
354
355                                 if (AutoPostBack){
356                                         w.AddAttribute (HtmlTextWriterAttribute.Onclick,
357                                                              Page.ClientScript.GetPostBackClientEvent (this, String.Empty));
358                                         w.AddAttribute ("language", "javascript");
359                                 }
360
361                                 if (AccessKey.Length > 0)
362                                         w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
363
364                                 if (TabIndex != 0)
365                                         w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
366                                                              TabIndex.ToString (CultureInfo.InvariantCulture));
367
368                                 w.RenderBeginTag (HtmlTextWriterTag.Input);
369                                 w.RenderEndTag ();
370                                 string text = Text;
371                                 if (text != "") {
372 #if NET_2_0
373                                         if (labelAttributes != null)
374                                                 labelAttributes.AddAttributes (w);
375 #endif
376                                         w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
377                                         w.RenderBeginTag (HtmlTextWriterTag.Label);
378                                         w.Write (text);
379                                         w.RenderEndTag ();
380                                 }
381                         } else {
382                                 string text = Text;
383                                 if (text != "") {
384 #if NET_2_0
385                                         if (labelAttributes != null)
386                                                 labelAttributes.AddAttributes (w);
387 #endif
388                                         w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
389                                         w.RenderBeginTag (HtmlTextWriterTag.Label);
390                                         w.Write (text);
391                                         w.RenderEndTag ();
392                                 }
393
394                                 if (!Enabled)
395                                         w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
396
397                                 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
398                                 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
399                                 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
400                                 InternalAddAttributesToRender (w);
401                                 if (Checked)
402                                         w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
403
404                                 if (AutoPostBack){
405                                         w.AddAttribute (HtmlTextWriterAttribute.Onclick,
406                                                              Page.ClientScript.GetPostBackClientEvent (this, String.Empty));
407                                         w.AddAttribute ("language", "javascript");
408                                 }
409
410                                 if (AccessKey.Length > 0)
411                                         w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
412
413                                 if (TabIndex != 0)
414                                         w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
415                                                              TabIndex.ToString (NumberFormatInfo.InvariantInfo));
416
417                                 w.RenderBeginTag (HtmlTextWriterTag.Input);
418                                 w.RenderEndTag ();
419                         }
420
421                         if (need_span)
422                                 w.RenderEndTag ();
423                 }
424
425 #if NET_2_0
426                 protected virtual
427 #endif
428                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
429                 {
430                         string postedValue = postCollection[postDataKey];
431                         bool postedBool = ((postedValue != null) &&
432                                            (postedValue.Length > 0));
433                         
434                         if (Checked != postedBool) {
435                                 Checked = postedBool;
436                                 return (true);
437                         }
438
439                         return (false);
440                 }
441
442 #if NET_2_0
443                 protected virtual
444 #endif
445                 void RaisePostDataChangedEvent ()
446                 {
447 #if NET_2_0
448                         if (CausesValidation)
449                                 Page.Validate (ValidationGroup);
450 #endif
451                         OnCheckedChanged (EventArgs.Empty);
452                 }
453
454                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
455                 {
456                         return LoadPostData (postDataKey, postCollection);
457                 }
458                 
459                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
460                 {
461                         RaisePostDataChangedEvent ();
462                 }
463
464 #if NET_2_0
465                 protected override void AddAttributesToRender (HtmlTextWriter writer)
466                 {
467                         base.AddAttributesToRender (writer);
468                 }
469 #endif
470
471                 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
472                 {
473                 }
474         }
475 }