2005-10-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Style.cs
index 7a28da8cbd8328df4e7c8e8c4359756782f085a5..91861f5733369d93f70853f51e4c50619287e94c 100644 (file)
 //
 //
 
-using System;
 using System.ComponentModel;
 using System.Drawing;
+using System.Security.Permissions;
 
-namespace System.Web.UI.WebControls 
-{
+namespace System.Web.UI.WebControls {
+
+       // CAS
+       [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       // attributes
+#if NET_2_0
+// Not until we actually have StyleConverter
+//     [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
+#else
        [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
+#endif
        [ToolboxItem("")]
        public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager 
        {
@@ -96,10 +105,14 @@ namespace System.Web.UI.WebControls
                #endregion      // Public Constructors
 
                #region Public Instance Properties
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Color), "")]
                [NotifyParentProperty(true)]
                [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Color BackColor 
                {
                        get 
@@ -119,10 +132,14 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Color), "")]
                [NotifyParentProperty(true)]
                [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Color BorderColor 
                {
                        get 
@@ -142,9 +159,13 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(BorderStyle.NotSet)]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public BorderStyle BorderStyle 
                {
                        get 
@@ -164,9 +185,13 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Unit), "")]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Unit BorderWidth 
                {
                        get 
@@ -193,6 +218,8 @@ namespace System.Web.UI.WebControls
 
                [DefaultValue("")]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public string CssClass 
                {
                        get 
@@ -214,6 +241,8 @@ namespace System.Web.UI.WebControls
 
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public FontInfo Font 
                {
                        get 
@@ -226,10 +255,14 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Color), "")]
                [NotifyParentProperty(true)]
                [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Color ForeColor 
                {
                        get 
@@ -249,9 +282,13 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Unit), "")]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Unit Height 
                {
                        get 
@@ -276,9 +313,13 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+#if !NET_2_0
                [Bindable(true)]
+#endif
                [DefaultValue(typeof (Unit), "")]
                [NotifyParentProperty(true)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
                public Unit Width 
                {
                        get 
@@ -305,11 +346,17 @@ namespace System.Web.UI.WebControls
                #endregion      // Public Instance Properties
 
                #region Protected Instance Properties
+#if NET_2_0
+               [Browsable (false)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               public virtual bool IsEmpty 
+#else
                protected internal virtual bool IsEmpty 
+#endif
                {
                        get 
                        {
-                               return (styles == 0);
+                               return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
                        }
                }
 
@@ -347,12 +394,93 @@ namespace System.Web.UI.WebControls
                                        writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
                        }
 
-                       CssStyleCollection attributes = new CssStyleCollection ();
-                       FillStyleAttributes (attributes);
-                       foreach (string attr in attributes.Keys)
-                               writer.AddStyleAttribute (attr, attributes [attr]);
+                       WriteStyleAttributes (writer);
                }
-                
+
+               void WriteStyleAttributes (HtmlTextWriter writer) 
+               {
+                       string          s;
+                       Color           color;
+                       BorderStyle     bs;
+                       Unit            u;
+
+                       if ((styles & Styles.BackColor) != 0) {
+                               color = (Color)viewstate["BackColor"];
+                               if (!color.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
+                       }
+
+                       if ((styles & Styles.BorderColor) != 0) {
+                               color = (Color)viewstate["BorderColor"];
+                               if (!color.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
+                       }
+
+                       if ((styles & Styles.BorderStyle) != 0) {
+                               bs = (BorderStyle)viewstate["BorderStyle"];
+                               if (bs != BorderStyle.NotSet) 
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
+                       }
+
+                       if ((styles & Styles.BorderWidth) != 0) {
+                               u = (Unit)viewstate["BorderWidth"];
+                               if (!u.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
+                       }
+
+                       if ((styles & Styles.ForeColor) != 0) {
+                               color = (Color)viewstate["ForeColor"];
+                               if (!color.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
+                       }
+
+                       if ((styles & Styles.Height) != 0) {
+                               u = (Unit)viewstate["Height"];
+                               if (!u.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
+                       }
+
+                       if ((styles & Styles.Width) != 0) {
+                               u = (Unit)viewstate["Width"];
+                               if (!u.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
+                       }
+
+                       if (fontinfo != null) {
+                               // Fonts are a bit weird
+                               if (fontinfo.Name != string.Empty) {
+                                       s = fontinfo.Names[0];
+                                       for (int i = 1; i < fontinfo.Names.Length; i++)
+                                               s += "," + fontinfo.Names[i];
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
+                               }
+
+                               if (fontinfo.Bold)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
+
+                               if (fontinfo.Italic)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
+
+                               if (!fontinfo.Size.IsEmpty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
+
+                               // These styles are munged into a attribute decoration
+                               s = string.Empty;
+
+                               if (fontinfo.Overline)
+                                       s += "overline ";
+
+                               if (fontinfo.Strikeout)
+                                       s += "line-through ";
+
+                               if (fontinfo.Underline)
+                                       s += "underline ";
+
+                               if (s != string.Empty)
+                                       writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
+                       }
+               }
+
                void FillStyleAttributes (CssStyleCollection attributes) 
                {
                        string          s;
@@ -553,6 +681,20 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               /*
+               internal void Print ()
+               {
+                       Console.WriteLine ("BackColor: {0}", BackColor);
+                       Console.WriteLine ("BorderColor: {0}", BorderColor);
+                       Console.WriteLine ("BorderStyle: {0}", BorderStyle);
+                       Console.WriteLine ("BorderWidth: {0}", BorderWidth);
+                       Console.WriteLine ("CssClass: {0}", CssClass);
+                       Console.WriteLine ("ForeColor: {0}", ForeColor);
+                       Console.WriteLine ("Height: {0}", Height);
+                       Console.WriteLine ("Width: {0}", Width);
+               }
+               */
+
                public virtual void Reset() 
                {
                        viewstate.Remove("BackColor");
@@ -569,11 +711,12 @@ namespace System.Web.UI.WebControls
                        }
                        styles = Styles.None;
                }
-
+#if ONLY_1_1
                public override string ToString() 
                {
                        return string.Empty;
                }
+#endif
                #endregion      // Public Instance Methods
 
                #region Protected Instance Methods
@@ -675,28 +818,6 @@ namespace System.Web.UI.WebControls
                }
                #endregion      // IStateManager Properties & Methods
 
-               #region REMOVE ME
-               internal static int BORDERWIDTH = 0xeadbeef;
-               internal static int FORECOLOR   = 0xeadbeef;
-
-               [Obsolete ("This method will be removed in Fresh")]
-               internal bool IsSet(int blah) 
-               {
-                       if (blah == BORDERWIDTH)
-                               return ((styles & Styles.BorderWidth) != 0);
-
-                       if (blah == FORECOLOR)
-                               return ((styles & Styles.ForeColor) != 0);
-
-                       return false;
-               }
-
-               [Obsolete ("This method will be removed in Fresh")]
-               internal void Set(int blah) 
-               {
-               }
-               #endregion      // REMOVE ME
-
 #if NET_2_0
                protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
                {
@@ -710,14 +831,19 @@ namespace System.Web.UI.WebControls
 
                public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
                {
-                       CssStyleCollection col = new CssStyleCollection ();
+                       CssStyleCollection col = new CssStyleCollection (new StateBag ());
                        FillStyleAttributes (col, resolver);
                        return col;
                }
 
                [MonoTODO]
+               [EditorBrowsable(EditorBrowsableState.Advanced)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [Browsable(false)]
                public string RegisteredCssClass {
                        get {
+                               if (registered_class == null)
+                                       registered_class = String.Empty;
                                return registered_class;
                        }
                }
@@ -736,16 +862,8 @@ namespace System.Web.UI.WebControls
                public void SetDirty ()
                {
                        if (viewstate != null)
-                               viewstate.SetDirty ();
+                               viewstate.SetDirty (true);
                }
-
-               public static bool IsStyleEmpty (Style s)
-               {
-                       if (s == null)
-                               return true;
-                       return s.IsEmpty;
-               }
-
 #endif
        }
 }