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
359 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
363 protected bool IsTrackingViewState
372 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
373 protected internal StateBag ViewState
380 #endregion // Protected Instance Properties
382 #region Public Instance Methods
383 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
385 AddAttributesToRender(writer, null);
388 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
390 if ((styles & Styles.CssClass) != 0)
392 string s = (string)viewstate["CssClass"];
393 if (s != string.Empty)
394 writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
397 WriteStyleAttributes (writer);
400 void WriteStyleAttributes (HtmlTextWriter writer)
407 if ((styles & Styles.BackColor) != 0) {
408 color = (Color)viewstate["BackColor"];
410 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
413 if ((styles & Styles.BorderColor) != 0) {
414 color = (Color)viewstate["BorderColor"];
416 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
419 bool have_width = false;
420 if ((styles & Styles.BorderWidth) != 0) {
421 u = (Unit)viewstate["BorderWidth"];
424 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
428 if ((styles & Styles.BorderStyle) != 0) {
429 bs = (BorderStyle)viewstate["BorderStyle"];
430 if (bs != BorderStyle.NotSet)
431 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
433 if ((styles & Styles.BorderWidth) != 0)
434 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
436 } else if (have_width) {
437 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
440 if ((styles & Styles.ForeColor) != 0) {
441 color = (Color)viewstate["ForeColor"];
443 writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
446 if ((styles & Styles.Height) != 0) {
447 u = (Unit)viewstate["Height"];
449 writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
452 if ((styles & Styles.Width) != 0) {
453 u = (Unit)viewstate["Width"];
455 writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
458 if (fontinfo != null) {
459 // Fonts are a bit weird
460 if (fontinfo.Name != string.Empty) {
461 s = fontinfo.Names[0];
462 for (int i = 1; i < fontinfo.Names.Length; i++)
463 s += "," + fontinfo.Names[i];
464 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
468 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
471 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
473 if (!fontinfo.Size.IsEmpty)
474 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
476 // These styles are munged into a attribute decoration
479 if (fontinfo.Overline)
482 if (fontinfo.Strikeout)
483 s += "line-through ";
485 if (fontinfo.Underline)
488 if (s != string.Empty)
489 writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
493 void FillStyleAttributes (CssStyleCollection attributes)
500 if ((styles & Styles.BackColor) != 0)
502 color = (Color)viewstate["BackColor"];
504 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
507 if ((styles & Styles.BorderColor) != 0)
509 color = (Color)viewstate["BorderColor"];
511 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
514 if ((styles & Styles.BorderStyle) != 0)
516 bs = (BorderStyle)viewstate["BorderStyle"];
517 if (bs != BorderStyle.NotSet)
518 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
521 if ((styles & Styles.BorderWidth) != 0)
523 u = (Unit)viewstate["BorderWidth"];
525 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
528 if ((styles & Styles.ForeColor) != 0)
530 color = (Color)viewstate["ForeColor"];
532 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
535 if ((styles & Styles.Height) != 0)
537 u = (Unit)viewstate["Height"];
539 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
542 if ((styles & Styles.Width) != 0)
544 u = (Unit)viewstate["Width"];
546 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
549 if (fontinfo != null) {
550 // Fonts are a bit weird
551 if (fontinfo.Name != string.Empty)
553 s = fontinfo.Names[0];
554 for (int i = 1; i < fontinfo.Names.Length; i++)
556 s += "," + fontinfo.Names[i];
558 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
563 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
568 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
571 if (!fontinfo.Size.IsEmpty)
573 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
576 // These styles are munged into a attribute decoration
579 if (fontinfo.Overline)
584 if (fontinfo.Strikeout)
586 s += "line-through ";
589 if (fontinfo.Underline)
594 if (s != string.Empty)
596 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
601 public virtual void CopyFrom(Style s)
603 if ((s == null) || s.IsEmpty)
608 if (s.fontinfo != null)
610 Font.CopyFrom(s.fontinfo);
613 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
615 this.BackColor = s.BackColor;
617 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
619 this.BorderColor = s.BorderColor;
621 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
623 this.BorderStyle = s.BorderStyle;
625 if (((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
627 this.BorderWidth = s.BorderWidth;
629 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
631 this.CssClass = s.CssClass;
633 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
635 this.ForeColor = s.ForeColor;
637 if (((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
639 this.Height = s.Height;
641 if (((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
643 this.Width = s.Width;
647 public virtual void MergeWith(Style s)
649 if ((s == null) || (s.IsEmpty))
654 if (s.fontinfo != null)
656 Font.MergeWith(s.fontinfo);
659 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
661 this.BackColor = s.BackColor;
663 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
665 this.BorderColor = s.BorderColor;
667 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
669 this.BorderStyle = s.BorderStyle;
671 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
673 this.BorderWidth = s.BorderWidth;
675 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
677 this.CssClass = s.CssClass;
679 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
681 this.ForeColor = s.ForeColor;
683 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
685 this.Height = s.Height;
687 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
689 this.Width = s.Width;
694 internal void Print ()
696 Console.WriteLine ("BackColor: {0}", BackColor);
697 Console.WriteLine ("BorderColor: {0}", BorderColor);
698 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
699 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
700 Console.WriteLine ("CssClass: {0}", CssClass);
701 Console.WriteLine ("ForeColor: {0}", ForeColor);
702 Console.WriteLine ("Height: {0}", Height);
703 Console.WriteLine ("Width: {0}", Width);
707 public virtual void Reset()
709 viewstate.Remove("BackColor");
710 viewstate.Remove("BorderColor");
711 viewstate.Remove("BorderStyle");
712 viewstate.Remove("BorderWidth");
713 viewstate.Remove("CssClass");
714 viewstate.Remove("ForeColor");
715 viewstate.Remove("Height");
716 viewstate.Remove("Width");
717 if (fontinfo != null)
721 styles = Styles.None;
724 public override string ToString()
729 #endregion // Public Instance Methods
731 #region Protected Instance Methods
732 protected internal void LoadViewState(object state)
734 viewstate.LoadViewState(state);
737 this.styles = Styles.None;
739 if (viewstate["BackColor"] != null)
741 styles |= Styles.BackColor;
743 if (viewstate["BorderColor"] != null)
745 styles |= Styles.BorderColor;
747 if (viewstate["BorderStyle"] != null)
749 styles |= Styles.BorderStyle;
751 if (viewstate["BorderWidth"] != null)
753 styles |= Styles.BorderWidth;
755 if (viewstate["CssClass"] != null)
757 styles |= Styles.CssClass;
759 if (viewstate["ForeColor"] != null)
761 styles |= Styles.ForeColor;
763 if (viewstate["Height"] != null)
765 styles |= Styles.Height;
767 if (viewstate["Width"] != null)
769 styles |= Styles.Width;
771 if (fontinfo != null) {
772 fontinfo.LoadViewState();
775 LoadViewStateInternal();
778 internal virtual void LoadViewStateInternal()
783 protected internal virtual object SaveViewState ()
785 if (styles != Styles.None)
787 return viewstate.SaveViewState();
793 protected internal virtual void SetBit( int bit )
795 throw new NotImplementedException();
798 protected internal virtual void TrackViewState()
801 viewstate.TrackViewState();
803 #endregion // Protected Instance Methods
805 #region IStateManager Properties & Methods
806 void IStateManager.LoadViewState(object state)
808 LoadViewState(state);
811 object IStateManager.SaveViewState()
813 return SaveViewState();
816 void IStateManager.TrackViewState()
821 bool IStateManager.IsTrackingViewState
825 return this.IsTrackingViewState;
828 #endregion // IStateManager Properties & Methods
831 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
833 FillStyleAttributes (attributes);
836 internal void SetRegisteredCssClass (string name)
838 registered_class = name;
841 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
843 CssStyleCollection col = new CssStyleCollection (new StateBag ());
844 FillStyleAttributes (col, resolver);
849 [EditorBrowsable(EditorBrowsableState.Advanced)]
850 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
852 public string RegisteredCssClass {
854 if (registered_class == null)
855 registered_class = String.Empty;
856 return registered_class;
860 internal virtual void CopyTextStylesFrom (Style source)
862 // Need to ask lluis if we need fonts, too
863 if ((styles & Styles.ForeColor) != 0) {
864 ForeColor = source.ForeColor;
866 if ((styles & Styles.BackColor) != 0) {
867 BackColor = source.BackColor;
871 public void SetDirty ()
873 if (viewstate != null)
874 viewstate.SetDirty (true);