2 // System.Web.UI.WebControls.Style.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
14 using System.Collections;
16 using System.Globalization;
17 using System.ComponentModel;
21 namespace System.Web.UI.WebControls
24 [TypeConverter(typeof(ExpandableObjectConverter))]
25 public class Style : Component , IStateManager
27 internal static int MARKED = (0x01 << 0);
28 internal static int BACKCOLOR = (0x01 << 1);
29 internal static int BORDERCOLOR = (0x01 << 2);
30 internal static int BORDERSTYLE = (0x01 << 3);
31 internal static int BORDERWIDTH = (0x01 << 4);
32 internal static int CSSCLASS = (0x01 << 5);
33 internal static int FORECOLOR = (0x01 << 6);
34 internal static int HEIGHT = (0x01 << 7);
35 internal static int WIDTH = (0x01 << 8);
36 internal static int FONT_BOLD = (0x01 << 9);
37 internal static int FONT_ITALIC = (0x01 << 10);
38 internal static int FONT_NAMES = (0x01 << 11);
39 internal static int FONT_SIZE = (0x01 << 12);
40 internal static int FONT_STRIKE = (0x01 << 13);
41 internal static int FONT_OLINE = (0x01 << 14);
42 internal static int FONT_ULINE = (0x01 << 15);
44 internal static string selectionBitString = "_SBS";
51 private FontInfo font;
59 public Style (StateBag bag)
65 private void Initialize (StateBag bag)
72 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
73 protected internal StateBag ViewState {
75 if (viewState == null) {
76 viewState = new StateBag (false);
77 if (IsTrackingViewState)
78 viewState.TrackViewState ();
84 internal bool IsSet (int bit)
86 return ((selectionBits & bit) != 0x00);
89 internal virtual void Set (int bit)
92 if (IsTrackingViewState)
93 selectionBits |= MARKED;
96 [NotifyParentProperty (true)]
97 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
98 [TypeConverter (typeof (WebColorConverter))]
99 [WebSysDescription ("The background color for the WebControl.")]
100 public Color BackColor {
103 return (Color)ViewState["BackColor"];
107 ViewState["BackColor"] = value;
112 [NotifyParentProperty (true)]
113 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
114 [TypeConverter (typeof (WebColorConverter))]
115 [WebSysDescription ("The border color for the WebControl.")]
116 public Color BorderColor {
118 if (IsSet (BORDERCOLOR))
119 return (Color) ViewState ["BorderColor"];
123 ViewState ["BorderColor"] = value;
128 [NotifyParentProperty (true)]
129 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
130 [WebSysDescription ("The style/type of the border used for the WebControl.")]
131 public BorderStyle BorderStyle {
133 if (IsSet (BORDERSTYLE))
134 return (BorderStyle) ViewState ["BorderStyle"];
135 return BorderStyle.NotSet;
138 ViewState ["BorderStyle"] = value;
143 [NotifyParentProperty (true)]
144 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
145 [WebSysDescription ("The width of the border used for the WebControl.")]
146 public Unit BorderWidth {
148 if (IsSet (BORDERWIDTH))
149 return (Unit) ViewState ["BorderWidth"];
153 ViewState ["BorderWidth"] = value;
158 [NotifyParentProperty (true)]
159 [DefaultValue (""), WebCategory ("Appearance")]
160 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
161 public string CssClass {
163 if (IsSet (CSSCLASS))
164 return (string) ViewState["CssClass"];
168 ViewState ["CssClass"] = value;
173 [NotifyParentProperty (true)]
174 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
175 [TypeConverter (typeof (WebColorConverter))]
176 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
177 public Color ForeColor {
179 if (IsSet (FORECOLOR))
180 return (Color) ViewState ["ForeColor"];
184 ViewState ["ForeColor"] = value;
189 [NotifyParentProperty (true)]
190 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
191 [WebSysDescription ("The height of this WebControl.")]
195 return (Unit) ViewState ["Height"];
199 ViewState ["Height"] = value;
204 [NotifyParentProperty (true)]
205 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
206 [WebSysDescription ("The width of this WebControl.")]
210 return (Unit) ViewState ["Width"];
214 ViewState ["Width"] = value;
219 [NotifyParentProperty (true)]
220 [WebCategory ("Appearance")]
221 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
222 [WebSysDescription ("The font of this WebControl.")]
223 public FontInfo Font {
226 font = new FontInfo (this);
231 protected internal virtual bool IsEmpty
233 get { return (selectionBits == 0); }
236 private void AddColor (HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
239 writer.AddStyleAttribute (style, ColorTranslator.ToHtml (color));
242 private static string StringArrayToString (string [] array, char separator)
244 if (array.Length == 0)
246 StringBuilder sb = new StringBuilder ();
247 for (int i = 0; i < array.Length; i++) {
249 sb.Append (array [0]);
251 sb.Append (separator);
252 sb.Append (array [i]);
255 return sb.ToString ();
258 public void AddAttributesToRender (HtmlTextWriter writer)
260 AddAttributesToRender (writer, null);
263 public virtual void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
265 if (IsSet (BACKCOLOR))
266 AddColor (writer, HtmlTextWriterStyle.BackgroundColor, BackColor);
268 if (IsSet(BORDERCOLOR))
269 AddColor (writer, HtmlTextWriterStyle.BorderColor, BorderColor);
271 if (IsSet (FORECOLOR))
272 AddColor (writer, HtmlTextWriterStyle.Color, ForeColor);
274 if (IsSet (CSSCLASS)) {
275 string cssClass = (string) ViewState ["CssClass"];
276 if (cssClass.Length > 0)
277 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssClass);
280 if (!BorderWidth.IsEmpty) {
281 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth,
282 BorderWidth.ToString (CultureInfo.InvariantCulture));
284 if (BorderStyle != BorderStyle.NotSet) {
285 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
286 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
288 if (BorderWidth.Value != 0.0)
289 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
293 if (BorderStyle != BorderStyle.NotSet)
294 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
295 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
298 if (Font.Names.Length > 0)
299 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily,
300 StringArrayToString (Font.Names, ','));
302 if (!Font.Size.IsEmpty)
303 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize,
304 Font.Size.ToString (CultureInfo.InvariantCulture));
307 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
310 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
312 string textDecoration = String.Empty;
314 textDecoration += " line-through";
317 textDecoration += " underline";
320 textDecoration += " overline";
322 if (textDecoration.Length > 0)
323 writer.AddStyleAttribute( HtmlTextWriterStyle.TextDecoration, textDecoration);
326 if (IsSet (HEIGHT)) {
327 u = (Unit) ViewState ["Height"];
329 writer.AddStyleAttribute (HtmlTextWriterStyle.Height,
330 u.ToString (CultureInfo.InvariantCulture));
334 u = (Unit) ViewState ["Width"];
336 writer.AddStyleAttribute (HtmlTextWriterStyle.Width,
337 u.ToString (CultureInfo.InvariantCulture));
341 public virtual void CopyFrom (Style source)
343 if (source == null || source.IsEmpty)
346 Font.CopyFrom (source.Font);
347 if (source.IsSet (HEIGHT)&& (source.Height != Unit.Empty))
348 Height = source.Height;
350 if (source.IsSet (WIDTH)&& (source.Width != Unit.Empty))
351 Width = source.Width;
353 if (source.IsSet (BORDERCOLOR)&& (source.BorderColor != Color.Empty))
354 BorderColor = source.BorderColor;
356 if (source.IsSet (BORDERWIDTH)&& (source.BorderWidth != Unit.Empty))
357 BorderWidth = source.BorderWidth;
359 if (source.IsSet (BORDERSTYLE))
360 BorderStyle = source.BorderStyle;
362 if (source.IsSet (BACKCOLOR)&& (source.BackColor != Color.Empty))
363 BackColor = source.BackColor;
365 if (source.IsSet (CSSCLASS))
366 CssClass = source.CssClass;
368 if (source.IsSet (FORECOLOR)&& (source.ForeColor != Color.Empty))
369 ForeColor = source.ForeColor;
373 public virtual void MergeWith (Style with)
375 if (with == null || with.IsEmpty)
383 Font.MergeWith (with.Font);
384 if (!IsSet (HEIGHT) && with.Height != Unit.Empty)
385 Height = with.Height;
387 if (!IsSet(WIDTH) && with.Width != Unit.Empty)
390 if (!IsSet (BORDERCOLOR) && with.BorderColor != Color.Empty)
391 BorderColor = with.BorderColor;
393 if (!IsSet (BORDERWIDTH) && with.BorderWidth != Unit.Empty)
394 BorderWidth = with.BorderWidth;
396 if (!IsSet (BORDERSTYLE) && with.BorderStyle != BorderStyle.NotSet)
397 BorderStyle = with.BorderStyle;
399 if (!IsSet (BACKCOLOR) && with.BackColor != Color.Empty)
400 BackColor = with.BackColor;
402 if (!IsSet (CSSCLASS) && with.CssClass != String.Empty)
403 CssClass = with.CssClass;
405 if (!IsSet (FORECOLOR) && with.ForeColor != Color.Empty)
406 ForeColor = with.ForeColor;
409 public virtual void Reset ()
411 if (IsSet (BACKCOLOR))
412 ViewState.Remove ("BackColor");
414 if (IsSet (BORDERCOLOR))
415 ViewState.Remove ("BorderColor");
417 if (IsSet (BORDERSTYLE))
418 ViewState.Remove ("BorderStyle");
420 if (IsSet (BORDERWIDTH))
421 ViewState.Remove ("BorderWidth");
423 if (IsSet (CSSCLASS))
424 ViewState.Remove ("CssClass");
426 if (IsSet (FORECOLOR))
427 ViewState.Remove ("ForeColor");
430 ViewState.Remove ("Height");
433 ViewState.Remove( "Width");
438 selectionBits = 0x00;
441 protected bool IsTrackingViewState {
442 get { return marked; }
445 protected internal virtual void TrackViewState ()
448 ViewState.TrackViewState ();
453 protected internal virtual object SaveViewState ()
455 if (viewState != null) {
456 if (marked && IsSet (MARKED))
457 ViewState [selectionBitString] = selectionBits;
460 return ViewState.SaveViewState ();
466 protected internal void LoadViewState (object state)
468 if (state != null && selfStateBag)
469 ViewState.LoadViewState (state);
471 if (viewState != null) {
472 object o = ViewState [selectionBitString];
474 selectionBits = (int) o;
478 void IStateManager.LoadViewState(object state)
480 LoadViewState(state);
483 object IStateManager.SaveViewState ()
485 return SaveViewState ();
488 void IStateManager.TrackViewState ()
493 bool IStateManager.IsTrackingViewState {
494 get { return IsTrackingViewState; }
497 public override string ToString ()
502 protected internal void SetBit (int bit)