* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
index c85b3eeb9ed0cf555e8e860e8695673ce05ca2c7..5083d01eb275c412cd612a0128439e949dc75ae0 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System.Web.UI;
+using System.Collections;
 using System.Collections.Specialized;
 using System.ComponentModel;
+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
@@ -44,6 +51,7 @@ namespace System.Web.UI.WebControls {
 #endif
        {
                string render_type;
+               AttributeCollection common_attrs;
 
 #if NET_2_0
                AttributeCollection inputAttributes;
@@ -174,15 +182,7 @@ namespace System.Web.UI.WebControls {
                [WebCategory ("Appearance")]
                public virtual TextAlign TextAlign
                {
-                       get {
-                               object o = ViewState["TextAlign"];
-
-                               if (o == null) {
-                                       return (TextAlign.Right);
-                               } else {
-                                       return ((TextAlign)o);
-                               }
-                       }
+                       get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
                        set {
                                if (value != TextAlign.Left &&
                                    value != TextAlign.Right) {
@@ -198,7 +198,7 @@ namespace System.Web.UI.WebControls {
                [DefaultValue ("")]
                [WebSysDescription ("")]
                [WebCategoryAttribute ("Behavior")]
-               public string ValidationGroup
+               public virtual string ValidationGroup
                {
                        get { return ViewState.GetString ("ValidationGroup", String.Empty); }
                        set { ViewState["ValidationGroup"] = value; }
@@ -234,56 +234,6 @@ namespace System.Web.UI.WebControls {
                        }
                }
                
-               protected override void AddAttributesToRender (HtmlTextWriter w)
-               {
-                       if (Page != null)
-                               Page.VerifyRenderingInServerForm (this);
-
-                       /* This is a nasty kludge to avoid rendering
-                        * "style" and the ControlStyle in checkboxes
-                        * (we use a surrounding <span> instead), but
-                        * still pass the unit test that shows the
-                        * style being rendered in multiple calls to
-                        * Render () (which means we can't just delete
-                        * Attributes["style"] or ControlStyle.Reset()
-                        * in Render ())
-                        */
-                       string css_style = Attributes ["style"];
-                       if (css_style != null) {
-                               Attributes.Remove ("style");
-                       }
-
-                       Style style = new Style ();
-                       if (ControlStyleCreated) {
-                               style.CopyFrom (ControlStyle);
-                               ControlStyle.Reset ();
-                       }
-                       
-                       base.AddAttributesToRender (w);
-
-                       if (css_style != null) {
-                               Attributes ["style"] = css_style;
-                       }
-                       if (!style.IsEmpty) {
-                               ApplyStyle (style);
-                       }
-                       
-                       InternalAddAttributesToRender (w);
-                       
-                       w.AddAttribute (HtmlTextWriterAttribute.Type,
-                                       render_type);
-                       w.AddAttribute (HtmlTextWriterAttribute.Name,
-                                       NameAttribute);
-                       
-                       if (AutoPostBack) {
-                               w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
-                       }
-
-                       if (Checked) {
-                               w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
-                       }
-               }
-
 #if NET_2_0
                protected override void LoadViewState (object savedState)
                {
@@ -354,20 +304,57 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               void RenderLabel (HtmlTextWriter w)
+               static bool IsInputOrCommonAttr (string attname)
                {
-                       if (Text.Length > 0) {
-#if NET_2_0
-                               if (labelAttributes != null)
-                                       labelAttributes.AddAttributes (w);
-#endif
-                               w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
-                               w.RenderBeginTag (HtmlTextWriterTag.Label);
-                               w.Write (this.Text);
-                               w.RenderEndTag ();
+                       attname = attname.ToUpper (CultureInfo.InvariantCulture);
+                       switch (attname) {
+                       case "VALUE":
+                       case "CHECKED":
+                       case "SIZE":
+                       case "MAXLENGTH":
+                       case "SRC":
+                       case "ALT":
+                       case "USEMAP":
+                       case "DISABLED":
+                       case "READONLY":
+                       case "ACCEPT":
+                       case "ACCESSKEY":
+                       case "TABINDEX":
+                       case "ONFOCUS":
+                       case "ONBLUR":
+                       case "ONSELECT":
+                       case "ONCHANGE":
+                       case "ONCLICK":
+                       case "ONDBLCLICK":
+                       case "ONMOUSEDOWN":
+                       case "ONMOUSEUP":
+                       case "ONMOUSEOVER":
+                       case "ONMOUSEMOVE":
+                       case "ONMOUSEOUT":
+                       case "ONKEYPRESS":
+                       case "ONKEYDOWN":
+                       case "ONKEYUP":
+                               return true;
+                       default:
+                               return false;
                        }
                }
-               
+
+               void AddAttributesForSpan (HtmlTextWriter writer)
+               {
+                       ICollection k = Attributes.Keys;
+                       string [] keys = new string [k.Count];
+                       k.CopyTo (keys, 0);
+                       foreach (string key in keys) {
+                               if (!IsInputOrCommonAttr (key))
+                                       continue;
+                               if (common_attrs == null)
+                                       common_attrs = new AttributeCollection (new StateBag ());
+                               common_attrs [key] = Attributes [key];
+                               Attributes.Remove (key);
+                       }
+                       Attributes.AddAttributes (writer);
+               }
 #if NET_2_0
                protected internal
 #else          
@@ -375,43 +362,111 @@ namespace System.Web.UI.WebControls {
 #endif         
                override void Render (HtmlTextWriter w)
                {
-                       bool control_style = false;
-                       
-                       /* Need to apply the styles around the text
-                        * label too
-                        */
-                       if (ControlStyleCreated) {
+                       if (Page != null)
+                               Page.VerifyRenderingInServerForm (this);
+
+                       bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
+                       if (need_span)
                                ControlStyle.AddAttributesToRender (w, this);
-                               w.RenderBeginTag (HtmlTextWriterTag.Span);
 
-                               control_style = true;
-                       } else if (Attributes ["style"] != null) {
-                               /* TODO: check if this or the style
-                                * has precendence, or if they should
-                                * be merged (if I can figure out how
-                                * to turn a CssStyleCollection into a
-                                * Style)
-                                */
-                               CssStyleCollection style = Attributes.CssStyle;
-                               
-                               w.AddAttribute (HtmlTextWriterAttribute.Style,
-                                               style.BagToString ());
-                               w.RenderBeginTag (HtmlTextWriterTag.Span);
+                       if (!Enabled) {
+                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
+                               need_span = true;
+                       }
 
-                               control_style = true;
+                       string tt = ToolTip;
+                       if (tt != ""){
+                               w.AddAttribute ("title", tt);
+                               need_span = true;
                        }
-                       
-                       if (TextAlign == TextAlign.Left) {
-                               RenderLabel (w);
-                               base.Render (w);
-                       } else {
-                               base.Render (w);
-                               RenderLabel (w);
+
+                       if (Attributes.Count > 0){
+                               AddAttributesForSpan (w);
+                               Attributes.AddAttributes (w);
+                               need_span = true;
                        }
 
-                       if (control_style) {
+                       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");
+                               }
+
+                               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 {
+                               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 (need_span)
+                               w.RenderEndTag ();
                }
 
 #if NET_2_0
@@ -419,6 +474,9 @@ namespace System.Web.UI.WebControls {
 #endif
                bool LoadPostData (string postDataKey, NameValueCollection postCollection)
                {
+                       if (!Enabled)
+                               return false;
+
                        string postedValue = postCollection[postDataKey];
                        bool postedBool = ((postedValue != null) &&
                                           (postedValue.Length > 0));
@@ -453,8 +511,17 @@ namespace System.Web.UI.WebControls {
                        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");
                }
        }
 }