2005-10-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Style.cs
index a0dc409f8294d331255f9362f18afba09fa332e7..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))]
@@ -342,7 +346,13 @@ 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 
                        {
@@ -384,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;
@@ -590,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");
@@ -606,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
@@ -725,7 +831,7 @@ 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;
                }
@@ -736,6 +842,8 @@ namespace System.Web.UI.WebControls
                [Browsable(false)]
                public string RegisteredCssClass {
                        get {
+                               if (registered_class == null)
+                                       registered_class = String.Empty;
                                return registered_class;
                        }
                }
@@ -756,14 +864,6 @@ namespace System.Web.UI.WebControls
                        if (viewstate != null)
                                viewstate.SetDirty (true);
                }
-
-               public static bool IsStyleEmpty (Style s)
-               {
-                       if (s == null)
-                               return true;
-                       return s.IsEmpty;
-               }
-
 #endif
        }
 }