1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Dennis Bartok (pbartok@novell.com)
28 using System.ComponentModel;
31 namespace System.Web.UI.WebControls
33 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
35 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
41 BackColor = 0x00000001,
42 BorderColor = 0x00000002,
43 BorderStyle = 0x00000004,
44 BorderWidth = 0x00000008,
45 CssClass = 0x00000010,
47 ForeColor = 0x00000040,
51 // from TableStyle (which doesn't override IsEmpty)
52 BackImageUrl = 0x00000200,
53 CellPadding = 0x00000400,
54 CellSpacing = 0x00000800,
55 GridLines = 0x00001000,
56 HorizontalAlign = 0x00002000,
58 // from TableItemStyle (which doesn't override IsEmpty neither)
59 VerticalAlign = 0x00004000,
62 // from DataGridPagerStyle (and, once again, no IsEmpty override)
64 NextPageText = 0x00020000,
65 PageButtonCount = 0x00040000,
66 Position = 0x00080000,
67 PrevPageText = 0x00100000,
73 internal Styles styles;
74 internal StateBag viewstate;
75 private FontInfo fontinfo;
76 private bool tracking;
78 private string registered_class;
82 #region Public Constructors
83 public Style() : this(new StateBag())
87 public Style(System.Web.UI.StateBag bag)
92 viewstate = new StateBag();
96 #endregion // Public Constructors
98 #region Public Instance Properties
100 [DefaultValue("Color [Empty]")]
101 [NotifyParentProperty(true)]
102 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
103 public Color BackColor
107 if ((styles & Styles.BackColor) == 0)
112 return (Color)viewstate["BackColor"];
117 viewstate["BackColor"] = value;
118 styles |= Styles.BackColor;
123 [DefaultValue("Color [Empty]")]
124 [NotifyParentProperty(true)]
125 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
126 public Color BorderColor
130 if ((styles & Styles.BorderColor) == 0)
135 return (Color)viewstate["BorderColor"];
140 viewstate["BorderColor"] = value;
141 styles |= Styles.BorderColor;
146 [DefaultValue(BorderStyle.NotSet)]
147 [NotifyParentProperty(true)]
148 public BorderStyle BorderStyle
152 if ((styles & Styles.BorderStyle) == 0)
154 return BorderStyle.NotSet;
157 return (BorderStyle)viewstate["BorderStyle"];
162 viewstate["BorderStyle"] = value;
163 styles |= Styles.BorderStyle;
169 [NotifyParentProperty(true)]
170 public Unit BorderWidth
174 if ((styles & Styles.BorderWidth) == 0)
179 return (Unit)viewstate["BorderWidth"];
186 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
189 viewstate["BorderWidth"] = value;
190 styles |= Styles.BorderWidth;
195 [NotifyParentProperty(true)]
196 public string CssClass
200 if ((styles & Styles.CssClass) == 0)
205 return (string)viewstate["CssClass"];
210 viewstate["CssClass"] = value;
211 styles |= Styles.CssClass;
215 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
216 [NotifyParentProperty(true)]
221 if (fontinfo == null)
223 fontinfo = new FontInfo(this);
230 [DefaultValue("Color [Empty]")]
231 [NotifyParentProperty(true)]
232 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
233 public Color ForeColor
237 if ((styles & Styles.ForeColor) == 0)
242 return (Color)viewstate["ForeColor"];
247 viewstate["ForeColor"] = value;
248 styles |= Styles.ForeColor;
254 [NotifyParentProperty(true)]
259 if ((styles & Styles.Height) == 0)
264 return (Unit)viewstate["Height"];
271 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
274 viewstate["Height"] = value;
275 styles |= Styles.Height;
281 [NotifyParentProperty(true)]
286 if ((styles & Styles.Width) == 0)
291 return (Unit)viewstate["Width"];
298 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
301 viewstate["Width"] = value;
302 styles |= Styles.Width;
305 #endregion // Public Instance Properties
307 #region Protected Instance Properties
308 protected internal virtual bool IsEmpty
312 return (styles == 0);
316 protected bool IsTrackingViewState
325 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
326 protected internal StateBag ViewState
333 #endregion // Protected Instance Properties
335 #region Public Instance Methods
336 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
338 AddAttributesToRender(writer, null);
341 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
343 if ((styles & Styles.CssClass) != 0)
345 string s = (string)viewstate["CssClass"];
346 if (s != string.Empty)
347 writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
350 CssStyleCollection attributes = new CssStyleCollection ();
351 FillStyleAttributes (attributes);
352 foreach (string attr in attributes.Keys)
353 writer.AddStyleAttribute (attr, attributes [attr]);
356 void FillStyleAttributes (CssStyleCollection attributes)
363 if ((styles & Styles.BackColor) != 0)
365 color = (Color)viewstate["BackColor"];
367 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
370 if ((styles & Styles.BorderColor) != 0)
372 color = (Color)viewstate["BorderColor"];
374 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
377 if ((styles & Styles.BorderStyle) != 0)
379 bs = (BorderStyle)viewstate["BorderStyle"];
380 if (bs != BorderStyle.NotSet)
381 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
384 if ((styles & Styles.BorderWidth) != 0)
386 u = (Unit)viewstate["BorderWidth"];
388 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
391 if ((styles & Styles.ForeColor) != 0)
393 color = (Color)viewstate["ForeColor"];
395 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
398 if ((styles & Styles.Height) != 0)
400 u = (Unit)viewstate["Height"];
402 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
405 if ((styles & Styles.Width) != 0)
407 u = (Unit)viewstate["Width"];
409 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
412 if (fontinfo != null) {
413 // Fonts are a bit weird
414 if (fontinfo.Name != string.Empty)
416 s = fontinfo.Names[0];
417 for (int i = 1; i < fontinfo.Names.Length; i++)
419 s += "," + fontinfo.Names[i];
421 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
426 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
431 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
434 if (!fontinfo.Size.IsEmpty)
436 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
439 // These styles are munged into a attribute decoration
442 if (fontinfo.Overline)
447 if (fontinfo.Strikeout)
449 s += "line-through ";
452 if (fontinfo.Underline)
457 if (s != string.Empty)
459 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
464 public virtual void CopyFrom(Style s)
466 if ((s == null) || s.IsEmpty)
471 if (s.fontinfo != null)
473 Font.CopyFrom(s.fontinfo);
476 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
478 this.BackColor = s.BackColor;
480 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
482 this.BorderColor = s.BorderColor;
484 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
486 this.BorderStyle = s.BorderStyle;
488 if (((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
490 this.BorderWidth = s.BorderWidth;
492 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
494 this.CssClass = s.CssClass;
496 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
498 this.ForeColor = s.ForeColor;
500 if (((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
502 this.Height = s.Height;
504 if (((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
506 this.Width = s.Width;
510 public virtual void MergeWith(Style s)
512 if ((s == null) || (s.IsEmpty))
517 if (s.fontinfo != null)
519 Font.MergeWith(s.fontinfo);
522 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
524 this.BackColor = s.BackColor;
526 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
528 this.BorderColor = s.BorderColor;
530 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
532 this.BorderStyle = s.BorderStyle;
534 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
536 this.BorderWidth = s.BorderWidth;
538 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
540 this.CssClass = s.CssClass;
542 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
544 this.ForeColor = s.ForeColor;
546 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
548 this.Height = s.Height;
550 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
552 this.Width = s.Width;
556 public virtual void Reset()
558 viewstate.Remove("BackColor");
559 viewstate.Remove("BorderColor");
560 viewstate.Remove("BorderStyle");
561 viewstate.Remove("BorderWidth");
562 viewstate.Remove("CssClass");
563 viewstate.Remove("ForeColor");
564 viewstate.Remove("Height");
565 viewstate.Remove("Width");
566 if (fontinfo != null)
570 styles = Styles.None;
573 public override string ToString()
577 #endregion // Public Instance Methods
579 #region Protected Instance Methods
580 protected internal void LoadViewState(object state)
582 viewstate.LoadViewState(state);
585 this.styles = Styles.None;
587 if (viewstate["BackColor"] != null)
589 styles |= Styles.BackColor;
591 if (viewstate["BorderColor"] != null)
593 styles |= Styles.BorderColor;
595 if (viewstate["BorderStyle"] != null)
597 styles |= Styles.BorderStyle;
599 if (viewstate["BorderWidth"] != null)
601 styles |= Styles.BorderWidth;
603 if (viewstate["CssClass"] != null)
605 styles |= Styles.CssClass;
607 if (viewstate["ForeColor"] != null)
609 styles |= Styles.ForeColor;
611 if (viewstate["Height"] != null)
613 styles |= Styles.Height;
615 if (viewstate["Width"] != null)
617 styles |= Styles.Width;
619 if (fontinfo != null) {
620 fontinfo.LoadViewState();
623 LoadViewStateInternal();
626 internal virtual void LoadViewStateInternal()
631 protected internal virtual object SaveViewState ()
633 if (styles != Styles.None)
635 return viewstate.SaveViewState();
641 protected internal virtual void SetBit( int bit )
643 throw new NotImplementedException();
646 protected internal virtual void TrackViewState()
649 viewstate.TrackViewState();
651 #endregion // Protected Instance Methods
653 #region IStateManager Properties & Methods
654 void IStateManager.LoadViewState(object state)
656 LoadViewState(state);
659 object IStateManager.SaveViewState()
661 return SaveViewState();
664 void IStateManager.TrackViewState()
669 bool IStateManager.IsTrackingViewState
673 return this.IsTrackingViewState;
676 #endregion // IStateManager Properties & Methods
679 internal static int BORDERWIDTH = 0xeadbeef;
680 internal static int FORECOLOR = 0xeadbeef;
682 [Obsolete ("This method will be removed in Fresh")]
683 internal bool IsSet(int blah)
685 if (blah == BORDERWIDTH)
686 return ((styles & Styles.BorderWidth) != 0);
688 if (blah == FORECOLOR)
689 return ((styles & Styles.ForeColor) != 0);
694 [Obsolete ("This method will be removed in Fresh")]
695 internal void Set(int blah)
698 #endregion // REMOVE ME
701 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
703 FillStyleAttributes (attributes);
706 internal void SetRegisteredCssClass (string name)
708 registered_class = name;
711 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
713 CssStyleCollection col = new CssStyleCollection ();
714 FillStyleAttributes (col, resolver);
719 public string RegisteredCssClass {
721 return registered_class;
725 internal virtual void CopyTextStylesFrom (Style source)
727 // Need to ask lluis if we need fonts, too
728 if ((styles & Styles.ForeColor) != 0) {
729 ForeColor = source.ForeColor;
731 if ((styles & Styles.BackColor) != 0) {
732 BackColor = source.BackColor;
736 public void SetDirty ()
738 if (viewstate != null)
739 viewstate.SetDirty ();
742 public static bool IsStyleEmpty (Style s)