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
33 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
35 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
41 BackColor = 0x00000001,
42 BorderColor = 0x00000002,
43 BorderStyle = 0x00000004,
44 BorderWidth = 0x00000008,
45 CssClass = 0x00000010,
47 ForeColor = 0x00000040,
51 // from TableStyle (which doesn't override IsEmpty)
52 BackImageUrl = 0x00000200,
53 CellPadding = 0x00000400,
54 CellSpacing = 0x00000800,
55 GridLines = 0x00001000,
56 HorizontalAlign = 0x00002000,
58 // from TableItemStyle (which doesn't override IsEmpty neither)
59 VerticalAlign = 0x00004000,
62 // from DataGridPagerStyle (and, once again, no IsEmpty override)
64 NextPageText = 0x00020000,
65 PageButtonCount = 0x00040000,
66 Position = 0x00080000,
67 PrevPageText = 0x00100000,
73 internal Styles styles;
74 internal StateBag viewstate;
75 private FontInfo fontinfo;
76 private bool tracking;
78 private string registered_class;
82 #region Public Constructors
83 public Style() : this(new StateBag())
87 public Style(System.Web.UI.StateBag bag)
92 viewstate = new StateBag();
96 #endregion // Public Constructors
98 #region Public Instance Properties
100 [DefaultValue(typeof (Color), "")]
101 [NotifyParentProperty(true)]
102 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
103 [WebSysDescription ("")]
104 [WebCategory ("Appearance")]
105 public Color BackColor
109 if ((styles & Styles.BackColor) == 0)
114 return (Color)viewstate["BackColor"];
119 viewstate["BackColor"] = value;
120 styles |= Styles.BackColor;
125 [DefaultValue(typeof (Color), "")]
126 [NotifyParentProperty(true)]
127 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
128 [WebSysDescription ("")]
129 [WebCategory ("Appearance")]
130 public Color BorderColor
134 if ((styles & Styles.BorderColor) == 0)
139 return (Color)viewstate["BorderColor"];
144 viewstate["BorderColor"] = value;
145 styles |= Styles.BorderColor;
150 [DefaultValue(BorderStyle.NotSet)]
151 [NotifyParentProperty(true)]
152 [WebSysDescription ("")]
153 [WebCategory ("Appearance")]
154 public BorderStyle BorderStyle
158 if ((styles & Styles.BorderStyle) == 0)
160 return BorderStyle.NotSet;
163 return (BorderStyle)viewstate["BorderStyle"];
168 viewstate["BorderStyle"] = value;
169 styles |= Styles.BorderStyle;
174 [DefaultValue(typeof (Unit), "")]
175 [NotifyParentProperty(true)]
176 [WebSysDescription ("")]
177 [WebCategory ("Appearance")]
178 public Unit BorderWidth
182 if ((styles & Styles.BorderWidth) == 0)
187 return (Unit)viewstate["BorderWidth"];
194 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
197 viewstate["BorderWidth"] = value;
198 styles |= Styles.BorderWidth;
203 [NotifyParentProperty(true)]
204 [WebSysDescription ("")]
205 [WebCategory ("Appearance")]
206 public string CssClass
210 if ((styles & Styles.CssClass) == 0)
215 return (string)viewstate["CssClass"];
220 viewstate["CssClass"] = value;
221 styles |= Styles.CssClass;
225 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
226 [NotifyParentProperty(true)]
227 [WebSysDescription ("")]
228 [WebCategory ("Appearance")]
233 if (fontinfo == null)
235 fontinfo = new FontInfo(this);
242 [DefaultValue(typeof (Color), "")]
243 [NotifyParentProperty(true)]
244 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
245 [WebSysDescription ("")]
246 [WebCategory ("Appearance")]
247 public Color ForeColor
251 if ((styles & Styles.ForeColor) == 0)
256 return (Color)viewstate["ForeColor"];
261 viewstate["ForeColor"] = value;
262 styles |= Styles.ForeColor;
267 [DefaultValue(typeof (Unit), "")]
268 [NotifyParentProperty(true)]
269 [WebSysDescription ("")]
270 [WebCategory ("Appearance")]
275 if ((styles & Styles.Height) == 0)
280 return (Unit)viewstate["Height"];
287 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
290 viewstate["Height"] = value;
291 styles |= Styles.Height;
296 [DefaultValue(typeof (Unit), "")]
297 [NotifyParentProperty(true)]
298 [WebSysDescription ("")]
299 [WebCategory ("Appearance")]
304 if ((styles & Styles.Width) == 0)
309 return (Unit)viewstate["Width"];
316 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
319 viewstate["Width"] = value;
320 styles |= Styles.Width;
323 #endregion // Public Instance Properties
325 #region Protected Instance Properties
326 protected internal virtual bool IsEmpty
330 return (styles == 0 && (fontinfo == null || fontinfo.IsEmpty));
334 protected bool IsTrackingViewState
343 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
344 protected internal StateBag ViewState
351 #endregion // Protected Instance Properties
353 #region Public Instance Methods
354 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
356 AddAttributesToRender(writer, null);
359 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
361 if ((styles & Styles.CssClass) != 0)
363 string s = (string)viewstate["CssClass"];
364 if (s != string.Empty)
365 writer.AddAttribute (HtmlTextWriterAttribute.Class, s);
368 CssStyleCollection attributes = new CssStyleCollection ();
369 FillStyleAttributes (attributes);
370 foreach (string attr in attributes.Keys)
371 writer.AddStyleAttribute (attr, attributes [attr]);
374 void FillStyleAttributes (CssStyleCollection attributes)
381 if ((styles & Styles.BackColor) != 0)
383 color = (Color)viewstate["BackColor"];
385 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
388 if ((styles & Styles.BorderColor) != 0)
390 color = (Color)viewstate["BorderColor"];
392 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
395 if ((styles & Styles.BorderStyle) != 0)
397 bs = (BorderStyle)viewstate["BorderStyle"];
398 if (bs != BorderStyle.NotSet)
399 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
402 if ((styles & Styles.BorderWidth) != 0)
404 u = (Unit)viewstate["BorderWidth"];
406 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
409 if ((styles & Styles.ForeColor) != 0)
411 color = (Color)viewstate["ForeColor"];
413 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
416 if ((styles & Styles.Height) != 0)
418 u = (Unit)viewstate["Height"];
420 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
423 if ((styles & Styles.Width) != 0)
425 u = (Unit)viewstate["Width"];
427 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
430 if (fontinfo != null) {
431 // Fonts are a bit weird
432 if (fontinfo.Name != string.Empty)
434 s = fontinfo.Names[0];
435 for (int i = 1; i < fontinfo.Names.Length; i++)
437 s += "," + fontinfo.Names[i];
439 attributes.Add (HtmlTextWriterStyle.FontFamily, s);
444 attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
449 attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
452 if (!fontinfo.Size.IsEmpty)
454 attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
457 // These styles are munged into a attribute decoration
460 if (fontinfo.Overline)
465 if (fontinfo.Strikeout)
467 s += "line-through ";
470 if (fontinfo.Underline)
475 if (s != string.Empty)
477 attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
482 public virtual void CopyFrom(Style s)
484 if ((s == null) || s.IsEmpty)
489 if (s.fontinfo != null)
491 Font.CopyFrom(s.fontinfo);
494 if (((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
496 this.BackColor = s.BackColor;
498 if (((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
500 this.BorderColor = s.BorderColor;
502 if (((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
504 this.BorderStyle = s.BorderStyle;
506 if (((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
508 this.BorderWidth = s.BorderWidth;
510 if (((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
512 this.CssClass = s.CssClass;
514 if (((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
516 this.ForeColor = s.ForeColor;
518 if (((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
520 this.Height = s.Height;
522 if (((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
524 this.Width = s.Width;
528 public virtual void MergeWith(Style s)
530 if ((s == null) || (s.IsEmpty))
535 if (s.fontinfo != null)
537 Font.MergeWith(s.fontinfo);
540 if (((styles & Styles.BackColor) == 0) && ((s.styles & Styles.BackColor) != 0) && (s.BackColor != Color.Empty))
542 this.BackColor = s.BackColor;
544 if (((styles & Styles.BorderColor) == 0) && ((s.styles & Styles.BorderColor) != 0) && (s.BorderColor != Color.Empty))
546 this.BorderColor = s.BorderColor;
548 if (((styles & Styles.BorderStyle) == 0) && ((s.styles & Styles.BorderStyle) != 0) && (s.BorderStyle != BorderStyle.NotSet))
550 this.BorderStyle = s.BorderStyle;
552 if (((styles & Styles.BorderWidth) == 0) && ((s.styles & Styles.BorderWidth) != 0) && (s.BorderWidth != Unit.Empty))
554 this.BorderWidth = s.BorderWidth;
556 if (((styles & Styles.CssClass) == 0) && ((s.styles & Styles.CssClass) != 0) && (s.CssClass != string.Empty))
558 this.CssClass = s.CssClass;
560 if (((styles & Styles.ForeColor) == 0) && ((s.styles & Styles.ForeColor) != 0) && (s.ForeColor != Color.Empty))
562 this.ForeColor = s.ForeColor;
564 if (((styles & Styles.Height) == 0) && ((s.styles & Styles.Height) != 0) && (s.Height != Unit.Empty))
566 this.Height = s.Height;
568 if (((styles & Styles.Width) == 0) && ((s.styles & Styles.Width) != 0) && (s.Width != Unit.Empty))
570 this.Width = s.Width;
574 public virtual void Reset()
576 viewstate.Remove("BackColor");
577 viewstate.Remove("BorderColor");
578 viewstate.Remove("BorderStyle");
579 viewstate.Remove("BorderWidth");
580 viewstate.Remove("CssClass");
581 viewstate.Remove("ForeColor");
582 viewstate.Remove("Height");
583 viewstate.Remove("Width");
584 if (fontinfo != null)
588 styles = Styles.None;
591 public override string ToString()
595 #endregion // Public Instance Methods
597 #region Protected Instance Methods
598 protected internal void LoadViewState(object state)
600 viewstate.LoadViewState(state);
603 this.styles = Styles.None;
605 if (viewstate["BackColor"] != null)
607 styles |= Styles.BackColor;
609 if (viewstate["BorderColor"] != null)
611 styles |= Styles.BorderColor;
613 if (viewstate["BorderStyle"] != null)
615 styles |= Styles.BorderStyle;
617 if (viewstate["BorderWidth"] != null)
619 styles |= Styles.BorderWidth;
621 if (viewstate["CssClass"] != null)
623 styles |= Styles.CssClass;
625 if (viewstate["ForeColor"] != null)
627 styles |= Styles.ForeColor;
629 if (viewstate["Height"] != null)
631 styles |= Styles.Height;
633 if (viewstate["Width"] != null)
635 styles |= Styles.Width;
637 if (fontinfo != null) {
638 fontinfo.LoadViewState();
641 LoadViewStateInternal();
644 internal virtual void LoadViewStateInternal()
649 protected internal virtual object SaveViewState ()
651 if (styles != Styles.None)
653 return viewstate.SaveViewState();
659 protected internal virtual void SetBit( int bit )
661 throw new NotImplementedException();
664 protected internal virtual void TrackViewState()
667 viewstate.TrackViewState();
669 #endregion // Protected Instance Methods
671 #region IStateManager Properties & Methods
672 void IStateManager.LoadViewState(object state)
674 LoadViewState(state);
677 object IStateManager.SaveViewState()
679 return SaveViewState();
682 void IStateManager.TrackViewState()
687 bool IStateManager.IsTrackingViewState
691 return this.IsTrackingViewState;
694 #endregion // IStateManager Properties & Methods
697 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
699 FillStyleAttributes (attributes);
702 internal void SetRegisteredCssClass (string name)
704 registered_class = name;
707 public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
709 CssStyleCollection col = new CssStyleCollection ();
710 FillStyleAttributes (col, resolver);
715 public string RegisteredCssClass {
717 return registered_class;
721 internal virtual void CopyTextStylesFrom (Style source)
723 // Need to ask lluis if we need fonts, too
724 if ((styles & Styles.ForeColor) != 0) {
725 ForeColor = source.ForeColor;
727 if ((styles & Styles.BackColor) != 0) {
728 BackColor = source.BackColor;
732 public void SetDirty ()
734 if (viewstate != null)
735 viewstate.SetDirty ();
738 public static bool IsStyleEmpty (Style s)