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
34 // Not until we actually have StyleConverter
35 // [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
37 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
40 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
46 BackColor = 0x00000001,
47 BorderColor = 0x00000002,
48 BorderStyle = 0x00000004,
49 BorderWidth = 0x00000008,
50 CssClass = 0x00000010,
52 ForeColor = 0x00000040,
56 // from TableStyle (which doesn't override IsEmpty)
57 BackImageUrl = 0x00000200,
58 CellPadding = 0x00000400,
59 CellSpacing = 0x00000800,
60 GridLines = 0x00001000,
61 HorizontalAlign = 0x00002000,
63 // from TableItemStyle (which doesn't override IsEmpty neither)
64 VerticalAlign = 0x00004000,
67 // from DataGridPagerStyle (and, once again, no IsEmpty override)
69 NextPageText = 0x00020000,
70 PageButtonCount = 0x00040000,
71 Position = 0x00080000,
72 PrevPageText = 0x00100000,
78 internal Styles styles;
79 internal StateBag viewstate;
80 private FontInfo fontinfo;
81 private bool tracking;
83 private string registered_class;
87 #region Public Constructors
88 public Style() : this(new StateBag())
92 public Style(System.Web.UI.StateBag bag)
97 viewstate = new StateBag();
101 #endregion // Public Constructors
103 #region Public Instance Properties
107 [DefaultValue(typeof (Color), "")]
108 [NotifyParentProperty(true)]
109 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
110 [WebSysDescription ("")]
111 [WebCategory ("Appearance")]
112 public Color BackColor
116 if ((styles & Styles.BackColor) == 0)
121 return (Color)viewstate["BackColor"];
126 viewstate["BackColor"] = value;
127 styles |= Styles.BackColor;
134 [DefaultValue(typeof (Color), "")]
135 [NotifyParentProperty(true)]
136 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
137 [WebSysDescription ("")]
138 [WebCategory ("Appearance")]
139 public Color BorderColor
143 if ((styles & Styles.BorderColor) == 0)
148 return (Color)viewstate["BorderColor"];
153 viewstate["BorderColor"] = value;
154 styles |= Styles.BorderColor;
161 [DefaultValue(BorderStyle.NotSet)]
162 [NotifyParentProperty(true)]
163 [WebSysDescription ("")]
164 [WebCategory ("Appearance")]
165 public BorderStyle BorderStyle
169 if ((styles & Styles.BorderStyle) == 0)
171 return BorderStyle.NotSet;
174 return (BorderStyle)viewstate["BorderStyle"];
179 viewstate["BorderStyle"] = value;
180 styles |= Styles.BorderStyle;
187 [DefaultValue(typeof (Unit), "")]
188 [NotifyParentProperty(true)]
189 [WebSysDescription ("")]
190 [WebCategory ("Appearance")]
191 public Unit BorderWidth
195 if ((styles & Styles.BorderWidth) == 0)
200 return (Unit)viewstate["BorderWidth"];
207 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
210 viewstate["BorderWidth"] = value;
211 styles |= Styles.BorderWidth;
216 [NotifyParentProperty(true)]
217 [WebSysDescription ("")]
218 [WebCategory ("Appearance")]
219 public string CssClass
223 if ((styles & Styles.CssClass) == 0)
228 return (string)viewstate["CssClass"];
233 viewstate["CssClass"] = value;
234 styles |= Styles.CssClass;
238 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
239 [NotifyParentProperty(true)]
240 [WebSysDescription ("")]
241 [WebCategory ("Appearance")]
246 if (fontinfo == null)
248 fontinfo = new FontInfo(this);
257 [DefaultValue(typeof (Color), "")]
258 [NotifyParentProperty(true)]
259 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
260 [WebSysDescription ("")]
261 [WebCategory ("Appearance")]
262 public Color ForeColor
266 if ((styles & Styles.ForeColor) == 0)
271 return (Color)viewstate["ForeColor"];
276 viewstate["ForeColor"] = value;
277 styles |= Styles.ForeColor;
284 [DefaultValue(typeof (Unit), "")]
285 [NotifyParentProperty(true)]
286 [WebSysDescription ("")]
287 [WebCategory ("Appearance")]
292 if ((styles & Styles.Height) == 0)
297 return (Unit)viewstate["Height"];
304 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
307 viewstate["Height"] = value;
308 styles |= Styles.Height;
315 [DefaultValue(typeof (Unit), "")]
316 [NotifyParentProperty(true)]
317 [WebSysDescription ("")]
318 [WebCategory ("Appearance")]
323 if ((styles & Styles.Width) == 0)
328 return (Unit)viewstate["Width"];
335 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
338 viewstate["Width"] = value;
339 styles |= Styles.Width;
342 #endregion // Public Instance Properties
344 #region Protected Instance Properties
345 protected internal virtual bool IsEmpty
349 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
353 protected bool IsTrackingViewState
362 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
363 protected internal StateBag ViewState
370 #endregion // Protected Instance Properties
372 #region Public Instance Methods
373 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
375 AddAttributesToRender(writer, null);
378 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
380 if ((styles & Styles.CssClass) != 0)
382 string s = (string)viewstate["CssClass"];
383 if (s != string.Empty)
384 writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
387 CssStyleCollection attributes = new CssStyleCollection ();
388 FillStyleAttributes (attributes);
389 foreach (string attr in attributes.Keys)
390 writer.AddStyleAttribute (attr, attributes [attr]);
393 void FillStyleAttributes (CssStyleCollection attributes)
400 if ((styles & Styles.BackColor) != 0)
402 color = (Color)viewstate["BackColor"];
404 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
407 if ((styles & Styles.BorderColor) != 0)
409 color = (Color)viewstate["BorderColor"];
411 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
414 if ((styles & Styles.BorderStyle) != 0)
416 bs = (BorderStyle)viewstate["BorderStyle"];
417 if (bs != BorderStyle.NotSet)
418 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
421 if ((styles & Styles.BorderWidth) != 0)
423 u = (Unit)viewstate["BorderWidth"];
425 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
428 if ((styles & Styles.ForeColor) != 0)
430 color = (Color)viewstate["ForeColor"];
432 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
435 if ((styles & Styles.Height) != 0)
437 u = (Unit)viewstate["Height"];
439 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
442 if ((styles & Styles.Width) != 0)
444 u = (Unit)viewstate["Width"];
446 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
449 if (fontinfo != null) {
450 // Fonts are a bit weird
451 if (fontinfo.Name != string.Empty)
453 s = fontinfo.Names[0];
454 for (int i = 1; i < fontinfo.Names.Length; i++)
456 s += "," + fontinfo.Names[i];
458 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
463 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
468 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
471 if (!fontinfo.Size.IsEmpty)
473 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
476 // These styles are munged into a attribute decoration
479 if (fontinfo.Overline)
484 if (fontinfo.Strikeout)
486 s += "line-through ";
489 if (fontinfo.Underline)
494 if (s != string.Empty)
496 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
501 public virtual void CopyFrom(Style s)
503 if ((s == null) || s.IsEmpty)
508 if (s.fontinfo != null)
510 Font.CopyFrom(s.fontinfo);
513 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
515 this.BackColor = s.BackColor;
517 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
519 this.BorderColor = s.BorderColor;
521 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
523 this.BorderStyle = s.BorderStyle;
525 if (((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
527 this.BorderWidth = s.BorderWidth;
529 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
531 this.CssClass = s.CssClass;
533 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
535 this.ForeColor = s.ForeColor;
537 if (((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
539 this.Height = s.Height;
541 if (((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
543 this.Width = s.Width;
547 public virtual void MergeWith(Style s)
549 if ((s == null) || (s.IsEmpty))
554 if (s.fontinfo != null)
556 Font.MergeWith(s.fontinfo);
559 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
561 this.BackColor = s.BackColor;
563 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
565 this.BorderColor = s.BorderColor;
567 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
569 this.BorderStyle = s.BorderStyle;
571 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
573 this.BorderWidth = s.BorderWidth;
575 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
577 this.CssClass = s.CssClass;
579 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
581 this.ForeColor = s.ForeColor;
583 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
585 this.Height = s.Height;
587 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
589 this.Width = s.Width;
593 public virtual void Reset()
595 viewstate.Remove("BackColor");
596 viewstate.Remove("BorderColor");
597 viewstate.Remove("BorderStyle");
598 viewstate.Remove("BorderWidth");
599 viewstate.Remove("CssClass");
600 viewstate.Remove("ForeColor");
601 viewstate.Remove("Height");
602 viewstate.Remove("Width");
603 if (fontinfo != null)
607 styles = Styles.None;
610 public override string ToString()
614 #endregion // Public Instance Methods
616 #region Protected Instance Methods
617 protected internal void LoadViewState(object state)
619 viewstate.LoadViewState(state);
622 this.styles = Styles.None;
624 if (viewstate["BackColor"] != null)
626 styles |= Styles.BackColor;
628 if (viewstate["BorderColor"] != null)
630 styles |= Styles.BorderColor;
632 if (viewstate["BorderStyle"] != null)
634 styles |= Styles.BorderStyle;
636 if (viewstate["BorderWidth"] != null)
638 styles |= Styles.BorderWidth;
640 if (viewstate["CssClass"] != null)
642 styles |= Styles.CssClass;
644 if (viewstate["ForeColor"] != null)
646 styles |= Styles.ForeColor;
648 if (viewstate["Height"] != null)
650 styles |= Styles.Height;
652 if (viewstate["Width"] != null)
654 styles |= Styles.Width;
656 if (fontinfo != null) {
657 fontinfo.LoadViewState();
660 LoadViewStateInternal();
663 internal virtual void LoadViewStateInternal()
668 protected internal virtual object SaveViewState ()
670 if (styles != Styles.None)
672 return viewstate.SaveViewState();
678 protected internal virtual void SetBit( int bit )
680 throw new NotImplementedException();
683 protected internal virtual void TrackViewState()
686 viewstate.TrackViewState();
688 #endregion // Protected Instance Methods
690 #region IStateManager Properties & Methods
691 void IStateManager.LoadViewState(object state)
693 LoadViewState(state);
696 object IStateManager.SaveViewState()
698 return SaveViewState();
701 void IStateManager.TrackViewState()
706 bool IStateManager.IsTrackingViewState
710 return this.IsTrackingViewState;
713 #endregion // IStateManager Properties & Methods
716 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
718 FillStyleAttributes (attributes);
721 internal void SetRegisteredCssClass (string name)
723 registered_class = name;
726 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
728 CssStyleCollection col = new CssStyleCollection ();
729 FillStyleAttributes (col, resolver);
734 [EditorBrowsable(EditorBrowsableState.Advanced)]
735 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
737 public string RegisteredCssClass {
739 return registered_class;
743 internal virtual void CopyTextStylesFrom (Style source)
745 // Need to ask lluis if we need fonts, too
746 if ((styles & Styles.ForeColor) != 0) {
747 ForeColor = source.ForeColor;
749 if ((styles & Styles.BackColor) != 0) {
750 BackColor = source.BackColor;
754 public void SetDirty ()
756 if (viewstate != null)
757 viewstate.SetDirty (true);
760 public static bool IsStyleEmpty (Style s)