public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
{
[Flags]
- internal enum Styles
- {
- None = 0,
- BackColor = 0x00000001,
- BorderColor = 0x00000002,
- BorderStyle = 0x00000004,
- BorderWidth = 0x00000008,
- CssClass = 0x00000010,
- Font = 0x00000020,
- ForeColor = 0x00000040,
+ enum Styles
+ {
+ BackColor = 0x00000008,
+ BorderColor = 0x00000010,
+ BorderStyle = 0x00000040,
+ BorderWidth = 0x00000020,
+ CssClass = 0x00000002,
+ Font = 0x00000001,
+ ForeColor = 0x00000004,
Height = 0x00000080,
Width = 0x00000100,
-
- // from TableStyle (which doesn't override IsEmpty)
- BackImageUrl = 0x00000200,
- CellPadding = 0x00000400,
- CellSpacing = 0x00000800,
- GridLines = 0x00001000,
- HorizontalAlign = 0x00002000,
-
- // from TableItemStyle (which doesn't override IsEmpty neither)
- VerticalAlign = 0x00004000,
- Wrap = 0x00008000,
-
- // from DataGridPagerStyle (and, once again, no IsEmpty override)
- Mode = 0x00010000,
- NextPageText = 0x00020000,
- PageButtonCount = 0x00040000,
- Position = 0x00080000,
- PrevPageText = 0x00100000,
- Visible = 0x00200000
-
}
#region Fields
- internal Styles styles;
+ private int styles;
internal StateBag viewstate;
private FontInfo fontinfo;
private bool tracking;
{
get
{
- if ((styles & Styles.BackColor) == 0)
+ if (!CheckBit ((int) Styles.BackColor))
{
return Color.Empty;
}
set
{
viewstate["BackColor"] = value;
- styles |= Styles.BackColor;
+ SetBit ((int) Styles.BackColor);
}
}
{
get
{
- if ((styles & Styles.BorderColor) == 0)
+ if (!CheckBit ((int) Styles.BorderColor))
{
return Color.Empty;
}
set
{
viewstate["BorderColor"] = value;
- styles |= Styles.BorderColor;
+ SetBit ((int) Styles.BorderColor);
}
}
{
get
{
- if ((styles & Styles.BorderStyle) == 0)
+ if (!CheckBit ((int) Styles.BorderStyle))
{
return BorderStyle.NotSet;
}
set
{
viewstate["BorderStyle"] = value;
- styles |= Styles.BorderStyle;
+ SetBit ((int) Styles.BorderStyle);
}
}
{
get
{
- if ((styles & Styles.BorderWidth) == 0)
+ if (!CheckBit ((int) Styles.BorderWidth))
{
return Unit.Empty;
}
}
viewstate["BorderWidth"] = value;
- styles |= Styles.BorderWidth;
+ SetBit ((int) Styles.BorderWidth);
}
}
{
get
{
- if ((styles & Styles.CssClass) == 0)
+ if (!CheckBit ((int) Styles.CssClass))
{
return string.Empty;
}
set
{
viewstate["CssClass"] = value;
- styles |= Styles.CssClass;
+ SetBit ((int) Styles.CssClass);
}
}
{
get
{
- if ((styles & Styles.ForeColor) == 0)
+ if (!CheckBit ((int) Styles.ForeColor))
{
return Color.Empty;
}
set
{
viewstate["ForeColor"] = value;
- styles |= Styles.ForeColor;
+ SetBit ((int) Styles.ForeColor);
}
}
{
get
{
- if ((styles & Styles.Height) == 0)
+ if (!CheckBit ((int) Styles.Height))
{
return Unit.Empty;
}
}
viewstate["Height"] = value;
- styles |= Styles.Height;
+ SetBit ((int) Styles.Height);
}
}
{
get
{
- if ((styles & Styles.Width) == 0)
+ if (!CheckBit ((int) Styles.Width))
{
return Unit.Empty;
}
}
viewstate["Width"] = value;
- styles |= Styles.Width;
+ SetBit ((int) Styles.Width);
}
}
#endregion // Public Instance Properties
{
get
{
+#if NET_2_0
+ return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty) && RegisteredCssClass.Length == 0);
+#else
return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
+#endif
}
}
}
#endregion // Protected Instance Properties
+ #region Internal Instance Properties
+ internal bool AlwaysRenderTextDecoration
+ {
+ get
+ {
+ if (viewstate["AlwaysRenderTextDecoration"] == null)
+ return false;
+ return (bool)viewstate["AlwaysRenderTextDecoration"];
+ }
+
+ set
+ {
+ viewstate["AlwaysRenderTextDecoration"] = value;
+ }
+ }
+ #endregion // Internal Instance Properties
+
#region Public Instance Methods
public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
{
- if ((styles & Styles.CssClass) != 0)
+#if NET_2_0
+ if (RegisteredCssClass.Length > 0) {
+ if (CssClass.Length > 0)
+ writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass + " " + RegisteredCssClass);
+ else
+ writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
+ }
+ else
+#endif
{
- string s = (string)viewstate["CssClass"];
- if (s != string.Empty)
- writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
+ if (CssClass.Length > 0)
+ writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass);
+ WriteStyleAttributes (writer);
}
-
- WriteStyleAttributes (writer);
}
void WriteStyleAttributes (HtmlTextWriter writer)
{
- string s;
+#if NET_2_0
+ CssStyleCollection col = new CssStyleCollection (new StateBag ());
+ FillStyleAttributes (col, null);
+ foreach (string key in col.Keys) {
+ writer.AddStyleAttribute (key, col [key]);
+ }
+#else
+ string s;
Color color;
BorderStyle bs;
Unit u;
- if ((styles & Styles.BackColor) != 0) {
+ if (CheckBit ((int) Styles.BackColor)) {
color = (Color)viewstate["BackColor"];
if (!color.IsEmpty)
writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
}
- if ((styles & Styles.BorderColor) != 0) {
+ if (CheckBit ((int) Styles.BorderColor)) {
color = (Color)viewstate["BorderColor"];
if (!color.IsEmpty)
writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
}
bool have_width = false;
- if ((styles & Styles.BorderWidth) != 0) {
+ if (CheckBit ((int) Styles.BorderWidth)) {
u = (Unit)viewstate["BorderWidth"];
if (!u.IsEmpty) {
if (u.Value > 0)
}
}
- if ((styles & Styles.BorderStyle) != 0) {
+ if (CheckBit ((int) Styles.BorderStyle)) {
bs = (BorderStyle)viewstate["BorderStyle"];
if (bs != BorderStyle.NotSet)
writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
else {
- if ((styles & Styles.BorderWidth) != 0)
+ if (CheckBit ((int) Styles.BorderWidth))
writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
}
} else if (have_width) {
writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
}
- if ((styles & Styles.ForeColor) != 0) {
+ if (CheckBit ((int) Styles.ForeColor)) {
color = (Color)viewstate["ForeColor"];
if (!color.IsEmpty)
writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
}
- if ((styles & Styles.Height) != 0) {
+ if (CheckBit ((int) Styles.Height)) {
u = (Unit)viewstate["Height"];
if (!u.IsEmpty)
writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
}
- if ((styles & Styles.Width) != 0) {
+ if (CheckBit ((int) Styles.Width)) {
u = (Unit)viewstate["Width"];
if (!u.IsEmpty)
writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
if (fontinfo.Underline)
s += "underline ";
- s = (s != "") ? s : "none";
- writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
+ s = (s != "") ? s : AlwaysRenderTextDecoration ? "none" : "";
+ if (s != "")
+ writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
}
+#endif
}
+#if NET_2_0
void FillStyleAttributes (CssStyleCollection attributes)
{
- string s;
Color color;
BorderStyle bs;
Unit u;
- if ((styles & Styles.BackColor) != 0)
+ if (CheckBit ((int) Styles.BackColor))
{
color = (Color)viewstate["BackColor"];
if (!color.IsEmpty)
attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
}
- if ((styles & Styles.BorderColor) != 0)
+ if (CheckBit ((int) Styles.BorderColor))
{
color = (Color)viewstate["BorderColor"];
if (!color.IsEmpty)
attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
}
- if ((styles & Styles.BorderStyle) != 0)
- {
- bs = (BorderStyle)viewstate["BorderStyle"];
- if (bs != BorderStyle.NotSet)
- attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
+ bool have_width = false;
+ if (CheckBit ((int) Styles.BorderWidth)) {
+ u = (Unit) viewstate ["BorderWidth"];
+ if (!u.IsEmpty) {
+ if (u.Value > 0)
+ have_width = true;
+ attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
+ }
}
- if ((styles & Styles.BorderWidth) != 0)
- {
- u = (Unit)viewstate["BorderWidth"];
- if (!u.IsEmpty)
- attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
+ if (CheckBit ((int) Styles.BorderStyle)) {
+ bs = (BorderStyle) viewstate ["BorderStyle"];
+ if (bs != BorderStyle.NotSet)
+ attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
+ else if (have_width)
+ attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
+ }
+ else if (have_width) {
+ attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
}
- if ((styles & Styles.ForeColor) != 0)
+ if (CheckBit ((int) Styles.ForeColor))
{
color = (Color)viewstate["ForeColor"];
if (!color.IsEmpty)
attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
}
- if ((styles & Styles.Height) != 0)
+ if (CheckBit ((int) Styles.Height))
{
u = (Unit)viewstate["Height"];
if (!u.IsEmpty)
attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
}
- if ((styles & Styles.Width) != 0)
+ if (CheckBit ((int) Styles.Width))
{
u = (Unit)viewstate["Width"];
if (!u.IsEmpty)
attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
}
- if (!Font.IsEmpty) {
- // 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];
- }
- attributes.Add (HtmlTextWriterStyle.FontFamily, s);
- }
-
- if (fontinfo.Bold)
- {
- attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
- }
-
- if (fontinfo.Italic)
- {
- attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
- }
-
- if (!fontinfo.Size.IsEmpty)
- {
- attributes.Add (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 ";
- }
-
- s = (s != "") ? s : "none";
- attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
- }
+ Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
}
+#endif
public virtual void CopyFrom(Style s)
{
Font.CopyFrom(s.fontinfo);
}
- if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
+ if ((s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
{
this.BackColor = s.BackColor;
}
- if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
+ if ((s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
{
this.BorderColor = s.BorderColor;
}
- if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
+ if ((s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
{
this.BorderStyle = s.BorderStyle;
}
- if (((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
+ if ((s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
{
this.BorderWidth = s.BorderWidth;
}
- if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
+ if ((s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
{
this.CssClass = s.CssClass;
}
- if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
+ if ((s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
{
this.ForeColor = s.ForeColor;
}
- if (((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
+ if ((s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
{
this.Height = s.Height;
}
- if (((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
+ if ((s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
{
this.Width = s.Width;
}
Font.MergeWith(s.fontinfo);
}
- if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
+ if ((!CheckBit ((int) Styles.BackColor)) && (s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
{
this.BackColor = s.BackColor;
}
- if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
+ if ((!CheckBit ((int) Styles.BorderColor)) && (s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
{
this.BorderColor = s.BorderColor;
}
- if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
+ if ((!CheckBit ((int) Styles.BorderStyle)) && (s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
{
this.BorderStyle = s.BorderStyle;
}
- if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
+ if ((!CheckBit ((int) Styles.BorderWidth)) && (s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
{
this.BorderWidth = s.BorderWidth;
}
- if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
+ if ((!CheckBit ((int) Styles.CssClass)) && (s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
{
this.CssClass = s.CssClass;
}
- if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
+ if ((!CheckBit ((int) Styles.ForeColor)) && (s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
{
this.ForeColor = s.ForeColor;
}
- if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
+ if ((!CheckBit ((int) Styles.Height)) && (s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
{
this.Height = s.Height;
}
- if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
+ if ((!CheckBit ((int) Styles.Width)) && (s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
{
this.Width = s.Width;
}
{
fontinfo.Reset();
}
- styles = Styles.None;
+ styles = 0;
}
#if ONLY_1_1
public override string ToString()
viewstate.LoadViewState(state);
// Update our style
- this.styles = Styles.None;
+ this.styles = 0;
if (viewstate["BackColor"] != null)
{
- styles |= Styles.BackColor;
+ styles |= (int) Styles.BackColor;
}
if (viewstate["BorderColor"] != null)
{
- styles |= Styles.BorderColor;
+ styles |= (int) Styles.BorderColor;
}
if (viewstate["BorderStyle"] != null)
{
- styles |= Styles.BorderStyle;
+ styles |= (int) Styles.BorderStyle;
}
if (viewstate["BorderWidth"] != null)
{
- styles |= Styles.BorderWidth;
+ styles |= (int) Styles.BorderWidth;
}
if (viewstate["CssClass"] != null)
{
- styles |= Styles.CssClass;
+ styles |= (int) Styles.CssClass;
}
if (viewstate["ForeColor"] != null)
{
- styles |= Styles.ForeColor;
+ styles |= (int) Styles.ForeColor;
}
if (viewstate["Height"] != null)
{
- styles |= Styles.Height;
+ styles |= (int) Styles.Height;
}
if (viewstate["Width"] != null)
{
- styles |= Styles.Width;
+ styles |= (int) Styles.Width;
}
Font.LoadViewState();
protected internal virtual object SaveViewState ()
{
- if (styles != Styles.None || !Font.IsEmpty)
+ if (styles != 0 || !Font.IsEmpty)
{
return viewstate.SaveViewState();
}
return null;
}
- [MonoTODO]
protected internal virtual void SetBit( int bit )
{
- throw new NotImplementedException();
+ styles |= bit;
}
- protected internal virtual void TrackViewState()
+ internal bool CheckBit (int bit) {
+ return (styles & bit) != 0;
+ }
+
+ protected internal virtual void TrackViewState ()
{
tracking = true;
viewstate.TrackViewState();
}
}
- internal virtual void CopyTextStylesFrom (Style source)
- {
- // Need to ask lluis if we need fonts, too
- if ((styles & Styles.ForeColor) != 0) {
+ internal void CopyTextStylesFrom (Style source) {
+ // Used primary for TreeView and Menu
+ if (source.CheckBit ((int) Styles.ForeColor)) {
ForeColor = source.ForeColor;
}
- if ((styles & Styles.BackColor) != 0) {
- BackColor = source.BackColor;
+ if (!source.Font.IsEmpty) {
+ Font.CopyFrom (source.Font);
}
}
+ internal void AddCssClass (string cssClass) {
+ if (String.IsNullOrEmpty (cssClass))
+ return;
+
+ if (CssClass.Length > 0)
+ CssClass += " ";
+ CssClass += cssClass;
+ }
+
public void SetDirty ()
{
if (viewstate != null)