* Maintainer: gvaish@iitk.ac.in\r
* Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
* Implementation: yes\r
- * Status: 10%\r
+ * Status: 100%\r
*\r
* (C) Gaurav Vaish (2001)\r
*/\r
\r
using System;\r
+using System.Text;\r
using System.Collections;\r
using System.Drawing;\r
+using System.Globalization;\r
using System.ComponentModel;\r
using System.Web;\r
using System.Web.UI;\r
\r
namespace System.Web.UI.WebControls\r
{\r
- public class Style : Component, IStateManager\r
+ public class Style : Component , IStateManager\r
{\r
internal static int MARKED = (0x01 << 0);\r
internal static int BACKCOLOR = (0x01 << 1);\r
internal static int FONT_STRIKE = (0x01 << 13);\r
internal static int FONT_OLINE = (0x01 << 14);\r
internal static int FONT_ULINE = (0x01 << 15);\r
- \r
- internal static string selectionBitString = "_!SBS";\r
+\r
+ internal static string selectionBitString = "_SystemWebUIWebControlsStyle_SBS";\r
\r
private StateBag viewState;\r
- private bool marked;\r
private int selectionBits;\r
- private bool selfStateBag; \r
+ private bool selfStateBag;\r
\r
private FontInfo font;\r
\r
{\r
Initialize(null);\r
selfStateBag = true;\r
- \r
}\r
- \r
+\r
public Style(StateBag bag): base()\r
{\r
Initialize(bag);\r
selfStateBag = false;\r
}\r
- \r
+\r
private void Initialize(StateBag bag)\r
{\r
viewState = bag;\r
- marked = false;\r
- selectionBits = 0x00; \r
+ selectionBits = 0x00;\r
}\r
- \r
- StateBag ViewState\r
+\r
+ internal virtual StateBag ViewState\r
{\r
get\r
{\r
- if(stateBag == null)\r
+ if(viewState == null)\r
{\r
- stateBag = new stateBag(false);\r
+ viewState = new StateBag(false);\r
if(IsTrackingViewState)\r
- stateBag.TrackViewState();\r
+ viewState.TrackViewState();\r
}\r
- return stateBag;\r
+ return viewState;\r
}\r
}\r
\r
internal bool IsSet(int bit)\r
{\r
- return ( (selectionBits & bitIndex) != 0x00);\r
+ return ( (selectionBits & bit) != 0x00);\r
}\r
- \r
- virtual void Set(int bit)\r
+\r
+ internal virtual void Set(int bit)\r
{\r
selectionBits |= bit;\r
if(IsTrackingViewState)\r
selectionBits |= MARKED;\r
}\r
- \r
+\r
public Color BackColor\r
{\r
get\r
Set(BACKCOLOR);\r
}\r
}\r
- \r
+\r
public Color BorderColor\r
{\r
get\r
set\r
{\r
ViewState["BorderColor"] = value;\r
- Set(BORERCOLOR);\r
+ Set(BORDERCOLOR);\r
}\r
}\r
- \r
+\r
public BorderStyle BorderStyle\r
{\r
get\r
{\r
if(IsSet(BORDERSTYLE))\r
- return (Color)ViewState["BorderStyle"];\r
+ return (BorderStyle)ViewState["BorderStyle"];\r
return BorderStyle.NotSet;\r
}\r
set\r
Set(BORDERSTYLE);\r
}\r
}\r
- \r
+\r
public Unit BorderWidth\r
{\r
get\r
Set(BORDERWIDTH);\r
}\r
}\r
- \r
+\r
public string CssClass\r
{\r
get\r
{\r
- if(Set(CSSCLASS))\r
+ if(IsSet(CSSCLASS))\r
return (string)ViewState["CssClass"];\r
return string.Empty;\r
}\r
Set(CSSCLASS);\r
}\r
}\r
- \r
+\r
public Color ForeColor\r
{\r
get\r
Set(FORECOLOR);\r
}\r
}\r
- \r
+\r
public Unit Height\r
{\r
get\r
Set(HEIGHT);\r
}\r
}\r
- \r
+\r
public Unit Width\r
{\r
get\r
Set(HEIGHT);\r
}\r
}\r
- \r
+\r
public FontInfo Font\r
{\r
get\r
return font;\r
}\r
}\r
- \r
+\r
+ internal virtual bool IsEmpty\r
+ {\r
+ get\r
+ {\r
+ return (selectionBits != 0);\r
+ }\r
+ }\r
+\r
+ private void AddColor(HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)\r
+ {\r
+ if(!color.IsEmpty)\r
+ writer.AddStyleAttribute(style, ColorTranslator.ToHtml(color));\r
+ }\r
+\r
+ private static string StringArrayToString(string[] array, char separator)\r
+ {\r
+ if(array.Length == 0)\r
+ return String.Empty;\r
+ StringBuilder sb = new StringBuilder();\r
+ for(int i=0; i < array.Length; i++)\r
+ {\r
+ if(i==0)\r
+ {\r
+ sb.Append(array[0]);\r
+ } else\r
+ {\r
+ sb.Append(separator);\r
+ sb.Append(array[i]);\r
+ }\r
+ }\r
+ return sb.ToString();\r
+ }\r
+\r
public void AddAttributesToRender(HtmlTextWriter writer)\r
{\r
AddAttributesToRender(writer, null);\r
}\r
- \r
+\r
public void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)\r
{\r
- if(Set(CSSCLASS))\r
+ if(IsSet(BACKCOLOR))\r
+ {\r
+ AddColor(writer, HtmlTextWriterStyle.BackgroundColor, (Color)ViewState["BackColor"]);\r
+ }\r
+\r
+ if(IsSet(BORDERCOLOR))\r
+ {\r
+ AddColor(writer, HtmlTextWriterStyle.BorderColor, (Color)ViewState["BorderColor"]);\r
+ }\r
+\r
+ if(IsSet(FORECOLOR))\r
+ {\r
+ AddColor(writer, HtmlTextWriterStyle.Color, (Color)ViewState["ForeColor"]);\r
+ }\r
+\r
+ if(IsSet(CSSCLASS))\r
{\r
string cssClass = (string)ViewState["CssClass"];\r
- //if(cssClass.Length > 0)\r
- // writer.Add(HtmlTextWriterAttribute.\r
+ if(cssClass.Length > 0)\r
+ writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);\r
+ }\r
+\r
+ if(!BorderWidth.IsEmpty)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString(CultureInfo.InvariantCulture));\r
+ if(BorderStyle!=BorderStyle.NotSet)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));\r
+ } else\r
+ {\r
+ if(BorderWidth.Value != 0.0)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");\r
+ }\r
+ }\r
+ } else\r
+ {\r
+ if(BorderStyle!=BorderStyle.NotSet)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));\r
+ }\r
+ }\r
+\r
+ if(Font.Names.Length > 0)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, StringArrayToString(Font.Names,','));\r
+ }\r
+\r
+ if(!Font.Size.IsEmpty)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, Font.Size.ToString(CultureInfo.InvariantCulture));\r
+ }\r
+\r
+ if(Font.Bold)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");\r
+ }\r
+\r
+ if(Font.Italic)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");\r
+ }\r
+\r
+ string textDecoration = String.Empty;\r
+ if(Font.Strikeout)\r
+ {\r
+ textDecoration += " strikeout";\r
+ }\r
+ if(Font.Underline)\r
+ {\r
+ textDecoration += " underline";\r
+ }\r
+ if(Font.Overline)\r
+ {\r
+ textDecoration += " overline";\r
+ }\r
+ if(textDecoration.Length > 0)\r
+ {\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, textDecoration);\r
+ }\r
+\r
+ Unit u = Unit.Empty;\r
+ if(IsSet(HEIGHT))\r
+ {\r
+ u = (Unit)ViewState["Height"];\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.Height, u.ToString(CultureInfo.InvariantCulture));\r
+ }\r
+ if(IsSet(WIDTH))\r
+ {\r
+ u = (Unit)ViewState["Width"];\r
+ writer.AddStyleAttribute(HtmlTextWriterStyle.Width, u.ToString(CultureInfo.InvariantCulture));\r
+ }\r
+ }\r
+\r
+ public virtual void CopyFrom(Style source)\r
+ {\r
+ if(source!=null && !source.IsEmpty)\r
+ {\r
+ Font.CopyFrom(source.Font);\r
+ if(source.Height!=Unit.Empty)\r
+ {\r
+ Height = source.Height;\r
+ }\r
+ if(source.Width!=Unit.Empty)\r
+ {\r
+ Width = source.Width;\r
+ }\r
+ if(source.BorderColor!=Color.Empty)\r
+ {\r
+ BorderColor = source.BorderColor;\r
+ }\r
+ if(source.BorderWidth!=Unit.Empty)\r
+ {\r
+ BorderWidth = source.BorderWidth;\r
+ }\r
+ if(source.BorderStyle!=BorderStyle.NotSet)\r
+ {\r
+ BorderStyle = source.BorderStyle;\r
+ }\r
+ if(source.BackColor!=Color.Empty)\r
+ {\r
+ BackColor = source.BackColor;\r
+ }\r
+ if(source.CssClass!=String.Empty)\r
+ {\r
+ CssClass = source.CssClass;\r
+ }\r
+ if(source.ForeColor!=Color.Empty)\r
+ {\r
+ ForeColor = source.ForeColor;\r
+ }\r
+ }\r
+ }\r
+\r
+ public virtual void MergeWith(Style with)\r
+ {\r
+ if(with!=null && !with.IsEmpty)\r
+ {\r
+ if(IsEmpty)\r
+ {\r
+ CopyFrom(with);\r
+ return;\r
+ }\r
+\r
+ Font.MergeWith(with.Font);\r
+ if(!IsSet(HEIGHT) && with.Height!=Unit.Empty)\r
+ {\r
+ Height = with.Height;\r
+ }\r
+ if(!IsSet(WIDTH) && with.Width!=Unit.Empty)\r
+ {\r
+ Width = with.Width;\r
+ }\r
+ if(!IsSet(BORDERCOLOR) && with.BorderColor!=Color.Empty)\r
+ {\r
+ BorderColor = with.BorderColor;\r
+ }\r
+ if(!IsSet(BORDERWIDTH) && with.BorderWidth!=Unit.Empty)\r
+ {\r
+ BorderWidth = with.BorderWidth;\r
+ }\r
+ if(!IsSet(BORDERSTYLE) && with.BorderStyle!=BorderStyle.NotSet)\r
+ {\r
+ BorderStyle = with.BorderStyle;\r
+ }\r
+ if(!IsSet(BACKCOLOR) && with.BackColor!=Color.Empty)\r
+ {\r
+ BackColor = with.BackColor;\r
+ }\r
+ if(!IsSet(CSSCLASS) && with.CssClass!=String.Empty)\r
+ {\r
+ CssClass = with.CssClass;\r
+ }\r
+ if(!IsSet(FORECOLOR) && with.ForeColor!=Color.Empty)\r
+ {\r
+ ForeColor = with.ForeColor;\r
+ }\r
+ }\r
+ }\r
+ \r
+ public virtual void Reset()\r
+ {\r
+ if(IsSet(BACKCOLOR))\r
+ ViewState.Remove("BackColor");\r
+ if(IsSet(BORDERCOLOR))\r
+ ViewState.Remove("BorderColor");\r
+ if(IsSet(BORDERSTYLE))\r
+ ViewState.Remove("BorderStyle");\r
+ if(IsSet(BORDERWIDTH))\r
+ ViewState.Remove("BorderWidth");\r
+ if(IsSet(CSSCLASS))\r
+ ViewState.Remove("CssClass");\r
+ if(IsSet(FORECOLOR))\r
+ ViewState.Remove("ForeColor");\r
+ if(IsSet(HEIGHT))\r
+ ViewState.Remove("Height");\r
+ if(IsSet(WIDTH))\r
+ ViewState.Remove("Width");\r
+ if(font!=null)\r
+ font.Reset();\r
+ selectionBits = 0x00;\r
+ }\r
+\r
+ protected bool IsTrackingViewState\r
+ {\r
+ get\r
+ {\r
+ return ( (selectionBits & MARKED) != 0x00 );\r
+ }\r
+ }\r
+\r
+ protected internal virtual void TrackViewState()\r
+ {\r
+ if(viewState!=null)\r
+ ViewState.TrackViewState();\r
+ Set(MARKED);\r
+ }\r
+\r
+ protected internal object SaveViewState()\r
+ {\r
+ if(viewState != null)\r
+ {\r
+ if(IsSet(MARKED))\r
+ {\r
+ ViewState[selectionBitString] = selectionBits;\r
+ }\r
+ if(selfStateBag)\r
+ {\r
+ return ViewState.SaveViewState();\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ protected internal void LoadViewState(object state)\r
+ {\r
+ if(state!=null && selfStateBag)\r
+ {\r
+ ViewState.LoadViewState(state);\r
+ }\r
+ if(viewState!=null)\r
+ {\r
+ selectionBits = (int)ViewState[selectionBitString];\r
+ }\r
+ }\r
+ \r
+ void IStateManager.LoadViewState(object state)\r
+ {\r
+ LoadViewState(state);\r
+ }\r
+ \r
+ object IStateManager.SaveViewState()\r
+ {\r
+ return SaveViewState();\r
+ }\r
+ \r
+ void IStateManager.TrackViewState()\r
+ {\r
+ TrackViewState();\r
+ }\r
+ \r
+ bool IStateManager.IsTrackingViewState\r
+ {\r
+ get\r
+ {\r
+ return IsTrackingViewState;\r
}\r
}\r
}\r
}\r
-\r