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"];
425 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
429 if ((styles & Styles.BorderStyle) != 0) {
430 bs = (BorderStyle)viewstate["BorderStyle"];
431 if (bs != BorderStyle.NotSet)
432 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
434 if ((styles & Styles.BorderWidth) != 0)
435 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
437 } else if (have_width) {
438 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
441 if ((styles & Styles.ForeColor) != 0) {
442 color = (Color)viewstate["ForeColor"];
444 writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
447 if ((styles & Styles.Height) != 0) {
448 u = (Unit)viewstate["Height"];
450 writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
453 if ((styles & Styles.Width) != 0) {
454 u = (Unit)viewstate["Width"];
456 writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
459 if (fontinfo != null) {
460 // Fonts are a bit weird
461 if (fontinfo.Name != string.Empty) {
462 s = fontinfo.Names[0];
463 for (int i = 1; i < fontinfo.Names.Length; i++)
464 s += "," + fontinfo.Names[i];
465 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
469 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
472 writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
474 if (!fontinfo.Size.IsEmpty)
475 writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
477 // These styles are munged into a attribute decoration
480 if (fontinfo.Overline)
483 if (fontinfo.Strikeout)
484 s += "line-through ";
486 if (fontinfo.Underline)
489 if (s != string.Empty)
490 writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
494 void FillStyleAttributes (CssStyleCollection attributes)
501 if ((styles & Styles.BackColor) != 0)
503 color = (Color)viewstate["BackColor"];
505 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
508 if ((styles & Styles.BorderColor) != 0)
510 color = (Color)viewstate["BorderColor"];
512 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
515 if ((styles & Styles.BorderStyle) != 0)
517 bs = (BorderStyle)viewstate["BorderStyle"];
518 if (bs != BorderStyle.NotSet)
519 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
522 if ((styles & Styles.BorderWidth) != 0)
524 u = (Unit)viewstate["BorderWidth"];
526 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
529 if ((styles & Styles.ForeColor) != 0)
531 color = (Color)viewstate["ForeColor"];
533 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
536 if ((styles & Styles.Height) != 0)
538 u = (Unit)viewstate["Height"];
540 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
543 if ((styles & Styles.Width) != 0)
545 u = (Unit)viewstate["Width"];
547 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
550 if (fontinfo != null) {
551 // Fonts are a bit weird
552 if (fontinfo.Name != string.Empty)
554 s = fontinfo.Names[0];
555 for (int i = 1; i < fontinfo.Names.Length; i++)
557 s += "," + fontinfo.Names[i];
559 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
564 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
569 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
572 if (!fontinfo.Size.IsEmpty)
574 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
577 // These styles are munged into a attribute decoration
580 if (fontinfo.Overline)
585 if (fontinfo.Strikeout)
587 s += "line-through ";
590 if (fontinfo.Underline)
595 if (s != string.Empty)
597 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
602 public virtual void CopyFrom(Style s)
604 if ((s == null) || s.IsEmpty)
609 if (s.fontinfo != null)
611 Font.CopyFrom(s.fontinfo);
614 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
616 this.BackColor = s.BackColor;
618 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
620 this.BorderColor = s.BorderColor;
622 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
624 this.BorderStyle = s.BorderStyle;
626 if (((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
628 this.BorderWidth = s.BorderWidth;
630 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
632 this.CssClass = s.CssClass;
634 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
636 this.ForeColor = s.ForeColor;
638 if (((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
640 this.Height = s.Height;
642 if (((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
644 this.Width = s.Width;
648 public virtual void MergeWith(Style s)
650 if ((s == null) || (s.IsEmpty))
655 if (s.fontinfo != null)
657 Font.MergeWith(s.fontinfo);
660 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
662 this.BackColor = s.BackColor;
664 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
666 this.BorderColor = s.BorderColor;
668 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
670 this.BorderStyle = s.BorderStyle;
672 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (!s.BorderWidth.IsEmpty))
674 this.BorderWidth = s.BorderWidth;
676 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
678 this.CssClass = s.CssClass;
680 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
682 this.ForeColor = s.ForeColor;
684 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (!s.Height.IsEmpty))
686 this.Height = s.Height;
688 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (!s.Width.IsEmpty))
690 this.Width = s.Width;
695 internal void Print ()
697 Console.WriteLine ("BackColor: {0}", BackColor);
698 Console.WriteLine ("BorderColor: {0}", BorderColor);
699 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
700 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
701 Console.WriteLine ("CssClass: {0}", CssClass);
702 Console.WriteLine ("ForeColor: {0}", ForeColor);
703 Console.WriteLine ("Height: {0}", Height);
704 Console.WriteLine ("Width: {0}", Width);
708 public virtual void Reset()
710 viewstate.Remove("BackColor");
711 viewstate.Remove("BorderColor");
712 viewstate.Remove("BorderStyle");
713 viewstate.Remove("BorderWidth");
714 viewstate.Remove("CssClass");
715 viewstate.Remove("ForeColor");
716 viewstate.Remove("Height");
717 viewstate.Remove("Width");
718 if (fontinfo != null)
722 styles = Styles.None;
725 public override string ToString()
730 #endregion // Public Instance Methods
732 #region Protected Instance Methods
733 protected internal void LoadViewState(object state)
735 viewstate.LoadViewState(state);
738 this.styles = Styles.None;
740 if (viewstate["BackColor"] != null)
742 styles |= Styles.BackColor;
744 if (viewstate["BorderColor"] != null)
746 styles |= Styles.BorderColor;
748 if (viewstate["BorderStyle"] != null)
750 styles |= Styles.BorderStyle;
752 if (viewstate["BorderWidth"] != null)
754 styles |= Styles.BorderWidth;
756 if (viewstate["CssClass"] != null)
758 styles |= Styles.CssClass;
760 if (viewstate["ForeColor"] != null)
762 styles |= Styles.ForeColor;
764 if (viewstate["Height"] != null)
766 styles |= Styles.Height;
768 if (viewstate["Width"] != null)
770 styles |= Styles.Width;
772 if (fontinfo != null) {
773 fontinfo.LoadViewState();
776 LoadViewStateInternal();
779 internal virtual void LoadViewStateInternal()
784 protected internal virtual object SaveViewState ()
786 if (styles != Styles.None)
788 return viewstate.SaveViewState();
794 protected internal virtual void SetBit( int bit )
796 throw new NotImplementedException();
799 protected internal virtual void TrackViewState()
802 viewstate.TrackViewState();
804 #endregion // Protected Instance Methods
806 #region IStateManager Properties & Methods
807 void IStateManager.LoadViewState(object state)
809 LoadViewState(state);
812 object IStateManager.SaveViewState()
814 return SaveViewState();
817 void IStateManager.TrackViewState()
822 bool IStateManager.IsTrackingViewState
826 return this.IsTrackingViewState;
829 #endregion // IStateManager Properties & Methods
832 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
834 FillStyleAttributes (attributes);
837 internal void SetRegisteredCssClass (string name)
839 registered_class = name;
842 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
844 CssStyleCollection col = new CssStyleCollection (new StateBag ());
845 FillStyleAttributes (col, resolver);
850 [EditorBrowsable(EditorBrowsableState.Advanced)]
851 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
853 public string RegisteredCssClass {
855 if (registered_class == null)
856 registered_class = String.Empty;
857 return registered_class;
861 internal virtual void CopyTextStylesFrom (Style source)
863 // Need to ask lluis if we need fonts, too
864 if ((styles & Styles.ForeColor) != 0) {
865 ForeColor = source.ForeColor;
867 if ((styles & Styles.BackColor) != 0) {
868 BackColor = source.BackColor;
872 public void SetDirty ()
874 if (viewstate != null)
875 viewstate.SetDirty (true);