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 CssStyleCollection attributes = new CssStyleCollection ();
398 FillStyleAttributes (attributes);
399 foreach (string attr in attributes.Keys)
400 writer.AddStyleAttribute (attr, attributes [attr]);
403 void FillStyleAttributes (CssStyleCollection attributes)
410 if ((styles & Styles.BackColor) != 0)
412 color = (Color)viewstate["BackColor"];
414 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
417 if ((styles & Styles.BorderColor) != 0)
419 color = (Color)viewstate["BorderColor"];
421 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
424 if ((styles & Styles.BorderStyle) != 0)
426 bs = (BorderStyle)viewstate["BorderStyle"];
427 if (bs != BorderStyle.NotSet)
428 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
431 if ((styles & Styles.BorderWidth) != 0)
433 u = (Unit)viewstate["BorderWidth"];
435 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
438 if ((styles & Styles.ForeColor) != 0)
440 color = (Color)viewstate["ForeColor"];
442 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
445 if ((styles & Styles.Height) != 0)
447 u = (Unit)viewstate["Height"];
449 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
452 if ((styles & Styles.Width) != 0)
454 u = (Unit)viewstate["Width"];
456 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
459 if (fontinfo != null) {
460 // Fonts are a bit weird
461 if (fontinfo.Name != string.Empty)
463 s = fontinfo.Names[0];
464 for (int i = 1; i < fontinfo.Names.Length; i++)
466 s += "," + fontinfo.Names[i];
468 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
473 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
478 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
481 if (!fontinfo.Size.IsEmpty)
483 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
486 // These styles are munged into a attribute decoration
489 if (fontinfo.Overline)
494 if (fontinfo.Strikeout)
496 s += "line-through ";
499 if (fontinfo.Underline)
504 if (s != string.Empty)
506 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
511 public virtual void CopyFrom(Style s)
513 if ((s == null) || s.IsEmpty)
518 if (s.fontinfo != null)
520 Font.CopyFrom(s.fontinfo);
523 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
525 this.BackColor = s.BackColor;
527 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
529 this.BorderColor = s.BorderColor;
531 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
533 this.BorderStyle = s.BorderStyle;
535 if (((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
537 this.BorderWidth = s.BorderWidth;
539 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
541 this.CssClass = s.CssClass;
543 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
545 this.ForeColor = s.ForeColor;
547 if (((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
549 this.Height = s.Height;
551 if (((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
553 this.Width = s.Width;
557 public virtual void MergeWith(Style s)
559 if ((s == null) || (s.IsEmpty))
564 if (s.fontinfo != null)
566 Font.MergeWith(s.fontinfo);
569 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
571 this.BackColor = s.BackColor;
573 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
575 this.BorderColor = s.BorderColor;
577 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
579 this.BorderStyle = s.BorderStyle;
581 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
583 this.BorderWidth = s.BorderWidth;
585 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
587 this.CssClass = s.CssClass;
589 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
591 this.ForeColor = s.ForeColor;
593 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
595 this.Height = s.Height;
597 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
599 this.Width = s.Width;
603 public virtual void Reset()
605 viewstate.Remove("BackColor");
606 viewstate.Remove("BorderColor");
607 viewstate.Remove("BorderStyle");
608 viewstate.Remove("BorderWidth");
609 viewstate.Remove("CssClass");
610 viewstate.Remove("ForeColor");
611 viewstate.Remove("Height");
612 viewstate.Remove("Width");
613 if (fontinfo != null)
617 styles = Styles.None;
620 public override string ToString()
625 #endregion // Public Instance Methods
627 #region Protected Instance Methods
628 protected internal void LoadViewState(object state)
630 viewstate.LoadViewState(state);
633 this.styles = Styles.None;
635 if (viewstate["BackColor"] != null)
637 styles |= Styles.BackColor;
639 if (viewstate["BorderColor"] != null)
641 styles |= Styles.BorderColor;
643 if (viewstate["BorderStyle"] != null)
645 styles |= Styles.BorderStyle;
647 if (viewstate["BorderWidth"] != null)
649 styles |= Styles.BorderWidth;
651 if (viewstate["CssClass"] != null)
653 styles |= Styles.CssClass;
655 if (viewstate["ForeColor"] != null)
657 styles |= Styles.ForeColor;
659 if (viewstate["Height"] != null)
661 styles |= Styles.Height;
663 if (viewstate["Width"] != null)
665 styles |= Styles.Width;
667 if (fontinfo != null) {
668 fontinfo.LoadViewState();
671 LoadViewStateInternal();
674 internal virtual void LoadViewStateInternal()
679 protected internal virtual object SaveViewState ()
681 if (styles != Styles.None)
683 return viewstate.SaveViewState();
689 protected internal virtual void SetBit( int bit )
691 throw new NotImplementedException();
694 protected internal virtual void TrackViewState()
697 viewstate.TrackViewState();
699 #endregion // Protected Instance Methods
701 #region IStateManager Properties & Methods
702 void IStateManager.LoadViewState(object state)
704 LoadViewState(state);
707 object IStateManager.SaveViewState()
709 return SaveViewState();
712 void IStateManager.TrackViewState()
717 bool IStateManager.IsTrackingViewState
721 return this.IsTrackingViewState;
724 #endregion // IStateManager Properties & Methods
727 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
729 FillStyleAttributes (attributes);
732 internal void SetRegisteredCssClass (string name)
734 registered_class = name;
737 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
739 CssStyleCollection col = new CssStyleCollection ();
740 FillStyleAttributes (col, resolver);
745 [EditorBrowsable(EditorBrowsableState.Advanced)]
746 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
748 public string RegisteredCssClass {
750 if (registered_class == null)
751 registered_class = String.Empty;
752 return registered_class;
756 internal virtual void CopyTextStylesFrom (Style source)
758 // Need to ask lluis if we need fonts, too
759 if ((styles & Styles.ForeColor) != 0) {
760 ForeColor = source.ForeColor;
762 if ((styles & Styles.BackColor) != 0) {
763 BackColor = source.BackColor;
767 public void SetDirty ()
769 if (viewstate != null)
770 viewstate.SetDirty (true);