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
46 internal const string BitStateKey = "_!SB";
51 BackColor = 0x00000008,
52 BorderColor = 0x00000010,
53 BorderStyle = 0x00000040,
54 BorderWidth = 0x00000020,
55 CssClass = 0x00000002,
57 ForeColor = 0x00000004,
65 FontOverline = 0x4000,
67 FontStrikeout = 0x8000,
68 FontUnderline = 0x2000
74 internal StateBag viewstate;
77 bool _isSharedViewState;
79 string registered_class;
83 #region Public Constructors
86 viewstate = new StateBag ();
87 GC.SuppressFinalize (this);
90 public Style(System.Web.UI.StateBag bag)
93 if (viewstate == null)
94 viewstate = new StateBag ();
95 _isSharedViewState = true;
96 GC.SuppressFinalize (this);
98 #endregion // Public Constructors
100 #region Public Instance Properties
104 [DefaultValue(typeof (Color), "")]
105 [NotifyParentProperty(true)]
106 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
107 [WebSysDescription ("")]
108 [WebCategory ("Appearance")]
109 public Color BackColor
113 if (!CheckBit ((int) Styles.BackColor))
118 return (Color)viewstate["BackColor"];
123 viewstate["BackColor"] = value;
124 SetBit ((int) Styles.BackColor);
131 [DefaultValue(typeof (Color), "")]
132 [NotifyParentProperty(true)]
133 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
134 [WebSysDescription ("")]
135 [WebCategory ("Appearance")]
136 public Color BorderColor
140 if (!CheckBit ((int) Styles.BorderColor))
145 return (Color)viewstate["BorderColor"];
150 viewstate["BorderColor"] = value;
151 SetBit ((int) Styles.BorderColor);
158 [DefaultValue(BorderStyle.NotSet)]
159 [NotifyParentProperty(true)]
160 [WebSysDescription ("")]
161 [WebCategory ("Appearance")]
162 public BorderStyle BorderStyle
166 if (!CheckBit ((int) Styles.BorderStyle))
168 return BorderStyle.NotSet;
171 return (BorderStyle)viewstate["BorderStyle"];
176 if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
177 throw new ArgumentOutOfRangeException ("value", "The selected value is not one of the BorderStyle enumeration values.");
179 viewstate["BorderStyle"] = value;
180 SetBit ((int) Styles.BorderStyle);
187 [DefaultValue(typeof (Unit), "")]
188 [NotifyParentProperty(true)]
189 [WebSysDescription ("")]
190 [WebCategory ("Appearance")]
191 public Unit BorderWidth
195 if (!CheckBit ((int) Styles.BorderWidth))
200 return (Unit)viewstate["BorderWidth"];
207 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
210 viewstate["BorderWidth"] = value;
211 SetBit ((int) Styles.BorderWidth);
215 //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
218 [NotifyParentProperty(true)]
219 [WebSysDescription ("")]
220 [WebCategory ("Appearance")]
221 public string CssClass {
223 if (!CheckBit ((int) Styles.CssClass))
228 string ret = viewstate["CssClass"] as string;
236 viewstate["CssClass"] = value;
237 SetBit ((int) Styles.CssClass);
241 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
242 [NotifyParentProperty(true)]
243 [WebSysDescription ("")]
244 [WebCategory ("Appearance")]
249 if (fontinfo == null)
251 fontinfo = new FontInfo(this);
260 [DefaultValue(typeof (Color), "")]
261 [NotifyParentProperty(true)]
262 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
263 [WebSysDescription ("")]
264 [WebCategory ("Appearance")]
265 public Color ForeColor
269 if (!CheckBit ((int) Styles.ForeColor))
274 return (Color)viewstate["ForeColor"];
279 viewstate["ForeColor"] = value;
280 SetBit ((int) Styles.ForeColor);
287 [DefaultValue(typeof (Unit), "")]
288 [NotifyParentProperty(true)]
289 [WebSysDescription ("")]
290 [WebCategory ("Appearance")]
295 if (!CheckBit ((int) Styles.Height))
300 return (Unit)viewstate["Height"];
307 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
310 viewstate["Height"] = value;
311 SetBit ((int) Styles.Height);
318 [DefaultValue(typeof (Unit), "")]
319 [NotifyParentProperty(true)]
320 [WebSysDescription ("")]
321 [WebCategory ("Appearance")]
326 if (!CheckBit ((int) Styles.Width))
331 return (Unit)viewstate["Width"];
338 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
341 viewstate["Width"] = value;
342 SetBit ((int) Styles.Width);
345 #endregion // Public Instance Properties
347 #region Protected Instance Properties
350 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
351 public virtual bool IsEmpty
353 protected internal virtual bool IsEmpty
359 return (styles == 0 && RegisteredCssClass.Length == 0);
361 return (styles == 0);
366 protected bool IsTrackingViewState
375 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
376 protected internal StateBag ViewState
383 #endregion // Protected Instance Properties
385 #region Internal Instance Properties
386 internal bool AlwaysRenderTextDecoration
390 if (viewstate["AlwaysRenderTextDecoration"] == null)
392 return (bool)viewstate["AlwaysRenderTextDecoration"];
397 viewstate["AlwaysRenderTextDecoration"] = value;
400 #endregion // Internal Instance Properties
402 #region Public Instance Methods
403 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
405 AddAttributesToRender(writer, null);
408 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
411 if (RegisteredCssClass.Length > 0) {
412 string cssclass = CssClass;
413 if (!String.IsNullOrEmpty (cssclass))
414 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass + " " + RegisteredCssClass);
416 writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
421 string cssclass = CssClass;
422 if (cssclass != null && cssclass.Length > 0)
423 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass);
425 CssStyleCollection col = new CssStyleCollection ();
426 FillStyleAttributes (col, owner);
427 foreach (string key in col.Keys) {
428 writer.AddStyleAttribute (key, col [key]);
431 WriteStyleAttributes (writer);
436 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
442 if (CheckBit ((int) Styles.BackColor))
444 color = (Color)viewstate["BackColor"];
446 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
449 if (CheckBit ((int) Styles.BorderColor))
451 color = (Color)viewstate["BorderColor"];
453 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
456 bool have_width = false;
457 if (CheckBit ((int) Styles.BorderWidth)) {
458 u = (Unit) viewstate ["BorderWidth"];
462 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
466 if (CheckBit ((int) Styles.BorderStyle)) {
467 bs = (BorderStyle) viewstate ["BorderStyle"];
468 if (bs != BorderStyle.NotSet)
469 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
471 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
473 else if (have_width) {
474 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
477 if (CheckBit ((int) Styles.ForeColor))
479 color = (Color)viewstate["ForeColor"];
481 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
484 if (CheckBit ((int) Styles.Height))
486 u = (Unit)viewstate["Height"];
488 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
491 if (CheckBit ((int) Styles.Width))
493 u = (Unit)viewstate["Width"];
495 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
498 Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
502 public virtual void CopyFrom(Style s)
504 if ((s == null) || s.IsEmpty)
509 if (s.fontinfo != null)
511 Font.CopyFrom(s.fontinfo);
514 if ((s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
516 this.BackColor = s.BackColor;
518 if ((s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
520 this.BorderColor = s.BorderColor;
522 if ((s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
524 this.BorderStyle = s.BorderStyle;
526 if ((s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
528 this.BorderWidth = s.BorderWidth;
530 if ((s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
532 this.CssClass = s.CssClass;
534 if ((s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
536 this.ForeColor = s.ForeColor;
538 if ((s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
540 this.Height = s.Height;
542 if ((s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
544 this.Width = s.Width;
548 public virtual void MergeWith(Style s)
550 if ((s == null) || (s.IsEmpty))
555 if (s.fontinfo != null)
557 Font.MergeWith(s.fontinfo);
560 if ((!CheckBit ((int) Styles.BackColor)) && (s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
562 this.BackColor = s.BackColor;
564 if ((!CheckBit ((int) Styles.BorderColor)) && (s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
566 this.BorderColor = s.BorderColor;
568 if ((!CheckBit ((int) Styles.BorderStyle)) && (s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
570 this.BorderStyle = s.BorderStyle;
572 if ((!CheckBit ((int) Styles.BorderWidth)) && (s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
574 this.BorderWidth = s.BorderWidth;
576 if ((!CheckBit ((int) Styles.CssClass)) && (s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
578 this.CssClass = s.CssClass;
580 if ((!CheckBit ((int) Styles.ForeColor)) && (s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
582 this.ForeColor = s.ForeColor;
584 if ((!CheckBit ((int) Styles.Height)) && (s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
586 this.Height = s.Height;
588 if ((!CheckBit ((int) Styles.Width)) && (s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
590 this.Width = s.Width;
595 internal void Print ()
597 Console.WriteLine ("BackColor: {0}", BackColor);
598 Console.WriteLine ("BorderColor: {0}", BorderColor);
599 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
600 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
601 Console.WriteLine ("CssClass: {0}", CssClass);
602 Console.WriteLine ("ForeColor: {0}", ForeColor);
603 Console.WriteLine ("Height: {0}", Height);
604 Console.WriteLine ("Width: {0}", Width);
608 public virtual void Reset()
610 viewstate.Remove("BackColor");
611 viewstate.Remove("BorderColor");
612 viewstate.Remove("BorderStyle");
613 viewstate.Remove("BorderWidth");
614 viewstate.Remove("CssClass");
615 viewstate.Remove("ForeColor");
616 viewstate.Remove("Height");
617 viewstate.Remove("Width");
618 if (fontinfo != null)
623 viewstate.Remove (BitStateKey);
627 public override string ToString()
632 #endregion // Public Instance Methods
634 #region Protected Instance Methods
635 protected internal void LoadViewState(object state)
637 viewstate.LoadViewState(state);
642 protected internal virtual object SaveViewState ()
646 if (_isSharedViewState)
649 return viewstate.SaveViewState ();
653 internal void SaveBitState ()
655 if (stylesTraked != 0)
656 viewstate [BitStateKey] = stylesTraked;
659 internal void LoadBitState () {
660 if (viewstate [BitStateKey] == null)
663 int bit = (int) viewstate [BitStateKey];
668 protected internal virtual void SetBit( int bit )
675 internal void RemoveBit (int bit) {
678 stylesTraked &= ~bit;
679 if (stylesTraked == 0)
680 viewstate.Remove (BitStateKey);
684 internal bool CheckBit (int bit) {
685 return (styles & bit) != 0;
688 protected internal virtual void TrackViewState ()
691 viewstate.TrackViewState();
693 #endregion // Protected Instance Methods
695 #region IStateManager Properties & Methods
696 void IStateManager.LoadViewState(object state)
698 LoadViewState(state);
701 object IStateManager.SaveViewState()
703 return SaveViewState();
706 void IStateManager.TrackViewState()
711 bool IStateManager.IsTrackingViewState
715 return this.IsTrackingViewState;
718 #endregion // IStateManager Properties & Methods
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);
733 [EditorBrowsable(EditorBrowsableState.Advanced)]
734 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
736 public string RegisteredCssClass {
738 if (registered_class == null)
739 registered_class = String.Empty;
740 return registered_class;
744 internal void CopyTextStylesFrom (Style source) {
745 // Used primary for TreeView and Menu
746 if (source.CheckBit ((int) Styles.ForeColor)) {
747 ForeColor = source.ForeColor;
749 if (source.CheckBit((int) Styles.FontAll)) {
750 Font.CopyFrom (source.Font);
754 internal void RemoveTextStyles () {
755 ForeColor = Color.Empty;
759 internal void AddCssClass (string cssClass)
761 if (String.IsNullOrEmpty (cssClass))
764 string newClass = CssClass;
765 if (newClass.Length > 0)
767 newClass += cssClass;
771 internal void PrependCssClass (string cssClass)
773 if (String.IsNullOrEmpty (cssClass))
776 string oldClass = CssClass;
777 if (oldClass.Length > 0)
779 CssClass = cssClass + oldClass;
782 public void SetDirty ()
784 if (viewstate != null)
785 viewstate.SetDirty (true);
786 stylesTraked = styles;