Merge pull request #396 from directhex/master
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / StyleBlock.cs
1 //
2 // Authors:
3 //      Marek Habersack <grendel@twistedcode.net>
4 //
5 // (C) 2010 Novell, Inc (http://novell.com)
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Web.UI;
31
32 namespace System.Web.UI.WebControls
33 {
34         sealed class StyleBlock : Control
35         {
36                 List <NamedCssStyleCollection> cssStyles;
37                 Dictionary <string, NamedCssStyleCollection> cssStyleIndex;
38                 string stylePrefix;
39                 
40                 List <NamedCssStyleCollection> CssStyles {
41                         get {
42                                 if (cssStyles == null) {
43                                         cssStyles = new List <NamedCssStyleCollection> ();
44                                         cssStyleIndex = new Dictionary <string, NamedCssStyleCollection> (StringComparer.Ordinal);
45                                 }
46                                 
47                                 return cssStyles;
48                         }
49                 }
50                 
51                 public StyleBlock (string stylePrefix)
52                 {
53                         if (String.IsNullOrEmpty (stylePrefix))
54                                 throw new ArgumentNullException ("stylePrefix");
55
56                         this.stylePrefix = stylePrefix;
57                 }
58
59                 public NamedCssStyleCollection RegisterStyle (string name = null)
60                 {
61                         if (name == null)
62                                 name = String.Empty;
63
64                         return GetStyle (name);
65                 }
66                 
67                 public NamedCssStyleCollection RegisterStyle (Style style, string name = null)
68                 {
69                         if (style == null)
70                                 throw new ArgumentNullException ("style");
71                         
72                         if (name == null)
73                                 name = String.Empty;
74
75                         NamedCssStyleCollection cssStyle = GetStyle (name);
76                         cssStyle.CopyFrom (style.GetStyleAttributes (null));
77
78                         return cssStyle;
79                 }
80
81                 public NamedCssStyleCollection RegisterStyle (HtmlTextWriterStyle key, string value, string styleName = null)
82                 {
83                         if (styleName == null)
84                                 styleName = String.Empty;
85
86                         NamedCssStyleCollection style = GetStyle (styleName);
87                         style.Add (key, value);
88
89                         return style;
90                 }
91                 
92                 NamedCssStyleCollection GetStyle (string name)
93                 {
94                         List <NamedCssStyleCollection> cssStyles = CssStyles;
95                         NamedCssStyleCollection style;
96
97                         if (!cssStyleIndex.TryGetValue (name, out style)) {
98                                 style = new NamedCssStyleCollection (name);
99                                 cssStyleIndex.Add (name, style);
100                                 cssStyles.Add (style);
101                         }
102
103                         if (style == null)
104                                 throw new InvalidOperationException (String.Format ("Internal error. Stylesheet for style {0} is null.", name));
105                         
106                         return style;
107                 }
108                 
109                 protected internal override void Render (HtmlTextWriter writer)
110                 {
111                         if (cssStyles == null || cssStyles.Count == 0)
112                                 return;
113
114                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "text/css");
115                         writer.RenderBeginTag (HtmlTextWriterTag.Style);
116                         writer.WriteLine ("/* <![CDATA[ */");
117
118                         string name, value;
119                         foreach (var css in cssStyles) {
120                                 value = css.Collection.Value;
121                                 if (String.IsNullOrEmpty (value))
122                                         continue;
123                                 
124                                 name = css.Name;
125                                 if (name != String.Empty)
126                                         name = name + " ";
127                                 
128                                 writer.WriteLine ("#{0} {1}{{ {2} }}", stylePrefix, name, value);
129                         }
130                         
131                         writer.WriteLine ("/* ]]> */");
132                         writer.RenderEndTag (); // </style>
133                 }
134         }
135 }