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
);
216 [NotifyParentProperty(true)]
217 [WebSysDescription ("")]
218 [WebCategory ("Appearance")]
219 public string CssClass
223 if (!CheckBit ((int) Styles
.CssClass
))
228 string ret
= viewstate
["CssClass"] as string;
237 viewstate
["CssClass"] = value;
238 SetBit ((int) 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 (!CheckBit ((int) Styles
.ForeColor
))
275 return (Color
)viewstate
["ForeColor"];
280 viewstate
["ForeColor"] = value;
281 SetBit ((int) Styles
.ForeColor
);
288 [DefaultValue(typeof (Unit
), "")]
289 [NotifyParentProperty(true)]
290 [WebSysDescription ("")]
291 [WebCategory ("Appearance")]
296 if (!CheckBit ((int) Styles
.Height
))
301 return (Unit
)viewstate
["Height"];
308 throw new ArgumentOutOfRangeException("Value", value.Value
, "Height must not be negative");
311 viewstate
["Height"] = value;
312 SetBit ((int) Styles
.Height
);
319 [DefaultValue(typeof (Unit
), "")]
320 [NotifyParentProperty(true)]
321 [WebSysDescription ("")]
322 [WebCategory ("Appearance")]
327 if (!CheckBit ((int) Styles
.Width
))
332 return (Unit
)viewstate
["Width"];
339 throw new ArgumentOutOfRangeException("Value", value.Value
, "Width must not be negative");
342 viewstate
["Width"] = value;
343 SetBit ((int) 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
360 return (styles
== 0 && RegisteredCssClass
.Length
== 0);
362 return (styles
== 0);
367 protected bool IsTrackingViewState
376 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
377 protected internal StateBag ViewState
384 #endregion // Protected Instance Properties
386 #region Internal Instance Properties
387 internal bool AlwaysRenderTextDecoration
391 if (viewstate
["AlwaysRenderTextDecoration"] == null)
393 return (bool)viewstate
["AlwaysRenderTextDecoration"];
398 viewstate
["AlwaysRenderTextDecoration"] = value;
401 #endregion // Internal Instance Properties
403 #region Public Instance Methods
404 public void AddAttributesToRender(System
.Web
.UI
.HtmlTextWriter writer
)
406 AddAttributesToRender(writer
, null);
409 public virtual void AddAttributesToRender(System
.Web
.UI
.HtmlTextWriter writer
, WebControl owner
)
412 if (RegisteredCssClass
.Length
> 0) {
413 string cssclass
= CssClass
;
414 if (!String
.IsNullOrEmpty (cssclass
))
415 writer
.AddAttribute (HtmlTextWriterAttribute
.Class
, cssclass
+ " " + RegisteredCssClass
);
417 writer
.AddAttribute (HtmlTextWriterAttribute
.Class
, RegisteredCssClass
);
422 string cssclass
= CssClass
;
423 if (cssclass
!= null && cssclass
.Length
> 0)
424 writer
.AddAttribute (HtmlTextWriterAttribute
.Class
, cssclass
);
426 CssStyleCollection col
= new CssStyleCollection ();
427 FillStyleAttributes (col
, owner
);
428 foreach (string key
in col
.Keys
) {
429 writer
.AddStyleAttribute (key
, col
[key
]);
432 WriteStyleAttributes (writer
);
438 void WriteStyleAttributes (HtmlTextWriter writer
)
445 if (CheckBit ((int) Styles
.BackColor
)) {
446 color
= (Color
)viewstate
["BackColor"];
448 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BackgroundColor
, ColorTranslator
.ToHtml(color
));
451 if (CheckBit ((int) Styles
.BorderColor
)) {
452 color
= (Color
)viewstate
["BorderColor"];
454 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BorderColor
, ColorTranslator
.ToHtml(color
));
457 bool have_width
= false;
458 if (CheckBit ((int) Styles
.BorderWidth
)) {
459 u
= (Unit
)viewstate
["BorderWidth"];
463 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BorderWidth
, u
.ToString());
467 if (CheckBit ((int) Styles
.BorderStyle
)) {
468 bs
= (BorderStyle
)viewstate
["BorderStyle"];
469 if (bs
!= BorderStyle
.NotSet
)
470 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BorderStyle
, bs
.ToString());
472 if (CheckBit ((int) Styles
.BorderWidth
))
473 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BorderStyle
, "solid");
475 } else if (have_width
) {
476 writer
.AddStyleAttribute (HtmlTextWriterStyle
.BorderStyle
, "solid");
479 if (CheckBit ((int) Styles
.ForeColor
)) {
480 color
= (Color
)viewstate
["ForeColor"];
482 writer
.AddStyleAttribute (HtmlTextWriterStyle
.Color
, ColorTranslator
.ToHtml(color
));
485 if (CheckBit ((int) Styles
.Height
)) {
486 u
= (Unit
)viewstate
["Height"];
488 writer
.AddStyleAttribute (HtmlTextWriterStyle
.Height
, u
.ToString());
491 if (CheckBit ((int) Styles
.Width
)) {
492 u
= (Unit
)viewstate
["Width"];
494 writer
.AddStyleAttribute (HtmlTextWriterStyle
.Width
, u
.ToString());
497 if (CheckBit ((int) Style
.Styles
.FontAll
)) {
498 // Fonts are a bit weird
499 FontInfo font
= Font
;
500 if (font
.Name
!= string.Empty
) {
502 for (int i
= 1; i
< font
.Names
.Length
; i
++)
503 s
+= "," + font
.Names
[i
];
504 writer
.AddStyleAttribute (HtmlTextWriterStyle
.FontFamily
, s
);
508 writer
.AddStyleAttribute (HtmlTextWriterStyle
.FontWeight
, "bold");
511 writer
.AddStyleAttribute (HtmlTextWriterStyle
.FontStyle
, "italic");
513 if (!font
.Size
.IsEmpty
)
514 writer
.AddStyleAttribute (HtmlTextWriterStyle
.FontSize
, font
.Size
.ToString());
516 // These styles are munged into a attribute decoration
523 s
+= "line-through ";
528 s
= (s
!= "") ? s
: AlwaysRenderTextDecoration
? "none" : "";
530 writer
.AddStyleAttribute (HtmlTextWriterStyle
.TextDecoration
, s
);
536 protected virtual void FillStyleAttributes (CssStyleCollection attributes
, IUrlResolutionService urlResolver
)
542 if (CheckBit ((int) Styles
.BackColor
))
544 color
= (Color
)viewstate
["BackColor"];
546 attributes
.Add (HtmlTextWriterStyle
.BackgroundColor
, ColorTranslator
.ToHtml(color
));
549 if (CheckBit ((int) Styles
.BorderColor
))
551 color
= (Color
)viewstate
["BorderColor"];
553 attributes
.Add (HtmlTextWriterStyle
.BorderColor
, ColorTranslator
.ToHtml(color
));
556 bool have_width
= false;
557 if (CheckBit ((int) Styles
.BorderWidth
)) {
558 u
= (Unit
) viewstate
["BorderWidth"];
562 attributes
.Add (HtmlTextWriterStyle
.BorderWidth
, u
.ToString ());
566 if (CheckBit ((int) Styles
.BorderStyle
)) {
567 bs
= (BorderStyle
) viewstate
["BorderStyle"];
568 if (bs
!= BorderStyle
.NotSet
)
569 attributes
.Add (HtmlTextWriterStyle
.BorderStyle
, bs
.ToString ());
571 attributes
.Add (HtmlTextWriterStyle
.BorderStyle
, "solid");
573 else if (have_width
) {
574 attributes
.Add (HtmlTextWriterStyle
.BorderStyle
, "solid");
577 if (CheckBit ((int) Styles
.ForeColor
))
579 color
= (Color
)viewstate
["ForeColor"];
581 attributes
.Add (HtmlTextWriterStyle
.Color
, ColorTranslator
.ToHtml(color
));
584 if (CheckBit ((int) Styles
.Height
))
586 u
= (Unit
)viewstate
["Height"];
588 attributes
.Add (HtmlTextWriterStyle
.Height
, u
.ToString());
591 if (CheckBit ((int) Styles
.Width
))
593 u
= (Unit
)viewstate
["Width"];
595 attributes
.Add (HtmlTextWriterStyle
.Width
, u
.ToString());
598 Font
.FillStyleAttributes (attributes
, AlwaysRenderTextDecoration
);
602 public virtual void CopyFrom(Style s
)
604 if ((s
== null) || s
.IsEmpty
)
609 if (s
.fontinfo
!= null)
611 Font
.CopyFrom(s
.fontinfo
);
614 if ((s
.CheckBit ((int) Styles
.BackColor
)) && (s
.BackColor
!= Color
.Empty
))
616 this.BackColor
= s
.BackColor
;
618 if ((s
.CheckBit ((int) Styles
.BorderColor
)) && (s
.BorderColor
!= Color
.Empty
))
620 this.BorderColor
= s
.BorderColor
;
622 if ((s
.CheckBit ((int) Styles
.BorderStyle
)) && (s
.BorderStyle
!= BorderStyle
.NotSet
))
624 this.BorderStyle
= s
.BorderStyle
;
626 if ((s
.CheckBit ((int) Styles
.BorderWidth
)) && (!s
.BorderWidth
.IsEmpty
))
628 this.BorderWidth
= s
.BorderWidth
;
630 if ((s
.CheckBit ((int) Styles
.CssClass
)) && (s
.CssClass
!= string.Empty
))
632 this.CssClass
= s
.CssClass
;
634 if ((s
.CheckBit ((int) Styles
.ForeColor
)) && (s
.ForeColor
!= Color
.Empty
))
636 this.ForeColor
= s
.ForeColor
;
638 if ((s
.CheckBit ((int) Styles
.Height
)) && (!s
.Height
.IsEmpty
))
640 this.Height
= s
.Height
;
642 if ((s
.CheckBit ((int) Styles
.Width
)) && (!s
.Width
.IsEmpty
))
644 this.Width
= s
.Width
;
648 public virtual void MergeWith(Style s
)
650 if ((s
== null) || (s
.IsEmpty
))
655 if (s
.fontinfo
!= null)
657 Font
.MergeWith(s
.fontinfo
);
660 if ((!CheckBit ((int) Styles
.BackColor
)) && (s
.CheckBit ((int) Styles
.BackColor
)) && (s
.BackColor
!= Color
.Empty
))
662 this.BackColor
= s
.BackColor
;
664 if ((!CheckBit ((int) Styles
.BorderColor
)) && (s
.CheckBit ((int) Styles
.BorderColor
)) && (s
.BorderColor
!= Color
.Empty
))
666 this.BorderColor
= s
.BorderColor
;
668 if ((!CheckBit ((int) Styles
.BorderStyle
)) && (s
.CheckBit ((int) Styles
.BorderStyle
)) && (s
.BorderStyle
!= BorderStyle
.NotSet
))
670 this.BorderStyle
= s
.BorderStyle
;
672 if ((!CheckBit ((int) Styles
.BorderWidth
)) && (s
.CheckBit ((int) Styles
.BorderWidth
)) && (!s
.BorderWidth
.IsEmpty
))
674 this.BorderWidth
= s
.BorderWidth
;
676 if ((!CheckBit ((int) Styles
.CssClass
)) && (s
.CheckBit ((int) Styles
.CssClass
)) && (s
.CssClass
!= string.Empty
))
678 this.CssClass
= s
.CssClass
;
680 if ((!CheckBit ((int) Styles
.ForeColor
)) && (s
.CheckBit ((int) Styles
.ForeColor
)) && (s
.ForeColor
!= Color
.Empty
))
682 this.ForeColor
= s
.ForeColor
;
684 if ((!CheckBit ((int) Styles
.Height
)) && (s
.CheckBit ((int) Styles
.Height
)) && (!s
.Height
.IsEmpty
))
686 this.Height
= s
.Height
;
688 if ((!CheckBit ((int) Styles
.Width
)) && (s
.CheckBit ((int) Styles
.Width
)) && (!s
.Width
.IsEmpty
))
690 this.Width
= s
.Width
;
695 internal void Print ()
697 Console.WriteLine ("BackColor: {0}", BackColor);
698 Console.WriteLine ("BorderColor: {0}", BorderColor);
699 Console.WriteLine ("BorderStyle: {0}", BorderStyle);
700 Console.WriteLine ("BorderWidth: {0}", BorderWidth);
701 Console.WriteLine ("CssClass: {0}", CssClass);
702 Console.WriteLine ("ForeColor: {0}", ForeColor);
703 Console.WriteLine ("Height: {0}", Height);
704 Console.WriteLine ("Width: {0}", Width);
708 public virtual void Reset()
710 viewstate
.Remove("BackColor");
711 viewstate
.Remove("BorderColor");
712 viewstate
.Remove("BorderStyle");
713 viewstate
.Remove("BorderWidth");
714 viewstate
.Remove("CssClass");
715 viewstate
.Remove("ForeColor");
716 viewstate
.Remove("Height");
717 viewstate
.Remove("Width");
718 if (fontinfo
!= null)
723 viewstate
.Remove (BitStateKey
);
727 public override string ToString()
732 #endregion // Public Instance Methods
734 #region Protected Instance Methods
735 protected internal void LoadViewState(object state
)
737 viewstate
.LoadViewState(state
);
742 protected internal virtual object SaveViewState ()
746 if (_isSharedViewState
)
749 return viewstate
.SaveViewState ();
753 internal void SaveBitState ()
755 if (stylesTraked
!= 0)
756 viewstate
[BitStateKey
] = stylesTraked
;
759 internal void LoadBitState () {
760 if (viewstate
[BitStateKey
] == null)
763 int bit
= (int) viewstate
[BitStateKey
];
768 protected internal virtual void SetBit( int bit
)
775 internal void RemoveBit (int bit
) {
778 stylesTraked
&= ~bit
;
779 if (stylesTraked
== 0)
780 viewstate
.Remove (BitStateKey
);
784 internal bool CheckBit (int bit
) {
785 return (styles
& bit
) != 0;
788 protected internal virtual void TrackViewState ()
791 viewstate
.TrackViewState();
793 #endregion // Protected Instance Methods
795 #region IStateManager Properties & Methods
796 void IStateManager
.LoadViewState(object state
)
798 LoadViewState(state
);
801 object IStateManager
.SaveViewState()
803 return SaveViewState();
806 void IStateManager
.TrackViewState()
811 bool IStateManager
.IsTrackingViewState
815 return this.IsTrackingViewState
;
818 #endregion // IStateManager Properties & Methods
821 internal void SetRegisteredCssClass (string name
)
823 registered_class
= name
;
826 public CssStyleCollection
GetStyleAttributes (IUrlResolutionService resolver
)
828 CssStyleCollection col
= new CssStyleCollection ();
829 FillStyleAttributes (col
, resolver
);
833 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
834 [DesignerSerializationVisibility(DesignerSerializationVisibility
.Hidden
)]
836 public string RegisteredCssClass
{
838 if (registered_class
== null)
839 registered_class
= String
.Empty
;
840 return registered_class
;
844 internal void CopyTextStylesFrom (Style source
) {
845 // Used primary for TreeView and Menu
846 if (source
.CheckBit ((int) Styles
.ForeColor
)) {
847 ForeColor
= source
.ForeColor
;
849 if (source
.CheckBit((int) Styles
.FontAll
)) {
850 Font
.CopyFrom (source
.Font
);
854 internal void RemoveTextStyles () {
855 ForeColor
= Color
.Empty
;
859 internal void AddCssClass (string cssClass
) {
860 if (String
.IsNullOrEmpty (cssClass
))
863 if (CssClass
.Length
> 0)
865 CssClass
+= cssClass
;
868 public void SetDirty ()
870 if (viewstate
!= null)
871 viewstate
.SetDirty (true);
872 stylesTraked
= styles
;