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(typeof (Color), "")]
101 [NotifyParentProperty(true)]
102 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
103 [WebSysDescription ("")]
104 [WebCategory ("Appearance")]
105 public Color BackColor
109 if ((styles & Styles.BackColor) == 0)
114 return (Color)viewstate["BackColor"];
119 viewstate["BackColor"] = value;
120 styles |= Styles.BackColor;
125 [DefaultValue(typeof (Color), "")]
126 [NotifyParentProperty(true)]
127 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
128 [WebSysDescription ("")]
129 [WebCategory ("Appearance")]
130 public Color BorderColor
134 if ((styles & Styles.BorderColor) == 0)
139 return (Color)viewstate["BorderColor"];
144 viewstate["BorderColor"] = value;
145 styles |= Styles.BorderColor;
150 [DefaultValue(BorderStyle.NotSet)]
151 [NotifyParentProperty(true)]
152 [WebSysDescription ("")]
153 [WebCategory ("Appearance")]
154 public BorderStyle BorderStyle
158 if ((styles & Styles.BorderStyle) == 0)
160 return BorderStyle.NotSet;
163 return (BorderStyle)viewstate["BorderStyle"];
168 viewstate["BorderStyle"] = value;
169 styles |= Styles.BorderStyle;
174 [DefaultValue(typeof (Unit), "")]
175 [NotifyParentProperty(true)]
176 [WebSysDescription ("")]
177 [WebCategory ("Appearance")]
178 public Unit BorderWidth
182 if ((styles & Styles.BorderWidth) == 0)
187 return (Unit)viewstate["BorderWidth"];
194 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
197 viewstate["BorderWidth"] = value;
198 styles |= Styles.BorderWidth;
203 [NotifyParentProperty(true)]
204 [WebSysDescription ("")]
205 [WebCategory ("Appearance")]
206 public string CssClass
210 if ((styles & Styles.CssClass) == 0)
215 return (string)viewstate["CssClass"];
220 viewstate["CssClass"] = value;
221 styles |= Styles.CssClass;
225 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
226 [NotifyParentProperty(true)]
227 [WebSysDescription ("")]
228 [WebCategory ("Appearance")]
233 if (fontinfo == null)
235 fontinfo = new FontInfo(this);
242 [DefaultValue(typeof (Color), "")]
243 [NotifyParentProperty(true)]
244 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
245 [WebSysDescription ("")]
246 [WebCategory ("Appearance")]
247 public Color ForeColor
251 if ((styles & Styles.ForeColor) == 0)
256 return (Color)viewstate["ForeColor"];
261 viewstate["ForeColor"] = value;
262 styles |= Styles.ForeColor;
267 [DefaultValue(typeof (Unit), "")]
268 [NotifyParentProperty(true)]
269 [WebSysDescription ("")]
270 [WebCategory ("Appearance")]
275 if ((styles & Styles.Height) == 0)
280 return (Unit)viewstate["Height"];
287 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
290 viewstate["Height"] = value;
291 styles |= Styles.Height;
296 [DefaultValue(typeof (Unit), "")]
297 [NotifyParentProperty(true)]
298 [WebSysDescription ("")]
299 [WebCategory ("Appearance")]
304 if ((styles & Styles.Width) == 0)
309 return (Unit)viewstate["Width"];
316 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
319 viewstate["Width"] = value;
320 styles |= Styles.Width;
323 #endregion // Public Instance Properties
325 #region Protected Instance Properties
326 protected internal virtual bool IsEmpty
330 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
334 protected bool IsTrackingViewState
343 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
344 protected internal StateBag ViewState
351 #endregion // Protected Instance Properties
353 #region Public Instance Methods
354 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
356 AddAttributesToRender(writer, null);
359 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
361 if ((styles & Styles.CssClass) != 0)
363 string s = (string)viewstate["CssClass"];
364 if (s != string.Empty)
365 writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
368 WriteStyleAttributes (writer);
371 void WriteStyleAttributes (HtmlTextWriter writer)
378 if ((styles & Styles.BackColor) != 0) {
379 color = (Color)viewstate["BackColor"];
381 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
384 if ((styles & Styles.BorderColor) != 0) {
385 color = (Color)viewstate["BorderColor"];
387 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
390 if ((styles & Styles.BorderStyle) != 0) {
391 bs = (BorderStyle)viewstate["BorderStyle"];
392 if (bs != BorderStyle.NotSet)
393 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
396 if ((styles & Styles.BorderWidth) != 0) {
397 u = (Unit)viewstate["BorderWidth"];
399 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
402 if ((styles & Styles.ForeColor) != 0) {
403 color = (Color)viewstate["ForeColor"];
405 writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
408 if ((styles & Styles.Height) != 0) {
409 u = (Unit)viewstate["Height"];
411 writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
414 if ((styles & Styles.Width) != 0) {
415 u = (Unit)viewstate["Width"];
417 writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
420 if (fontinfo != null) {
421 // Fonts are a bit weird
422 if (fontinfo.Name != string.Empty) {
423 s = fontinfo.Names[0];
424 for (int i = 1; i < fontinfo.Names.Length; i++)
425 s += "," + fontinfo.Names[i];
426 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
430 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
433 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
435 if (!fontinfo.Size.IsEmpty)
436 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
438 // These styles are munged into a attribute decoration
441 if (fontinfo.Overline)
444 if (fontinfo.Strikeout)
445 s += "line-through ";
447 if (fontinfo.Underline)
450 if (s != string.Empty)
451 writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
455 void FillStyleAttributes (CssStyleCollection attributes)
462 if ((styles & Styles.BackColor) != 0)
464 color = (Color)viewstate["BackColor"];
466 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
469 if ((styles & Styles.BorderColor) != 0)
471 color = (Color)viewstate["BorderColor"];
473 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
476 if ((styles & Styles.BorderStyle) != 0)
478 bs = (BorderStyle)viewstate["BorderStyle"];
479 if (bs != BorderStyle.NotSet)
480 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
483 if ((styles & Styles.BorderWidth) != 0)
485 u = (Unit)viewstate["BorderWidth"];
487 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
490 if ((styles & Styles.ForeColor) != 0)
492 color = (Color)viewstate["ForeColor"];
494 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
497 if ((styles & Styles.Height) != 0)
499 u = (Unit)viewstate["Height"];
501 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
504 if ((styles & Styles.Width) != 0)
506 u = (Unit)viewstate["Width"];
508 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
511 if (fontinfo != null) {
512 // Fonts are a bit weird
513 if (fontinfo.Name != string.Empty)
515 s = fontinfo.Names[0];
516 for (int i = 1; i < fontinfo.Names.Length; i++)
518 s += "," + fontinfo.Names[i];
520 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
525 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
530 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
533 if (!fontinfo.Size.IsEmpty)
535 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
538 // These styles are munged into a attribute decoration
541 if (fontinfo.Overline)
546 if (fontinfo.Strikeout)
548 s += "line-through ";
551 if (fontinfo.Underline)
556 if (s != string.Empty)
558 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
563 public virtual void CopyFrom(Style s)
565 if ((s == null) || s.IsEmpty)
570 if (s.fontinfo != null)
572 Font.CopyFrom(s.fontinfo);
575 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
577 this.BackColor = s.BackColor;
579 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
581 this.BorderColor = s.BorderColor;
583 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
585 this.BorderStyle = s.BorderStyle;
587 if (((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
589 this.BorderWidth = s.BorderWidth;
591 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
593 this.CssClass = s.CssClass;
595 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
597 this.ForeColor = s.ForeColor;
599 if (((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
601 this.Height = s.Height;
603 if (((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
605 this.Width = s.Width;
609 public virtual void MergeWith(Style s)
611 if ((s == null) || (s.IsEmpty))
616 if (s.fontinfo != null)
618 Font.MergeWith(s.fontinfo);
621 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
623 this.BackColor = s.BackColor;
625 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
627 this.BorderColor = s.BorderColor;
629 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
631 this.BorderStyle = s.BorderStyle;
633 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
635 this.BorderWidth = s.BorderWidth;
637 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
639 this.CssClass = s.CssClass;
641 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
643 this.ForeColor = s.ForeColor;
645 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
647 this.Height = s.Height;
649 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
651 this.Width = s.Width;
656 internal void Print ()
658 Console.WriteLine ("BackColor: {0}", BackColor);
659 Console.WriteLine ("BorderColor: {0}", BorderColor);
660 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
661 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
662 Console.WriteLine ("CssClass: {0}", CssClass);
663 Console.WriteLine ("ForeColor: {0}", ForeColor);
664 Console.WriteLine ("Height: {0}", Height);
665 Console.WriteLine ("Width: {0}", Width);
669 public virtual void Reset()
671 viewstate.Remove("BackColor");
672 viewstate.Remove("BorderColor");
673 viewstate.Remove("BorderStyle");
674 viewstate.Remove("BorderWidth");
675 viewstate.Remove("CssClass");
676 viewstate.Remove("ForeColor");
677 viewstate.Remove("Height");
678 viewstate.Remove("Width");
679 if (fontinfo != null)
683 styles = Styles.None;
686 public override string ToString()
690 #endregion // Public Instance Methods
692 #region Protected Instance Methods
693 protected internal void LoadViewState(object state)
695 viewstate.LoadViewState(state);
698 this.styles = Styles.None;
700 if (viewstate["BackColor"] != null)
702 styles |= Styles.BackColor;
704 if (viewstate["BorderColor"] != null)
706 styles |= Styles.BorderColor;
708 if (viewstate["BorderStyle"] != null)
710 styles |= Styles.BorderStyle;
712 if (viewstate["BorderWidth"] != null)
714 styles |= Styles.BorderWidth;
716 if (viewstate["CssClass"] != null)
718 styles |= Styles.CssClass;
720 if (viewstate["ForeColor"] != null)
722 styles |= Styles.ForeColor;
724 if (viewstate["Height"] != null)
726 styles |= Styles.Height;
728 if (viewstate["Width"] != null)
730 styles |= Styles.Width;
732 if (fontinfo != null) {
733 fontinfo.LoadViewState();
736 LoadViewStateInternal();
739 internal virtual void LoadViewStateInternal()
744 protected internal virtual object SaveViewState ()
746 if (styles != Styles.None)
748 return viewstate.SaveViewState();
754 protected internal virtual void SetBit( int bit )
756 throw new NotImplementedException();
759 protected internal virtual void TrackViewState()
762 viewstate.TrackViewState();
764 #endregion // Protected Instance Methods
766 #region IStateManager Properties & Methods
767 void IStateManager.LoadViewState(object state)
769 LoadViewState(state);
772 object IStateManager.SaveViewState()
774 return SaveViewState();
777 void IStateManager.TrackViewState()
782 bool IStateManager.IsTrackingViewState
786 return this.IsTrackingViewState;
789 #endregion // IStateManager Properties & Methods
792 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
794 FillStyleAttributes (attributes);
797 internal void SetRegisteredCssClass (string name)
799 registered_class = name;
802 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
804 CssStyleCollection col = new CssStyleCollection (new StateBag ());
805 FillStyleAttributes (col, resolver);
810 public string RegisteredCssClass {
812 return registered_class;
816 internal virtual void CopyTextStylesFrom (Style source)
818 // Need to ask lluis if we need fonts, too
819 if ((styles & Styles.ForeColor) != 0) {
820 ForeColor = source.ForeColor;
822 if ((styles & Styles.BackColor) != 0) {
823 BackColor = source.BackColor;
827 public void SetDirty ()
829 if (viewstate != null)
830 viewstate.SetDirty ();
833 public static bool IsStyleEmpty (Style s)