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)
27 using System.ComponentModel;
29 using System.Security.Permissions;
31 namespace System.Web.UI.WebControls {
34 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
35 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 // Not until we actually have StyleConverter
39 // [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
41 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
44 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
50 BackColor = 0x00000001,
51 BorderColor = 0x00000002,
52 BorderStyle = 0x00000004,
53 BorderWidth = 0x00000008,
54 CssClass = 0x00000010,
56 ForeColor = 0x00000040,
60 // from TableStyle (which doesn't override IsEmpty)
61 BackImageUrl = 0x00000200,
62 CellPadding = 0x00000400,
63 CellSpacing = 0x00000800,
64 GridLines = 0x00001000,
65 HorizontalAlign = 0x00002000,
67 // from TableItemStyle (which doesn't override IsEmpty neither)
68 VerticalAlign = 0x00004000,
71 // from DataGridPagerStyle (and, once again, no IsEmpty override)
73 NextPageText = 0x00020000,
74 PageButtonCount = 0x00040000,
75 Position = 0x00080000,
76 PrevPageText = 0x00100000,
82 internal Styles styles;
83 internal StateBag viewstate;
84 private FontInfo fontinfo;
85 private bool tracking;
87 private string registered_class;
91 #region Public Constructors
92 public Style() : this(new StateBag())
96 public Style(System.Web.UI.StateBag bag)
101 viewstate = new StateBag();
105 #endregion // Public Constructors
107 #region Public Instance Properties
111 [DefaultValue(typeof (Color), "")]
112 [NotifyParentProperty(true)]
113 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
114 [WebSysDescription ("")]
115 [WebCategory ("Appearance")]
116 public Color BackColor
120 if ((styles & Styles.BackColor) == 0)
125 return (Color)viewstate["BackColor"];
130 viewstate["BackColor"] = value;
131 styles |= Styles.BackColor;
138 [DefaultValue(typeof (Color), "")]
139 [NotifyParentProperty(true)]
140 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
141 [WebSysDescription ("")]
142 [WebCategory ("Appearance")]
143 public Color BorderColor
147 if ((styles & Styles.BorderColor) == 0)
152 return (Color)viewstate["BorderColor"];
157 viewstate["BorderColor"] = value;
158 styles |= Styles.BorderColor;
165 [DefaultValue(BorderStyle.NotSet)]
166 [NotifyParentProperty(true)]
167 [WebSysDescription ("")]
168 [WebCategory ("Appearance")]
169 public BorderStyle BorderStyle
173 if ((styles & Styles.BorderStyle) == 0)
175 return BorderStyle.NotSet;
178 return (BorderStyle)viewstate["BorderStyle"];
183 viewstate["BorderStyle"] = value;
184 styles |= Styles.BorderStyle;
191 [DefaultValue(typeof (Unit), "")]
192 [NotifyParentProperty(true)]
193 [WebSysDescription ("")]
194 [WebCategory ("Appearance")]
195 public Unit BorderWidth
199 if ((styles & Styles.BorderWidth) == 0)
204 return (Unit)viewstate["BorderWidth"];
211 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
214 viewstate["BorderWidth"] = value;
215 styles |= Styles.BorderWidth;
220 [NotifyParentProperty(true)]
221 [WebSysDescription ("")]
222 [WebCategory ("Appearance")]
223 public string CssClass
227 if ((styles & Styles.CssClass) == 0)
232 return (string)viewstate["CssClass"];
237 viewstate["CssClass"] = value;
238 styles |= Styles.CssClass;
242 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
243 [NotifyParentProperty(true)]
244 [WebSysDescription ("")]
245 [WebCategory ("Appearance")]
250 if (fontinfo == null)
252 fontinfo = new FontInfo(this);
261 [DefaultValue(typeof (Color), "")]
262 [NotifyParentProperty(true)]
263 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
264 [WebSysDescription ("")]
265 [WebCategory ("Appearance")]
266 public Color ForeColor
270 if ((styles & Styles.ForeColor) == 0)
275 return (Color)viewstate["ForeColor"];
280 viewstate["ForeColor"] = value;
281 styles |= Styles.ForeColor;
288 [DefaultValue(typeof (Unit), "")]
289 [NotifyParentProperty(true)]
290 [WebSysDescription ("")]
291 [WebCategory ("Appearance")]
296 if ((styles & Styles.Height) == 0)
301 return (Unit)viewstate["Height"];
308 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
311 viewstate["Height"] = value;
312 styles |= Styles.Height;
319 [DefaultValue(typeof (Unit), "")]
320 [NotifyParentProperty(true)]
321 [WebSysDescription ("")]
322 [WebCategory ("Appearance")]
327 if ((styles & Styles.Width) == 0)
332 return (Unit)viewstate["Width"];
339 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
342 viewstate["Width"] = value;
343 styles |= Styles.Width;
346 #endregion // Public Instance Properties
348 #region Protected Instance Properties
351 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
352 public virtual bool IsEmpty
354 protected internal virtual bool IsEmpty
360 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty) && RegisteredCssClass.Length == 0);
362 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
367 protected bool IsTrackingViewState
376 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
377 protected internal StateBag ViewState
384 #endregion // Protected Instance Properties
386 #region Internal Instance Properties
387 internal bool AlwaysRenderTextDecoration
391 if (viewstate["AlwaysRenderTextDecoration"] == null)
393 return (bool)viewstate["AlwaysRenderTextDecoration"];
398 viewstate["AlwaysRenderTextDecoration"] = value;
401 #endregion // Internal Instance Properties
403 #region Public Instance Methods
404 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
406 AddAttributesToRender(writer, null);
409 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
412 if (RegisteredCssClass.Length > 0) {
413 if (CssClass.Length > 0)
414 writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass + " " + RegisteredCssClass);
416 writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
421 if (CssClass.Length > 0)
422 writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass);
423 WriteStyleAttributes (writer);
427 void WriteStyleAttributes (HtmlTextWriter writer)
430 CssStyleCollection col = new CssStyleCollection (new StateBag ());
431 FillStyleAttributes (col, null);
432 foreach (string key in col.Keys) {
433 writer.AddStyleAttribute (key, col [key]);
441 if ((styles & Styles.BackColor) != 0) {
442 color = (Color)viewstate["BackColor"];
444 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
447 if ((styles & Styles.BorderColor) != 0) {
448 color = (Color)viewstate["BorderColor"];
450 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
453 bool have_width = false;
454 if ((styles & Styles.BorderWidth) != 0) {
455 u = (Unit)viewstate["BorderWidth"];
459 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
463 if ((styles & Styles.BorderStyle) != 0) {
464 bs = (BorderStyle)viewstate["BorderStyle"];
465 if (bs != BorderStyle.NotSet)
466 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
468 if ((styles & Styles.BorderWidth) != 0)
469 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
471 } else if (have_width) {
472 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
475 if ((styles & Styles.ForeColor) != 0) {
476 color = (Color)viewstate["ForeColor"];
478 writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
481 if ((styles & Styles.Height) != 0) {
482 u = (Unit)viewstate["Height"];
484 writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
487 if ((styles & Styles.Width) != 0) {
488 u = (Unit)viewstate["Width"];
490 writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
494 // Fonts are a bit weird
495 if (fontinfo.Name != string.Empty) {
496 s = fontinfo.Names[0];
497 for (int i = 1; i < fontinfo.Names.Length; i++)
498 s += "," + fontinfo.Names[i];
499 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
503 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
506 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
508 if (!fontinfo.Size.IsEmpty)
509 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
511 // These styles are munged into a attribute decoration
514 if (fontinfo.Overline)
517 if (fontinfo.Strikeout)
518 s += "line-through ";
520 if (fontinfo.Underline)
523 s = (s != "") ? s : AlwaysRenderTextDecoration ? "none" : "";
525 writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
531 void FillStyleAttributes (CssStyleCollection attributes)
537 if ((styles & Styles.BackColor) != 0)
539 color = (Color)viewstate["BackColor"];
541 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
544 if ((styles & Styles.BorderColor) != 0)
546 color = (Color)viewstate["BorderColor"];
548 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
551 bool have_width = false;
552 if ((styles & Styles.BorderWidth) != 0) {
553 u = (Unit) viewstate ["BorderWidth"];
557 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
561 if ((styles & Styles.BorderStyle) != 0) {
562 bs = (BorderStyle) viewstate ["BorderStyle"];
563 if (bs != BorderStyle.NotSet)
564 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
566 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
568 else if (have_width) {
569 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
572 if ((styles & Styles.ForeColor) != 0)
574 color = (Color)viewstate["ForeColor"];
576 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
579 if ((styles & Styles.Height) != 0)
581 u = (Unit)viewstate["Height"];
583 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
586 if ((styles & Styles.Width) != 0)
588 u = (Unit)viewstate["Width"];
590 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
593 Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
597 public virtual void CopyFrom(Style s)
599 if ((s == null) || s.IsEmpty)
604 if (s.fontinfo != null)
606 Font.CopyFrom(s.fontinfo);
609 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
611 this.BackColor = s.BackColor;
613 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
615 this.BorderColor = s.BorderColor;
617 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
619 this.BorderStyle = s.BorderStyle;
621 if (((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
623 this.BorderWidth = s.BorderWidth;
625 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
627 this.CssClass = s.CssClass;
629 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
631 this.ForeColor = s.ForeColor;
633 if (((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
635 this.Height = s.Height;
637 if (((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
639 this.Width = s.Width;
643 public virtual void MergeWith(Style s)
645 if ((s == null) || (s.IsEmpty))
650 if (s.fontinfo != null)
652 Font.MergeWith(s.fontinfo);
655 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
657 this.BackColor = s.BackColor;
659 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
661 this.BorderColor = s.BorderColor;
663 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
665 this.BorderStyle = s.BorderStyle;
667 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
669 this.BorderWidth = s.BorderWidth;
671 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
673 this.CssClass = s.CssClass;
675 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
677 this.ForeColor = s.ForeColor;
679 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
681 this.Height = s.Height;
683 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
685 this.Width = s.Width;
690 internal void Print ()
692 Console.WriteLine ("BackColor: {0}", BackColor);
693 Console.WriteLine ("BorderColor: {0}", BorderColor);
694 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
695 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
696 Console.WriteLine ("CssClass: {0}", CssClass);
697 Console.WriteLine ("ForeColor: {0}", ForeColor);
698 Console.WriteLine ("Height: {0}", Height);
699 Console.WriteLine ("Width: {0}", Width);
703 public virtual void Reset()
705 viewstate.Remove("BackColor");
706 viewstate.Remove("BorderColor");
707 viewstate.Remove("BorderStyle");
708 viewstate.Remove("BorderWidth");
709 viewstate.Remove("CssClass");
710 viewstate.Remove("ForeColor");
711 viewstate.Remove("Height");
712 viewstate.Remove("Width");
713 if (fontinfo != null)
717 styles = Styles.None;
720 public override string ToString()
725 #endregion // Public Instance Methods
727 #region Protected Instance Methods
728 protected internal void LoadViewState(object state)
730 viewstate.LoadViewState(state);
733 this.styles = Styles.None;
735 if (viewstate["BackColor"] != null)
737 styles |= Styles.BackColor;
739 if (viewstate["BorderColor"] != null)
741 styles |= Styles.BorderColor;
743 if (viewstate["BorderStyle"] != null)
745 styles |= Styles.BorderStyle;
747 if (viewstate["BorderWidth"] != null)
749 styles |= Styles.BorderWidth;
751 if (viewstate["CssClass"] != null)
753 styles |= Styles.CssClass;
755 if (viewstate["ForeColor"] != null)
757 styles |= Styles.ForeColor;
759 if (viewstate["Height"] != null)
761 styles |= Styles.Height;
763 if (viewstate["Width"] != null)
765 styles |= Styles.Width;
767 Font.LoadViewState();
769 LoadViewStateInternal();
772 internal virtual void LoadViewStateInternal()
777 protected internal virtual object SaveViewState ()
779 if (styles != Styles.None || !Font.IsEmpty)
781 return viewstate.SaveViewState();
787 protected internal virtual void SetBit( int bit )
789 throw new NotImplementedException();
792 protected internal virtual void TrackViewState()
795 viewstate.TrackViewState();
797 #endregion // Protected Instance Methods
799 #region IStateManager Properties & Methods
800 void IStateManager.LoadViewState(object state)
802 LoadViewState(state);
805 object IStateManager.SaveViewState()
807 return SaveViewState();
810 void IStateManager.TrackViewState()
815 bool IStateManager.IsTrackingViewState
819 return this.IsTrackingViewState;
822 #endregion // IStateManager Properties & Methods
825 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
827 FillStyleAttributes (attributes);
830 internal void SetRegisteredCssClass (string name)
832 registered_class = name;
835 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
837 CssStyleCollection col = new CssStyleCollection (new StateBag ());
838 FillStyleAttributes (col, resolver);
843 [EditorBrowsable(EditorBrowsableState.Advanced)]
844 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
846 public string RegisteredCssClass {
848 if (registered_class == null)
849 registered_class = String.Empty;
850 return registered_class;
854 internal virtual void CopyTextStylesFrom (Style source)
856 // Need to ask lluis if we need fonts, too
857 if ((styles & Styles.ForeColor) != 0) {
858 ForeColor = source.ForeColor;
860 if ((styles & Styles.BackColor) != 0) {
861 BackColor = source.BackColor;
865 public void SetDirty ()
867 if (viewstate != null)
868 viewstate.SetDirty (true);