2 * Namespace: System.Web.UI.WebControls
\r
5 * Author: Gaurav Vaish
\r
6 * Maintainer: gvaish@iitk.ac.in
\r
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
\r
8 * Implementation: yes
\r
11 * (C) Gaurav Vaish (2001)
\r
16 using System.Collections;
\r
17 using System.Drawing;
\r
18 using System.Globalization;
\r
19 using System.ComponentModel;
\r
21 using System.Web.UI;
\r
23 namespace System.Web.UI.WebControls
\r
25 public class Style : Component , IStateManager
\r
27 internal static int MARKED = (0x01 << 0);
\r
28 internal static int BACKCOLOR = (0x01 << 1);
\r
29 internal static int BORDERCOLOR = (0x01 << 2);
\r
30 internal static int BORDERSTYLE = (0x01 << 3);
\r
31 internal static int BORDERWIDTH = (0x01 << 4);
\r
32 internal static int CSSCLASS = (0x01 << 5);
\r
33 internal static int FORECOLOR = (0x01 << 6);
\r
34 internal static int HEIGHT = (0x01 << 7);
\r
35 internal static int WIDTH = (0x01 << 8);
\r
36 internal static int FONT_BOLD = (0x01 << 9);
\r
37 internal static int FONT_ITALIC = (0x01 << 10);
\r
38 internal static int FONT_NAMES = (0x01 << 11);
\r
39 internal static int FONT_SIZE = (0x01 << 12);
\r
40 internal static int FONT_STRIKE = (0x01 << 13);
\r
41 internal static int FONT_OLINE = (0x01 << 14);
\r
42 internal static int FONT_ULINE = (0x01 << 15);
\r
44 internal static string selectionBitString = "_SystemWebUIWebControlsStyle_SBS";
\r
46 private StateBag viewState;
\r
47 private int selectionBits;
\r
48 private bool selfStateBag;
\r
50 private FontInfo font;
\r
55 selfStateBag = true;
\r
58 public Style(StateBag bag): base()
\r
61 selfStateBag = false;
\r
64 private void Initialize(StateBag bag)
\r
67 selectionBits = 0x00;
\r
70 internal virtual StateBag ViewState
\r
74 if(viewState == null)
\r
76 viewState = new StateBag(false);
\r
77 if(IsTrackingViewState)
\r
78 viewState.TrackViewState();
\r
84 internal bool IsSet(int bit)
\r
86 return ( (selectionBits & bit) != 0x00);
\r
89 internal virtual void Set(int bit)
\r
91 selectionBits |= bit;
\r
92 if(IsTrackingViewState)
\r
93 selectionBits |= MARKED;
\r
96 public Color BackColor
\r
100 if(IsSet(BACKCOLOR))
\r
101 return (Color)ViewState["BackColor"];
\r
102 return Color.Empty;
\r
106 ViewState["BackColor"] = value;
\r
111 public Color BorderColor
\r
115 if(IsSet(BORDERCOLOR))
\r
116 return (Color)ViewState["BorderColor"];
\r
117 return Color.Empty;
\r
121 ViewState["BorderColor"] = value;
\r
126 public BorderStyle BorderStyle
\r
130 if(IsSet(BORDERSTYLE))
\r
131 return (BorderStyle)ViewState["BorderStyle"];
\r
132 return BorderStyle.NotSet;
\r
136 ViewState["BorderStyle"] = value;
\r
141 public Unit BorderWidth
\r
145 if(IsSet(BORDERWIDTH))
\r
146 return (Unit)ViewState["BorderWidth"];
\r
151 ViewState["BorderWidth"] = value;
\r
156 public string CssClass
\r
160 if(IsSet(CSSCLASS))
\r
161 return (string)ViewState["CssClass"];
\r
162 return string.Empty;
\r
166 ViewState["CssClass"] = value;
\r
171 public Color ForeColor
\r
175 if(IsSet(FORECOLOR))
\r
176 return (Color)ViewState["ForeColor"];
\r
177 return Color.Empty;
\r
181 ViewState["ForeColor"] = value;
\r
191 return (Unit)ViewState["Height"];
\r
196 ViewState["Height"] = value;
\r
206 return (Unit)ViewState["Width"];
\r
211 ViewState["Width"] = value;
\r
216 public FontInfo Font
\r
221 font = new FontInfo(this);
\r
226 internal virtual bool IsEmpty
\r
230 return (selectionBits != 0);
\r
234 private void AddColor(HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
\r
237 writer.AddStyleAttribute(style, ColorTranslator.ToHtml(color));
\r
240 private static string StringArrayToString(string[] array, char separator)
\r
242 if(array.Length == 0)
\r
243 return String.Empty;
\r
244 StringBuilder sb = new StringBuilder();
\r
245 for(int i=0; i < array.Length; i++)
\r
249 sb.Append(array[0]);
\r
252 sb.Append(separator);
\r
253 sb.Append(array[i]);
\r
256 return sb.ToString();
\r
259 public void AddAttributesToRender(HtmlTextWriter writer)
\r
261 AddAttributesToRender(writer, null);
\r
264 public void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
\r
266 if(IsSet(BACKCOLOR))
\r
268 AddColor(writer, HtmlTextWriterStyle.BackgroundColor, (Color)ViewState["BackColor"]);
\r
271 if(IsSet(BORDERCOLOR))
\r
273 AddColor(writer, HtmlTextWriterStyle.BorderColor, (Color)ViewState["BorderColor"]);
\r
276 if(IsSet(FORECOLOR))
\r
278 AddColor(writer, HtmlTextWriterStyle.Color, (Color)ViewState["ForeColor"]);
\r
281 if(IsSet(CSSCLASS))
\r
283 string cssClass = (string)ViewState["CssClass"];
\r
284 if(cssClass.Length > 0)
\r
285 writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
\r
288 if(!BorderWidth.IsEmpty)
\r
290 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString(CultureInfo.InvariantCulture));
\r
291 if(BorderStyle!=BorderStyle.NotSet)
\r
293 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
\r
296 if(BorderWidth.Value != 0.0)
\r
298 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
\r
303 if(BorderStyle!=BorderStyle.NotSet)
\r
305 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
\r
309 if(Font.Names.Length > 0)
\r
311 writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, StringArrayToString(Font.Names,','));
\r
314 if(!Font.Size.IsEmpty)
\r
316 writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, Font.Size.ToString(CultureInfo.InvariantCulture));
\r
321 writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
\r
326 writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");
\r
329 string textDecoration = String.Empty;
\r
332 textDecoration += " strikeout";
\r
336 textDecoration += " underline";
\r
340 textDecoration += " overline";
\r
342 if(textDecoration.Length > 0)
\r
344 writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, textDecoration);
\r
347 Unit u = Unit.Empty;
\r
350 u = (Unit)ViewState["Height"];
\r
351 writer.AddStyleAttribute(HtmlTextWriterStyle.Height, u.ToString(CultureInfo.InvariantCulture));
\r
355 u = (Unit)ViewState["Width"];
\r
356 writer.AddStyleAttribute(HtmlTextWriterStyle.Width, u.ToString(CultureInfo.InvariantCulture));
\r
360 public virtual void CopyFrom(Style source)
\r
362 if(source!=null && !source.IsEmpty)
\r
364 Font.CopyFrom(source.Font);
\r
365 if(source.Height!=Unit.Empty)
\r
367 Height = source.Height;
\r
369 if(source.Width!=Unit.Empty)
\r
371 Width = source.Width;
\r
373 if(source.BorderColor!=Color.Empty)
\r
375 BorderColor = source.BorderColor;
\r
377 if(source.BorderWidth!=Unit.Empty)
\r
379 BorderWidth = source.BorderWidth;
\r
381 if(source.BorderStyle!=BorderStyle.NotSet)
\r
383 BorderStyle = source.BorderStyle;
\r
385 if(source.BackColor!=Color.Empty)
\r
387 BackColor = source.BackColor;
\r
389 if(source.CssClass!=String.Empty)
\r
391 CssClass = source.CssClass;
\r
393 if(source.ForeColor!=Color.Empty)
\r
395 ForeColor = source.ForeColor;
\r
400 public virtual void MergeWith(Style with)
\r
402 if(with!=null && !with.IsEmpty)
\r
410 Font.MergeWith(with.Font);
\r
411 if(!IsSet(HEIGHT) && with.Height!=Unit.Empty)
\r
413 Height = with.Height;
\r
415 if(!IsSet(WIDTH) && with.Width!=Unit.Empty)
\r
417 Width = with.Width;
\r
419 if(!IsSet(BORDERCOLOR) && with.BorderColor!=Color.Empty)
\r
421 BorderColor = with.BorderColor;
\r
423 if(!IsSet(BORDERWIDTH) && with.BorderWidth!=Unit.Empty)
\r
425 BorderWidth = with.BorderWidth;
\r
427 if(!IsSet(BORDERSTYLE) && with.BorderStyle!=BorderStyle.NotSet)
\r
429 BorderStyle = with.BorderStyle;
\r
431 if(!IsSet(BACKCOLOR) && with.BackColor!=Color.Empty)
\r
433 BackColor = with.BackColor;
\r
435 if(!IsSet(CSSCLASS) && with.CssClass!=String.Empty)
\r
437 CssClass = with.CssClass;
\r
439 if(!IsSet(FORECOLOR) && with.ForeColor!=Color.Empty)
\r
441 ForeColor = with.ForeColor;
\r
446 public virtual void Reset()
\r
448 if(IsSet(BACKCOLOR))
\r
449 ViewState.Remove("BackColor");
\r
450 if(IsSet(BORDERCOLOR))
\r
451 ViewState.Remove("BorderColor");
\r
452 if(IsSet(BORDERSTYLE))
\r
453 ViewState.Remove("BorderStyle");
\r
454 if(IsSet(BORDERWIDTH))
\r
455 ViewState.Remove("BorderWidth");
\r
456 if(IsSet(CSSCLASS))
\r
457 ViewState.Remove("CssClass");
\r
458 if(IsSet(FORECOLOR))
\r
459 ViewState.Remove("ForeColor");
\r
461 ViewState.Remove("Height");
\r
463 ViewState.Remove("Width");
\r
466 selectionBits = 0x00;
\r
469 protected bool IsTrackingViewState
\r
473 return ( (selectionBits & MARKED) != 0x00 );
\r
477 protected internal virtual void TrackViewState()
\r
479 if(viewState!=null)
\r
480 ViewState.TrackViewState();
\r
484 protected internal object SaveViewState()
\r
486 if(viewState != null)
\r
490 ViewState[selectionBitString] = selectionBits;
\r
494 return ViewState.SaveViewState();
\r
500 protected internal void LoadViewState(object state)
\r
502 if(state!=null && selfStateBag)
\r
504 ViewState.LoadViewState(state);
\r
506 if(viewState!=null)
\r
508 selectionBits = (int)ViewState[selectionBitString];
\r
512 void IStateManager.LoadViewState(object state)
\r
514 LoadViewState(state);
\r
517 object IStateManager.SaveViewState()
\r
519 return SaveViewState();
\r
522 void IStateManager.TrackViewState()
\r
527 bool IStateManager.IsTrackingViewState
\r
531 return IsTrackingViewState;
\r