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)
71 protected internal StateBag ViewState {
73 if (viewState == null) {
74 viewState = new StateBag (false);
75 if (IsTrackingViewState)
76 viewState.TrackViewState ();
82 internal bool IsSet (int bit)
84 return ((selectionBits & bit) != 0x00);
87 internal virtual void Set (int bit)
90 if (IsTrackingViewState)
91 selectionBits |= MARKED;
94 [NotifyParentProperty (true)]
95 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
96 [TypeConverter (typeof (WebColorConverter))]
97 [WebSysDescription ("The background color for the WebControl.")]
98 public Color BackColor {
101 return (Color)ViewState["BackColor"];
105 ViewState["BackColor"] = value;
110 [NotifyParentProperty (true)]
111 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
112 [TypeConverter (typeof (WebColorConverter))]
113 [WebSysDescription ("The border color for the WebControl.")]
114 public Color BorderColor {
116 if (IsSet (BORDERCOLOR))
117 return (Color) ViewState ["BorderColor"];
121 ViewState ["BorderColor"] = value;
126 [NotifyParentProperty (true)]
127 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
128 [WebSysDescription ("The style/type of the border used for the WebControl.")]
129 public BorderStyle BorderStyle {
131 if (IsSet (BORDERSTYLE))
132 return (BorderStyle) ViewState ["BorderStyle"];
133 return BorderStyle.NotSet;
136 ViewState ["BorderStyle"] = value;
141 [NotifyParentProperty (true)]
142 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
143 [WebSysDescription ("The width of the border used for the WebControl.")]
144 public Unit BorderWidth {
146 if (IsSet (BORDERWIDTH))
147 return (Unit) ViewState ["BorderWidth"];
151 ViewState ["BorderWidth"] = value;
156 [NotifyParentProperty (true)]
157 [DefaultValue (""), WebCategory ("Appearance")]
158 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
159 public string CssClass {
161 if (IsSet (CSSCLASS))
162 return (string) ViewState["CssClass"];
166 ViewState ["CssClass"] = value;
171 [NotifyParentProperty (true)]
172 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
173 [TypeConverter (typeof (WebColorConverter))]
174 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
175 public Color ForeColor {
177 if (IsSet (FORECOLOR))
178 return (Color) ViewState ["ForeColor"];
182 ViewState ["ForeColor"] = value;
187 [NotifyParentProperty (true)]
188 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
189 [WebSysDescription ("The height of this WebControl.")]
193 return (Unit) ViewState ["Height"];
197 ViewState ["Height"] = value;
202 [NotifyParentProperty (true)]
203 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
204 [WebSysDescription ("The width of this WebControl.")]
208 return (Unit) ViewState ["Width"];
212 ViewState ["Width"] = value;
217 [NotifyParentProperty (true)]
218 [WebCategory ("Appearance")]
219 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
220 [WebSysDescription ("The font of this WebControl.")]
221 public FontInfo Font {
224 font = new FontInfo (this);
229 protected internal virtual bool IsEmpty
231 get { return (selectionBits == 0); }
234 private void AddColor (HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
237 writer.AddStyleAttribute (style, ColorTranslator.ToHtml (color));
240 private static string StringArrayToString (string [] array, char separator)
242 if (array.Length == 0)
244 StringBuilder sb = new StringBuilder ();
245 for (int i = 0; i < array.Length; i++) {
247 sb.Append (array [0]);
249 sb.Append (separator);
250 sb.Append (array [i]);
253 return sb.ToString ();
256 public void AddAttributesToRender (HtmlTextWriter writer)
258 AddAttributesToRender (writer, null);
261 public virtual void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
263 if (IsSet (BACKCOLOR))
264 AddColor (writer, HtmlTextWriterStyle.BackgroundColor, BackColor);
266 if (IsSet(BORDERCOLOR))
267 AddColor (writer, HtmlTextWriterStyle.BorderColor, BorderColor);
269 if (IsSet (FORECOLOR))
270 AddColor (writer, HtmlTextWriterStyle.Color, ForeColor);
272 if (IsSet (CSSCLASS)) {
273 string cssClass = (string) ViewState ["CssClass"];
274 if (cssClass.Length > 0)
275 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssClass);
278 if (!BorderWidth.IsEmpty) {
279 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth,
280 BorderWidth.ToString (CultureInfo.InvariantCulture));
282 if (BorderStyle != BorderStyle.NotSet) {
283 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
284 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
286 if (BorderWidth.Value != 0.0)
287 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
291 if (BorderStyle != BorderStyle.NotSet)
292 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
293 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
296 if (Font.Names.Length > 0)
297 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily,
298 StringArrayToString (Font.Names, ','));
300 if (!Font.Size.IsEmpty)
301 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize,
302 Font.Size.ToString (CultureInfo.InvariantCulture));
305 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
308 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
310 string textDecoration = String.Empty;
312 textDecoration += " line-through";
315 textDecoration += " underline";
318 textDecoration += " overline";
320 if (textDecoration.Length > 0)
321 writer.AddStyleAttribute( HtmlTextWriterStyle.TextDecoration, textDecoration);
324 if (IsSet (HEIGHT)) {
325 u = (Unit) ViewState ["Height"];
327 writer.AddStyleAttribute (HtmlTextWriterStyle.Height,
328 u.ToString (CultureInfo.InvariantCulture));
332 u = (Unit) ViewState ["Width"];
334 writer.AddStyleAttribute (HtmlTextWriterStyle.Width,
335 u.ToString (CultureInfo.InvariantCulture));
339 public virtual void CopyFrom (Style source)
341 if (source == null || source.IsEmpty)
344 Font.CopyFrom (source.Font);
345 if (source.IsSet (HEIGHT))
346 Height = source.Height;
348 if (source.IsSet (WIDTH))
349 Width = source.Width;
351 if (source.IsSet (BORDERCOLOR))
352 BorderColor = source.BorderColor;
354 if (source.IsSet (BORDERWIDTH))
355 BorderWidth = source.BorderWidth;
357 if (source.IsSet (BORDERSTYLE))
358 BorderStyle = source.BorderStyle;
360 if (source.IsSet (BACKCOLOR))
361 BackColor = source.BackColor;
363 if (source.IsSet (CSSCLASS))
364 CssClass = source.CssClass;
366 if (source.IsSet (FORECOLOR))
367 ForeColor = source.ForeColor;
371 public virtual void MergeWith (Style with)
373 if (with == null || with.IsEmpty)
381 Font.MergeWith (with.Font);
382 if (!IsSet (HEIGHT) && with.Height != Unit.Empty)
383 Height = with.Height;
385 if (!IsSet(WIDTH) && with.Width != Unit.Empty)
388 if (!IsSet (BORDERCOLOR) && with.BorderColor != Color.Empty)
389 BorderColor = with.BorderColor;
391 if (!IsSet (BORDERWIDTH) && with.BorderWidth != Unit.Empty)
392 BorderWidth = with.BorderWidth;
394 if (!IsSet (BORDERSTYLE) && with.BorderStyle != BorderStyle.NotSet)
395 BorderStyle = with.BorderStyle;
397 if (!IsSet (BACKCOLOR) && with.BackColor != Color.Empty)
398 BackColor = with.BackColor;
400 if (!IsSet (CSSCLASS) && with.CssClass != String.Empty)
401 CssClass = with.CssClass;
403 if (!IsSet (FORECOLOR) && with.ForeColor != Color.Empty)
404 ForeColor = with.ForeColor;
407 public virtual void Reset ()
409 if (IsSet (BACKCOLOR))
410 ViewState.Remove ("BackColor");
412 if (IsSet (BORDERCOLOR))
413 ViewState.Remove ("BorderColor");
415 if (IsSet (BORDERSTYLE))
416 ViewState.Remove ("BorderStyle");
418 if (IsSet (BORDERWIDTH))
419 ViewState.Remove ("BorderWidth");
421 if (IsSet (CSSCLASS))
422 ViewState.Remove ("CssClass");
424 if (IsSet (FORECOLOR))
425 ViewState.Remove ("ForeColor");
428 ViewState.Remove ("Height");
431 ViewState.Remove( "Width");
436 selectionBits = 0x00;
439 protected bool IsTrackingViewState {
440 get { return marked; }
443 protected internal virtual void TrackViewState ()
446 ViewState.TrackViewState ();
451 protected internal virtual object SaveViewState ()
453 if (viewState != null) {
455 ViewState [selectionBitString] = selectionBits;
458 return ViewState.SaveViewState ();
464 protected internal void LoadViewState (object state)
466 if (state != null && selfStateBag)
467 ViewState.LoadViewState (state);
469 if (viewState != null) {
470 object o = ViewState [selectionBitString];
472 selectionBits = (int) o;
476 void IStateManager.LoadViewState(object state)
478 LoadViewState(state);
481 object IStateManager.SaveViewState ()
483 return SaveViewState ();
486 void IStateManager.TrackViewState ()
491 bool IStateManager.IsTrackingViewState {
492 get { return IsTrackingViewState; }
495 public override string ToString ()