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
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
37 using System.Globalization;
38 using System.ComponentModel;
42 namespace System.Web.UI.WebControls
45 [TypeConverter(typeof(ExpandableObjectConverter))]
46 public class Style : Component , IStateManager
48 internal static int MARKED = (0x01 << 0);
49 internal static int BACKCOLOR = (0x01 << 1);
50 internal static int BORDERCOLOR = (0x01 << 2);
51 internal static int BORDERSTYLE = (0x01 << 3);
52 internal static int BORDERWIDTH = (0x01 << 4);
53 internal static int CSSCLASS = (0x01 << 5);
54 internal static int FORECOLOR = (0x01 << 6);
55 internal static int HEIGHT = (0x01 << 7);
56 internal static int WIDTH = (0x01 << 8);
57 internal static int FONT_BOLD = (0x01 << 9);
58 internal static int FONT_ITALIC = (0x01 << 10);
59 internal static int FONT_NAMES = (0x01 << 11);
60 internal static int FONT_SIZE = (0x01 << 12);
61 internal static int FONT_STRIKE = (0x01 << 13);
62 internal static int FONT_OLINE = (0x01 << 14);
63 internal static int FONT_ULINE = (0x01 << 15);
65 internal static string selectionBitString = "_SBS";
72 private FontInfo font;
80 public Style (StateBag bag)
86 private void Initialize (StateBag bag)
93 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94 protected internal StateBag ViewState {
96 if (viewState == null) {
97 viewState = new StateBag (false);
98 if (IsTrackingViewState)
99 viewState.TrackViewState ();
105 internal bool IsSet (int bit)
107 return ((selectionBits & bit) != 0x00);
110 internal virtual void Set (int bit)
112 selectionBits |= bit;
113 if (IsTrackingViewState)
114 selectionBits |= MARKED;
117 [NotifyParentProperty (true)]
118 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
119 [TypeConverter (typeof (WebColorConverter))]
120 [WebSysDescription ("The background color for the WebControl.")]
121 public Color BackColor {
124 return (Color)ViewState["BackColor"];
128 ViewState["BackColor"] = value;
133 [NotifyParentProperty (true)]
134 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
135 [TypeConverter (typeof (WebColorConverter))]
136 [WebSysDescription ("The border color for the WebControl.")]
137 public Color BorderColor {
139 if (IsSet (BORDERCOLOR))
140 return (Color) ViewState ["BorderColor"];
144 ViewState ["BorderColor"] = value;
149 [NotifyParentProperty (true)]
150 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
151 [WebSysDescription ("The style/type of the border used for the WebControl.")]
152 public BorderStyle BorderStyle {
154 if (IsSet (BORDERSTYLE))
155 return (BorderStyle) ViewState ["BorderStyle"];
156 return BorderStyle.NotSet;
159 ViewState ["BorderStyle"] = value;
164 [NotifyParentProperty (true)]
165 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
166 [WebSysDescription ("The width of the border used for the WebControl.")]
167 public Unit BorderWidth {
169 if (IsSet (BORDERWIDTH))
170 return (Unit) ViewState ["BorderWidth"];
174 ViewState ["BorderWidth"] = value;
179 [NotifyParentProperty (true)]
180 [DefaultValue (""), WebCategory ("Appearance")]
181 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
182 public string CssClass {
184 if (IsSet (CSSCLASS))
185 return (string) ViewState["CssClass"];
189 ViewState ["CssClass"] = value;
194 [NotifyParentProperty (true)]
195 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
196 [TypeConverter (typeof (WebColorConverter))]
197 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
198 public Color ForeColor {
200 if (IsSet (FORECOLOR))
201 return (Color) ViewState ["ForeColor"];
205 ViewState ["ForeColor"] = value;
210 [NotifyParentProperty (true)]
211 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
212 [WebSysDescription ("The height of this WebControl.")]
216 return (Unit) ViewState ["Height"];
220 ViewState ["Height"] = value;
225 [NotifyParentProperty (true)]
226 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
227 [WebSysDescription ("The width of this WebControl.")]
231 return (Unit) ViewState ["Width"];
235 ViewState ["Width"] = value;
240 [NotifyParentProperty (true)]
241 [WebCategory ("Appearance")]
242 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
243 [WebSysDescription ("The font of this WebControl.")]
244 public FontInfo Font {
247 font = new FontInfo (this);
252 protected internal virtual bool IsEmpty
254 get { return (selectionBits == 0); }
257 private void AddColor (HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
260 writer.AddStyleAttribute (style, ColorTranslator.ToHtml (color));
263 private static string StringArrayToString (string [] array, char separator)
265 if (array.Length == 0)
267 StringBuilder sb = new StringBuilder ();
268 for (int i = 0; i < array.Length; i++) {
270 sb.Append (array [0]);
272 sb.Append (separator);
273 sb.Append (array [i]);
276 return sb.ToString ();
279 public void AddAttributesToRender (HtmlTextWriter writer)
281 AddAttributesToRender (writer, null);
284 public virtual void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
286 if (IsSet (BACKCOLOR))
287 AddColor (writer, HtmlTextWriterStyle.BackgroundColor, BackColor);
289 if (IsSet(BORDERCOLOR))
290 AddColor (writer, HtmlTextWriterStyle.BorderColor, BorderColor);
292 if (IsSet (FORECOLOR))
293 AddColor (writer, HtmlTextWriterStyle.Color, ForeColor);
295 if (IsSet (CSSCLASS)) {
296 string cssClass = (string) ViewState ["CssClass"];
297 if (cssClass.Length > 0)
298 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssClass);
301 if (!BorderWidth.IsEmpty) {
302 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth,
303 BorderWidth.ToString (CultureInfo.InvariantCulture));
305 if (BorderStyle != BorderStyle.NotSet) {
306 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
307 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
309 if (BorderWidth.Value != 0.0)
310 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
314 if (BorderStyle != BorderStyle.NotSet)
315 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
316 Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
319 if (Font.Names.Length > 0)
320 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily,
321 StringArrayToString (Font.Names, ','));
323 if (!Font.Size.IsEmpty)
324 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize,
325 Font.Size.ToString (CultureInfo.InvariantCulture));
328 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
331 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
333 string textDecoration = String.Empty;
335 textDecoration += " line-through";
338 textDecoration += " underline";
341 textDecoration += " overline";
343 if (textDecoration.Length > 0)
344 writer.AddStyleAttribute( HtmlTextWriterStyle.TextDecoration, textDecoration);
347 if (IsSet (HEIGHT)) {
348 u = (Unit) ViewState ["Height"];
350 writer.AddStyleAttribute (HtmlTextWriterStyle.Height,
351 u.ToString (CultureInfo.InvariantCulture));
355 u = (Unit) ViewState ["Width"];
357 writer.AddStyleAttribute (HtmlTextWriterStyle.Width,
358 u.ToString (CultureInfo.InvariantCulture));
362 public virtual void CopyFrom (Style source)
364 if (source == null || source.IsEmpty)
367 Font.CopyFrom (source.Font);
368 if (source.IsSet (HEIGHT)&& (source.Height != Unit.Empty))
369 Height = source.Height;
371 if (source.IsSet (WIDTH)&& (source.Width != Unit.Empty))
372 Width = source.Width;
374 if (source.IsSet (BORDERCOLOR)&& (source.BorderColor != Color.Empty))
375 BorderColor = source.BorderColor;
377 if (source.IsSet (BORDERWIDTH)&& (source.BorderWidth != Unit.Empty))
378 BorderWidth = source.BorderWidth;
380 if (source.IsSet (BORDERSTYLE))
381 BorderStyle = source.BorderStyle;
383 if (source.IsSet (BACKCOLOR)&& (source.BackColor != Color.Empty))
384 BackColor = source.BackColor;
386 if (source.IsSet (CSSCLASS))
387 CssClass = source.CssClass;
389 if (source.IsSet (FORECOLOR)&& (source.ForeColor != Color.Empty))
390 ForeColor = source.ForeColor;
394 public virtual void MergeWith (Style with)
396 if (with == null || with.IsEmpty)
404 Font.MergeWith (with.Font);
405 if (!IsSet (HEIGHT) && with.Height != Unit.Empty)
406 Height = with.Height;
408 if (!IsSet(WIDTH) && with.Width != Unit.Empty)
411 if (!IsSet (BORDERCOLOR) && with.BorderColor != Color.Empty)
412 BorderColor = with.BorderColor;
414 if (!IsSet (BORDERWIDTH) && with.BorderWidth != Unit.Empty)
415 BorderWidth = with.BorderWidth;
417 if (!IsSet (BORDERSTYLE) && with.BorderStyle != BorderStyle.NotSet)
418 BorderStyle = with.BorderStyle;
420 if (!IsSet (BACKCOLOR) && with.BackColor != Color.Empty)
421 BackColor = with.BackColor;
423 if (!IsSet (CSSCLASS) && with.CssClass != String.Empty)
424 CssClass = with.CssClass;
426 if (!IsSet (FORECOLOR) && with.ForeColor != Color.Empty)
427 ForeColor = with.ForeColor;
430 public virtual void Reset ()
432 if (IsSet (BACKCOLOR))
433 ViewState.Remove ("BackColor");
435 if (IsSet (BORDERCOLOR))
436 ViewState.Remove ("BorderColor");
438 if (IsSet (BORDERSTYLE))
439 ViewState.Remove ("BorderStyle");
441 if (IsSet (BORDERWIDTH))
442 ViewState.Remove ("BorderWidth");
444 if (IsSet (CSSCLASS))
445 ViewState.Remove ("CssClass");
447 if (IsSet (FORECOLOR))
448 ViewState.Remove ("ForeColor");
451 ViewState.Remove ("Height");
454 ViewState.Remove( "Width");
459 selectionBits = 0x00;
462 protected bool IsTrackingViewState {
463 get { return marked; }
466 protected internal virtual void TrackViewState ()
469 ViewState.TrackViewState ();
474 protected internal virtual object SaveViewState ()
476 if (viewState != null) {
477 if (marked && IsSet (MARKED))
478 ViewState [selectionBitString] = selectionBits;
481 return ViewState.SaveViewState ();
487 protected internal void LoadViewState (object state)
489 if (state != null && selfStateBag)
490 ViewState.LoadViewState (state);
492 if (viewState != null) {
493 object o = ViewState [selectionBitString];
495 selectionBits = (int) o;
499 void IStateManager.LoadViewState(object state)
501 LoadViewState(state);
504 object IStateManager.SaveViewState ()
506 return SaveViewState ();
509 void IStateManager.TrackViewState ()
514 bool IStateManager.IsTrackingViewState {
515 get { return IsTrackingViewState; }
518 public override string ToString ()
523 protected internal void SetBit (int bit)