2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web / System.Web.UI.WebControls / Style.cs
blob045cf1904d78f5a25e50a5b82532fbee70b01a39
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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)
22 // Authors:
23 // Peter Dennis Bartok (pbartok@novell.com)
27 using System.ComponentModel;
28 using System.Drawing;
29 using System.Security.Permissions;
31 namespace System.Web.UI.WebControls {
33 // CAS
34 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
35 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 // attributes
37 #if NET_2_0
38 // Not until we actually have StyleConverter
39 // [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
40 #else
41 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
42 #endif
43 [ToolboxItem("")]
44 public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
46 internal const string BitStateKey = "_!SB";
48 [Flags]
49 public enum Styles
51 BackColor = 0x00000008,
52 BorderColor = 0x00000010,
53 BorderStyle = 0x00000040,
54 BorderWidth = 0x00000020,
55 CssClass = 0x00000002,
56 Font = 0x00000001,
57 ForeColor = 0x00000004,
58 Height = 0x00000080,
59 Width = 0x00000100,
61 FontAll = 0xFE00,
62 FontBold = 0x800,
63 FontItalic = 0x1000,
64 FontNames = 0x200,
65 FontOverline = 0x4000,
66 FontSize = 0x400,
67 FontStrikeout = 0x8000,
68 FontUnderline = 0x2000
71 #region Fields
72 int styles;
73 int stylesTraked;
74 internal StateBag viewstate;
75 FontInfo fontinfo;
76 bool tracking;
77 bool _isSharedViewState;
78 #if NET_2_0
79 string registered_class;
80 #endif
81 #endregion // Fields
83 #region Public Constructors
84 public Style()
86 viewstate = new StateBag ();
87 GC.SuppressFinalize (this);
90 public Style(System.Web.UI.StateBag bag)
92 viewstate = 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
101 #if !NET_2_0
102 [Bindable(true)]
103 #endif
104 [DefaultValue(typeof (Color), "")]
105 [NotifyParentProperty(true)]
106 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
107 [WebSysDescription ("")]
108 [WebCategory ("Appearance")]
109 public Color BackColor
111 get
113 if (!CheckBit ((int) Styles.BackColor))
115 return Color.Empty;
118 return (Color)viewstate["BackColor"];
121 set
123 viewstate["BackColor"] = value;
124 SetBit ((int) Styles.BackColor);
128 #if !NET_2_0
129 [Bindable(true)]
130 #endif
131 [DefaultValue(typeof (Color), "")]
132 [NotifyParentProperty(true)]
133 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
134 [WebSysDescription ("")]
135 [WebCategory ("Appearance")]
136 public Color BorderColor
138 get
140 if (!CheckBit ((int) Styles.BorderColor))
142 return Color.Empty;
145 return (Color)viewstate["BorderColor"];
148 set
150 viewstate["BorderColor"] = value;
151 SetBit ((int) Styles.BorderColor);
155 #if !NET_2_0
156 [Bindable(true)]
157 #endif
158 [DefaultValue(BorderStyle.NotSet)]
159 [NotifyParentProperty(true)]
160 [WebSysDescription ("")]
161 [WebCategory ("Appearance")]
162 public BorderStyle BorderStyle
164 get
166 if (!CheckBit ((int) Styles.BorderStyle))
168 return BorderStyle.NotSet;
171 return (BorderStyle)viewstate["BorderStyle"];
174 set
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);
184 #if !NET_2_0
185 [Bindable(true)]
186 #endif
187 [DefaultValue(typeof (Unit), "")]
188 [NotifyParentProperty(true)]
189 [WebSysDescription ("")]
190 [WebCategory ("Appearance")]
191 public Unit BorderWidth
193 get
195 if (!CheckBit ((int) Styles.BorderWidth))
197 return Unit.Empty;
200 return (Unit)viewstate["BorderWidth"];
203 set
205 if (value.Value < 0)
207 throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
210 viewstate["BorderWidth"] = value;
211 SetBit ((int) Styles.BorderWidth);
215 [DefaultValue("")]
216 [NotifyParentProperty(true)]
217 [WebSysDescription ("")]
218 [WebCategory ("Appearance")]
219 public string CssClass
221 get
223 if (!CheckBit ((int) Styles.CssClass))
225 return String.Empty;
228 string ret = viewstate["CssClass"] as string;
229 if (ret == null)
230 return String.Empty;
232 return ret;
235 set
237 viewstate["CssClass"] = value;
238 SetBit ((int) Styles.CssClass);
242 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
243 [NotifyParentProperty(true)]
244 [WebSysDescription ("")]
245 [WebCategory ("Appearance")]
246 public FontInfo Font
248 get
250 if (fontinfo == null)
252 fontinfo = new FontInfo(this);
254 return fontinfo;
258 #if !NET_2_0
259 [Bindable(true)]
260 #endif
261 [DefaultValue(typeof (Color), "")]
262 [NotifyParentProperty(true)]
263 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
264 [WebSysDescription ("")]
265 [WebCategory ("Appearance")]
266 public Color ForeColor
268 get
270 if (!CheckBit ((int) Styles.ForeColor))
272 return Color.Empty;
275 return (Color)viewstate["ForeColor"];
278 set
280 viewstate["ForeColor"] = value;
281 SetBit ((int) Styles.ForeColor);
285 #if !NET_2_0
286 [Bindable(true)]
287 #endif
288 [DefaultValue(typeof (Unit), "")]
289 [NotifyParentProperty(true)]
290 [WebSysDescription ("")]
291 [WebCategory ("Appearance")]
292 public Unit Height
294 get
296 if (!CheckBit ((int) Styles.Height))
298 return Unit.Empty;
301 return (Unit)viewstate["Height"];
304 set
306 if (value.Value < 0)
308 throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
311 viewstate["Height"] = value;
312 SetBit ((int) Styles.Height);
316 #if !NET_2_0
317 [Bindable(true)]
318 #endif
319 [DefaultValue(typeof (Unit), "")]
320 [NotifyParentProperty(true)]
321 [WebSysDescription ("")]
322 [WebCategory ("Appearance")]
323 public Unit Width
325 get
327 if (!CheckBit ((int) Styles.Width))
329 return Unit.Empty;
332 return (Unit)viewstate["Width"];
335 set
337 if (value.Value < 0)
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
349 #if NET_2_0
350 [Browsable (false)]
351 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
352 public virtual bool IsEmpty
353 #else
354 protected internal virtual bool IsEmpty
355 #endif
357 get
359 #if NET_2_0
360 return (styles == 0 && RegisteredCssClass.Length == 0);
361 #else
362 return (styles == 0);
363 #endif
367 protected bool IsTrackingViewState
369 get
371 return tracking;
375 [Browsable(false)]
376 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
377 protected internal StateBag ViewState
379 get
381 return viewstate;
384 #endregion // Protected Instance Properties
386 #region Internal Instance Properties
387 internal bool AlwaysRenderTextDecoration
391 if (viewstate["AlwaysRenderTextDecoration"] == null)
392 return false;
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)
411 #if NET_2_0
412 if (RegisteredCssClass.Length > 0) {
413 string cssclass = CssClass;
414 if (!String.IsNullOrEmpty (cssclass))
415 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass + " " + RegisteredCssClass);
416 else
417 writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
419 else
420 #endif
422 string cssclass = CssClass;
423 if (cssclass != null && cssclass.Length > 0)
424 writer.AddAttribute (HtmlTextWriterAttribute.Class, cssclass);
425 #if NET_2_0
426 CssStyleCollection col = new CssStyleCollection ();
427 FillStyleAttributes (col, owner);
428 foreach (string key in col.Keys) {
429 writer.AddStyleAttribute (key, col [key]);
431 #else
432 WriteStyleAttributes (writer);
433 #endif
437 #if ONLY_1_1
438 void WriteStyleAttributes (HtmlTextWriter writer)
440 string s;
441 Color color;
442 BorderStyle bs;
443 Unit u;
445 if (CheckBit ((int) Styles.BackColor)) {
446 color = (Color)viewstate["BackColor"];
447 if (!color.IsEmpty)
448 writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
451 if (CheckBit ((int) Styles.BorderColor)) {
452 color = (Color)viewstate["BorderColor"];
453 if (!color.IsEmpty)
454 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
457 bool have_width = false;
458 if (CheckBit ((int) Styles.BorderWidth)) {
459 u = (Unit)viewstate["BorderWidth"];
460 if (!u.IsEmpty) {
461 if (u.Value > 0)
462 have_width = true;
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());
471 else {
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"];
481 if (!color.IsEmpty)
482 writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
485 if (CheckBit ((int) Styles.Height)) {
486 u = (Unit)viewstate["Height"];
487 if (!u.IsEmpty)
488 writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
491 if (CheckBit ((int) Styles.Width)) {
492 u = (Unit)viewstate["Width"];
493 if (!u.IsEmpty)
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) {
501 s = font.Names[0];
502 for (int i = 1; i < font.Names.Length; i++)
503 s += "," + font.Names[i];
504 writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
507 if (font.Bold)
508 writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
510 if (font.Italic)
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
517 s = string.Empty;
519 if (font.Overline)
520 s += "overline ";
522 if (font.Strikeout)
523 s += "line-through ";
525 if (font.Underline)
526 s += "underline ";
528 s = (s != "") ? s : AlwaysRenderTextDecoration ? "none" : "";
529 if (s != "")
530 writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
533 #endif
535 #if NET_2_0
536 protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
538 Color color;
539 BorderStyle bs;
540 Unit u;
542 if (CheckBit ((int) Styles.BackColor))
544 color = (Color)viewstate["BackColor"];
545 if (!color.IsEmpty)
546 attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
549 if (CheckBit ((int) Styles.BorderColor))
551 color = (Color)viewstate["BorderColor"];
552 if (!color.IsEmpty)
553 attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
556 bool have_width = false;
557 if (CheckBit ((int) Styles.BorderWidth)) {
558 u = (Unit) viewstate ["BorderWidth"];
559 if (!u.IsEmpty) {
560 if (u.Value > 0)
561 have_width = true;
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 ());
570 else if (have_width)
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"];
580 if (!color.IsEmpty)
581 attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
584 if (CheckBit ((int) Styles.Height))
586 u = (Unit)viewstate["Height"];
587 if (!u.IsEmpty)
588 attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
591 if (CheckBit ((int) Styles.Width))
593 u = (Unit)viewstate["Width"];
594 if (!u.IsEmpty)
595 attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
598 Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
600 #endif
602 public virtual void CopyFrom(Style s)
604 if ((s == null) || s.IsEmpty)
606 return;
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))
652 return;
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)
720 fontinfo.Reset();
722 styles = 0;
723 viewstate.Remove (BitStateKey);
724 stylesTraked = 0;
726 #if ONLY_1_1
727 public override string ToString()
729 return string.Empty;
731 #endif
732 #endregion // Public Instance Methods
734 #region Protected Instance Methods
735 protected internal void LoadViewState(object state)
737 viewstate.LoadViewState(state);
739 LoadBitState ();
742 protected internal virtual object SaveViewState ()
744 SaveBitState ();
746 if (_isSharedViewState)
747 return null;
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)
761 return;
763 int bit = (int) viewstate [BitStateKey];
764 styles |= bit;
765 stylesTraked |= bit;
768 protected internal virtual void SetBit( int bit )
770 styles |= bit;
771 if (tracking)
772 stylesTraked |= bit;
775 internal void RemoveBit (int bit) {
776 styles &= ~bit;
777 if (tracking) {
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 ()
790 tracking = true;
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()
808 TrackViewState();
811 bool IStateManager.IsTrackingViewState
813 get
815 return this.IsTrackingViewState;
818 #endregion // IStateManager Properties & Methods
820 #if NET_2_0
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);
830 return col;
833 [EditorBrowsable(EditorBrowsableState.Advanced)]
834 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
835 [Browsable(false)]
836 public string RegisteredCssClass {
837 get {
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;
856 fontinfo = null;
859 internal void AddCssClass (string cssClass) {
860 if (String.IsNullOrEmpty (cssClass))
861 return;
863 if (CssClass.Length > 0)
864 CssClass += " ";
865 CssClass += cssClass;
868 public void SetDirty ()
870 if (viewstate != null)
871 viewstate.SetDirty (true);
872 stylesTraked = styles;
874 #endif