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)]
37 // Not until we actually have StyleConverter
38 // [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
40 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
42 internal const string BitStateKey = "_!SB";
47 BackColor = 0x00000008,
48 BorderColor = 0x00000010,
49 BorderStyle = 0x00000040,
50 BorderWidth = 0x00000020,
51 CssClass = 0x00000002,
53 ForeColor = 0x00000004,
61 FontOverline = 0x4000,
63 FontStrikeout = 0x8000,
64 FontUnderline = 0x2000
70 internal StateBag viewstate;
73 bool _isSharedViewState;
74 string registered_class;
77 #region Public Constructors
80 viewstate = new StateBag ();
81 GC.SuppressFinalize (this);
84 public Style(System.Web.UI.StateBag bag)
87 if (viewstate == null)
88 viewstate = new StateBag ();
89 _isSharedViewState = true;
90 GC.SuppressFinalize (this);
92 #endregion // Public Constructors
94 #region Public Instance Properties
95 [DefaultValue(typeof (Color), "")]
96 [NotifyParentProperty(true)]
97 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
98 [WebSysDescription ("")]
99 [WebCategory ("Appearance")]
100 public Color BackColor
104 if (!CheckBit ((int) Styles.BackColor))
109 return (Color)viewstate["BackColor"];
114 viewstate["BackColor"] = value;
115 SetBit ((int) Styles.BackColor);
119 [DefaultValue(typeof (Color), "")]
120 [NotifyParentProperty(true)]
121 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
122 [WebSysDescription ("")]
123 [WebCategory ("Appearance")]
124 public Color BorderColor
128 if (!CheckBit ((int) Styles.BorderColor))
133 return (Color)viewstate["BorderColor"];
138 viewstate["BorderColor"] = value;
139 SetBit ((int) Styles.BorderColor);
143 [DefaultValue(BorderStyle.NotSet)]
144 [NotifyParentProperty(true)]
145 [WebSysDescription ("")]
146 [WebCategory ("Appearance")]
147 public BorderStyle BorderStyle
151 if (!CheckBit ((int) Styles.BorderStyle))
153 return BorderStyle.NotSet;
156 return (BorderStyle)viewstate["BorderStyle"];
161 if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
162 throw new ArgumentOutOfRangeException ("value", "The selected value is not one of the BorderStyle enumeration values.");
164 viewstate["BorderStyle"] = value;
165 SetBit ((int) Styles.BorderStyle);
169 [DefaultValue(typeof (Unit), "")]
170 [NotifyParentProperty(true)]
171 [WebSysDescription ("")]
172 [WebCategory ("Appearance")]
173 public Unit BorderWidth
177 if (!CheckBit ((int) Styles.BorderWidth))
182 return (Unit)viewstate["BorderWidth"];
189 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
192 viewstate["BorderWidth"] = value;
193 SetBit ((int) Styles.BorderWidth);
197 //[TypeConverter ("System.Web.UI.WebControls.EmptyStringExpandableObjectConverter")]
200 [NotifyParentProperty(true)]
201 [WebSysDescription ("")]
202 [WebCategory ("Appearance")]
203 public string CssClass {
205 if (!CheckBit ((int) Styles.CssClass))
210 string ret = viewstate["CssClass"] as string;
218 viewstate["CssClass"] = value;
219 SetBit ((int) Styles.CssClass);
223 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
224 [NotifyParentProperty(true)]
225 [WebSysDescription ("")]
226 [WebCategory ("Appearance")]
231 if (fontinfo == null)
233 fontinfo = new FontInfo(this);
239 [DefaultValue(typeof (Color), "")]
240 [NotifyParentProperty(true)]
241 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
242 [WebSysDescription ("")]
243 [WebCategory ("Appearance")]
244 public Color ForeColor
248 if (!CheckBit ((int) Styles.ForeColor))
253 return (Color)viewstate["ForeColor"];
258 viewstate["ForeColor"] = value;
259 SetBit ((int) Styles.ForeColor);
263 [DefaultValue(typeof (Unit), "")]
264 [NotifyParentProperty(true)]
265 [WebSysDescription ("")]
266 [WebCategory ("Appearance")]
271 if (!CheckBit ((int) Styles.Height))
276 return (Unit)viewstate["Height"];
283 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
286 viewstate["Height"] = value;
287 SetBit ((int) Styles.Height);
291 [DefaultValue(typeof (Unit), "")]
292 [NotifyParentProperty(true)]
293 [WebSysDescription ("")]
294 [WebCategory ("Appearance")]
299 if (!CheckBit ((int) Styles.Width))
304 return (Unit)viewstate["Width"];
311 throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
314 viewstate["Width"] = value;
315 SetBit ((int) Styles.Width);
318 #endregion // Public Instance Properties
320 #region Protected Instance Properties
322 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
323 public virtual bool IsEmpty
327 return (styles == 0 && RegisteredCssClass.Length == 0);
331 protected bool IsTrackingViewState
340 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
341 protected internal StateBag ViewState
348 #endregion // Protected Instance Properties
350 #region Internal Instance Properties
351 internal bool AlwaysRenderTextDecoration
355 if (viewstate["AlwaysRenderTextDecoration"] == null)
357 return (bool)viewstate["AlwaysRenderTextDecoration"];
362 viewstate["AlwaysRenderTextDecoration"] = value;
365 #endregion // Internal Instance Properties
367 #region Public Instance Methods
368 public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
370 AddAttributesToRender(writer, null);
373 public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
375 if (RegisteredCssClass.Length > 0) {
376 string cssclass = CssClass;
377 if (!String.IsNullOrEmpty (cssclass))
378 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass + " " + RegisteredCssClass);
380 writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
384 string cssclass = CssClass;
385 if (cssclass != null && cssclass.Length > 0)
386 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass);
387 CssStyleCollection col = new CssStyleCollection ();
388 FillStyleAttributes (col, owner);
389 foreach (string key in col.Keys) {
390 writer.AddStyleAttribute (key, col [key]);
394 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
400 if (CheckBit ((int) Styles.BackColor))
402 color = (Color)viewstate["BackColor"];
404 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
407 if (CheckBit ((int) Styles.BorderColor))
409 color = (Color)viewstate["BorderColor"];
411 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
414 bool have_width = false;
415 if (CheckBit ((int) Styles.BorderWidth)) {
416 u = (Unit) viewstate ["BorderWidth"];
420 attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
424 if (CheckBit ((int) Styles.BorderStyle)) {
425 bs = (BorderStyle) viewstate ["BorderStyle"];
426 if (bs != BorderStyle.NotSet)
427 attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
429 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
431 else if (have_width) {
432 attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
435 if (CheckBit ((int) Styles.ForeColor))
437 color = (Color)viewstate["ForeColor"];
439 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
442 if (CheckBit ((int) Styles.Height))
444 u = (Unit)viewstate["Height"];
446 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
449 if (CheckBit ((int) Styles.Width))
451 u = (Unit)viewstate["Width"];
453 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
456 Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
459 public virtual void CopyFrom(Style s)
461 if ((s == null) || s.IsEmpty)
466 if (s.fontinfo != null)
468 Font.CopyFrom(s.fontinfo);
471 if ((s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
473 this.BackColor = s.BackColor;
475 if ((s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
477 this.BorderColor = s.BorderColor;
479 if ((s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
481 this.BorderStyle = s.BorderStyle;
483 if ((s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
485 this.BorderWidth = s.BorderWidth;
487 if ((s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
489 this.CssClass = s.CssClass;
491 if ((s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
493 this.ForeColor = s.ForeColor;
495 if ((s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
497 this.Height = s.Height;
499 if ((s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
501 this.Width = s.Width;
505 public virtual void MergeWith(Style s)
507 if ((s == null) || (s.IsEmpty))
512 if (s.fontinfo != null)
514 Font.MergeWith(s.fontinfo);
517 if ((!CheckBit ((int) Styles.BackColor)) && (s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
519 this.BackColor = s.BackColor;
521 if ((!CheckBit ((int) Styles.BorderColor)) && (s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
523 this.BorderColor = s.BorderColor;
525 if ((!CheckBit ((int) Styles.BorderStyle)) && (s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
527 this.BorderStyle = s.BorderStyle;
529 if ((!CheckBit ((int) Styles.BorderWidth)) && (s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
531 this.BorderWidth = s.BorderWidth;
533 if ((!CheckBit ((int) Styles.CssClass)) && (s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
535 this.CssClass = s.CssClass;
537 if ((!CheckBit ((int) Styles.ForeColor)) && (s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
539 this.ForeColor = s.ForeColor;
541 if ((!CheckBit ((int) Styles.Height)) && (s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
543 this.Height = s.Height;
545 if ((!CheckBit ((int) Styles.Width)) && (s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
547 this.Width = s.Width;
552 internal void Print ()
554 Console.WriteLine ("BackColor: {0}", BackColor);
555 Console.WriteLine ("BorderColor: {0}", BorderColor);
556 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
557 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
558 Console.WriteLine ("CssClass: {0}", CssClass);
559 Console.WriteLine ("ForeColor: {0}", ForeColor);
560 Console.WriteLine ("Height: {0}", Height);
561 Console.WriteLine ("Width: {0}", Width);
565 public virtual void Reset()
567 viewstate.Remove("BackColor");
568 viewstate.Remove("BorderColor");
569 viewstate.Remove("BorderStyle");
570 viewstate.Remove("BorderWidth");
571 viewstate.Remove("CssClass");
572 viewstate.Remove("ForeColor");
573 viewstate.Remove("Height");
574 viewstate.Remove("Width");
575 if (fontinfo != null)
580 viewstate.Remove (BitStateKey);
583 #endregion // Public Instance Methods
585 #region Protected Instance Methods
586 protected internal void LoadViewState(object state)
588 viewstate.LoadViewState(state);
593 protected internal virtual object SaveViewState ()
597 if (_isSharedViewState)
600 return viewstate.SaveViewState ();
604 internal void SaveBitState ()
606 if (stylesTraked != 0)
607 viewstate [BitStateKey] = stylesTraked;
610 internal void LoadBitState () {
611 if (viewstate [BitStateKey] == null)
614 int bit = (int) viewstate [BitStateKey];
619 protected internal virtual void SetBit( int bit )
626 internal void RemoveBit (int bit) {
629 stylesTraked &= ~bit;
630 if (stylesTraked == 0)
631 viewstate.Remove (BitStateKey);
635 internal bool CheckBit (int bit) {
636 return (styles & bit) != 0;
639 protected internal virtual void TrackViewState ()
642 viewstate.TrackViewState();
644 #endregion // Protected Instance Methods
646 #region IStateManager Properties & Methods
647 void IStateManager.LoadViewState(object state)
649 LoadViewState(state);
652 object IStateManager.SaveViewState()
654 return SaveViewState();
657 void IStateManager.TrackViewState()
662 bool IStateManager.IsTrackingViewState
666 return this.IsTrackingViewState;
669 #endregion // IStateManager Properties & Methods
671 internal void SetRegisteredCssClass (string name)
673 registered_class = name;
676 public CssStyleCollection GetStyleAttributes (IUrlResolutionService urlResolver)
678 CssStyleCollection col = new CssStyleCollection ();
679 FillStyleAttributes (col, urlResolver);
683 [EditorBrowsable(EditorBrowsableState.Advanced)]
684 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
686 public string RegisteredCssClass {
688 if (registered_class == null)
689 registered_class = String.Empty;
690 return registered_class;
694 internal void CopyTextStylesFrom (Style source) {
695 // Used primary for TreeView and Menu
696 if (source.CheckBit ((int) Styles.ForeColor)) {
697 ForeColor = source.ForeColor;
699 if (source.CheckBit((int) Styles.FontAll)) {
700 Font.CopyFrom (source.Font);
704 internal void RemoveTextStyles () {
705 ForeColor = Color.Empty;
709 internal void AddCssClass (string cssClass)
711 if (String.IsNullOrEmpty (cssClass))
714 string newClass = CssClass;
715 if (newClass.Length > 0)
717 newClass += cssClass;
720 internal void PrependCssClass (string cssClass)
722 if (String.IsNullOrEmpty (cssClass))
725 string oldClass = CssClass;
726 if (oldClass.Length > 0)
728 CssClass = cssClass + oldClass;
730 public void SetDirty ()
732 if (viewstate != null)
733 viewstate.SetDirty (true);
734 stylesTraked = styles;