* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
index 6854ee8993c6b926d8ab2d4ae74484a49dba896f..5083d01eb275c412cd612a0128439e949dc75ae0 100644 (file)
@@ -1,16 +1,10 @@
 //
 // System.Web.UI.WebControls.CheckBox.cs
 //
-// Authors:
-//   Gaurav Vaish (gvaish@iitk.ac.in)
-//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+// Author:
+//      Dick Porter  <dick@ximian.com>
 //
-// (C) Gaurav Vaish (2002)
-// (C) 2003 Andreas Nahr
-// Thanks to Leen Toelen (toelen@hotmail.com)'s classes that helped me
-// to write the contents of the function LoadPostData(...)
-//
-
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Collections;
 using System.Collections.Specialized;
-using System.Globalization;
-using System.Web;
-using System.Web.UI;
 using System.ComponentModel;
-using System.ComponentModel.Design;
-
-namespace System.Web.UI.WebControls
-{
-       [DefaultEvent("CheckedChanged")]
-       [DefaultProperty("Text")]
-       [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
-       [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
+using System.Globalization;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls {
+       // CAS
+       [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       // attributes
+       [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
+       [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
+       [DefaultEvent ("CheckedChanged")]
+       [DefaultProperty ("Text")]
+#if NET_2_0
+       [ControlValueProperty ("Checked", null)]
+       [SupportsEventValidation]
+#endif         
        public class CheckBox : WebControl, IPostBackDataHandler
+#if NET_2_0
+       , ICheckBoxControl
+#endif
        {
-               private static readonly object CheckedChangedEvent = new object();
-               AttributeCollection commonAttrs;
+               string render_type;
+               AttributeCollection common_attrs;
+
+#if NET_2_0
+               AttributeCollection inputAttributes;
+               StateBag inputAttributesState;
+               AttributeCollection labelAttributes;
+               StateBag labelAttributesState;
+#endif
                
-               public CheckBox(): base(HtmlTextWriterTag.Input)
+               public CheckBox () : base (HtmlTextWriterTag.Input)
+               {
+                       render_type = "checkbox";
+               }
+
+               internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
                {
+                       this.render_type = render_type;
                }
 
-               [DefaultValue (false), WebCategory ("Behavior")]
-               [WebSysDescription ("The control automatically posts back after changing the text.")]
-               public virtual bool AutoPostBack
+               [DefaultValue (false)]
+#if NET_2_0
+               [Themeable (false)]
+#endif         
+               [WebSysDescription ("")]
+               [WebCategory ("Behavior")]
+               public virtual bool AutoPostBack 
                {
                        get {
-                                object o = ViewState ["AutoPostBack"];
-                                return (o == null) ? false : (bool) o;
+                               return (ViewState.GetBool ("AutoPostBack",
+                                                          false));
+                       }
+                       set {
+                               ViewState["AutoPostBack"] = value;
                        }
-
-                       set { ViewState ["AutoPostBack"] = value; }
                }
 
+#if NET_2_0
+               [DefaultValue (false)]
+               [Themeable (false)]
+               [MonoTODO]
+               [WebSysDescription ("")]
+               [WebCategory ("Behavior")]
+               public virtual bool CausesValidation 
+               {
+                       get { return ViewState.GetBool ("CausesValidation", false); }
+                       set { ViewState ["CausesValidation"] = value; }
+               }
+#endif         
+               
 
-               [DefaultValue (false), Bindable (true)]
-               [WebSysDescription ("Determines if the control is checked.")]
-               public virtual bool Checked
+               [DefaultValue (false)]
+#if NET_2_0
+               [Bindable (true, BindingDirection.TwoWay)]
+               [Themeable (false)]
+#else          
+               [Bindable (true)]
+#endif         
+               [WebSysDescription ("")]
+               [WebCategory ("Behavior")]
+               public virtual bool Checked 
                {
                        get {
-                               object o = ViewState ["Checked"];
-                               return (o == null) ? false : (bool) o;
+                               return (ViewState.GetBool ("Checked", false));
+                       }
+                       set {
+                               ViewState["Checked"] = value;
                        }
-
-                       set { ViewState ["Checked"] = value; }
                }
 
-               [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
-               [WebSysDescription ("The text that this control displays.")]
-               public virtual string Text
+#if NET_2_0
+               [Browsable (false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public AttributeCollection InputAttributes 
                {
                        get {
-                               object o = ViewState ["Text"];
-                               return (o == null) ? String.Empty : (string) o;
+                               if (inputAttributes == null) {
+                                       if (inputAttributesState == null) {
+                                               inputAttributesState = new StateBag (true);
+                                               if (IsTrackingViewState)
+                                                       inputAttributesState.TrackViewState();
+                                       }
+                                       inputAttributes = new AttributeCollection (inputAttributesState);
+                               }
+                               return inputAttributes;
                        }
+               }
 
-                       set { ViewState ["Text"] = value; }
+               [Browsable (false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public AttributeCollection LabelAttributes
+               {
+                       get {
+                               if (labelAttributes == null) {
+                                       if (labelAttributesState == null) {
+                                               labelAttributesState = new StateBag (true);
+                                               if (IsTrackingViewState)
+                                                       labelAttributesState.TrackViewState();
+                                       }
+                                       labelAttributes = new AttributeCollection (labelAttributesState);
+                               }
+                               return labelAttributes;
+                       }
                }
-               
-               private bool SaveCheckedViewState
+#endif         
+
+               [DefaultValue ("")]
+               [Bindable (true)]
+#if NET_2_0
+               [Localizable (true)]
+#endif         
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
+               public virtual string Text 
                {
                        get {
-                               if (Events [CheckedChangedEvent] != null || !Enabled)
-                                       return true;
+                               return (ViewState.GetString ("Text",
+                                                            String.Empty));
+                       }
+                       set {
+                               ViewState["Text"] = value;
+                       }
+               }
 
-                               Type type = GetType ();
-                               return (type != typeof (CheckBox) && type != typeof (RadioButton));
+               [DefaultValue (TextAlign.Right)]
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
+               public virtual TextAlign TextAlign
+               {
+                       get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
+                       set {
+                               if (value != TextAlign.Left &&
+                                   value != TextAlign.Right) {
+                                       throw new ArgumentOutOfRangeException ("value");
+                               }
+                               
+                               ViewState["TextAlign"] = value;
                        }
                }
 
+#if NET_2_0
+               [Themeable (false)]
+               [DefaultValue ("")]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Behavior")]
+               public virtual string ValidationGroup
+               {
+                       get { return ViewState.GetString ("ValidationGroup", String.Empty); }
+                       set { ViewState["ValidationGroup"] = value; }
+               }
+#endif         
 
-               [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
-               [WebSysDescription ("The alignment of the text.")]
-               public virtual TextAlign TextAlign
+               private static readonly object EventCheckedChanged = new object ();
+               [WebSysDescription ("")]
+               [WebCategory ("Action")]
+               public event EventHandler CheckedChanged 
                {
-                       get {
-                               object o = ViewState ["TextAlign"];
-                               return (o == null) ? TextAlign.Right : (TextAlign) o;
+                       add {
+                               Events.AddHandler (EventCheckedChanged, value);
+                       }
+                       remove {
+                               Events.RemoveHandler (EventCheckedChanged, value);
                        }
+               }
 
-                       set {
-                               if (!System.Enum.IsDefined (typeof (TextAlign), value))
-                                       throw new ArgumentException ();
-                               ViewState ["TextAlign"] = value;
+               protected virtual void OnCheckedChanged (EventArgs e)
+               {
+                       EventHandler handler = (EventHandler)Events[EventCheckedChanged];
+                       
+                       if (handler != null) {
+                               handler (this, e);
                        }
                }
 
-               [WebCategory ("Action")]
-               [WebSysDescription ("Raised when the control is checked or unchecked.")]
-               public event EventHandler CheckedChanged
+               internal virtual string NameAttribute 
                {
-                       add { Events.AddHandler (CheckedChangedEvent, value); }
-                       remove { Events.RemoveHandler (CheckedChangedEvent, value); }
+                       get {
+                               return (this.UniqueID);
+                       }
                }
                
-               protected virtual void OnCheckedChanged(EventArgs e)
+#if NET_2_0
+               protected override void LoadViewState (object savedState)
                {
-                       if(Events != null){
-                               EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
-                               if(eh != null)
-                                       eh (this, e);
+                       if (savedState == null) {
+                               base.LoadViewState (null);
+                               return;
+                       }
+
+                       Triplet saved = (Triplet) savedState;
+                       base.LoadViewState (saved.First);
+
+                       if (saved.Second != null) {
+                               if (inputAttributesState == null) {
+                                       inputAttributesState = new StateBag(true);
+                                       inputAttributesState.TrackViewState ();
+                               }
+                               inputAttributesState.LoadViewState (saved.Second);
+                       }
+
+                       if (saved.Third != null) {
+                               if (labelAttributesState == null) {
+                                       labelAttributesState = new StateBag(true);
+                                       labelAttributesState.TrackViewState ();
+                               }
+                               labelAttributesState.LoadViewState (saved.Third);
                        }
                }
-               
-               protected override void OnPreRender(EventArgs e)
+
+               protected override object SaveViewState ()
                {
-                       if (Page != null && Enabled) {
+                       object baseView = base.SaveViewState ();
+                       object inputAttrView = null;
+                       object labelAttrView = null;
+
+                       if (inputAttributesState != null)
+                               inputAttrView = inputAttributesState.SaveViewState ();
+
+                       if (labelAttributesState != null)
+                               labelAttrView = labelAttributesState.SaveViewState ();
+
+                       if (baseView == null && inputAttrView == null && labelAttrView == null)
+                               return null;
+
+                       return new Triplet (baseView, inputAttrView, labelAttrView);            
+               }
+
+               protected override void TrackViewState ()
+               {
+                       base.TrackViewState();
+                       if (inputAttributesState != null)
+                               inputAttributesState.TrackViewState ();
+                       if (labelAttributesState != null)
+                               labelAttributesState.TrackViewState ();
+               }
+#endif         
+
+#if NET_2_0
+               protected internal
+#else          
+               protected
+#endif         
+               override void OnPreRender (EventArgs e)
+               {
+                       base.OnPreRender (e);
+
+                       if (Page != null) {
                                Page.RegisterRequiresPostBack (this);
-                               if (AutoPostBack)
-                                       Page.RequiresPostBackScript ();
                        }
-
-                       if (!SaveCheckedViewState)
-                               ViewState.SetItemDirty ("Checked", false);
                }
 
                static bool IsInputOrCommonAttr (string attname)
                {
+                       attname = attname.ToUpper (CultureInfo.InvariantCulture);
                        switch (attname) {
                        case "VALUE":
                        case "CHECKED":
@@ -191,112 +346,182 @@ namespace System.Web.UI.WebControls
                        string [] keys = new string [k.Count];
                        k.CopyTo (keys, 0);
                        foreach (string key in keys) {
-                               if (!IsInputOrCommonAttr (key.ToUpper ()))
+                               if (!IsInputOrCommonAttr (key))
                                        continue;
-
-                               if (commonAttrs == null)
-                                       commonAttrs = new AttributeCollection (new StateBag ());
-
-                               commonAttrs [key] = Attributes [key];
+                               if (common_attrs == null)
+                                       common_attrs = new AttributeCollection (new StateBag ());
+                               common_attrs [key] = Attributes [key];
                                Attributes.Remove (key);
                        }
-
                        Attributes.AddAttributes (writer);
                }
-
-               protected override void Render (HtmlTextWriter writer)
+#if NET_2_0
+               protected internal
+#else          
+               protected
+#endif         
+               override void Render (HtmlTextWriter w)
                {
-                       bool hasBeginRendering = false;
-                       if(ControlStyleCreated && !ControlStyle.IsEmpty){
-                               hasBeginRendering = true;
-                               ControlStyle.AddAttributesToRender (writer, this);
-                       }
-                       
-                       if (!Enabled)\r
-                       {
-                               hasBeginRendering = true;\r
-                               writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");                              \r
+                       if (Page != null)
+                               Page.VerifyRenderingInServerForm (this);
+
+                       bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
+                       if (need_span)
+                               ControlStyle.AddAttributesToRender (w, this);
+
+                       if (!Enabled) {
+                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
+                               need_span = true;
                        }
 
-                       if (ToolTip.Length > 0){
-                               hasBeginRendering = true;
-                               writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
+                       string tt = ToolTip;
+                       if (tt != ""){
+                               w.AddAttribute ("title", tt);
+                               need_span = true;
                        }
 
                        if (Attributes.Count > 0){
-                               hasBeginRendering = true;
-                               AddAttributesForSpan (writer);
+                               AddAttributesForSpan (w);
+                               Attributes.AddAttributes (w);
+                               need_span = true;
                        }
 
-                       if (hasBeginRendering)
-                               writer.RenderBeginTag (HtmlTextWriterTag.Span);
-
-                       if (Text.Length > 0){
-                               TextAlign ta = TextAlign;
-                               if(ta == TextAlign.Right) {
-                                       if (commonAttrs != null)
-                                               commonAttrs.AddAttributes (writer);
-                                       RenderInputTag (writer, ClientID);
+                       if (need_span)
+                               w.RenderBeginTag (HtmlTextWriterTag.Span);
+
+                       TextAlign align = TextAlign;
+                       if (align == TextAlign.Right) {
+                               w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
+                               w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
+                               w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
+                               InternalAddAttributesToRender (w);
+                               if (Checked)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
+
+                               if (AutoPostBack){
+                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick,
+                                                       Page.ClientScript.GetPostBackEventReference (this, String.Empty));
+                                       w.AddAttribute ("language", "javascript");
                                }
-                               writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
-                               writer.RenderBeginTag (HtmlTextWriterTag.Label);
-                               writer.Write (Text);
-                               writer.RenderEndTag ();
-                               if(ta == TextAlign.Left) {
-                                       if (commonAttrs != null)
-                                               commonAttrs.AddAttributes (writer);
-                                       RenderInputTag (writer, ClientID);
+
+                               if (AccessKey.Length > 0)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
+
+                               if (TabIndex != 0)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
+                                                            TabIndex.ToString (CultureInfo.InvariantCulture));
+
+                               if (common_attrs != null)
+                                       common_attrs.AddAttributes (w);
+                               w.RenderBeginTag (HtmlTextWriterTag.Input);
+                               w.RenderEndTag ();
+                               string text = Text;
+                               if (text != "") {
+#if NET_2_0
+                                       if (labelAttributes != null)
+                                               labelAttributes.AddAttributes (w);
+#endif
+                                       w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
+                                       w.RenderBeginTag (HtmlTextWriterTag.Label);
+                                       w.Write (text);
+                                       w.RenderEndTag ();
                                }
                        } else {
-                               if (commonAttrs != null)
-                                       commonAttrs.AddAttributes (writer);
-                               RenderInputTag (writer, ClientID);
+                               string text = Text;
+                               if (text != "") {
+#if NET_2_0
+                                       if (labelAttributes != null)
+                                               labelAttributes.AddAttributes (w);
+#endif
+                                       w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
+                                       w.RenderBeginTag (HtmlTextWriterTag.Label);
+                                       w.Write (text);
+                                       w.RenderEndTag ();
+                               }
+
+                               w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
+                               w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
+                               w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
+                               InternalAddAttributesToRender (w);
+                               if (Checked)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
+
+                               if (AutoPostBack){
+                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick,
+                                                       Page.ClientScript.GetPostBackEventReference (this, String.Empty));
+                                       w.AddAttribute ("language", "javascript");
+                               }
+
+                               if (AccessKey.Length > 0)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
+
+                               if (TabIndex != 0)
+                                       w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
+                                                            TabIndex.ToString (NumberFormatInfo.InvariantInfo));
+
+                               if (common_attrs != null)
+                                       common_attrs.AddAttributes (w);
+                               w.RenderBeginTag (HtmlTextWriterTag.Input);
+                               w.RenderEndTag ();
                        }
 
-                       if (hasBeginRendering)
-                               writer.RenderEndTag ();
+                       if (need_span)
+                               w.RenderEndTag ();
                }
-               
-               internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
+
+#if NET_2_0
+               protected virtual
+#endif
+               bool LoadPostData (string postDataKey, NameValueCollection postCollection)
                {
                        if (!Enabled)
-                               writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
-
-                       writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
-                       writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
-                       writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
-                       if (Checked)
-                               writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
-
-                       if (AutoPostBack){
-                               writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
-                                                    Page.GetPostBackClientEvent (this, String.Empty));
-                               writer.AddAttribute ("language", "javascript");
-                       }
+                               return false;
 
-                       if (AccessKey.Length > 0)
-                               writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
+                       string postedValue = postCollection[postDataKey];
+                       bool postedBool = ((postedValue != null) &&
+                                          (postedValue.Length > 0));
+                       
+                       if (Checked != postedBool) {
+                               Checked = postedBool;
+                               return (true);
+                       }
 
-                       if (TabIndex != 0)
-                               writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
-                                                    TabIndex.ToString (NumberFormatInfo.InvariantInfo));
+                       return (false);
+               }
 
-                       writer.RenderBeginTag (HtmlTextWriterTag.Input);
-                       writer.RenderEndTag ();
+#if NET_2_0
+               protected virtual
+#endif
+               void RaisePostDataChangedEvent ()
+               {
+#if NET_2_0
+                       if (CausesValidation)
+                               Page.Validate (ValidationGroup);
+#endif
+                       OnCheckedChanged (EventArgs.Empty);
                }
-               
+
                bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
                {
-                       string postedVal = postCollection [postDataKey];                        \r
-                       bool haveData = ((postedVal != null)&& (postedVal.Length > 0));\r
-                       bool diff  = (haveData != Checked);
-                       Checked = haveData;
-                       return diff ;
+                       return LoadPostData (postDataKey, postCollection);
                }
                
-               void IPostBackDataHandler.RaisePostDataChangedEvent()
+               void IPostBackDataHandler.RaisePostDataChangedEvent ()
                {
-                       OnCheckedChanged (EventArgs.Empty);
+                       RaisePostDataChangedEvent ();
+               }
+
+#if NET_2_0
+               protected override void AddAttributesToRender (HtmlTextWriter writer)
+               {
+                       base.AddAttributesToRender (writer);
+               }
+#endif
+
+               internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
+               {
+                       if (!Enabled)
+                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
                }
        }
 }